From df6c4f08c91de860be0ecd58eeee641968f21425 Mon Sep 17 00:00:00 2001 From: Morgan - 6 Freedom Date: Sat, 27 Jan 2024 19:54:06 +0100 Subject: [PATCH] players can join room --- Assets/Scenes/ComputerView.unity | 15 ++- Assets/Scripts/DatabaseClasses/Player.cs | 1 + Assets/Scripts/DatabaseClasses/Prompt.cs | 1 + Assets/Scripts/DatabaseClasses/Proposition.cs | 1 + Assets/Scripts/DatabaseClasses/Question.cs | 1 + Assets/Scripts/DatabaseClasses/Room.cs | 22 ++++- Assets/Scripts/GameManager.cs | 3 +- Assets/Scripts/RoomManager.cs | 96 ++++++++++++++----- Packages/manifest.json | 1 + 9 files changed, 110 insertions(+), 31 deletions(-) diff --git a/Assets/Scenes/ComputerView.unity b/Assets/Scenes/ComputerView.unity index 7771eb2..abc2a2d 100644 --- a/Assets/Scenes/ComputerView.unity +++ b/Assets/Scenes/ComputerView.unity @@ -1325,9 +1325,18 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: de098f8fd5f884a1aa55db7874246b92, type: 3} m_Name: m_EditorClassIdentifier: - propositionTime: 60 + playerLabels: + - {fileID: 1794849355} + - {fileID: 1452182577} + - {fileID: 1018720202} + - {fileID: 983174567} + - {fileID: 132605380} + - {fileID: 496953434} + - {fileID: 2137991537} + - {fileID: 340074661} + propositionTime: 59.8 votingTime: 20 - roomCodeLabel: {fileID: 0} + roomCodeLabel: {fileID: 1805240027} --- !u!114 &375256413 MonoBehaviour: m_ObjectHideFlags: 0 @@ -6916,7 +6925,7 @@ GameObject: - component: {fileID: 1901141297} - component: {fileID: 1901141296} m_Layer: 5 - m_Name: Image + m_Name: Logo m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 diff --git a/Assets/Scripts/DatabaseClasses/Player.cs b/Assets/Scripts/DatabaseClasses/Player.cs index d03d3a8..11ed8d7 100644 --- a/Assets/Scripts/DatabaseClasses/Player.cs +++ b/Assets/Scripts/DatabaseClasses/Player.cs @@ -3,6 +3,7 @@ using System.Text.RegularExpressions; [System.Serializable] +[Newtonsoft.Json.JsonObject] public class Player { public string name; diff --git a/Assets/Scripts/DatabaseClasses/Prompt.cs b/Assets/Scripts/DatabaseClasses/Prompt.cs index c19c6e2..6cc576f 100644 --- a/Assets/Scripts/DatabaseClasses/Prompt.cs +++ b/Assets/Scripts/DatabaseClasses/Prompt.cs @@ -4,6 +4,7 @@ using UnityEngine; [System.Serializable, FirestoreData] +[Newtonsoft.Json.JsonObject] public class Prompt { [field: SerializeField] diff --git a/Assets/Scripts/DatabaseClasses/Proposition.cs b/Assets/Scripts/DatabaseClasses/Proposition.cs index 74d5059..65198d8 100644 --- a/Assets/Scripts/DatabaseClasses/Proposition.cs +++ b/Assets/Scripts/DatabaseClasses/Proposition.cs @@ -3,6 +3,7 @@ using UnityEngine; [System.Serializable] +[Newtonsoft.Json.JsonObject] public class Proposition { public string photoUrl; diff --git a/Assets/Scripts/DatabaseClasses/Question.cs b/Assets/Scripts/DatabaseClasses/Question.cs index 5bc2f3e..52f6837 100644 --- a/Assets/Scripts/DatabaseClasses/Question.cs +++ b/Assets/Scripts/DatabaseClasses/Question.cs @@ -4,6 +4,7 @@ using UnityEngine; [System.Serializable] +[Newtonsoft.Json.JsonObject] public class Question { public string promptId; diff --git a/Assets/Scripts/DatabaseClasses/Room.cs b/Assets/Scripts/DatabaseClasses/Room.cs index 27e43a2..e147eda 100644 --- a/Assets/Scripts/DatabaseClasses/Room.cs +++ b/Assets/Scripts/DatabaseClasses/Room.cs @@ -1,12 +1,15 @@ using System.Collections.Generic; using UnityEngine; +using Newtonsoft.Json; +using System.Collections; [System.Serializable] +[Newtonsoft.Json.JsonObject] public class Room { public string code; - public List questions; - public List players; + public Dictionary questions; + public Dictionary players; public int currentQuestion; public double creationDate; @@ -15,8 +18,19 @@ public Room(string _code) { this.code = _code; this.creationDate = System.DateTime.Now.ToOADate(); - this.players = new List(); - this.questions = new List(); + this.players = new Dictionary(); + this.questions = new Dictionary(); this.currentQuestion = 0; } + + public List GetPlayerList() + { + return new List(players.Values); + } + + public List GetQuestionList() + { + return new List(questions.Values); + } } + diff --git a/Assets/Scripts/GameManager.cs b/Assets/Scripts/GameManager.cs index 3411685..f7ff232 100644 --- a/Assets/Scripts/GameManager.cs +++ b/Assets/Scripts/GameManager.cs @@ -23,7 +23,7 @@ public class GameManager : MonoBehaviour public float explanationTime = 4f; private float currentExplanationTime = 0; - [Header("Home Component")] + [Header("Home Connection Component")] public TMP_InputField roomCodeField; public TextMeshProUGUI roomError; public TMP_InputField playerNameField; @@ -70,6 +70,7 @@ private void Start() submitNewPlayer.interactable = false; } + public GameState GetCurrentState() { return currentState; diff --git a/Assets/Scripts/RoomManager.cs b/Assets/Scripts/RoomManager.cs index 3535528..533fc34 100644 --- a/Assets/Scripts/RoomManager.cs +++ b/Assets/Scripts/RoomManager.cs @@ -6,11 +6,17 @@ using TMPro; using UnityEngine; using System; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using System.Linq; public class RoomManager : MonoBehaviour { + public List playerLabels = new List(); + + private RoomState currentState; - private Room currentRoom = null; + private Room myRoom = null; private List players; public float propositionTime = 60; @@ -41,6 +47,7 @@ public class RoomManager : MonoBehaviour private void Awake() { FirebaseInitializer.Instance.onFirebaseReady += Initialize; + currentState = RoomState.None; } @@ -48,13 +55,23 @@ private void Start() { propositionCurrentTime = propositionTime; votingCurrentTime = votingTime; + DisableAllPlayerLabels(); + currentState = RoomState.WaitingForPlayers; + } + + private void DisableAllPlayerLabels() + { + for (int i = 0; i < playerLabels.Count; i++) + { + playerLabels[i].text = $"Waiting for P{i + 1}"; + } } private void OnApplicationQuit() { - realtimeDB.Child("rooms").Child(currentRoom.code).RemoveValueAsync(); - Debug.Log($"delete room {currentRoom.code}"); - currentRoom = null; + realtimeDB.Child("rooms").Child(myRoom.code).RemoveValueAsync(); + Debug.Log($"delete room {myRoom.code}"); + myRoom = null; } private void Initialize() @@ -100,20 +117,21 @@ public void CreateNewRoom() WhichCodesAreAlreadyUsed(codes => { Room newRoom = new Room(GenerateRandomAvailableCode(codes).ToString("D4")); - currentRoom = newRoom; - + myRoom = 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"); - realtimeDB.Child("rooms").Child(newRoom.code).Child("players").ChildAdded += PlayerConnect; - //TODO MARINE : uncomment and reference the correct game object - //roomCodeLabel.text = currentRoom.code; + + //then subscribe to it + realtimeDB.Child("rooms").Child(newRoom.code).ValueChanged += OnRoomUpdate; + roomCodeLabel.text = myRoom.code; + Debug.Log($"room {myRoom.code} has been created on the server"); }); - } + } catch (Exception e) { Debug.LogException(e); @@ -216,28 +234,59 @@ public void GenerateCouples() } /// - /// is automatically called when a player connect to the room + /// Automatically called when something change in your room /// - /// - public void PlayerConnect(object sender, ChildChangedEventArgs args) + private void OnRoomUpdate(object sender, ValueChangedEventArgs value) { - if (args.DatabaseError != null) + Debug.Log("coucou"); + if (value.DatabaseError != null) { - Debug.LogError(args.DatabaseError.Message); + Debug.LogError(value.DatabaseError.Message); return; } + string JSON = value.Snapshot.GetRawJsonValue(); + Debug.Log(JSON); + try + { + myRoom = JsonConvert.DeserializeObject(JSON); + } + catch (Exception ex) + { + Debug.LogException(ex); + } - string JSON = args.Snapshot.GetRawJsonValue(); - Player joinedPlayer = JsonUtility.FromJson(JSON); - Debug.Log($"{joinedPlayer.name} has joined the room"); - //TODO Marine : do somtethjing with the newly joinde player + Debug.Log("caca"); + + switch (currentState) + { + case RoomState.WaitingForPlayers: + Debug.Log("prout"); + UpdateConnectedPlayerList(myRoom.GetPlayerList()); + break; + default: + break; + } + } + + /// + /// Update the player labels on the WaitingForPlayer page + /// + /// + private void UpdateConnectedPlayerList(List _players) + { + Debug.Log($"players count = {_players.Count}"); + for (int i = 0; i < _players.Count; i++) + { + Debug.Log($"player {i} = {_players[i].name}"); + playerLabels[i].text = _players[i].name; + } } [ContextMenu("Fake Player Connection")] private void FakePlayerConnection() { Player temp = new Player("Momo"); - temp.id = System.Guid.NewGuid().ToString(); + temp.id = Guid.NewGuid().ToString(); temp.SetName("Momo"); } @@ -245,7 +294,8 @@ private void FakePlayerConnection() public enum RoomState { - WaitingForPlayer, + None, + WaitingForPlayers, WaitingForPropositions, ShowPropositions, ShowVoters, diff --git a/Packages/manifest.json b/Packages/manifest.json index 91b486a..b696682 100644 --- a/Packages/manifest.json +++ b/Packages/manifest.json @@ -3,6 +3,7 @@ "com.unity.collab-proxy": "2.2.0", "com.unity.feature.development": "1.0.1", "com.unity.memoryprofiler": "1.1.0", + "com.unity.nuget.newtonsoft-json": "3.2.1", "com.unity.textmeshpro": "3.0.6", "com.unity.timeline": "1.7.6", "com.unity.toolchain.linux-x86_64": "2.0.6",