Muthuraja18 commited on
Commit
40343d3
·
verified ·
1 Parent(s): f0f25b8
Files changed (1) hide show
  1. app.py +25 -15
app.py CHANGED
@@ -557,22 +557,30 @@ def join_game(game_id, username, avatar):
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
578
  flags = []
@@ -729,15 +737,9 @@ def home_page():
729
 
730
  # Create game
731
  def create_game(topics=None, num_questions=5, auto_close=True, ai_topic=None):
732
- """
733
- Creates a game, saves it, and returns a unique game ID.
734
- """
735
  topics = topics or []
736
 
737
- # Generate unique game ID
738
  gid = f"GAME{int(time.time())}{random.randint(100,999)}"
739
-
740
- # Prepare game dict
741
  game_dict = {
742
  "game_id": gid,
743
  "host": st.session_state.get('username', 'Host'),
@@ -750,24 +752,32 @@ def create_game(topics=None, num_questions=5, auto_close=True, ai_topic=None):
750
  "closed": False
751
  }
752
 
753
- # Optional: Add AI-generated questions
754
  if ai_topic:
755
  ai_questions = generate_ai_questions(ai_topic, n=num_questions)
756
- game_dict["questions"].extend(ai_questions)
 
 
 
 
 
 
 
 
 
 
 
757
 
758
- # Load current games
759
  games = unified_get("games") or {}
760
  games[gid] = game_dict
761
-
762
- # Persist games
763
  unified_set("games", games)
764
-
765
- # Also save in session_state for fast access
766
  st.session_state['games'] = games
767
 
768
  return gid
769
 
770
 
 
771
  # Join game
772
  def join_game_page():
773
  st.header("Join Game")
 
557
  return False, "Invalid Game ID"
558
  if games[game_id].get("closed"):
559
  return False, "Game is closed"
560
+
561
  players = unified_get("players") or {}
562
+ if game_id not in players:
563
+ players[game_id] = {}
564
+
565
+ game_players = players[game_id]
566
+
567
  if username in game_players:
568
  game_players[username]['avatar'] = avatar
569
  game_players[username]['last_joined'] = now_iso()
570
  else:
571
  game_players[username] = {"avatar": avatar, "joined_at": now_iso(), "submitted": False}
572
+
573
  players[game_id] = game_players
574
  unified_set("players", players)
575
+
576
  ok, msg = claim_session_unified(game_id, username)
577
  if ok:
 
578
  players[game_id][username]['last_heartbeat'] = now_iso()
579
  unified_set("players", players)
580
+
581
  return ok, msg
582
 
583
+
584
  def compute_score(questions, answers, times):
585
  total = 0
586
  flags = []
 
737
 
738
  # Create game
739
  def create_game(topics=None, num_questions=5, auto_close=True, ai_topic=None):
 
 
 
740
  topics = topics or []
741
 
 
742
  gid = f"GAME{int(time.time())}{random.randint(100,999)}"
 
 
743
  game_dict = {
744
  "game_id": gid,
745
  "host": st.session_state.get('username', 'Host'),
 
752
  "closed": False
753
  }
754
 
755
+ # Add AI-generated questions if provided
756
  if ai_topic:
757
  ai_questions = generate_ai_questions(ai_topic, n=num_questions)
758
+ if ai_questions:
759
+ game_dict["questions"].extend(ai_questions)
760
+ else:
761
+ st.warning(f"No AI questions generated for topic '{ai_topic}'. Using static topics if any.")
762
+
763
+ # Add static questions if topics provided
764
+ for topic in topics:
765
+ qs = questions_db.get(topic, [])[:num_questions]
766
+ game_dict["questions"].extend(qs)
767
+
768
+ # Shuffle questions
769
+ random.shuffle(game_dict["questions"])
770
 
771
+ # Save
772
  games = unified_get("games") or {}
773
  games[gid] = game_dict
 
 
774
  unified_set("games", games)
 
 
775
  st.session_state['games'] = games
776
 
777
  return gid
778
 
779
 
780
+
781
  # Join game
782
  def join_game_page():
783
  st.header("Join Game")