Spaces:
Running
Running
Update app.py (#96)
Browse files- Update app.py (98479d5f7bfaebb8e72dcd9496b39f64d8e3659c)
app.py
CHANGED
|
@@ -921,83 +921,84 @@ def create_game(host=None, topics=[], num_questions=5, auto_close=True, ai_topic
|
|
| 921 |
# PLAY PAGE
|
| 922 |
# -------------------------
|
| 923 |
def play_page():
|
|
|
|
|
|
|
|
|
|
| 924 |
gid = st.session_state.get("active_game_id")
|
| 925 |
uname = st.session_state.get("username")
|
|
|
|
| 926 |
if not gid or not uname:
|
| 927 |
st.error("No active game or username found. Please join or create a game first.")
|
| 928 |
return
|
| 929 |
|
| 930 |
-
#
|
| 931 |
games = unified_get("games") or {}
|
| 932 |
game = games.get(gid)
|
| 933 |
if not game:
|
| 934 |
st.error("Game not found.")
|
| 935 |
return
|
| 936 |
-
if game.get(
|
| 937 |
-
st.warning("This game
|
| 938 |
return
|
| 939 |
|
| 940 |
questions = game.get("questions", [])
|
| 941 |
if not questions:
|
| 942 |
-
st.info("No questions loaded.")
|
| 943 |
return
|
| 944 |
|
| 945 |
-
# Initialize session state
|
| 946 |
-
if
|
| 947 |
-
st.session_state["
|
| 948 |
-
if
|
| 949 |
-
st.session_state[
|
| 950 |
-
if
|
| 951 |
-
st.session_state[
|
| 952 |
-
if
|
| 953 |
-
st.session_state[
|
| 954 |
|
| 955 |
-
idx = st.session_state[
|
| 956 |
|
| 957 |
-
#
|
| 958 |
if idx >= len(questions):
|
| 959 |
st.success("All done — submit your answers!")
|
| 960 |
return
|
| 961 |
|
| 962 |
-
# ----------------
|
| 963 |
q = questions[idx]
|
| 964 |
-
st.subheader(f"Question {idx+1}/{len(questions)}")
|
| 965 |
st.write(q["question"])
|
| 966 |
-
|
|
|
|
|
|
|
|
|
|
| 967 |
time_limit = 15
|
| 968 |
st.markdown(f"**Time left:** {max(0, time_limit - elapsed)} seconds")
|
| 969 |
|
| 970 |
-
#
|
| 971 |
choice = st.radio(
|
| 972 |
"Choose an answer:",
|
| 973 |
q["options"],
|
| 974 |
-
index=st.session_state['answers'][idx] if st.session_state['answers'][idx] in q["options"] else 0,
|
| 975 |
key=f"choice_{gid}_{idx}"
|
| 976 |
)
|
| 977 |
|
| 978 |
col1, col2 = st.columns(2)
|
| 979 |
|
| 980 |
-
# ----------------
|
| 981 |
with col1:
|
| 982 |
if st.button("Next", key=f"next_{gid}_{idx}"):
|
| 983 |
-
# Record answer and time
|
| 984 |
st.session_state['answers'][idx] = choice
|
| 985 |
-
start_time = st.session_state.get('question_started_at') or time.time()
|
| 986 |
st.session_state['answer_times'][idx] = time.time() - start_time
|
| 987 |
-
|
| 988 |
-
# st.session_state['answer_times'][idx] = time.time() - st.session_state['question_started_at']
|
| 989 |
-
# Move to next question
|
| 990 |
st.session_state['current_index'] = idx + 1
|
| 991 |
st.session_state['question_started_at'] = time.time()
|
| 992 |
st.rerun()
|
| 993 |
|
| 994 |
-
# ----------------
|
| 995 |
with col2:
|
| 996 |
if idx == len(questions) - 1:
|
| 997 |
if st.button("Submit All Answers", key=f"submit_{gid}_{idx}"):
|
| 998 |
# Record last question
|
| 999 |
st.session_state['answers'][idx] = choice
|
| 1000 |
-
st.session_state['answer_times'][idx] = time.time() -
|
| 1001 |
|
| 1002 |
answers = st.session_state['answers']
|
| 1003 |
times = st.session_state['answer_times']
|
|
@@ -1006,7 +1007,7 @@ def play_page():
|
|
| 1006 |
score, flags = compute_score(questions, answers, times)
|
| 1007 |
percentage = int(score / (len(questions) * 15) * 100)
|
| 1008 |
|
| 1009 |
-
# Update players dict
|
| 1010 |
players = unified_get("players") or {}
|
| 1011 |
players.setdefault(gid, {})
|
| 1012 |
players[gid][uname] = {
|
|
@@ -1019,7 +1020,7 @@ def play_page():
|
|
| 1019 |
}
|
| 1020 |
unified_set("players", players)
|
| 1021 |
|
| 1022 |
-
# Push
|
| 1023 |
row = {
|
| 1024 |
"name": uname,
|
| 1025 |
"avatar": st.session_state.get("avatar", "🎮"),
|
|
@@ -1049,9 +1050,6 @@ def play_page():
|
|
| 1049 |
st.session_state['answers'] = []
|
| 1050 |
st.session_state['answer_times'] = []
|
| 1051 |
st.rerun()
|
| 1052 |
-
|
| 1053 |
-
|
| 1054 |
-
|
| 1055 |
# Join game
|
| 1056 |
def join_game_page():
|
| 1057 |
st.header("Join Game")
|
|
|
|
| 921 |
# PLAY PAGE
|
| 922 |
# -------------------------
|
| 923 |
def play_page():
|
| 924 |
+
import time
|
| 925 |
+
import streamlit as st
|
| 926 |
+
|
| 927 |
gid = st.session_state.get("active_game_id")
|
| 928 |
uname = st.session_state.get("username")
|
| 929 |
+
|
| 930 |
if not gid or not uname:
|
| 931 |
st.error("No active game or username found. Please join or create a game first.")
|
| 932 |
return
|
| 933 |
|
| 934 |
+
# Get game and questions
|
| 935 |
games = unified_get("games") or {}
|
| 936 |
game = games.get(gid)
|
| 937 |
if not game:
|
| 938 |
st.error("Game not found.")
|
| 939 |
return
|
| 940 |
+
if game.get('closed'):
|
| 941 |
+
st.warning("This game is closed.")
|
| 942 |
return
|
| 943 |
|
| 944 |
questions = game.get("questions", [])
|
| 945 |
if not questions:
|
| 946 |
+
st.info("No questions loaded for this game.")
|
| 947 |
return
|
| 948 |
|
| 949 |
+
# Initialize session state for answers and times
|
| 950 |
+
if 'answers' not in st.session_state:
|
| 951 |
+
st.session_state['answers'] = [""] * len(questions)
|
| 952 |
+
if 'answer_times' not in st.session_state:
|
| 953 |
+
st.session_state['answer_times'] = [0] * len(questions)
|
| 954 |
+
if 'current_index' not in st.session_state:
|
| 955 |
+
st.session_state['current_index'] = 0
|
| 956 |
+
if 'question_started_at' not in st.session_state:
|
| 957 |
+
st.session_state['question_started_at'] = time.time()
|
| 958 |
|
| 959 |
+
idx = st.session_state['current_index']
|
| 960 |
|
| 961 |
+
# All done
|
| 962 |
if idx >= len(questions):
|
| 963 |
st.success("All done — submit your answers!")
|
| 964 |
return
|
| 965 |
|
| 966 |
+
# ---------------- QUESTION UI ----------------
|
| 967 |
q = questions[idx]
|
| 968 |
+
st.subheader(f"Question {idx + 1}/{len(questions)}")
|
| 969 |
st.write(q["question"])
|
| 970 |
+
|
| 971 |
+
# Safe elapsed calculation
|
| 972 |
+
start_time = st.session_state.get('question_started_at') or time.time()
|
| 973 |
+
elapsed = int(time.time() - start_time)
|
| 974 |
time_limit = 15
|
| 975 |
st.markdown(f"**Time left:** {max(0, time_limit - elapsed)} seconds")
|
| 976 |
|
| 977 |
+
# Stable radio buttons
|
| 978 |
choice = st.radio(
|
| 979 |
"Choose an answer:",
|
| 980 |
q["options"],
|
|
|
|
| 981 |
key=f"choice_{gid}_{idx}"
|
| 982 |
)
|
| 983 |
|
| 984 |
col1, col2 = st.columns(2)
|
| 985 |
|
| 986 |
+
# ---------------- NEXT ----------------
|
| 987 |
with col1:
|
| 988 |
if st.button("Next", key=f"next_{gid}_{idx}"):
|
|
|
|
| 989 |
st.session_state['answers'][idx] = choice
|
|
|
|
| 990 |
st.session_state['answer_times'][idx] = time.time() - start_time
|
|
|
|
|
|
|
|
|
|
| 991 |
st.session_state['current_index'] = idx + 1
|
| 992 |
st.session_state['question_started_at'] = time.time()
|
| 993 |
st.rerun()
|
| 994 |
|
| 995 |
+
# ---------------- SUBMIT ----------------
|
| 996 |
with col2:
|
| 997 |
if idx == len(questions) - 1:
|
| 998 |
if st.button("Submit All Answers", key=f"submit_{gid}_{idx}"):
|
| 999 |
# Record last question
|
| 1000 |
st.session_state['answers'][idx] = choice
|
| 1001 |
+
st.session_state['answer_times'][idx] = time.time() - start_time
|
| 1002 |
|
| 1003 |
answers = st.session_state['answers']
|
| 1004 |
times = st.session_state['answer_times']
|
|
|
|
| 1007 |
score, flags = compute_score(questions, answers, times)
|
| 1008 |
percentage = int(score / (len(questions) * 15) * 100)
|
| 1009 |
|
| 1010 |
+
# ---------------- Update players dict ----------------
|
| 1011 |
players = unified_get("players") or {}
|
| 1012 |
players.setdefault(gid, {})
|
| 1013 |
players[gid][uname] = {
|
|
|
|
| 1020 |
}
|
| 1021 |
unified_set("players", players)
|
| 1022 |
|
| 1023 |
+
# ---------------- Push leaderboard ----------------
|
| 1024 |
row = {
|
| 1025 |
"name": uname,
|
| 1026 |
"avatar": st.session_state.get("avatar", "🎮"),
|
|
|
|
| 1050 |
st.session_state['answers'] = []
|
| 1051 |
st.session_state['answer_times'] = []
|
| 1052 |
st.rerun()
|
|
|
|
|
|
|
|
|
|
| 1053 |
# Join game
|
| 1054 |
def join_game_page():
|
| 1055 |
st.header("Join Game")
|