using System.Collections.Generic; using System.Linq; using TMPro; using UnityEngine; using UnityEngine.UI; public class QuestionHandler : MonoBehaviour { public GameObject managers; public Button submitButton; public TextMeshProUGUI explainText; public PromptList promptList; private GameManager gameManager; 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(); questionsToAnswer = gameManager.myRoom.GetQuestionsByPlayer(gameManager.currentPlayer); Redraw(activeAnsweredQuestionIndex); } /// /// Called when the script is disabled. /// Cleans up resources and resets the activeAnsweredQuestionIndex. /// void OnDisable() { 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 == questionsToAnswer[currentQuestion].promptId); explainText.SetText(prompt.en); CameraManager cameraManager = gameObject.GetComponent(); cameraManager.WebcamResume(); } /// /// 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 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..."; StorageManager storageManager = managers.GetComponent(); if (storageManager != null) { storageManager.UploadPhoto( gameManager.myRoom.code, gameManager.currentPlayer.id, questionsToAnswer[activeAnsweredQuestionIndex].index, GetPropositionIndex(gameManager.currentPlayer.id), succeed => { 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); } }); } } }