Snaparazzi/Assets/Scripts/RoomManager.cs

245 lines
6.3 KiB
C#
Raw Normal View History

2024-01-27 09:19:32 +00:00
using System.Collections;
using System.Collections.Generic;
2024-01-27 14:08:12 +00:00
using Firebase;
using Firebase.Database;
2024-01-27 14:54:23 +00:00
using Firebase.Extensions;
using TMPro;
2024-01-27 09:19:32 +00:00
using UnityEngine;
2024-01-27 14:54:23 +00:00
using System;
2024-01-27 09:19:32 +00:00
public class RoomManager : MonoBehaviour
{
2024-01-27 12:19:56 +00:00
private RoomState currentState;
2024-01-27 14:08:12 +00:00
private Room currentRoom = null;
2024-01-27 12:19:56 +00:00
private List<Player> players;
2024-01-27 09:19:32 +00:00
2024-01-27 12:19:56 +00:00
public float propositionTime = 60;
public float votingTime = 20;
private float propositionCurrentTime = 0;
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;
2024-01-27 14:54:23 +00:00
/// <summary>
/// TextMeshPro that show the value of the current rooom code
/// </summary>
public TextMeshProUGUI roomCodeLabel;
2024-01-27 14:08:12 +00:00
private void Awake()
{
FirebaseInitializer.Instance.onFirebaseReady += Initialize;
}
2024-01-27 12:19:56 +00:00
private void Start()
{
propositionCurrentTime = propositionTime;
votingCurrentTime = votingTime;
2024-01-27 14:08:12 +00:00
}
private void OnApplicationQuit()
{
realtimeDB.Child("rooms").Child(currentRoom.code).RemoveValueAsync();
Debug.Log($"delete room {currentRoom.code}");
currentRoom = null;
}
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)
{
List<int> alreadyUsedCodes = new List<int>();
realtimeDB.Child("rooms").GetValueAsync().ContinueWithOnMainThread(task =>
{
if (task.IsFaulted)
{
Debug.LogException(task.Exception);
}
else if (task.IsCompleted)
{
DataSnapshot snapshot = task.Result;
List<Room> onlineRooms = JsonUtility.FromJson<List<Room>>(snapshot.GetRawJsonValue());
foreach (Room r in onlineRooms)
{
alreadyUsedCodes.Add(int.Parse(r.code));
}
}
callback_OnCodesChecked?.Invoke(alreadyUsedCodes);
});
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 =>
{
Room newRoom = new Room(GenerateRandomAvailableCode(codes).ToString("D4"));
currentRoom = newRoom;
string JSON = JsonUtility.ToJson(newRoom);
realtimeDB.Child("rooms").Child(newRoom.code).SetRawJsonValueAsync(JSON).ContinueWithOnMainThread(task =>
{
Debug.Log($"room {currentRoom.code} has been created on the server");
2024-01-27 15:32:12 +00:00
realtimeDB.Child("rooms").Child(newRoom.code).Child("players").ChildAdded += PlayerConnect;
2024-01-27 14:54:23 +00:00
//TODO MARINE : uncomment and reference the correct game object
//roomCodeLabel.text = currentRoom.code;
});
});
}
/// <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;
}
private void DisplayRoomCode()
{
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()
{
}
/// <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>
/// is automatically called when a player connect to the room
/// </summary>
/// <param name="_player"></param>
2024-01-27 15:32:12 +00:00
public void PlayerConnect(object sender, ChildChangedEventArgs args)
2024-01-27 12:19:56 +00:00
{
2024-01-27 15:32:12 +00:00
if (args.DatabaseError != null)
{
Debug.LogError(args.DatabaseError.Message);
return;
}
2024-01-27 12:19:56 +00:00
2024-01-27 15:32:12 +00:00
string JSON = args.Snapshot.GetRawJsonValue();
Player joinedPlayer = JsonUtility.FromJson<Player>(JSON);
Debug.Log($"{joinedPlayer.name} has joined the room");
//TODO Marine : do somtethjing with the newly joinde player
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 12:19:56 +00:00
temp.id = System.Guid.NewGuid().ToString();
temp.SetName("Momo");
}
}
public enum RoomState
{
WaitingForPlayer,
WaitingForPropositions,
ShowPropositions,
ShowVoters,
Score
2024-01-27 09:19:32 +00:00
}