fixed state not updating when joining from lobby

This commit is contained in:
Martin Berg Alstad 2023-07-20 22:56:35 +02:00
parent f0a701a2fd
commit 7af502f570
6 changed files with 15 additions and 12 deletions

View File

@ -110,8 +110,8 @@ public class GameTests
_game.AddPlayer(_redPlayer); _game.AddPlayer(_redPlayer);
_game.AddPlayer(_bluePlayer); _game.AddPlayer(_bluePlayer);
_game.SetReady(_redPlayer); _game.SetReady(_redPlayer.Username);
_game.SetReady(_bluePlayer); _game.SetReady(_bluePlayer.Username);
_game.SetAllInGame(); _game.SetAllInGame();
Assert.That(_game.AddPlayer(_greenPlayer), Is.False); Assert.That(_game.AddPlayer(_greenPlayer), Is.False);
@ -154,7 +154,7 @@ public class GameTests
_game.AddPlayer(_redPlayer); _game.AddPlayer(_redPlayer);
_game.AddPlayer(_bluePlayer); _game.AddPlayer(_bluePlayer);
var players = _game.SetReady(_redPlayer).ToList(); var players = _game.SetReady(_redPlayer.Username).ToList();
Assert.Multiple(() => Assert.Multiple(() =>
{ {
@ -171,7 +171,7 @@ public class GameTests
Assert.That(_redPlayer.State, Is.Not.EqualTo(State.Ready)); Assert.That(_redPlayer.State, Is.Not.EqualTo(State.Ready));
_game.SetReady(_redPlayer); _game.SetReady(_redPlayer.Username);
Assert.That(_redPlayer.State, Is.EqualTo(State.Ready)); Assert.That(_redPlayer.State, Is.EqualTo(State.Ready));
} }
@ -179,7 +179,7 @@ public class GameTests
[Test] [Test]
public void SetReady_WhenPlayerIsNotInPlayers() public void SetReady_WhenPlayerIsNotInPlayers()
{ {
Assert.Throws<PlayerNotFoundException>(() => _game.SetReady(_redPlayer)); Assert.Throws<PlayerNotFoundException>(() => _game.SetReady(_redPlayer.Username));
} }
#endregion #endregion

View File

@ -12,6 +12,7 @@ export enum GameAction {
playerInfo, playerInfo,
ready, ready,
nextPlayer, nextPlayer,
disconnect,
} }
const store = getDefaultStore(); const store = getDefaultStore();

View File

@ -82,7 +82,7 @@ public class GameController : GenericController
protected override Task Echo() protected override Task Echo()
{ {
_gameService.Connections += WsServiceOnFire; _gameService.Connections += WsServiceOnFire; // TODO move to ActionService
// _actionService.Game.Connections += WsServiceOnFire; // _actionService.Game.Connections += WsServiceOnFire;
return base.Echo(); return base.Echo();
} }

View File

@ -9,7 +9,8 @@ public enum GameAction
MoveCharacter, MoveCharacter,
PlayerInfo, PlayerInfo,
Ready, Ready,
NextPlayer NextPlayer,
Disconnect
} }
public class ActionMessage<T> public class ActionMessage<T>

View File

@ -38,10 +38,10 @@ public class ActionService : IActionService
message.Data = message.Action switch message.Data = message.Action switch
{ {
GameAction.RollDice => RollDice(), GameAction.RollDice => RollDice(),
GameAction.MoveCharacter => HandleMoveCharacter(message.Data),
GameAction.PlayerInfo => SetPlayerInfo(message.Data), GameAction.PlayerInfo => SetPlayerInfo(message.Data),
GameAction.Ready => Ready(), GameAction.Ready => Ready(),
GameAction.NextPlayer => FindNextPlayer(), GameAction.NextPlayer => FindNextPlayer(),
GameAction.MoveCharacter => HandleMoveCharacter(message.Data),
_ => message.Data _ => message.Data
}; };
} }
@ -55,7 +55,7 @@ public class ActionService : IActionService
return rolls; return rolls;
} }
public List<Player> SetPlayerInfo(JsonElement? jsonElement) public List<Player> SetPlayerInfo(JsonElement? jsonElement) // TODO split up into two actions
{ {
var data = jsonElement?.Deserialize<PlayerInfoData>() ?? throw new NullReferenceException("Data is null"); var data = jsonElement?.Deserialize<PlayerInfoData>() ?? throw new NullReferenceException("Data is null");
Player = data.Player; Player = data.Player;
@ -83,7 +83,7 @@ public class ActionService : IActionService
object data; object data;
if (Player != null && Game != null) if (Player != null && Game != null)
{ {
var players = Game.SetReady(Player).ToArray(); var players = Game.SetReady(Player.Username).ToArray();
// TODO roll to start // TODO roll to start
Game.Shuffle(); Game.Shuffle();
var allReady = players.All(p => p.State == State.Ready); var allReady = players.All(p => p.State == State.Ready);

View File

@ -81,9 +81,10 @@ public class Game // TODO handle disconnects and reconnects
public void SendToAll(ArraySegment<byte> segment) => Connections?.Invoke(segment); public void SendToAll(ArraySegment<byte> segment) => Connections?.Invoke(segment);
public IEnumerable<Player> SetReady(Player player) public IEnumerable<Player> SetReady(string username)
{ {
if (!Players.Contains(player)) var player = Players.FirstOrDefault(p => p.Username == username);
if (player is null)
throw new PlayerNotFoundException("The player was not found in the game group."); throw new PlayerNotFoundException("The player was not found in the game group.");
player.State = State.Ready; player.State = State.Ready;
return Players; return Players;