Muthuraja18 commited on
Commit
bac8e62
·
verified ·
1 Parent(s): d298bb6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -47
app.py CHANGED
@@ -921,23 +921,25 @@ def create_game(host=None, topics=[], num_questions=5, auto_close=True, ai_topic
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
 
@@ -946,35 +948,33 @@ def play_page():
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"],
@@ -986,28 +986,29 @@ def play_page():
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']
 
 
 
 
1005
 
1006
  # Compute score
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,37 +1021,35 @@ def play_page():
1020
  }
1021
  unified_set("players", players)
1022
 
1023
- # ---------------- Push leaderboard ----------------
1024
  row = {
1025
  "name": uname,
1026
- "avatar": st.session_state.get("avatar", "🎮"),
1027
  "score": score,
1028
- "percentage": percentage,
1029
  "game_id": gid,
1030
- "topics": game.get("topics", []),
1031
  "timestamp": now_iso(),
 
1032
  "questions": len(questions),
1033
- "answers": answers,
1034
- "correct_flags": flags
1035
  }
 
1036
  unified_push_leaderboard(row)
1037
  save_score_to_csv(row)
1038
 
1039
- # Auto-close game if needed
1040
- if game.get('auto_close', True):
1041
- games[gid]['closed'] = True
1042
- games[gid]['closed_at'] = now_iso()
1043
  unified_set("games", games)
1044
 
1045
- # Reset session state
1046
- st.success(f"Submitted! Score: {score} / {len(questions)*15} ({percentage}%)")
1047
  st.balloons()
1048
- st.session_state['last_score'] = score
1049
- st.session_state['last_game'] = gid
1050
- st.session_state['current_index'] = 0
1051
- st.session_state['answers'] = []
1052
- st.session_state['answer_times'] = []
1053
  st.rerun()
 
1054
  # Join game
1055
  def join_game_page():
1056
  st.header("Join Game")
 
921
  # PLAY PAGE
922
  # -------------------------
923
  def play_page():
924
+ import time, json
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
+ # ---------------- Load game ----------------
935
  games = unified_get("games") or {}
936
  game = games.get(gid)
937
+
938
  if not game:
939
  st.error("Game not found.")
940
  return
941
+
942
+ if game.get("closed"):
943
  st.warning("This game is closed.")
944
  return
945
 
 
948
  st.info("No questions loaded for this game.")
949
  return
950
 
951
+ # ---------------- Init session state ----------------
952
+ if "answers" not in st.session_state:
953
+ st.session_state.answers = [""] * len(questions)
954
+ if "answer_times" not in st.session_state:
955
+ st.session_state.answer_times = [0] * len(questions)
956
+ if "current_index" not in st.session_state:
957
+ st.session_state.current_index = 0
958
+ if "question_started_at" not in st.session_state:
959
+ st.session_state.question_started_at = time.time()
960
 
961
+ idx = st.session_state.current_index
962
 
963
+ # ---------------- All done ----------------
964
  if idx >= len(questions):
965
  st.success("All done — submit your answers!")
966
  return
967
 
968
+ # ---------------- Question UI ----------------
969
  q = questions[idx]
970
  st.subheader(f"Question {idx + 1}/{len(questions)}")
971
  st.write(q["question"])
972
 
973
+ start_time = st.session_state.question_started_at
 
974
  elapsed = int(time.time() - start_time)
975
  time_limit = 15
976
+ st.markdown(f"**⏱ Time left:** {max(0, time_limit - elapsed)} seconds")
977
 
 
978
  choice = st.radio(
979
  "Choose an answer:",
980
  q["options"],
 
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 += 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
 
1000
+ # Save last answer
1001
+ st.session_state.answers[idx] = choice
1002
+ st.session_state.answer_times[idx] = time.time() - start_time
1003
+
1004
+ answers = st.session_state.answers
1005
+ times = st.session_state.answer_times
1006
 
1007
  # Compute score
1008
  score, flags = compute_score(questions, answers, times)
1009
  percentage = int(score / (len(questions) * 15) * 100)
1010
 
1011
+ # ---------------- Update players ----------------
1012
  players = unified_get("players") or {}
1013
  players.setdefault(gid, {})
1014
  players[gid][uname] = {
 
1021
  }
1022
  unified_set("players", players)
1023
 
1024
+ # ---------------- Save leaderboard ----------------
1025
  row = {
1026
  "name": uname,
 
1027
  "score": score,
 
1028
  "game_id": gid,
1029
+ "topics": ",".join(game.get("topics", [])),
1030
  "timestamp": now_iso(),
1031
+ "avatar": st.session_state.get("avatar", "🎮"),
1032
  "questions": len(questions),
1033
+ "answers": json.dumps(answers),
1034
+ "correct_flags": json.dumps(flags),
1035
  }
1036
+
1037
  unified_push_leaderboard(row)
1038
  save_score_to_csv(row)
1039
 
1040
+ # ---------------- Auto close game ----------------
1041
+ if game.get("auto_close", True):
1042
+ games[gid]["closed"] = True
1043
+ games[gid]["closed_at"] = now_iso()
1044
  unified_set("games", games)
1045
 
1046
+ st.success(f"🎉 Submitted! Score: {score} / {len(questions)*15} ({percentage}%)")
 
1047
  st.balloons()
1048
+
1049
+ # Reset state
1050
+ st.session_state.clear()
 
 
1051
  st.rerun()
1052
+
1053
  # Join game
1054
  def join_game_page():
1055
  st.header("Join Game")