diff --git a/pyteamquiz/game.py b/pyteamquiz/game.py index 770f094..88f0126 100644 --- a/pyteamquiz/game.py +++ b/pyteamquiz/game.py @@ -4,7 +4,7 @@ from .errors import * class Game(): - def __init__(self, catalgogues:Set[QuestionCatalogueFile], teams:Set[str]): + def __init__(self, catalgogues:Set[QuestionCatalogueFile], teams:Set[str], questions_per_team:int): self.questions = list() for catalogue in catalgogues: for q in catalogue.get_question_list(): @@ -25,11 +25,11 @@ def __init__(self, catalgogues:Set[QuestionCatalogueFile], teams:Set[str]): if not 1 <= len(team) <= 30: raise TeamNameError("Team name must be between 1 and 30 chars.") - # Ensure each player gets the same number of questions - if len(self.questions) < len(self.teams): - raise NotEnoughQuestionsError("You have fewer questions than teams. Select more catalogues.") - pick_questions = len(self.questions) - len(self.questions) % len(self.teams) - self.questions = self.questions[:pick_questions] + # Ensure each team gets the desired number of questions + questions_per_team = max(1, questions_per_team) + if len(self.questions) < len(self.teams) * questions_per_team: + raise NotEnoughQuestionsError("You have not enough questions in the selected catalogues. Select more catalogues.") + self.questions = self.questions[:(len(self.teams) * questions_per_team)] # Init self.round = 0 diff --git a/pyteamquiz/main.py b/pyteamquiz/main.py index 6a414f5..0fae648 100644 --- a/pyteamquiz/main.py +++ b/pyteamquiz/main.py @@ -13,8 +13,9 @@ def start(): def new_game(): selected = set(filter(len, request.form.getlist('catalogues[]'))) teams = set(filter(len, request.form.getlist('teams[]'))) + questions_per_team = int(request.form.get('q_per_team')) try: - game_id = service.new_game(selected, teams) + game_id = service.new_game(selected, teams, questions_per_team) except (NotEnoughQuestionsError, NotEnoughTeamsError, TeamNameError) as e: return render_template('error.html', msg=str(e)) return render_template('created.html', game_id=game_id) diff --git a/pyteamquiz/service.py b/pyteamquiz/service.py index 282b74a..5e8e1b9 100644 --- a/pyteamquiz/service.py +++ b/pyteamquiz/service.py @@ -8,10 +8,10 @@ def get_available_catalogues() -> List[str]: ret.sort() return ret -def new_game(catalogue_names: Set[str], team_names: Set[str]) -> str: +def new_game(catalogue_names: Set[str], team_names: Set[str], questions_per_team: int) -> str: token = secrets.token_hex(16) selected_catalogues = [catalogues[x] for x in catalogue_names] - games[token] = Game(selected_catalogues, team_names) + games[token] = Game(selected_catalogues, team_names, questions_per_team) return token def whose_turn(game_id: str) -> str: diff --git a/pyteamquiz/static/style.css b/pyteamquiz/static/style.css index f9b79c5..fabbd41 100644 --- a/pyteamquiz/static/style.css +++ b/pyteamquiz/static/style.css @@ -34,6 +34,14 @@ input[type=text]{ border-radius: 6px; } +input[type=number]{ + padding: 7px 10px; + margin: 5px 2px; + display: inline-block; + border: 1px solid #868686; + border-radius: 6px; +} + /* Title */ diff --git a/pyteamquiz/templates/start.html b/pyteamquiz/templates/start.html index 6833897..09e9d71 100644 --- a/pyteamquiz/templates/start.html +++ b/pyteamquiz/templates/start.html @@ -12,11 +12,16 @@

Welcome to pyTeamQuiz




-
-
+ +


+ + How many questions do you want per team? +

+ +


+ Choose at least one question catalogue: -
-
+

{% for x in catalogue_names %} {{ x }}     {% endfor %}