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>();
private RoomState currentState;
private Room myRoom = null;
private List<Player> players;
@ -86,23 +85,38 @@ private void Initialize()
private void WhichCodesAreAlreadyUsed(Action<List<int>> callback_OnCodesChecked)
{
List<int> alreadyUsedCodes = new List<int>();
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<Room> onlineRooms = JsonUtility.FromJson<List<Room>>(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<Room> onlineRooms = JsonConvert.DeserializeObject<List<Room>>(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()
/// </summary>
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: