diff options
| author | Karan Jayachandra <karan.jayachandra@nxp.com> | 2024-10-04 18:23:33 +0200 |
|---|---|---|
| committer | Karan Jayachandra <karan.jayachandra@nxp.com> | 2024-10-04 18:23:33 +0200 |
| commit | 88d7300021cd3963f2d6a6202609b0f6e956cdab (patch) | |
| tree | d9e9e186d86572e57253a188467ebb4084535039 | |
| parent | c58caf5b8b8801ba826b0ff6b241d7f25a333bc8 (diff) | |
Added the games view
| -rw-r--r-- | app.py | 33 | ||||
| -rw-r--r-- | model.py | 40 | ||||
| -rw-r--r-- | templates/courts.j2 | 3 | ||||
| -rw-r--r-- | templates/games.j2 | 18 | ||||
| -rw-r--r-- | templates/index.j2 | 12 |
5 files changed, 100 insertions, 6 deletions
@@ -1,6 +1,6 @@ from os import getenv -from model import PlayerList -from flask import Flask, abort, render_template +from model import PlayerList, GameList +from flask import Flask, abort, render_template, request from dotenv import load_dotenv load_dotenv() @@ -8,6 +8,7 @@ load_dotenv() print("Importing player list...\n") PLAYER_LIST = PlayerList(location="test_data.csv") +GAME_GENERATOR = GameList() print("List of player:\n") print(PLAYER_LIST) @@ -21,6 +22,34 @@ def home(): return render_template("index.j2") +# @app.route("/courts", methods=["POST"]) +# def set_courts(): +# print(request) +# print(request.data) +# print(request.json) +# print(request.args) +# return render_template( +# "courts.j2", +# courts=GAME_GENERATOR.courts, +# ) + + +@app.route("/new-games", methods=["POST"]) +def get_new_games(): + return render_template( + "games.j2", + games=GAME_GENERATOR.create_games(PLAYER_LIST.get_active_players()), + ) + + +@app.route("/game-list", methods=["GET"]) +def get_list_of_games(): + return render_template( + "games.j2", + games=GAME_GENERATOR.create_games(PLAYER_LIST.get_active_players()), + ) + + @app.route("/player-toggle/<player_request>", methods=["POST"]) def toggle_player(player_request): selection = PLAYER_LIST.get_player(player_request) @@ -1,3 +1,4 @@ +from math import floor from csv import reader from pathlib import Path from dataclasses import dataclass @@ -10,7 +11,20 @@ class Player: status: bool +@dataclass +class Team: + player1: Player + player2: Player + + +@dataclass +class Game: + team1: Team + team2: Team + + class PlayerList: + def __init__(self, location: Path = "players.csv"): with open(location, newline="") as file: data_reader = reader(file) @@ -29,6 +43,9 @@ class PlayerList: def get_player(self, player_request: str): return [player for player in self.players if player.name == player_request] + def get_active_players(self): + return [player for player in self.players if player.status] + def toggle_player(self, player_request: Player): self.players = [ ( @@ -48,3 +65,26 @@ class PlayerList: for player in self.players: display_string += player.__str__() + "\n" return display_string + + +@dataclass +class GameList: + courts: int = 8 + + def create_games(self, players: list[Player]): + possible_games = floor(len(players) / 4) + 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]) + 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 diff --git a/templates/courts.j2 b/templates/courts.j2 new file mode 100644 index 0000000..a7893a7 --- /dev/null +++ b/templates/courts.j2 @@ -0,0 +1,3 @@ +<form hx-post="/courts" hx-trigger="change" hx-swap="outerHTML"> + <input type="number" disabled id="courts" min="1" max="15" name="courts" value="{{ courts }}"> +</form>
\ No newline at end of file diff --git a/templates/games.j2 b/templates/games.j2 new file mode 100644 index 0000000..22ea936 --- /dev/null +++ b/templates/games.j2 @@ -0,0 +1,18 @@ +<div class="games"> +<table> +<tr> + <th> Court </th> + <th colspan="2"> Team 1 </th> + <th colspan="2"> Team 2 </th> +</tr> +{% for game in games %} + <tr> + <th>{{ loop.index }}</th> + <td>{{ game.team1.player1.name }}</th> + <td>{{ game.team1.player2.name }}</th> + <td>{{ game.team2.player1.name }}</th> + <td>{{ game.team2.player2.name }}</th> + </tr> +{% endfor %} +</table> +</div>
\ No newline at end of file diff --git a/templates/index.j2 b/templates/index.j2 index 82c3c80..be92207 100644 --- a/templates/index.j2 +++ b/templates/index.j2 @@ -30,16 +30,20 @@ </head> <body> <header> - <h1>🏸Match Up!</h1> - <p class="tagline"> Come play with the <a href="https://www.bctsf.nl/">Smashing Fellows</a>! </p> + <h1>🏸The Smashing Fellows </h1> + <h4> Total Courts: 8</h4> </header> - <hr> + <div> + <h2>Games</h2> + <div class="games" hx-get="/game-list" hx-swap="outerHTML" hx-trigger="revealed"></div> + </div> + <button hx-post="/new-games" hx-trigger="click" hx-target=".games" hx-swap="outerHTML"> New Games </button> <div class="players"> <h2>Players</h2> <div hx-get="/player-list" hx-swap="outerHTML" hx-trigger="revealed"></div> </div> <footer> - Made by <a href="https://karanjayachandra.com/">Karan Jayachandra</a> using <a href="https://htmx.org/">HTMX</a> and <a href="https://flask.palletsprojects.com">Flask</a> + Made by <a href="https://karanjayachandra.com/">K. Jayachandra</a> using <a href="https://htmx.org/">HTMX</a> and <a href="https://flask.palletsprojects.com">Flask</a> </footer> </body> </html>
\ No newline at end of file |
