Beispiel #1
0
    async def load_game_by_id(game_id):
        g = Game(game_id=game_id)
        res = await db.execute(Search.sql_load_game_by_id(game_id))
        if len(res) == 0:
            raise RuntimeError(f'Could not find game game_id({g.id})')

        res = res[0]
        g.date = datetime.datetime.strptime(res[0], '%Y-%m-%d %H:%M:%S')
        g.score_won = res[1]
        g.score_lost = res[2]

        res = await db.execute(Search.sql_load_game_players_by_id(game_id))

        for record in res:
            won = record[1]
            p = await Search.load_player_by_id(player_id=record[0])
            if won:
                g.nicks_won.append(p.nick)
            else:
                g.nicks_lost.append(p.nick)

            res = await db.execute(
                Search.sql_rating_change(g.id, p.id, ratingSystem))
            if len(res) == 0:
                raise RuntimeError(
                    f'Could not find rating change for game_id({g.id}), player_id({p.id})'
                )

            g.set_rating_before(p.nick,
                                Rating(value=res[0][0], accuracy=res[0][2]))
            g.set_rating_after(p.nick,
                               Rating(value=res[0][1], accuracy=res[0][3]))

        logger.debug(f'load_game_by_id {g}')
        return g
Beispiel #2
0
async def test_all():
    g = None
    team_won = []
    team_lost = []
    try:
        # create players
        for i in range(0, 4):
            p = Player(nick=f'NewPlayer{i}')
            p.set_rating(Rating(value=1200, accuracy=0))
            await Manage.delete_player(p)
            await Manage.save_player(p)
            if i < 2:
                team_won.append(p)
            else:
                team_lost.append(p)

        g = Game(date=datetime.now(),
                 nicks_won=[p.nick for p in team_won],
                 nicks_lost=[p.nick for p in team_lost],
                 score_won=23,
                 score_lost=21)
        for p in team_won:
            g.set_rating_before(p.nick, p.get_rating())
            p.set_rating(Rating(value=1300, accuracy=0))
            g.set_rating_after(p.nick, p.get_rating())

        for p in team_lost:
            g.set_rating_before(p.nick, p.get_rating())
            p.set_rating(Rating(value=1100, accuracy=0))
            g.set_rating_after(p.nick, p.get_rating())

        await Manage.save_game(g)

        test_g = await Search.load_game_by_id(g.id)

        assert test_g.nicks_won == ['NewPlayer0', 'NewPlayer1']
        assert test_g.nicks_lost == ['NewPlayer2', 'NewPlayer3']

        for nick in test_g.nicks_won:
            assert test_g.rating_before(nick) == Rating(value=1200, accuracy=0)
            assert test_g.rating_after(nick) == Rating(value=1300, accuracy=0)

        for nick in test_g.nicks_lost:
            assert test_g.rating_before(nick) == Rating(value=1200, accuracy=0)
            assert test_g.rating_after(nick) == Rating(value=1100, accuracy=0)

        assert test_g.score_won == 23
        assert test_g.score_lost == 21

    finally:
        # clear
        await Manage.delete_game(g)
        for p in team_won + team_lost:
            await Manage.delete_player(p)
Beispiel #3
0
 def calculate(cls, game: Game):
     env = TrueSkill()
     team_won = {
         nick: cls.from_model_rating(game.rating_before(nick))
         for nick in game.nicks_won
     }
     team_lost = {
         nick: cls.from_model_rating(game.rating_before(nick))
         for nick in game.nicks_lost
     }
     rated_rating_groups = env.rate([team_won, team_lost], ranks=[0, 1])
     for team in rated_rating_groups:
         for nick in team:
             game.set_rating_after(nick, cls.to_model_rating(team[nick]))
Beispiel #4
0
async def test_all():
    # create players
    team_won = []
    team_lost = []
    g = None
    try:
        for i in range(0, 4):
            p = Player(nick=f'NewPlayer{i}', phone=f'7916123456{i}')
            p.set_rating(Rating(value=1200 + i, accuracy=1))
            if i < 2:
                team_won.append(p)
            else:
                team_lost.append(p)

            await Manage.delete_player(p)
            await Manage.save_player(p)

        # find players LIKE
        players = await Search.load_players_nick_like('New')
        assert len(players) == 4

        players = await Search.load_players_nick_like('NewNotExists')
        assert len(players) == 0

        # game
        g = Game(date=datetime.now(), nicks_won=[p.nick for p in team_won], nicks_lost=[p.nick for p in team_lost],
                 score_won=15, score_lost=10)
        for p in team_won:
            g.set_rating_before(p.nick, p.get_rating())
            p.set_rating(Rating(value=p.get_rating().value + 100, accuracy=p.get_rating().accuracy))
            g.set_rating_after(p.nick, p.get_rating())
            await Manage.save_player(p)

        for p in team_lost:
            g.set_rating_before(p.nick, p.get_rating())
            p.set_rating(Rating(value=p.get_rating().value - 100, accuracy=p.get_rating().accuracy))
            g.set_rating_after(p.nick, p.get_rating())
            await Manage.save_player(p)

        await Manage.save_game(g)

        # find game
        res = await Search.games(player=team_lost[0])
        assert res[0].id == g.id

        # find games vs
        res = await Search.games(player=team_won[0], vs_players=[team_lost[0]])
        assert len(res) == 1
        assert res[0].id == g.id

        # not find games vs
        res = await Search.games(player=team_lost[0], vs_players=[team_lost[1]])
        assert len(res) == 0

        # find games with
        res = await Search.games(player=team_lost[1], with_players=[team_lost[0]])
        assert len(res) == 1
        assert res[0].id == g.id

        # not find games with
        res = await Search.games(player=team_lost[0], with_players=[team_won[1]])
        assert len(res) == 0

        # find games vs and with
        res = await Search.games(player=team_lost[0], vs_players=[team_won[0]], with_players=[team_lost[1]])
        assert len(res) == 1
        assert res[0].id == g.id

        # not find games vs
        res = await Search.games(player=team_lost[1], vs_players=[team_won[1]], with_players=[team_won[0]])
        assert len(res) == 0

        res = await Search.games(player=team_lost[0], vs_players=[team_lost[1]], with_players=[team_lost[1]])
        assert len(res) == 0

    finally:
        # clear
        await Manage.delete_game(g)
        for player in team_lost + team_won:
            await Manage.delete_player(player)