Skip to content
This repository has been archived by the owner on Feb 2, 2022. It is now read-only.

Make team_size configurable in config.json #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config.json.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"token": "longtesttokenfromdiscordapi"
"token": "longtesttokenfromdiscordapi",
"team_size": 5
}
6 changes: 3 additions & 3 deletions gather/gatherbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@


class GatherBot(ListenerBot):
def __init__(self):
def __init__(self, team_size):
super().__init__()

self.organiser = Organiser()
self.organiser = Organiser(team_size)
self.client = discord.Client()

@self.client.event
Expand Down Expand Up @@ -44,7 +44,7 @@ async def announce_players(self, channel):
channel,
'Currently signed in players ({0}/{1}): {2}.'.format(
len(self.organiser.queues[channel]),
self.organiser.TEAM_SIZE * 2,
self.organiser.team_size * 2,
', '.join([str(p) for p in self.organiser.queues[channel]])
)
)
2 changes: 1 addition & 1 deletion gather/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def main():
with open('config.json') as f:
config = json.load(f)

bot = GatherBot()
bot = GatherBot(config.get('team_size', 5))
bot.register_action('^!help$', commands.bot_help)
bot.register_action('^!(?:add|s)$', commands.add)
bot.register_action('^!(?:remove|so)$', commands.remove)
Expand Down
15 changes: 7 additions & 8 deletions gather/organiser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ class NotEnoughPlayersError(Exception):


class Organiser:
TEAM_SIZE = 5

def __init__(self):
def __init__(self, team_size):
self.team_size = team_size
self.queues = defaultdict(lambda: set())

def add(self, queue, player):
Expand All @@ -19,17 +18,17 @@ def remove(self, queue, player):
self.queues[queue].remove(player)

def ready(self, queue):
return len(self.queues[queue]) >= Organiser.TEAM_SIZE * 2
return len(self.queues[queue]) >= self.team_size * 2

def pop_teams(self, queue):
if len(self.queues[queue]) < Organiser.TEAM_SIZE * 2:
if len(self.queues[queue]) < self.team_size * 2:
raise NotEnoughPlayersError('Not enough players!')

candidates = list(self.queues[queue])
random.shuffle(candidates)
players = candidates[:Organiser.TEAM_SIZE * 2]
team_one = players[Organiser.TEAM_SIZE:]
team_two = players[:Organiser.TEAM_SIZE]
players = candidates[:self.team_size * 2]
team_one = players[self.team_size:]
team_two = players[:self.team_size]
for player in players:
self.queues[queue].remove(player)
return team_one, team_two