From 9ef7d95c7010351078a929f5144f5098d4142cd0 Mon Sep 17 00:00:00 2001 From: Karan Jayachandra Date: Fri, 5 Apr 2024 16:55:07 +0200 Subject: Added the working version in dash --- src/match_up/__init__.py | 4 +- src/match_up/model.py | 41 +++++++++++++-- src/match_up/templates/index.html | 14 +++++ src/match_up/view.py | 107 ++++++++++++++++++++++---------------- 4 files changed, 118 insertions(+), 48 deletions(-) create mode 100644 src/match_up/templates/index.html (limited to 'src') diff --git a/src/match_up/__init__.py b/src/match_up/__init__.py index c41394a..04c2f12 100644 --- a/src/match_up/__init__.py +++ b/src/match_up/__init__.py @@ -19,7 +19,9 @@ class Application: database_location = DATABASE_LOCATION self._session = PlaySession(PurePath(database_location)) self._dashboard = Dash("Match Up!", external_stylesheets=[ZEPHYR]) - self._layout = MainLayout(self._session.data.get_players()) + self._layout = MainLayout( + self._session.data.get_players(), self._session.get_matches() + ) self._dashboard.layout = self._layout.get() def run(self): diff --git a/src/match_up/model.py b/src/match_up/model.py index 3a9990b..d27893d 100644 --- a/src/match_up/model.py +++ b/src/match_up/model.py @@ -1,6 +1,7 @@ from pathlib import PurePath from tinydb import TinyDB, where from dataclasses import dataclass +from random import randint MIN_SKILL = 1 MAX_SKILL = 10 @@ -10,9 +11,24 @@ class PlaySession: def __init__(self, database_location: PurePath): self.data = PlayerDatabase(database_location) - def start_round(self): ... - - def end_round(self): ... + def get_matches(self, number_of_courts: int = 4): + player_list = self.data.get_players() + game_list = [] + for i in range(number_of_courts): + game_list.append( + Game( + court_number=i + 1, + team_1=Team( + player_1=player_list[randint(0, len(player_list) - 1)], + player_2=player_list[randint(0, len(player_list) - 1)], + ), + team_2=Team( + player_1=player_list[randint(0, len(player_list) - 1)], + player_2=player_list[randint(0, len(player_list) - 1)], + ), + ) + ) + return game_list def end(self): self.data.close() @@ -35,6 +51,25 @@ class Player: status: bool = False wait_time: int = 0 + def get_name(self): + return self.first_name + " " + self.last_name + + +@dataclass +class Team: + player_1: Player + player_2: Player + + def __str__(self): + return self.player_1.get_name() + " & " + self.player_2.get_name() + + +@dataclass +class Game: + court_number: int + team_1: Team + team_2: Team + class PlayerDatabase: def __init__(self, database_location: PurePath = PurePath("storage.json")): diff --git a/src/match_up/templates/index.html b/src/match_up/templates/index.html new file mode 100644 index 0000000..23bba20 --- /dev/null +++ b/src/match_up/templates/index.html @@ -0,0 +1,14 @@ + + + + {{ title }} + + +

{{ title }}

+ + + \ No newline at end of file diff --git a/src/match_up/view.py b/src/match_up/view.py index 9da82a9..8fa1c54 100644 --- a/src/match_up/view.py +++ b/src/match_up/view.py @@ -1,45 +1,37 @@ -from dash.html import Div, H4 -from match_up.model import Player -from dash.dash_table import DataTable -from dash_bootstrap_components import Row, Col, Container, Input, Select +from dash.html import Div, H4, Thead, Tbody, Tr, Td, Th +from match_up.model import Player, Game +from dash_bootstrap_components import ( + Row, + Col, + Container, + Input, + Select, + Table, + NavbarSimple, +) -class PlayerLayout: - def __init__(self, player_list: list[Player]): +class GameLayout: + def __init__(self, game_list: list[Game]): + table_header = [Thead(Tr([Th("Court"), Th("Team 1"), Th("Team 2")]))] + table_body = [ + Tbody( + [ + Tr( + [ + Td(children=game.court_number), + Td(children=game.team_1.__str__()), + Td(children=game.team_2.__str__()), + ] + ) + for game in game_list + ] + ) + ] self._layout = Div( [ - H4(children="Player List"), - DataTable( - data=[ - { - "Player": player.first_name + " " + player.last_name, - "Status": "Active" if player.status else "Inactive", - } - for player in player_list - ], - columns=[ - {"name": "Player", "id": "Player", "type": "text"}, - ], - style_data_conditional=[ - { - "if": { - "column_id": "Player", - "filter_query": "{Status} = Active", - }, - "backgroundColor": "green", - "color": "white", - }, - { - "if": { - "column_id": "Player", - "filter_query": "{Status} = Inactive", - }, - "backgroundColor": "tomato", - "color": "white", - "fontWeight": "bold", - }, - ], - ), + H4(children="Game List", style={"textAlign": "center"}), + Table(table_header + table_body, bordered=True), ] ) @@ -47,30 +39,57 @@ class PlayerLayout: return self._layout -class GameLayout: - def __init__(): ... +class PlayerLayout: + def __init__(self, player_list: list[Player]): + table_header = [Thead(Tr([Th("Player Name")]))] + table_body = [ + Tbody( + [ + Tr( + [ + Td( + children=player.first_name + " " + player.last_name, + style={"color": ("green" if player.status else "red")}, + ) + ] + ) + for player in player_list + ] + ) + ] + self._layout = Div( + [ + H4(children="Player List", style={"textAlign": "center"}), + Table(table_header + table_body, bordered=True), + ] + ) + + def get(self): + return self._layout class MainLayout: - def __init__(self, player_list: list[Player]): + def __init__(self, player_list: list[Player], game_list: list[Game]): self._player_layout = PlayerLayout(player_list) + self._game_layout = GameLayout(game_list) def refresh(): ... def get(self): full_layout = Div( [ + NavbarSimple(brand="Match Up!"), Container( [ Row( [ - Col(["Total Samples"], width=1), - Col([self._player_layout.get()], width=10), + Col([self._game_layout.get()], width=9), + Col([self._player_layout.get()], width=3), ], style={"margin": "15px"}, ) ] - ) + ), ] ) # full_layout = Div([Div(children="Hello World")]) -- cgit v1.3.1