def test_create_teams_5(entry_and_teams): entry = entry_and_teams["entry"] entry.members.append(models.Member(0, 1)) entry.members.append(models.Member(0, 2)) entry.members.append(models.Member(0, 3)) entry.members.append(models.Member(0, 4)) # Should create 2 teams with 5 in each (1+4, 2+3) teams = teamcreation.create_teams(entry) assert len(teams) == 2 assert teams[0].get_num_players() == 5 assert teams[1].get_num_players() == 5 entry_and_teams["teams"] = teams
def test_create_teams_4(entry_and_teams): entry = entry_and_teams["entry"] entry.max_players = 4 entry.members.append(models.Member(0, 1)) entry.members.append(models.Member(0, 1)) entry.members.append(models.Member(0, 2)) entry.members.append(models.Member(0, 2)) entry.members.append(models.Member(0, 3)) # Should create 3 teams with 3 in each (1+2, 1+2, 3) teams = teamcreation.create_teams(entry) assert len(teams) == 3 for team in teams: assert team.get_num_players() == 3 entry_and_teams["teams"] = teams
async def get_member(self, entry_id: int, member_id: int, db=None) -> models.Member: cursor = await db.execute( "SELECT member_id, num_players FROM members WHERE entry_id=? AND member_id=?", (entry_id, member_id)) row = await cursor.fetchone() if row is None: return None return models.Member(*row)
async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent): if payload.member.bot: return # Check that reaction is either number or cancel emoji emoji: discord.PartialEmoji = payload.emoji if emoji.name not in utils.number_emojis and emoji.name != utils.cancel_emoji: return # Make sure the message reacted to is a Teamo message (exists in db) message_id = payload.message_id db_entry = await self.db.get_entry(message_id) if (db_entry is None): return await self.startup_done.wait() # If cancel emoji: Start cancel procedure if str(emoji) == utils.cancel_emoji: logging.info(f"Received cancel emoji on {message_id}") cancel_task = asyncio.create_task(self.cancel_after(message_id)) self.cancel_tasks[message_id] = cancel_task await self.update_message(message_id) await self.sync_message(message_id) return # If number emoji: Add or edit member, remove old reactions, update message async with self.locks[message_id]: logging.info( f"Received number emoji {str(emoji)} on {message_id} from user {payload.member.id} ({payload.member.display_name})" ) num_players = utils.number_emojis.index(emoji.name) + 1 member = models.Member(payload.member.id, num_players) previous_num_players = await self.db.edit_or_insert_member( message_id, member) # Do not have to remove any reactions if the user wasn't registered before # or if the previous entry was the same as the current one (somehow) if previous_num_players is None or previous_num_players == num_players: await self.sync_message(message_id) await self.update_message(message_id) return # Update message await self.update_message(message_id) # Delete old reactions message = self.cached_messages[message_id] previous_emoji = utils.number_emojis[previous_num_players - 1] old_reaction = next( (r for r in message.reactions if r.emoji == previous_emoji), None) await old_reaction.remove(payload.member) await self.sync_message(message_id)
def test_create_teams_3(entry_and_teams): entry = entry_and_teams["entry"] entry.max_players = 3 entry.members.append(models.Member(0, 1)) entry.members.append(models.Member(0, 2)) entry.members.append(models.Member(0, 1)) entry.members.append(models.Member(0, 1)) entry.members.append(models.Member(0, 2)) entry.members.append(models.Member(0, 1)) entry.members.append(models.Member(0, 2)) # Should create 4 teams: 1+2, 1+2, 1+1, 2 teams = teamcreation.create_teams(entry) assert len(teams) == 4 teams_2 = list(filter(lambda t: t.get_num_players() == 2, teams)) teams_3 = list(filter(lambda t: t.get_num_players() == 2, teams)) assert len(teams_2) == 2 assert len(teams_3) == 2 entry_and_teams["teams"] = teams
async def get_entry(self, message_id: int, db=None) -> models.Entry: # Get the time zone info server_id = await self.get_entry_server_id(message_id, db=db) settings = await self.get_settings(server_id, db=db) tzinfo = settings.get_tzinfo() # Get the entry from db and create the Entry object entry_cursor = await db.execute( "SELECT * FROM entries WHERE entry_id=?", (message_id, )) entry_row = await entry_cursor.fetchone() if entry_row is None: return None entry = models.Entry.create_with_tz(*entry_row, tzinfo=tzinfo) # Add members row_cursor = await db.execute( "SELECT member_id, num_players FROM members WHERE entry_id=?", (message_id, )) member_rows = await row_cursor.fetchall() for row in member_rows: member = models.Member(*row) entry.members.append(member) return entry