add crea date + room state

This commit is contained in:
Marine 2024-01-27 22:26:23 +01:00
parent a2e2eb5898
commit 762c05fa6c
3 changed files with 67 additions and 10 deletions

View File

@ -1,6 +1,7 @@
using Newtonsoft.Json; using Newtonsoft.Json;
using System; using System;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using UnityEngine;
[Serializable] [Serializable]
[JsonObject] [JsonObject]
@ -8,17 +9,21 @@ public class Player
{ {
public string name; public string name;
public string id; public string id;
public float creationDate;
[JsonConstructor] [JsonConstructor]
public Player(string _name, string _id) public Player(string _name, string _id, float _creationDate)
{ {
name = _name; name = _name;
id = _id; id = _id;
creationDate = _creationDate;
} }
public Player(string _name) public Player(string _name)
{ {
id = Guid.NewGuid().ToString(); id = Guid.NewGuid().ToString();
creationDate = Time.time;
SetName(_name); SetName(_name);
} }

View File

@ -11,6 +11,7 @@ public class Room
public Dictionary<string, Player> players; public Dictionary<string, Player> players;
public int currentQuestion; public int currentQuestion;
public double creationDate; public double creationDate;
public int currentState;
public Room(string _code) public Room(string _code)
@ -20,6 +21,7 @@ public Room(string _code)
players = new Dictionary<string, Player>(); players = new Dictionary<string, Player>();
questions = new Dictionary<string, Question>(); questions = new Dictionary<string, Question>();
currentQuestion = 0; currentQuestion = 0;
currentState = 0;
} }
public List<Player> GetPlayerList() public List<Player> GetPlayerList()
@ -31,5 +33,10 @@ public List<Question> GetQuestionList()
{ {
return new List<Question>(questions.Values); return new List<Question>(questions.Values);
} }
public void setPlayersAreReady(int _state)
{
currentState = _state;
}
} }

View File

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using Firebase.Database; using Firebase.Database;
using Firebase.Extensions; using Firebase.Extensions;
using Newtonsoft.Json;
using TMPro; using TMPro;
using UnityEngine; using UnityEngine;
@ -12,6 +13,7 @@ public class GameManager : MonoBehaviour
{ {
private GameState currentState; private GameState currentState;
private List<Player> players = new(); private List<Player> players = new();
private bool isFirst = false;
public Player currentPlayer = null; public Player currentPlayer = null;
public Question currentQuestion = null; public Question currentQuestion = null;
@ -54,6 +56,12 @@ private void Start()
HomeConnection.SetActive(true); HomeConnection.SetActive(true);
submitNewPlayer.interactable = false; submitNewPlayer.interactable = false;
} }
private void OnApplicationQuit()
{
myOnlineRoom.Child("players").Child(currentPlayer.id).RemoveValueAsync();
Debug.Log($"delete player {currentPlayer.name}");
myRoom = null;
}
public GameState GetCurrentState() public GameState GetCurrentState()
{ {
@ -123,7 +131,10 @@ public void PlayerValidateNameAndServerRoom(string _name, string _code)
WaitingRoom.SetActive(true); WaitingRoom.SetActive(true);
HomeConnection.SetActive(false); HomeConnection.SetActive(false);
UpdateDisplayedListUser(); List<Player> list = new List<Player>();
list.Add(currentPlayer);
UpdateDisplayedListUser(list) ;
}); });
} }
}); });
@ -187,9 +198,32 @@ private void JoinRoom(Action callback_OnRoomJoined)
public void StartGame() public void StartGame()
{ {
// send Start Game // send Start Game
currentState = GameState.Explanation; myRoom.setPlayersAreReady(1);
WaitingRoom.SetActive(false); string JSON = JsonUtility.ToJson(myRoom);
BeforeStart.SetActive(true); Debug.Log(JSON);
try
{
myOnlineRoom.Child("currentState").SetValueAsync(2).ContinueWithOnMainThread(task =>
{
if (task.IsFaulted)
{
Debug.LogException(task.Exception);
}
else
{
Debug.Log($"start the game", this);
currentState = GameState.Explanation;
WaitingRoom.SetActive(false);
BeforeStart.SetActive(true);
}
});
}
catch (Exception ex)
{
Debug.LogException(ex);
}
} }
/// <summary> /// <summary>
@ -243,7 +277,7 @@ private void OnRoomUpdate(object sender, ValueChangedEventArgs e)
{ {
try try
{ {
myRoom = JsonUtility.FromJson<Room>(e.Snapshot.GetRawJsonValue()); myRoom = JsonConvert.DeserializeObject<Room>(e.Snapshot.GetRawJsonValue());
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -254,16 +288,15 @@ private void OnRoomUpdate(object sender, ValueChangedEventArgs e)
{ {
case GameState.WaitingForOtherPlayersToJoin: case GameState.WaitingForOtherPlayersToJoin:
{ {
CheckIfIAmTheFirst(myRoom.GetPlayerList());
// players = new list en fonction de ce qu'envoie fangh UpdateDisplayedListUser(myRoom.GetPlayerList());
UpdateDisplayedListUser();
break; break;
} }
} }
} }
private void UpdateDisplayedListUser() private void UpdateDisplayedListUser(List<Player> players)
{ {
listPlayersUI.text = string.Empty; listPlayersUI.text = string.Empty;
for (int i = 0; i < players.Count; i++) for (int i = 0; i < players.Count; i++)
@ -271,6 +304,18 @@ private void UpdateDisplayedListUser()
listPlayersUI.text += "\n" + players[i].name; listPlayersUI.text += "\n" + players[i].name;
} }
} }
private void CheckIfIAmTheFirst(List<Player> players)
{
if (players.Count > 1)
{
}
else
{
}
}
public void OnClickSubmitSignIn() public void OnClickSubmitSignIn()