update current state + sendExplanation

This commit is contained in:
Marine 2024-01-27 23:50:57 +01:00
parent 8e77f1cb13
commit 8519964e06

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Firebase.Database;
using Firebase.Extensions;
using Newtonsoft.Json;
@ -11,9 +12,7 @@
/// </summary>
public class GameManager : MonoBehaviour
{
private GameState currentState;
private List<Player> players = new();
private bool isFirst = false;
public Player currentPlayer = null;
public Question currentQuestion = null;
@ -58,15 +57,13 @@ private void Start()
}
private void OnApplicationQuit()
{
myOnlineRoom.Child("players").Child(currentPlayer.id).RemoveValueAsync();
Debug.Log($"delete player {currentPlayer.name}");
myRoom = null;
myOnlineRoom.Child("players").Child(currentPlayer.id).RemoveValueAsync().ContinueWithOnMainThread(task =>
{
Debug.Log($"delete player {currentPlayer.name}");
myOnlineRoom.ValueChanged -= OnRoomUpdate;
});
}
public GameState GetCurrentState()
{
return currentState;
}
private void Initialize()
{
@ -125,7 +122,7 @@ public void PlayerValidateNameAndServerRoom(string _name, string _code)
{
//then subscribe to it
myOnlineRoom.ValueChanged += OnRoomUpdate;
currentState = GameState.WaitingForOtherPlayersToJoin;
myRoom.currentState = (int)GameState.WaitingForOtherPlayersToJoin;
players.Add(currentPlayer);
WaitingRoom.SetActive(true);
@ -203,19 +200,13 @@ public void StartGame()
Debug.Log(JSON);
try
{
myOnlineRoom.Child("currentState").SetValueAsync(2).ContinueWithOnMainThread(task =>
sendCurrentState(GameState.Explanation, () =>
{
if (task.IsFaulted)
{
Debug.LogException(task.Exception);
}
else
{
Debug.Log($"start the game", this);
currentState = GameState.Explanation;
WaitingRoom.SetActive(false);
BeforeStart.SetActive(true);
}
Debug.Log($"start the game", this);
myRoom.currentState = (int)GameState.Explanation;
WaitingRoom.SetActive(false);
BeforeStart.SetActive(true);
});
}
catch (Exception ex)
@ -233,7 +224,7 @@ public void StartGame()
/// <param name="_prompt">The prompt to display</param>
public void MakeAProposition(Prompt _prompt)
{
currentState = GameState.MakeProposition;
//currentState = GameState.MakeProposition;
}
/// <summary>
@ -283,13 +274,30 @@ private void OnRoomUpdate(object sender, ValueChangedEventArgs e)
{
Debug.LogException(ex);
}
switch (currentState)
if(myRoom == null) {
return;
}
switch (myRoom.currentState)
{
case GameState.WaitingForOtherPlayersToJoin:
case (int)GameState.WaitingForOtherPlayersToJoin:
{
CheckIfIAmTheFirst(myRoom.GetPlayerList());
UpdateDisplayedListUser(myRoom.GetPlayerList());
break;
}
case (int)GameState.Explanation:
{
break;
}
case (int)GameState.MakeProposition:
{
if (BeforeStart.activeInHierarchy)
{
BeforeStart.SetActive(false);
TakePicture.SetActive(true);
}
break;
}
}
@ -307,16 +315,38 @@ private void UpdateDisplayedListUser(List<Player> players)
private void CheckIfIAmTheFirst(List<Player> players)
{
bool isFirst = false;
if (players.Count > 1)
{
IOrderedEnumerable<Player> sortedList = players.OrderBy(x=>x.creationDate);
if(sortedList.Last().id == currentPlayer.id)
{
isFirst = true;
}
}
else
if (isFirst)
{
submitStartGame.SetActive(true);
}
}
public void sendCurrentState(GameState state,Action callback_oncCurrentStateSent)
{
myOnlineRoom.Child("currentState").SetValueAsync((int)state).ContinueWithOnMainThread(task =>
{
if (task.IsFaulted)
{
Debug.LogException(task.Exception);
}
else
{
callback_oncCurrentStateSent?.Invoke();
}
}); ;
}
public void OnClickSubmitSignIn()
{