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 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>
|
||||||
|
@ -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,29 +90,44 @@ 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
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(_code))
|
||||||
|
{
|
||||||
|
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.LogException(task.Exception);
|
Debug.LogError("The room doesn't exists");
|
||||||
|
roomError.text = "Error: the room doesn't exists";
|
||||||
|
roomError.gameObject.SetActive(true);
|
||||||
}
|
}
|
||||||
else if (task.IsCompleted)
|
else
|
||||||
{
|
{
|
||||||
DataSnapshot snapshot = task.Result;
|
//if room exists, join it
|
||||||
if (snapshot == null)
|
JoinRoom(() =>
|
||||||
{
|
{
|
||||||
Debug.LogError("The room doesn't exists");
|
//then subscribe to it
|
||||||
roomError.text = "Error: the room doesn't exists";
|
myOnlineRoom = realtimeDB.Child("rooms").Child(_code);
|
||||||
roomError.gameObject.SetActive(true);
|
myOnlineRoom.ChildChanged += OnRoomUpdate;
|
||||||
}
|
Debug.Log($"room {myRoom.code} exists, I subscribe to it");
|
||||||
else
|
});
|
||||||
{
|
|
||||||
myRoom = JsonUtility.FromJson<Room>(snapshot.GetRawJsonValue());
|
|
||||||
realtimeDB.Child("rooms").Child(_code).ChildChanged += OnRoomUpdate;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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>
|
||||||
|
Loading…
Reference in New Issue
Block a user