Files changed (1) hide show
  1. app.py +26 -33
app.py CHANGED
@@ -459,43 +459,46 @@ def sync_firebase_leaderboard_to_csv():
459
  # ---------------------------
460
  def unified_push_leaderboard(row):
461
  """
462
- Push a row to:
463
- - Firebase per game
464
- - Firebase global leaderboard
465
- - Local CSV backup
466
- - Online CSV (optional)
467
  """
 
468
  row = row.copy()
469
  row["timestamp"] = datetime.utcnow().isoformat() + "Z"
470
 
471
- game_id = row.get("game_id")
472
- username = row.get("name")
473
  mode = st.session_state.get("mode_selection", "Offline")
474
 
475
  # ---------------- ONLINE MODE ----------------
476
  if mode == "Online":
477
  ok, _ = init_firebase_if_needed()
478
- if ok and game_id and username:
479
-
480
- # 1️⃣ Push per game
481
- ref = db.reference(f"/games/{game_id}/scores/{username}")
482
- ref.set(row)
483
 
484
- # 2️⃣ Push to global leaderboard
485
- global_ref = db.reference("leaderboard_online")
486
- global_ref.push(row)
 
487
 
488
- # ---------------- LOCAL CSV BACKUP ----------------
489
  try:
490
- df = pd.read_csv(LEADERBOARD_FILE)
491
- except FileNotFoundError:
492
- df = pd.DataFrame(columns=list(row.keys()))
 
 
 
 
 
 
 
 
 
 
493
 
494
- df = pd.concat([df, pd.DataFrame([row])], ignore_index=True)
495
- df.to_csv(LEADERBOARD_FILE, index=False)
496
 
497
- # ---------------- ONLINE CSV PUSH (OPTIONAL) ----------------
498
- # If you host CSV somewhere like GitHub raw file
499
  # You must handle upload using requests (if needed)
500
 
501
 
@@ -1314,16 +1317,6 @@ def play_page():
1314
  "correct_flags": json.dumps(flags),
1315
  }
1316
  unified_push_leaderboard(row)
1317
- save_score_to_csv(
1318
- name=row["name"],
1319
- score=row["score"],
1320
- game_id=row["game_id"],
1321
- topics=row["topics"],
1322
- avatar=row["avatar"],
1323
- questions=row["questions"],
1324
- answers=row["answers"],
1325
- correct_flags=row["correct_flags"]
1326
- )
1327
 
1328
 
1329
  # ---------------- AUTO CLOSE GAME ----------------
 
459
  # ---------------------------
460
  def unified_push_leaderboard(row):
461
  """
462
+ Push score to:
463
+ - Firebase (only Online mode)
464
+ - Local leaderboard.csv (ALWAYS)
 
 
465
  """
466
+
467
  row = row.copy()
468
  row["timestamp"] = datetime.utcnow().isoformat() + "Z"
469
 
 
 
470
  mode = st.session_state.get("mode_selection", "Offline")
471
 
472
  # ---------------- ONLINE MODE ----------------
473
  if mode == "Online":
474
  ok, _ = init_firebase_if_needed()
475
+ if ok:
476
+ try:
477
+ # Save in per-game scores
478
+ db.reference(f"/games/{row['game_id']}/scores").push(row)
 
479
 
480
+ # Save in global online leaderboard
481
+ db.reference("/leaderboard_online").push(row)
482
+ except Exception as e:
483
+ st.warning(f"Firebase save failed: {e}")
484
 
485
+ # ---------------- LOCAL CSV (MAIN LEADERBOARD) ----------------
486
  try:
487
+ file_exists = os.path.isfile(LEADERBOARD_FILE)
488
+
489
+ with open(LEADERBOARD_FILE, "a", newline="", encoding="utf-8") as f:
490
+ writer = csv.DictWriter(f, fieldnames=row.keys())
491
+
492
+ if not file_exists:
493
+ writer.writeheader()
494
+
495
+ writer.writerow(row)
496
+
497
+ except Exception as e:
498
+ st.warning(f"CSV save failed: {e}")
499
+
500
 
 
 
501
 
 
 
502
  # You must handle upload using requests (if needed)
503
 
504
 
 
1317
  "correct_flags": json.dumps(flags),
1318
  }
1319
  unified_push_leaderboard(row)
 
 
 
 
 
 
 
 
 
 
1320
 
1321
 
1322
  # ---------------- AUTO CLOSE GAME ----------------