0Mart00
v2.0: AI cache, valtozo szo hossz (4-7), dashboard stats, multiplayer gateway, success modal, univerzalis Dockerfile, docker-compose mindket service
fc7b2a8 Download src/wordgame/core/database.py from humut/WordGame: direct link, hf CLI and curl.
- Browser
- Download file 3.18 kB
-
https://huggingface.co/spaces/humut/WordGame/resolve/main/src/wordgame/core/database.py
- Command line
-
hf download hf://spaces/humut/WordGame/src/wordgame/core/database.py
-
curl -L -o database.py https://huggingface.co/spaces/humut/WordGame/resolve/main/src/wordgame/core/database.py
3.18 kB
| """Supabase database client initialization (lazy-loading). | |
| A kliens csak az első használatkor jön létre. Ha nincsenek beállítva | |
| a SUPABASE_URL / SUPABASE_KEY környezeti változók, az első hívásig | |
| nem történik hiba – ez lehetővé teszi, hogy a Python modulok | |
| importálhatók legyenek statikus analízishez / teszteléshez is. | |
| """ | |
| import hashlib | |
| from typing import Optional | |
| from supabase import Client, create_client | |
| from .config import settings | |
| _supabase_client: Optional[Client] = None | |
| def get_supabase_client() -> Client: | |
| """Létrehozza (ha még nem létezik) és visszaadja a Supabase klienst.""" | |
| global _supabase_client | |
| if _supabase_client is not None: | |
| return _supabase_client | |
| if not settings.SUPABASE_URL or not settings.SUPABASE_KEY: | |
| raise ValueError( | |
| "Supabase credentials not configured. " | |
| "Set SUPABASE_URL and SUPABASE_KEY environment variables." | |
| ) | |
| _supabase_client = create_client( | |
| settings.SUPABASE_URL, | |
| settings.SUPABASE_KEY | |
| ) | |
| return _supabase_client | |
| class _SupabaseProxy: | |
| """Proxy objektum, ami lazy-loadolja a Supabase klienst. | |
| A `supabase.table(...).select(...)` hívások működjenek | |
| anélkül, hogy a kódban mindenütt át kéne írni `get_supabase_client()`-re. | |
| """ | |
| def __getattr__(self, name): | |
| return getattr(get_supabase_client(), name) | |
| # Modul-szintű proxy – a régi `supabase` változó helyett. | |
| supabase = _SupabaseProxy() | |
| # ------------------------------------------------------------------ | |
| # AI cache database helpers | |
| # ------------------------------------------------------------------ | |
| def _hash_prompt(prompt: str) -> str: | |
| """SHA256 hash a promptbol, roviditve.""" | |
| return hashlib.sha256(prompt.lower().strip().encode()).hexdigest()[:16] | |
| def get_cached_ai_word(prompt: str) -> Optional[str]: | |
| """Lekerdezi, hogy van-e mar elmentett szo a prompt_hash alapjan.""" | |
| prompt_hash = _hash_prompt(prompt) | |
| try: | |
| res = supabase.table(settings.TABLE_WORDS)\ | |
| .select("szo")\ | |
| .execute() | |
| if res.data: | |
| for row in res.data: | |
| stored_hash = row.get("prompt_hash", "") | |
| if stored_hash == prompt_hash and row.get("ai_generated"): | |
| return row["szo"].upper() | |
| except Exception as e: | |
| print(f"[AI-CACHE] Supabase lookup error: {e}") | |
| return None | |
| def save_ai_word(prompt: str, word: str, kategoria: str = "ai") -> dict: | |
| """Elment egy AI altal generalt szot az adatbazisba, ha meg nincs.""" | |
| prompt_hash = _hash_prompt(prompt) | |
| try: | |
| existing = supabase.table(settings.TABLE_WORDS)\ | |
| .select("id")\ | |
| .eq("szo", word.upper())\ | |
| .execute() | |
| if existing.data: | |
| return existing.data[0] | |
| res = supabase.table(settings.TABLE_WORDS)\ | |
| .insert({ | |
| "szo": word.upper(), | |
| "kategoria": kategoria, | |
| })\ | |
| .execute() | |
| return res.data[0] if res.data else {} | |
| except Exception as e: | |
| print(f"[AI-CACHE] Supabase insert error: {e}") | |
| return {} | |