2024-01-28 01:40:12 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using TMPro;
|
|
|
|
using UnityEngine;
|
2024-01-29 21:10:52 +00:00
|
|
|
using UnityEngine.UI;
|
2024-01-28 01:40:12 +00:00
|
|
|
|
|
|
|
public class QuestionHandler : MonoBehaviour
|
|
|
|
{
|
|
|
|
public GameObject managers;
|
2024-01-29 21:10:52 +00:00
|
|
|
public Button submitButton;
|
2024-01-28 01:40:12 +00:00
|
|
|
public TextMeshProUGUI explainText;
|
|
|
|
public PromptList promptList;
|
2024-01-29 21:10:52 +00:00
|
|
|
|
|
|
|
private GameManager gameManager;
|
2024-02-04 12:02:58 +00:00
|
|
|
private List<Question> questionsToAnswer = new List<Question>();
|
2024-01-31 21:34:50 +00:00
|
|
|
private int activeAnsweredQuestionIndex = 0;
|
2024-01-28 01:40:12 +00:00
|
|
|
|
2024-01-31 21:34:50 +00:00
|
|
|
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>
|
2024-01-28 01:40:12 +00:00
|
|
|
void OnEnable()
|
|
|
|
{
|
2024-02-04 12:02:58 +00:00
|
|
|
activeAnsweredQuestionIndex = FirstQuestionIndex;
|
|
|
|
submitButton.interactable = true;
|
|
|
|
submitButton.GetComponent<TextMeshProUGUI>().text = "Submit";
|
2024-01-28 15:28:04 +00:00
|
|
|
gameManager = managers.GetComponent<GameManager>();
|
2024-01-31 21:34:50 +00:00
|
|
|
questionsToAnswer = gameManager.myRoom.GetQuestionsByPlayer(gameManager.currentPlayer);
|
|
|
|
Redraw(activeAnsweredQuestionIndex);
|
2024-01-28 01:40:12 +00:00
|
|
|
}
|
|
|
|
|
2024-01-31 21:34:50 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Redraws the UI with the information from the current question.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="currentQuestion">The index of the current question to display.</param>
|
2024-01-28 01:40:12 +00:00
|
|
|
void Redraw(int currentQuestion)
|
|
|
|
{
|
2024-01-31 21:34:50 +00:00
|
|
|
Prompt prompt = promptList.prompts.Find(x => x.id == questionsToAnswer[currentQuestion].promptId);
|
2024-02-29 20:34:05 +00:00
|
|
|
explainText.SetText(gameManager.myRoom.promptsLanguage == "en" ? prompt.en : prompt.fr);
|
2024-01-28 01:40:12 +00:00
|
|
|
|
|
|
|
CameraManager cameraManager = gameObject.GetComponent<CameraManager>();
|
|
|
|
cameraManager.WebcamResume();
|
|
|
|
}
|
|
|
|
|
2024-01-31 21:34:50 +00:00
|
|
|
/// <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)
|
2024-01-28 01:40:12 +00:00
|
|
|
{
|
2024-01-31 21:34:50 +00:00
|
|
|
return questionsToAnswer[activeAnsweredQuestionIndex].propositions.First(x => x.Value.owner.id == _playerId).Key;
|
2024-01-28 01:40:12 +00:00
|
|
|
}
|
|
|
|
|
2024-01-31 21:34:50 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Handles the click event of the submit button.
|
|
|
|
/// Uploads a photo and manages the UI accordingly.
|
|
|
|
/// </summary>
|
2024-01-28 01:40:12 +00:00
|
|
|
public void OnSubmitButton()
|
|
|
|
{
|
2024-01-29 21:10:52 +00:00
|
|
|
submitButton.interactable = false;
|
|
|
|
submitButton.GetComponent<TextMeshProUGUI>().text = "Uploading...";
|
|
|
|
|
2024-01-31 21:34:50 +00:00
|
|
|
StorageManager storageManager = managers.GetComponent<StorageManager>();
|
|
|
|
if (storageManager != null)
|
|
|
|
{
|
|
|
|
storageManager.UploadPhoto(
|
|
|
|
gameManager.myRoom.code,
|
|
|
|
gameManager.currentPlayer.id,
|
|
|
|
questionsToAnswer[activeAnsweredQuestionIndex].index,
|
|
|
|
GetPropositionIndex(gameManager.currentPlayer.id),
|
|
|
|
succeed =>
|
2024-01-29 21:10:52 +00:00
|
|
|
{
|
2024-01-31 21:34:50 +00:00
|
|
|
if (!succeed)
|
|
|
|
{
|
|
|
|
Debug.LogError("Photo upload failed. Please do something.", this);
|
|
|
|
return;
|
|
|
|
}
|
2024-01-28 15:28:04 +00:00
|
|
|
|
2024-01-31 21:34:50 +00:00
|
|
|
if (activeAnsweredQuestionIndex == SecondQuestionIndex)
|
|
|
|
{
|
|
|
|
gameManager.WaitForPlayers();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
submitButton.interactable = true;
|
|
|
|
submitButton.GetComponent<TextMeshProUGUI>().text = "Submit";
|
|
|
|
activeAnsweredQuestionIndex = SecondQuestionIndex;
|
|
|
|
Redraw(activeAnsweredQuestionIndex);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2024-01-28 01:40:12 +00:00
|
|
|
}
|
|
|
|
}
|