Snaparazzi/Assets/Scripts/DatabaseClasses/Room.cs

190 lines
5.1 KiB
C#
Raw Normal View History

2024-01-27 18:58:02 +00:00
using System;
2024-01-27 09:19:32 +00:00
using System.Collections.Generic;
using System.Linq;
2024-01-27 18:54:06 +00:00
using Newtonsoft.Json;
2024-01-28 15:13:25 +00:00
using Newtonsoft.Json.Linq;
using UnityEngine;
2024-01-27 09:19:32 +00:00
2024-01-27 18:58:02 +00:00
[Serializable]
2024-01-27 19:05:24 +00:00
[JsonObject]
2024-01-27 09:19:32 +00:00
public class Room
{
public string code;
2024-01-28 15:13:25 +00:00
[JsonConverter(typeof(ArrayToDictionaryConverter<Question>))]
2024-01-28 14:42:32 +00:00
public Dictionary<int, Question> questions;
2024-01-27 18:54:06 +00:00
public Dictionary<string, Player> players;
2024-01-28 16:34:11 +00:00
public int currentQuestionId;
2024-01-27 14:08:12 +00:00
public double creationDate;
2024-01-27 21:26:23 +00:00
public int currentState;
public string promptsLanguage;
2024-01-27 14:08:12 +00:00
public Room(string _code)
{
2024-01-27 18:58:02 +00:00
code = _code;
creationDate = DateTime.Now.ToOADate();
players = new Dictionary<string, Player>();
2024-01-28 14:42:32 +00:00
questions = new Dictionary<int, Question>();
2024-01-28 16:34:11 +00:00
currentQuestionId = 0;
2024-01-27 23:06:23 +00:00
currentState = 1; //default by PC
promptsLanguage = "en";
2024-01-27 14:08:12 +00:00
}
2024-01-27 18:54:06 +00:00
public Room Copy()
{
Room newRoom = new Room(this.code)
{
questions = new Dictionary<int, Question>(this.questions),
players = new Dictionary<string, Player>(this.players),
currentQuestionId = this.currentQuestionId,
creationDate = this.creationDate,
currentState = this.currentState
};
return newRoom;
}
2024-01-28 12:18:40 +00:00
/// <summary>
/// Return the only player with a specific ID
/// </summary>
/// <returns></returns>
public Player GetPlayerById(string id)
{
return players[id];
}
/// <summary>
/// Get the order of arrival of a player (the first player is 0, the last to connect is player.count-1)
/// </summary>
/// <param name="p">The player you want to know the position of</param>
/// <returns>the position by arrival order</returns>
public int GetPlayerOrder(Player p)
{
return GetOrderedPlayerList().FindIndex(x => x.id == p.id);
}
2024-01-27 18:54:06 +00:00
public List<Player> GetPlayerList()
{
return new List<Player>(players.Values);
}
/// <summary>
/// return the list of player ordered by who joined first
/// </summary>
/// <returns></returns>
public List<Player> GetOrderedPlayerList()
{
return players.Values.OrderBy(x => x.creationDate).ToList();
}
2024-01-28 01:40:12 +00:00
public List<Question> GetQuestionsByPlayer(Player player)
{
2024-01-28 14:42:32 +00:00
List<Question> playerQuestions = new();
2024-01-28 01:40:12 +00:00
foreach (Question question in questions.Values.ToList())
2024-01-28 01:40:12 +00:00
{
foreach (Proposition proposition in question.propositions.Values.ToList())
2024-01-28 01:40:12 +00:00
{
if (proposition.owner.id == player.id)
{
2024-01-28 14:42:32 +00:00
playerQuestions.Add(question);
2024-01-28 01:40:12 +00:00
break;
}
}
}
2024-01-28 14:42:32 +00:00
return playerQuestions;
2024-01-28 01:40:12 +00:00
}
2024-01-28 01:26:57 +00:00
2024-01-28 01:40:12 +00:00
public void SetPlayersAreReady(int _state)
2024-01-27 21:26:23 +00:00
{
currentState = _state;
}
/// <summary>
/// Return all the propositions linked to a specific player.
/// </summary>
/// <param name="player"></param>
/// <returns></returns>
public List<Proposition> GetPropositionsForPlayer(Player player)
{
2024-01-28 14:42:32 +00:00
List<Proposition> playerPropositions = new();
foreach (Question question in questions.Values.ToList())
{
foreach (Proposition proposition in question.propositions.Values.ToList())
{
if (proposition.owner.id == player.id)
{
2024-01-28 14:42:32 +00:00
playerPropositions.Add(proposition);
break;
}
}
}
2024-01-28 14:42:32 +00:00
return playerPropositions;
}
/// <summary>
/// Return the number of voters for a specific question
/// </summary>
/// <param name="_question"></param>
/// <returns></returns>
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;
}
2024-01-27 09:19:32 +00:00
}
2024-01-27 18:54:06 +00:00
2024-01-28 15:13:25 +00:00
public class ArrayToDictionaryConverter<T> : JsonConverter<Dictionary<int, T>>
{
public override Dictionary<int, T> ReadJson(JsonReader reader, Type objectType, Dictionary<int, T> existingValue, bool hasExistingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.StartArray)
{
var jsonArray = JArray.Load(reader);
var dictionary = new Dictionary<int, T>();
for (int i = 0; i < jsonArray.Count; i++)
{
var item = jsonArray[i].ToObject<T>(serializer);
dictionary.Add(i, item);
}
return dictionary;
}
return null;
}
public override void WriteJson(JsonWriter writer, Dictionary<int, T> 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);
}
}
}