Exemple #1
0
    def post(self):
        if not _array_has_values(self.request.arguments(),
                                 ['join_hash', 'board']):
            self.response.set_status(STATUS_CODES.INTERNAL_ERROR)
            return

        join_hash = self.request.get('join_hash')
        board = json.loads(self.request.get('board'))

        for row in board:
            for piece in row:
                piece['side'] = 1

        game = models.Game.query(models.Game.join_hash == join_hash).get()

        if game:
            game.set_blue_setup(board)
            game.game_state = GAME_STATES.READY
            game.put()

            # Tell red we're ready.
            pusher = Pusher(app_id=PUSHER_CREDENTIALS.APP_ID,
                            key=PUSHER_CREDENTIALS.KEY,
                            secret=PUSHER_CREDENTIALS.SECRET)

            pusher.trigger('public-game-%s' % game.red_hash, 'blue_ready', {})

            game_dict = board_utils.get_sendable_game(game, 1)

            self.response.headers['Content-Type'] = 'text/json'
            self.response.write(json.dumps(game_dict))

        else:
            self.response.set_status(STATUS_CODES.NOT_FOUND)
            return
Exemple #2
0
    def get(self):
        player_hash = self.request.get('player_hash')

        if not player_hash:
            self.response.set_status(STATUS_CODES.UNAUTHORIZED)
            return

        game = models.Game.query(
            models.Game.red_hash == player_hash
        ).get()
        side = 0

        if not game:
            game = models.Game.query(
                models.Game.blue_hash == player_hash
            ).get()
            side = 1

        # If still not ;)
        if not game:
            self.response.set_status(STATUS_CODES.NOT_FOUND)
            return

        game_dict = board_utils.get_sendable_game(game, side)

        self.response.headers['Content-Type'] = 'text/json'
        self.response.write(json.dumps(game_dict))
Exemple #3
0
    def get(self):
        player_hash = self.request.get('player_hash')

        if not player_hash:
            self.response.set_status(status_codes.UNAUTHORIZED)
            return

        game = models.Game.query(
            models.Game.red_hash == player_hash
        ).get()
        side = 0

        if not game:
            game = models.Game.query(
                models.Game.blue_hash == player_hash
            ).get()
            side = 1

        # If still not ;)
        if not game:
            self.response.set_status(status_codes.NOT_FOUND)
            return

        game_dict = board_utils.get_sendable_game(game, side)

        self.response.headers['Content-Type'] = 'text/json'
        self.response.write(json.dumps(game_dict))
Exemple #4
0
    def post(self):
        json_board = self.request.get('board')
        board = json.loads(json_board)

        game_dict = board_utils.get_sendable_game(_create_game(board), 0)

        self.response.headers['Content-Type'] = 'text/json'
        self.response.write(json.dumps(game_dict))
Exemple #5
0
    def post(self):
        json_board = self.request.get('board')
        board = json.loads(json_board)

        game_dict = board_utils.get_sendable_game(_create_game(board), 0)

        self.response.headers['Content-Type'] = 'text/json'
        self.response.write(json.dumps(game_dict))
Exemple #6
0
    def post(self):
        if not _array_has_values(self.request.arguments(), ['join_hash', 'board']):
            self.response.set_status(STATUS_CODES.INTERNAL_ERROR)
            return

        join_hash = self.request.get('join_hash')
        board = json.loads(self.request.get('board'))

        for row in board:
            for piece in row:
                piece['side'] = 1

        game = models.Game.query(
            models.Game.join_hash == join_hash
        ).get()

        if game:
            game.set_blue_setup(board)
            game.game_state = GAME_STATES.READY
            game.put()

            # Tell red we're ready.
            pusher = Pusher(app_id=PUSHER_CREDENTIALS.APP_ID,
                            key=PUSHER_CREDENTIALS.KEY,
                            secret=PUSHER_CREDENTIALS.SECRET)

            pusher.trigger('public-game-%s' % game.red_hash,
                           'blue_ready',
                           {})

            game_dict = board_utils.get_sendable_game(game, 1)

            self.response.headers['Content-Type'] = 'text/json'
            self.response.write(json.dumps(game_dict))

        else:
            self.response.set_status(STATUS_CODES.NOT_FOUND)
            return
Exemple #7
0
    def post(self):
        if not _array_has_values(self.request.arguments(), ['player_hash', 'side', 'from', 'to']):
            self.response.set_status(STATUS_CODES.INTERNAL_ERROR)
            return

        player_hash = self.request.get('player_hash')
        side = int(self.request.get('side'))
        from_pos = json.loads(self.request.get('from'))
        to_pos = json.loads(self.request.get('to'))

        if side == 0:
            game = models.Game.query(
                models.Game.red_hash == player_hash
            ).get()

        elif side == 1:
            game = models.Game.query(
                models.Game.blue_hash == player_hash
            ).get()

        if game.has_ended():
            self.response.set_status(STATUS_CODES.UNAUTHORIZED)
            return

        try:
            # We need to reverse if we're blue
            if side == 1:
                board_utils.reverse_position(from_pos)
                board_utils.reverse_position(to_pos)

            # Will raise if not valid.
            move_type = game.check_move(from_pos, to_pos)

            if move_type == MOVE_TYPES.MOVE:
                game.move_piece(from_pos, to_pos)
                game.flip_turn()
                game.set_last_move({
                    'type': 'move',
                    'from': {
                        'position': from_pos
                    },
                    'to': {
                        'position': to_pos
                    }
                })

            elif move_type == MOVE_TYPES.ATTACK_WON:
                from_piece = game.get_piece(from_pos)
                to_piece = game.get_piece(to_pos)

                game.move_piece(from_pos, to_pos)

                game.flip_turn()
                game.set_last_move({
                    'type': 'won',
                    'from': {
                        'piece': from_piece,
                        'position': from_pos
                    },
                    'to': {
                        'piece': to_piece,
                        'position': to_pos
                    }
                })

            elif move_type == MOVE_TYPES.ATTACK_LOST:
                from_piece = game.get_piece(from_pos)
                to_piece = game.get_piece(to_pos)

                game.delete_piece(from_pos)

                game.flip_turn()
                game.set_last_move({
                    'type': 'lost',
                    'from': {
                        'piece': from_piece,
                        'position': from_pos
                    },
                    'to': {
                        'piece': to_piece,
                        'position': to_pos
                    }
                })

            elif move_type == MOVE_TYPES.ATTACK_DRAW:
                from_piece = game.get_piece(from_pos)
                to_piece = game.get_piece(to_pos)

                game.delete_piece(from_pos)
                game.delete_piece(to_pos)

                game.flip_turn()
                game.set_last_move({
                    'type': 'draw',
                    'from': {
                        'piece': from_piece,
                        'position': from_pos
                    },
                    'to': {
                        'piece': to_piece,
                        'position': to_pos
                    }
                })

            elif move_type == MOVE_TYPES.CAPTURE:
                from_piece = game.get_piece(from_pos)
                to_piece = game.get_piece(to_pos)

                game.move_piece(from_pos, to_pos)

                game.set_last_move({
                    'type': 'capture',
                    'from': {
                        'piece': from_piece,
                        'position': from_pos
                    },
                    'to': {
                        'piece': to_piece,
                        'position': to_pos
                    }
                })

            game.put()

            # Tell clients to update
            pusher = Pusher(app_id=PUSHER_CREDENTIALS.APP_ID,
                            key=PUSHER_CREDENTIALS.KEY,
                            secret=PUSHER_CREDENTIALS.SECRET)

            pusher.trigger('public-game-%s' % game.get_opponent_hash(player_hash),
                           'update',
                           {'command': 'refresh'})

            game_dict = board_utils.get_sendable_game(game, side)

            self.response.headers['Content-Type'] = 'text/json'
            self.response.write(json.dumps(game_dict))

        except models.InvalidMove as e:
            self.response.write(json.dumps({
                'message': str(e)
            }))
            self.response.set_status(STATUS_CODES.UNAUTHORIZED)
Exemple #8
0
    def post(self):
        if not _array_has_values(self.request.arguments(),
                                 ['player_hash', 'side', 'from', 'to']):
            self.response.set_status(STATUS_CODES.INTERNAL_ERROR)
            return

        player_hash = self.request.get('player_hash')
        side = int(self.request.get('side'))
        from_pos = json.loads(self.request.get('from'))
        to_pos = json.loads(self.request.get('to'))

        if side == 0:
            game = models.Game.query(models.Game.red_hash == player_hash).get()

        elif side == 1:
            game = models.Game.query(
                models.Game.blue_hash == player_hash).get()

        # These checks fix an issue where an opponent can move the other players
        # piece when they attacked and lost.
        if game.red_hash == player_hash and game.turn != False:
            self.response.set_status(STATUS_CODES.UNAUTHORIZED)
            return
        elif game.blue_hash == player_hash and game.turn != True:
            self.response.set_status(STATUS_CODES.UNAUTHORIZED)
            return

        if game.has_ended():
            self.response.set_status(STATUS_CODES.UNAUTHORIZED)
            return

        try:
            # We need to reverse if we're blue
            if side == 1:
                board_utils.reverse_position(from_pos)
                board_utils.reverse_position(to_pos)

            # Will raise if not valid.
            move_type = game.check_move(from_pos, to_pos)

            if move_type == MOVE_TYPES.MOVE:
                game.move_piece(from_pos, to_pos)
                game.flip_turn()
                game.set_last_move({
                    'type': 'move',
                    'from': {
                        'position': from_pos
                    },
                    'to': {
                        'position': to_pos
                    }
                })

            elif move_type == MOVE_TYPES.ATTACK_WON:
                from_piece = game.get_piece(from_pos)
                to_piece = game.get_piece(to_pos)

                game.move_piece(from_pos, to_pos)

                game.flip_turn()
                game.set_last_move({
                    'type': 'won',
                    'from': {
                        'piece': from_piece,
                        'position': from_pos
                    },
                    'to': {
                        'piece': to_piece,
                        'position': to_pos
                    }
                })

            elif move_type == MOVE_TYPES.ATTACK_LOST:
                from_piece = game.get_piece(from_pos)
                to_piece = game.get_piece(to_pos)

                game.delete_piece(from_pos)

                game.flip_turn()
                game.set_last_move({
                    'type': 'lost',
                    'from': {
                        'piece': from_piece,
                        'position': from_pos
                    },
                    'to': {
                        'piece': to_piece,
                        'position': to_pos
                    }
                })

            elif move_type == MOVE_TYPES.ATTACK_DRAW:
                from_piece = game.get_piece(from_pos)
                to_piece = game.get_piece(to_pos)

                game.delete_piece(from_pos)
                game.delete_piece(to_pos)

                game.flip_turn()
                game.set_last_move({
                    'type': 'draw',
                    'from': {
                        'piece': from_piece,
                        'position': from_pos
                    },
                    'to': {
                        'piece': to_piece,
                        'position': to_pos
                    }
                })

            elif move_type == MOVE_TYPES.CAPTURE:
                from_piece = game.get_piece(from_pos)
                to_piece = game.get_piece(to_pos)

                game.move_piece(from_pos, to_pos)

                game.set_last_move({
                    'type': 'capture',
                    'from': {
                        'piece': from_piece,
                        'position': from_pos
                    },
                    'to': {
                        'piece': to_piece,
                        'position': to_pos
                    }
                })

            game.put()

            # Tell clients to update
            pusher = Pusher(app_id=PUSHER_CREDENTIALS.APP_ID,
                            key=PUSHER_CREDENTIALS.KEY,
                            secret=PUSHER_CREDENTIALS.SECRET)

            pusher.trigger(
                'public-game-%s' % game.get_opponent_hash(player_hash),
                'update', {'command': 'refresh'})

            game_dict = board_utils.get_sendable_game(game, side)

            self.response.headers['Content-Type'] = 'text/json'
            self.response.write(json.dumps(game_dict))

        except models.InvalidMove as e:
            self.response.write(json.dumps({'message': str(e)}))
            self.response.set_status(STATUS_CODES.UNAUTHORIZED)
Exemple #9
0
    def post(self):
        if not general.array_has_values(self.request.arguments(), ['player_hash', 'side', 'from', 'to']):
            self.response.set_status(status_codes.INTERNAL_ERROR)
            return

        player_hash = self.request.get('player_hash')
        side = int(self.request.get('side'))
        from_pos = json.loads(self.request.get('from'))
        to_pos = json.loads(self.request.get('to'))

        if side == 0:
            game = models.Game.query(
                models.Game.red_hash == player_hash
            ).get()

        elif side == 1:
            game = models.Game.query(
                models.Game.blue_hash == player_hash
            ).get()

        if game.has_ended():
            self.response.set_status(status_codes.UNAUTHORIZED)
            return

        try:
            # Will raise if not valid.
            move_type = game.check_move(from_pos, to_pos)

            if move_type == move_types.MOVE:
                game.move_piece(from_pos, to_pos)
                game.flip_turn()
                game.set_last_move({
                    'type': 'move',
                    'from': {
                        'position': from_pos
                    },
                    'to': {
                        'position': to_pos
                    }
                })

            elif move_type == move_types.ATTACK_WON:
                from_piece = game.get_piece(from_pos)
                to_piece = game.get_piece(to_pos)

                game.move_piece(from_pos, to_pos)

                game.flip_turn()
                game.set_last_move({
                    'type': 'won',
                    'from': {
                        'piece': from_piece,
                        'position': from_pos
                    },
                    'to': {
                        'piece': to_piece,
                        'position': to_pos
                    }
                })

            elif move_type == move_types.ATTACK_LOST:
                from_piece = game.get_piece(from_pos)
                to_piece = game.get_piece(to_pos)

                game.delete_piece(from_pos)

                game.flip_turn()
                game.set_last_move({
                    'type': 'lost',
                    'from': {
                        'piece': from_piece,
                        'position': from_pos
                    },
                    'to': {
                        'piece': to_piece,
                        'position': to_pos
                    }
                })

            elif move_type == move_types.ATTACK_DRAW:
                from_piece = game.get_piece(from_pos)
                to_piece = game.get_piece(to_pos)

                game.delete_piece(from_pos)
                game.delete_piece(to_pos)

                game.flip_turn()
                game.set_last_move({
                    'type': 'draw',
                    'from': {
                        'piece': from_piece,
                        'position': from_pos
                    },
                    'to': {
                        'piece': to_piece,
                        'position': to_pos
                    }
                })

            elif move_type == move_types.CAPTURE:
                from_piece = game.get_piece(from_pos)
                to_piece = game.get_piece(to_pos)

                game.move_piece(from_pos, to_pos)

                game.set_last_move({
                    'type': 'capture',
                    'from': {
                        'piece': from_piece,
                        'position': from_pos
                    },
                    'to': {
                        'piece': to_piece,
                        'position': to_pos
                    }
                })

            game.put()

            # Tell clients to update
            pusher = Pusher(app_id=pusher_utils.APP_ID,
                            key=pusher_utils.KEY,
                            secret=pusher_utils.SECRET)

            pusher.trigger('public-game-%s' % game.get_opponent_hash(player_hash),
                           'update',
                           {'command': 'refresh'})

            game_dict = board_utils.get_sendable_game(game, side)

            self.response.headers['Content-Type'] = 'text/json'
            self.response.write(json.dumps(game_dict))

        except models.InvalidMove:
            self.response.set_status(status_codes.UNAUTHORIZED)