From 600b6afff3066d5fc9c4eca659f969322e670a77 Mon Sep 17 00:00:00 2001 From: Karan Jayachandra Date: Tue, 15 Oct 2024 12:07:42 +0200 Subject: Cleaned up the code significantly --- templates/index.j2 | 115 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 65 insertions(+), 50 deletions(-) (limited to 'templates/index.j2') diff --git a/templates/index.j2 b/templates/index.j2 index cfd32b5..3a2118b 100644 --- a/templates/index.j2 +++ b/templates/index.j2 @@ -1,52 +1,67 @@ - - - - 🏸Match Up! - - - - - - -
-
-

🏸The Smashing Fellows

-
-
-
-

Games

-
-
-
-
-
-
-
-
-
-
-
-

Courts

-
-
-
-

Players

-
-
- - - \ No newline at end of file + + + + 🏸Match Up! + + + + + + +
+
+

🏸The Smashing Fellows

+
+
+
+

Games

+
+
+
+ +
+
+
+ +
+
+
+ +
+
+ +
+
+
+
+
+

Courts

+
+
+
+

Players

+
+
+ + + -- cgit v1.3.1 From d304a470e5a9fae6fc0c8975f050cb7aa290f74a Mon Sep 17 00:00:00 2001 From: Karan Jayachandra Date: Wed, 16 Oct 2024 11:08:15 +0200 Subject: First working version with history --- app.py | 21 ++++--- controller.py | 161 +++++++++++++++++++++++++++------------------------ model.py | 19 ++++-- templates/court.j2 | 2 +- templates/courts.j2 | 2 +- templates/games.j2 | 6 +- templates/index.j2 | 15 +++-- templates/player.j2 | 2 +- templates/players.j2 | 2 +- utilities.py | 23 ++++++++ 10 files changed, 151 insertions(+), 102 deletions(-) create mode 100644 utilities.py (limited to 'templates/index.j2') diff --git a/app.py b/app.py index 85dd4f0..87151fe 100644 --- a/app.py +++ b/app.py @@ -1,6 +1,6 @@ from os import getenv -from controller import Session from dotenv import load_dotenv +from controller import RoundGenerator from model import CourtList, PlayerList from flask import Flask, render_template, request @@ -8,7 +8,7 @@ from flask import Flask, render_template, request load_dotenv() -S = Session(courts=CourtList(12), players=PlayerList("test_data_large.csv")) +rg = RoundGenerator(courts=CourtList(12), players=PlayerList("test_data_large.csv")) app = Flask("Match Up! Backend") @@ -23,33 +23,38 @@ def home(): @app.route("/propose", methods=["POST"]) def get_random_games(): levels = int(request.form["type"].split()[0]) - return render_template("games.j2", title="Proposal", games=S.propose_round(levels)) + return render_template("games.j2", title="Proposal", games=rg.propose(levels)) + + +@app.route("/confirm", methods=["POST"]) +def confirm_games(): + return render_template("games.j2", title=f"Round: {rg.round}", games=rg.confirm()) @app.route("/clear", methods=["POST"]) def clear_games(): - return render_template("games.j2", title=f"Round: {S.round}", games=S.clear_round()) + return render_template("games.j2", title=f"Round: {rg.round}", games=rg.clear()) @app.route("/player-toggle/", methods=["POST"]) def toggle_player(player_request): id = int(player_request) - status, name = S.players.toggle_player_status(id) + status, name = rg.players.toggle_player_status(id) return render_template("player.j2", id=id, status=status, name=name) @app.route("/players", methods=["GET"]) def get_list_of_players(): - return render_template("players.j2", players=S.players.get_players()) + return render_template("players.j2", players=rg.players.get_players()) @app.route("/court-toggle/", methods=["POST"]) def toggle_court(court_number): id = int(court_number) - status = S.courts.toggle_court_status(id) + status = rg.courts.toggle_court_status(id) return render_template("court.j2", id=id, status=status) @app.route("/courts", methods=["GET"]) def get_list_of_courts(): - return render_template("courts.j2", courts=S.courts.get_courts()) + return render_template("courts.j2", courts=rg.courts.get_courts()) diff --git a/controller.py b/controller.py index 45ae2bf..751586e 100644 --- a/controller.py +++ b/controller.py @@ -1,102 +1,109 @@ -from math import floor -from random import sample +from typing import Tuple from operator import attrgetter -from dataclasses import dataclass, field -from model import Team, Game, PlayerList, CourtList +from dataclasses import dataclass +from utilities import sample_list +from model import Team, Game, PlayerList, CourtList, MAX_LEVEL, PLAYER_PER_COURT -PLAYER_PER_COURT = 4 - -def _create_placeholders(courts: dict, name: str): +def _create_placeholders(courts: dict, name: str) -> list[Game]: dummy_team = Team(player1=name, player2=name) - return [Game(court, dummy_team, dummy_team) for court in courts] - - -def create_game(court: int, players: dict): - ids = list(players.keys()) - team1 = Team(player1=players[ids[0]].name, player2=players[ids[3]].name) - team2 = Team(player1=players[ids[1]].name, player2=players[ids[2]].name) - return Game(court, team1, team2) + games = [Game(court, dummy_team, dummy_team) for court in courts] + return games -def select_players(players: dict, count: int): - identifiers = sample(sorted(players), count) - return {id: players[id] for id in identifiers} +def _normalize_skill(players: dict, new_max: int) -> int: + player_levels = [] + for _, player in players.items(): + skill_level = player.skill + new_level = round((new_max - 1) * (skill_level / MAX_LEVEL)) + 1 + player_levels.append(new_level) + return player_levels -def get_random_combinations(players: dict) -> list[dict]: - ids = list(players.keys()) - clumps = [ - ids[i : i + PLAYER_PER_COURT] for i in range(0, len(players), PLAYER_PER_COURT) - ] +def _create_groups_of_four_players(players: dict, new_max: int) -> list[dict]: + player_levels = _normalize_skill(players, new_max) + identifiers = sample_list([*players], player_levels) + clumps = [] + for i in range(0, len(players), PLAYER_PER_COURT): + clumps.append(identifiers[i : i + PLAYER_PER_COURT]) combinations = [{id: players[id] for id in clump} for clump in clumps] return combinations -def create_random_games(courts: CourtList, players: PlayerList): - active_courts, reserved_courts = courts.separate_courts() - - # Block the courts for other activities - reserved_games = _create_placeholders(reserved_courts, "RESERVED") - - # Check if there are too many courts - possible_players = players.separate_players()[0] - possible_games = floor(len(possible_players) / PLAYER_PER_COURT) - - # Create the games - selected_players = select_players( - possible_players, possible_games * PLAYER_PER_COURT - ) - combinations = get_random_combinations(selected_players) - - used_courts = dict(list(active_courts.items())[:possible_games]) - active_games = [ - create_game(court, clump) for court, clump in zip(used_courts, combinations) - ] - - # Create dummy games if there are empty courts - dummy_games = [] - if possible_games < len(active_courts): - unused_courts = dict(list(active_courts.items())[possible_games:]) - dummy_games = _create_placeholders(unused_courts, "---") - - # Return the list of games - games = active_games + dummy_games + reserved_games - games.sort(key=attrgetter("court")) - games = {idx: value for idx, value in enumerate(games)} +def _create_games(courts: dict, players: list[dict], levels: int): + grouped_players = _create_groups_of_four_players(players, levels) + games = [] + for court, players in zip(courts, grouped_players): + ids = [*players] + team1 = Team(player1=players[ids[0]].name, player2=players[ids[3]].name) + team2 = Team(player1=players[ids[1]].name, player2=players[ids[2]].name) + games.append(Game(court, team1, team2)) return games @dataclass -class Session: +class RoundGenerator: courts: CourtList players: PlayerList round: int = 0 - games: dict = field(default_factory=dict) - proposal: dict = field(default_factory=dict) - history: list[dict] = field(default_factory=list) - - def __post_init__(self): - self.history = [] - active_courts, inactive_courts = self.courts.separate_courts() - dummy_games = _create_placeholders(active_courts, "---") - reserved_games = _create_placeholders(inactive_courts, "RESERVED") - games = dummy_games + reserved_games + proposal: tuple = tuple() + + def __post_init__(self) -> None: + self.history = {key: 0 for key, _ in self.players.get_players().items()} + self.games = self._generate_proposal(MAX_LEVEL)[0] + + def _separate_courts(self, courts: dict) -> Tuple[dict, dict]: + possible_games = min(len(courts), self.players.get_possible_game_count()) + courts_as_list = list(courts.items()) + used_courts = dict(courts_as_list[:possible_games]) + unused_courts = dict(courts_as_list[possible_games:]) + return used_courts, unused_courts + + def _select_players(self, count: int) -> dict: + active_players = self.players.get_active_players() + identifiers = [*active_players] + play_count = [self.history[player_id] for player_id in active_players] + ids = sample_list(identifiers, play_count, count) + return {player_id: active_players[player_id] for player_id in ids} + + def _create_inactive_games(self) -> list[Game]: + active_courts, reserved_courts = self.courts.separate_courts() + reserved_games = _create_placeholders(reserved_courts, "RESERVED") + _, unused_courts = self._separate_courts(active_courts) + blank_games = _create_placeholders(unused_courts, "---") + games = reserved_games + blank_games + return games + + def _create_active_games(self, total_levels: int): + active_courts, _ = self.courts.separate_courts() + used_courts, _ = self._separate_courts(active_courts) + required_player_count = len(used_courts) * PLAYER_PER_COURT + selected_players = self._select_players(required_player_count) + games = _create_games(used_courts, selected_players, total_levels) + return games, selected_players + + def _generate_proposal(self, total_levels: int) -> Tuple[list[Game], dict]: + inactive_games = self._create_inactive_games() + active_games, selected_players = self._create_active_games(total_levels) + games = active_games + inactive_games games.sort(key=attrgetter("court")) - games = {idx: value for idx, value in enumerate(games)} - self.games = games + return games, selected_players - def clear_round(self): - self.proposal = {} + def clear(self) -> list[Game]: + self.proposal = tuple() return self.games - def propose_round(self, levels: int = 10): - self.proposal = create_random_games(self.courts, self.players) - return self.proposal - - def confirm_round(self): - self.games = self.proposal - self.history.append(self.proposal) - self.proposal = {} + def propose(self, levels: int) -> list[Game]: + self.proposal = self._generate_proposal(levels) + return self.proposal[0] + + def confirm(self) -> list[Game]: + proposed_games, proposed_players = self.proposal + if self.games == proposed_games or len(proposed_games) == 0: + return self.games + self.games = proposed_games + for player_id in proposed_players: + self.history[player_id] = self.history[player_id] + 1 + self.proposal = tuple() self.round += 1 return self.games diff --git a/model.py b/model.py index c0b9dd3..3e76085 100644 --- a/model.py +++ b/model.py @@ -1,8 +1,13 @@ +from math import floor from csv import reader from typing import Tuple from pathlib import Path from dataclasses import dataclass +MIN_LEVEL = 1 +MAX_LEVEL = 10 +PLAYER_PER_COURT = 4 + @dataclass class Player: @@ -56,7 +61,9 @@ class PlayerList: with open(self.location, newline="") as file: pointer = reader(file) next(pointer, None) - players = {id: Player(row[0], row[1]) for id, row in enumerate(pointer)} + players = { + id: Player(row[0], int(row[1])) for id, row in enumerate(pointer) + } self.players = players def get_player_status(self, player_number: int) -> bool: @@ -70,7 +77,11 @@ class PlayerList: def get_players(self) -> dict: return self.players - def separate_players(self) -> Tuple[dict, dict]: + def get_possible_game_count(self) -> int: + active_player_count = len(self.get_active_players()) + possible_games = floor(active_player_count / PLAYER_PER_COURT) + return possible_games + + def get_active_players(self) -> Tuple[dict, dict]: active_players = {k: v for k, v in self.players.items() if v.status} - inactive_players = {k: v for k, v in self.players.items() if not v.status} - return active_players, inactive_players + return active_players diff --git a/templates/court.j2 b/templates/court.j2 index 5eeb65a..859c942 100644 --- a/templates/court.j2 +++ b/templates/court.j2 @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/templates/courts.j2 b/templates/courts.j2 index 5dc64f9..2bc8a95 100644 --- a/templates/courts.j2 +++ b/templates/courts.j2 @@ -2,7 +2,7 @@
{% for key, value in courts.items() %}
- diff --git a/templates/games.j2 b/templates/games.j2 index 55cabf4..50e1087 100644 --- a/templates/games.j2 +++ b/templates/games.j2 @@ -3,14 +3,14 @@ {{ title }} - Court + Court Team 1 Team 2 -{% for _, game in games.items() %} +{% for game in games %} - {{ game.court }} + {{ game.court }} {{ game.team1.player1 }} {{ game.team1.player2 }} {{ game.team2.player1 }} diff --git a/templates/index.j2 b/templates/index.j2 index 3a2118b..40d0eb1 100644 --- a/templates/index.j2 +++ b/templates/index.j2 @@ -9,10 +9,13 @@ @@ -26,7 +29,7 @@

Games

-
+
+
\ No newline at end of file diff --git a/templates/index.j2 b/templates/index.j2 index 40d0eb1..42fc43e 100644 --- a/templates/index.j2 +++ b/templates/index.j2 @@ -29,7 +29,18 @@

Games

-
+
+
+ +
+
+
+ +
+
+
+
+
+
\ No newline at end of file diff --git a/templates/index.j2 b/templates/index.j2 index 42fc43e..4ccdf92 100644 --- a/templates/index.j2 +++ b/templates/index.j2 @@ -3,10 +3,10 @@ - 🏸Match Up! - - - + Match Up! + + + -
-
-

🏸The Smashing Fellows

+
+

Games

@@ -31,11 +46,19 @@
- +
- +
@@ -52,14 +75,21 @@
- +
- +
- +
diff --git a/templates/macros.j2 b/templates/macros.j2 new file mode 100644 index 0000000..b4767be --- /dev/null +++ b/templates/macros.j2 @@ -0,0 +1,11 @@ +{% macro court(id, status) -%} + +{%- endmacro %} + +{% macro player(id, status, name) -%} + +{%- endmacro %} \ No newline at end of file diff --git a/templates/player.j2 b/templates/player.j2 index 7e80285..68a1ea4 100644 --- a/templates/player.j2 +++ b/templates/player.j2 @@ -1,6 +1 @@ - \ No newline at end of file +{% from "macros.j2" import player %}{{ player(id, status, name) }} \ No newline at end of file diff --git a/templates/players.j2 b/templates/players.j2 index 080c03a..4eedc54 100644 --- a/templates/players.j2 +++ b/templates/players.j2 @@ -1,8 +1,9 @@ +{% from "macros.j2" import player %}
-{% for id, player in players.items() %} +{% for id, value in players.items() %}
- + {{ player(id, value.status, value.name) }}
{% endfor %}
\ No newline at end of file -- cgit v1.3.1 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 (limited to 'templates/index.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