diff --git a/Assets/Scripts/DatabaseClasses/Player.cs b/Assets/Scripts/DatabaseClasses/Player.cs index 60657d9..5bb47c7 100644 --- a/Assets/Scripts/DatabaseClasses/Player.cs +++ b/Assets/Scripts/DatabaseClasses/Player.cs @@ -1,6 +1,7 @@ using Newtonsoft.Json; using System; using System.Text.RegularExpressions; +using UnityEngine; [Serializable] [JsonObject] @@ -8,17 +9,21 @@ public class Player { public string name; public string id; + public float creationDate; [JsonConstructor] - public Player(string _name, string _id) + public Player(string _name, string _id, float _creationDate) { name = _name; id = _id; + creationDate = _creationDate; + } public Player(string _name) { id = Guid.NewGuid().ToString(); + creationDate = Time.time; SetName(_name); } diff --git a/Assets/Scripts/DatabaseClasses/Room.cs b/Assets/Scripts/DatabaseClasses/Room.cs index 65ad7ee..cbf45bf 100644 --- a/Assets/Scripts/DatabaseClasses/Room.cs +++ b/Assets/Scripts/DatabaseClasses/Room.cs @@ -11,6 +11,7 @@ public class Room public Dictionary players; public int currentQuestion; public double creationDate; + public int currentState; public Room(string _code) @@ -20,6 +21,7 @@ public Room(string _code) players = new Dictionary(); questions = new Dictionary(); currentQuestion = 0; + currentState = 0; } public List GetPlayerList() @@ -31,5 +33,10 @@ public List GetQuestionList() { return new List(questions.Values); } + + public void setPlayersAreReady(int _state) + { + currentState = _state; + } } diff --git a/Assets/Scripts/GameManager.cs b/Assets/Scripts/GameManager.cs index c3edb8f..50837a8 100644 --- a/Assets/Scripts/GameManager.cs +++ b/Assets/Scripts/GameManager.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using Firebase.Database; using Firebase.Extensions; +using Newtonsoft.Json; using TMPro; using UnityEngine; @@ -12,6 +13,7 @@ public class GameManager : MonoBehaviour { private GameState currentState; private List players = new(); + private bool isFirst = false; public Player currentPlayer = null; public Question currentQuestion = null; @@ -54,6 +56,12 @@ private void Start() HomeConnection.SetActive(true); submitNewPlayer.interactable = false; } + private void OnApplicationQuit() + { + myOnlineRoom.Child("players").Child(currentPlayer.id).RemoveValueAsync(); + Debug.Log($"delete player {currentPlayer.name}"); + myRoom = null; + } public GameState GetCurrentState() { @@ -123,7 +131,10 @@ public void PlayerValidateNameAndServerRoom(string _name, string _code) WaitingRoom.SetActive(true); HomeConnection.SetActive(false); - UpdateDisplayedListUser(); + List list = new List(); + list.Add(currentPlayer); + + UpdateDisplayedListUser(list) ; }); } }); @@ -187,9 +198,32 @@ private void JoinRoom(Action callback_OnRoomJoined) public void StartGame() { // send Start Game - currentState = GameState.Explanation; - WaitingRoom.SetActive(false); - BeforeStart.SetActive(true); + myRoom.setPlayersAreReady(1); + string JSON = JsonUtility.ToJson(myRoom); + Debug.Log(JSON); + try + { + myOnlineRoom.Child("currentState").SetValueAsync(2).ContinueWithOnMainThread(task => + { + if (task.IsFaulted) + { + Debug.LogException(task.Exception); + } + else + { + Debug.Log($"start the game", this); + currentState = GameState.Explanation; + WaitingRoom.SetActive(false); + BeforeStart.SetActive(true); + } + }); + } + catch (Exception ex) + { + Debug.LogException(ex); + } + + } /// @@ -243,7 +277,7 @@ private void OnRoomUpdate(object sender, ValueChangedEventArgs e) { try { - myRoom = JsonUtility.FromJson(e.Snapshot.GetRawJsonValue()); + myRoom = JsonConvert.DeserializeObject(e.Snapshot.GetRawJsonValue()); } catch (Exception ex) { @@ -254,16 +288,15 @@ private void OnRoomUpdate(object sender, ValueChangedEventArgs e) { case GameState.WaitingForOtherPlayersToJoin: { - - // players = new list en fonction de ce qu'envoie fangh - UpdateDisplayedListUser(); + CheckIfIAmTheFirst(myRoom.GetPlayerList()); + UpdateDisplayedListUser(myRoom.GetPlayerList()); break; } } } - private void UpdateDisplayedListUser() + private void UpdateDisplayedListUser(List players) { listPlayersUI.text = string.Empty; for (int i = 0; i < players.Count; i++) @@ -271,6 +304,18 @@ private void UpdateDisplayedListUser() listPlayersUI.text += "\n" + players[i].name; } } + + private void CheckIfIAmTheFirst(List players) + { + if (players.Count > 1) + { + + } + else + { + + } + } public void OnClickSubmitSignIn()