Added some playerstates

This commit is contained in:
martin 2023-07-01 18:54:59 +02:00
parent 49b8d05f97
commit eaf17ef484
4 changed files with 31 additions and 4 deletions

View File

@ -5,6 +5,7 @@ using pacMan.Game;
using pacMan.Game.Interfaces;
using pacMan.Game.Items;
using pacMan.Interfaces;
using pacMan.Services;
using pacMan.Utils;
namespace pacMan.Controllers;
@ -14,6 +15,8 @@ namespace pacMan.Controllers;
public class GameController : GenericController
{
private readonly IDiceCup _diceCup;
private GameGroup _group = new();
private IPlayer? _player;
public GameController(ILogger<GameController> logger, IWebSocketService wsService) : base(logger, wsService)
{
@ -48,13 +51,17 @@ public class GameController : GenericController
message.Data = rolls;
break;
case GameAction.PlayerInfo:
Player player = JsonSerializer.Deserialize<Player>(message.Data);
var group = WsService.AddPlayer(player); // TODO missing some data?
_player = JsonSerializer.Deserialize<Player>(message.Data);
_group = WsService.AddPlayer(_player); // TODO missing some data?
message.Data = group.Players;
message.Data = _group.Players;
break;
case GameAction.Ready:
// TODO select starter player
if (_player != null) _group.SetReady(_player);
if (_group.AllReady())
{
// TODO select starter player and send response
}
break;
default:
Logger.Log(LogLevel.Information, "Forwarding message to all clients");

View File

@ -8,4 +8,12 @@ public interface IPlayer
Character PacMan { get; init; }
string Colour { get; init; }
Box Box { get; init; }
State State { get; set; }
}
public enum State
{
WaitingForPlayers,
Ready,
InGame
}

View File

@ -8,4 +8,5 @@ public class Player : IPlayer
public required Character PacMan { get; init; }
public required string Colour { get; init; }
public required Box Box { get; init; }
public State State { get; set; } = State.WaitingForPlayers;
}

View File

@ -13,6 +13,7 @@ public class GameGroup
if (Players.Count >= Rules.MaxPlayers) return false;
if (Players.Exists(p => p.Name == player.Name)) return false;
player.State = State.WaitingForPlayers;
Players.Add(player);
return true;
}
@ -21,4 +22,14 @@ public class GameGroup
{
Connections?.Invoke(segment);
}
public bool AllReady()
{
return Players.All(p => p.State == State.Ready);
}
public void SetReady(IPlayer player)
{
player.State = State.Ready;
}
}