diff --git a/Assets/Scripts/DatabaseClasses/Room.cs b/Assets/Scripts/DatabaseClasses/Room.cs index be080be..dfe1213 100644 --- a/Assets/Scripts/DatabaseClasses/Room.cs +++ b/Assets/Scripts/DatabaseClasses/Room.cs @@ -76,7 +76,12 @@ public void SetPlayersAreReady(int _state) currentState = _state; } - public List GetPropositionsByPlayer(Player player) + /// + /// Return all the propositions linked to a specific player. + /// + /// + /// + public List GetPropositionsForPlayer(Player player) { List playerPropositions = new(); diff --git a/Assets/Scripts/QuestionHandler.cs b/Assets/Scripts/QuestionHandler.cs index f3f24be..2aeae02 100644 --- a/Assets/Scripts/QuestionHandler.cs +++ b/Assets/Scripts/QuestionHandler.cs @@ -12,69 +12,94 @@ public class QuestionHandler : MonoBehaviour public PromptList promptList; private GameManager gameManager; - private List player2questions; - private int currentQuestion = 0; + private List questionsToAnswer; + private int activeAnsweredQuestionIndex = 0; + private const int FirstQuestionIndex = 0; + private const int SecondQuestionIndex = 1; + + + /// + /// Called when the script is enabled. + /// Initializes the GameManager and questionsToAnswer list, and updates the UI. + /// void OnEnable() { gameManager = managers.GetComponent(); - player2questions = gameManager.myRoom.GetQuestionsByPlayer(gameManager.currentPlayer); - Redraw(currentQuestion); + questionsToAnswer = gameManager.myRoom.GetQuestionsByPlayer(gameManager.currentPlayer); + Redraw(activeAnsweredQuestionIndex); } + /// + /// Called when the script is disabled. + /// Cleans up resources and resets the activeAnsweredQuestionIndex. + /// void OnDisable() { - player2questions = null; - currentQuestion = 0; + questionsToAnswer = null; + activeAnsweredQuestionIndex = FirstQuestionIndex; } + /// + /// Redraws the UI with the information from the current question. + /// + /// The index of the current question to display. 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.WebcamResume(); } - int GetPropRef(Player player) + /// + /// Gets the index of the proposition owned by a player for the current question. + /// + /// The player's ID for whom the proposition index is retrieved. + /// The index of the proposition for the player. + 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; } + /// + /// Handles the click event of the submit button. + /// Uploads a photo and manages the UI accordingly. + /// public void OnSubmitButton() { submitButton.interactable = false; submitButton.GetComponent().text = "Uploading..."; - managers.GetComponent().UploadPhoto( - gameManager.myRoom.code, - gameManager.currentPlayer.id, - currentQuestion, - GetPropRef(gameManager.currentPlayer), - succeed => - { - if(!succeed) + StorageManager storageManager = managers.GetComponent(); + 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().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().text = "Submit"; + activeAnsweredQuestionIndex = SecondQuestionIndex; + Redraw(activeAnsweredQuestionIndex); + } + }); + } } - } diff --git a/Assets/Scripts/WaitForPropositionsPage.cs b/Assets/Scripts/WaitForPropositionsPage.cs index 73f2903..77e689a 100644 --- a/Assets/Scripts/WaitForPropositionsPage.cs +++ b/Assets/Scripts/WaitForPropositionsPage.cs @@ -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)