コード例 #1
0
def test_room_created():
    create_test_game()
    assert game_room_exists(TEST_GAME)

    room = get_room(TEST_GAME)
    assert build_player('Mick') in room.team_1_players
    assert build_player('Dvir') in room.team_2_players
コード例 #2
0
def test_create_then_join_one_player_per_team(setup):
    room_name = 'room'
    player = 'Mick'
    request = {PLAYER_NAME: player, ROOM_NAME: room_name}

    result = create_game(request)
    assert ERROR not in result

    player2 = 'Dvir'
    result = join_game({PLAYER_NAME: player2, ROOM_NAME: room_name})
    assert ERROR not in result

    room = get_room(room_name)
    assert build_player(player) in room.team_1_players
    assert build_player(player2) in room.team_2_players
コード例 #3
0
def unready_phrases(room_name, player_name) -> Dict[str, Any]:
    """Remove phrases for a player and mark them as unready.

    Args:
        room_name: The room that this update will impact.
        player_name: The player submitting the phrases.
    """

    # get player from name
    room = get_room(room_name)

    player_found = False
    for player in room.all_players():
        if player_name == player.name:
            player_found = True
    if not player_found:
        raise ValueError(f'Player {player_name} not in room {room_name}.')

    # check phrases

    # update player as ready, with phrases
    player_with_phrases = build_player(player_name, False, [])
    room.update_player(player_name, player_with_phrases)

    return {}
コード例 #4
0
ファイル: game_rooms.py プロジェクト: mick-warehime/hatgame
def initialize_game_room(room_name: str,
                         first_player: str,
                         icons: Tuple[str, str] = None) -> None:
    """Initialize a game room with the desired properties.

    The game's name must not already exist in the database.
    The game is populated by the first player, with team names set as default
    values.

    Args:
        first_player: Name of the first player.
        room_name: Name assigned to the game.
        icons: Icons desired for the two teams. If not specified they are chosen
            randomly.
    """
    assert room_name not in _room_dict, (
        f'Tried to add ({room_name}) but a room with that name already '
        f'exists.')
    if icons is None:
        icons = random.sample(ICONS, 2)

    player = build_player(first_player)
    _room_dict[room_name] = Room(room_name, [player],
                                 0,
                                 icons[0], [],
                                 0,
                                 icons[1],
                                 GameModes.LOBBY,
                                 0,
                                 player,
                                 last_clue_giver=None)
コード例 #5
0
def join_game(join_request: Dict[str, str]) -> Dict[str, Any]:
    """Receive and respond to a join game request from a client.

    If the game room exists and the player name is not already in use, then
    the player is added to the game room. This is done by updating the game
    state in that room. The player is added to the team with fewest players.

    If the game room does not exist an error message is returned.
    If the player name is already taken an error message is returned.
    If a field is missing or blank in the request an error message is returned.

    Args:
        join_request: Dictionary containing a 'player name' and 'room name'
            fields.

    Returns:
        On success, an empty dictionary is returned and the updated game
        state is broadcast to all clients in the room.
        Otherwise returns a dictionary with an 'error' field.
    """

    error = validate_fields(join_request,
                            (fields.ROOM_NAME, fields.PLAYER_NAME),
                            (fields.ROOM_NAME, fields.PLAYER_NAME))
    if error:
        return {fields.ERROR: error}

    room_name = join_request[fields.ROOM_NAME]
    if not game_room_exists(room_name):
        return {fields.ERROR: f'Room {room_name} does not exist.'}

    # Get current teams and check player name not already in use
    room = get_room(room_name)

    player_name = join_request[fields.PLAYER_NAME]
    if any(plyr.name == player_name for plyr in room.all_players()):
        return {fields.ERROR: f'Player \'{player_name}\' already in game.'}

    # Add player to team with fewest members.
    if len(room.team_1_players) > len(room.team_2_players):
        room.team_2_players.append(build_player(player_name))
    else:
        room.team_1_players.append(build_player(player_name))
    update_room(room_name, room)
    return {}
コード例 #6
0
ファイル: test_game.py プロジェクト: mick-warehime/hatgame
def create_test_game() -> None:
    """Add a test game to the data model.

    The game has room name 'test' and is populated with random characters.
    If the game already exists then this function does nothing.
    """

    room_name = fields.TEST_GAME
    if game_room_exists(room_name):
        return

    # initialize room
    initialize_game_room(room_name, 'Mick')

    # populate room with players
    room = get_room(room_name)
    team_1 = [build_player(p) for p in ['Mick', 'Liz', 'M\'Lickz']]
    team_2 = [build_player(p) for p in ['Dvir', 'Celeste', 'Boaz', 'Ronen']]
    room = evolve(room, **dict(team_1_players=team_1, team_2_players=team_2))
    update_room(room_name, room)
コード例 #7
0
def submit_phrases(room_name, player_name,
                   submit_request: Dict[str, Any]) -> Dict[str, Any]:
    """Record phrases for a player and mark them as ready.

    If the submit request has a valid room, player name, and phrases, update
    the room with those phrases and mark the player as ready. Empty phrases
    are not added.

    Otherwise, an error is returned if a phrase is repeated (or has already been
    submitted).

    Args:
        room_name: The room that this update will impact.
        player_name: The player submitting the phrases.
        submit_request: Message with fields PLAYER_NAME and PHRASES.
             The PHRASES field should store a sequence of string phrases.

    """

    # Validate message structure
    error = validate_fields(submit_request, (PHRASES, ), (PHRASES, ))
    if error:
        raise ValueError(error)

    # get player from name
    room = get_room(room_name)

    player_found = False
    for player in room.all_players():
        if player_name == player.name:
            player_found = True
    if not player_found:
        raise ValueError(f'Player {player_name} not in room {room_name}.')

    # check phrases
    phrases = submit_request[PHRASES]

    # update player as ready, with phrases
    player_with_phrases = build_player(player_name, True, phrases)
    room.update_player(player_name, player_with_phrases)