fix: the correct questions should be sent to the correct players

This commit is contained in:
Fangh 2024-01-31 22:34:50 +01:00
parent 7e4416824e
commit 8c825b0d0c
3 changed files with 68 additions and 38 deletions

View File

@ -76,7 +76,12 @@ public void SetPlayersAreReady(int _state)
currentState = _state;
}
public List<Proposition> GetPropositionsByPlayer(Player player)
/// <summary>
/// Return all the propositions linked to a specific player.
/// </summary>
/// <param name="player"></param>
/// <returns></returns>
public List<Proposition> GetPropositionsForPlayer(Player player)
{
List<Proposition> playerPropositions = new();

View File

@ -12,69 +12,94 @@ public class QuestionHandler : MonoBehaviour
public PromptList promptList;
private GameManager gameManager;
private List<Question> player2questions;
private int currentQuestion = 0;
private List<Question> questionsToAnswer;
private int activeAnsweredQuestionIndex = 0;
private const int FirstQuestionIndex = 0;
private const int SecondQuestionIndex = 1;
/// <summary>
/// Called when the script is enabled.
/// Initializes the GameManager and questionsToAnswer list, and updates the UI.
/// </summary>
void OnEnable()
{
gameManager = managers.GetComponent<GameManager>();
player2questions = gameManager.myRoom.GetQuestionsByPlayer(gameManager.currentPlayer);
Redraw(currentQuestion);
questionsToAnswer = gameManager.myRoom.GetQuestionsByPlayer(gameManager.currentPlayer);
Redraw(activeAnsweredQuestionIndex);
}
/// <summary>
/// Called when the script is disabled.
/// Cleans up resources and resets the activeAnsweredQuestionIndex.
/// </summary>
void OnDisable()
{
player2questions = null;
currentQuestion = 0;
questionsToAnswer = null;
activeAnsweredQuestionIndex = FirstQuestionIndex;
}
/// <summary>
/// Redraws the UI with the information from the current question.
/// </summary>
/// <param name="currentQuestion">The index of the current question to display.</param>
void Redraw(int currentQuestion)
{
Prompt prompt = promptList.prompts.Find(x => x.id == player2questions[currentQuestion].promptId);
//Debug.Log(JsonUtility.ToJson(prompt));
Prompt prompt = promptList.prompts.Find(x => x.id == questionsToAnswer[currentQuestion].promptId);
explainText.SetText(prompt.en);
CameraManager cameraManager = gameObject.GetComponent<CameraManager>();
cameraManager.WebcamResume();
}
int GetPropRef(Player player)
/// <summary>
/// Gets the index of the proposition owned by a player for the current question.
/// </summary>
/// <param name="_playerId">The player's ID for whom the proposition index is retrieved.</param>
/// <returns>The index of the proposition for the player.</returns>
int GetPropositionIndex(string _playerId)
{
return gameManager.myRoom.questions[currentQuestion].propositions.First(x => x.Value.owner.id == player.id).Key;
return questionsToAnswer[activeAnsweredQuestionIndex].propositions.First(x => x.Value.owner.id == _playerId).Key;
}
/// <summary>
/// Handles the click event of the submit button.
/// Uploads a photo and manages the UI accordingly.
/// </summary>
public void OnSubmitButton()
{
submitButton.interactable = false;
submitButton.GetComponent<TextMeshProUGUI>().text = "Uploading...";
managers.GetComponent<StorageManager>().UploadPhoto(
gameManager.myRoom.code,
gameManager.currentPlayer.id,
currentQuestion,
GetPropRef(gameManager.currentPlayer),
succeed =>
{
if(!succeed)
StorageManager storageManager = managers.GetComponent<StorageManager>();
if (storageManager != null)
{
storageManager.UploadPhoto(
gameManager.myRoom.code,
gameManager.currentPlayer.id,
questionsToAnswer[activeAnsweredQuestionIndex].index,
GetPropositionIndex(gameManager.currentPlayer.id),
succeed =>
{
Debug.LogError("Photo upload failed. please do something", this);
return;
}
if (currentQuestion == 1)
{
gameManager.WaitForPlayers();
return;
}
else
{
submitButton.interactable = true;
submitButton.GetComponent<TextMeshProUGUI>().text = "Submit";
currentQuestion++;
Redraw(currentQuestion);
}
});
if (!succeed)
{
Debug.LogError("Photo upload failed. Please do something.", this);
return;
}
if (activeAnsweredQuestionIndex == SecondQuestionIndex)
{
gameManager.WaitForPlayers();
}
else
{
submitButton.interactable = true;
submitButton.GetComponent<TextMeshProUGUI>().text = "Submit";
activeAnsweredQuestionIndex = SecondQuestionIndex;
Redraw(activeAnsweredQuestionIndex);
}
});
}
}
}

View File

@ -81,7 +81,7 @@ private void CheckPlayersWhoHaveProposed()
continue;
bool playerHasAnsweredBothProposition = true;
Proposition[] propositions = myRoom.GetPropositionsByPlayer(player).ToArray();
Proposition[] propositions = myRoom.GetPropositionsForPlayer(player).ToArray();
//Debug.Log($"I found {propositions.Length} propositions for {player.name}", this);
if (propositions.Length < 2)