fix(connection): when there is no room

This commit is contained in:
Morgan - 6 Freedom 2024-01-27 21:07:17 +01:00
parent 94c3daa525
commit 65ebad88cb

View File

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