diff --git a/Assets/Scripts/RoomManager.cs b/Assets/Scripts/RoomManager.cs index b5d30fa..aef4f77 100644 --- a/Assets/Scripts/RoomManager.cs +++ b/Assets/Scripts/RoomManager.cs @@ -12,7 +12,6 @@ public class RoomManager : MonoBehaviour { public List playerLabels = new List(); - private RoomState currentState; private Room myRoom = null; private List players; @@ -86,23 +85,38 @@ private void Initialize() private void WhichCodesAreAlreadyUsed(Action> callback_OnCodesChecked) { List alreadyUsedCodes = new List(); - realtimeDB.Child("rooms").GetValueAsync().ContinueWithOnMainThread(task => + try { - if (task.IsFaulted) + realtimeDB.Child("rooms").GetValueAsync().ContinueWithOnMainThread(task => { - Debug.LogException(task.Exception); - } - else if (task.IsCompleted) - { - DataSnapshot snapshot = task.Result; - List onlineRooms = JsonUtility.FromJson>(snapshot.GetRawJsonValue()); - foreach (Room r in onlineRooms) + if (task.IsFaulted) { - alreadyUsedCodes.Add(int.Parse(r.code)); + Debug.LogException(task.Exception); } - } - callback_OnCodesChecked?.Invoke(alreadyUsedCodes); - }); + else if (task.IsCompleted) + { + DataSnapshot snapshot = task.Result; + if (snapshot.Value != null) + { + List onlineRooms = JsonConvert.DeserializeObject>(snapshot.GetRawJsonValue()); + foreach (Room r in onlineRooms) + { + Debug.Log($"Code {r.code} is already used by another party", this); + alreadyUsedCodes.Add(int.Parse(r.code)); + } + } + else + { + Debug.Log($"Your party is the first one!", this); + } + } + callback_OnCodesChecked?.Invoke(alreadyUsedCodes); + }); + } + catch (Exception ex) + { + Debug.LogException(ex); + } } @@ -114,16 +128,15 @@ public void CreateNewRoom() { WhichCodesAreAlreadyUsed(codes => { + Debug.Log("coucou"); Room newRoom = new Room(GenerateRandomAvailableCode(codes).ToString("D4")); myRoom = newRoom; - try { - string JSON = JsonUtility.ToJson(newRoom); + string JSON = JsonConvert.SerializeObject(newRoom); realtimeDB.Child("rooms").Child(newRoom.code).SetRawJsonValueAsync(JSON).ContinueWithOnMainThread(task => { - //then subscribe to it realtimeDB.Child("rooms").Child(newRoom.code).ValueChanged += OnRoomUpdate; roomCodeLabel.text = myRoom.code; @@ -236,7 +249,6 @@ public void GenerateCouples() /// private void OnRoomUpdate(object sender, ValueChangedEventArgs value) { - Debug.Log("coucou"); if (value.DatabaseError != null) { Debug.LogError(value.DatabaseError.Message); @@ -253,12 +265,10 @@ private void OnRoomUpdate(object sender, ValueChangedEventArgs value) Debug.LogException(ex); } - Debug.Log("caca"); switch (currentState) { case RoomState.WaitingForPlayers: - Debug.Log("prout"); UpdateConnectedPlayerList(myRoom.GetPlayerList()); break; default: