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
async def post_nick(self, args: typing.Dict): logger.debug(f'post_nick {args}') request = valid_request_from_dict(AddNickRequest, args) if await self._search.load_player_by_nick(request.nick) is not None: raise RuntimeError(f'Player already exists: {request.nick}') p = Player(nick=request.nick, phone=request.phone) p.set_rating(self.ranking.initial_rating()) await self._manage.save_player(p) return p.as_dict()
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
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
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)
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)