Files changed (1) hide show
  1. app.py +107 -64
app.py CHANGED
@@ -859,104 +859,147 @@ def home_page():
859
  st.write("Create games, invite friends, play and climb the leaderboard.")
860
 
861
  # ---------------- TOTAL ONLINE PLAYERS ----------------
862
- total_online = get_total_online_players()
 
863
  st.metric("๐ŸŸข Players Online", total_online)
864
 
865
  # ---------------- SHOW LAST SCORE ----------------
866
- last_score = st.session_state.get('last_score')
867
- last_game = st.session_state.get('last_game')
868
- if last_score is not None and last_game:
869
  st.success(f"Your last score: {last_score} (Game {last_game})")
870
 
871
  # ---------------- LOAD GAMES ----------------
872
  games = unified_get("games") or {}
873
- if not games:
874
- st.info("No games exist yet. Please create a game first.")
875
- return
 
 
 
 
876
 
877
- st.subheader("Recent Games")
878
- for g in sorted(games.values(), key=lambda x: x.get("created_at", ""), reverse=True)[:10]:
879
  gid = g.get("game_id")
880
- st.markdown(f"### ๐ŸŽฎ Game: **{gid}** {'(Closed)' if g.get('closed') else ''}")
 
 
 
 
 
881
  st.write(f"Host: {g.get('host')} โ€” Topics: {', '.join(g.get('topics', []))}")
882
  st.write(f"Created: {g.get('created_at')}")
883
 
884
- # Players & submissions
885
- players = g.get("players", [])
886
- submissions = g.get("submissions", {})
887
 
888
- st.write(f"Players joined: **{len(players)}**")
889
- st.write(f"Submitted: **{len(submissions)} / {len(players)}**")
890
 
891
- # Top 3 players
892
  if submissions:
893
  st.markdown("๐Ÿ† **Top 3 Players**")
894
- top_players = sorted(submissions.items(), key=lambda x: x[1], reverse=True)[:3]
895
- for i, (uname, score) in enumerate(top_players, start=1):
896
- st.write(f"{i}. ๐ŸŽฎ **{uname}** โ€” {score} pts")
 
 
 
 
 
897
 
898
- # Player status
899
  if players:
900
  st.markdown("**๐Ÿ‘ฅ Player Status**")
901
- for uname in players:
902
- if uname in submissions:
903
- st.write(f"๐ŸŽฎ **{uname}** โ€” โœ… Submitted ({submissions[uname]} pts)")
 
 
904
  else:
905
- st.write(f"๐ŸŽฎ **{uname}** โ€” โณ Playing")
906
 
907
  st.markdown("---")
908
 
909
- # Invite & challenge friends
910
- if not g.get("closed") and st.session_state.get("username"):
911
  st.info(f"Share this Game ID: {gid}")
912
  render_copy_button(gid, gid)
913
 
914
- if st.button(f"Invite friends to {gid}", key=f"invite_{gid}"):
915
- friends = get_friends_map().get(st.session_state["username"], [])
916
- if not friends:
917
- st.warning("No friends to invite.")
918
- else:
919
- for f in friends:
920
- send_game_invite(st.session_state["username"], f, gid)
921
- st.success("Invites sent to friends.")
922
-
923
- if st.button(f"Challenge friends with a new game like {gid}", key=f"challenge_{gid}"):
924
- new_id = create_game(
925
- host=st.session_state.get("username", "Host"),
926
- topics=g.get("topics", []),
927
- num_questions=len(g.get("questions", []))
928
- )
929
- st.success(f"Challenge created: {new_id}")
930
- st.session_state['active_game_id'] = new_id
931
- st.session_state['game_questions'] = games.get(new_id, {}).get("questions", [])
932
- st.experimental_rerun()
 
 
 
 
 
 
 
 
 
 
 
 
933
 
934
  # ---------------- WEEKLY LEADERBOARD ----------------
935
  st.subheader("๐Ÿ† Weekly Leaderboard (Top 10)")
936
  weekly = get_weekly_leaderboard()
 
937
  if not weekly:
938
  st.info("No scores yet this week.")
939
  else:
940
  for i, r in enumerate(weekly, 1):
941
- st.write(f"{i}. {r.get('avatar','๐ŸŽฎ')} **{r['name']}** (Game {r['game_id']}) โ€” {r['score']} pts")
942
-
943
- # ---------------- UPDATE ACTIVE GAME SUBMISSIONS ----------------
944
- active_game_id = st.session_state.get('active_game')
945
- username = st.session_state.get('username')
946
- if active_game_id and 'final_score' in st.session_state and username:
947
- games = unified_get("games") or {}
948
- game = games.get(active_game_id, {})
949
- if game:
950
- players = game.get("players", [])
951
- submissions = game.get("submissions", {})
952
-
953
- if username not in players:
954
- players.append(username)
955
- submissions[username] = st.session_state['final_score']
956
-
957
- game["players"] = players
958
- game["submissions"] = submissions
959
- unified_set("games", {active_game_id: game})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
960
 
961
 
962
  # -------------------------
 
859
  st.write("Create games, invite friends, play and climb the leaderboard.")
860
 
861
  # ---------------- TOTAL ONLINE PLAYERS ----------------
862
+ players_map = unified_get("players") or {}
863
+ total_online = sum(len(v) for v in players_map.values())
864
  st.metric("๐ŸŸข Players Online", total_online)
865
 
866
  # ---------------- SHOW LAST SCORE ----------------
867
+ last_score = st.session_state.get("last_score")
868
+ last_game = st.session_state.get("last_game")
869
+ if last_score is not None:
870
  st.success(f"Your last score: {last_score} (Game {last_game})")
871
 
872
  # ---------------- LOAD GAMES ----------------
873
  games = unified_get("games") or {}
874
+ st.subheader("Recent games")
875
+
876
+ for g in sorted(
877
+ games.values(),
878
+ key=lambda x: x.get("created_at", ""),
879
+ reverse=True
880
+ )[:10]:
881
 
 
 
882
  gid = g.get("game_id")
883
+ players = g.get("players", [])
884
+ submissions = g.get("submissions", {})
885
+
886
+ st.markdown(
887
+ f"### ๐ŸŽฎ Game: **{gid}** {'(Closed)' if g.get('closed') else ''}"
888
+ )
889
  st.write(f"Host: {g.get('host')} โ€” Topics: {', '.join(g.get('topics', []))}")
890
  st.write(f"Created: {g.get('created_at')}")
891
 
892
+ joined = len(players)
893
+ submitted = len(submissions)
 
894
 
895
+ st.write(f"Players joined: **{joined}**")
896
+ st.write(f"Submitted: **{submitted} / {joined}**")
897
 
898
+ # ---------------- TOP 3 PLAYERS ----------------
899
  if submissions:
900
  st.markdown("๐Ÿ† **Top 3 Players**")
901
+ top_players = sorted(
902
+ submissions.items(),
903
+ key=lambda x: x[1],
904
+ reverse=True
905
+ )[:3]
906
+
907
+ for i, (uname_p, score) in enumerate(top_players, start=1):
908
+ st.write(f"{i}. ๐ŸŽฎ **{uname_p}** โ€” {score} pts")
909
 
910
+ # ---------------- PLAYER STATUS ----------------
911
  if players:
912
  st.markdown("**๐Ÿ‘ฅ Player Status**")
913
+ for uname_p in players:
914
+ if uname_p in submissions:
915
+ st.write(
916
+ f"๐ŸŽฎ **{uname_p}** โ€” โœ… Submitted ({submissions[uname_p]} pts)"
917
+ )
918
  else:
919
+ st.write(f"๐ŸŽฎ **{uname_p}** โ€” โณ Playing")
920
 
921
  st.markdown("---")
922
 
923
+ # ---------------- INVITE & CHALLENGE ----------------
924
+ if not g.get("closed"):
925
  st.info(f"Share this Game ID: {gid}")
926
  render_copy_button(gid, gid)
927
 
928
+ if st.session_state.get("username"):
929
+ if st.button(f"Invite your friends to {gid}", key=f"invite_{gid}"):
930
+ friends = get_friends_map().get(
931
+ st.session_state["username"], []
932
+ )
933
+ if not friends:
934
+ st.warning("No friends to invite.")
935
+ else:
936
+ for f in friends:
937
+ send_game_invite(
938
+ st.session_state["username"], f, gid
939
+ )
940
+ st.success("Invites sent to friends.")
941
+
942
+ if st.button(
943
+ f"Challenge friends with a new game like {gid}",
944
+ key=f"challenge_{gid}"
945
+ ):
946
+ new_id = create_game(
947
+ st.session_state.get("username", "Host"),
948
+ g.get("topics", []),
949
+ num_questions=len(g.get("questions", []))
950
+ )
951
+
952
+ st.session_state["active_game_id"] = new_id
953
+ st.session_state["game_questions"] = games.get(
954
+ new_id, {}
955
+ ).get("questions", [])
956
+
957
+ st.success(f"Challenge created: {new_id}")
958
+ st.experimental_rerun()
959
 
960
  # ---------------- WEEKLY LEADERBOARD ----------------
961
  st.subheader("๐Ÿ† Weekly Leaderboard (Top 10)")
962
  weekly = get_weekly_leaderboard()
963
+
964
  if not weekly:
965
  st.info("No scores yet this week.")
966
  else:
967
  for i, r in enumerate(weekly, 1):
968
+ st.write(
969
+ f"{i}. {r.get('avatar', '๐ŸŽฎ')} "
970
+ f"**{r['name']}** (Game {r['game_id']}) โ€” {r['score']} pts"
971
+ )
972
+
973
+ # ---------------- PLAY PAGE SETUP ----------------
974
+ if "active_game" not in st.session_state:
975
+ if games:
976
+ st.session_state["active_game"] = list(games.keys())[0]
977
+ else:
978
+ st.warning("No games exist yet. Please create a game first.")
979
+
980
+ if "username" not in st.session_state:
981
+ st.session_state["username"] = "Guest"
982
+
983
+ # ---------------- SAFE SCORE UPDATE ----------------
984
+ active_game_id = st.session_state.get("active_game")
985
+
986
+ if active_game_id and "final_score" in st.session_state:
987
+ game = unified_get(f"games/{active_game_id}") or {}
988
+ players = game.get("players", [])
989
+ submissions = game.get("submissions", {})
990
+
991
+ username = st.session_state["username"]
992
+
993
+ if username not in players:
994
+ players.append(username)
995
+
996
+ submissions[username] = st.session_state["final_score"]
997
+
998
+ game["players"] = players
999
+ game["submissions"] = submissions
1000
+
1001
+ unified_set(f"games/{active_game_id}", game)
1002
+
1003
 
1004
 
1005
  # -------------------------