From 156056e9c31468564fb53ca284996a5de41471fb Mon Sep 17 00:00:00 2001 From: Karan Jayachandra Date: Wed, 16 Oct 2024 23:28:46 +0200 Subject: Working version with all functionality --- app.py | 12 ++++++-- controller.py | 7 +++-- model.py | 33 +++++++++++++++++++++ static/custom.css | 15 ++++++++++ templates/controls.j2 | 51 +++++++++++++++++++++++++++++++++ templates/court.j2 | 1 - templates/games.j2 | 2 +- templates/index.j2 | 79 ++++++--------------------------------------------- templates/player.j2 | 1 - templates/time.j2 | 3 ++ 10 files changed, 125 insertions(+), 79 deletions(-) create mode 100644 static/custom.css create mode 100644 templates/controls.j2 delete mode 100644 templates/court.j2 delete mode 100644 templates/player.j2 create mode 100644 templates/time.j2 diff --git a/app.py b/app.py index 261c76b..22f6767 100644 --- a/app.py +++ b/app.py @@ -9,7 +9,9 @@ from flask import Flask, render_template, request load_dotenv() app = Flask("Match Up! Backend") app.config.from_object(getenv("APP_SETTINGS")) -rg = RoundGenerator(courts=CourtList(12), players=PlayerList(getenv("DATABASE"))) +rg = RoundGenerator( + courts=CourtList(int(getenv("COURTS"))), players=PlayerList(getenv("DATABASE")) +) re = Environment(loader=FileSystemLoader("templates")) @@ -18,6 +20,12 @@ def home(): return render_template("index.j2") +@app.route("/time", methods=["GET"]) +def get_time(): + mins, secs = rg.timer.get() + return render_template("time.j2", mins=mins, secs=secs) + + @app.route("/guests", methods=["GET"]) def get_guests(): return render_template("guests.j2", guests=rg.players.guests) @@ -68,9 +76,7 @@ def get_list_of_players(): def toggle_court(court_number): id = int(court_number) status = rg.courts.toggle_court_status(id) - print(status) t = re.from_string('{% from "macros.j2" import court %}{{ court(id, status) }}') - print(render_template(t, id=id, status=status)) return render_template(t, id=id, status=status) diff --git a/controller.py b/controller.py index 1116443..2e09e9f 100644 --- a/controller.py +++ b/controller.py @@ -2,7 +2,7 @@ from typing import Tuple from operator import attrgetter from dataclasses import dataclass from utilities import sample_list -from model import Team, Game, PlayerList, CourtList, MAX_LEVEL, PLAYER_PER_COURT +from model import Team, Game, PlayerList, CourtList, Timer, MAX_LEVEL, PLAYER_PER_COURT def _create_placeholders(courts: dict, name: str) -> list[Game]: @@ -49,6 +49,7 @@ class RoundGenerator: proposal: tuple = tuple() def __post_init__(self) -> None: + self.timer = Timer() self.history = {key: 0 for key, _ in self.players.get_players().items()} self.games = self._generate_proposal(MAX_LEVEL)[0] @@ -110,7 +111,9 @@ class RoundGenerator: return self.games self.games = proposed_games for player_id in proposed_players: - self.history[player_id] = self.history[player_id] + 1 + if player_id < self.players.guest_id_start: + self.history[player_id] = self.history[player_id] + 1 self.proposal = tuple() self.round += 1 + self.timer.start() return self.games diff --git a/model.py b/model.py index f768d0f..c69ba48 100644 --- a/model.py +++ b/model.py @@ -1,6 +1,7 @@ from math import floor from csv import reader from typing import Tuple +from time import time, strftime from dataclasses import dataclass MIN_LEVEL = 1 @@ -101,3 +102,35 @@ class PlayerList: } players.update(guests) return players + + +@dataclass +class Timer: + ref: float = None + running: bool = False + max_mins: int = 1 + default: str = ("00", "00") + + def __post_init__(self): + self.max_seconds = self.max_mins * 60 + + def start(self): + self.ref = time() + self.max_seconds + self.running = True + + def stop(self): + self.ref = None + self.running = False + + def get(self): + if not self.running: + return self.default + total_seconds = self.ref - time() + if total_seconds < 0: + self.stop() + return self.default + mins = f"{int(total_seconds // 60):02d}" + secs = f"{int(total_seconds % 60):02d}" + print(mins) + print(secs) + return mins, secs diff --git a/static/custom.css b/static/custom.css new file mode 100644 index 0000000..3e57091 --- /dev/null +++ b/static/custom.css @@ -0,0 +1,15 @@ +button, input, .select, .select select { + width: 100%; +} +table { + font-size: 30px; +} +td { + width: 20%; +} +img { + position: absolute; + top: 50%; + left: 50%; + transform: translate(-50%,-50%); +} \ No newline at end of file diff --git a/templates/controls.j2 b/templates/controls.j2 new file mode 100644 index 0000000..1799e01 --- /dev/null +++ b/templates/controls.j2 @@ -0,0 +1,51 @@ +
+
+
+
+ +
+
+
+ +
+
+
+
+
+ +
+
+
+ +
+
+
+ +
+
+ +
+
+
diff --git a/templates/court.j2 b/templates/court.j2 deleted file mode 100644 index d7aec86..0000000 --- a/templates/court.j2 +++ /dev/null @@ -1 +0,0 @@ -{% from "macros.j2" import court %}{{ court(key, value) }} \ No newline at end of file diff --git a/templates/games.j2 b/templates/games.j2 index 50e1087..0285de1 100644 --- a/templates/games.j2 +++ b/templates/games.j2 @@ -9,7 +9,7 @@ {% for game in games %} - + {{ game.court }} {{ game.team1.player1 }} {{ game.team1.player2 }} diff --git a/templates/index.j2 b/templates/index.j2 index 4ccdf92..fcea4e3 100644 --- a/templates/index.j2 +++ b/templates/index.j2 @@ -6,24 +6,8 @@ Match Up! + -
@@ -36,63 +20,16 @@
-

Today's Games

+

Match Up!

+
+
+
-

Games

-
-
-
-
- -
-
-
- -
-
-
-
-
- -
-
-
- -
-
-
- -
-
- -
-
-
+

Games

+ {% include 'controls.j2' %}

Courts

@@ -104,7 +41,7 @@
diff --git a/templates/player.j2 b/templates/player.j2 deleted file mode 100644 index 68a1ea4..0000000 --- a/templates/player.j2 +++ /dev/null @@ -1 +0,0 @@ -{% from "macros.j2" import player %}{{ player(id, status, name) }} \ No newline at end of file diff --git a/templates/time.j2 b/templates/time.j2 new file mode 100644 index 0000000..d006fc4 --- /dev/null +++ b/templates/time.j2 @@ -0,0 +1,3 @@ +
+

Timer: {{mins}}:{{secs}}

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