Skip to content
This repository was archived by the owner on Jan 4, 2025. It is now read-only.

Commit 181cb1b

Browse files
committed
ADD: Choose how many questions each team gets
1 parent b719074 commit 181cb1b

File tree

5 files changed

+27
-13
lines changed

5 files changed

+27
-13
lines changed

pyteamquiz/game.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from .errors import *
55

66
class Game():
7-
def __init__(self, catalgogues:Set[QuestionCatalogueFile], teams:Set[str]):
7+
def __init__(self, catalgogues:Set[QuestionCatalogueFile], teams:Set[str], questions_per_team:int):
88
self.questions = list()
99
for catalogue in catalgogues:
1010
for q in catalogue.get_question_list():
@@ -25,11 +25,11 @@ def __init__(self, catalgogues:Set[QuestionCatalogueFile], teams:Set[str]):
2525
if not 1 <= len(team) <= 30:
2626
raise TeamNameError("Team name must be between 1 and 30 chars.")
2727

28-
# Ensure each player gets the same number of questions
29-
if len(self.questions) < len(self.teams):
30-
raise NotEnoughQuestionsError("You have fewer questions than teams. Select more catalogues.")
31-
pick_questions = len(self.questions) - len(self.questions) % len(self.teams)
32-
self.questions = self.questions[:pick_questions]
28+
# Ensure each team gets the desired number of questions
29+
questions_per_team = max(1, questions_per_team)
30+
if len(self.questions) < len(self.teams) * questions_per_team:
31+
raise NotEnoughQuestionsError("You have not enough questions in the selected catalogues. Select more catalogues.")
32+
self.questions = self.questions[:(len(self.teams) * questions_per_team)]
3333

3434
# Init
3535
self.round = 0

pyteamquiz/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ def start():
1313
def new_game():
1414
selected = set(filter(len, request.form.getlist('catalogues[]')))
1515
teams = set(filter(len, request.form.getlist('teams[]')))
16+
questions_per_team = int(request.form.get('q_per_team'))
1617
try:
17-
game_id = service.new_game(selected, teams)
18+
game_id = service.new_game(selected, teams, questions_per_team)
1819
except (NotEnoughQuestionsError, NotEnoughTeamsError, TeamNameError) as e:
1920
return render_template('error.html', msg=str(e))
2021
return render_template('created.html', game_id=game_id)

pyteamquiz/service.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ def get_available_catalogues() -> List[str]:
88
ret.sort()
99
return ret
1010

11-
def new_game(catalogue_names: Set[str], team_names: Set[str]) -> str:
11+
def new_game(catalogue_names: Set[str], team_names: Set[str], questions_per_team: int) -> str:
1212
token = secrets.token_hex(16)
1313
selected_catalogues = [catalogues[x] for x in catalogue_names]
14-
games[token] = Game(selected_catalogues, team_names)
14+
games[token] = Game(selected_catalogues, team_names, questions_per_team)
1515
return token
1616

1717
def whose_turn(game_id: str) -> str:

pyteamquiz/static/style.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ input[type=text]{
3434
border-radius: 6px;
3535
}
3636

37+
input[type=number]{
38+
padding: 7px 10px;
39+
margin: 5px 2px;
40+
display: inline-block;
41+
border: 1px solid #868686;
42+
border-radius: 6px;
43+
}
44+
3745
/*
3846
Title
3947
*/

pyteamquiz/templates/start.html

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@ <h1>Welcome to <i>pyTeamQuiz</i></h1>
1212
<input type="text" name="teams[]" placeholder="*Team 2" required><br>
1313
<input type="text" name="teams[]" placeholder="Team 3"><br>
1414
<input type="text" name="teams[]" placeholder="Team 4"><br>
15-
<input type="text" name="teams[]" placeholder="Team 5"><br>
16-
<br>
15+
<input type="text" name="teams[]" placeholder="Team 5">
16+
<br><br><br>
17+
18+
How many questions do you want per team?
19+
<br><br>
20+
<input type="number" min="1" max="15" value="5" name="q_per_team">
21+
<br><br><br>
22+
1723
Choose at least one question catalogue:
18-
<br>
19-
<br>
24+
<br><br>
2025
{% for x in catalogue_names %}
2126
<nobr><input type="checkbox" name="catalogues[]" value="{{ x }}"> {{ x }} &nbsp;&nbsp;&nbsp;</nobr>
2227
{% endfor %}

0 commit comments

Comments
 (0)