aboutsummaryrefslogtreecommitdiff
path: root/src/match_up
diff options
context:
space:
mode:
Diffstat (limited to 'src/match_up')
-rw-r--r--src/match_up/__init__.py19
-rw-r--r--src/match_up/model.py59
-rw-r--r--src/match_up/tests/__init__.py16
3 files changed, 93 insertions, 1 deletions
diff --git a/src/match_up/__init__.py b/src/match_up/__init__.py
index 3e74a3d..7b30c1b 100644
--- a/src/match_up/__init__.py
+++ b/src/match_up/__init__.py
@@ -1,2 +1,19 @@
+from pathlib import PurePath
+from time import sleep
+from match_up.model import PlayerDatabase
+from match_up.tests import generate_test_database, remove_test_database
+
+
+class PlaySession:
+ def __init__(self, database_location: PurePath):
+ self.data = PlayerDatabase(database_location)
+
+
def main():
- print("Hello!")
+ generate_test_database()
+ s = PlaySession(PurePath("test_storage.json"))
+ for player in s.data.get_players():
+ print(player)
+ sleep(2)
+ s.data._database.close()
+ remove_test_database()
diff --git a/src/match_up/model.py b/src/match_up/model.py
new file mode 100644
index 0000000..1de0811
--- /dev/null
+++ b/src/match_up/model.py
@@ -0,0 +1,59 @@
+from pathlib import PurePath
+from tinydb import TinyDB, where
+from dataclasses import dataclass
+
+
+@dataclass
+class Player:
+ first_name: str
+ last_name: str
+ skill: int = 1
+ status: bool = False
+ wait_time: int = 0
+
+
+class PlayerDatabase:
+ def __init__(self, database_location: PurePath = PurePath("storage.json")):
+ print(f"Using database location: {database_location.name}")
+ self._database = TinyDB(database_location.name)
+
+ def get_players(self) -> list[Player]:
+ player_list = []
+ for row in self._database:
+ player_list.append(
+ Player(
+ first_name=row["first_name"],
+ last_name=row["last_name"],
+ skill=row["skill"],
+ status=row["skill"],
+ wait_time=row["wait_time"],
+ )
+ )
+ return player_list
+
+ def add_player(self, player: Player) -> bool:
+ self._database.insert(
+ {
+ "first_name": player.first_name,
+ "last_name": player.last_name,
+ "skill": player.skill,
+ "status": player.status,
+ "wait_time": player.wait_time,
+ }
+ )
+
+ def remove_player(self, player: Player) -> bool:
+ self._database.remove(
+ where("first_name") == player.first_name
+ and where("last_name") == player.last_name
+ )
+
+ def update_player_skill(self, player: Player): ...
+
+ def activate_player(self, player: Player): ...
+
+ def deactivate_player(self, player: Player): ...
+
+ def deactivate_all_players(self) -> bool:
+ for row in self._database:
+ row["status"] = False
diff --git a/src/match_up/tests/__init__.py b/src/match_up/tests/__init__.py
new file mode 100644
index 0000000..0a2bd15
--- /dev/null
+++ b/src/match_up/tests/__init__.py
@@ -0,0 +1,16 @@
+from os import remove
+from pathlib import PurePath
+from names import get_first_name, get_last_name
+from match_up.model import Player, PlayerDatabase
+
+TEST_DATABASE_LOCATION = PurePath("test_storage.json")
+
+
+def generate_test_database(total_players: int = 10) -> None:
+ p = PlayerDatabase(TEST_DATABASE_LOCATION)
+ for _ in range(total_players):
+ p.add_player(Player(first_name=get_first_name(), last_name=get_last_name()))
+
+
+def remove_test_database() -> None:
+ remove(TEST_DATABASE_LOCATION)