the_goth_cardgame_collection/models/db_pouch_repo.py

194 lines
8.2 KiB
Python
Raw Permalink Normal View History

# -*- coding: utf-8 -*-
# models/db_pouch_repo.py
from __future__ import annotations
import resources.code_res.sql_queries as sql
from resources.code_res.game_constants import (
BJ_DEFAULT_BET,
BJ_STARTING_GOLD,
DEVILS_DEAL_GOLD_REWARD,
GOLD_MAX_ACCOUNTS,
PK_STARTING_GOLD,
XP_TO_GOLD_AMOUNT,
)
class DbPouchRepo:
"""
Kapselt alle Operationen für Blackjack- & Poker-Goldbeutel,
XP->Gold-Umtausch (inkl. Divine Punishment) sowie The Devil's Deal.
"""
@staticmethod
def get_sacrificeable_profiles(db, active_profile_id: int, game_folder: str) -> list[dict]:
from models.progression import Progression
min_lvl = Progression.get_devils_deal_min_level()
eligible = []
for row in db.fetch_all(sql.SELECT_CANDIDATES_FOR_GAME_SACRIFICE, (game_folder, active_profile_id)):
info = Progression.get_level_info(row["points"])
if info["level"] >= min_lvl:
eligible.append({
"id": row["id"],
"name": row["name"],
"level": info["level"],
"title": info["title"],
})
return eligible
@classmethod
def execute_devils_deal(
cls,
db,
active_profile_id: int,
game_folder: str,
sacrifice_profile_id: int,
account_id: int,
) -> tuple[bool, str]:
from resources.code_res.i18n import t
candidates = {c["id"]: c for c in cls.get_sacrificeable_profiles(db, active_profile_id, game_folder)}
victim = candidates.get(sacrifice_profile_id)
if not victim:
return False, ""
if game_folder == "poker":
with db.conn():
db.conn().execute(sql.DELETE_PROFILE_BY_ID, (sacrifice_profile_id,))
db.conn().execute(sql.UPDATE_PK_GOLD_ONLY, (DEVILS_DEAL_GOLD_REWARD, account_id))
db.conn().execute(sql.SET_PK_DEVILS_DEAL_ACTIVE, (1, account_id))
acc = cls.get_poker_account(db, account_id)
else:
with db.conn():
db.conn().execute(sql.DELETE_PROFILE_BY_ID, (sacrifice_profile_id,))
db.conn().execute(sql.UPDATE_BJ_GOLD_ONLY, (DEVILS_DEAL_GOLD_REWARD, account_id))
db.conn().execute(sql.SET_BJ_DEVILS_DEAL_ACTIVE, (1, account_id))
acc = cls.get_blackjack_account(db, account_id)
return True, t("bj_devils_deal_success", victim["name"], DEVILS_DEAL_GOLD_REWARD, acc["account_name"])
@staticmethod
def get_active_devils_bj_account(db, profile_id: int) -> dict | None:
return db.fetch_one(sql.SELECT_ACTIVE_DEVILS_BJ_ACCOUNT, (profile_id,))
@staticmethod
def set_bj_devils_deal_active(db, bj_account_id: int, active: bool) -> None:
db.execute(sql.SET_BJ_DEVILS_DEAL_ACTIVE, (1 if active else 0, bj_account_id))
@staticmethod
def get_active_devils_pk_account(db, profile_id: int) -> dict | None:
return db.fetch_one(sql.SELECT_ACTIVE_DEVILS_PK_ACCOUNT, (profile_id,))
@staticmethod
def set_pk_devils_deal_active(db, pk_account_id: int, active: bool) -> None:
db.execute(sql.SET_PK_DEVILS_DEAL_ACTIVE, (1 if active else 0, pk_account_id))
# --- Blackjack Gold-Konten ---
@staticmethod
def get_blackjack_accounts(db, profile_id: int) -> list[dict]:
return db.fetch_all(sql.SELECT_BJ_ACCOUNTS_BY_PROFILE, (profile_id,))
@staticmethod
def get_blackjack_account(db, bj_account_id: int) -> dict | None:
return db.fetch_one(sql.SELECT_BJ_ACCOUNT_BY_ID, (bj_account_id,))
@classmethod
def create_blackjack_account(cls, db, profile_id: int, account_name: str) -> dict:
from resources.code_res.i18n import t
clean_name = account_name.strip()
if not clean_name:
raise ValueError(t("bj_err_empty_name"))
if len(cls.get_blackjack_accounts(db, profile_id)) >= GOLD_MAX_ACCOUNTS:
raise RuntimeError(t("bj_max_acc_reached", GOLD_MAX_ACCOUNTS))
db.execute(sql.INSERT_BJ_ACCOUNT, (profile_id, clean_name, BJ_STARTING_GOLD, BJ_DEFAULT_BET))
return db.fetch_one(sql.SELECT_BJ_ACCOUNT_BY_NAME, (profile_id, clean_name))
@classmethod
def update_blackjack_gold(cls, db, bj_account_id: int, gold_delta: int, last_bet: int | None = None) -> dict:
if last_bet is not None:
db.execute(sql.UPDATE_BJ_GOLD_AND_BET, (int(gold_delta), int(last_bet), bj_account_id))
else:
db.execute(sql.UPDATE_BJ_GOLD_ONLY, (int(gold_delta), bj_account_id))
return cls.get_blackjack_account(db, bj_account_id)
@staticmethod
def save_blackjack_last_bet(db, bj_account_id: int, bet: int) -> None:
db.execute(sql.UPDATE_BJ_LAST_BET, (int(bet), bj_account_id))
@staticmethod
def delete_blackjack_account(db, bj_account_id: int) -> None:
db.execute(sql.DELETE_BJ_ACCOUNT, (bj_account_id,))
# --- Poker Gold-Konten ---
@staticmethod
def get_poker_accounts(db, profile_id: int) -> list[dict]:
return db.fetch_all(sql.SELECT_PK_ACCOUNTS_BY_PROFILE, (profile_id,))
@staticmethod
def get_poker_account(db, pk_account_id: int) -> dict | None:
return db.fetch_one(sql.SELECT_PK_ACCOUNT_BY_ID, (pk_account_id,))
@classmethod
def create_poker_account(cls, db, profile_id: int, account_name: str) -> dict:
from resources.code_res.i18n import t
clean_name = account_name.strip()
if not clean_name:
raise ValueError(t("bj_err_empty_name"))
if len(cls.get_poker_accounts(db, profile_id)) >= GOLD_MAX_ACCOUNTS:
raise RuntimeError(t("bj_max_acc_reached", GOLD_MAX_ACCOUNTS))
db.execute(sql.INSERT_PK_ACCOUNT, (profile_id, clean_name, PK_STARTING_GOLD))
return db.fetch_one(sql.SELECT_PK_ACCOUNT_BY_NAME, (profile_id, clean_name))
@classmethod
def update_poker_gold(cls, db, pk_account_id: int, gold_delta: int) -> dict:
db.execute(sql.UPDATE_PK_GOLD_ONLY, (int(gold_delta), pk_account_id))
return cls.get_poker_account(db, pk_account_id)
@staticmethod
def delete_poker_account(db, pk_account_id: int) -> None:
db.execute(sql.DELETE_PK_ACCOUNT, (pk_account_id,))
# --- Gemeinsamer XP->Gold-Tausch ---
@classmethod
def exchange_game_xp_for_gold(
cls, db, profile_id: int, game_folder: str, account_id: int
) -> tuple[bool, str]:
from models.progression import Progression
from resources.code_res.i18n import t
prog = db.get_game_progress(profile_id, game_folder)
if prog.get("at_divine_floor"):
return False, t("bj_err_floor_reached")
cost = prog["xp_exchange_cost"]
is_divine = prog["is_divine_punishment"]
if not is_divine and prog["total_points"] < cost:
return False, t("bj_err_not_enough_xp", prog["total_points"], cost)
if is_divine:
floor_xp = Progression.get_creator_spirit_v_floor_xp()
new_points = max(floor_xp, prog["total_points"] - cost)
actual_deducted = prog["total_points"] - new_points
if actual_deducted <= 0:
return False, t("bj_err_floor_reached")
else:
new_points = max(0, prog["total_points"] - cost)
actual_deducted = cost
with db.conn():
db.conn().execute(sql.SET_GAME_POINTS, (new_points, profile_id, game_folder))
if game_folder == "poker":
db.conn().execute(sql.UPDATE_PK_GOLD_ONLY, (XP_TO_GOLD_AMOUNT, account_id))
db.conn().execute(sql.SET_PK_DEVILS_DEAL_ACTIVE, (0, account_id))
acc = cls.get_poker_account(db, account_id)
else:
db.conn().execute(sql.UPDATE_BJ_GOLD_ONLY, (XP_TO_GOLD_AMOUNT, account_id))
db.conn().execute(sql.SET_BJ_DEVILS_DEAL_ACTIVE, (0, account_id))
acc = cls.get_blackjack_account(db, account_id)
updated_prog = db.get_game_progress(profile_id, game_folder)
if is_divine:
return True, t(
"bj_exchange_divine_success",
XP_TO_GOLD_AMOUNT, acc["account_name"], actual_deducted, updated_prog["title"],
)
return True, t("bj_exchange_success", XP_TO_GOLD_AMOUNT, acc["account_name"], actual_deducted)