Snaparazzi/Assets/Scripts/QuestionHandler.cs

99 lines
3.6 KiB
C#
Raw Permalink Normal View History

2024-01-28 01:40:12 +00:00
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
2024-01-28 01:40:12 +00:00
public class QuestionHandler : MonoBehaviour
{
public GameObject managers;
public Button submitButton;
2024-01-28 01:40:12 +00:00
public TextMeshProUGUI explainText;
public PromptList promptList;
private GameManager gameManager;
2024-02-04 12:02:58 +00:00
private List<Question> questionsToAnswer = new List<Question>();
private int activeAnsweredQuestionIndex = 0;
2024-01-28 01:40:12 +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";
gameManager = managers.GetComponent<GameManager>();
questionsToAnswer = gameManager.myRoom.GetQuestionsByPlayer(gameManager.currentPlayer);
Redraw(activeAnsweredQuestionIndex);
2024-01-28 01:40:12 +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)
{
Prompt prompt = promptList.prompts.Find(x => x.id == questionsToAnswer[currentQuestion].promptId);
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();
}
/// <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
{
return questionsToAnswer[activeAnsweredQuestionIndex].propositions.First(x => x.Value.owner.id == _playerId).Key;
2024-01-28 01:40:12 +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()
{
submitButton.interactable = false;
submitButton.GetComponent<TextMeshProUGUI>().text = "Uploading...";
StorageManager storageManager = managers.GetComponent<StorageManager>();
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<TextMeshProUGUI>().text = "Submit";
activeAnsweredQuestionIndex = SecondQuestionIndex;
Redraw(activeAnsweredQuestionIndex);
}
});
}
2024-01-28 01:40:12 +00:00
}
}