2023-07-18 13:50:46 +02:00
|
|
|
using System.Text.Json.Serialization;
|
2023-07-11 14:23:41 +02:00
|
|
|
using pacMan.Exceptions;
|
2023-07-18 17:09:27 +02:00
|
|
|
using pacMan.GameStuff;
|
|
|
|
using pacMan.GameStuff.Items;
|
2023-06-24 19:43:03 +02:00
|
|
|
|
|
|
|
namespace pacMan.Services;
|
|
|
|
|
2023-07-18 17:09:27 +02:00
|
|
|
public class Game // TODO handle disconnects and reconnects
|
2023-06-24 19:43:03 +02:00
|
|
|
{
|
2023-07-02 16:44:30 +02:00
|
|
|
private readonly Random _random = new();
|
2023-07-16 12:10:53 +02:00
|
|
|
private int _currentPlayerIndex;
|
2023-07-21 13:54:44 +02:00
|
|
|
private List<Player> _players = new();
|
2023-07-14 19:14:19 +02:00
|
|
|
|
2023-07-18 17:09:27 +02:00
|
|
|
public Game(Queue<DirectionalPosition> spawns) => Spawns = spawns;
|
2023-07-14 19:14:19 +02:00
|
|
|
|
2023-07-18 13:50:46 +02:00
|
|
|
[JsonInclude] public Guid Id { get; } = Guid.NewGuid();
|
2023-07-02 16:44:30 +02:00
|
|
|
|
2023-07-21 13:54:44 +02:00
|
|
|
[JsonIgnore]
|
|
|
|
public List<Player> Players
|
|
|
|
{
|
|
|
|
get => _players;
|
|
|
|
set =>
|
|
|
|
_players = _players.Select((player, index) =>
|
|
|
|
{
|
|
|
|
player.State = value[index].State;
|
|
|
|
player.PacMan = value[index].PacMan;
|
|
|
|
player.Box = value[index].Box;
|
|
|
|
return player;
|
|
|
|
}).ToList();
|
|
|
|
}
|
2023-07-12 21:33:52 +02:00
|
|
|
|
2023-07-19 17:09:47 +02:00
|
|
|
[JsonIgnore] public List<Character> Ghosts { get; set; } = new();
|
|
|
|
|
2023-07-18 13:50:46 +02:00
|
|
|
[JsonIgnore] private Queue<DirectionalPosition> Spawns { get; }
|
2023-07-16 12:10:53 +02:00
|
|
|
|
2023-07-19 16:19:51 +02:00
|
|
|
[JsonIgnore] public DiceCup DiceCup { get; } = new();
|
|
|
|
|
2023-07-18 13:50:46 +02:00
|
|
|
[JsonInclude] public int Count => Players.Count;
|
2023-07-12 21:33:52 +02:00
|
|
|
|
2023-07-18 13:50:46 +02:00
|
|
|
[JsonInclude]
|
|
|
|
public bool IsGameStarted => Count > 0 && Players.All(player => player.State is State.InGame or State.Disconnected);
|
2023-07-14 19:14:19 +02:00
|
|
|
|
2023-07-20 18:06:30 +02:00
|
|
|
public Player NextPlayer()
|
2023-07-16 13:23:12 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
_currentPlayerIndex = (_currentPlayerIndex + 1) % Count;
|
|
|
|
}
|
|
|
|
catch (DivideByZeroException)
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("There are no players in the game group.");
|
|
|
|
}
|
2023-07-18 13:50:46 +02:00
|
|
|
|
2023-07-16 13:23:12 +02:00
|
|
|
return Players[_currentPlayerIndex];
|
|
|
|
}
|
|
|
|
|
2023-07-16 12:10:53 +02:00
|
|
|
public void Shuffle() => Players.Sort((_, _) => _random.Next(-1, 2));
|
|
|
|
|
2023-06-24 19:43:03 +02:00
|
|
|
public event Func<ArraySegment<byte>, Task>? Connections;
|
|
|
|
|
2023-07-20 18:06:30 +02:00
|
|
|
public bool AddPlayer(Player player)
|
2023-06-24 19:43:03 +02:00
|
|
|
{
|
2023-07-16 12:10:53 +02:00
|
|
|
if (Players.Count >= Rules.MaxPlayers || IsGameStarted) return false;
|
2023-07-18 18:19:54 +02:00
|
|
|
/* TODO remove above and uncomment below
|
|
|
|
if (Players.Count >= Rules.MaxPlayers)
|
|
|
|
throw new GameNotPlayableException("Game is full");
|
|
|
|
if (IsGameStarted)
|
|
|
|
throw new GameNotPlayableException("Game has already started");
|
|
|
|
*/
|
2023-06-24 19:43:03 +02:00
|
|
|
|
2023-07-01 18:54:59 +02:00
|
|
|
player.State = State.WaitingForPlayers;
|
2023-07-19 23:25:54 +02:00
|
|
|
if (Players.Exists(p => p.Username == player.Username)) return true; // TODO change to false
|
2023-06-24 19:43:03 +02:00
|
|
|
Players.Add(player);
|
2023-07-14 19:14:19 +02:00
|
|
|
if (player.PacMan.SpawnPosition is null) SetSpawn(player);
|
2023-06-24 19:43:03 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-07-20 18:06:30 +02:00
|
|
|
public Player? RemovePlayer(string username)
|
2023-07-20 15:00:03 +02:00
|
|
|
{
|
|
|
|
var index = Players.FindIndex(p => p.Username == username);
|
|
|
|
if (index == -1) return null;
|
|
|
|
var removedPlayer = Players[index];
|
|
|
|
Players.RemoveAt(index);
|
|
|
|
return removedPlayer;
|
|
|
|
}
|
|
|
|
|
2023-07-20 18:06:30 +02:00
|
|
|
private void SetSpawn(Player player)
|
2023-07-14 19:14:19 +02:00
|
|
|
{
|
|
|
|
if (player.PacMan.SpawnPosition is not null) return;
|
|
|
|
var spawn = Spawns.Dequeue();
|
|
|
|
player.PacMan.SpawnPosition = spawn;
|
|
|
|
player.PacMan.Position = spawn;
|
|
|
|
}
|
|
|
|
|
2023-07-02 16:44:30 +02:00
|
|
|
public void SendToAll(ArraySegment<byte> segment) => Connections?.Invoke(segment);
|
2023-07-01 18:54:59 +02:00
|
|
|
|
2023-07-20 22:56:35 +02:00
|
|
|
public IEnumerable<Player> SetReady(string username)
|
2023-07-01 18:54:59 +02:00
|
|
|
{
|
2023-07-20 22:56:35 +02:00
|
|
|
var player = Players.FirstOrDefault(p => p.Username == username);
|
|
|
|
if (player is null)
|
2023-07-11 14:23:41 +02:00
|
|
|
throw new PlayerNotFoundException("The player was not found in the game group.");
|
2023-07-01 18:54:59 +02:00
|
|
|
player.State = State.Ready;
|
2023-07-02 16:44:30 +02:00
|
|
|
return Players;
|
2023-07-01 18:54:59 +02:00
|
|
|
}
|
2023-07-04 22:42:44 +02:00
|
|
|
|
2023-07-11 14:31:50 +02:00
|
|
|
public bool SetAllInGame()
|
2023-07-04 22:42:44 +02:00
|
|
|
{
|
2023-07-11 14:31:50 +02:00
|
|
|
if (Players.Any(player => player.State != State.Ready)) return false;
|
|
|
|
|
2023-07-04 22:42:44 +02:00
|
|
|
foreach (var player in Players) player.State = State.InGame;
|
2023-07-11 14:31:50 +02:00
|
|
|
return true;
|
2023-07-04 22:42:44 +02:00
|
|
|
}
|
2023-07-02 16:44:30 +02:00
|
|
|
}
|