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

Commit

Permalink
ADD: Choose how many questions each team gets
Browse files Browse the repository at this point in the history
  • Loading branch information
claussmann committed Feb 20, 2024
1 parent b719074 commit 181cb1b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
12 changes: 6 additions & 6 deletions pyteamquiz/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pyteamquiz/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pyteamquiz/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions pyteamquiz/static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
13 changes: 9 additions & 4 deletions pyteamquiz/templates/start.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ <h1>Welcome to <i>pyTeamQuiz</i></h1>
<input type="text" name="teams[]" placeholder="*Team 2" required><br>
<input type="text" name="teams[]" placeholder="Team 3"><br>
<input type="text" name="teams[]" placeholder="Team 4"><br>
<input type="text" name="teams[]" placeholder="Team 5"><br>
<br>
<input type="text" name="teams[]" placeholder="Team 5">
<br><br><br>

How many questions do you want per team?
<br><br>
<input type="number" min="1" max="15" value="5" name="q_per_team">
<br><br><br>

Choose at least one question catalogue:
<br>
<br>
<br><br>
{% for x in catalogue_names %}
<nobr><input type="checkbox" name="catalogues[]" value="{{ x }}"> {{ x }} &nbsp;&nbsp;&nbsp;</nobr>
{% endfor %}
Expand Down

0 comments on commit 181cb1b

Please sign in to comment.