Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -620,15 +620,27 @@ def create_game_page():
|
|
| 620 |
st.info(f"AI questions will be generated for: {ai_topic}")
|
| 621 |
# Here you can call your AI question generator function if you have one
|
| 622 |
|
| 623 |
-
def save_score_to_csv(row: dict):
|
| 624 |
-
import csv, os
|
| 625 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 626 |
file_exists = os.path.exists(LEADERBOARD_FILE)
|
| 627 |
|
| 628 |
with open(LEADERBOARD_FILE, "a", newline="", encoding="utf-8") as f:
|
| 629 |
-
writer = csv.
|
| 630 |
-
|
| 631 |
-
|
|
|
|
|
|
|
| 632 |
"name",
|
| 633 |
"score",
|
| 634 |
"game_id",
|
|
@@ -637,15 +649,21 @@ def save_score_to_csv(row: dict):
|
|
| 637 |
"avatar",
|
| 638 |
"questions",
|
| 639 |
"answers",
|
| 640 |
-
"correct_flags"
|
| 641 |
-
]
|
| 642 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 643 |
|
| 644 |
-
if not file_exists:
|
| 645 |
-
writer.writeheader()
|
| 646 |
-
|
| 647 |
-
writer.writerow(row)
|
| 648 |
-
f.flush() # 🔥 FORCE write
|
| 649 |
|
| 650 |
# 1️⃣ Define join_game first
|
| 651 |
def join_game(game_id, username, avatar):
|
|
@@ -1075,8 +1093,6 @@ def play_page():
|
|
| 1075 |
"answers": json.dumps(answers),
|
| 1076 |
"correct_flags": json.dumps(flags),
|
| 1077 |
}
|
| 1078 |
-
|
| 1079 |
-
unified_push_leaderboard(row)
|
| 1080 |
save_score_to_csv(row)
|
| 1081 |
|
| 1082 |
# ---------------- AUTO CLOSE GAME ----------------
|
|
|
|
| 620 |
st.info(f"AI questions will be generated for: {ai_topic}")
|
| 621 |
# Here you can call your AI question generator function if you have one
|
| 622 |
|
|
|
|
|
|
|
| 623 |
|
| 624 |
+
LEADERBOARD_FILE = "leaderboard.csv"
|
| 625 |
+
|
| 626 |
+
def save_score_to_csv(
|
| 627 |
+
name,
|
| 628 |
+
score,
|
| 629 |
+
game_id,
|
| 630 |
+
topics,
|
| 631 |
+
avatar="🎮",
|
| 632 |
+
questions=None,
|
| 633 |
+
answers=None,
|
| 634 |
+
correct_flags=None
|
| 635 |
+
):
|
| 636 |
file_exists = os.path.exists(LEADERBOARD_FILE)
|
| 637 |
|
| 638 |
with open(LEADERBOARD_FILE, "a", newline="", encoding="utf-8") as f:
|
| 639 |
+
writer = csv.writer(f)
|
| 640 |
+
|
| 641 |
+
# Write header only once
|
| 642 |
+
if not file_exists:
|
| 643 |
+
writer.writerow([
|
| 644 |
"name",
|
| 645 |
"score",
|
| 646 |
"game_id",
|
|
|
|
| 649 |
"avatar",
|
| 650 |
"questions",
|
| 651 |
"answers",
|
| 652 |
+
"correct_flags"
|
| 653 |
+
])
|
| 654 |
+
|
| 655 |
+
writer.writerow([
|
| 656 |
+
name,
|
| 657 |
+
score,
|
| 658 |
+
game_id,
|
| 659 |
+
",".join(topics) if isinstance(topics, list) else topics,
|
| 660 |
+
datetime.now().isoformat(),
|
| 661 |
+
avatar,
|
| 662 |
+
str(questions),
|
| 663 |
+
str(answers),
|
| 664 |
+
str(correct_flags)
|
| 665 |
+
])
|
| 666 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 667 |
|
| 668 |
# 1️⃣ Define join_game first
|
| 669 |
def join_game(game_id, username, avatar):
|
|
|
|
| 1093 |
"answers": json.dumps(answers),
|
| 1094 |
"correct_flags": json.dumps(flags),
|
| 1095 |
}
|
|
|
|
|
|
|
| 1096 |
save_score_to_csv(row)
|
| 1097 |
|
| 1098 |
# ---------------- AUTO CLOSE GAME ----------------
|