correct user connect room

This commit is contained in:
Marine 2024-01-27 18:50:04 +01:00
parent 609ec22957
commit 3ed3dc31dd
3 changed files with 51 additions and 23 deletions

View File

@ -214,7 +214,7 @@ GameObject:
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
m_IsActive: 1 m_IsActive: 0
--- !u!114 &157909814 --- !u!114 &157909814
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@ -102,6 +102,7 @@ public void PlayerValidateNameAndServerRoom(string _name, string _code)
//TODO : MARINE : use the error label to explain to the user that they have forget to put a room code //TODO : MARINE : use the error label to explain to the user that they have forget to put a room code
return; return;
} }
currentPlayer = new Player(_name);
//check if the room exists, if not display an error message //check if the room exists, if not display an error message
CheckIfRoomExists(_code, room => CheckIfRoomExists(_code, room =>
@ -114,13 +115,23 @@ public void PlayerValidateNameAndServerRoom(string _name, string _code)
} }
else else
{ {
myOnlineRoom = realtimeDB.Child("rooms").Child(_code);
Debug.Log("Before join room");
//if room exists, join it //if room exists, join it
JoinRoom(() => JoinRoom(() =>
{ {
//then subscribe to it //then subscribe to it
myOnlineRoom = realtimeDB.Child("rooms").Child(_code);
myOnlineRoom.ChildChanged += OnRoomUpdate; myOnlineRoom.ChildChanged += OnRoomUpdate;
Debug.Log($"room {myRoom.code} exists, I subscribe to it"); currentState = GameState.WaitingForOtherPlayersToJoin;
players.Add(currentPlayer);
WaitingRoom.SetActive(true);
HomeConnection.SetActive(false);
UpdateDisplayedListUser();
}); });
} }
}); });
@ -160,18 +171,27 @@ private void JoinRoom(Action callback_OnRoomJoined)
{ {
string JSON = JsonUtility.ToJson(currentPlayer); string JSON = JsonUtility.ToJson(currentPlayer);
myOnlineRoom.Child("players").Child(currentPlayer.id).SetRawJsonValueAsync(JSON).ContinueWithOnMainThread(task => Debug.Log(JSON);
try
{ {
if (task.IsFaulted) myOnlineRoom.Child("players").Child(currentPlayer.id).SetRawJsonValueAsync(JSON).ContinueWithOnMainThread(task =>
{ {
Debug.LogException(task.Exception); if (task.IsFaulted)
} {
else Debug.LogException(task.Exception);
{ }
Debug.Log($"{currentPlayer.name} has been added to the room", this); else
callback_OnRoomJoined?.Invoke(); {
} Debug.Log($"{currentPlayer.name} has been added to the room", this);
}); callback_OnRoomJoined?.Invoke();
}
});
}
catch (Exception ex)
{
Debug.LogException(ex);
}
} }
@ -246,8 +266,7 @@ private void OnRoomUpdate(object sender, ChildChangedEventArgs e)
{ {
case GameState.WaitingForOtherPlayersToJoin: case GameState.WaitingForOtherPlayersToJoin:
{ {
WaitingRoom.SetActive(true);
HomeConnection.SetActive(false);
// players = new list en fonction de ce qu'envoie fangh // players = new list en fonction de ce qu'envoie fangh
UpdateDisplayedListUser(); UpdateDisplayedListUser();
@ -310,7 +329,7 @@ public void OnClickSubmitSignIn()
{ {
string playerName = playerNameField.text; string playerName = playerNameField.text;
string roomCode = roomCodeField.text; string roomCode = roomCodeField.text;
Debug.Log("click btn");
PlayerValidateNameAndServerRoom(playerName, roomCode); PlayerValidateNameAndServerRoom(playerName, roomCode);
} }
} }

View File

@ -101,14 +101,23 @@ public void CreateNewRoom()
{ {
Room newRoom = new Room(GenerateRandomAvailableCode(codes).ToString("D4")); Room newRoom = new Room(GenerateRandomAvailableCode(codes).ToString("D4"));
currentRoom = newRoom; currentRoom = newRoom;
string JSON = JsonUtility.ToJson(newRoom);
realtimeDB.Child("rooms").Child(newRoom.code).SetRawJsonValueAsync(JSON).ContinueWithOnMainThread(task => try
{ {
Debug.Log($"room {currentRoom.code} has been created on the server"); string JSON = JsonUtility.ToJson(newRoom);
realtimeDB.Child("rooms").Child(newRoom.code).Child("players").ChildAdded += PlayerConnect;
//TODO MARINE : uncomment and reference the correct game object realtimeDB.Child("rooms").Child(newRoom.code).SetRawJsonValueAsync(JSON).ContinueWithOnMainThread(task =>
//roomCodeLabel.text = currentRoom.code; {
}); Debug.Log($"room {currentRoom.code} has been created on the server");
realtimeDB.Child("rooms").Child(newRoom.code).Child("players").ChildAdded += PlayerConnect;
//TODO MARINE : uncomment and reference the correct game object
//roomCodeLabel.text = currentRoom.code;
});
}
catch (Exception e)
{
Debug.LogException(e);
}
}); });
} }