fix(roommanager): deserialize dictionnary and not list of rooms

This commit is contained in:
Morgan - 6 Freedom 2024-01-27 21:27:17 +01:00
parent 032bc974bf
commit a2e2eb5898
2 changed files with 15 additions and 3 deletions

View File

@ -9,6 +9,13 @@ public class Player
public string name;
public string id;
[JsonConstructor]
public Player(string _name, string _id)
{
name = _name;
id = _id;
}
public Player(string _name)
{
id = Guid.NewGuid().ToString();

View File

@ -84,11 +84,13 @@ private void Initialize()
/// </summary>
private void WhichCodesAreAlreadyUsed(Action<List<int>> callback_OnCodesChecked)
{
Debug.Log("Checking other rooms to get which codes are already used", this);
List<int> alreadyUsedCodes = new List<int>();
try
{
realtimeDB.Child("rooms").GetValueAsync().ContinueWithOnMainThread(task =>
{
Debug.Log("looking into the online rooms", this);
if (task.IsFaulted)
{
Debug.LogException(task.Exception);
@ -96,10 +98,13 @@ private void WhichCodesAreAlreadyUsed(Action<List<int>> callback_OnCodesChecked)
else if (task.IsCompleted)
{
DataSnapshot snapshot = task.Result;
Debug.Log(snapshot.Value);
if (snapshot.Value != null)
{
List<Room> onlineRooms = JsonConvert.DeserializeObject<List<Room>>(snapshot.GetRawJsonValue());
foreach (Room r in onlineRooms)
string JSON = snapshot.GetRawJsonValue();
Debug.Log($"found some rooms :\n{JSON}", this);
Dictionary<string, Room> onlineRooms = JsonConvert.DeserializeObject<Dictionary<string, Room>>(JSON);
foreach (Room r in onlineRooms.Values)
{
Debug.Log($"Code {r.code} is already used by another party", this);
alreadyUsedCodes.Add(int.Parse(r.code));