summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKaran Jayachandra <karan.jayachandra@nxp.com>2024-04-05 10:12:17 +0200
committerKaran Jayachandra <karan.jayachandra@nxp.com>2024-04-05 10:12:17 +0200
commit6dcdffeea79c76436fccaad5b382af709af8c121 (patch)
treeff3191f8fa6bdecd8f32c959d626c38aa63a5567
parentbd3bd7de2fdb6cdb181a9b02b627c98c712f3739 (diff)
Updated the code the be in the right place for the data model
-rw-r--r--src/match_up/__init__.py34
-rw-r--r--src/match_up/model.py68
-rw-r--r--src/match_up/tests/__init__.py35
3 files changed, 102 insertions, 35 deletions
diff --git a/src/match_up/__init__.py b/src/match_up/__init__.py
index c9c3d0b..2b1e42b 100644
--- a/src/match_up/__init__.py
+++ b/src/match_up/__init__.py
@@ -1,25 +1,25 @@
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
+from match_up.model import PlaySession
+from match_up.tests import (
+ TEST_DATABASE_LOCATION,
+ generate_test_database,
+ activate_random_players,
+)
+DATABASE_LOCATION = "storage.json"
-class PlaySession:
- def __init__(self, database_location: PurePath):
- self.data = PlayerDatabase(database_location)
- def end(self):
- self.data.close()
-
- def __del__(self):
- self.end()
+class Application:
+ def __init__(self, debug=False):
+ if debug:
+ database_location = TEST_DATABASE_LOCATION
+ else:
+ database_location = DATABASE_LOCATION
+ self.session = PlaySession(PurePath(database_location))
def main():
generate_test_database()
- s = PlaySession(PurePath("test_storage.json"))
- for player in s.data.get_players():
- print(player)
- sleep(2)
- s.end()
- remove_test_database()
+ activate_random_players()
+ a = Application(debug=True)
+ print(a.session)
diff --git a/src/match_up/model.py b/src/match_up/model.py
index 924e674..181b698 100644
--- a/src/match_up/model.py
+++ b/src/match_up/model.py
@@ -3,6 +3,27 @@ from tinydb import TinyDB, where
from dataclasses import dataclass
+class PlaySession:
+ def __init__(self, database_location: PurePath):
+ self.data = PlayerDatabase(database_location)
+
+ def start_round(self): ...
+
+ def end_round(self): ...
+
+ def end(self):
+ self.data.close()
+
+ def __str__(self):
+ display_string = ""
+ for player in self.data.get_players():
+ display_string = display_string + player.__str__() + "\n"
+ return display_string.rstrip()
+
+ def __del__(self):
+ self.end()
+
+
@dataclass
class Player:
first_name: str
@@ -31,6 +52,38 @@ class PlayerDatabase:
)
return player_list
+ def activate_player(self, player: Player):
+ self._database.update(
+ {"status": True},
+ where("first_name") == player.first_name
+ and where("last_name") == player.last_name,
+ )
+
+ def deactivate_player(self, player: Player):
+ self._database.update(
+ {"wait_time": False},
+ where("first_name") == player.first_name
+ and where("last_name") == player.last_name,
+ )
+
+ def increment_player_wait(self, player: Player):
+ self._database.update(
+ {"wait_time": 1},
+ where("first_name") == player.first_name
+ and where("last_name") == player.last_name,
+ )
+
+ def reset_player_wait(self, player: Player):
+ self._database.update(
+ {"wait_time": 0},
+ where("first_name") == player.first_name
+ and where("last_name") == player.last_name,
+ )
+
+ def deactivate_all_players(self) -> bool:
+ for row in self._database:
+ row["status"] = False
+
def add_player(self, player: Player) -> bool:
self._database.insert(
{
@@ -48,15 +101,12 @@ class PlayerDatabase:
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
+ def update_player_skill(self, player: Player):
+ self._database.update(
+ {"skill": player.skill},
+ where("first_name") == player.first_name
+ and where("last_name") == player.last_name,
+ )
def close(self) -> None:
self._database.close()
diff --git a/src/match_up/tests/__init__.py b/src/match_up/tests/__init__.py
index 38e377f..4dbd883 100644
--- a/src/match_up/tests/__init__.py
+++ b/src/match_up/tests/__init__.py
@@ -1,4 +1,5 @@
from os import remove
+from os.path import isfile
from random import randint
from pathlib import PurePath
from names import get_first_name, get_last_name
@@ -9,16 +10,32 @@ MIN_SKILL = 1
MAX_SKILL = 10
-def generate_test_database(total_players: int = 20, active_players: int = 17) -> 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(),
- skill=randint(MIN_SKILL, MAX_SKILL),
+def generate_test_database(total_players: int = 20) -> None:
+ try:
+ if isfile(TEST_DATABASE_LOCATION):
+ remove_test_database()
+ db = PlayerDatabase(TEST_DATABASE_LOCATION)
+ for _ in range(total_players):
+ db.add_player(
+ Player(
+ first_name=get_first_name(),
+ last_name=get_last_name(),
+ skill=randint(MIN_SKILL, MAX_SKILL),
+ )
)
- )
+ finally:
+ db.close()
+
+
+def activate_random_players(activate_players: int = 17) -> None:
+ try:
+ db = PlayerDatabase(TEST_DATABASE_LOCATION)
+ player_list = db.get_players()
+ for index, player in enumerate(player_list):
+ if index < activate_players:
+ db.activate_player(player)
+ finally:
+ db.close()
def remove_test_database() -> None: