132 lines
4.3 KiB
C#
132 lines
4.3 KiB
C#
|
using System;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
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;
|
||
|
|
||
|
|
||
|
if (endOfTimer <= DateTime.Now)
|
||
|
{
|
||
|
timerLabel.text = "0";
|
||
|
if (remainingQuestions.Count > 0)
|
||
|
{
|
||
|
currentQuestion = remainingQuestions.Dequeue();
|
||
|
ShowQuestion();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
OnVoteEnded?.Invoke();
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
timerLabel.text = (DateTime.Now - endOfTimer).ToString("D2");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
// Start is called before the first frame update
|
||
|
public void ShowVotingPage(DatabaseReference _roomRef, Dictionary<string, Player> _currentPlayers, Dictionary<string, Question> _questions, Action _callback_OnVoteEnded)
|
||
|
{
|
||
|
Debug.Log("Initializing voting page");
|
||
|
this.gameObject.SetActive(true);
|
||
|
roomRef = _roomRef;
|
||
|
currentPlayers = _currentPlayers;
|
||
|
OnVoteEnded = _callback_OnVoteEnded;
|
||
|
|
||
|
foreach (var QuestionByID in _questions)
|
||
|
{
|
||
|
remainingQuestions.Enqueue(QuestionByID.Value);
|
||
|
roomRef.Child("questions").Child(QuestionByID.Key).ValueChanged += OnQuestionChanged;
|
||
|
}
|
||
|
|
||
|
currentQuestion = remainingQuestions.Dequeue();
|
||
|
roomRef.Child("currentQuestion").SetValueAsync(currentQuestion.id);
|
||
|
ShowQuestion();
|
||
|
}
|
||
|
|
||
|
void ShowQuestion()
|
||
|
{
|
||
|
Debug.Log($"Show Question {currentQuestion.promptId}", this);
|
||
|
Prompt currentPrompt = promptList.GetPromptById(currentQuestion.promptId);
|
||
|
currentPromptLabel.text = currentPrompt.en;
|
||
|
|
||
|
Debug.Log($"Prompt is {currentPromptLabel.text}", this);
|
||
|
|
||
|
proposition1.Initialize(currentQuestion.GetFirstProposition());
|
||
|
proposition1.Initialize(currentQuestion.GetSecondProposition());
|
||
|
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)
|
||
|
{
|
||
|
Debug.Log("a question value has changed, maybe someone has voted", this);
|
||
|
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.id != currentQuestion.id)
|
||
|
return;
|
||
|
|
||
|
Debug.Log($"someone has voted for {question.id}", this);
|
||
|
currentQuestion.propositions = question.propositions;
|
||
|
UpdateVoters();
|
||
|
|
||
|
}
|
||
|
|
||
|
private void UpdateVoters()
|
||
|
{
|
||
|
List<Player> playersThatHasVotedForFirstProposition = new List<Player>();
|
||
|
foreach (string playerId in currentQuestion.GetFirstProposition().voters)
|
||
|
{
|
||
|
playersThatHasVotedForFirstProposition.Add(currentPlayers[playerId]);
|
||
|
}
|
||
|
|
||
|
List<Player> playersThatHasVotedForSecondProposition = new List<Player>();
|
||
|
foreach (string playerId in currentQuestion.GetSecondProposition().voters)
|
||
|
{
|
||
|
playersThatHasVotedForSecondProposition.Add(currentPlayers[playerId]);
|
||
|
}
|
||
|
|
||
|
proposition1.UpdateVoters(playersThatHasVotedForFirstProposition);
|
||
|
proposition2.UpdateVoters(playersThatHasVotedForSecondProposition);
|
||
|
}
|
||
|
}
|