player really join rooms
This commit is contained in:
parent
7fd4657e81
commit
cd659d87d5
@ -8,6 +8,12 @@ public class Player
|
||||
public string name;
|
||||
public string id;
|
||||
|
||||
public Player(string _name)
|
||||
{
|
||||
id = System.Guid.NewGuid().ToString();
|
||||
name = _name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Will sanitize the text and set it for the player
|
||||
/// </summary>
|
||||
|
@ -58,6 +58,7 @@ public class GameManager : MonoBehaviour
|
||||
|
||||
private DatabaseReference realtimeDB;
|
||||
private Room myRoom;
|
||||
private DatabaseReference myOnlineRoom;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
@ -89,17 +90,24 @@ private void Initialize()
|
||||
/// </summary>
|
||||
public void PlayerValidateNameAndServerRoom(string _name, string _code)
|
||||
{
|
||||
//check if the room exists, if not display an error message
|
||||
realtimeDB.Child("rooms").Child(_code).GetValueAsync().ContinueWithOnMainThread(task =>
|
||||
if (string.IsNullOrEmpty(_name))
|
||||
{
|
||||
if (task.IsFaulted)
|
||||
{
|
||||
Debug.LogException(task.Exception);
|
||||
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
|
||||
return;
|
||||
}
|
||||
else if (task.IsCompleted)
|
||||
|
||||
if (string.IsNullOrEmpty(_code))
|
||||
{
|
||||
DataSnapshot snapshot = task.Result;
|
||||
if (snapshot == null)
|
||||
Debug.LogError("Room code is empty", this);
|
||||
//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");
|
||||
roomError.text = "Error: the room doesn't exists";
|
||||
@ -107,11 +115,19 @@ public void PlayerValidateNameAndServerRoom(string _name, string _code)
|
||||
}
|
||||
else
|
||||
{
|
||||
myRoom = JsonUtility.FromJson<Room>(snapshot.GetRawJsonValue());
|
||||
realtimeDB.Child("rooms").Child(_code).ChildChanged += OnRoomUpdate;
|
||||
}
|
||||
//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");
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
string playerName = playerNameField.text;
|
||||
string roomCode = roomCodeField.text;
|
||||
|
||||
@ -130,7 +146,7 @@ public void PlayerValidateNameAndServerRoom(string _name, string _code)
|
||||
|
||||
|
||||
//TEMP
|
||||
Player player = new Player();
|
||||
Player player = new Player(playerName);
|
||||
player.SetName(playerName);
|
||||
player.id = "1";
|
||||
|
||||
@ -140,19 +156,19 @@ public void PlayerValidateNameAndServerRoom(string _name, string _code)
|
||||
submitStartGame.SetActive(true);
|
||||
}
|
||||
|
||||
Player player2 = new Player();
|
||||
Player player2 = new Player("3J");
|
||||
player2.SetName("3J");
|
||||
player2.id = "2";
|
||||
Player player3 = new Player();
|
||||
Player player3 = new Player("3J");
|
||||
player3.SetName("3J");
|
||||
player3.id = "3";
|
||||
Player player4 = new Player();
|
||||
Player player4 = new Player("3J");
|
||||
player4.SetName("3J");
|
||||
player4.id = "4";
|
||||
Player player5 = new Player();
|
||||
Player player5 = new Player("3J");
|
||||
player5.SetName("3J");
|
||||
player5.id = "5";
|
||||
Player player6 = new Player();
|
||||
Player player6 = new Player("3J");
|
||||
player6.SetName("3J");
|
||||
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>
|
||||
/// Call this only by the first player
|
||||
/// </summary>
|
||||
|
Loading…
Reference in New Issue
Block a user