Snaparazzi/Assets/Scripts/WaitForPropositionsPage.cs

97 lines
2.9 KiB
C#
Raw Normal View History

2024-01-29 18:32:07 +00:00
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class WaitForPropositionsPage : MonoBehaviour
{
public TextMeshProUGUI propositionCounter;
public float propositionTime = 60;
public List<TextMeshProUGUI> playerLabels = new List<TextMeshProUGUI>();
private DateTime endOfPropositionDate = DateTime.MinValue;
private bool allPlayersHasProposedTwoPictures = false;
private event Action OnPropositionFinish;
private Room myRoom;
private List<string> playersIdWhoHaveProposed = new List<string>();
public void Initialize(Room _myRoom, Action _onPropositionFinish)
{
endOfPropositionDate = DateTime.Now.AddSeconds(propositionTime);
gameObject.SetActive(true);
OnPropositionFinish = _onPropositionFinish;
myRoom = _myRoom;
ShowPlayerLabels();
}
private void ShowPlayerLabels()
{
//display only correct numbers of labels
for (int i = 0; i < playerLabels.Count; i++)
{
TextMeshProUGUI tmp = playerLabels[i];
tmp.gameObject.SetActive(i < myRoom.players.Count);
Debug.Log($"toggling {tmp.gameObject.name} accordingly to its player connection");
}
}
// Update is called once per frame
void Update()
{
//while MakeProposition State
if (endOfPropositionDate != DateTime.MinValue)
{
TimeSpan duration = endOfPropositionDate - DateTime.Now;
if (duration.TotalMilliseconds <= 0 || allPlayersHasProposedTwoPictures)
{
OnPropositionFinish?.Invoke();
return;
}
CheckPlayersWhoHaveProposed();
propositionCounter.text = ((int)duration.TotalSeconds).ToString("D1");
}
}
private void CheckPlayersWhoHaveProposed()
{
foreach (Player player in myRoom.GetPlayerList())
{
if (playersIdWhoHaveProposed.Contains(player.id))
continue;
bool playerHasAnsweredBothProposition = true;
Proposition[] propositions = myRoom.GetPropositionsByPlayer(player).ToArray();
if (propositions.Length < 2)
playerHasAnsweredBothProposition = false;
else
{
for (int i = 0; i < propositions.Length; i++)
{
if (string.IsNullOrEmpty(propositions[i].photoUrl))
{
playerHasAnsweredBothProposition = false;
}
}
}
if (playerHasAnsweredBothProposition)
{
playerLabels.Find(x => x.text == player.name).color = Color.green;
playersIdWhoHaveProposed.Add(player.id);
}
if(playersIdWhoHaveProposed.Count == myRoom.players.Count)
{
allPlayersHasProposedTwoPictures = true;
}
}
}
}