Muthuraja18 commited on
Commit
f0f25b8
·
verified ·
1 Parent(s): 5ffe95d
Files changed (1) hide show
  1. app.py +48 -50
app.py CHANGED
@@ -550,35 +550,28 @@ def create_game_page():
550
  # Here you can call your AI question generator function if you have one
551
 
552
 
553
- def join_game_page():
554
- st.header("Join Game")
555
-
556
- game_id = st.text_input("Enter Game ID")
557
- username = st.text_input("Your Name")
558
- avatar = st.selectbox("Choose Avatar", ["🎮","🤖","🧩","🛡️"])
559
-
560
- if st.button("Join Game"):
561
- if not game_id or not username:
562
- st.warning("Enter both Game ID and Username")
563
- return
564
-
565
- ok, msg = join_game(game_id, username, avatar)
566
- if ok:
567
- st.success(f"Joined game {game_id} successfully!")
568
-
569
- # Safe way to get questions
570
- games = unified_get("games") or {}
571
- g = games.get(game_id, {})
572
-
573
- # Ensure 'questions' key exists
574
- st.session_state['game_questions'] = g.get('questions', [])
575
-
576
- st.session_state['game_id'] = game_id
577
- st.session_state['username'] = username
578
- st.session_state['avatar'] = avatar
579
- else:
580
- st.error(msg)
581
-
582
 
583
  def compute_score(questions, answers, times):
584
  total = 0
@@ -778,28 +771,33 @@ def create_game(topics=None, num_questions=5, auto_close=True, ai_topic=None):
778
  # Join game
779
  def join_game_page():
780
  st.header("Join Game")
781
- gid = st.text_input("Game ID", value=st.session_state.get('game_id',''))
782
- uname = st.text_input("Your name", value=st.session_state.get('username',''))
783
- avatar = st.selectbox("Avatar", ["🎮","🐱","🐶","🦄","👽","🎩"], index=0)
784
- if st.button("Join"):
785
- if not gid or not uname:
786
- st.error("Provide Game ID and name.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
787
  else:
788
- ok, msg = join_game(gid, uname, avatar)
789
- if not ok:
790
- st.error(msg)
791
- else:
792
- st.success("Joined game.")
793
- st.session_state['game_id'] = gid
794
- st.session_state['username'] = uname
795
- st.session_state['avatar'] = avatar
796
- st.session_state['is_host'] = (unified_get("games") or {}).get(gid, {}).get("host") == uname
797
- g = unified_get("games") or {}
798
- st.session_state['game_questions'] = g[gid]['questions']
799
- st.session_state['current_index'] = 0
800
- st.session_state['answers'] = [""] * len(st.session_state['game_questions'])
801
- st.session_state['answer_times'] = [None] * len(st.session_state['game_questions'])
802
- st.rerun()
803
 
804
  # Play page
805
  # ----------------- Create Game -----------------
 
550
  # Here you can call your AI question generator function if you have one
551
 
552
 
553
+ # 1️⃣ Define join_game first
554
+ def join_game(game_id, username, avatar):
555
+ games = unified_get("games") or {}
556
+ if game_id not in games:
557
+ return False, "Invalid Game ID"
558
+ if games[game_id].get("closed"):
559
+ return False, "Game is closed"
560
+ players = unified_get("players") or {}
561
+ game_players = players.get(game_id, {})
562
+ if username in game_players:
563
+ game_players[username]['avatar'] = avatar
564
+ game_players[username]['last_joined'] = now_iso()
565
+ else:
566
+ game_players[username] = {"avatar": avatar, "joined_at": now_iso(), "submitted": False}
567
+ players[game_id] = game_players
568
+ unified_set("players", players)
569
+ ok, msg = claim_session_unified(game_id, username)
570
+ if ok:
571
+ players = unified_get("players") or {}
572
+ players[game_id][username]['last_heartbeat'] = now_iso()
573
+ unified_set("players", players)
574
+ return ok, msg
 
 
 
 
 
 
 
575
 
576
  def compute_score(questions, answers, times):
577
  total = 0
 
771
  # Join game
772
  def join_game_page():
773
  st.header("Join Game")
774
+
775
+ game_id = st.text_input("Enter Game ID")
776
+ username = st.text_input("Your Name")
777
+ avatar = st.selectbox("Choose Avatar", ["🎮","🤖","🧩","🛡️"])
778
+
779
+ if st.button("Join Game"):
780
+ if not game_id or not username:
781
+ st.warning("Enter both Game ID and Username")
782
+ return
783
+
784
+ ok, msg = join_game(game_id, username, avatar)
785
+ if ok:
786
+ st.success(f"Joined game {game_id} successfully!")
787
+
788
+ # Safe way to get questions
789
+ games = unified_get("games") or {}
790
+ g = games.get(game_id, {})
791
+
792
+ # Ensure 'questions' key exists
793
+ st.session_state['game_questions'] = g.get('questions', [])
794
+
795
+ st.session_state['game_id'] = game_id
796
+ st.session_state['username'] = username
797
+ st.session_state['avatar'] = avatar
798
  else:
799
+ st.error(msg)
800
+
 
 
 
 
 
 
 
 
 
 
 
 
 
801
 
802
  # Play page
803
  # ----------------- Create Game -----------------