player can connect WIP
This commit is contained in:
parent
449c0829ae
commit
9d8019556c
@ -1,5 +1,7 @@
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using Firebase.Database;
|
||||||
|
using Firebase.Extensions;
|
||||||
using TMPro;
|
using TMPro;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.UI;
|
using UnityEngine.UI;
|
||||||
@ -20,6 +22,14 @@ public class GameManager : MonoBehaviour
|
|||||||
public InputField playerNameField;
|
public InputField playerNameField;
|
||||||
public TextMeshProUGUI nameError;
|
public TextMeshProUGUI nameError;
|
||||||
|
|
||||||
|
private DatabaseReference realtimeDB;
|
||||||
|
private Room myRoom;
|
||||||
|
|
||||||
|
private void Awake()
|
||||||
|
{
|
||||||
|
FirebaseInitializer.Instance.onFirebaseReady += Initialize;
|
||||||
|
}
|
||||||
|
|
||||||
private void Start()
|
private void Start()
|
||||||
{
|
{
|
||||||
currentExplanationTime = explanationTime;
|
currentExplanationTime = explanationTime;
|
||||||
@ -30,12 +40,43 @@ public GameState GetCurrentState()
|
|||||||
return currentState;
|
return currentState;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void Initialize()
|
||||||
|
{
|
||||||
|
FirebaseInitializer.Instance.onFirebaseReady -= Initialize;
|
||||||
|
realtimeDB = FirebaseDatabase.DefaultInstance.RootReference;
|
||||||
|
Debug.Log("Realtime DB initialized");
|
||||||
|
|
||||||
|
//TODO MARINE : enable the submit button here. Disabled it at start
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Send your name and game room to the server
|
/// Send your name and game room to the server
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void PlayerValidateNameAndServerRoom()
|
public void PlayerValidateNameAndServerRoom(string _name, string _code)
|
||||||
{
|
{
|
||||||
//check if the room exists, if not display an error message
|
//check if the room exists, if not display an error message
|
||||||
|
realtimeDB.Child("rooms").Child(_code).GetValueAsync().ContinueWithOnMainThread(task =>
|
||||||
|
{
|
||||||
|
if (task.IsFaulted)
|
||||||
|
{
|
||||||
|
Debug.LogException(task.Exception);
|
||||||
|
}
|
||||||
|
else if (task.IsCompleted)
|
||||||
|
{
|
||||||
|
DataSnapshot snapshot = task.Result;
|
||||||
|
if (snapshot == null)
|
||||||
|
{
|
||||||
|
Debug.LogError("The room doesn't exists");
|
||||||
|
roomError.text = "Error: the room doesn't exists";
|
||||||
|
roomError.gameObject.SetActive(true);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
myRoom = JsonUtility.FromJson<Room>(snapshot.GetRawJsonValue());
|
||||||
|
realtimeDB.Child("rooms").Child(_code).ChildChanged += OnRoomUpdate;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
//if succeed, then the player to the server
|
//if succeed, then the player to the server
|
||||||
|
|
||||||
@ -99,8 +140,9 @@ public void DisplayEndScreen()
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Automatically called when something change in your room
|
/// Automatically called when something change in your room
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void OnRoomUpdate()
|
private void OnRoomUpdate(object sender, ChildChangedEventArgs e)
|
||||||
{
|
{
|
||||||
|
myRoom = JsonUtility.FromJson<Room>(e.Snapshot.GetRawJsonValue());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,7 +46,6 @@ private void Awake()
|
|||||||
|
|
||||||
private void Start()
|
private void Start()
|
||||||
{
|
{
|
||||||
|
|
||||||
propositionCurrentTime = propositionTime;
|
propositionCurrentTime = propositionTime;
|
||||||
votingCurrentTime = votingTime;
|
votingCurrentTime = votingTime;
|
||||||
}
|
}
|
||||||
@ -106,6 +105,7 @@ public void CreateNewRoom()
|
|||||||
realtimeDB.Child("rooms").Child(newRoom.code).SetRawJsonValueAsync(JSON).ContinueWithOnMainThread(task =>
|
realtimeDB.Child("rooms").Child(newRoom.code).SetRawJsonValueAsync(JSON).ContinueWithOnMainThread(task =>
|
||||||
{
|
{
|
||||||
Debug.Log($"room {currentRoom.code} has been created on the server");
|
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
|
//TODO MARINE : uncomment and reference the correct game object
|
||||||
//roomCodeLabel.text = currentRoom.code;
|
//roomCodeLabel.text = currentRoom.code;
|
||||||
});
|
});
|
||||||
@ -210,9 +210,18 @@ public void GenerateCouples()
|
|||||||
/// is automatically called when a player connect to the room
|
/// is automatically called when a player connect to the room
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="_player"></param>
|
/// <param name="_player"></param>
|
||||||
public void PlayerConnect(Player _player)
|
public void PlayerConnect(object sender, ChildChangedEventArgs args)
|
||||||
{
|
{
|
||||||
|
if (args.DatabaseError != null)
|
||||||
|
{
|
||||||
|
Debug.LogError(args.DatabaseError.Message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
string JSON = args.Snapshot.GetRawJsonValue();
|
||||||
|
Player joinedPlayer = JsonUtility.FromJson<Player>(JSON);
|
||||||
|
Debug.Log($"{joinedPlayer.name} has joined the room");
|
||||||
|
//TODO Marine : do somtethjing with the newly joinde player
|
||||||
}
|
}
|
||||||
|
|
||||||
[ContextMenu("Fake Player Connection")]
|
[ContextMenu("Fake Player Connection")]
|
||||||
|
Loading…
Reference in New Issue
Block a user