Пример #1
0
        public async Task <ActionResult> OnPost()
        {
            var allPlayers = await _database.GetAllPlayers();

            var thisPlayer = allPlayers.SingleOrDefault(a => a.Name == ViewModel.PlayerName);

            if (thisPlayer == null)
            {
                return(LocalRedirect("/Index"));
            }

            var currentGameID = thisPlayer.CurrentGameID;

            var newGame = new Game();

            newGame.ID             = Guid.NewGuid();
            newGame.YellowPlayerID = ViewModel.OtherPlayerID.Value;
            newGame.RedPlayerID    = thisPlayer.ID;

            var otherPlayer = await _database.LoadPlayer(ViewModel.OtherPlayerID.Value);

            if (otherPlayer.SystemBot)
            {
                var bot = _botCreator.GetSystemBot(ViewModel.OtherPlayerID.Value);
                await bot.MakeMove(newGame);
            }

            // The other player supports http webhooks
            if (!string.IsNullOrWhiteSpace(otherPlayer.WebHook))
            {
                var bot = _botCreator.GetWebHookBot(new Uri(otherPlayer.WebHook), ViewModel.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(LocalRedirect($"/DisplayGame?gameID={newGame.ID}"));
        }
Пример #2
0
        public async Task <ActionResult> POST([FromBody] NewGameViewModel newGameViewModel)
        {
            // Get the details of this player
            var player = await _database.LoadPlayer(newGameViewModel.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 == newGameViewModel.PlayerID) ? newGame.YellowPlayerID : newGame.RedPlayerID;
                if (otherPlayerID == newGame.YellowPlayerID)
                {
                    var otherPlayer = await _database.LoadPlayer(otherPlayerID);

                    if (otherPlayer is null)
                    {
                        throw new Exception("Other player missing from the database!");
                    }

                    if (otherPlayer.SystemBot)
                    {
                        var bot = _botCreator.GetSystemBot(otherPlayerID);
                        await bot.MakeMove(newGame);
                    }

                    // The other player supports http webhooks
                    if (!string.IsNullOrWhiteSpace(otherPlayer.WebHook))
                    {
                        var bot = _botCreator.GetWebHookBot(new Uri(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 _gameCreator.CreateInitialGame(newGameViewModel.PlayerID);
            }
            return(Ok());
        }
Пример #3
0
        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());
        }