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_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!114 &157909814
MonoBehaviour:
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
return;
}
currentPlayer = new Player(_name);
//check if the room exists, if not display an error message
CheckIfRoomExists(_code, room =>
@ -114,13 +115,23 @@ public void PlayerValidateNameAndServerRoom(string _name, string _code)
}
else
{
myOnlineRoom = realtimeDB.Child("rooms").Child(_code);
Debug.Log("Before join room");
//if room exists, join it
JoinRoom(() =>
{
//then subscribe to it
myOnlineRoom = realtimeDB.Child("rooms").Child(_code);
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,6 +171,9 @@ private void JoinRoom(Action callback_OnRoomJoined)
{
string JSON = JsonUtility.ToJson(currentPlayer);
Debug.Log(JSON);
try
{
myOnlineRoom.Child("players").Child(currentPlayer.id).SetRawJsonValueAsync(JSON).ContinueWithOnMainThread(task =>
{
if (task.IsFaulted)
@ -172,6 +186,12 @@ private void JoinRoom(Action callback_OnRoomJoined)
callback_OnRoomJoined?.Invoke();
}
});
}
catch (Exception ex)
{
Debug.LogException(ex);
}
}
@ -246,8 +266,7 @@ private void OnRoomUpdate(object sender, ChildChangedEventArgs e)
{
case GameState.WaitingForOtherPlayersToJoin:
{
WaitingRoom.SetActive(true);
HomeConnection.SetActive(false);
// players = new list en fonction de ce qu'envoie fangh
UpdateDisplayedListUser();
@ -310,7 +329,7 @@ public void OnClickSubmitSignIn()
{
string playerName = playerNameField.text;
string roomCode = roomCodeField.text;
Debug.Log("click btn");
PlayerValidateNameAndServerRoom(playerName, roomCode);
}
}

View File

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