From f80463a6437533deef1d85ffa31cf0cc5ed8e454 Mon Sep 17 00:00:00 2001 From: Karan Jayachandra Date: Fri, 11 Oct 2024 19:23:01 +0200 Subject: Added skilled and random games --- app.py | 16 +++++++++++++--- model.py | 30 +++++++++++++++++++++++------- templates/index.j2 | 7 ++++++- 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/app.py b/app.py index fac8a29..c4e4d89 100644 --- a/app.py +++ b/app.py @@ -22,10 +22,20 @@ def home(): return render_template("index.j2") -@app.route("/new-games", methods=["POST"]) -def get_new_games(): +@app.route("/random-games", methods=["POST"]) +def get_random_games(): print("Creating new games...") - GAME_GENERATOR.create_games(COURT_LIST, PLAYER_LIST) + GAME_GENERATOR.create_random_games(COURT_LIST, PLAYER_LIST) + return render_template( + "games.j2", + games=GAME_GENERATOR.games, + ) + + +@app.route("/skilled-games", methods=["POST"]) +def get_skilled_games(): + print("Creating new games...") + GAME_GENERATOR.create_skilled_games(COURT_LIST, PLAYER_LIST) return render_template( "games.j2", games=GAME_GENERATOR.games, diff --git a/model.py b/model.py index 9af7198..a623d0a 100644 --- a/model.py +++ b/model.py @@ -122,16 +122,15 @@ PLAYER_PER_COURT = 4 def create_single_game(court: Court, players: list[Player]): - team1 = Team(player1=players[0], player2=players[1]) - team2 = Team(player1=players[2], player2=players[3]) + team1 = Team(player1=players[0], player2=players[3]) + team2 = Team(player1=players[1], player2=players[2]) return Game(court, team1, team2) def create_possible_games(court_list: list[Court], players: list[Player]): - current_players = sample(players, len(court_list) * PLAYER_PER_COURT) clumped_players = [ - current_players[i : i + PLAYER_PER_COURT] - for i in range(0, len(current_players), PLAYER_PER_COURT) + players[i : i + PLAYER_PER_COURT] + for i in range(0, len(players), PLAYER_PER_COURT) ] games = [ create_single_game(court, clump) @@ -153,13 +152,30 @@ class GameList: games.sort(key=attrgetter("court.number")) self.games = games - def create_games(self, court_list: CourtList, player_list: PlayerList): + def create_random_games(self, court_list: CourtList, player_list: PlayerList): active_players = player_list.get_active_players() active_courts = [court for court in court_list.courts if court.status] inactive_courts = [court for court in court_list.courts if not court.status] possible_games = floor(len(active_players) / PLAYER_PER_COURT) + current_players = sample(active_players, possible_games * PLAYER_PER_COURT) active_games = create_possible_games( - active_courts[:possible_games], active_players + active_courts[:possible_games], current_players + ) + dummy_games = create_dummy_games(active_courts[possible_games:]) + reserved_games = create_reserved_games(inactive_courts) + games = active_games + dummy_games + reserved_games + games.sort(key=attrgetter("court.number")) + self.games = games + + def create_skilled_games(self, court_list: CourtList, player_list: PlayerList): + active_players = player_list.get_active_players() + active_courts = [court for court in court_list.courts if court.status] + inactive_courts = [court for court in court_list.courts if not court.status] + possible_games = floor(len(active_players) / PLAYER_PER_COURT) + current_players = sample(active_players, possible_games * PLAYER_PER_COURT) + current_players.sort(key=attrgetter("skill")) + active_games = create_possible_games( + active_courts[:possible_games], current_players ) dummy_games = create_dummy_games(active_courts[possible_games:]) reserved_games = create_reserved_games(inactive_courts) diff --git a/templates/index.j2 b/templates/index.j2 index ac6ddf3..cfd32b5 100644 --- a/templates/index.j2 +++ b/templates/index.j2 @@ -11,6 +11,10 @@ button { width: 100%; } + td { + text-align:center; + width:20%; + } @@ -23,7 +27,8 @@

Games

-
+
+
-- cgit v1.3.1