Kerikim commited on
Commit
3839e18
·
1 Parent(s): 92fc829

elkay frontend api.py pp

Browse files
phase/Student_view/games/profitpuzzle.py CHANGED
@@ -1,12 +1,19 @@
1
- from utils import db as dbapi
2
  import streamlit as st
 
 
 
 
3
 
4
  def _refresh_global_xp():
5
  user = st.session_state.get("user")
6
  if not user:
7
  return
8
  try:
9
- stats = dbapi.user_xp_and_level(user["user_id"])
 
 
 
10
  st.session_state.xp = stats.get("xp", st.session_state.get("xp", 0))
11
  st.session_state.streak = stats.get("streak", st.session_state.get("streak", 0))
12
  except Exception as e:
@@ -279,6 +286,7 @@ def reset_game():
279
  st.session_state.show_solution = False
280
  st.session_state.score = 0
281
  st.session_state.completed_scenarios = []
 
282
  st.rerun()
283
 
284
 
@@ -288,6 +296,9 @@ def show_profit_puzzle():
288
  # Load CSS styling
289
  load_css()
290
 
 
 
 
291
  st.markdown("""
292
  <div class="game-header">
293
  <h1>🎯 Profit Puzzle Challenge!</h1>
@@ -401,28 +412,58 @@ def show_profit_puzzle():
401
  # Persist to TiDB if logged in
402
  user = st.session_state.get("user")
403
  if user:
 
 
404
  try:
405
- dbapi.record_profit_puzzle_result(
406
- user_id=user["user_id"],
407
- scenario_id=scenario.get("id") or f"scenario_{st.session_state.current_scenario}",
408
- title=scenario.get("title", f"Scenario {st.session_state.current_scenario+1}"),
409
- units=units, price=price, cost=cost,
410
- user_answer=user_val, actual_profit=float(actual_profit),
411
- is_correct=bool(correct), gained_xp=int(reward)
412
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
413
  _refresh_global_xp()
414
  except Exception as e:
415
- st.warning(f"Could not save result: {e}")
416
  else:
417
  st.info("Login to earn and save XP.")
418
 
419
  st.session_state.show_solution = True
420
 
421
  def next_scenario():
422
- if st.session_state.current_scenario < len(scenarios) - 1:
423
  st.session_state.current_scenario += 1
424
  st.session_state.user_answer = ""
425
  st.session_state.show_solution = False
 
 
426
 
427
  def reset_game():
428
  st.session_state.current_scenario = 0
 
1
+ import os, time
2
  import streamlit as st
3
+ from utils import api as backend # HTTP to backend Space
4
+ from utils import db as dbapi # direct DB path (only if DISABLE_DB=0)
5
+
6
+ DISABLE_DB = os.getenv("DISABLE_DB", "1") == "1"
7
 
8
  def _refresh_global_xp():
9
  user = st.session_state.get("user")
10
  if not user:
11
  return
12
  try:
13
+ if DISABLE_DB:
14
+ stats = backend.user_stats(user["user_id"])
15
+ else:
16
+ stats = dbapi.user_xp_and_level(user["user_id"])
17
  st.session_state.xp = stats.get("xp", st.session_state.get("xp", 0))
18
  st.session_state.streak = stats.get("streak", st.session_state.get("streak", 0))
19
  except Exception as e:
 
286
  st.session_state.show_solution = False
287
  st.session_state.score = 0
288
  st.session_state.completed_scenarios = []
289
+ st.session_state.pp_start_ts = time.time()
290
  st.rerun()
291
 
292
 
 
296
  # Load CSS styling
297
  load_css()
298
 
299
+ if "pp_start_ts" not in st.session_state:
300
+ st.session_state.pp_start_ts = time.time()
301
+
302
  st.markdown("""
303
  <div class="game-header">
304
  <h1>🎯 Profit Puzzle Challenge!</h1>
 
412
  # Persist to TiDB if logged in
413
  user = st.session_state.get("user")
414
  if user:
415
+ elapsed_ms = int((time.time() - st.session_state.get("pp_start_ts", time.time())) * 1000)
416
+
417
  try:
418
+ if DISABLE_DB:
419
+ # Route to backend Space
420
+ backend.record_profit_puzzler_play(
421
+ user_id=user["user_id"],
422
+ puzzles_solved=1 if correct else 0,
423
+ mistakes=0 if correct else 1,
424
+ elapsed_ms=elapsed_ms,
425
+ gained_xp=reward # keep UI and server in sync
426
+ )
427
+ else:
428
+ # Direct DB path if you keep it
429
+ if hasattr(dbapi, "record_profit_puzzler_play"):
430
+ dbapi.record_profit_puzzler_play(
431
+ user_id=user["user_id"],
432
+ puzzles_solved=1 if correct else 0,
433
+ mistakes=0 if correct else 1,
434
+ elapsed_ms=elapsed_ms,
435
+ gained_xp=reward
436
+ )
437
+ else:
438
+ # Fallback to your existing detailed writer
439
+ dbapi.record_profit_puzzle_result(
440
+ user_id=user["user_id"],
441
+ scenario_id=scenario.get("id") or f"scenario_{st.session_state.current_scenario}",
442
+ title=scenario.get("title", f"Scenario {st.session_state.current_scenario+1}"),
443
+ units=int(scenario["variables"]["units"]),
444
+ price=int(scenario["variables"]["sellingPrice"]),
445
+ cost=int(scenario["variables"]["costPerUnit"]),
446
+ user_answer=float(st.session_state.user_answer),
447
+ actual_profit=float(actual_profit),
448
+ is_correct=bool(correct),
449
+ gained_xp=int(reward)
450
+ )
451
+
452
  _refresh_global_xp()
453
  except Exception as e:
454
+ st.warning(f"Save failed: {e}")
455
  else:
456
  st.info("Login to earn and save XP.")
457
 
458
  st.session_state.show_solution = True
459
 
460
  def next_scenario():
461
+ if st.session_state.get("current_scenario", 0) < len(st.session_state.get("profit_scenarios", [])) - 1:
462
  st.session_state.current_scenario += 1
463
  st.session_state.user_answer = ""
464
  st.session_state.show_solution = False
465
+ st.session_state.pp_start_ts = time.time()
466
+ st.rerun()
467
 
468
  def reset_game():
469
  st.session_state.current_scenario = 0
utils/api.py CHANGED
@@ -445,17 +445,8 @@ def record_debt_dilemma_play(user_id: int, loans_cleared: int,
445
  ])
446
 
447
 
448
- def record_profit_puzzler_play(user_id: int, puzzles_solved: int,
449
- mistakes: int, elapsed_ms: int,
450
- gained_xp: int | None = None):
451
- payload = {
452
- "user_id": user_id,
453
- "puzzles_solved": puzzles_solved,
454
- "mistakes": mistakes,
455
- "elapsed_ms": elapsed_ms,
456
- }
457
  if gained_xp is not None:
458
  payload["gained_xp"] = gained_xp
459
- return _try_candidates("POST", [
460
- ("/games/profit_puzzler/record", {"json": payload}),
461
- ])
 
445
  ])
446
 
447
 
448
+ def record_profit_puzzler_play(user_id: int, puzzles_solved: int, mistakes: int, elapsed_ms: int, gained_xp: int | None = None):
449
+ payload = {"user_id": user_id, "puzzles_solved": puzzles_solved, "mistakes": mistakes, "elapsed_ms": elapsed_ms}
 
 
 
 
 
 
 
450
  if gained_xp is not None:
451
  payload["gained_xp"] = gained_xp
452
+ return _try_candidates("POST", [("/games/profit_puzzler/record", {"json": payload})])