using System; using System.Collections.Generic; using System.Linq; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using UnityEngine; [Serializable] [JsonObject] public class Room { public string code; [JsonConverter(typeof(ArrayToDictionaryConverter))] public Dictionary questions; public Dictionary players; public int currentQuestionId; public double creationDate; public int currentState; public Room(string _code) { code = _code; creationDate = DateTime.Now.ToOADate(); players = new Dictionary(); questions = new Dictionary(); currentQuestionId = 0; currentState = 1; //default by PC } /// /// Return the only player with a specific ID /// /// public Player GetPlayerById(string id) { return players[id]; } /// /// Get the order of arrival of a player (the first player is 0, the last to connect is player.count-1) /// /// The player you want to know the position of /// the position by arrival order public int GetPlayerOrder(Player p) { return GetOrderedPlayerList().FindIndex(x => x.id == p.id); } public List GetPlayerList() { return new List(players.Values); } /// /// return the list of player ordered by who joined first /// /// public List GetOrderedPlayerList() { return players.Values.OrderBy(x => x.creationDate).ToList(); } public List GetQuestionsByPlayer(Player player) { List playerQuestions = new(); foreach (Question question in questions.Values.ToList()) { foreach (Proposition proposition in question.propositions.Values.ToList()) { if (proposition.owner.id == player.id) { playerQuestions.Add(question); break; } } } return playerQuestions; } public void SetPlayersAreReady(int _state) { currentState = _state; } /// /// Return all the propositions linked to a specific player. /// /// /// public List GetPropositionsForPlayer(Player player) { List playerPropositions = new(); foreach (Question question in questions.Values.ToList()) { foreach (Proposition proposition in question.propositions.Values.ToList()) { if (proposition.owner.id == player.id) { playerPropositions.Add(proposition); break; } } } return playerPropositions; } /// /// Return the number of voters for a specific question /// /// /// public int NumbersOfVotersForQuestion(Question _question) { int numberOfVoters = 0; foreach(Proposition proposition in _question.propositions.Values.ToList()) { if (proposition.voters == null) continue; numberOfVoters += proposition.voters.Count; } return numberOfVoters; } } public class ArrayToDictionaryConverter : JsonConverter> { public override Dictionary ReadJson(JsonReader reader, Type objectType, Dictionary existingValue, bool hasExistingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.StartArray) { var jsonArray = JArray.Load(reader); var dictionary = new Dictionary(); for (int i = 0; i < jsonArray.Count; i++) { var item = jsonArray[i].ToObject(serializer); dictionary.Add(i, item); } return dictionary; } return null; } public override void WriteJson(JsonWriter writer, Dictionary value, JsonSerializer serializer) { if (value != null) { var jsonArray = new JArray(); foreach (var kvp in value) { var itemJson = JToken.FromObject(kvp.Value, serializer); jsonArray.Add(itemJson); } jsonArray.WriteTo(writer); } } }