diff --git a/Assets/Scripts/DatabaseClasses/Question.cs b/Assets/Scripts/DatabaseClasses/Question.cs index 12f3c6b..db6666e 100644 --- a/Assets/Scripts/DatabaseClasses/Question.cs +++ b/Assets/Scripts/DatabaseClasses/Question.cs @@ -1,11 +1,12 @@ using Newtonsoft.Json; using System; +using System.Collections.Generic; [Serializable] [JsonObject] public class Question { public string promptId; - public Proposition propositions; - public int creationDate; + public Dictionary propositions; + public double creationDate; } diff --git a/Assets/Scripts/GameManager.cs b/Assets/Scripts/GameManager.cs index c3edb8f..38a0d60 100644 --- a/Assets/Scripts/GameManager.cs +++ b/Assets/Scripts/GameManager.cs @@ -13,7 +13,6 @@ public class GameManager : MonoBehaviour private GameState currentState; private List players = new(); public Player currentPlayer = null; - public Question currentQuestion = null; [Header("Other component")] public float explanationTime = 4f; diff --git a/Assets/Scripts/PromptListLoader.cs b/Assets/Scripts/PromptListLoader.cs index 8f4d619..879bd51 100644 --- a/Assets/Scripts/PromptListLoader.cs +++ b/Assets/Scripts/PromptListLoader.cs @@ -25,6 +25,7 @@ private void Initialize() { FirebaseInitializer.Instance.onFirebaseReady -= Initialize; db = FirebaseFirestore.DefaultInstance; + LoadPromptsFromFirestore(); } /// diff --git a/Assets/Scripts/RoomManager.cs b/Assets/Scripts/RoomManager.cs index 5693d9a..4f4022d 100644 --- a/Assets/Scripts/RoomManager.cs +++ b/Assets/Scripts/RoomManager.cs @@ -5,7 +5,6 @@ using TMPro; using UnityEngine; using Newtonsoft.Json; -using Newtonsoft.Json.Linq; using System.Linq; public class RoomManager : MonoBehaviour @@ -15,6 +14,7 @@ public class RoomManager : MonoBehaviour private RoomState currentState; private Room myRoom = null; private List players; + public PromptList promptList; public float propositionTime = 60; public float votingTime = 20; @@ -231,22 +231,41 @@ public void OnPropositionUpdate() } - /// - /// Will generate a question with a prompt and two owners - /// - public void GenerateQuestion() + public void GeneratePrompts() { + System.Random rnd = new(); + List prompts = promptList.prompts.OrderBy(x => rnd.Next()).Take(players.Count() * 2).ToList(); + List fullPlayers = players.Concat(players).OrderBy(x => rnd.Next()).ToList(); + Dictionary questions = new(); - } + foreach (Prompt prompt in prompts) + { + Dictionary propositions = new(); + Player basePlayer = fullPlayers[0]; + propositions.Add(Guid.NewGuid().ToString(), new Proposition() { owner = basePlayer }); - /// - /// Generate all the player pairs - /// (players should not play against themself. - /// (players should not play twice with the same person) - /// - public void GenerateCouples() - { + for (int i = 1; i < fullPlayers.Count(); i++) + { + Player secondPlayer = fullPlayers[i]; + if (basePlayer.id != secondPlayer.id) + { + propositions.Add(Guid.NewGuid().ToString(), new Proposition() { owner = secondPlayer }); + fullPlayers.RemoveAt(0); + break; + } + } + + questions.Add(Guid.NewGuid().ToString(), new Question() + { + promptId = prompt.id, + propositions = propositions, + creationDate = DateTime.Now.ToOADate(), + }); + } + + string JSON = JsonConvert.SerializeObject(questions); + realtimeDB.Child("rooms").Child(myRoom.code).Child("questions").SetRawJsonValueAsync(JSON); } ///