def test_round_end(self): evmap = EventMap.new() evmap.register( EqHandler(tag=2, key="round", expect=Round(round_id=69), kind=EventKind.ROUND_END)) qe = RoundEndEvent(self.igctx, Round(round_id=69)) ready = evmap.poll(qe) handler = next(ready) assert handler is not None assert handler.tag == 2 assert next(ready, None) is None
def setUpClass(cls): cls.db = Database("tests/full_mockdb.sqlite3") cls.mm = MatchMaker(Config(), cls.db, Round(round_id=1)) cls.p1 = Player(discord_id=1, name="Player_1") cls.p2 = Player(discord_id=2, name="Player_2") cls.p3 = Player(discord_id=3, name="Player_3") cls.p4 = Player(discord_id=4, name="Player_4") cls.t1 = Team(team_id=1, name="Team_1_2", player_one=cls.p1, player_two=cls.p2) cls.t2 = Team(team_id=2, name="Team_3_4", player_one=cls.p3, player_two=cls.p4) cls.t3 = Team(team_id=3, name="Team_1_3", player_one=cls.p1, player_two=cls.p3) cls.t4 = Team(team_id=4, name="Team_2_4", player_one=cls.p2, player_two=cls.p4)
def setUpClass(cls): cls.round = Round(round_id=1) cls.principal = get_principal(cls.round, Config()) cls.p1 = Player(discord_id=1, name="Player_1") cls.p2 = Player(discord_id=2, name="Player_2") cls.p3 = Player(discord_id=3, name="Player_3") cls.p4 = Player(discord_id=4, name="Player_4") cls.t1 = Team(team_id=1, name="Team_1_2", player_one=cls.p1, player_two=cls.p2, elo=1000) cls.t2 = Team(team_id=2, name="Team_3_4", player_one=cls.p3, player_two=cls.p4, elo=1000) cls.r1 = Result(result_id=1, team=cls.t1, points=3.5, delta=0) cls.r2 = Result(result_id=2, team=cls.t2, points=3.5, delta=0) cls.m1 = Match(match_id=1, round=cls.round, team_one=cls.r1, team_two=cls.r2)
def setUpClass(cls): cls.round = Round(round_id=1) cls.ctx = QueueContext(cls.round) cls.p1 = Player(discord_id=1, name="Player_1") cls.p2 = Player(discord_id=2, name="Player_2") cls.p3 = Player(discord_id=3, name="Player_3") cls.p4 = Player(discord_id=4, name="Player_4") cls.t1 = Team(team_id=1, name="Team_1_2", player_one=cls.p1, player_two=cls.p2) cls.t2 = Team(team_id=2, name="Team_3_4", player_one=cls.p3, player_two=cls.p4) cls.t3 = Team(team_id=3, name="Team_1_3", player_one=cls.p1, player_two=cls.p3) cls.r = Result(result_id=1, team=cls.t1, points=0) cls.m1 = Match(match_id=1, round=cls.round, team_one=cls.r, team_two=cls.r) cls.m2 = Match(match_id=2, round=cls.round, team_one=cls.r, team_two=cls.r)
def test_end_trigger(self): evmap = EventMap.new() evmap.register(GameEndHandler(self.round, self.games, evmap)) evmap.register( EqHandler( tag=1, key="round", expect=Round(round_id=self.round.round_id), kind=EventKind.ROUND_END, persistent=False, )) m = Match( match_id=1, round=self.round, team_one=Result(result_id=1, team=self.t1, points=3, delta=-1), team_two=Result(result_id=2, team=self.t2, points=7, delta=1), ) ctx = self.games.add_result(m) assert not isinstance(ctx, Error) assert ctx == 1 assert self.games[ctx].is_complete() e = ResultEvent(self.games[1], m) assert not isinstance(evmap.handle(e), HandlingError) assert len(self.games) == 0 assert len(evmap[EventKind.ROUND_END]) == 0
def generate(db): players = [] for i in range(1, PLAYERS + 1): player = Player(i, f"Player_{i}") assert db.insert(player) players.append(player) print(f"-- Generated {len(players)} players") teams = [] for i, team in enumerate(combinations(players, 2), 1): one, two = team team = Team( team_id=i, name=f"Team_{one.discord_id}_{two.discord_id}", player_one=one, player_two=two, ) teams.append(team) assert db.insert(team) print(f"-- Generated {len(teams)} teams") assert len(teams) == no_teams() prev_round = None matches = 0 prev_time = datetime.now() for i, team in enumerate(combinations(teams, 2), 1): if (i - 1) % ROUND_EVERY == 0: start = prev_time prev_time += timedelta(minutes=15) prev_round = Round( round_id=i // ROUND_EVERY + 1, start_time=start, end_time=prev_time, participants=4, ) assert db.insert(prev_round) res1 = Result(result_id=i, team=team[0], points=7, delta=1.0) res2 = Result(result_id=i + 1, team=team[1], points=6, delta=-1.0) assert db.insert(res1) assert db.insert(res2) assert db.insert( Match(match_id=i, round=prev_round, team_one=res1, team_two=res2, odds_ratio=1)) matches = i assert matches == no_matches() print(f"-- Generated {matches} matches") print(f"-- Generated {matches//ROUND_EVERY} rounds")
def test_queue_trigger(self): self.games.clear() self.qctx.clear() t1 = Team( team_id=42, elo=1000, player_one=Player(discord_id=1), player_two=Player(discord_id=2), ) t2 = Team( team_id=69, elo=1000, player_one=Player(discord_id=3), player_two=Player(discord_id=4), ) evmap = EventMap.new() evmap.register(MatchTriggerHandler(self.config, self.games, evmap)) evmap.register( EqHandler( tag=1, key="round", expect=Round(round_id=self.qctx.round.round_id), kind=EventKind.ROUND_START, persistent=False, )) prev_round = self.qctx.round.round_id self.qctx.queue.append(t1) q1 = QueueEvent(self.qctx, t1) assert not isinstance(evmap.handle(q1), HandlingError) self.qctx.queue.append(t2) q2 = QueueEvent(self.qctx, t2) assert not isinstance(evmap.handle(q2), HandlingError) assert evmap[EventKind.RESULT][0].tag == prev_round assert self.qctx.round.round_id == prev_round + 1 assert self.qctx.is_empty() assert len(evmap[EventKind.QUEUE]) == 1 assert len(evmap[EventKind.ROUND_START]) == 0
def __init__(self, config: BotConfig, mmcfg: Config, db: Database): super().__init__(command_prefix=config.command_prefix) self.logger = logging.getLogger(__name__) self.help_command = Help() self.db = db self.config = config query = ColumnQuery(QueryKind.SELECT, "turn", Max("round_id"), []) execq = self.db.execute(query, "QueryInitialRound") assert execq is not None round_id = execq.fetchone()[0] round_id = 0 if round_id is None else round_id self.mm = MatchMaker(mmcfg, Round(round_id=round_id + 1)) self.__register_handlers() for cog in COGS: self.add_cog(cog())
def setUpClass(cls): cls.round = Round(round_id=1) cls.t1 = Team( team_id=42, elo=1000, player_one=Player(discord_id=1), player_two=Player(discord_id=2), ) cls.t2 = Team( team_id=69, elo=1000, player_one=Player(discord_id=3), player_two=Player(discord_id=4), ) m = Match( match_id=1, round=cls.round, team_one=Result(result_id=1, team=cls.t1), team_two=Result(result_id=2, team=cls.t2), ) cls.principal = get_principal(cls.round, Config()) cls.games = Games( {cls.round.round_id: InGameContext(cls.principal, [m])})
def setUpClass(cls): cls.qctx = QueueContext(Round(round_id=1))
def setUpClass(cls): principal = get_principal(Round(round_id=1), Config()) cls.igctx = InGameContext(principal, None)
def test_load_round(self): for i in range(1, no_rounds() + 1): round = self.db.load(Round(round_id=i)) assert round is not None assert round.round_id == i
def test_exists_round(self): for i in range(1, no_rounds() + 1): assert self.db.exists(Round(round_id=i))
def setUpClass(cls): cls.db = Database("tests/full_mockdb.sqlite3") cls.r1 = Round(round_id=1) cls.r2 = Round(round_id=1) cls.r3 = Round(round_id=2)
def setUpClass(cls): cls.qctx = QueueContext(Round(round_id=1)) cls.games = Games.new() cls.config = Config(trigger_threshold=2)