Beispiel #1
0
def add_player(game_id, player_request_dto):
    """ Adds a player to a game.
    Creates the game if it does not exist.
    After adding the player, the game is started.

    :param game_id: specifies the game
    :param player_request_dto: if this parameter is given, it contains information about the type of player to add
    :return: the added player
    """
    _ = interactors.OverduePlayerInteractor(game_repository(), logging.get_logger())
    _ = interactors.UpdateOnTurnChangeInteractor(game_repository())
    game = _get_or_create_game(game_id)
    is_bot, computation_method = mapper.dto_to_type(player_request_dto)
    player_name = mapper.dto_to_player_name(player_request_dto)
    player_id = _try(game.unused_player_id)
    player = None
    if not is_bot:
        player = Player(player_id, player_name=player_name)
    else:
        player = _try(lambda: bots.create_bot(compute_method=computation_method,
                                              url_supplier=URLSupplier(), player_id=player_id,
                                              player_name=player_name))
    _try(lambda: game.add_player(player))
    DatabaseGateway.get_instance().update_game(game_id, game)
    DatabaseGateway.get_instance().commit()
    logging.get_logger().add_player(player_id, game_id=game_id, is_bot=is_bot, num_players=len(game.players))
    return mapper.player_to_dto(player)
Beispiel #2
0
def perform_move(game_id, player_id, move_dto):
    """Performs a move operation on the game."""
    location = mapper.dto_to_move_action(move_dto)
    _ = interactors.OverduePlayerInteractor(game_repository())
    interactor = interactors.PlayerActionInteractor(game_repository())
    _try(lambda: interactor.perform_move(game_id, player_id, location))
    DatabaseGateway.get_instance().commit()
Beispiel #3
0
def perform_shift(game_id, player_id, shift_dto):
    """Performs a shift operation on the game."""
    location, rotation = mapper.dto_to_shift_action(shift_dto)
    _ = interactors.OverduePlayerInteractor(game_repository(), logging.get_logger())
    _ = interactors.UpdateOnTurnChangeInteractor(game_repository())
    interactor = interactors.PlayerActionInteractor(game_repository())
    _try(lambda: interactor.perform_shift(game_id, player_id, location, rotation))
    DatabaseGateway.get_instance().commit()
Beispiel #4
0
def get_game_state(game_id):
    """ Returns the game state """
    _ = interactors.OverduePlayerInteractor(game_repository())
    interactor = interactors.ObserveGameInteractor(game_repository())
    game = _try(lambda: interactor.retrieve_game(game_id))
    game_state = mapper.game_state_to_dto(game)
    DatabaseGateway.get_instance().commit()
    return game_state
Beispiel #5
0
def get_game_state(game_id):
    """ Returns the game state """
    _ = interactors.OverduePlayerInteractor(game_repository(), logging.get_logger())
    _ = interactors.UpdateOnTurnChangeInteractor(game_repository())
    action_timeout = timedelta(seconds=int(current_app.config["OVERDUE_PLAYER_TIMEDELTA_S"]))
    interactor = interactors.ObserveGameInteractor(game_repository(), action_timeout=action_timeout)
    game, remaining_timedelta = _try(lambda: interactor.retrieve_game(game_id))
    game_state = mapper.game_state_to_dto(game, remaining_timedelta)
    DatabaseGateway.get_instance().commit()
    return game_state
Beispiel #6
0
def test_remove_overdue_players__with_one_blocking_player__removes_current_player(
):
    game, player = setup_test()
    game.remove_player = Mock()
    game_repository = when_game_repository_find_all_before_action_timestamp_then_return(
        [game])

    interactor = interactors.OverduePlayerInteractor(game_repository)
    interactor.remove_overdue_players()

    game.remove_player.assert_called_once_with(3)
def test_interactor__when_game_notifies_turn_listeners__updates_player_action_timestamp(
):
    game, _ = setup_test()
    data_access = DatabaseGateway(settings={"DATABASE": "foo"})
    game_repository = interactors.GameRepository(data_access)
    game_repository.update_action_timestamp = Mock()
    _ = interactors.OverduePlayerInteractor(game_repository, logger)
    data_access._notify_listeners(game)
    game._notify_turn_listeners()

    game_repository.update_action_timestamp.assert_called_once_with(
        game, matchers.time_close_to(datetime.now()))
def test_remove_overdue_players__with_player_just_removed__does_not_raise_exception(
):
    """ When the last remaining player was removed or has left the game,
    the update timestamp will stay at its last value. Then, the interactor must not try to remove a player.
    """
    game, player = setup_test()
    game.remove_player(player.identifier)
    game_repository = when_game_repository_find_all_before_action_timestamp_then_return(
        [game])

    interactor = interactors.OverduePlayerInteractor(game_repository, logger)
    interactor.remove_overdue_players()
Beispiel #9
0
def delete_player(game_id, player_id):
    """ Removes a player from a game

    :param game_id: specifies the game
    :param player_id: specifies the player to remove
    """
    _ = interactors.OverduePlayerInteractor(game_repository())
    game = _load_game_or_throw(game_id, for_update=True)
    _try(lambda: game.remove_player(player_id))
    DatabaseGateway.get_instance().update_game(game_id, game)
    DatabaseGateway.get_instance().commit()
    return ""
Beispiel #10
0
def change_game(game_id, game_request_dto):
    """ Changes game setup.

    Currently, the only option is to change the maze size.
    This will restart the game.
    :param game_id: specifies the game. Has to exist.
    :param game_request_dto: contains the new maze size."""
    new_size = mapper.dto_to_maze_size(game_request_dto)
    _ = interactors.OverduePlayerInteractor(game_repository())
    game = _load_game_or_throw(game_id)
    new_board = _try(lambda: factory.create_board(maze_size=new_size))
    _try(lambda: game.replace_board(new_board))
    DatabaseGateway.get_instance().update_game(game_id, game)
    DatabaseGateway.get_instance().commit()
Beispiel #11
0
def remove_overdue_players(overdue_timedelta):
    """ Uses OverduePlayerInteractor to remove players which block the game by not performing actions """
    interactor = interactors.OverduePlayerInteractor(game_repository(), logging.get_logger())
    _try(lambda: interactor.remove_overdue_players(overdue_timedelta))
    DatabaseGateway.get_instance().commit()