Error action
This commit is contained in:
parent
f3b1641d4b
commit
fdcc1b8f34
@ -84,7 +84,7 @@ const Tile: FC<TileProps> = (
|
||||
case TileType.ghostSpawn:
|
||||
return "bg-red-500";
|
||||
case TileType.pacmanSpawn:
|
||||
return "bg-green-500";
|
||||
return "bg-green-500"; // TODO should be the colour of the player
|
||||
default:
|
||||
return "bg-black";
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import {currentPlayerNameAtom, diceAtom, ghostsAtom, playersAtom, rollDiceButton
|
||||
import {Colour} from "../game/colour";
|
||||
|
||||
export enum GameAction {
|
||||
error,
|
||||
rollDice,
|
||||
moveCharacter,
|
||||
joinGame,
|
||||
@ -37,6 +38,9 @@ export const doAction: MessageEventFunction<string> = (event): void => {
|
||||
console.debug("Received message:", message);
|
||||
|
||||
switch (message.action as GameAction) {
|
||||
case GameAction.error:
|
||||
console.error("Error:", message.data); // TODO show error to user
|
||||
break;
|
||||
case GameAction.rollDice:
|
||||
setDice(message.data);
|
||||
break;
|
||||
|
@ -79,11 +79,26 @@ public class GameController : GenericController
|
||||
Logger.Log(LogLevel.Information, "Received: {}", stringResult);
|
||||
var action = ActionMessage.FromJson(stringResult);
|
||||
|
||||
_actionService.DoAction(action);
|
||||
try
|
||||
{
|
||||
_actionService.DoAction(action);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.Log(LogLevel.Error, "{}", e.Message);
|
||||
action = new ActionMessage { Action = GameAction.Error, Data = e.Message };
|
||||
}
|
||||
|
||||
return action.ToArraySegment();
|
||||
}
|
||||
|
||||
protected override void Send(ArraySegment<byte> segment) => _actionService.SendToAll(segment);
|
||||
protected override async void Send(ArraySegment<byte> segment)
|
||||
{
|
||||
if (_actionService.Game is not null)
|
||||
_actionService.SendToAll(segment);
|
||||
else if (WebSocket is not null)
|
||||
await _gameService.Send(WebSocket, segment);
|
||||
}
|
||||
|
||||
protected override ArraySegment<byte>? Disconnect() =>
|
||||
new ActionMessage { Action = GameAction.Disconnect, Data = _actionService.Disconnect() }
|
||||
|
@ -5,6 +5,7 @@ namespace pacMan.GameStuff;
|
||||
|
||||
public enum GameAction
|
||||
{
|
||||
Error,
|
||||
RollDice,
|
||||
MoveCharacter,
|
||||
JoinGame,
|
||||
|
@ -10,7 +10,7 @@ namespace pacMan.Services;
|
||||
public interface IActionService
|
||||
{
|
||||
Player Player { set; }
|
||||
Game Game { set; }
|
||||
Game? Game { get; set; }
|
||||
WebSocket? WebSocket { set; }
|
||||
void DoAction(ActionMessage message);
|
||||
List<int> RollDice();
|
||||
@ -68,7 +68,7 @@ public class ActionService : IActionService
|
||||
if (Game != null && jsonElement.HasValue)
|
||||
{
|
||||
Game.Ghosts = jsonElement.Value.GetProperty("ghosts").Deserialize<List<Character>>() ??
|
||||
throw new JsonException("Ghosts is null");
|
||||
throw new NullReferenceException("Ghosts is null");
|
||||
Game.Players = jsonElement.Value.GetProperty("players").Deserialize<List<Player>>() ??
|
||||
throw new NullReferenceException("Players is null");
|
||||
}
|
||||
@ -82,10 +82,10 @@ public class ActionService : IActionService
|
||||
var data = jsonElement?.Deserialize<JoinGameData>() ?? throw new NullReferenceException("Data is null");
|
||||
|
||||
var game = _gameService.Games.FirstOrDefault(game => game.Id == data.GameId) ??
|
||||
throw new GameNotFoundException();
|
||||
throw new GameNotFoundException($"Game was not found, id \"{data.GameId}\" does not exist");
|
||||
|
||||
var player = game.Players.Find(p => p.Username == data.Username)
|
||||
?? throw new PlayerNotFoundException("Player was not found in game");
|
||||
?? throw new PlayerNotFoundException($"Player \"{data.Username}\" was not found in game");
|
||||
|
||||
player.State = game.IsGameStarted ? State.InGame : State.WaitingForPlayers; // TODO doesn't work anymore
|
||||
Player = player;
|
||||
|
Loading…
x
Reference in New Issue
Block a user