예제 #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
예제 #2
0
    async def load_top_players(offset, count):
        res = await db.execute(Search.sql_load_top_players(offset, count))
        players = []
        for record in res:
            player_id, nick, phone, value, accuracy = record
            p = Player(player_id=player_id, nick=nick, phone=phone)
            p.set_rating(Rating(value, accuracy))
            players.append(p)

        return players
예제 #3
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)
예제 #4
0
    async def load_player_by_id(player_id):
        res = await db.execute(Search.sql_load_player_by_id(player_id))
        if len(res) == 0:
            logger.debug('load_player_by_nick None')
            return None

        player_id, nick, phone = res[0]
        p = Player(player_id=player_id, nick=nick, phone=phone)

        res = await db.execute(Search.sql_ratings(player_id))
        if len(res) > 0:
            p.set_rating(Rating(value=res[0][1], accuracy=res[0][2]))

        logger.debug(f'load_player_by_id {p}')
        return p
예제 #5
0
    async def load_players_nick_like(nick):
        res = await db.execute(Search.sql_load_players_nick_like(nick))

        players = []
        for record in res:
            player_id, nick, phone = record
            p = Player(player_id=player_id, nick=nick, phone=phone)

            res = await db.execute(Search.sql_ratings(player_id))
            if len(res) > 0:
                p.set_rating(Rating(value=res[0][1], accuracy=res[0][2]))

            players.append(p)

        logger.debug(f'load_players_nick_like {players}')
        return players
예제 #6
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)
예제 #7
0
 def initial_rating():
     rating = TrueSkill().create_rating()
     return Rating(value=rating.mu, accuracy=rating.sigma)
예제 #8
0
 def to_model_rating(rating: trueskill.Rating) -> trueskill.Rating:
     return Rating(value=rating.mu, accuracy=rating.sigma)