Snaparazzi/Assets/Scripts/DatabaseClasses/Room.cs
2024-01-28 15:42:32 +01:00

95 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
[Serializable]
[JsonObject]
public class Room
{
public string code;
public Dictionary<int, Question> questions;
public Dictionary<string, Player> players;
public string currentQuestionId;
public double creationDate;
public int currentState;
public Room(string _code)
{
code = _code;
creationDate = DateTime.Now.ToOADate();
players = new Dictionary<string, Player>();
questions = new Dictionary<int, Question>();
currentQuestionId = "";
currentState = 1; //default by PC
}
/// <summary>
/// Return the only player with a specific ID
/// </summary>
/// <returns></returns>
public Player GetPlayerById(string id)
{
return players[id];
}
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();
}
public List<Question> GetQuestionsByPlayer(Player player)
{
List<Question> playerQuestions = new();
foreach (Question question in new List<Question>(questions.Values))
{
foreach (Proposition proposition in new List<Proposition>(question.propositions.Values))
{
if (proposition.owner.id == player.id)
{
playerQuestions.Add(question);
break;
}
}
}
return playerQuestions;
}
public void SetPlayersAreReady(int _state)
{
currentState = _state;
}
public List<Proposition> GetPropositionsByPlayer(Player player)
{
List<Proposition> playerPropositions = new();
foreach (Question question in new List<Question>(questions.Values))
{
foreach (Proposition proposition in new List<Proposition>(question.propositions.Values))
{
if (proposition.owner.id == player.id)
{
playerPropositions.Add(proposition);
break;
}
}
}
return playerPropositions;
}
}