Refactor draft app with improved state management and components
* Rename WebSocket message types for better organization * Improve state handling with dedicated methods like broadcast_state * Restructure frontend components and remove unused code
This commit is contained in:
75
data/cache_concept.py
Normal file
75
data/cache_concept.py
Normal file
@@ -0,0 +1,75 @@
|
||||
import pickle
|
||||
import os
|
||||
from typing import Any
|
||||
from pathlib import Path
|
||||
import json
|
||||
|
||||
DEFAULT_PATH = Path("/Users/asc/Developer/boxofficefantasy/main/data/draft_cache.json")
|
||||
|
||||
class CachedDraftState:
|
||||
participants: list
|
||||
phase: str # Replace with Enum if needed
|
||||
draft_order: list = []
|
||||
draft_index: int
|
||||
current_movie: str
|
||||
bids: list
|
||||
|
||||
def __init__(self, cache_file: str = "draft_cache.json"):
|
||||
super().__setattr__("_cache_file", cache_file)
|
||||
super().__setattr__("_cache", self._load_cache())
|
||||
|
||||
def _load_cache(self) -> dict:
|
||||
if os.path.exists(self._cache_file):
|
||||
try:
|
||||
with open(self._cache_file, "r", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
except Exception as e:
|
||||
print(f"Failed to load cache: {e}")
|
||||
return {}
|
||||
return {}
|
||||
|
||||
def _save_cache(self):
|
||||
try:
|
||||
with open(self._cache_file, "w", encoding="utf-8") as f:
|
||||
json.dump(self._cache, f, indent=2)
|
||||
except Exception as e:
|
||||
print(f"Failed to save cache: {e}")
|
||||
|
||||
def __getattr__(self, name: str) -> Any:
|
||||
if name in self.__class__.__annotations__:
|
||||
print(f"[GET] {name} -> {self._cache.get(name)}")
|
||||
return self._cache.get(name, None)
|
||||
raise AttributeError(f"'{type(self).__name__}' object has no attribute '{name}'")
|
||||
|
||||
def __setattr__(self, name: str, value: Any):
|
||||
if name in self.__class__.__annotations__:
|
||||
print(f"[SET] {name} = {value}")
|
||||
self._cache[name] = value
|
||||
self._save_cache()
|
||||
else:
|
||||
super().__setattr__(name, value)
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Clean start for testing
|
||||
if os.path.exists("draft_cache.pkl"):
|
||||
os.remove("draft_cache.pkl")
|
||||
|
||||
print("\n--- First Run: Setting Attributes ---")
|
||||
state = CachedDraftState()
|
||||
state.participants = ["Alice", "Bob"]
|
||||
state.phase = "nominating"
|
||||
# state.draft_order = ["Bob", "Alice"]
|
||||
state.draft_index = 0
|
||||
state.current_movie = "The Matrix"
|
||||
state.bids = [{"Alice": 10}, {"Bob": 12}]
|
||||
|
||||
print("\n--- Second Run: Reading from Cache ---")
|
||||
state2 = CachedDraftState()
|
||||
print("participants:", state2.participants)
|
||||
print("phase:", state2.phase)
|
||||
print("draft_order:", state2.draft_order)
|
||||
print("draft_index:", state2.draft_index)
|
||||
print("current_movie:", state2.current_movie)
|
||||
print("bids:", state2.bids)
|
||||
|
||||
pass
|
||||
BIN
data/draft_cache.json
Normal file
BIN
data/draft_cache.json
Normal file
Binary file not shown.
Reference in New Issue
Block a user