예제 #1
0
async def test_rate_game(laddergame: LadderGame, database, game_add_players):
    laddergame.state = GameState.LOBBY
    players = game_add_players(laddergame, 2)
    laddergame.set_player_option(players[0].id, 'Team', 2)
    laddergame.set_player_option(players[1].id, 'Team', 3)
    player_1_old_mean = players[0].ratings[RatingType.LADDER_1V1][0]
    player_2_old_mean = players[1].ratings[RatingType.LADDER_1V1][0]

    await laddergame.launch()
    laddergame.launched_at = time.time() - 60*20
    await laddergame.add_result(0, 0, 'victory', 5)
    await laddergame.add_result(0, 1, 'defeat', -5)
    await laddergame.on_game_end()

    assert laddergame.validity is ValidityState.VALID
    assert players[0].ratings[RatingType.LADDER_1V1][0] > player_1_old_mean
    assert players[1].ratings[RatingType.LADDER_1V1][0] < player_2_old_mean

    async with database.acquire() as conn:
        result = await conn.execute("SELECT mean, deviation, after_mean, after_deviation FROM game_player_stats WHERE gameid = %s", laddergame.id)
        rows = list(await result.fetchall())

    assert rows[0]['mean'] == 1500
    assert rows[0]['deviation'] == 500
    assert rows[0]['after_mean'] > rows[0]['mean']
    assert rows[0]['after_deviation'] < rows[0]['deviation']

    assert rows[1]['mean'] == 1500
    assert rows[1]['deviation'] == 500
    assert rows[1]['after_mean'] < rows[0]['mean']
    assert rows[1]['after_deviation'] < rows[0]['deviation']
예제 #2
0
async def test_persist_rating_victory(laddergame: LadderGame, database,
                                      game_add_players):
    laddergame.state = GameState.LOBBY
    players = game_add_players(laddergame, 2)
    laddergame.set_player_option(players[0].id, 'Team', 2)
    laddergame.set_player_option(players[1].id, 'Team', 3)

    async with database.acquire() as conn:
        result = await conn.execute(
            text("SELECT id, numGames, winGames FROM ladder1v1_rating WHERE id in :ids ORDER BY id"),
            ids=tuple([players[0].id, players[1].id])
        )
        result_before = await result.fetchall()

    await laddergame.launch()
    laddergame.launched_at = time.time() - 60*20
    await laddergame.add_result(0, 0, 'victory', 5)
    await laddergame.add_result(0, 1, 'defeat', -5)
    await laddergame.on_game_end()

    assert laddergame.validity is ValidityState.VALID

    async with database.acquire() as conn:
        result = await conn.execute(
            text("SELECT id, numGames, winGames FROM ladder1v1_rating WHERE id in :ids ORDER BY id"),
            ids=tuple([players[0].id, players[1].id])
        )
        result_after = await result.fetchall()

    assert result_after[0]['numGames'] == result_before[0]['numGames'] + 1
    assert result_after[1]['numGames'] == result_before[1]['numGames'] + 1
    assert result_after[0]['winGames'] == result_before[0]['winGames'] + 1
    assert result_after[1]['winGames'] == result_before[1]['winGames']
예제 #3
0
    def create_game(self,
                    visibility=VisibilityState.PUBLIC,
                    game_mode: str=None,
                    host: Player=None,
                    name: str=None,
                    mapname: str=None,
                    password: str=None):
        """
        Main entrypoint for creating new games
        """
        id = self.createUuid()
        args = {
            "id": id,
            "host": host,
            "name": name,
            "map": mapname,
            "game_mode": game_mode,
            "game_service": self,
            "game_stats_service": self.game_stats_service
        }
        if game_mode == 'ladder1v1':
            game = LadderGame(**args)
        elif game_mode == 'coop':
            game = CoopGame(**args)
        elif game_mode == 'faf' or game_mode == 'fafbeta' or game_mode == 'equilibrium':
            game = CustomGame(**args)
        else:
            game = Game(**args)
        self.games[id] = game

        game.visibility = visibility
        game.password = password

        self.mark_dirty(game)
        return game
예제 #4
0
async def test_rate_game(laddergame: LadderGame, db_pool):
    async with db.db_pool.get() as conn:
        cursor = await conn.cursor()
        # TODO remove as soon as we have isolated tests (transactions)
        await cursor.execute("DELETE FROM game_stats WHERE id = %s",
                             laddergame.id)
        await cursor.execute("DELETE FROM game_player_stats WHERE gameId = %s",
                             laddergame.id)

    laddergame.state = GameState.LOBBY
    players = add_players(laddergame, 2)
    laddergame.set_player_option(players[0].id, 'Team', 1)
    laddergame.set_player_option(players[1].id, 'Team', 2)
    player_1_old_mean = players[0].ladder_rating[0]
    player_2_old_mean = players[1].ladder_rating[0]

    await laddergame.launch()
    laddergame.launched_at = time.time() - 60 * 20
    await laddergame.add_result(0, 0, 'victory', 5)
    await laddergame.add_result(0, 1, 'defeat', -5)
    await laddergame.on_game_end()

    assert laddergame.validity is ValidityState.VALID
    assert players[0].ladder_rating[0] > player_1_old_mean
    assert players[1].ladder_rating[0] < player_2_old_mean

    async with db.db_pool.get() as conn:
        cursor = await conn.cursor(DictCursor)
        await cursor.execute(
            "SELECT mean, deviation, after_mean, after_deviation FROM game_player_stats WHERE gameid = %s",
            laddergame.id)
        result = await cursor.fetchall()

    assert result[0]['mean'] == 1500
    assert result[0]['deviation'] == 500
    assert result[0]['after_mean'] > result[0]['mean']
    assert result[0]['after_deviation'] < result[0]['deviation']

    assert result[1]['mean'] == 1500
    assert result[1]['deviation'] == 500
    assert result[1]['after_mean'] < result[0]['mean']
    assert result[1]['after_deviation'] < result[0]['deviation']
예제 #5
0
    def create_game(self,
                    visibility=VisibilityState.PUBLIC,
                    game_mode: str=None,
                    host: Player=None,
                    name: str=None,
                    mapname: str=None,
                    password: str=None):
        """
        Main entrypoint for creating new games
        """
        id = self.createUuid()
        args = {
            "id": id,
            "host": host,
            "name": name,
            "map": mapname,
            "game_mode": game_mode,
            "game_service": self,
            "game_stats_service": self.game_stats_service
        }
        if game_mode == 'ladder1v1':
            game = LadderGame(**args)
        elif game_mode == 'coop':
            game = CoopGame(**args)
        elif game_mode == 'faf' or game_mode == 'fafbeta' or game_mode == 'equilibrium':
            game = CustomGame(**args)
        else:
            game = Game(**args)
        self.games[id] = game

        game.visibility = visibility
        game.password = password

        self.mark_dirty(game)
        return game
예제 #6
0
async def test_rate_game(laddergame: LadderGame, database, game_add_players):
    laddergame.state = GameState.LOBBY
    players = game_add_players(laddergame, 2)
    laddergame.set_player_option(players[0].id, "Team", 2)
    laddergame.set_player_option(players[1].id, "Team", 3)
    player_1_old_mean = players[0].ratings[RatingType.LADDER_1V1][0]
    player_2_old_mean = players[1].ratings[RatingType.LADDER_1V1][0]

    # As saved in test-data.sql:
    before_mean = {1: 2000, 2: 1500}
    before_deviation = {1: 125, 2: 75}

    await laddergame.launch()
    laddergame.launched_at = time.time() - 60 * 20
    await laddergame.add_result(0, 0, "victory", 5)
    await laddergame.add_result(0, 1, "defeat", -5)
    await laddergame.on_game_end()

    await laddergame.game_service._rating_service._join_rating_queue()

    assert laddergame.validity is ValidityState.VALID
    assert players[0].ratings[RatingType.LADDER_1V1][0] > player_1_old_mean
    assert players[1].ratings[RatingType.LADDER_1V1][0] < player_2_old_mean

    async with database.acquire() as conn:
        result = await conn.execute(
            "SELECT mean, deviation, after_mean, after_deviation FROM game_player_stats WHERE gameid = %s",
            laddergame.id)
        rows = list(await result.fetchall())

    assert rows[0]["mean"] == before_mean[players[0].id]
    assert rows[0]["deviation"] == before_deviation[players[0].id]
    assert rows[0]["after_mean"] > rows[0]["mean"]
    assert rows[0]["after_deviation"] < rows[0]["deviation"]

    assert rows[1]["mean"] == before_mean[players[1].id]
    assert rows[1]["deviation"] == before_deviation[players[1].id]
    assert rows[1]["after_mean"] < rows[0]["mean"]
    assert rows[1]["after_deviation"] < rows[0]["deviation"]
예제 #7
0
async def test_persist_rating_victory(laddergame: LadderGame, database,
                                      game_add_players):
    laddergame.state = GameState.LOBBY
    players = game_add_players(laddergame, 2)
    laddergame.set_player_option(players[0].id, "Team", 2)
    laddergame.set_player_option(players[1].id, "Team", 3)

    rating_sql = select([
        leaderboard_rating.c.total_games, leaderboard_rating.c.won_games
    ]).where(
        and_(
            leaderboard_rating.c.login_id.in_((players[0].id, players[1].id)),
            leaderboard_rating.c.leaderboard_id == 2,
        )).order_by(leaderboard_rating.c.login_id)

    async with database.acquire() as conn:
        result = await conn.execute(rating_sql)
        result_before = await result.fetchall()

    await laddergame.launch()
    laddergame.launched_at = time.time() - 60 * 20
    await laddergame.add_result(0, 0, "victory", 5)
    await laddergame.add_result(0, 1, "defeat", -5)
    await laddergame.on_game_end()

    await laddergame.game_service._rating_service._join_rating_queue()

    assert laddergame.validity is ValidityState.VALID

    async with database.acquire() as conn:
        result = await conn.execute(rating_sql)
        result_after = await result.fetchall()

    assert result_after[0][
        "total_games"] == result_before[0]["total_games"] + 1
    assert result_after[1][
        "total_games"] == result_before[1]["total_games"] + 1
    assert result_after[0]["won_games"] == result_before[0]["won_games"] + 1
    assert result_after[1]["won_games"] == result_before[1]["won_games"]
예제 #8
0
async def test_rate_game(laddergame: LadderGame, db_pool):
    async with db.db_pool.get() as conn:
        cursor = await conn.cursor()
        # TODO remove as soon as we have isolated tests (transactions)
        await cursor.execute("DELETE FROM game_stats WHERE id = %s", laddergame.id)
        await cursor.execute("DELETE FROM game_player_stats WHERE gameId = %s", laddergame.id)

    laddergame.state = GameState.LOBBY
    players = add_players(laddergame, 2)
    laddergame.set_player_option(players[0].id, 'Team', 1)
    laddergame.set_player_option(players[1].id, 'Team', 2)
    player_1_old_mean = players[0].ladder_rating[0]
    player_2_old_mean = players[1].ladder_rating[0]

    await laddergame.launch()
    laddergame.launched_at = time.time() - 60*20
    await laddergame.add_result(0, 0, 'victory', 5)
    await laddergame.add_result(0, 1, 'defeat', -5)
    await laddergame.on_game_end()

    assert laddergame.validity is ValidityState.VALID
    assert players[0].ladder_rating[0] > player_1_old_mean
    assert players[1].ladder_rating[0] < player_2_old_mean

    async with db.db_pool.get() as conn:
        cursor = await conn.cursor(DictCursor)
        await cursor.execute("SELECT mean, deviation, after_mean, after_deviation FROM game_player_stats WHERE gameid = %s", laddergame.id)
        result = await cursor.fetchall()

    assert result[0]['mean'] == 1500
    assert result[0]['deviation'] == 500
    assert result[0]['after_mean'] > result[0]['mean']
    assert result[0]['after_deviation'] < result[0]['deviation']

    assert result[1]['mean'] == 1500
    assert result[1]['deviation'] == 500
    assert result[1]['after_mean'] < result[0]['mean']
    assert result[1]['after_deviation'] < result[0]['deviation']
예제 #9
0
async def test_persist_rating_victory(laddergame: LadderGame, db_engine):
    async with db_engine.acquire() as conn:
        # TODO remove as soon as we have isolated tests (transactions)
        await conn.execute("DELETE FROM game_player_stats WHERE gameId = %s", laddergame.id)
        await conn.execute("DELETE FROM game_stats WHERE id = %s", laddergame.id)

    laddergame.state = GameState.LOBBY
    players = add_players(laddergame, 2)
    laddergame.set_player_option(players[0].id, 'Team', 1)
    laddergame.set_player_option(players[1].id, 'Team', 2)

    async with db_engine.acquire() as conn:
        result = await conn.execute(
            text("SELECT id, numGames, winGames FROM ladder1v1_rating WHERE id in :ids ORDER BY id"),
            ids=tuple([players[0].id, players[1].id])
        )
        result_before = await result.fetchall()

    await laddergame.launch()
    laddergame.launched_at = time.time() - 60*20
    await laddergame.add_result(0, 0, 'victory', 5)
    await laddergame.add_result(0, 1, 'defeat', -5)
    await laddergame.on_game_end()

    assert laddergame.validity is ValidityState.VALID

    async with db_engine.acquire() as conn:
        result = await conn.execute(
            text("SELECT id, numGames, winGames FROM ladder1v1_rating WHERE id in :ids ORDER BY id"),
            ids=tuple([players[0].id, players[1].id])
        )
        result_after = await result.fetchall()

    assert result_after[0]['numGames'] == result_before[0]['numGames'] + 1
    assert result_after[1]['numGames'] == result_before[1]['numGames'] + 1
    assert result_after[0]['winGames'] == result_before[0]['winGames'] + 1
    assert result_after[1]['winGames'] == result_before[1]['winGames']
예제 #10
0
def laddergame(game_service, game_stats_service):
    return LadderGame(465312, game_service, game_stats_service)
예제 #11
0
def ladder_game(event_loop, database, game_service, game_stats_service):
    return LadderGame(42, database, game_service, game_stats_service)
예제 #12
0
def laddergame(database, game_service, game_stats_service):
    return LadderGame(id_=465312,
                      database=database,
                      game_service=game_service,
                      game_stats_service=game_stats_service)