summaryrefslogtreecommitdiff
path: root/src/match_up/model.py
blob: 467310b1cab83551fe9556d404e92fc360c603f3 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from math import floor
from typing import Tuple
from os.path import isfile
from pandas import read_csv
from dataclasses import dataclass
from importlib.resources import path
from random import getrandbits, randint
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine, not_
from names import get_first_name, get_last_name
from match_up.data import (
    MIN_LEVEL,
    MAX_LEVEL,
    PLAYER_PER_COURT,
    BASE,
    GUEST_LEVELS,
    GUESTS_PER_LEVEL,
    Player,
    DisplayPlayer,
)


@dataclass
class CourtList:
    total: int = 12

    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) -> bool:
        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


def _init_guests(session) -> None:
    for level_id, level in enumerate(GUEST_LEVELS):
        for player_id in range(GUESTS_PER_LEVEL):
            unique_id = (level_id * GUESTS_PER_LEVEL) + player_id + 1
            player = Player(
                id=-unique_id,
                first="Guest",
                last=str(unique_id),
                level=level,
                status=False,
                count=0,
            )
            session.add(player)
    session.commit()


def _init_database_from_csv(session, csv_location: str) -> None:
    data = read_csv(csv_location)
    session.query(Player).delete()
    for _, entry in data.iterrows():
        player = Player(
            id=entry["id"],
            first=entry["first"],
            last=entry["last"],
            level=entry["skill"],
            team=entry["team"],
            status=False,
            count=0,
        )
        session.add(player)
    session.commit()
    _init_guests(session)


def _init_random_database(session, player_count: int = 80) -> None:
    total_teams = 5
    players_per_team = 5
    for i in range(player_count):
        if i < total_teams * players_per_team:
            team = floor(i / players_per_team) + 1
        else:
            team = 0
        player = Player(
            id=i,
            first=get_first_name(),
            last=get_last_name(),
            level=randint(MIN_LEVEL, MAX_LEVEL),
            team=team,
            status=bool(getrandbits(1)),
            count=0,
        )
        session.add(player)
    session.commit()
    _init_guests(session)


class PlayerList:

    def __init__(self, csv_location=None) -> None:
        with path("match_up", "players.db") as p:
            self.db_location = p
        db_file_found = True
        if not isfile(self.db_location):
            db_file_found = False
        self._init_database()
        if csv_location is not None:
            _init_database_from_csv(self.session, csv_location)
        else:
            if not db_file_found:
                _init_random_database(self.session)

    def _init_database(self):
        engine = create_engine("sqlite:///" + str(self.db_location))
        BASE.metadata.create_all(bind=engine)
        self.session = sessionmaker(bind=engine)()

    def get_player_status(self, id: int) -> bool:
        return self.session.query(Player).filter(Player.id == id).first().status

    def toggle_player_status(self, id: int) -> DisplayPlayer:
        player = self.session.query(Player).filter(Player.id == id).first()
        player.status = not player.status
        self.session.commit()
        return DisplayPlayer(player)

    def get_all_players(self) -> Tuple[list[DisplayPlayer], list[DisplayPlayer]]:
        all_players = [
            DisplayPlayer(p)
            for p in self.session.query(Player)
            .order_by(Player.first, Player.last)
            .all()
        ]
        regulars = [player for player in all_players if player.first != "Guest"]
        guests = [player for player in all_players if player.first == "Guest"]
        guests.sort(key=lambda x: int(x.last))
        return regulars, guests

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

    def get_players(self) -> list[Player]:
        active_players = (
            self.session.query(Player)
            .order_by(Player.first, Player.last)
            .filter(not_(Player.status == 0))
            .all()
        )
        return active_players

    def increment_game_count(self, player_ids: list[int]):
        players = self.session.query(Player).filter(Player.id.in_(player_ids))
        for player in players:
            player.count += 1
        self.session.commit()

    def reset_game_count(self):
        players = self.session.query(Player)
        for player in players:
            player.count = 0
        self.session.commit()

    def reset_all_players(self):
        players = self.session.query(Player)
        for player in players:
            player.status = 0
        self.session.commit()