This commit is contained in:
Otaku9999 2024-01-28 00:03:16 +01:00
commit d7eb0c52d9
3 changed files with 78 additions and 44 deletions

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using Firebase.Database; using Firebase.Database;
using Firebase.Extensions; using Firebase.Extensions;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -11,9 +12,7 @@
/// </summary> /// </summary>
public class GameManager : MonoBehaviour public class GameManager : MonoBehaviour
{ {
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;
[Header("Other component")] [Header("Other component")]
@ -57,15 +56,13 @@ private void Start()
} }
private void OnApplicationQuit() private void OnApplicationQuit()
{ {
myOnlineRoom.Child("players").Child(currentPlayer.id).RemoveValueAsync(); myOnlineRoom.Child("players").Child(currentPlayer.id).RemoveValueAsync().ContinueWithOnMainThread(task =>
Debug.Log($"delete player {currentPlayer.name}"); {
myRoom = null; Debug.Log($"delete player {currentPlayer.name}");
myOnlineRoom.ValueChanged -= OnRoomUpdate;
});
} }
public GameState GetCurrentState()
{
return currentState;
}
private void Initialize() private void Initialize()
{ {
@ -124,7 +121,7 @@ public void PlayerValidateNameAndServerRoom(string _name, string _code)
{ {
//then subscribe to it //then subscribe to it
myOnlineRoom.ValueChanged += OnRoomUpdate; myOnlineRoom.ValueChanged += OnRoomUpdate;
currentState = GameState.WaitingForOtherPlayersToJoin; myRoom.currentState = (int)GameState.WaitingForOtherPlayersToJoin;
players.Add(currentPlayer); players.Add(currentPlayer);
WaitingRoom.SetActive(true); WaitingRoom.SetActive(true);
@ -202,19 +199,13 @@ public void StartGame()
Debug.Log(JSON); Debug.Log(JSON);
try try
{ {
myOnlineRoom.Child("currentState").SetValueAsync(2).ContinueWithOnMainThread(task => sendCurrentState(GameState.Explanation, () =>
{ {
if (task.IsFaulted) Debug.Log($"start the game", this);
{ myRoom.currentState = (int)GameState.Explanation;
Debug.LogException(task.Exception); WaitingRoom.SetActive(false);
} BeforeStart.SetActive(true);
else
{
Debug.Log($"start the game", this);
currentState = GameState.Explanation;
WaitingRoom.SetActive(false);
BeforeStart.SetActive(true);
}
}); });
} }
catch (Exception ex) catch (Exception ex)
@ -232,7 +223,7 @@ public void StartGame()
/// <param name="_prompt">The prompt to display</param> /// <param name="_prompt">The prompt to display</param>
public void MakeAProposition(Prompt _prompt) public void MakeAProposition(Prompt _prompt)
{ {
currentState = GameState.MakeProposition; //currentState = GameState.MakeProposition;
} }
/// <summary> /// <summary>
@ -282,13 +273,30 @@ private void OnRoomUpdate(object sender, ValueChangedEventArgs e)
{ {
Debug.LogException(ex); Debug.LogException(ex);
} }
if(myRoom == null) {
switch (currentState) return;
}
switch (myRoom.currentState)
{ {
case GameState.WaitingForOtherPlayersToJoin: case (int)GameState.WaitingForOtherPlayersToJoin:
{ {
CheckIfIAmTheFirst(myRoom.GetPlayerList()); CheckIfIAmTheFirst(myRoom.GetPlayerList());
UpdateDisplayedListUser(myRoom.GetPlayerList()); UpdateDisplayedListUser(myRoom.GetPlayerList());
break;
}
case (int)GameState.Explanation:
{
break;
}
case (int)GameState.MakeProposition:
{
if (BeforeStart.activeInHierarchy)
{
BeforeStart.SetActive(false);
TakePicture.SetActive(true);
}
break; break;
} }
} }
@ -306,16 +314,38 @@ private void UpdateDisplayedListUser(List<Player> players)
private void CheckIfIAmTheFirst(List<Player> players) private void CheckIfIAmTheFirst(List<Player> players)
{ {
bool isFirst = false;
if (players.Count > 1) if (players.Count > 1)
{ {
IOrderedEnumerable<Player> sortedList = players.OrderBy(x=>x.creationDate);
if(sortedList.Last().id == currentPlayer.id)
{
isFirst = true;
}
} }
else
if (isFirst)
{ {
submitStartGame.SetActive(true);
} }
} }
public void sendCurrentState(GameState state,Action callback_oncCurrentStateSent)
{
myOnlineRoom.Child("currentState").SetValueAsync((int)state).ContinueWithOnMainThread(task =>
{
if (task.IsFaulted)
{
Debug.LogException(task.Exception);
}
else
{
callback_oncCurrentStateSent?.Invoke();
}
}); ;
}
public void OnClickSubmitSignIn() public void OnClickSubmitSignIn()
{ {

View File

@ -30,9 +30,7 @@ public class RoomManager : MonoBehaviour
private float propositionCurrentTime = 0; private float propositionCurrentTime = 0;
public TextMeshProUGUI timerGUI; public TextMeshProUGUI timerGUI;
private GameState currentState;
private Room myRoom = null; private Room myRoom = null;
public float votingTime = 20; public float votingTime = 20;
private float votingCurrentTime = 0; private float votingCurrentTime = 0;
@ -54,7 +52,6 @@ public class RoomManager : MonoBehaviour
private void Awake() private void Awake()
{ {
FirebaseInitializer.Instance.onFirebaseReady += Initialize; FirebaseInitializer.Instance.onFirebaseReady += Initialize;
currentState = GameState.EnteringName;
} }
@ -63,12 +60,14 @@ private void Start()
propositionCurrentTime = propositionTime; propositionCurrentTime = propositionTime;
votingCurrentTime = votingTime; votingCurrentTime = votingTime;
ResetAllPlayerLabels(); ResetAllPlayerLabels();
currentState = GameState.WaitingForOtherPlayersToJoin;
} }
private void Update() private void Update()
{ {
if (currentState == GameState.Explanation && endOfExplanationDate != DateTime.MinValue) if (myRoom == null)
return;
if (myRoom.currentState == (int)GameState.Explanation && endOfExplanationDate != DateTime.MinValue)
{ {
TimeSpan duration = endOfExplanationDate - DateTime.Now; TimeSpan duration = endOfExplanationDate - DateTime.Now;
counter.text = duration.TotalSeconds.ToString("D1"); counter.text = duration.TotalSeconds.ToString("D1");
@ -76,11 +75,16 @@ private void Update()
if (duration.TotalMilliseconds <= 0) if (duration.TotalMilliseconds <= 0)
{ {
Debug.Log("It's time to make proposition !", this); Debug.Log("It's time to make proposition !", this);
currentState = GameState.MakeProposition; SendRoomState(GameState.MakeProposition);
} }
} }
} }
private void SendRoomState(GameState _newState)
{
realtimeDB.Child("rooms").Child(myRoom.code).Child("currentState").SetValueAsync((int)_newState);
}
private void ResetAllPlayerLabels() private void ResetAllPlayerLabels()
{ {
for (int i = 0; i < waitingForPlayersLabels.Count; i++) for (int i = 0; i < waitingForPlayersLabels.Count; i++)
@ -207,7 +211,6 @@ public void PlayerSendProposition(Proposition _proposition)
/// </summary> /// </summary>
public void HostStartGame() public void HostStartGame()
{ {
currentState = GameState.MakeProposition;
waitingForPropositionsPage.SetActive(true); waitingForPropositionsPage.SetActive(true);
waitingForPlayersPage.SetActive(false); waitingForPlayersPage.SetActive(false);
propositionCurrentTime = propositionTime; propositionCurrentTime = propositionTime;
@ -313,7 +316,7 @@ private void OnRoomUpdate(object sender, ValueChangedEventArgs value)
return; return;
} }
string JSON = value.Snapshot.GetRawJsonValue(); string JSON = value.Snapshot.GetRawJsonValue();
Debug.Log(JSON); Debug.Log($"your room has been updated :\n{JSON}");
try try
{ {
myRoom = JsonConvert.DeserializeObject<Room>(JSON); myRoom = JsonConvert.DeserializeObject<Room>(JSON);
@ -324,17 +327,17 @@ private void OnRoomUpdate(object sender, ValueChangedEventArgs value)
} }
switch (currentState) switch (myRoom.currentState)
{ {
case GameState.WaitingForOtherPlayersToJoin: case (int)GameState.WaitingForOtherPlayersToJoin:
UpdateConnectedPlayerList(myRoom.GetPlayerList()); UpdateConnectedPlayerList(myRoom.GetPlayerList());
break; break;
case GameState.Explanation: case (int)GameState.Explanation:
waitingForPlayersPage.SetActive(false); waitingForPlayersPage.SetActive(false);
explanationPage.SetActive(true); explanationPage.SetActive(true);
endOfExplanationDate = DateTime.Now.AddSeconds(30); endOfExplanationDate = DateTime.Now.AddSeconds(30);
break; break;
case GameState.MakeProposition: case (int)GameState.MakeProposition:
HostStartGame(); HostStartGame();
break; break;
default: default:

View File

@ -8,18 +8,20 @@ public class StorageManager : MonoBehaviour
{ {
public GameObject PicturePlayer; public GameObject PicturePlayer;
public GameObject Canvas; public GameObject Canvas;
private FirebaseStorage storage; private StorageReference storage;
private DatabaseReference realtimeDB; private DatabaseReference realtimeDB;
void Awake() void Awake()
{ {
Debug.Log("non");
FirebaseInitializer.Instance.onFirebaseReady += Initialize; FirebaseInitializer.Instance.onFirebaseReady += Initialize;
} }
void Initialize() void Initialize()
{ {
Debug.Log("oui");
FirebaseInitializer.Instance.onFirebaseReady -= Initialize; FirebaseInitializer.Instance.onFirebaseReady -= Initialize;
storage = FirebaseStorage.DefaultInstance; storage = FirebaseStorage.DefaultInstance.RootReference;
realtimeDB = FirebaseDatabase.DefaultInstance.RootReference; realtimeDB = FirebaseDatabase.DefaultInstance.RootReference;
} }
@ -43,8 +45,7 @@ public void UploadPhoto()
GameManager game = Canvas.GetComponent<GameManager>(); GameManager game = Canvas.GetComponent<GameManager>();
string imageUuid = Guid.NewGuid().ToString(); string imageUuid = Guid.NewGuid().ToString();
StorageReference storageRef = storage.GetReferenceFromUrl("gs://ggj2024-5cb41.appspot.com"); StorageReference imageRef = storage.Child(game.myRoom.code).Child(game.currentPlayer.id).Child($"{imageUuid}.jpg");
StorageReference imageRef = storageRef.Child(game.myRoom.code).Child(game.currentPlayer.id).Child($"{imageUuid}.jpg");
imageRef.PutBytesAsync(photoBytes).ContinueWith((Task<StorageMetadata> task) => imageRef.PutBytesAsync(photoBytes).ContinueWith((Task<StorageMetadata> task) =>
{ {