player really join rooms

This commit is contained in:
Morgan - 6 Freedom 2024-01-27 17:54:37 +01:00
parent 7fd4657e81
commit cd659d87d5
2 changed files with 97 additions and 28 deletions

View File

@ -8,6 +8,12 @@ public class Player
public string name; public string name;
public string id; public string id;
public Player(string _name)
{
id = System.Guid.NewGuid().ToString();
name = _name;
}
/// <summary> /// <summary>
/// Will sanitize the text and set it for the player /// Will sanitize the text and set it for the player
/// </summary> /// </summary>

View File

@ -58,6 +58,7 @@ public class GameManager : MonoBehaviour
private DatabaseReference realtimeDB; private DatabaseReference realtimeDB;
private Room myRoom; private Room myRoom;
private DatabaseReference myOnlineRoom;
private void Awake() private void Awake()
{ {
@ -89,17 +90,24 @@ private void Initialize()
/// </summary> /// </summary>
public void PlayerValidateNameAndServerRoom(string _name, string _code) public void PlayerValidateNameAndServerRoom(string _name, string _code)
{ {
//check if the room exists, if not display an error message if (string.IsNullOrEmpty(_name))
realtimeDB.Child("rooms").Child(_code).GetValueAsync().ContinueWithOnMainThread(task =>
{ {
if (task.IsFaulted) Debug.LogError("Player name is empty", this);
{ //TODO : MARINE : use the error label to explain to the user that they have forget to put a name
Debug.LogException(task.Exception); return;
} }
else if (task.IsCompleted)
if (string.IsNullOrEmpty(_code))
{ {
DataSnapshot snapshot = task.Result; Debug.LogError("Room code is empty", this);
if (snapshot == null) //TODO : MARINE : use the error label to explain to the user that they have forget to put a room code
return;
}
//check if the room exists, if not display an error message
CheckIfRoomExists(_code, room =>
{
if (room == null)
{ {
Debug.LogError("The room doesn't exists"); Debug.LogError("The room doesn't exists");
roomError.text = "Error: the room doesn't exists"; roomError.text = "Error: the room doesn't exists";
@ -107,11 +115,19 @@ public void PlayerValidateNameAndServerRoom(string _name, string _code)
} }
else else
{ {
myRoom = JsonUtility.FromJson<Room>(snapshot.GetRawJsonValue()); //if room exists, join it
realtimeDB.Child("rooms").Child(_code).ChildChanged += OnRoomUpdate; JoinRoom(() =>
} {
//then subscribe to it
myOnlineRoom = realtimeDB.Child("rooms").Child(_code);
myOnlineRoom.ChildChanged += OnRoomUpdate;
Debug.Log($"room {myRoom.code} exists, I subscribe to it");
});
} }
}); });
string playerName = playerNameField.text; string playerName = playerNameField.text;
string roomCode = roomCodeField.text; string roomCode = roomCodeField.text;
@ -130,7 +146,7 @@ public void PlayerValidateNameAndServerRoom(string _name, string _code)
//TEMP //TEMP
Player player = new Player(); Player player = new Player(playerName);
player.SetName(playerName); player.SetName(playerName);
player.id = "1"; player.id = "1";
@ -140,19 +156,19 @@ public void PlayerValidateNameAndServerRoom(string _name, string _code)
submitStartGame.SetActive(true); submitStartGame.SetActive(true);
} }
Player player2 = new Player(); Player player2 = new Player("3J");
player2.SetName("3J"); player2.SetName("3J");
player2.id = "2"; player2.id = "2";
Player player3 = new Player(); Player player3 = new Player("3J");
player3.SetName("3J"); player3.SetName("3J");
player3.id = "3"; player3.id = "3";
Player player4 = new Player(); Player player4 = new Player("3J");
player4.SetName("3J"); player4.SetName("3J");
player4.id = "4"; player4.id = "4";
Player player5 = new Player(); Player player5 = new Player("3J");
player5.SetName("3J"); player5.SetName("3J");
player5.id = "5"; player5.id = "5";
Player player6 = new Player(); Player player6 = new Player("3J");
player6.SetName("3J"); player6.SetName("3J");
player6.id = "6"; player6.id = "6";
@ -171,6 +187,53 @@ public void PlayerValidateNameAndServerRoom(string _name, string _code)
} }
private void CheckIfRoomExists(string _roomCode, Action<Room> callback_Room)
{
realtimeDB.Child("rooms").Child(_roomCode).GetValueAsync().ContinueWithOnMainThread(task =>
{
if (task.IsFaulted)
{
Debug.LogException(task.Exception);
}
else if (task.IsCompleted)
{
DataSnapshot snapshot = task.Result;
if (snapshot == null)
{
callback_Room?.Invoke(null);
}
else
{
callback_Room?.Invoke(JsonUtility.FromJson<Room>(snapshot.GetRawJsonValue()));
}
}
});
}
/// <summary>
/// Add this player to the room
/// </summary>
private void JoinRoom(Action callback_OnRoomJoined)
{
//TODO marine : uncomment aftter merge
/*
string JSON = JsonUtility.ToJson(currentPlayer);
myOnlineRoom.Child("players").Child(currentPlayer.id).SetRawJsonValueAsync(JSON).ContinueWithOnMainThread(task =>
{
if (task.IsFaulted)
{
Debug.LogException(task.Exception);
}
else
{
Debug.Log($"{currentPlayer.name} has been added to the room", this);
callback_OnRoomJoined?.Invoke();
}
});
*/
}
/// <summary> /// <summary>
/// Call this only by the first player /// Call this only by the first player
/// </summary> /// </summary>