Snaparazzi/Assets/Scripts/WaitForPropositionsPage.cs

119 lines
3.8 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;
2024-02-04 11:42:57 +00:00
allPlayersHasProposedTwoPictures = false;
playersIdWhoHaveProposed.Clear();
2024-01-29 18:32:07 +00:00
ShowPlayerLabels();
}
public void OnRoomUpdate(Room _myRoom)
2024-01-29 18:32:07 +00:00
{
myRoom = _myRoom;
2024-01-29 18:32:07 +00:00
}
2024-01-29 18:32:07 +00:00
// 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();
OnPropositionFinish = null;
gameObject.SetActive(false);
2024-01-29 18:32:07 +00:00
return;
}
CheckPlayersWhoHaveProposed();
propositionCounter.text = ((int)duration.TotalSeconds).ToString("D1");
}
}
private void ShowPlayerLabels()
{
//display only correct numbers of labels and set names
List<Player> players = myRoom.GetOrderedPlayerList();
for (int i = 0; i < playerLabels.Count; i++)
{
if (i < players.Count)
{
playerLabels[i].gameObject.SetActive(true);
playerLabels[i].text = players[i].name;
}
else
{
playerLabels[i].gameObject.SetActive(false);
}
//Debug.Log($"toggling {playerLabels[i].gameObject.name} accordingly to its player connection");
}
}
2024-01-29 18:32:07 +00:00
private void CheckPlayersWhoHaveProposed()
{
foreach (Player player in myRoom.GetPlayerList())
{
if (playersIdWhoHaveProposed.Contains(player.id))
continue;
bool playerHasAnsweredBothProposition = true;
Proposition[] propositions = myRoom.GetPropositionsForPlayer(player).ToArray();
//Debug.Log($"I found {propositions.Length} propositions for {player.name}", this);
2024-01-29 18:32:07 +00:00
if (propositions.Length < 2)
playerHasAnsweredBothProposition = false;
else
{
for (int i = 0; i < propositions.Length; i++)
{
if (string.IsNullOrEmpty(propositions[i].photoUrl))
{
//Debug.Log($"{player.name}'s proposition {i} has no photoURL", this);
2024-01-29 18:32:07 +00:00
playerHasAnsweredBothProposition = false;
}
}
}
if (playerHasAnsweredBothProposition)
{
Debug.Log($"{player.name}'s has answered both propositions !", this);
2024-01-29 18:32:07 +00:00
playerLabels.Find(x => x.text == player.name).color = Color.green;
playersIdWhoHaveProposed.Add(player.id);
}
//Debug.Log($"{playersIdWhoHaveProposed.Count}/{myRoom.players.Count} players have answered.", this);
2024-01-29 18:32:07 +00:00
if(playersIdWhoHaveProposed.Count == myRoom.players.Count)
{
allPlayersHasProposedTwoPictures = true;
}
}
}
}