fix: currentstate is no more global

This commit is contained in:
Morgan - 6 Freedom 2024-01-27 23:47:31 +01:00
parent 9da2198871
commit 6deff961d4

View File

@ -30,9 +30,7 @@ public class RoomManager : MonoBehaviour
private float propositionCurrentTime = 0; private float propositionCurrentTime = 0;
public TextMeshProUGUI timerGUI; public TextMeshProUGUI timerGUI;
private GameState currentState;
private Room myRoom = null; private Room myRoom = null;
public float votingTime = 20; public float votingTime = 20;
private float votingCurrentTime = 0; private float votingCurrentTime = 0;
@ -54,7 +52,6 @@ public class RoomManager : MonoBehaviour
private void Awake() private void Awake()
{ {
FirebaseInitializer.Instance.onFirebaseReady += Initialize; FirebaseInitializer.Instance.onFirebaseReady += Initialize;
currentState = GameState.EnteringName;
} }
@ -63,12 +60,14 @@ private void Start()
propositionCurrentTime = propositionTime; propositionCurrentTime = propositionTime;
votingCurrentTime = votingTime; votingCurrentTime = votingTime;
ResetAllPlayerLabels(); ResetAllPlayerLabels();
currentState = GameState.WaitingForOtherPlayersToJoin;
} }
private void Update() private void Update()
{ {
if (currentState == GameState.Explanation && endOfExplanationDate != DateTime.MinValue) if (myRoom == null)
return;
if (myRoom.currentState == (int)GameState.Explanation && endOfExplanationDate != DateTime.MinValue)
{ {
TimeSpan duration = endOfExplanationDate - DateTime.Now; TimeSpan duration = endOfExplanationDate - DateTime.Now;
counter.text = duration.TotalSeconds.ToString("D1"); counter.text = duration.TotalSeconds.ToString("D1");
@ -76,11 +75,16 @@ private void Update()
if (duration.TotalMilliseconds <= 0) if (duration.TotalMilliseconds <= 0)
{ {
Debug.Log("It's time to make proposition !", this); Debug.Log("It's time to make proposition !", this);
currentState = GameState.MakeProposition; SendRoomState(GameState.MakeProposition);
} }
} }
} }
private void SendRoomState(GameState _newState)
{
realtimeDB.Child("rooms").Child(myRoom.code).Child("currentState").SetValueAsync((int)_newState);
}
private void ResetAllPlayerLabels() private void ResetAllPlayerLabels()
{ {
for (int i = 0; i < waitingForPlayersLabels.Count; i++) for (int i = 0; i < waitingForPlayersLabels.Count; i++)
@ -207,7 +211,6 @@ public void PlayerSendProposition(Proposition _proposition)
/// </summary> /// </summary>
public void HostStartGame() public void HostStartGame()
{ {
currentState = GameState.MakeProposition;
waitingForPropositionsPage.SetActive(true); waitingForPropositionsPage.SetActive(true);
waitingForPlayersPage.SetActive(false); waitingForPlayersPage.SetActive(false);
propositionCurrentTime = propositionTime; propositionCurrentTime = propositionTime;
@ -313,7 +316,7 @@ private void OnRoomUpdate(object sender, ValueChangedEventArgs value)
return; return;
} }
string JSON = value.Snapshot.GetRawJsonValue(); string JSON = value.Snapshot.GetRawJsonValue();
Debug.Log(JSON); Debug.Log($"your room has been updated :\n{JSON}");
try try
{ {
myRoom = JsonConvert.DeserializeObject<Room>(JSON); myRoom = JsonConvert.DeserializeObject<Room>(JSON);
@ -324,17 +327,17 @@ private void OnRoomUpdate(object sender, ValueChangedEventArgs value)
} }
switch (currentState) switch (myRoom.currentState)
{ {
case GameState.WaitingForOtherPlayersToJoin: case (int)GameState.WaitingForOtherPlayersToJoin:
UpdateConnectedPlayerList(myRoom.GetPlayerList()); UpdateConnectedPlayerList(myRoom.GetPlayerList());
break; break;
case GameState.Explanation: case (int)GameState.Explanation:
waitingForPlayersPage.SetActive(false); waitingForPlayersPage.SetActive(false);
explanationPage.SetActive(true); explanationPage.SetActive(true);
endOfExplanationDate = DateTime.Now.AddSeconds(30); endOfExplanationDate = DateTime.Now.AddSeconds(30);
break; break;
case GameState.MakeProposition: case (int)GameState.MakeProposition:
HostStartGame(); HostStartGame();
break; break;
default: default: