players can join room

This commit is contained in:
Morgan - 6 Freedom 2024-01-27 19:54:06 +01:00
parent a21f360440
commit df6c4f08c9
9 changed files with 110 additions and 31 deletions

View File

@ -1325,9 +1325,18 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: de098f8fd5f884a1aa55db7874246b92, type: 3} m_Script: {fileID: 11500000, guid: de098f8fd5f884a1aa55db7874246b92, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: 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 votingTime: 20
roomCodeLabel: {fileID: 0} roomCodeLabel: {fileID: 1805240027}
--- !u!114 &375256413 --- !u!114 &375256413
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -6916,7 +6925,7 @@ GameObject:
- component: {fileID: 1901141297} - component: {fileID: 1901141297}
- component: {fileID: 1901141296} - component: {fileID: 1901141296}
m_Layer: 5 m_Layer: 5
m_Name: Image m_Name: Logo
m_TagString: Untagged m_TagString: Untagged
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0

View File

@ -3,6 +3,7 @@
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
[System.Serializable] [System.Serializable]
[Newtonsoft.Json.JsonObject]
public class Player public class Player
{ {
public string name; public string name;

View File

@ -4,6 +4,7 @@
using UnityEngine; using UnityEngine;
[System.Serializable, FirestoreData] [System.Serializable, FirestoreData]
[Newtonsoft.Json.JsonObject]
public class Prompt public class Prompt
{ {
[field: SerializeField] [field: SerializeField]

View File

@ -3,6 +3,7 @@
using UnityEngine; using UnityEngine;
[System.Serializable] [System.Serializable]
[Newtonsoft.Json.JsonObject]
public class Proposition public class Proposition
{ {
public string photoUrl; public string photoUrl;

View File

@ -4,6 +4,7 @@
using UnityEngine; using UnityEngine;
[System.Serializable] [System.Serializable]
[Newtonsoft.Json.JsonObject]
public class Question public class Question
{ {
public string promptId; public string promptId;

View File

@ -1,12 +1,15 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using Newtonsoft.Json;
using System.Collections;
[System.Serializable] [System.Serializable]
[Newtonsoft.Json.JsonObject]
public class Room public class Room
{ {
public string code; public string code;
public List<Question> questions; public Dictionary<string, Question> questions;
public List<Player> players; public Dictionary<string, Player> players;
public int currentQuestion; public int currentQuestion;
public double creationDate; public double creationDate;
@ -15,8 +18,19 @@ public Room(string _code)
{ {
this.code = _code; this.code = _code;
this.creationDate = System.DateTime.Now.ToOADate(); this.creationDate = System.DateTime.Now.ToOADate();
this.players = new List<Player>(); this.players = new Dictionary<string, Player>();
this.questions = new List<Question>(); this.questions = new Dictionary<string, Question>();
this.currentQuestion = 0; this.currentQuestion = 0;
} }
public List<Player> GetPlayerList()
{
return new List<Player>(players.Values);
}
public List<Question> GetQuestionList()
{
return new List<Question>(questions.Values);
}
} }

View File

@ -23,7 +23,7 @@ public class GameManager : MonoBehaviour
public float explanationTime = 4f; public float explanationTime = 4f;
private float currentExplanationTime = 0; private float currentExplanationTime = 0;
[Header("Home Component")] [Header("Home Connection Component")]
public TMP_InputField roomCodeField; public TMP_InputField roomCodeField;
public TextMeshProUGUI roomError; public TextMeshProUGUI roomError;
public TMP_InputField playerNameField; public TMP_InputField playerNameField;
@ -70,6 +70,7 @@ private void Start()
submitNewPlayer.interactable = false; submitNewPlayer.interactable = false;
} }
public GameState GetCurrentState() public GameState GetCurrentState()
{ {
return currentState; return currentState;

View File

@ -6,11 +6,17 @@
using TMPro; using TMPro;
using UnityEngine; using UnityEngine;
using System; using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Linq;
public class RoomManager : MonoBehaviour public class RoomManager : MonoBehaviour
{ {
public List<TextMeshProUGUI> playerLabels = new List<TextMeshProUGUI>();
private RoomState currentState; private RoomState currentState;
private Room currentRoom = null; private Room myRoom = null;
private List<Player> players; private List<Player> players;
public float propositionTime = 60; public float propositionTime = 60;
@ -41,6 +47,7 @@ public class RoomManager : MonoBehaviour
private void Awake() private void Awake()
{ {
FirebaseInitializer.Instance.onFirebaseReady += Initialize; FirebaseInitializer.Instance.onFirebaseReady += Initialize;
currentState = RoomState.None;
} }
@ -48,13 +55,23 @@ private void Start()
{ {
propositionCurrentTime = propositionTime; propositionCurrentTime = propositionTime;
votingCurrentTime = votingTime; 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() private void OnApplicationQuit()
{ {
realtimeDB.Child("rooms").Child(currentRoom.code).RemoveValueAsync(); realtimeDB.Child("rooms").Child(myRoom.code).RemoveValueAsync();
Debug.Log($"delete room {currentRoom.code}"); Debug.Log($"delete room {myRoom.code}");
currentRoom = null; myRoom = null;
} }
private void Initialize() private void Initialize()
@ -100,20 +117,21 @@ public void CreateNewRoom()
WhichCodesAreAlreadyUsed(codes => WhichCodesAreAlreadyUsed(codes =>
{ {
Room newRoom = new Room(GenerateRandomAvailableCode(codes).ToString("D4")); Room newRoom = new Room(GenerateRandomAvailableCode(codes).ToString("D4"));
currentRoom = newRoom; myRoom = newRoom;
try try
{ {
string JSON = JsonUtility.ToJson(newRoom); string JSON = JsonUtility.ToJson(newRoom);
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");
realtimeDB.Child("rooms").Child(newRoom.code).Child("players").ChildAdded += PlayerConnect; //then subscribe to it
//TODO MARINE : uncomment and reference the correct game object realtimeDB.Child("rooms").Child(newRoom.code).ValueChanged += OnRoomUpdate;
//roomCodeLabel.text = currentRoom.code; roomCodeLabel.text = myRoom.code;
Debug.Log($"room {myRoom.code} has been created on the server");
}); });
} }
catch (Exception e) catch (Exception e)
{ {
Debug.LogException(e); Debug.LogException(e);
@ -216,28 +234,59 @@ public void GenerateCouples()
} }
/// <summary> /// <summary>
/// is automatically called when a player connect to the room /// Automatically called when something change in your room
/// </summary> /// </summary>
/// <param name="_player"></param> private void OnRoomUpdate(object sender, ValueChangedEventArgs value)
public void PlayerConnect(object sender, ChildChangedEventArgs args)
{ {
if (args.DatabaseError != null) Debug.Log("coucou");
if (value.DatabaseError != null)
{ {
Debug.LogError(args.DatabaseError.Message); Debug.LogError(value.DatabaseError.Message);
return; return;
} }
string JSON = value.Snapshot.GetRawJsonValue();
Debug.Log(JSON);
try
{
myRoom = JsonConvert.DeserializeObject<Room>(JSON);
}
catch (Exception ex)
{
Debug.LogException(ex);
}
string JSON = args.Snapshot.GetRawJsonValue(); Debug.Log("caca");
Player joinedPlayer = JsonUtility.FromJson<Player>(JSON);
Debug.Log($"{joinedPlayer.name} has joined the room"); switch (currentState)
//TODO Marine : do somtethjing with the newly joinde player {
case RoomState.WaitingForPlayers:
Debug.Log("prout");
UpdateConnectedPlayerList(myRoom.GetPlayerList());
break;
default:
break;
}
}
/// <summary>
/// Update the player labels on the WaitingForPlayer page
/// </summary>
/// <param name="_players"></param>
private void UpdateConnectedPlayerList(List<Player> _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")] [ContextMenu("Fake Player Connection")]
private void FakePlayerConnection() private void FakePlayerConnection()
{ {
Player temp = new Player("Momo"); Player temp = new Player("Momo");
temp.id = System.Guid.NewGuid().ToString(); temp.id = Guid.NewGuid().ToString();
temp.SetName("Momo"); temp.SetName("Momo");
} }
@ -245,7 +294,8 @@ private void FakePlayerConnection()
public enum RoomState public enum RoomState
{ {
WaitingForPlayer, None,
WaitingForPlayers,
WaitingForPropositions, WaitingForPropositions,
ShowPropositions, ShowPropositions,
ShowVoters, ShowVoters,

View File

@ -3,6 +3,7 @@
"com.unity.collab-proxy": "2.2.0", "com.unity.collab-proxy": "2.2.0",
"com.unity.feature.development": "1.0.1", "com.unity.feature.development": "1.0.1",
"com.unity.memoryprofiler": "1.1.0", "com.unity.memoryprofiler": "1.1.0",
"com.unity.nuget.newtonsoft-json": "3.2.1",
"com.unity.textmeshpro": "3.0.6", "com.unity.textmeshpro": "3.0.6",
"com.unity.timeline": "1.7.6", "com.unity.timeline": "1.7.6",
"com.unity.toolchain.linux-x86_64": "2.0.6", "com.unity.toolchain.linux-x86_64": "2.0.6",