Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <Game> > GET([FromRoute] Guid playerID)
        {
            // Get the details of this player
            var player = await _database.LoadPlayer(playerID);

            if (player is null)
            {
                return(NotFound());
            }

            // Retrieve the current state of the game
            var game = default(Game);

            if (!player.CurrentGameID.HasValue)
            {
                game = await _gameCreator.CreateInitialGame(playerID);
            }
            else
            {
                game = await _database.LoadGame(player.CurrentGameID.Value);
            }

            if (game == null)
            {
                return(BadRequest("There is no game for this player"));
            }

            // If it's not this player's turn then we force them to wait.
            if (!((game.YellowToPlay() && game.YellowPlayerID == playerID) || (game.RedToPlay() && game.RedPlayerID == playerID)))
            {
                await Task.Run(() => Thread.Sleep(500));
            }
            return(game);
        }
Ejemplo n.º 3
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());
        }