elkay frontend dd game
Browse files
phase/Student_view/games/debtdilemma.py
CHANGED
|
@@ -4,9 +4,11 @@ from dataclasses import dataclass, field
|
|
| 4 |
from typing import List, Optional, Dict, Literal
|
| 5 |
import random
|
| 6 |
import math
|
| 7 |
-
import os
|
|
|
|
| 8 |
from utils import db as dbapi
|
| 9 |
|
|
|
|
| 10 |
|
| 11 |
def load_css(file_name: str):
|
| 12 |
try:
|
|
@@ -16,6 +18,18 @@ def load_css(file_name: str):
|
|
| 16 |
st.warning("⚠️ Stylesheet not found. Please ensure 'assets/styles.css' exists.")
|
| 17 |
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
DD_SCOPE_CLASS = "dd-scope"
|
| 20 |
|
| 21 |
def _ensure_dd_css():
|
|
@@ -346,6 +360,8 @@ def init_state():
|
|
| 346 |
missedPayments=0,
|
| 347 |
creditScore=random.randint(200, 600),
|
| 348 |
)
|
|
|
|
|
|
|
| 349 |
|
| 350 |
# Fortnight helper (every 2 weeks)
|
| 351 |
def current_fortnight() -> int:
|
|
@@ -420,6 +436,7 @@ def gen_random_event() -> Optional[RandomEvent]:
|
|
| 420 |
# Game actions
|
| 421 |
# ===============================
|
| 422 |
def start_loan():
|
|
|
|
| 423 |
st.session_state.gamePhase = "repaying"
|
| 424 |
if DISBURSE_LOAN_TO_WALLET:
|
| 425 |
st.session_state.wallet += st.session_state.loan.principal
|
|
@@ -955,6 +972,7 @@ def reset_game():
|
|
| 955 |
if k not in {"user", "current_page", "xp", "streak", "current_game", "temp_user"}:
|
| 956 |
del st.session_state[k]
|
| 957 |
init_state()
|
|
|
|
| 958 |
st.rerun()
|
| 959 |
|
| 960 |
def hospital_screen():
|
|
@@ -984,8 +1002,8 @@ def level_complete_screen():
|
|
| 984 |
creditScore=st.session_state.loan.creditScore,
|
| 985 |
)
|
| 986 |
st.session_state.monthlyIncome = lvl.startingIncome
|
|
|
|
| 987 |
st.session_state.gamePhase = "setup"
|
| 988 |
-
# use scoped button
|
| 989 |
st.buttondd("➡️ Start next level", on_click=_go_next, key="btn_next_level", variant="success")
|
| 990 |
|
| 991 |
def completed_screen():
|
|
|
|
| 4 |
from typing import List, Optional, Dict, Literal
|
| 5 |
import random
|
| 6 |
import math
|
| 7 |
+
import os, time
|
| 8 |
+
from utils import api as backend
|
| 9 |
from utils import db as dbapi
|
| 10 |
|
| 11 |
+
DISABLE_DB = os.getenv("DISABLE_DB", "1") == "1"
|
| 12 |
|
| 13 |
def load_css(file_name: str):
|
| 14 |
try:
|
|
|
|
| 18 |
st.warning("⚠️ Stylesheet not found. Please ensure 'assets/styles.css' exists.")
|
| 19 |
|
| 20 |
|
| 21 |
+
def _refresh_global_xp():
|
| 22 |
+
user = st.session_state.get("user")
|
| 23 |
+
if not user:
|
| 24 |
+
return
|
| 25 |
+
try:
|
| 26 |
+
stats = backend.user_stats(user["user_id"]) if DISABLE_DB else dbapi.user_xp_and_level(user["user_id"])
|
| 27 |
+
st.session_state.xp = stats.get("xp", st.session_state.get("xp", 0))
|
| 28 |
+
st.session_state.streak = stats.get("streak", st.session_state.get("streak", 0))
|
| 29 |
+
except Exception as e:
|
| 30 |
+
st.warning(f"XP refresh failed: {e}")
|
| 31 |
+
|
| 32 |
+
|
| 33 |
DD_SCOPE_CLASS = "dd-scope"
|
| 34 |
|
| 35 |
def _ensure_dd_css():
|
|
|
|
| 360 |
missedPayments=0,
|
| 361 |
creditScore=random.randint(200, 600),
|
| 362 |
)
|
| 363 |
+
if "dd_start_ts" not in st.session_state:
|
| 364 |
+
st.session_state.dd_start_ts = time.time()
|
| 365 |
|
| 366 |
# Fortnight helper (every 2 weeks)
|
| 367 |
def current_fortnight() -> int:
|
|
|
|
| 436 |
# Game actions
|
| 437 |
# ===============================
|
| 438 |
def start_loan():
|
| 439 |
+
st.session_state.dd_start_ts = time.time() # start elapsed timer
|
| 440 |
st.session_state.gamePhase = "repaying"
|
| 441 |
if DISBURSE_LOAN_TO_WALLET:
|
| 442 |
st.session_state.wallet += st.session_state.loan.principal
|
|
|
|
| 972 |
if k not in {"user", "current_page", "xp", "streak", "current_game", "temp_user"}:
|
| 973 |
del st.session_state[k]
|
| 974 |
init_state()
|
| 975 |
+
st.session_state.dd_start_ts = time.time() # fresh timer after reset
|
| 976 |
st.rerun()
|
| 977 |
|
| 978 |
def hospital_screen():
|
|
|
|
| 1002 |
creditScore=st.session_state.loan.creditScore,
|
| 1003 |
)
|
| 1004 |
st.session_state.monthlyIncome = lvl.startingIncome
|
| 1005 |
+
st.session_state.dd_start_ts = time.time() # reset timer for new level
|
| 1006 |
st.session_state.gamePhase = "setup"
|
|
|
|
| 1007 |
st.buttondd("➡️ Start next level", on_click=_go_next, key="btn_next_level", variant="success")
|
| 1008 |
|
| 1009 |
def completed_screen():
|