Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -71,8 +71,16 @@ def create_game():
|
|
| 71 |
if selected_topics:
|
| 72 |
# Collect questions from the selected topics
|
| 73 |
questions = []
|
|
|
|
|
|
|
|
|
|
| 74 |
for topic in selected_topics:
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
random.shuffle(questions) # Shuffle questions
|
| 78 |
|
|
@@ -95,192 +103,6 @@ def create_game():
|
|
| 95 |
else:
|
| 96 |
st.error("Please select at least one topic.")
|
| 97 |
|
| 98 |
-
# Function to join a game
|
| 99 |
-
def join_game():
|
| 100 |
-
game_id = st.text_input("Enter Game ID to join:")
|
| 101 |
-
|
| 102 |
-
if game_id:
|
| 103 |
-
with open(GAME_DATA_FILE, "r") as file:
|
| 104 |
-
all_games = json.load(file)
|
| 105 |
-
|
| 106 |
-
if game_id in all_games:
|
| 107 |
-
game_data = all_games[game_id]
|
| 108 |
-
st.session_state['game_data'] = game_data
|
| 109 |
-
st.success(f"Joined game with ID: {game_id}")
|
| 110 |
-
start_game(game_data) # Start the game with the joined data
|
| 111 |
-
else:
|
| 112 |
-
st.error("Invalid Game ID. Please check and try again.")
|
| 113 |
-
|
| 114 |
-
# Function to start the game
|
| 115 |
-
def start_game(game_data):
|
| 116 |
-
topics = game_data['topics']
|
| 117 |
-
questions = game_data['questions']
|
| 118 |
-
|
| 119 |
-
username = st.text_input("Enter your name:")
|
| 120 |
-
avatar = st.selectbox("Choose your Avatar", ["🐱", "🐶", "🦄", "👽", "🎮"])
|
| 121 |
-
st.session_state['avatar'] = avatar
|
| 122 |
-
|
| 123 |
-
if username:
|
| 124 |
-
if 'answers' not in st.session_state:
|
| 125 |
-
st.session_state['answers'] = [""] * len(questions)
|
| 126 |
-
|
| 127 |
-
score = 0
|
| 128 |
-
current_question = 0
|
| 129 |
-
while current_question < len(questions):
|
| 130 |
-
question, options, correct_answer = questions[current_question]
|
| 131 |
-
topic = next(topic for topic in topics if any(q[0] == question for q in questions_db[topic]))
|
| 132 |
-
|
| 133 |
-
if current_question not in st.session_state.get('options', {}):
|
| 134 |
-
st.session_state['options'] = {current_question: options}
|
| 135 |
-
|
| 136 |
-
options = st.session_state['options'][current_question]
|
| 137 |
-
answer = st.radio(f"Question {current_question+1}: {question}", options, key=f"q{current_question}")
|
| 138 |
-
st.session_state['answers'][current_question] = answer
|
| 139 |
-
|
| 140 |
-
with st.empty():
|
| 141 |
-
timer_text = st.empty()
|
| 142 |
-
for time_left in range(10, 0, -1):
|
| 143 |
-
timer_text.text(f"Time left for this question: {time_left} seconds")
|
| 144 |
-
time.sleep(1)
|
| 145 |
-
|
| 146 |
-
# Display Correct or Incorrect after the timer ends
|
| 147 |
-
if answer == correct_answer:
|
| 148 |
-
st.write("Correct!")
|
| 149 |
-
score += 10
|
| 150 |
-
else:
|
| 151 |
-
st.write(f"Incorrect! The correct answer is: {correct_answer}")
|
| 152 |
-
|
| 153 |
-
current_question += 1
|
| 154 |
-
|
| 155 |
-
submit_button = st.button("Submit Answers")
|
| 156 |
-
if submit_button:
|
| 157 |
-
leaderboard_df = load_leaderboard()
|
| 158 |
-
new_row = pd.DataFrame({
|
| 159 |
-
'name': [username],
|
| 160 |
-
'score': [score],
|
| 161 |
-
'question': [', '.join([q[0] for q in questions])],
|
| 162 |
-
'answer': [', '.join(st.session_state['answers'])],
|
| 163 |
-
'correct': [', '.join(['Yes' if st.session_state['answers'][i].strip().lower() == questions[i][2].lower() else 'No' for i in range(len(st.session_state['answers']))])],
|
| 164 |
-
'topic': [', '.join(topics)],
|
| 165 |
-
'avatar': [avatar]
|
| 166 |
-
})
|
| 167 |
-
|
| 168 |
-
leaderboard_df = pd.concat([leaderboard_df, new_row], ignore_index=True)
|
| 169 |
-
leaderboard_df = leaderboard_df.sort_values(by='score', ascending=False).reset_index(drop=True)
|
| 170 |
-
save_leaderboard(leaderboard_df)
|
| 171 |
-
|
| 172 |
-
st.success(f"Game Over! Your final score is {score}")
|
| 173 |
-
|
| 174 |
-
# Function to display the leaderboard with animations
|
| 175 |
-
def display_dashboard():
|
| 176 |
-
leaderboard_df = load_leaderboard()
|
| 177 |
-
|
| 178 |
-
st.subheader("Top 3 Players")
|
| 179 |
-
top_3 = leaderboard_df.head(3)
|
| 180 |
-
|
| 181 |
-
# First Place Winner - Celebration Animation
|
| 182 |
-
first_place = top_3.iloc[0] if not leaderboard_df.empty else None
|
| 183 |
-
if first_place is not None:
|
| 184 |
-
st.write(f"🏆 {first_place['name']} - {first_place['score']} points! 🎉 {first_place['avatar']}")
|
| 185 |
-
st.markdown("<h3 style='color: gold;'>🎉 Congratulations on First Place! 🎉</h3>", unsafe_allow_html=True)
|
| 186 |
-
html_code = f"""
|
| 187 |
-
<div style="text-align: center;">
|
| 188 |
-
<h2>🏆 Winner: {first_place['name']} 🏆</h2>
|
| 189 |
-
<p>Score: {first_place['score']}</p>
|
| 190 |
-
<p>Avatar: {first_place['avatar']}</p>
|
| 191 |
-
</div>
|
| 192 |
-
<div id="balloon" style="position: absolute; top: 50%; left: 50%; animation: float 2s infinite;">
|
| 193 |
-
🎈🎉
|
| 194 |
-
</div>
|
| 195 |
-
<style>
|
| 196 |
-
@keyframes float {{
|
| 197 |
-
0% {{ transform: translateY(0); }}
|
| 198 |
-
50% {{ transform: translateY(-50px); }}
|
| 199 |
-
100% {{ transform: translateY(0); }}
|
| 200 |
-
}}
|
| 201 |
-
</style>
|
| 202 |
-
"""
|
| 203 |
-
html(html_code)
|
| 204 |
-
|
| 205 |
-
# Second Place Winner - Celebration Animation
|
| 206 |
-
if len(top_3) > 1:
|
| 207 |
-
second_place = top_3.iloc[1]
|
| 208 |
-
st.write(f"🥈 {second_place['name']} - {second_place['score']} points! {second_place['avatar']}")
|
| 209 |
-
st.markdown("<h3 style='color: silver;'>👏 Congratulations on Second Place! 👏</h3>", unsafe_allow_html=True)
|
| 210 |
-
html_code = f"""
|
| 211 |
-
<div style="text-align: center;">
|
| 212 |
-
<h2>🥈 Runner-up: {second_place['name']} 🥈</h2>
|
| 213 |
-
<p>Score: {second_place['score']}</p>
|
| 214 |
-
<p>Avatar: {second_place['avatar']}</p>
|
| 215 |
-
</div>
|
| 216 |
-
<div style="animation: clap 1s linear infinite;">
|
| 217 |
-
👏👏👏
|
| 218 |
-
</div>
|
| 219 |
-
<style>
|
| 220 |
-
@keyframes clap {{
|
| 221 |
-
0% {{ transform: scale(1); }}
|
| 222 |
-
50% {{ transform: scale(1.2); }}
|
| 223 |
-
100% {{ transform: scale(1); }}
|
| 224 |
-
}}
|
| 225 |
-
</style>
|
| 226 |
-
"""
|
| 227 |
-
html(html_code)
|
| 228 |
-
|
| 229 |
-
# Third Place Winner - Celebration Animation
|
| 230 |
-
if len(top_3) > 2:
|
| 231 |
-
third_place = top_3.iloc[2]
|
| 232 |
-
st.write(f"🥉 {third_place['name']} - {third_place['score']} points! {third_place['avatar']}")
|
| 233 |
-
st.markdown("<h3 style='color: #cd7f32;'>👍 Nice Try on Third Place! 👍</h3>", unsafe_allow_html=True)
|
| 234 |
-
html_code = f"""
|
| 235 |
-
<div style="text-align: center;">
|
| 236 |
-
<h2>🥉 Third Place: {third_place['name']} 🥉</h2>
|
| 237 |
-
<p>Score: {third_place['score']}</p>
|
| 238 |
-
<p>Avatar: {third_place['avatar']}</p>
|
| 239 |
-
</div>
|
| 240 |
-
<div style="animation: thumbsUp 1s linear infinite;">
|
| 241 |
-
👍
|
| 242 |
-
</div>
|
| 243 |
-
<style>
|
| 244 |
-
@keyframes thumbsUp {{
|
| 245 |
-
0% {{ transform: rotate(0deg); }}
|
| 246 |
-
50% {{ transform: rotate(20deg); }}
|
| 247 |
-
100% {{ transform: rotate(0deg); }}
|
| 248 |
-
}}
|
| 249 |
-
</style>
|
| 250 |
-
"""
|
| 251 |
-
html(html_code)
|
| 252 |
-
|
| 253 |
-
# Pie chart for the distribution of player scores
|
| 254 |
-
fig = go.Figure(data=[go.Pie(labels=leaderboard_df['name'], values=leaderboard_df['score'], hole=0.3)])
|
| 255 |
-
fig.update_layout(title="Score Distribution")
|
| 256 |
-
st.plotly_chart(fig)
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
st.subheader("Top 10 Players")
|
| 260 |
-
top_10 = leaderboard_df.head(10)
|
| 261 |
-
|
| 262 |
-
# Loop through the top 10 players and display their details
|
| 263 |
-
for index, row in top_10.iterrows():
|
| 264 |
-
st.write(f"🥇 {row['name']} - {row['score']} points! {row['avatar']}")
|
| 265 |
-
st.write(f"Topic(s): {row['topic']}")
|
| 266 |
-
st.write(f"Answered Correctly: {row['correct']}")
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
# Bar chart of leaderboard rankings
|
| 270 |
-
fig = px.bar(
|
| 271 |
-
leaderboard_df,
|
| 272 |
-
x='name',
|
| 273 |
-
y='score',
|
| 274 |
-
color='name',
|
| 275 |
-
title="Leaderboard",
|
| 276 |
-
labels={'name': 'Player', 'score': 'Score'},
|
| 277 |
-
hover_data=["avatar"]
|
| 278 |
-
)
|
| 279 |
-
fig = go.Figure(data=[go.Pie(labels=leaderboard_df['name'].head(10), values=leaderboard_df['score'].head(10), hole=0.3)])
|
| 280 |
-
fig.update_layout(title="Score Distribution of Top 10 Players")
|
| 281 |
-
st.plotly_chart(fig)
|
| 282 |
-
|
| 283 |
-
|
| 284 |
# Main function to handle Streamlit app
|
| 285 |
def main():
|
| 286 |
st.title('AI Quiz Game')
|
|
@@ -290,17 +112,17 @@ def main():
|
|
| 290 |
if mode == "Home":
|
| 291 |
st.write("Welcome to the Game!")
|
| 292 |
st.write("You can create a new game, join an existing game, or check the leaderboard.")
|
| 293 |
-
display_dashboard()
|
| 294 |
|
| 295 |
elif mode == "Create Game":
|
| 296 |
create_game()
|
| 297 |
|
| 298 |
elif mode == "Join Game":
|
| 299 |
-
|
|
|
|
| 300 |
|
| 301 |
elif mode == "Leaderboard":
|
| 302 |
-
|
|
|
|
| 303 |
|
| 304 |
if __name__ == "__main__":
|
| 305 |
main()
|
| 306 |
-
|
|
|
|
| 71 |
if selected_topics:
|
| 72 |
# Collect questions from the selected topics
|
| 73 |
questions = []
|
| 74 |
+
questions_per_topic = num_questions // len(selected_topics)
|
| 75 |
+
|
| 76 |
+
# Ensure we don't ask for more questions than are available
|
| 77 |
for topic in selected_topics:
|
| 78 |
+
available_questions = questions_db[topic]
|
| 79 |
+
if len(available_questions) < questions_per_topic:
|
| 80 |
+
st.warning(f"Not enough questions in {topic}. Only {len(available_questions)} questions available.")
|
| 81 |
+
questions_per_topic = len(available_questions)
|
| 82 |
+
|
| 83 |
+
questions.extend(random.sample(available_questions, questions_per_topic))
|
| 84 |
|
| 85 |
random.shuffle(questions) # Shuffle questions
|
| 86 |
|
|
|
|
| 103 |
else:
|
| 104 |
st.error("Please select at least one topic.")
|
| 105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
# Main function to handle Streamlit app
|
| 107 |
def main():
|
| 108 |
st.title('AI Quiz Game')
|
|
|
|
| 112 |
if mode == "Home":
|
| 113 |
st.write("Welcome to the Game!")
|
| 114 |
st.write("You can create a new game, join an existing game, or check the leaderboard.")
|
|
|
|
| 115 |
|
| 116 |
elif mode == "Create Game":
|
| 117 |
create_game()
|
| 118 |
|
| 119 |
elif mode == "Join Game":
|
| 120 |
+
# Functionality for joining game
|
| 121 |
+
pass
|
| 122 |
|
| 123 |
elif mode == "Leaderboard":
|
| 124 |
+
# Functionality for displaying leaderboard
|
| 125 |
+
pass
|
| 126 |
|
| 127 |
if __name__ == "__main__":
|
| 128 |
main()
|
|
|