public async Task <IHttpActionResult> POST(Guid playerID) { // Get the details of this player var player = await database.LoadPlayer(playerID); if (player == null) { return(BadRequest("No player with this ID exists")); } // Retrieve the current state of the game if (player.CurrentGameID.HasValue) { var currentGame = await database.LoadGame(player.CurrentGameID.Value); // Set up a new game, but they are planning the other player var newGame = new Game(); newGame.ID = Guid.NewGuid(); newGame.YellowPlayerID = currentGame.RedPlayerID; newGame.RedPlayerID = currentGame.YellowPlayerID; // Is the player playing against our bot? Yellow goes first. var otherPlayerID = (newGame.RedPlayerID == playerID) ? newGame.YellowPlayerID : newGame.RedPlayerID; if (otherPlayerID == newGame.YellowPlayerID) { var otherPlayer = await database.LoadPlayer(otherPlayerID); if (otherPlayer.SystemBot) { var bot = BaseBot.GetBot(otherPlayerID); await bot.MakeMove(newGame); } // The other player supports http webhooks if (!string.IsNullOrWhiteSpace(otherPlayer.WebHook)) { var bot = BaseBot.GetWebHookBot(otherPlayer.WebHook, otherPlayerID); await bot.MakeMove(newGame); } } using (var tx = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { // and delete the old game await database.DeleteGame(currentGame.ID); // Create the new game await database.SaveGame(newGame); tx.Complete(); } } else { // For some reason the player doesn't current have a game await CreateInitialGame(playerID); } return(Ok()); }
public async Task <ActionResult> Index(ChangeGameViewModel model) { var allPlayers = await this.database.GetAllPlayers(); var thisPlayer = allPlayers.SingleOrDefault(a => a.Name == model.PlayerName); if (thisPlayer == null) { return(RedirectToAction("Index", "Home")); } var currentGameID = thisPlayer.CurrentGameID; var newGame = new Game(); newGame.ID = Guid.NewGuid(); newGame.YellowPlayerID = model.OtherPlayerID.Value; newGame.RedPlayerID = thisPlayer.ID; var otherPlayer = await database.LoadPlayer(model.OtherPlayerID.Value); if (otherPlayer.SystemBot) { var bot = BaseBot.GetBot(model.OtherPlayerID.Value); await bot.MakeMove(newGame); } // The other player supports http webhooks if (!string.IsNullOrWhiteSpace(otherPlayer.WebHook)) { var bot = BaseBot.GetWebHookBot(otherPlayer.WebHook, model.OtherPlayerID.Value); await bot.MakeMove(newGame); } using (var tx = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { // and delete the old game if (currentGameID.HasValue) { await database.DeleteGame(currentGameID.Value); } // Delete the other player's previous game. (System bots can play multiple games) if (!otherPlayer.SystemBot && otherPlayer.CurrentGameID.HasValue) { await database.DeleteGame(otherPlayer.CurrentGameID.Value); } // Create the new game await database.SaveGame(newGame); tx.Complete(); } return(RedirectToAction("Index", "DisplayGame", new { gameID = newGame.ID })); }
public async Task <IHttpActionResult> POST(Guid playerID, int columnNumber, string password) { // Get the details of this player var player = await database.LoadPlayer(playerID); if (player == null) { return(BadRequest("The player with this ID does not exist")); } if (player.Password != password) { var response = new HttpResponseMessage(HttpStatusCode.Forbidden); response.ReasonPhrase = "Incorrect Password"; return(ResponseMessage(response)); } // Retrieve the current state of the game 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(columnNumber)) { throw new Exception("Sorry that move is not allowed"); } // Has the player won? game.MakeMove(playerIsYellow, 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 == playerID) ? game.YellowPlayerID : game.RedPlayerID; var otherPlayer = await database.LoadPlayer(otherPlayerID); if (otherPlayer.SystemBot) { var bot = BaseBot.GetBot(otherPlayerID); await bot.MakeMove(game); await database.SaveGame(game); } // The other player supports http webhooks if (!string.IsNullOrWhiteSpace(otherPlayer.WebHook)) { var bot = BaseBot.GetWebHookBot(otherPlayer.WebHook, otherPlayerID); await bot.MakeMove(game); await database.SaveGame(game); } } return(Ok()); }