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_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

View File

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

View File

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

View File

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

View File

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

View File

@ -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<Question> questions;
public List<Player> players;
public Dictionary<string, Question> questions;
public Dictionary<string, Player> 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<Player>();
this.questions = new List<Question>();
this.players = new Dictionary<string, Player>();
this.questions = new Dictionary<string, Question>();
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;
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;

View File

@ -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<TextMeshProUGUI> playerLabels = new List<TextMeshProUGUI>();
private RoomState currentState;
private Room currentRoom = null;
private Room myRoom = null;
private List<Player> 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,7 +117,7 @@ public void CreateNewRoom()
WhichCodesAreAlreadyUsed(codes =>
{
Room newRoom = new Room(GenerateRandomAvailableCode(codes).ToString("D4"));
currentRoom = newRoom;
myRoom = newRoom;
try
{
@ -108,12 +125,13 @@ public void CreateNewRoom()
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()
}
/// <summary>
/// is automatically called when a player connect to the room
/// Automatically called when something change in your room
/// </summary>
/// <param name="_player"></param>
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<Room>(JSON);
}
catch (Exception ex)
{
Debug.LogException(ex);
}
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
Debug.Log("caca");
switch (currentState)
{
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")]
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,

View File

@ -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",