Merge branch 'main' of github.com:LeGall29/GGJ2024

This commit is contained in:
Michel Roux 2024-01-27 21:07:51 +01:00
commit 032bc974bf

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,23 +85,38 @@ 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>();
realtimeDB.Child("rooms").GetValueAsync().ContinueWithOnMainThread(task => try
{ {
if (task.IsFaulted) realtimeDB.Child("rooms").GetValueAsync().ContinueWithOnMainThread(task =>
{ {
Debug.LogException(task.Exception); if (task.IsFaulted)
}
else if (task.IsCompleted)
{
DataSnapshot snapshot = task.Result;
List<Room> onlineRooms = JsonUtility.FromJson<List<Room>>(snapshot.GetRawJsonValue());
foreach (Room r in onlineRooms)
{ {
alreadyUsedCodes.Add(int.Parse(r.code)); Debug.LogException(task.Exception);
} }
} else if (task.IsCompleted)
callback_OnCodesChecked?.Invoke(alreadyUsedCodes); {
}); 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 => 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: