using System.Collections; using System.Collections.Generic; using Firebase; using Firebase.Database; using Firebase.Extensions; using TMPro; using UnityEngine; using System; public class RoomManager : MonoBehaviour { private RoomState currentState; private Room currentRoom = null; private List players; public float propositionTime = 60; public float votingTime = 20; private float propositionCurrentTime = 0; private float votingCurrentTime = 0; /// /// Contain the infos about the current displayed question (during votes) /// private Question currentQuestion; private List questions; /// /// When this is equal to questions.Count, go to score page /// private int numberOfQuestionVoted = 0; DatabaseReference realtimeDB; /// /// TextMeshPro that show the value of the current rooom code /// public TextMeshProUGUI roomCodeLabel; private void Awake() { FirebaseInitializer.Instance.onFirebaseReady += Initialize; } private void Start() { propositionCurrentTime = propositionTime; votingCurrentTime = votingTime; } 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"); CreateNewRoom(); } /// /// Check all the rooms in the server and give back the number already taken /// private void WhichCodesAreAlreadyUsed(Action> callback_OnCodesChecked) { List alreadyUsedCodes = new List(); realtimeDB.Child("rooms").GetValueAsync().ContinueWithOnMainThread(task => { if (task.IsFaulted) { Debug.LogException(task.Exception); } else if (task.IsCompleted) { DataSnapshot snapshot = task.Result; List onlineRooms = JsonUtility.FromJson>(snapshot.GetRawJsonValue()); foreach (Room r in onlineRooms) { alreadyUsedCodes.Add(int.Parse(r.code)); } } callback_OnCodesChecked?.Invoke(alreadyUsedCodes); }); } /// /// Automatically called at start of game /// [ContextMenu("Create New Room")] public void CreateNewRoom() { 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"); //TODO MARINE : uncomment and reference the correct game object //roomCodeLabel.text = currentRoom.code; }); }); } /// /// Generate a code between 0 and 1000 that is not in the list /// /// the list of code you don"t want to get /// private int GenerateRandomAvailableCode(List _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) { } /// /// Called when the first player clicked "Start" /// public void HostStartGame() { } /// /// Start the proposition timer /// public void StartPropositionTimer() { } /// /// Automatically called when the proposition timer has finished /// public void PropositionTimerFinished() { } /// /// Start the voting timer /// public void StartVotingTimer() { } /// /// Automatically called when the voting timer has finished /// public void VotingTimerFinished() { } /// /// Automatically called when a proposition is updated (someone has voted or a picture has been proposed) /// public void OnPropositionUpdate() { } /// /// Will generate a question with a prompt and two owners /// public void GenerateQuestion() { } /// /// Generate all the player pairs /// (players should not play against themself. /// (players should not play twice with the same person) /// public void GenerateCouples() { } /// /// is automatically called when a player connect to the room /// /// public void PlayerConnect(Player _player) { } [ContextMenu("Fake Player Connection")] private void FakePlayerConnection() { Player temp = new Player(); temp.id = System.Guid.NewGuid().ToString(); temp.SetName("Momo"); } } public enum RoomState { WaitingForPlayer, WaitingForPropositions, ShowPropositions, ShowVoters, Score }