From 88d7300021cd3963f2d6a6202609b0f6e956cdab Mon Sep 17 00:00:00 2001 From: Karan Jayachandra Date: Fri, 4 Oct 2024 18:23:33 +0200 Subject: Added the games view --- app.py | 33 +++++++++++++++++++++++++++++++-- model.py | 40 ++++++++++++++++++++++++++++++++++++++++ templates/courts.j2 | 3 +++ templates/games.j2 | 18 ++++++++++++++++++ templates/index.j2 | 12 ++++++++---- 5 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 templates/courts.j2 create mode 100644 templates/games.j2 diff --git a/app.py b/app.py index 7d9f808..7778b3e 100644 --- a/app.py +++ b/app.py @@ -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/", methods=["POST"]) def toggle_player(player_request): selection = PLAYER_LIST.get_player(player_request) diff --git a/model.py b/model.py index 53119eb..ef6e762 100644 --- a/model.py +++ b/model.py @@ -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 @@ +
+ +
\ 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 @@ +
+ + + + + + +{% for game in games %} + + + +{% endfor %} +
Court Team 1 Team 2
{{ loop.index }}{{ game.team1.player1.name }} + {{ game.team1.player2.name }} + {{ game.team2.player1.name }} + {{ game.team2.player2.name }} +
+
\ 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 @@
-

🏸Match Up!

-

Come play with the Smashing Fellows!

+

🏸The Smashing Fellows

+

Total Courts: 8

-
+
+

Games

+
+
+

Players

\ No newline at end of file -- cgit v1.3.1