Snaparazzi/Assets/Scripts/RoomManager.cs

313 lines
8.8 KiB
C#
Raw Normal View History

2024-01-27 14:08:12 +00:00
using Firebase.Database;
2024-01-27 14:54:23 +00:00
using Firebase.Extensions;
2024-01-27 18:58:02 +00:00
using System;
using System.Collections.Generic;
2024-01-27 14:54:23 +00:00
using TMPro;
2024-01-27 09:19:32 +00:00
using UnityEngine;
2024-01-27 18:54:06 +00:00
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Linq;
2024-01-27 09:19:32 +00:00
public class RoomManager : MonoBehaviour
{
2024-01-27 21:24:50 +00:00
[Header("Waiting For Players Page")]
public GameObject waitingForPlayersPage;
/// <summary>
/// TextMeshPro that show the value of the current rooom code
/// </summary>
public TextMeshProUGUI roomCodeLabel;
public List<TextMeshProUGUI> waitingForPlayersLabels = new List<TextMeshProUGUI>();
2024-01-27 18:54:06 +00:00
2024-01-27 09:19:32 +00:00
2024-01-27 21:24:50 +00:00
[Header("Waiting For Proposition Page")]
public GameObject waitingForPropositionsPage;
public List<TextMeshProUGUI> waitingForPropositionsLabels = new List<TextMeshProUGUI>();
2024-01-27 12:19:56 +00:00
public float propositionTime = 60;
2024-01-27 21:24:50 +00:00
private float propositionCurrentTime = 0;
public TextMeshProUGUI timerGUI;
private GameState currentState;
private Room myRoom = null;
2024-01-27 12:19:56 +00:00
public float votingTime = 20;
private float votingCurrentTime = 0;
/// <summary>
/// Contain the infos about the current displayed question (during votes)
/// </summary>
private Question currentQuestion;
private List<Question> questions;
/// <summary>
/// When this is equal to questions.Count, go to score page
/// </summary>
private int numberOfQuestionVoted = 0;
2024-01-27 14:08:12 +00:00
DatabaseReference realtimeDB;
private void Awake()
{
FirebaseInitializer.Instance.onFirebaseReady += Initialize;
2024-01-27 21:24:50 +00:00
currentState = GameState.EnteringName;
2024-01-27 14:08:12 +00:00
}
2024-01-27 12:19:56 +00:00
private void Start()
{
propositionCurrentTime = propositionTime;
votingCurrentTime = votingTime;
2024-01-27 18:54:06 +00:00
DisableAllPlayerLabels();
2024-01-27 21:24:50 +00:00
currentState = GameState.WaitingForOtherPlayersToJoin;
2024-01-27 18:54:06 +00:00
}
private void DisableAllPlayerLabels()
{
2024-01-27 21:24:50 +00:00
for (int i = 0; i < waitingForPlayersLabels.Count; i++)
2024-01-27 18:54:06 +00:00
{
2024-01-27 21:24:50 +00:00
waitingForPlayersLabels[i].text = $"Waiting for P{i + 1}";
2024-01-27 18:54:06 +00:00
}
2024-01-27 14:08:12 +00:00
}
private void OnApplicationQuit()
{
2024-01-27 18:54:06 +00:00
realtimeDB.Child("rooms").Child(myRoom.code).RemoveValueAsync();
Debug.Log($"delete room {myRoom.code}");
myRoom = null;
2024-01-27 14:08:12 +00:00
}
private void Initialize()
{
FirebaseInitializer.Instance.onFirebaseReady -= Initialize;
realtimeDB = FirebaseDatabase.DefaultInstance.RootReference;
Debug.Log("Realtime DB initialized");
2024-01-27 14:54:23 +00:00
CreateNewRoom();
}
/// <summary>
/// Check all the rooms in the server and give back the number already taken
/// </summary>
private void WhichCodesAreAlreadyUsed(Action<List<int>> callback_OnCodesChecked)
{
Debug.Log("Checking other rooms to get which codes are already used", this);
2024-01-27 14:54:23 +00:00
List<int> alreadyUsedCodes = new List<int>();
2024-01-27 20:07:17 +00:00
try
2024-01-27 14:54:23 +00:00
{
2024-01-27 20:07:17 +00:00
realtimeDB.Child("rooms").GetValueAsync().ContinueWithOnMainThread(task =>
2024-01-27 14:54:23 +00:00
{
Debug.Log("looking into the online rooms", this);
2024-01-27 20:07:17 +00:00
if (task.IsFaulted)
2024-01-27 14:54:23 +00:00
{
2024-01-27 20:07:17 +00:00
Debug.LogException(task.Exception);
2024-01-27 14:54:23 +00:00
}
2024-01-27 20:07:17 +00:00
else if (task.IsCompleted)
{
DataSnapshot snapshot = task.Result;
if (snapshot.Value != null)
{
string JSON = snapshot.GetRawJsonValue();
Debug.Log($"found some rooms :\n{JSON}", this);
Dictionary<string, Room> onlineRooms = JsonConvert.DeserializeObject<Dictionary<string, Room>>(JSON);
foreach (Room r in onlineRooms.Values)
2024-01-27 20:07:17 +00:00
{
Debug.Log($"Code {r.code} is already used by another party", this);
alreadyUsedCodes.Add(int.Parse(r.code));
}
}
else
{
Debug.Log($"Your party is the first one!", this);
}
}
callback_OnCodesChecked?.Invoke(alreadyUsedCodes);
});
}
catch (Exception ex)
{
Debug.LogException(ex);
}
2024-01-27 14:54:23 +00:00
2024-01-27 14:08:12 +00:00
}
/// <summary>
/// Automatically called at start of game
/// </summary>
[ContextMenu("Create New Room")]
public void CreateNewRoom()
{
2024-01-27 14:54:23 +00:00
WhichCodesAreAlreadyUsed(codes =>
{
2024-01-27 20:07:17 +00:00
Debug.Log("coucou");
2024-01-27 14:54:23 +00:00
Room newRoom = new Room(GenerateRandomAvailableCode(codes).ToString("D4"));
2024-01-27 18:54:06 +00:00
myRoom = newRoom;
2024-01-27 17:50:04 +00:00
try
2024-01-27 14:54:23 +00:00
{
2024-01-27 20:07:17 +00:00
string JSON = JsonConvert.SerializeObject(newRoom);
2024-01-27 18:54:06 +00:00
2024-01-27 17:50:04 +00:00
realtimeDB.Child("rooms").Child(newRoom.code).SetRawJsonValueAsync(JSON).ContinueWithOnMainThread(task =>
{
2024-01-27 18:54:06 +00:00
//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");
2024-01-27 17:50:04 +00:00
});
2024-01-27 18:54:06 +00:00
}
2024-01-27 17:50:04 +00:00
catch (Exception e)
{
Debug.LogException(e);
}
2024-01-27 14:54:23 +00:00
});
}
/// <summary>
/// Generate a code between 0 and 1000 that is not in the list
/// </summary>
/// <param name="_impossibleCodes">the list of code you don"t want to get</param>
/// <returns></returns>
private int GenerateRandomAvailableCode(List<int> _impossibleCodes)
{
int random = UnityEngine.Random.Range(0, 1000);
while (_impossibleCodes.Contains(random))
{
Debug.Log($"{random} is already taken, choosing another room code", this);
random = UnityEngine.Random.Range(0, 1000);
}
return random;
}
2024-01-27 12:19:56 +00:00
public void PlayerSendProposition(Proposition _proposition)
{
}
/// <summary>
/// Called when the first player clicked "Start"
/// </summary>
public void HostStartGame()
{
2024-01-27 21:24:50 +00:00
currentState = GameState.MakeProposition;
waitingForPropositionsPage.SetActive(true);
waitingForPlayersPage.SetActive(false);
propositionCurrentTime = propositionTime;
2024-01-27 12:19:56 +00:00
}
/// <summary>
/// Start the proposition timer
/// </summary>
public void StartPropositionTimer()
{
}
/// <summary>
/// Automatically called when the proposition timer has finished
/// </summary>
public void PropositionTimerFinished()
{
}
/// <summary>
/// Start the voting timer
/// </summary>
public void StartVotingTimer()
2024-01-27 09:19:32 +00:00
{
}
2024-01-27 12:19:56 +00:00
/// <summary>
/// Automatically called when the voting timer has finished
/// </summary>
public void VotingTimerFinished()
2024-01-27 09:19:32 +00:00
{
}
2024-01-27 12:19:56 +00:00
/// <summary>
/// Automatically called when a proposition is updated (someone has voted or a picture has been proposed)
/// </summary>
public void OnPropositionUpdate()
{
}
/// <summary>
/// Will generate a question with a prompt and two owners
/// </summary>
public void GenerateQuestion()
{
}
/// <summary>
/// Generate all the player pairs
/// (players should not play against themself.
/// (players should not play twice with the same person)
/// </summary>
public void GenerateCouples()
{
}
/// <summary>
2024-01-27 18:54:06 +00:00
/// Automatically called when something change in your room
2024-01-27 12:19:56 +00:00
/// </summary>
2024-01-27 18:54:06 +00:00
private void OnRoomUpdate(object sender, ValueChangedEventArgs value)
2024-01-27 12:19:56 +00:00
{
2024-01-27 18:54:06 +00:00
if (value.DatabaseError != null)
2024-01-27 15:32:12 +00:00
{
2024-01-27 18:54:06 +00:00
Debug.LogError(value.DatabaseError.Message);
2024-01-27 15:32:12 +00:00
return;
}
2024-01-27 18:54:06 +00:00
string JSON = value.Snapshot.GetRawJsonValue();
Debug.Log(JSON);
try
{
myRoom = JsonConvert.DeserializeObject<Room>(JSON);
}
catch (Exception ex)
{
Debug.LogException(ex);
}
2024-01-27 12:19:56 +00:00
2024-01-27 18:54:06 +00:00
switch (currentState)
{
2024-01-27 21:24:50 +00:00
case GameState.WaitingForOtherPlayersToJoin:
2024-01-27 18:54:06 +00:00
UpdateConnectedPlayerList(myRoom.GetPlayerList());
break;
2024-01-27 21:24:50 +00:00
case GameState.MakeProposition:
HostStartGame();
break;
2024-01-27 18:54:06 +00:00
default:
break;
}
}
2024-01-27 12:19:56 +00:00
2024-01-27 18:54:06 +00:00
/// <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}");
2024-01-27 21:24:50 +00:00
waitingForPlayersLabels[i].text = _players[i].name;
2024-01-27 18:54:06 +00:00
}
2024-01-27 12:19:56 +00:00
}
[ContextMenu("Fake Player Connection")]
private void FakePlayerConnection()
{
2024-01-27 17:10:17 +00:00
Player temp = new Player("Momo");
2024-01-27 18:54:06 +00:00
temp.id = Guid.NewGuid().ToString();
2024-01-27 12:19:56 +00:00
temp.SetName("Momo");
}
}