2024-01-28 12:18:40 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2024-01-28 14:52:05 +00:00
|
|
|
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;
|
2024-02-01 21:38:34 +00:00
|
|
|
private Dictionary<string, Player> currentPlayers; // All the current playing players
|
2024-01-28 12:18:40 +00:00
|
|
|
|
|
|
|
private DateTime endOfTimer = DateTime.MinValue;
|
|
|
|
|
|
|
|
private Action OnVoteEnded;
|
|
|
|
|
2024-02-01 21:38:34 +00:00
|
|
|
private bool allPlayersHaveVotedForCurrentQuestion = false;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Is the number of questions for this game sessions
|
|
|
|
/// </summary>
|
|
|
|
private int numberOfQuestions = 0;
|
2024-01-28 12:18:40 +00:00
|
|
|
|
|
|
|
private void Update()
|
|
|
|
{
|
2024-02-01 21:38:34 +00:00
|
|
|
// If there is no question displayed or the timer is not started, return early.
|
2024-01-28 12:18:40 +00:00
|
|
|
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-02-01 21:38:34 +00:00
|
|
|
|
2024-02-01 21:38:58 +00:00
|
|
|
if (allPlayersHaveVotedForCurrentQuestion && endOfTimer > DateTime.Now.AddSeconds(4))
|
2024-02-01 21:38:34 +00:00
|
|
|
{
|
2024-02-01 21:38:58 +00:00
|
|
|
Debug.Log("All players have voted. So I put only 4sec to look at the winner.", this);
|
|
|
|
endOfTimer = DateTime.Now.AddSeconds(4);
|
2024-02-01 21:38:34 +00:00
|
|
|
}
|
2024-01-28 15:34:06 +00:00
|
|
|
}
|
|
|
|
else
|
2024-01-28 12:18:40 +00:00
|
|
|
{
|
2024-02-01 21:38:34 +00:00
|
|
|
EndCurrentQuestion();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// End current question and timer and go to next question (or end the vote if there is no more questions)
|
|
|
|
/// </summary>
|
|
|
|
private void EndCurrentQuestion()
|
|
|
|
{
|
|
|
|
timerLabel.text = "0";
|
|
|
|
if (remainingQuestions.Count > 0)
|
|
|
|
{
|
|
|
|
currentQuestion = remainingQuestions.Dequeue();
|
|
|
|
ShowNextQuestion();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
OnVoteEnded?.Invoke();
|
|
|
|
OnVoteEnded = null;
|
|
|
|
gameObject.SetActive(false);
|
|
|
|
|
|
|
|
for (int i = 0; i < numberOfQuestions; i++)
|
2024-01-28 12:18:40 +00:00
|
|
|
{
|
2024-02-01 21:38:34 +00:00
|
|
|
roomRef.Child("questions").Child(i.ToString()).ValueChanged -= OnQuestionDataUpdate;
|
2024-01-28 12:18:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-01 21:38:34 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes the voting page with necessary data, such as the room reference, current players,
|
|
|
|
/// questions, and a callback function for when the voting ends.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="_roomRef">Reference to the Firebase room database.</param>
|
|
|
|
/// <param name="_currentPlayers">Dictionary of current players.</param>
|
|
|
|
/// <param name="_questions">Dictionary of questions associated with their IDs.</param>
|
|
|
|
/// <param name="_callback_OnVoteEnded">Callback function to invoke when the voting ends.</param>
|
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");
|
|
|
|
roomRef = _roomRef;
|
|
|
|
currentPlayers = _currentPlayers;
|
|
|
|
OnVoteEnded = _callback_OnVoteEnded;
|
2024-02-04 11:42:57 +00:00
|
|
|
allPlayersHaveVotedForCurrentQuestion = false;
|
|
|
|
endOfTimer = DateTime.MinValue;
|
|
|
|
remainingQuestions.Clear();
|
|
|
|
currentQuestion = null;
|
2024-01-28 12:18:40 +00:00
|
|
|
|
2024-01-28 14:52:05 +00:00
|
|
|
Question[] _questionArray = _questions.Values.ToArray();
|
2024-02-01 21:38:34 +00:00
|
|
|
numberOfQuestions = _questionArray.Length;
|
2024-01-28 14:52:05 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < _questionArray.Length; i++)
|
2024-01-28 12:18:40 +00:00
|
|
|
{
|
2024-01-28 14:52:05 +00:00
|
|
|
remainingQuestions.Enqueue(_questionArray[i]);
|
2024-02-01 21:38:34 +00:00
|
|
|
roomRef.Child("questions").Child(i.ToString()).ValueChanged += OnQuestionDataUpdate;
|
2024-01-28 12:18:40 +00:00
|
|
|
}
|
|
|
|
|
2024-02-04 11:42:57 +00:00
|
|
|
gameObject.SetActive(true);
|
2024-01-28 12:18:40 +00:00
|
|
|
currentQuestion = remainingQuestions.Dequeue();
|
2024-02-01 21:38:34 +00:00
|
|
|
ShowNextQuestion();
|
2024-01-28 12:18:40 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 22:00:10 +00:00
|
|
|
/// <summary>
|
2024-02-01 21:38:34 +00:00
|
|
|
/// Displays the next question on the screen, updating the prompt, timer, and proposition frames.
|
2024-01-29 22:00:10 +00:00
|
|
|
/// </summary>
|
2024-02-01 21:38:34 +00:00
|
|
|
private void ShowNextQuestion()
|
2024-01-28 12:18:40 +00:00
|
|
|
{
|
2024-02-01 21:38:34 +00:00
|
|
|
// Set the current question ID in the Firebase room database.
|
2024-01-29 22:00:10 +00:00
|
|
|
roomRef.Child("currentQuestionId").SetValueAsync(currentQuestion.index);
|
|
|
|
|
2024-02-01 21:38:34 +00:00
|
|
|
// Update the prompt text based on the current question's prompt ID.
|
2024-01-28 12:18:40 +00:00
|
|
|
Debug.Log($"Show Question {currentQuestion.promptId}", this);
|
|
|
|
Prompt currentPrompt = promptList.GetPromptById(currentQuestion.promptId);
|
2024-02-29 20:34:05 +00:00
|
|
|
currentPromptLabel.text = RoomManager.Instance.myRoom.promptsLanguage == "en" ? currentPrompt.en : currentPrompt.fr;
|
2024-01-28 12:18:40 +00:00
|
|
|
|
|
|
|
Debug.Log($"Prompt is {currentPromptLabel.text}", this);
|
|
|
|
|
2024-02-01 21:38:34 +00:00
|
|
|
// Initialize the proposition frames with proposition data.
|
|
|
|
InitializeProposition(proposition1, currentQuestion.propositions[0]);
|
|
|
|
InitializeProposition(proposition2, currentQuestion.propositions[1]);
|
|
|
|
|
|
|
|
// Set the timer to end after the specified voting time.
|
|
|
|
endOfTimer = DateTime.Now.AddSeconds(votingTime);
|
|
|
|
|
|
|
|
//reset the count of voters
|
|
|
|
allPlayersHaveVotedForCurrentQuestion = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a proposition frame with the provided proposition data, updating it if available,
|
|
|
|
/// or handling the case when there is no proposition.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="proposition">The PropositionFrame to initialize or update.</param>
|
|
|
|
/// <param name="propositionData">The Proposition data to use for initialization or update.</param>
|
|
|
|
private void InitializeProposition(PropositionFrame proposition, Proposition propositionData)
|
|
|
|
{
|
|
|
|
if (propositionData != null)
|
2024-01-28 15:23:53 +00:00
|
|
|
{
|
2024-02-27 20:25:16 +00:00
|
|
|
proposition.SetProposition(propositionData);
|
2024-01-28 15:23:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Debug.Log("User has given no proposition", this);
|
2024-02-27 20:25:16 +00:00
|
|
|
proposition.SetNoProposition();
|
2024-01-28 15:23:53 +00:00
|
|
|
}
|
2024-01-28 12:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2024-02-01 21:38:34 +00:00
|
|
|
/// Handles updates to a question, such as when someone has voted for it.
|
2024-01-28 12:18:40 +00:00
|
|
|
/// </summary>
|
2024-02-01 21:38:34 +00:00
|
|
|
/// <param name="sender">The sender of the event.</param>
|
|
|
|
/// <param name="_questionRef">The ValueChangedEventArgs containing the updated question reference.</param>
|
|
|
|
private void OnQuestionDataUpdate(object sender, ValueChangedEventArgs _questionRef)
|
2024-01-28 12:18:40 +00:00
|
|
|
{
|
|
|
|
string JSON = _questionRef.Snapshot.GetRawJsonValue();
|
2024-02-01 21:38:34 +00:00
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(JSON))
|
|
|
|
return;
|
|
|
|
|
2024-01-28 12:18:40 +00:00
|
|
|
Question question = Newtonsoft.Json.JsonConvert.DeserializeObject<Question>(JSON);
|
|
|
|
|
2024-02-01 21:38:34 +00:00
|
|
|
// If someone succeeded in voting for another question than the one displayed, ignore it.
|
2024-01-28 14:52:05 +00:00
|
|
|
if (question.index != currentQuestion.index)
|
2024-01-28 12:18:40 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
currentQuestion.propositions = question.propositions;
|
2024-02-01 21:38:34 +00:00
|
|
|
|
|
|
|
if (currentQuestion.propositions.Count > 0)
|
|
|
|
{
|
|
|
|
// Update the proposition voters for both propositions.
|
|
|
|
UpdatePropositionVoters(proposition1, currentQuestion.propositions[0]);
|
|
|
|
UpdatePropositionVoters(proposition2, currentQuestion.propositions[1]);
|
|
|
|
}
|
|
|
|
|
2024-02-01 22:07:43 +00:00
|
|
|
//every players BUT the one who have created the propositions
|
|
|
|
if (RoomManager.Instance.myRoom.NumbersOfVotersForQuestion(currentQuestion) >= currentPlayers.Count - 2)
|
2024-02-01 21:38:34 +00:00
|
|
|
{
|
|
|
|
allPlayersHaveVotedForCurrentQuestion = true;
|
|
|
|
}
|
2024-01-28 12:18:40 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-02-01 21:38:34 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Updates the voters for a specific proposition frame based on the provided proposition data.
|
|
|
|
/// If the proposition data is not null, it retrieves the list of voters and updates the frame.
|
|
|
|
/// If there is no proposition data, it logs a message indicating that the user has not given a proposition.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="proposition">The PropositionFrame to update.</param>
|
|
|
|
/// <param name="propositionData">The Proposition data used to update voters for the proposition frame.</param>
|
|
|
|
private void UpdatePropositionVoters(PropositionFrame proposition, Proposition propositionData)
|
2024-01-28 12:18:40 +00:00
|
|
|
{
|
2024-02-01 21:38:34 +00:00
|
|
|
if (propositionData != null)
|
2024-01-28 12:18:40 +00:00
|
|
|
{
|
2024-02-01 21:38:34 +00:00
|
|
|
List<Player> voters = GetVotersForProposition(propositionData);
|
|
|
|
proposition.UpdateVoters(voters);
|
2024-01-28 12:18:40 +00:00
|
|
|
}
|
2024-02-01 21:38:34 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Debug.Log("User has given no proposition", this);
|
|
|
|
// Handle the case when there is no proposition
|
|
|
|
}
|
|
|
|
}
|
2024-01-28 12:18:40 +00:00
|
|
|
|
2024-02-01 21:38:34 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Retrieves the list of players who have voted for a specific proposition based on the provided proposition data.
|
|
|
|
/// It checks if the proposition data contains a list of voters and then looks up player information in the current players dictionary.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="propositionData">The Proposition data for which to retrieve voters.</param>
|
|
|
|
/// <returns>The list of players who voted for the proposition.</returns>
|
|
|
|
private List<Player> GetVotersForProposition(Proposition propositionData)
|
|
|
|
{
|
|
|
|
List<Player> voters = new List<Player>();
|
|
|
|
|
|
|
|
if (propositionData.voters != null)
|
2024-01-28 12:18:40 +00:00
|
|
|
{
|
2024-02-01 21:38:34 +00:00
|
|
|
foreach (string playerId in propositionData.voters)
|
2024-01-28 15:34:06 +00:00
|
|
|
{
|
2024-02-01 21:38:34 +00:00
|
|
|
voters.Add(currentPlayers[playerId]);
|
|
|
|
Debug.Log($"updating number of voters for: {propositionData.owner.name}'s propositions. They are now {voters.Count}.");
|
2024-01-28 15:34:06 +00:00
|
|
|
}
|
2024-01-28 12:18:40 +00:00
|
|
|
}
|
2024-02-01 21:38:34 +00:00
|
|
|
|
|
|
|
return voters;
|
2024-01-28 12:18:40 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-01 21:38:34 +00:00
|
|
|
|