95 lines
2.4 KiB
C#
Raw Normal View History

2024-01-27 19:58:02 +01:00
using System;
2024-01-27 10:19:32 +01:00
using System.Collections.Generic;
using System.Linq;
2024-01-27 19:54:06 +01:00
using Newtonsoft.Json;
2024-01-27 10:19:32 +01:00
2024-01-27 19:58:02 +01:00
[Serializable]
2024-01-27 20:05:24 +01:00
[JsonObject]
2024-01-27 10:19:32 +01:00
public class Room
{
public string code;
2024-01-28 15:42:32 +01:00
public Dictionary<int, Question> questions;
2024-01-27 19:54:06 +01:00
public Dictionary<string, Player> players;
2024-01-28 12:14:09 +01:00
public string currentQuestionId;
2024-01-27 15:08:12 +01:00
public double creationDate;
2024-01-27 22:26:23 +01:00
public int currentState;
2024-01-27 15:08:12 +01:00
public Room(string _code)
{
2024-01-27 19:58:02 +01:00
code = _code;
creationDate = DateTime.Now.ToOADate();
players = new Dictionary<string, Player>();
2024-01-28 15:42:32 +01:00
questions = new Dictionary<int, Question>();
2024-01-28 12:14:09 +01:00
currentQuestionId = "";
2024-01-28 00:06:23 +01:00
currentState = 1; //default by PC
2024-01-27 15:08:12 +01:00
}
2024-01-27 19:54:06 +01:00
2024-01-28 13:18:40 +01:00
/// <summary>
/// Return the only player with a specific ID
/// </summary>
/// <returns></returns>
public Player GetPlayerById(string id)
{
return players[id];
}
2024-01-27 19:54:06 +01: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 02:40:12 +01:00
public List<Question> GetQuestionsByPlayer(Player player)
{
2024-01-28 15:42:32 +01:00
List<Question> playerQuestions = new();
2024-01-28 02:40:12 +01:00
2024-01-28 15:42:32 +01:00
foreach (Question question in new List<Question>(questions.Values))
2024-01-28 02:40:12 +01:00
{
foreach (Proposition proposition in new List<Proposition>(question.propositions.Values))
{
if (proposition.owner.id == player.id)
{
2024-01-28 15:42:32 +01:00
playerQuestions.Add(question);
2024-01-28 02:40:12 +01:00
break;
}
}
}
2024-01-28 15:42:32 +01:00
return playerQuestions;
2024-01-28 02:40:12 +01:00
}
2024-01-28 02:26:57 +01:00
2024-01-28 02:40:12 +01:00
public void SetPlayersAreReady(int _state)
2024-01-27 22:26:23 +01:00
{
currentState = _state;
}
public List<Proposition> GetPropositionsByPlayer(Player player)
{
2024-01-28 15:42:32 +01:00
List<Proposition> playerPropositions = new();
2024-01-28 15:42:32 +01:00
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)
{
2024-01-28 15:42:32 +01:00
playerPropositions.Add(proposition);
break;
}
}
}
2024-01-28 15:42:32 +01:00
return playerPropositions;
}
2024-01-27 10:19:32 +01:00
}
2024-01-27 19:54:06 +01:00