Elbby Skermine commited on
Commit ·
79a225e
1
Parent(s): c5c63b8
new user tracking
Browse files
tools.py
CHANGED
|
@@ -5,7 +5,6 @@ MCP Tools for Session Management and Quiz
|
|
| 5 |
import json
|
| 6 |
import uuid
|
| 7 |
import random
|
| 8 |
-
from datetime import datetime
|
| 9 |
from mcp.server.fastmcp import FastMCP
|
| 10 |
from pydantic import Field
|
| 11 |
from typing import Dict, List, Optional
|
|
@@ -41,15 +40,10 @@ def register_tools(mcp: FastMCP, db_getter):
|
|
| 41 |
if session_doc.exists:
|
| 42 |
session_data = session_doc.to_dict()
|
| 43 |
|
| 44 |
-
# Add the new player
|
| 45 |
new_player = {
|
| 46 |
'pseudo': player_pseudo,
|
| 47 |
-
'score': 0
|
| 48 |
-
'answered_questions': [], # Track which questions this user has answered
|
| 49 |
-
'answer_history': [], # Detailed history of all answers
|
| 50 |
-
'current_question_index': 0, # Individual progress tracking
|
| 51 |
-
'session_start_time': {'$timestamp': 'server'},
|
| 52 |
-
'last_activity': {'$timestamp': 'server'}
|
| 53 |
}
|
| 54 |
|
| 55 |
# Check if player already exists in this session
|
|
@@ -185,41 +179,13 @@ def register_tools(mcp: FastMCP, db_getter):
|
|
| 185 |
correct_answer = current_question.get('correct')
|
| 186 |
is_correct = answer_index == correct_answer
|
| 187 |
|
| 188 |
-
# Find and update player
|
| 189 |
player_found = False
|
| 190 |
new_score = 0
|
| 191 |
for player in players:
|
| 192 |
if player.get('pseudo') == player_pseudo:
|
| 193 |
-
# Create detailed answer record
|
| 194 |
-
answer_record = {
|
| 195 |
-
'question_id': current_question.get('id'),
|
| 196 |
-
'question_index': current_question_index,
|
| 197 |
-
'submitted_answer': answer_index,
|
| 198 |
-
'correct_answer': correct_answer,
|
| 199 |
-
'is_correct': is_correct,
|
| 200 |
-
'timestamp': {'$timestamp': 'server'},
|
| 201 |
-
'response_time': None # Could be calculated if we track question start time
|
| 202 |
-
}
|
| 203 |
-
|
| 204 |
-
# Update player data
|
| 205 |
if is_correct:
|
| 206 |
player['score'] = player.get('score', 0) + 1
|
| 207 |
-
|
| 208 |
-
# Add to answer history
|
| 209 |
-
answer_history = player.get('answer_history', [])
|
| 210 |
-
answer_history.append(answer_record)
|
| 211 |
-
player['answer_history'] = answer_history
|
| 212 |
-
|
| 213 |
-
# Add question ID to answered questions if not already there
|
| 214 |
-
answered_questions = player.get('answered_questions', [])
|
| 215 |
-
question_id = current_question.get('id')
|
| 216 |
-
if question_id not in answered_questions:
|
| 217 |
-
answered_questions.append(question_id)
|
| 218 |
-
player['answered_questions'] = answered_questions
|
| 219 |
-
|
| 220 |
-
# Update activity timestamp
|
| 221 |
-
player['last_activity'] = {'$timestamp': 'server'}
|
| 222 |
-
|
| 223 |
new_score = player.get('score', 0)
|
| 224 |
player_found = True
|
| 225 |
break
|
|
@@ -288,234 +254,4 @@ def register_tools(mcp: FastMCP, db_getter):
|
|
| 288 |
return json.dumps(result, indent=2)
|
| 289 |
|
| 290 |
except Exception as e:
|
| 291 |
-
return f"Error getting scores: {str(e)}"
|
| 292 |
-
|
| 293 |
-
@mcp.tool(
|
| 294 |
-
title="Get User Progress",
|
| 295 |
-
description="Get detailed progress information for a specific user including answer history",
|
| 296 |
-
)
|
| 297 |
-
async def get_user_progress(
|
| 298 |
-
session_id: str = Field(description="The ID of the quiz session"),
|
| 299 |
-
player_pseudo: str = Field(description="The pseudo/username of the player")
|
| 300 |
-
) -> str:
|
| 301 |
-
"""Get detailed user progress and answer history"""
|
| 302 |
-
try:
|
| 303 |
-
# Get database client lazily
|
| 304 |
-
db = db_getter()
|
| 305 |
-
session_ref = db.collection('quiz_sessions').document(session_id)
|
| 306 |
-
session_doc = session_ref.get()
|
| 307 |
-
|
| 308 |
-
if not session_doc.exists:
|
| 309 |
-
return f"Session '{session_id}' not found"
|
| 310 |
-
|
| 311 |
-
session_data = session_doc.to_dict()
|
| 312 |
-
players = session_data.get('players', [])
|
| 313 |
-
|
| 314 |
-
# Find the specific player
|
| 315 |
-
player = next((p for p in players if p.get('pseudo') == player_pseudo), None)
|
| 316 |
-
if not player:
|
| 317 |
-
return f"Player '{player_pseudo}' not found in session '{session_id}'"
|
| 318 |
-
|
| 319 |
-
# Get session questions for context
|
| 320 |
-
questions = session_data.get('questions', [])
|
| 321 |
-
total_questions = len(questions)
|
| 322 |
-
|
| 323 |
-
result = {
|
| 324 |
-
"player_pseudo": player_pseudo,
|
| 325 |
-
"session_id": session_id,
|
| 326 |
-
"current_score": player.get('score', 0),
|
| 327 |
-
"answered_questions": player.get('answered_questions', []),
|
| 328 |
-
"answer_history": player.get('answer_history', []),
|
| 329 |
-
"current_question_index": player.get('current_question_index', 0),
|
| 330 |
-
"total_questions": total_questions,
|
| 331 |
-
"questions_remaining": total_questions - len(player.get('answered_questions', [])),
|
| 332 |
-
"session_start_time": player.get('session_start_time'),
|
| 333 |
-
"last_activity": player.get('last_activity'),
|
| 334 |
-
"completion_percentage": (len(player.get('answered_questions', [])) / total_questions * 100) if total_questions > 0 else 0
|
| 335 |
-
}
|
| 336 |
-
|
| 337 |
-
return json.dumps(result, indent=2)
|
| 338 |
-
|
| 339 |
-
except Exception as e:
|
| 340 |
-
return f"Error getting user progress: {str(e)}"
|
| 341 |
-
|
| 342 |
-
@mcp.tool(
|
| 343 |
-
title="Get Next Question for User",
|
| 344 |
-
description="Get the next question specifically for a user based on their individual progress",
|
| 345 |
-
)
|
| 346 |
-
async def get_next_question_for_user(
|
| 347 |
-
session_id: str = Field(description="The ID of the quiz session"),
|
| 348 |
-
player_pseudo: str = Field(description="The pseudo/username of the player")
|
| 349 |
-
) -> str:
|
| 350 |
-
"""Get the next unanswered question for a specific user"""
|
| 351 |
-
try:
|
| 352 |
-
# Get database client lazily
|
| 353 |
-
db = db_getter()
|
| 354 |
-
session_ref = db.collection('quiz_sessions').document(session_id)
|
| 355 |
-
session_doc = session_ref.get()
|
| 356 |
-
|
| 357 |
-
if not session_doc.exists:
|
| 358 |
-
return f"Session '{session_id}' not found"
|
| 359 |
-
|
| 360 |
-
session_data = session_doc.to_dict()
|
| 361 |
-
questions = session_data.get('questions', [])
|
| 362 |
-
players = session_data.get('players', [])
|
| 363 |
-
|
| 364 |
-
if not questions:
|
| 365 |
-
return json.dumps({
|
| 366 |
-
"error": "No questions available",
|
| 367 |
-
"message": "This session has no questions configured."
|
| 368 |
-
}, indent=2)
|
| 369 |
-
|
| 370 |
-
# Find the specific player
|
| 371 |
-
player = next((p for p in players if p.get('pseudo') == player_pseudo), None)
|
| 372 |
-
if not player:
|
| 373 |
-
return f"Player '{player_pseudo}' not found in session '{session_id}'"
|
| 374 |
-
|
| 375 |
-
# Get questions this user has already answered
|
| 376 |
-
answered_question_ids = player.get('answered_questions', [])
|
| 377 |
-
|
| 378 |
-
# Find next unanswered question
|
| 379 |
-
next_question = None
|
| 380 |
-
next_question_index = None
|
| 381 |
-
|
| 382 |
-
for i, question in enumerate(questions):
|
| 383 |
-
question_id = question.get('id')
|
| 384 |
-
if question_id not in answered_question_ids:
|
| 385 |
-
next_question = question
|
| 386 |
-
next_question_index = i
|
| 387 |
-
break
|
| 388 |
-
|
| 389 |
-
if next_question is None:
|
| 390 |
-
return json.dumps({
|
| 391 |
-
"finished": True,
|
| 392 |
-
"message": f"Player '{player_pseudo}' has completed all questions",
|
| 393 |
-
"total_questions": len(questions),
|
| 394 |
-
"answered_questions": len(answered_question_ids)
|
| 395 |
-
}, indent=2)
|
| 396 |
-
|
| 397 |
-
# Format the response (without the correct answer)
|
| 398 |
-
question_data = {
|
| 399 |
-
"question_id": next_question.get('id'),
|
| 400 |
-
"question_text": next_question.get('question'),
|
| 401 |
-
"options": next_question.get('options', []),
|
| 402 |
-
"question_number": next_question_index + 1,
|
| 403 |
-
"total_questions": len(questions),
|
| 404 |
-
"player_progress": {
|
| 405 |
-
"answered": len(answered_question_ids),
|
| 406 |
-
"remaining": len(questions) - len(answered_question_ids),
|
| 407 |
-
"completion_percentage": (len(answered_question_ids) / len(questions) * 100)
|
| 408 |
-
}
|
| 409 |
-
}
|
| 410 |
-
|
| 411 |
-
return json.dumps(question_data, indent=2)
|
| 412 |
-
|
| 413 |
-
except Exception as e:
|
| 414 |
-
return f"Error getting next question for user: {str(e)}"
|
| 415 |
-
|
| 416 |
-
@mcp.tool(
|
| 417 |
-
title="Update User Question Index",
|
| 418 |
-
description="Update a user's current question index for manual progression control",
|
| 419 |
-
)
|
| 420 |
-
async def update_user_question_index(
|
| 421 |
-
session_id: str = Field(description="The ID of the quiz session"),
|
| 422 |
-
player_pseudo: str = Field(description="The pseudo/username of the player"),
|
| 423 |
-
question_index: int = Field(description="The new question index for the user")
|
| 424 |
-
) -> str:
|
| 425 |
-
"""Update user's current question index"""
|
| 426 |
-
try:
|
| 427 |
-
# Get database client lazily
|
| 428 |
-
db = db_getter()
|
| 429 |
-
session_ref = db.collection('quiz_sessions').document(session_id)
|
| 430 |
-
session_doc = session_ref.get()
|
| 431 |
-
|
| 432 |
-
if not session_doc.exists:
|
| 433 |
-
return f"Session '{session_id}' not found"
|
| 434 |
-
|
| 435 |
-
session_data = session_doc.to_dict()
|
| 436 |
-
players = session_data.get('players', [])
|
| 437 |
-
questions = session_data.get('questions', [])
|
| 438 |
-
|
| 439 |
-
# Validate question index
|
| 440 |
-
if question_index < 0 or question_index >= len(questions):
|
| 441 |
-
return f"Invalid question index. Must be between 0 and {len(questions) - 1}"
|
| 442 |
-
|
| 443 |
-
# Find and update the specific player
|
| 444 |
-
player_found = False
|
| 445 |
-
for player in players:
|
| 446 |
-
if player.get('pseudo') == player_pseudo:
|
| 447 |
-
player['current_question_index'] = question_index
|
| 448 |
-
player['last_activity'] = {'$timestamp': 'server'}
|
| 449 |
-
player_found = True
|
| 450 |
-
break
|
| 451 |
-
|
| 452 |
-
if not player_found:
|
| 453 |
-
return f"Player '{player_pseudo}' not found in session '{session_id}'"
|
| 454 |
-
|
| 455 |
-
# Update session document
|
| 456 |
-
session_data['players'] = players
|
| 457 |
-
session_ref.set(session_data, merge=True)
|
| 458 |
-
|
| 459 |
-
result = {
|
| 460 |
-
"player_pseudo": player_pseudo,
|
| 461 |
-
"session_id": session_id,
|
| 462 |
-
"new_question_index": question_index,
|
| 463 |
-
"question_title": questions[question_index].get('question', 'N/A')[:50] + "...",
|
| 464 |
-
"message": f"Successfully updated question index for '{player_pseudo}'"
|
| 465 |
-
}
|
| 466 |
-
|
| 467 |
-
return json.dumps(result, indent=2)
|
| 468 |
-
|
| 469 |
-
except Exception as e:
|
| 470 |
-
return f"Error updating user question index: {str(e)}"
|
| 471 |
-
|
| 472 |
-
@mcp.tool(
|
| 473 |
-
title="Reset User Progress",
|
| 474 |
-
description="Reset a user's progress including answer history and scores",
|
| 475 |
-
)
|
| 476 |
-
async def reset_user_progress(
|
| 477 |
-
session_id: str = Field(description="The ID of the quiz session"),
|
| 478 |
-
player_pseudo: str = Field(description="The pseudo/username of the player")
|
| 479 |
-
) -> str:
|
| 480 |
-
"""Reset user's progress to start over"""
|
| 481 |
-
try:
|
| 482 |
-
# Get database client lazily
|
| 483 |
-
db = db_getter()
|
| 484 |
-
session_ref = db.collection('quiz_sessions').document(session_id)
|
| 485 |
-
session_doc = session_ref.get()
|
| 486 |
-
|
| 487 |
-
if not session_doc.exists:
|
| 488 |
-
return f"Session '{session_id}' not found"
|
| 489 |
-
|
| 490 |
-
session_data = session_doc.to_dict()
|
| 491 |
-
players = session_data.get('players', [])
|
| 492 |
-
|
| 493 |
-
# Find and reset the specific player
|
| 494 |
-
player_found = False
|
| 495 |
-
for player in players:
|
| 496 |
-
if player.get('pseudo') == player_pseudo:
|
| 497 |
-
player['score'] = 0
|
| 498 |
-
player['answered_questions'] = []
|
| 499 |
-
player['answer_history'] = []
|
| 500 |
-
player['current_question_index'] = 0
|
| 501 |
-
player['last_activity'] = {'$timestamp': 'server'}
|
| 502 |
-
player_found = True
|
| 503 |
-
break
|
| 504 |
-
|
| 505 |
-
if not player_found:
|
| 506 |
-
return f"Player '{player_pseudo}' not found in session '{session_id}'"
|
| 507 |
-
|
| 508 |
-
# Update session document
|
| 509 |
-
session_data['players'] = players
|
| 510 |
-
session_ref.set(session_data, merge=True)
|
| 511 |
-
|
| 512 |
-
result = {
|
| 513 |
-
"player_pseudo": player_pseudo,
|
| 514 |
-
"session_id": session_id,
|
| 515 |
-
"message": f"Successfully reset progress for '{player_pseudo}'"
|
| 516 |
-
}
|
| 517 |
-
|
| 518 |
-
return json.dumps(result, indent=2)
|
| 519 |
-
|
| 520 |
-
except Exception as e:
|
| 521 |
-
return f"Error resetting user progress: {str(e)}"
|
|
|
|
| 5 |
import json
|
| 6 |
import uuid
|
| 7 |
import random
|
|
|
|
| 8 |
from mcp.server.fastmcp import FastMCP
|
| 9 |
from pydantic import Field
|
| 10 |
from typing import Dict, List, Optional
|
|
|
|
| 40 |
if session_doc.exists:
|
| 41 |
session_data = session_doc.to_dict()
|
| 42 |
|
| 43 |
+
# Add the new player to the players list
|
| 44 |
new_player = {
|
| 45 |
'pseudo': player_pseudo,
|
| 46 |
+
'score': 0 # Initialize score to 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
}
|
| 48 |
|
| 49 |
# Check if player already exists in this session
|
|
|
|
| 179 |
correct_answer = current_question.get('correct')
|
| 180 |
is_correct = answer_index == correct_answer
|
| 181 |
|
| 182 |
+
# Find and update player score
|
| 183 |
player_found = False
|
| 184 |
new_score = 0
|
| 185 |
for player in players:
|
| 186 |
if player.get('pseudo') == player_pseudo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
if is_correct:
|
| 188 |
player['score'] = player.get('score', 0) + 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
new_score = player.get('score', 0)
|
| 190 |
player_found = True
|
| 191 |
break
|
|
|
|
| 254 |
return json.dumps(result, indent=2)
|
| 255 |
|
| 256 |
except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