private Task FireAndSendResponse(PacketConnection context, Coordinate coordinate, out Ship ship) { if (_game.LocalPlayer.Board.IsShotAt(in coordinate)) { throw new ProtocolException(ResponseCode.SequenceError, $"Sequence error: {coordinate} has already been fired upon."); } ship = _game.LocalPlayer.Board.ShootAtInternal(coordinate); if (ship is null) { return(context.SendResponseAsync(ResponseCode.FireMiss, "Miss!")); } if (ship.Health > 0) { return(context.SendResponseAsync(GetResponseCode(ship.Type, sunk: false), $"You hit my {ship.Name}")); } if (_game.LocalPlayer.Board.Ships.All(s => s.Health == 0)) { // Game over _game.GameState = GameState.Idle; return(context.SendResponseAsync(ResponseCode.FireYouWin, "You win!")); } return(context.SendResponseAsync(GetResponseCode(ship.Type, sunk: true), $"You sunk my {ship.Name}")); }
/// <inheritdoc /> public Task OnCommandAsync(PacketConnection context, string argument) { _game.ThrowIfNotHost(Command); _game.ThrowIfWrongState(Command, GameState.Idle); _game.GameState = GameState.InGame; _game.IsLocalsTurn = _random.NextBool(); if (_game.IsLocalsTurn) { return(context.SendResponseAsync(ResponseCode.StartHost, "Host starts")); } else { return(context.SendResponseAsync(ResponseCode.StartClient, "Client starts")); } }
/// <inheritdoc /> public async Task OnCommandAsync(PacketConnection context, string argument) { _game.ThrowIfNotHost(Command); _game.ThrowIfWrongState(Command, GameState.Handshake); SetNameFromArgument(argument); await context.SendResponseAsync(ResponseCode.Handshake, _game.LocalPlayer.Name); _game.GameState = GameState.Idle; }