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

View File

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

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using Firebase.Database;
using Firebase.Extensions;
using Newtonsoft.Json;
using TMPro;
using UnityEngine;
@ -12,6 +13,7 @@ public class GameManager : MonoBehaviour
{
private GameState currentState;
private List<Player> players = new();
private bool isFirst = false;
public Player currentPlayer = null;
public Question currentQuestion = null;
@ -54,6 +56,12 @@ private void Start()
HomeConnection.SetActive(true);
submitNewPlayer.interactable = false;
}
private void OnApplicationQuit()
{
myOnlineRoom.Child("players").Child(currentPlayer.id).RemoveValueAsync();
Debug.Log($"delete player {currentPlayer.name}");
myRoom = null;
}
public GameState GetCurrentState()
{
@ -123,7 +131,10 @@ public void PlayerValidateNameAndServerRoom(string _name, string _code)
WaitingRoom.SetActive(true);
HomeConnection.SetActive(false);
UpdateDisplayedListUser();
List<Player> list = new List<Player>();
list.Add(currentPlayer);
UpdateDisplayedListUser(list) ;
});
}
});
@ -187,10 +198,33 @@ private void JoinRoom(Action callback_OnRoomJoined)
public void StartGame()
{
// send Start Game
myRoom.setPlayersAreReady(1);
string JSON = JsonUtility.ToJson(myRoom);
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>
/// Dislay on the screen the current prompt ask to you and another player
@ -243,7 +277,7 @@ private void OnRoomUpdate(object sender, ValueChangedEventArgs e)
{
try
{
myRoom = JsonUtility.FromJson<Room>(e.Snapshot.GetRawJsonValue());
myRoom = JsonConvert.DeserializeObject<Room>(e.Snapshot.GetRawJsonValue());
}
catch (Exception ex)
{
@ -254,16 +288,15 @@ private void OnRoomUpdate(object sender, ValueChangedEventArgs e)
{
case GameState.WaitingForOtherPlayersToJoin:
{
// players = new list en fonction de ce qu'envoie fangh
UpdateDisplayedListUser();
CheckIfIAmTheFirst(myRoom.GetPlayerList());
UpdateDisplayedListUser(myRoom.GetPlayerList());
break;
}
}
}
private void UpdateDisplayedListUser()
private void UpdateDisplayedListUser(List<Player> players)
{
listPlayersUI.text = string.Empty;
for (int i = 0; i < players.Count; i++)
@ -272,6 +305,18 @@ private void UpdateDisplayedListUser()
}
}
private void CheckIfIAmTheFirst(List<Player> players)
{
if (players.Count > 1)
{
}
else
{
}
}
public void OnClickSubmitSignIn()
{