304 lines
7.6 KiB
C#
304 lines
7.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Firebase;
|
|
using Firebase.Database;
|
|
using Firebase.Extensions;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using System;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using System.Linq;
|
|
|
|
public class RoomManager : MonoBehaviour
|
|
{
|
|
public List<TextMeshProUGUI> playerLabels = new List<TextMeshProUGUI>();
|
|
|
|
|
|
private RoomState currentState;
|
|
private Room myRoom = null;
|
|
private List<Player> players;
|
|
|
|
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;
|
|
|
|
DatabaseReference realtimeDB;
|
|
|
|
/// <summary>
|
|
/// TextMeshPro that show the value of the current rooom code
|
|
/// </summary>
|
|
public TextMeshProUGUI roomCodeLabel;
|
|
|
|
private void Awake()
|
|
{
|
|
FirebaseInitializer.Instance.onFirebaseReady += Initialize;
|
|
currentState = RoomState.None;
|
|
}
|
|
|
|
|
|
private void Start()
|
|
{
|
|
propositionCurrentTime = propositionTime;
|
|
votingCurrentTime = votingTime;
|
|
DisableAllPlayerLabels();
|
|
currentState = RoomState.WaitingForPlayers;
|
|
}
|
|
|
|
private void DisableAllPlayerLabels()
|
|
{
|
|
for (int i = 0; i < playerLabels.Count; i++)
|
|
{
|
|
playerLabels[i].text = $"Waiting for P{i + 1}";
|
|
}
|
|
}
|
|
|
|
private void OnApplicationQuit()
|
|
{
|
|
realtimeDB.Child("rooms").Child(myRoom.code).RemoveValueAsync();
|
|
Debug.Log($"delete room {myRoom.code}");
|
|
myRoom = null;
|
|
}
|
|
|
|
private void Initialize()
|
|
{
|
|
FirebaseInitializer.Instance.onFirebaseReady -= Initialize;
|
|
realtimeDB = FirebaseDatabase.DefaultInstance.RootReference;
|
|
Debug.Log("Realtime DB initialized");
|
|
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);
|
|
});
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Automatically called at start of game
|
|
/// </summary>
|
|
[ContextMenu("Create New Room")]
|
|
public void CreateNewRoom()
|
|
{
|
|
WhichCodesAreAlreadyUsed(codes =>
|
|
{
|
|
Room newRoom = new Room(GenerateRandomAvailableCode(codes).ToString("D4"));
|
|
myRoom = newRoom;
|
|
|
|
try
|
|
{
|
|
string JSON = JsonUtility.ToJson(newRoom);
|
|
|
|
realtimeDB.Child("rooms").Child(newRoom.code).SetRawJsonValueAsync(JSON).ContinueWithOnMainThread(task =>
|
|
{
|
|
|
|
//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");
|
|
});
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogException(e);
|
|
}
|
|
});
|
|
}
|
|
|
|
/// <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()
|
|
{
|
|
|
|
}
|
|
|
|
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()
|
|
{
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Automatically called when the voting timer has finished
|
|
/// </summary>
|
|
public void VotingTimerFinished()
|
|
{
|
|
|
|
}
|
|
|
|
/// <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>
|
|
/// Automatically called when something change in your room
|
|
/// </summary>
|
|
private void OnRoomUpdate(object sender, ValueChangedEventArgs value)
|
|
{
|
|
Debug.Log("coucou");
|
|
if (value.DatabaseError != null)
|
|
{
|
|
Debug.LogError(value.DatabaseError.Message);
|
|
return;
|
|
}
|
|
string JSON = value.Snapshot.GetRawJsonValue();
|
|
Debug.Log(JSON);
|
|
try
|
|
{
|
|
myRoom = JsonConvert.DeserializeObject<Room>(JSON);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.LogException(ex);
|
|
}
|
|
|
|
Debug.Log("caca");
|
|
|
|
switch (currentState)
|
|
{
|
|
case RoomState.WaitingForPlayers:
|
|
Debug.Log("prout");
|
|
UpdateConnectedPlayerList(myRoom.GetPlayerList());
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
/// <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}");
|
|
playerLabels[i].text = _players[i].name;
|
|
}
|
|
}
|
|
|
|
[ContextMenu("Fake Player Connection")]
|
|
private void FakePlayerConnection()
|
|
{
|
|
Player temp = new Player("Momo");
|
|
temp.id = Guid.NewGuid().ToString();
|
|
temp.SetName("Momo");
|
|
}
|
|
|
|
}
|
|
|
|
public enum RoomState
|
|
{
|
|
None,
|
|
WaitingForPlayers,
|
|
WaitingForPropositions,
|
|
ShowPropositions,
|
|
ShowVoters,
|
|
Score
|
|
}
|