public IHttpActionResult MovePlayer([FromBody] Location location)
        {
            AuthResult auth = authorizeAndVerifyGameStart();

            if (auth.result != null)
            {
                return(auth.result);
            }

            var game   = auth.game.getGame();
            var player = auth.player;

            if (!isPlayerTurn(game, player))
            {
                return(Unauthorized());
            }

            if (game.movePlayer(player.Name, location))
            {
                Command command = new Command();
                command.command = CommandType.MovePlayer;
                command.data    = new CommandData {
                    moveData = new MoveData {
                        playerName = player.Name, location = location
                    }
                };
                CommandInterface.SetCommandForEveryone(auth.game, command);

                return(Created("", ""));
            }

            return(BadRequest("Invalid move"));
        }
        public IHttpActionResult MakeAccusation([FromBody] Accusation accusation)
        {
            AuthResult auth = authorizeAndVerifyGameStart();

            if (auth.result != null)
            {
                return(auth.result);
            }

            var game   = auth.game.getGame();
            var player = auth.player;

            if (!isPlayerTurn(game, player))
            {
                return(Unauthorized());
            }

            bool           isCorrect = game.makeAccusation(player.Name, accusation);
            AccusationData data      = new AccusationData {
                accusation        = accusation,
                playerName        = player.Name,
                accusationCorrect = isCorrect
            };

            var cmd = new Command {
                command = CommandType.AccusationMade,
                data    = new CommandData {
                    accusationData = data
                }
            };

            CommandInterface.SetCommandForEveryone(auth.game, cmd);

            return(Created("", data));
        }
        public IHttpActionResult MakeSuggestion([FromBody] Accusation accusation)
        {
            AuthResult auth = authorizeAndVerifyGameStart();

            if (auth.result != null)
            {
                return(auth.result);
            }

            // TODO: validate accusationData data

            var game   = auth.game.getGame();
            var player = auth.player;

            if (!isPlayerTurn(game, player))
            {
                return(Unauthorized());
            }

            Command command = new Command();

            command.command = CommandType.SuggestionMade;

            SuggestionData data = new SuggestionData {
                playerName = player.Name, accusation = accusation
            };
            var cmdData = new CommandData {
                suggestData = data
            };
            var disprovingPlayer = game.makeSuggestion(player.Name, accusation);

            if (disprovingPlayer != null)
            {
                data.disprovingPlayer = disprovingPlayer.name;
                var disproveCmd = new Command {
                    command = CommandType.DisproveSuggestion, data = cmdData
                };
                CommandInterface.SetCommandForPlayer(disprovingPlayer.name, disproveCmd);

                var waitCmd = new Command {
                    command = CommandType.Wait
                };
                CommandInterface.SetCommandForPlayer(player.Name, waitCmd);
            }

            command.data = cmdData;
            CommandInterface.SetCommandForEveryone(auth.game, command);

            return(Created("", data));
        }
        public IHttpActionResult EndTurn()
        {
            AuthResult auth = authorizeAndVerifyGameStart();

            if (auth.result != null)
            {
                return(auth.result);
            }

            var game   = auth.game.getGame();
            var player = auth.player;

            if (!isPlayerTurn(game, player))
            {
                return(Unauthorized());
            }

            game.nextPlayer();

            var cmd = new Command {
                command = CommandType.TurnEnd
            };

            CommandInterface.SetCommandForEveryone(auth.game, cmd);

            // remove 'TakeTurn' command from current player
            CommandInterface.SetCommandForPlayer(auth.player.Name, null);

            // Set command for next player's turn
            cmd = new Command {
                command = CommandType.TakeTurn
            };
            var nextPlayer = game.getPlayerTurn();

            CommandInterface.SetCommandForPlayer(nextPlayer.name, cmd);

            return(Created("", ""));
        }
예제 #5
0
        // This API can only be called by the host player
        public IHttpActionResult StartGame()
        {
            AuthResult auth = authorizePlayerMatchesGame();

            if (auth.result != null)
            {
                return(auth.result);
            }

            PlayerModel player = auth.player;
            GameModel   game   = auth.game;

            if (!game.Hostname.Equals(player.Name))
            {
                return(Unauthorized());
            }

            if (game.start())
            {
                var cmd = new Command {
                    command = CommandType.GameStart
                };
                CommandInterface.SetCommandForEveryone(game, cmd);

                var startPlayer = game.getGame().getPlayerTurn().name;
                cmd = new Command {
                    command = CommandType.TakeTurn
                };
                CommandInterface.SetCommandForPlayer(startPlayer, cmd);

                return(StatusCode(HttpStatusCode.NoContent));
            }
            else
            {
                return(BadRequest());
            }
        }