public async Task <ActionResult <Guid> > POST([FromBody] RegisterTeamViewModel registerTeamViewModel) { // Generate a unique playerID var player = new Player(registerTeamViewModel.TeamName); var hashedPassword = _passwordHashing.HashPassword(registerTeamViewModel.Password); await _database.SavePlayer(player, hashedPassword); // Reload the player var reloadedPlayer = await _database.LoadPlayer(player.ID); if (reloadedPlayer == null) { var details = new ValidationProblemDetails { Detail = $"No player with the ID {player.ID} exists" }; return(ValidationProblem(details)); } if (!_passwordHashing.CompareHashes(registerTeamViewModel.Password, reloadedPlayer.Password)) { return(Forbid()); } // Create them a game for them to develop against if (!reloadedPlayer.CurrentGameID.HasValue) { await _gameCreator.CreateInitialGame(reloadedPlayer.ID); } return(reloadedPlayer.ID); }
public async Task <ActionResult> POST([FromBody] MakeMoveViewModel makeMoveViewModel) { // Get the details of this player var player = await _database.LoadPlayer(makeMoveViewModel.PlayerId); if (player == null) { return(BadRequest("The player with this ID does not exist")); } if (!_passwordHashing.CompareHashes(makeMoveViewModel.Password, player.Password)) { return(Forbid()); } // Retrieve the current state of the game if (!player.CurrentGameID.HasValue) { return(BadRequest("Your player is not currently playing a game. Call NewGame")); } var game = await _database.LoadGame(player.CurrentGameID.Value); if (game == null) { return(BadRequest("Your player is not currently playing a game. Call NewGame")); } if (game.CurrentState != Models.GameState.RedToPlay && game.CurrentState != Models.GameState.YellowToPlay) { throw new Exception("This game is not playable"); } // Is it the players turn var playerIsYellow = (game.YellowPlayerID == player.ID); if (playerIsYellow && !game.YellowToPlay()) { throw new Exception("It is RED's turn to play. You are YELLOW."); } if ((!playerIsYellow) && game.YellowToPlay()) { throw new Exception("It is YELLOW's turn to play. You are RED."); } // Is the move allowed? if (!game.IsMoveAllowed(makeMoveViewModel.ColumnNumber)) { throw new Exception("Sorry that move is not allowed"); } // Has the player won? game.MakeMove(playerIsYellow, makeMoveViewModel.ColumnNumber); // Store away the updated game await _database.SaveGame(game); // Is the player playing against our bot? if (game.CurrentState == Models.GameState.RedToPlay || game.CurrentState == Models.GameState.YellowToPlay) { var otherPlayerID = (game.RedPlayerID == makeMoveViewModel.PlayerId) ? game.YellowPlayerID : game.RedPlayerID; var otherPlayer = await _database.LoadPlayer(otherPlayerID); if (otherPlayer is null) { throw new Exception("Player does not exist."); } if (otherPlayer.SystemBot) { var bot = _botCreator.GetSystemBot(otherPlayerID); await bot.MakeMove(game); await _database.SaveGame(game); } // The other player supports http webhooks if (!string.IsNullOrWhiteSpace(otherPlayer.WebHook)) { var bot = _botCreator.GetWebHookBot(new Uri(otherPlayer.WebHook), otherPlayerID); await bot.MakeMove(game); await _database.SaveGame(game); } } return(Ok()); }