Exemplo n.º 1
0
def save_guess(guess: int, id_game: str, id_player: int) -> None:
    """
    Save the user guess
    :param guess: The guess
    :param id_game: The id of the game
    :param id_player: The id of the player
    """
    session = session_factory()

    game = session.query(Game).filter(Game.id == id_game).first()
    if not game:
        create_game(id_game, id_player)

    last_guess = get_last_guess_of_game(id_game)
    if not last_guess:
        guess_count = 1
    else:
        guess_count = last_guess.guess_count + 1

    won = is_winning_guess(id_game, guess)

    guess = Guess(guess=guess,
                  guess_count=guess_count,
                  is_winning_guess=won,
                  id_game=id_game)

    session.add(guess)
    session.commit()
    session.close()
Exemplo n.º 2
0
    async def take_turn(self, message):
        player = getattr(self.game, f"player_{message.body['player_id']}")
        player.drawing_block()
        await self.pick_block(player=player, index=message.body['block_index'])
        request = await player.ws.recv()
        request = Request.deserialize(value=request)
        guess = Guess(**request.body)

        await self.guess_block(player, guess=guess)
Exemplo n.º 3
0
    async def guess_block(self, player, guess: Guess):
        from_player = player  # type: Player
        to_player = getattr(self.game,
                            f'player_{guess.to_player_id}')  # type: Player
        success = from_player.guess_block(target_player=to_player,
                                          target_block_index=guess.target,
                                          guess=guess.guess)
        await self.distribute_game()
        if success:
            response = Response(action=Actions.GUESS_SUCCESS.value,
                                message="Guess Succeeded!",
                                body="")
            from_player.guessing_more()
            await self.distribute_response(response=response)
            await self.distribute_game()
            next_action_message = await player.ws.recv()
            request = Request.deserialize(value=next_action_message)

            if request.action == Actions.MAKE_GUESS.value:
                guess = Guess(**request.body)
                await self.guess_block(player, guess)

            elif request.action == Actions.YIELD_TURN.value:
                from_player.get_ready()
                self.game.swap_turn()
                next_player = getattr(self.game, f'player_{self.game.turn}')
                next_player.drawing_block()
            else:
                raise TypeError('Invalid Action type.')

        else:
            response = Response(action=Actions.GUESS_FAIL.value,
                                message="Guess Failed!",
                                body="")
            await self.distribute_response(response=response)
            from_player.get_ready()
            self.game.swap_turn()
            next_player = getattr(self.game, f'player_{self.game.turn}')
            next_player.drawing_block()
        return self.game
Exemplo n.º 4
0
    def make_move(self, request):
        """endpoint to guess a letter or the word"""
        game = get_by_urlsafe(request.urlsafe_game_key, Game)
        # make guess lower case
        guess = request.guess.lower()
        guess_obj = Guess(guess=guess, msg="")

        if game.game_over:
            raise endpoints.ForbiddenException(
                'Illegal action: Game is already over.')

        guess_obj.guess_num = 1 + len(game.guess_history)
        guess_obj.word_state = ' '.join(game.guess_state)

        # check if isalpha
        if guess.isalpha() == False:
            raise endpoints.ForbiddenException('Illegal action: Letters only.')

        if guess == game.target:
            game.end_game(True)
            msg = 'You guessed the word, you win!'
            guess_obj.msg = msg
            guess_obj.word_state = game.target
            game.guess_hist_obj.append(guess_obj)
            game.guess_state = [letter for letter in game.target]
            game.put()
            return game.to_form(msg)

        if guess in game.guess_history:
            msg = "You already tried that, guess again. Lose a turn for being foolish."
            game.attempts_remaining -= 1
            guess_obj.msg = msg
            game.guess_hist_obj.append(guess_obj)
            game.put()
            return game.to_form(msg)

        # check if guess is a single letter
        if len(guess) == 1:

            game.guess_history.append(guess)

            if guess in game.target:
                msg = 'The word contains your letter!'
                game.update_guess_state(guess)
                guess_obj.word_state = ' '.join(game.guess_state)
            else:
                msg = 'Nope! Guess Again!'
                game.attempts_remaining -= 1

            if game.target == ''.join(game.guess_state):
                game.end_game(True)
                msg = 'You guessed all the letters, you win!'
                guess_obj.msg = msg
                game.guess_hist_obj.append(guess_obj)
                game.put()
                return game.to_form(msg)
        # otherwise, it's not a single letter or the word guess is wrong.
        else:
            game.attempts_remaining -= 1
            msg = 'Incorrect, try again.'

        if game.attempts_remaining < 1:
            game.end_game(False)
            guess_obj.msg = msg + ' Game over!'
            game.guess_hist_obj.append(guess_obj)
            game.put()
            return game.to_form(msg + ' Game over!')
        else:
            guess_obj.msg = msg
            game.guess_history.append(guess)
            game.update_guess_state(guess)
            game.guess_hist_obj.append(guess_obj)
            game.put()
            return game.to_form(msg)