Spaces:
Running
Running
Update app.py (#80)
Browse files- Update app.py (20d4c294f24de5c39b6c1495dac59a42400c45b6)
app.py
CHANGED
|
@@ -689,6 +689,143 @@ def get_top3_and_player_count(game_id):
|
|
| 689 |
HEARTBEAT_THRESHOLD_SECONDS = 60 # adjust if needed
|
| 690 |
|
| 691 |
def home_page():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 692 |
st.header("Home")
|
| 693 |
st.write("Create games, invite friends, play and climb the leaderboard.")
|
| 694 |
|
|
@@ -882,7 +1019,7 @@ def join_game_page():
|
|
| 882 |
# ----------------- Create Game -----------------
|
| 883 |
|
| 884 |
# ----------------- Play Page -----------------
|
| 885 |
-
def
|
| 886 |
# Fetch game info
|
| 887 |
gid = st.session_state.get('current_game_id')
|
| 888 |
uname = st.session_state.get('username')
|
|
@@ -1007,6 +1144,8 @@ def play_page():
|
|
| 1007 |
st.session_state['last_score'] = score
|
| 1008 |
st.session_state['last_game'] = gid
|
| 1009 |
st.session_state['current_index'] = 0
|
|
|
|
|
|
|
| 1010 |
# Friends page
|
| 1011 |
def friends_page():
|
| 1012 |
st.header("Friends")
|
|
|
|
| 689 |
HEARTBEAT_THRESHOLD_SECONDS = 60 # adjust if needed
|
| 690 |
|
| 691 |
def home_page():
|
| 692 |
+
st.header("Home")
|
| 693 |
+
st.write("Create games, join games, and view leaderboard.")
|
| 694 |
+
|
| 695 |
+
# Dummy username for demonstration
|
| 696 |
+
if "username" not in st.session_state:
|
| 697 |
+
st.session_state["username"] = st.text_input("Enter your name", value="Guest")
|
| 698 |
+
|
| 699 |
+
username = st.session_state.get("username")
|
| 700 |
+
|
| 701 |
+
# --- Games ---
|
| 702 |
+
games = unified_get("games") or {}
|
| 703 |
+
players_map = unified_get("players") or {}
|
| 704 |
+
|
| 705 |
+
st.subheader("Recent Games")
|
| 706 |
+
for g in sorted(games.values(), key=lambda x: x.get("created_at", ""), reverse=True)[:10]:
|
| 707 |
+
gid = g.get("game_id")
|
| 708 |
+
st.markdown(f"### ๐ฎ Game: **{gid}** {'(Closed)' if g.get('closed') else ''}")
|
| 709 |
+
st.write(f"Host: {g.get('host')} โ Topics: {', '.join(g.get('topics', []))}")
|
| 710 |
+
st.write(f"Created: {g.get('created_at')}")
|
| 711 |
+
|
| 712 |
+
# Players in this game
|
| 713 |
+
players_here = players_map.get(gid, {}) or {}
|
| 714 |
+
st.write(f"Players joined: **{len(players_here)}**")
|
| 715 |
+
submitted_count = sum(1 for p in players_here.values() if p.get("submitted"))
|
| 716 |
+
st.write(f"Submitted: **{submitted_count} / {len(players_here)}**")
|
| 717 |
+
|
| 718 |
+
# Final scores
|
| 719 |
+
if players_here:
|
| 720 |
+
st.markdown("**๐ Final Scores**")
|
| 721 |
+
for uname_p, info in players_here.items():
|
| 722 |
+
if info.get("submitted"):
|
| 723 |
+
st.write(f"{info.get('avatar','๐ฎ')} **{uname_p}** โ "
|
| 724 |
+
f"Score: **{info.get('score',0)}**, "
|
| 725 |
+
f"Percentage: **{info.get('percentage',0)}%**")
|
| 726 |
+
|
| 727 |
+
# Player status
|
| 728 |
+
if players_here:
|
| 729 |
+
st.markdown("**๐ฅ Player Status**")
|
| 730 |
+
for uname_p, info in players_here.items():
|
| 731 |
+
status = "โ
Submitted" if info.get('submitted') else "โณ Playing"
|
| 732 |
+
st.write(f"{info.get('avatar','๐ฎ')} **{uname_p}** โ {status}")
|
| 733 |
+
|
| 734 |
+
# Buttons to join or challenge
|
| 735 |
+
if st.button(f"Join Game {gid}", key=f"join_{gid}"):
|
| 736 |
+
st.session_state["current_game_id"] = gid
|
| 737 |
+
st.session_state["username"] = username
|
| 738 |
+
st.success(f"You joined Game {gid}")
|
| 739 |
+
st.experimental_rerun() # redirect to play_page automatically
|
| 740 |
+
|
| 741 |
+
if st.button(f"Challenge friends with new game like {gid}", key=f"challenge_{gid}"):
|
| 742 |
+
new_id = f"{gid}_new" # Dummy new game id
|
| 743 |
+
# Add game to unified storage
|
| 744 |
+
games[new_id] = {
|
| 745 |
+
"game_id": new_id,
|
| 746 |
+
"host": username,
|
| 747 |
+
"topics": g.get("topics", []),
|
| 748 |
+
"created_at": now_iso(),
|
| 749 |
+
"closed": False
|
| 750 |
+
}
|
| 751 |
+
unified_set("games", games)
|
| 752 |
+
st.success(f"Challenge created: {new_id}")
|
| 753 |
+
st.session_state["current_game_id"] = new_id
|
| 754 |
+
st.experimental_rerun()
|
| 755 |
+
|
| 756 |
+
st.markdown("---")
|
| 757 |
+
|
| 758 |
+
# Weekly leaderboard
|
| 759 |
+
st.subheader("๐ Weekly Leaderboard (Top 10)")
|
| 760 |
+
weekly = get_weekly_leaderboard()
|
| 761 |
+
if not weekly:
|
| 762 |
+
st.info("No scores yet this week.")
|
| 763 |
+
else:
|
| 764 |
+
for i, r in enumerate(weekly, 1):
|
| 765 |
+
st.write(f"{i}. {r.get('avatar','๐ฎ')} **{r['name']}** "
|
| 766 |
+
f"(Game {r['game_id']}) โ {r['score']} pts")
|
| 767 |
+
|
| 768 |
+
# ---------- PLAY PAGE ----------
|
| 769 |
+
def play_page():
|
| 770 |
+
gid = st.session_state.get("current_game_id")
|
| 771 |
+
uname = st.session_state.get("username")
|
| 772 |
+
|
| 773 |
+
if not gid or not uname:
|
| 774 |
+
st.error("No active game or username found. Please join or create a game first.")
|
| 775 |
+
return
|
| 776 |
+
|
| 777 |
+
games = unified_get("games") or {}
|
| 778 |
+
players_map = unified_get("players") or {}
|
| 779 |
+
|
| 780 |
+
game = games.get(gid)
|
| 781 |
+
if not game:
|
| 782 |
+
st.error("Game not found.")
|
| 783 |
+
return
|
| 784 |
+
|
| 785 |
+
questions = game.get("questions", [{"question":"Sample Q1","options":["A","B","C","D"],"answer":"A"}])
|
| 786 |
+
if 'question_started_at' not in st.session_state or st.session_state['question_started_at'] is None:
|
| 787 |
+
st.session_state['question_started_at'] = time.time()
|
| 788 |
+
idx = st.session_state.get('current_index', 0)
|
| 789 |
+
if idx >= len(questions):
|
| 790 |
+
st.success("All done โ submit!")
|
| 791 |
+
return
|
| 792 |
+
|
| 793 |
+
q = questions[idx]
|
| 794 |
+
st.subheader(f"Question {idx+1}/{len(questions)}")
|
| 795 |
+
st.write(q["question"])
|
| 796 |
+
choice = st.radio("Choose an answer:", q["options"], key=f"choice_{idx}")
|
| 797 |
+
|
| 798 |
+
col1, col2 = st.columns(2)
|
| 799 |
+
|
| 800 |
+
with col1:
|
| 801 |
+
if st.button("Next", key=f"next_{idx}"):
|
| 802 |
+
answers = st.session_state.get('answers', [""]*len(questions))
|
| 803 |
+
answers[idx] = choice
|
| 804 |
+
st.session_state['answers'] = answers
|
| 805 |
+
st.session_state['current_index'] = idx+1
|
| 806 |
+
st.session_state['question_started_at'] = time.time()
|
| 807 |
+
st.experimental_rerun()
|
| 808 |
+
|
| 809 |
+
with col2:
|
| 810 |
+
if st.button("Submit All", key=f"submit_{idx}"):
|
| 811 |
+
answers = st.session_state.get('answers', [""]*len(questions))
|
| 812 |
+
answers[idx] = choice
|
| 813 |
+
# Update player score in players_map
|
| 814 |
+
players_here = players_map.get(gid, {}) or {}
|
| 815 |
+
players_here[uname] = {
|
| 816 |
+
"submitted": True,
|
| 817 |
+
"score": sum([10 for a in answers]), # dummy scoring
|
| 818 |
+
"percentage": int(sum([10 for a in answers])/len(questions)),
|
| 819 |
+
"avatar": "๐ฎ",
|
| 820 |
+
"correct_flags": ["โ
"]*len(answers)
|
| 821 |
+
}
|
| 822 |
+
players_map[gid] = players_here
|
| 823 |
+
unified_set("players", players_map)
|
| 824 |
+
st.success(f"Submitted! Score: {players_here[uname]['score']}")
|
| 825 |
+
st.session_state['current_index'] = 0
|
| 826 |
+
st.experimental_rerun()
|
| 827 |
+
|
| 828 |
+
def home():
|
| 829 |
st.header("Home")
|
| 830 |
st.write("Create games, invite friends, play and climb the leaderboard.")
|
| 831 |
|
|
|
|
| 1019 |
# ----------------- Create Game -----------------
|
| 1020 |
|
| 1021 |
# ----------------- Play Page -----------------
|
| 1022 |
+
def playe():
|
| 1023 |
# Fetch game info
|
| 1024 |
gid = st.session_state.get('current_game_id')
|
| 1025 |
uname = st.session_state.get('username')
|
|
|
|
| 1144 |
st.session_state['last_score'] = score
|
| 1145 |
st.session_state['last_game'] = gid
|
| 1146 |
st.session_state['current_index'] = 0
|
| 1147 |
+
|
| 1148 |
+
|
| 1149 |
# Friends page
|
| 1150 |
def friends_page():
|
| 1151 |
st.header("Friends")
|