summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app.py5
-rw-r--r--model.py30
2 files changed, 23 insertions, 12 deletions
diff --git a/app.py b/app.py
index 7778b3e..16659ec 100644
--- a/app.py
+++ b/app.py
@@ -36,9 +36,10 @@ def home():
@app.route("/new-games", methods=["POST"])
def get_new_games():
+ GAME_GENERATOR.create_games(PLAYER_LIST.get_active_players())
return render_template(
"games.j2",
- games=GAME_GENERATOR.create_games(PLAYER_LIST.get_active_players()),
+ games=GAME_GENERATOR.games,
)
@@ -46,7 +47,7 @@ def get_new_games():
def get_list_of_games():
return render_template(
"games.j2",
- games=GAME_GENERATOR.create_games(PLAYER_LIST.get_active_players()),
+ games=GAME_GENERATOR.games,
)
diff --git a/model.py b/model.py
index ef6e762..4a25400 100644
--- a/model.py
+++ b/model.py
@@ -1,6 +1,7 @@
from math import floor
from csv import reader
from pathlib import Path
+from random import sample
from dataclasses import dataclass
@@ -67,24 +68,33 @@ class PlayerList:
return display_string
+TOTAL_PLAYERS_PER_COURT = 4
+DUMMY_PLAYER = Player(name="-", skill=0, status=True)
+DUMMY_TEAM = Team(player1=DUMMY_PLAYER, player2=DUMMY_PLAYER)
+DUMMY_GAME = Game(DUMMY_TEAM, DUMMY_TEAM)
+
+
@dataclass
class GameList:
courts: int = 8
+ def __post_init__(self):
+ self.games = [DUMMY_GAME for _ in range(self.courts)]
+
def create_games(self, players: list[Player]):
- possible_games = floor(len(players) / 4)
+ possible_games = floor(len(players) / TOTAL_PLAYERS_PER_COURT)
+ chosen_players = sample(players, possible_games * TOTAL_PLAYERS_PER_COURT)
+ clumped_players = [
+ chosen_players[i : i + TOTAL_PLAYERS_PER_COURT]
+ for i in range(0, len(chosen_players), TOTAL_PLAYERS_PER_COURT)
+ ]
games = []
- for i in range(possible_games):
- team1 = Team(player1=players[4 * i + 0], player2=players[4 * i + 1])
- team2 = Team(player1=players[4 * i + 2], player2=players[4 * i + 3])
+ for clump in clumped_players:
+ team1 = Team(player1=clump[0], player2=clump[1])
+ team2 = Team(player1=clump[2], player2=clump[3])
game = Game(team1, team2)
games.append(game)
empty_courts = (
self.courts - possible_games if self.courts > possible_games else 0
)
- for i in range(empty_courts):
- dummy_player = Player(name="-", skill=0, status=True)
- dummy_team = Team(player1=dummy_player, player2=dummy_player)
- game = Game(dummy_team, dummy_team)
- games.append(game)
- return games
+ self.games = games + [DUMMY_GAME for _ in range(empty_courts)]