Spaces:
Running
Running
Update app.py
#124
by Muthuraja18 - opened
app.py
CHANGED
|
@@ -397,6 +397,63 @@ def unified_push_message(game_id, msg_obj):
|
|
| 397 |
|
| 398 |
# ---------------------------
|
| 399 |
# Push a new score to leaderboard
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 400 |
# ---------------------------
|
| 401 |
def unified_push_leaderboard(row):
|
| 402 |
"""
|
|
|
|
| 397 |
|
| 398 |
# ---------------------------
|
| 399 |
# Push a new score to leaderboard
|
| 400 |
+
def sync_firebase_leaderboard_to_csv():
|
| 401 |
+
"""
|
| 402 |
+
Sync Firebase /leaderboard → local CSV.
|
| 403 |
+
Runs only in Online mode.
|
| 404 |
+
Prevents duplicate entries.
|
| 405 |
+
"""
|
| 406 |
+
|
| 407 |
+
mode = st.session_state.get("mode_selection", "Offline")
|
| 408 |
+
if mode != "Online":
|
| 409 |
+
return
|
| 410 |
+
|
| 411 |
+
ok, msg = init_firebase_if_needed()
|
| 412 |
+
if not ok:
|
| 413 |
+
return
|
| 414 |
+
|
| 415 |
+
try:
|
| 416 |
+
# Fetch from Firebase
|
| 417 |
+
fb_data = fb_get("/leaderboard") or {}
|
| 418 |
+
|
| 419 |
+
if isinstance(fb_data, dict):
|
| 420 |
+
fb_rows = list(fb_data.values())
|
| 421 |
+
elif isinstance(fb_data, list):
|
| 422 |
+
fb_rows = fb_data
|
| 423 |
+
else:
|
| 424 |
+
fb_rows = []
|
| 425 |
+
|
| 426 |
+
if not fb_rows:
|
| 427 |
+
return
|
| 428 |
+
|
| 429 |
+
# Load existing CSV
|
| 430 |
+
if os.path.exists(LEADERBOARD_FILE):
|
| 431 |
+
csv_df = pd.read_csv(LEADERBOARD_FILE)
|
| 432 |
+
else:
|
| 433 |
+
csv_df = pd.DataFrame()
|
| 434 |
+
|
| 435 |
+
fb_df = pd.DataFrame(fb_rows)
|
| 436 |
+
|
| 437 |
+
# If CSV empty → just save
|
| 438 |
+
if csv_df.empty:
|
| 439 |
+
fb_df.to_csv(LEADERBOARD_FILE, index=False)
|
| 440 |
+
return
|
| 441 |
+
|
| 442 |
+
# Merge & remove duplicates
|
| 443 |
+
combined = pd.concat([csv_df, fb_df], ignore_index=True)
|
| 444 |
+
|
| 445 |
+
# Remove duplicates using unique combo
|
| 446 |
+
combined.drop_duplicates(
|
| 447 |
+
subset=["name", "game_id", "timestamp"],
|
| 448 |
+
keep="last",
|
| 449 |
+
inplace=True
|
| 450 |
+
)
|
| 451 |
+
|
| 452 |
+
combined.to_csv(LEADERBOARD_FILE, index=False)
|
| 453 |
+
|
| 454 |
+
except Exception as e:
|
| 455 |
+
st.warning(f"Leaderboard sync failed: {e}")
|
| 456 |
+
|
| 457 |
# ---------------------------
|
| 458 |
def unified_push_leaderboard(row):
|
| 459 |
"""
|