aboutsummaryrefslogtreecommitdiff
path: root/model.py
blob: 904265f09b8ecdab9cf6c5551e3b341b4c686448 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from math import floor
from csv import reader
from typing import Tuple
from time import time, strftime
from dataclasses import dataclass

MIN_LEVEL = 1
MAX_LEVEL = 10
PLAYER_PER_COURT = 4


@dataclass
class Player:
    name: str
    skill: int = 1
    status: bool = False


@dataclass
class Team:
    player1: str
    player2: str


@dataclass
class Game:
    court: int
    team1: Team
    team2: Team


@dataclass
class CourtList:
    total: int

    def __post_init__(self) -> None:
        self.courts = {i + 1: True for i in range(self.total)}

    def get_court_status(self, court_number: int) -> bool:
        return self.courts[court_number]

    def toggle_court_status(self, court_number: int) -> None:
        new_status = not self.courts[court_number]
        self.courts[court_number] = new_status
        return new_status

    def get_courts(self) -> dict:
        return self.courts

    def separate_courts(self) -> Tuple[dict, dict]:
        active_courts = {k: v for k, v in self.courts.items() if v}
        inactive_courts = {k: v for k, v in self.courts.items() if not v}
        return active_courts, inactive_courts


@dataclass
class PlayerList:
    location: str
    guests: int = 0

    def __post_init__(self) -> None:
        with open("data/" + self.location, newline="") as file:
            pointer = reader(file)
            next(pointer, None)
            players = {
                id: Player(row[0], int(row[1])) for id, row in enumerate(pointer)
            }
        self.guest_id_start = len(players) + 1
        self.players = players

    def increment_guests(self):
        self.guests = self.guests + 1
        return self.guests

    def decrement_guests(self):
        if self.guests == 0:
            return 0
        self.guests = self.guests - 1
        return self.guests

    def get_player_status(self, player_number: int) -> bool:
        return self.players[player_number].status

    def toggle_player_status(self, player_number: int) -> Tuple[bool, str]:
        new_status = not self.players[player_number].status
        self.players[player_number].status = new_status
        return new_status, self.players[player_number].name

    def get_players(self) -> dict:
        return self.players

    def get_possible_game_count(self) -> int:
        total_players = len(self.get_potential_players())
        possible_games = floor(total_players / PLAYER_PER_COURT)
        return possible_games

    def get_potential_players(self) -> dict:
        players = {k: v for k, v in self.players.items() if v.status}
        guests = {
            self.guest_id_start + k: Player(name=f"Guest {k}")
            for k in range(1, self.guests + 1)
        }
        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}"
        return mins, secs