Snaparazzi/Assets/Scripts/GameManager.cs

329 lines
8.7 KiB
C#
Raw Normal View History

2024-01-27 15:25:53 +00:00
using System;
2024-01-27 12:19:56 +00:00
using System.Collections;
using System.Collections.Generic;
2024-01-27 15:32:12 +00:00
using Firebase.Database;
using Firebase.Extensions;
2024-01-27 12:46:54 +00:00
using TMPro;
2024-01-27 12:19:56 +00:00
using UnityEngine;
2024-01-27 12:46:54 +00:00
using UnityEngine.UI;
2024-01-27 15:25:53 +00:00
using UnityEngine.UIElements;
2024-01-27 12:19:56 +00:00
2024-01-27 12:46:54 +00:00
/// <summary>
/// This is the game state manager on the phone side
/// </summary>
2024-01-27 12:19:56 +00:00
public class GameManager : MonoBehaviour
{
2024-01-27 12:46:54 +00:00
private GameState currentState;
private List<Player> players = new List<Player>();
2024-01-27 17:45:15 +00:00
private Player currentPlayer = null;
2024-01-27 12:46:54 +00:00
2024-01-27 15:25:53 +00:00
[Header("Other component")]
2024-01-27 12:46:54 +00:00
public float explanationTime = 4f;
private float currentExplanationTime = 0;
2024-01-27 15:25:53 +00:00
[Header("Home Component")]
public TMP_InputField roomCodeField;
2024-01-27 12:46:54 +00:00
public TextMeshProUGUI roomError;
2024-01-27 15:25:53 +00:00
public TMP_InputField playerNameField;
2024-01-27 12:46:54 +00:00
public TextMeshProUGUI nameError;
2024-01-27 15:25:53 +00:00
public UnityEngine.UI.Button submitNewPlayer;
[Header("WaitingRoom Component")]
public TextMeshProUGUI listPlayersUI;
public GameObject submitStartGame;
[Header("Pages")]
[SerializeField]
public GameObject HomeConnection;
[SerializeField]
public GameObject WaitingRoom;
[SerializeField]
public GameObject BeforeStart;
[SerializeField]
public GameObject TakePicture;
[SerializeField]
public GameObject VotePicture;
[SerializeField]
public GameObject WaitingOtherPlayers;
[SerializeField]
public GameObject EndGame;
2024-01-27 12:46:54 +00:00
2024-01-27 15:32:12 +00:00
private DatabaseReference realtimeDB;
private Room myRoom;
2024-01-27 16:54:37 +00:00
private DatabaseReference myOnlineRoom;
2024-01-27 15:32:12 +00:00
private void Awake()
{
FirebaseInitializer.Instance.onFirebaseReady += Initialize;
}
2024-01-27 12:46:54 +00:00
private void Start()
{
currentExplanationTime = explanationTime;
2024-01-27 15:25:53 +00:00
HomeConnection.SetActive(true);
2024-01-27 17:18:03 +00:00
submitNewPlayer.interactable = false;
2024-01-27 12:46:54 +00:00
}
public GameState GetCurrentState()
2024-01-27 12:19:56 +00:00
{
2024-01-27 12:46:54 +00:00
return currentState;
}
2024-01-27 15:32:12 +00:00
private void Initialize()
{
FirebaseInitializer.Instance.onFirebaseReady -= Initialize;
realtimeDB = FirebaseDatabase.DefaultInstance.RootReference;
Debug.Log("Realtime DB initialized");
2024-01-27 17:18:03 +00:00
submitNewPlayer.interactable = true;
2024-01-27 15:32:12 +00:00
}
2024-01-27 12:46:54 +00:00
/// <summary>
/// Send your name and game room to the server
/// </summary>
2024-01-27 15:32:12 +00:00
public void PlayerValidateNameAndServerRoom(string _name, string _code)
2024-01-27 12:46:54 +00:00
{
2024-01-27 16:54:37 +00:00
if (string.IsNullOrEmpty(_name))
{
Debug.LogError("Player name is empty", this);
//TODO : MARINE : use the error label to explain to the user that they have forget to put a name
return;
}
if (string.IsNullOrEmpty(_code))
{
Debug.LogError("Room code is empty", this);
//TODO : MARINE : use the error label to explain to the user that they have forget to put a room code
return;
}
2024-01-27 12:46:54 +00:00
//check if the room exists, if not display an error message
2024-01-27 16:54:37 +00:00
CheckIfRoomExists(_code, room =>
2024-01-27 15:32:12 +00:00
{
2024-01-27 16:54:37 +00:00
if (room == null)
2024-01-27 15:32:12 +00:00
{
2024-01-27 16:54:37 +00:00
Debug.LogError("The room doesn't exists");
roomError.text = "Error: the room doesn't exists";
roomError.gameObject.SetActive(true);
2024-01-27 15:32:12 +00:00
}
2024-01-27 16:54:37 +00:00
else
2024-01-27 15:32:12 +00:00
{
2024-01-27 16:54:37 +00:00
//if room exists, join it
JoinRoom(() =>
2024-01-27 15:32:12 +00:00
{
2024-01-27 16:54:37 +00:00
//then subscribe to it
myOnlineRoom = realtimeDB.Child("rooms").Child(_code);
myOnlineRoom.ChildChanged += OnRoomUpdate;
Debug.Log($"room {myRoom.code} exists, I subscribe to it");
});
2024-01-27 15:32:12 +00:00
}
});
2024-01-27 16:54:37 +00:00
2024-01-27 12:19:56 +00:00
}
2024-01-27 16:54:37 +00:00
private void CheckIfRoomExists(string _roomCode, Action<Room> callback_Room)
{
realtimeDB.Child("rooms").Child(_roomCode).GetValueAsync().ContinueWithOnMainThread(task =>
2024-01-27 15:32:12 +00:00
{
if (task.IsFaulted)
{
Debug.LogException(task.Exception);
}
else if (task.IsCompleted)
{
DataSnapshot snapshot = task.Result;
if (snapshot == null)
{
2024-01-27 16:54:37 +00:00
callback_Room?.Invoke(null);
2024-01-27 15:32:12 +00:00
}
else
{
2024-01-27 16:54:37 +00:00
callback_Room?.Invoke(JsonUtility.FromJson<Room>(snapshot.GetRawJsonValue()));
2024-01-27 15:32:12 +00:00
}
}
});
2024-01-27 15:25:53 +00:00
2024-01-27 16:54:37 +00:00
}
2024-01-27 15:25:53 +00:00
2024-01-27 16:54:37 +00:00
/// <summary>
/// Add this player to the room
/// </summary>
private void JoinRoom(Action callback_OnRoomJoined)
{
string JSON = JsonUtility.ToJson(currentPlayer);
myOnlineRoom.Child("players").Child(currentPlayer.id).SetRawJsonValueAsync(JSON).ContinueWithOnMainThread(task =>
{
if (task.IsFaulted)
{
Debug.LogException(task.Exception);
}
else
{
Debug.Log($"{currentPlayer.name} has been added to the room", this);
callback_OnRoomJoined?.Invoke();
}
});
2024-01-27 12:19:56 +00:00
}
2024-01-27 12:46:54 +00:00
/// <summary>
/// Call this only by the first player
/// </summary>
public void StartGame()
2024-01-27 12:19:56 +00:00
{
2024-01-27 15:25:53 +00:00
// send Start Game
currentState = GameState.Explanation;
WaitingRoom.SetActive(false);
BeforeStart.SetActive(true);
2024-01-27 12:19:56 +00:00
}
2024-01-27 12:46:54 +00:00
/// <summary>
/// Dislay on the screen the current prompt ask to you and another player
/// Also show all the camera stuff
/// </summary>
/// <param name="_prompt">The prompt to display</param>
public void MakeAProposition(Prompt _prompt)
{
2024-01-27 15:25:53 +00:00
currentState = GameState.MakeProposition;
2024-01-27 12:46:54 +00:00
2024-01-27 16:54:37 +00:00
2024-01-27 12:46:54 +00:00
}
/// <summary>
/// Create a proposition from the picture taken and send it to server.
/// Then go to next proposition (if any) or the page "WaitingForOtherPlayers"
/// </summary>
public void SubmitProposition()
{
Proposition temp = new Proposition();
}
/// <summary>
/// Display the voting page
/// </summary>
public void StartToVote()
{
}
/// <summary>
/// Choose one result and send it to the server
/// Then go to the next vote (if any) or to the page "WaitingForOtherPlayers"
/// </summary>
public void Vote()
{
}
/// <summary>
/// Display the UI of the end screen
/// </summary>
public void DisplayEndScreen()
{
}
/// <summary>
/// Automatically called when something change in your room
/// </summary>
2024-01-27 15:32:12 +00:00
private void OnRoomUpdate(object sender, ChildChangedEventArgs e)
2024-01-27 12:46:54 +00:00
{
2024-01-27 15:32:12 +00:00
myRoom = JsonUtility.FromJson<Room>(e.Snapshot.GetRawJsonValue());
2024-01-27 15:25:53 +00:00
switch (currentState)
{
case GameState.WaitingForOtherPlayersToJoin:
{
2024-01-27 16:56:03 +00:00
WaitingRoom.SetActive(true);
HomeConnection.SetActive(false);
2024-01-27 15:25:53 +00:00
// players = new list en fonction de ce qu'envoie fangh
UpdateDisplayedListUser();
2024-01-27 16:56:03 +00:00
break;
}
case GameState.EnteringName:
{
currentState = GameState.WaitingForOtherPlayersToJoin;
2024-01-27 17:45:15 +00:00
2024-01-27 16:56:03 +00:00
//TEMP
players.Add(currentPlayer);
if (players.Count == 1)
{
submitStartGame.SetActive(true);
}
2024-01-27 17:10:17 +00:00
Player player2 = new Player("3J");
2024-01-27 16:56:03 +00:00
player2.SetName("3J");
player2.id = "2";
2024-01-27 17:10:17 +00:00
Player player3 = new Player("3J");
2024-01-27 16:56:03 +00:00
player3.SetName("3J");
player3.id = "3";
2024-01-27 17:10:17 +00:00
Player player4 = new Player("3J");
2024-01-27 16:56:03 +00:00
player4.SetName("3J");
player4.id = "4";
2024-01-27 17:10:17 +00:00
Player player5 = new Player("3J");
2024-01-27 16:56:03 +00:00
player5.SetName("3J");
player5.id = "5";
2024-01-27 17:10:17 +00:00
Player player6 = new Player("3J");
2024-01-27 16:56:03 +00:00
player6.SetName("3J");
player6.id = "6";
players.Add(player2);
players.Add(player3);
players.Add(player4);
players.Add(player5);
players.Add(player6);
UpdateDisplayedListUser();
2024-01-27 15:25:53 +00:00
break;
}
}
}
2024-01-27 12:46:54 +00:00
2024-01-27 15:25:53 +00:00
private void UpdateDisplayedListUser()
{
listPlayersUI.text = string.Empty;
for (int i = 0; i < players.Count; i++)
{
listPlayersUI.text += "\n" + players[i].name;
}
2024-01-27 12:46:54 +00:00
}
2024-01-27 16:56:03 +00:00
public void OnClickSubmitSignIn()
{
string playerName = playerNameField.text;
string roomCode = roomCodeField.text;
PlayerValidateNameAndServerRoom(playerName, roomCode);
}
2024-01-27 12:19:56 +00:00
}
public enum GameState
{
EnteringName,
2024-01-27 12:46:54 +00:00
WaitingForOtherPlayersToJoin,
2024-01-27 12:19:56 +00:00
Explanation,
MakeProposition,
2024-01-27 12:46:54 +00:00
PropositionsSent,
WaitingForOtherPlayers,
2024-01-27 12:19:56 +00:00
VotingTime,
Ending
}