Snaparazzi/Assets/Scripts/VotingPage.cs

170 lines
5.5 KiB
C#
Raw Normal View History

2024-01-28 12:18:40 +00:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
2024-01-28 12:18:40 +00:00
using Firebase.Database;
using TMPro;
using UnityEngine;
public class VotingPage : MonoBehaviour
{
[Header("Scene References")]
public PropositionFrame proposition1;
public PropositionFrame proposition2;
public TextMeshProUGUI currentPromptLabel;
public TextMeshProUGUI timerLabel;
[Header("Asset References")]
public PromptList promptList;
[Header("Settings")]
public float votingTime = 20;
private Queue<Question> remainingQuestions = new Queue<Question>();
private Question currentQuestion = null;
private DatabaseReference roomRef;
private Dictionary<string, Player> currentPlayers; //all the current playing players
private DateTime endOfTimer = DateTime.MinValue;
private Action OnVoteEnded;
private void Update()
{
//there is no question displayed or timer is not started
if (currentQuestion == null || endOfTimer == DateTime.MinValue)
return;
2024-01-28 15:34:06 +00:00
if (DateTime.Now < endOfTimer)
{
2024-01-28 17:12:05 +00:00
TimeSpan duration = endOfTimer - DateTime.Now;
timerLabel.text = ((int)duration.TotalSeconds).ToString("D2");
2024-01-28 15:34:06 +00:00
}
else
2024-01-28 12:18:40 +00:00
{
timerLabel.text = "0";
if (remainingQuestions.Count > 0)
{
currentQuestion = remainingQuestions.Dequeue();
ShowQuestion();
}
else
{
OnVoteEnded?.Invoke();
OnVoteEnded = null;
gameObject.SetActive(false);
2024-01-28 12:18:40 +00:00
}
}
}
// Start is called before the first frame update
2024-01-28 14:42:32 +00:00
public void ShowVotingPage(DatabaseReference _roomRef, Dictionary<string, Player> _currentPlayers, Dictionary<int, Question> _questions, Action _callback_OnVoteEnded)
2024-01-28 12:18:40 +00:00
{
Debug.Log("Initializing voting page");
this.gameObject.SetActive(true);
roomRef = _roomRef;
currentPlayers = _currentPlayers;
OnVoteEnded = _callback_OnVoteEnded;
Question[] _questionArray = _questions.Values.ToArray();
for (int i = 0; i < _questionArray.Length; i++)
2024-01-28 12:18:40 +00:00
{
remainingQuestions.Enqueue(_questionArray[i]);
roomRef.Child("questions").Child(i.ToString()).ValueChanged += OnQuestionChanged;
2024-01-28 12:18:40 +00:00
}
currentQuestion = remainingQuestions.Dequeue();
ShowQuestion();
}
/// <summary>
/// Display next question on screen
/// </summary>
2024-01-28 12:18:40 +00:00
void ShowQuestion()
{
roomRef.Child("currentQuestionId").SetValueAsync(currentQuestion.index);
2024-01-28 12:18:40 +00:00
Debug.Log($"Show Question {currentQuestion.promptId}", this);
Prompt currentPrompt = promptList.GetPromptById(currentQuestion.promptId);
currentPromptLabel.text = currentPrompt.en;
Debug.Log($"Prompt is {currentPromptLabel.text}", this);
2024-01-28 15:23:53 +00:00
if (currentQuestion.propositions[0] != null)
proposition1.Initialize(currentQuestion.propositions[0]);
else
{
Debug.Log("User has given no proposition", this);
//manage if there is no proposition
}
if (currentQuestion.propositions[1] != null)
proposition2.Initialize(currentQuestion.propositions[1]);
else
{
Debug.Log("User has given no proposition", this);
//manage if there is no proposition
}
2024-01-28 12:18:40 +00:00
endOfTimer = DateTime.Now.AddSeconds(votingTime);
}
/// <summary>
/// Is called when a question is updated (someone has voted)
/// </summary>
/// <param name="sender"></param>
/// <param name="_questionRef"></param>
private void OnQuestionChanged(object sender, ValueChangedEventArgs _questionRef)
{
2024-01-29 22:32:30 +00:00
//Debug.Log("a question value has changed, maybe someone has voted", this);
2024-01-28 12:18:40 +00:00
string JSON = _questionRef.Snapshot.GetRawJsonValue();
Question question = Newtonsoft.Json.JsonConvert.DeserializeObject<Question>(JSON);
//if someone succeeded to vote for another question than the one that is displayed => yeet
if (question.index != currentQuestion.index)
2024-01-28 12:18:40 +00:00
return;
2024-01-29 22:32:30 +00:00
//Debug.Log($"someone has voted for {question.index}", this);
2024-01-28 12:18:40 +00:00
currentQuestion.propositions = question.propositions;
UpdateVoters();
}
private void UpdateVoters()
{
2024-01-28 17:12:05 +00:00
if (currentQuestion.propositions.Count == 0) //manage if there is no propositons
return;
2024-01-28 15:34:06 +00:00
if (currentQuestion.propositions[0] != null)
2024-01-28 12:18:40 +00:00
{
2024-01-28 15:34:06 +00:00
List<Player> playersThatHasVotedForFirstProposition = new List<Player>();
2024-01-28 15:52:13 +00:00
if (currentQuestion.propositions[0].voters != null)
2024-01-28 15:34:06 +00:00
{
2024-01-28 15:52:13 +00:00
foreach (string playerId in currentQuestion.propositions[0].voters)
{
playersThatHasVotedForFirstProposition.Add(currentPlayers[playerId]);
}
proposition1.UpdateVoters(playersThatHasVotedForFirstProposition);
2024-01-28 15:34:06 +00:00
}
2024-01-28 12:18:40 +00:00
}
2024-01-28 15:34:06 +00:00
if (currentQuestion.propositions[1] != null)
2024-01-28 12:18:40 +00:00
{
2024-01-28 15:34:06 +00:00
List<Player> playersThatHasVotedForSecondProposition = new List<Player>();
2024-01-28 16:54:19 +00:00
if (currentQuestion.propositions[1].voters != null)
2024-01-28 15:34:06 +00:00
{
2024-01-28 15:52:13 +00:00
foreach (string playerId in currentQuestion.propositions[1].voters)
{
playersThatHasVotedForSecondProposition.Add(currentPlayers[playerId]);
}
proposition2.UpdateVoters(playersThatHasVotedForSecondProposition);
2024-01-28 15:34:06 +00:00
}
2024-01-28 12:18:40 +00:00
}
}
}