Update GPT and add Profile
This commit is contained in:
@@ -14,12 +14,10 @@ from app.schemas import (
|
||||
ChallengesPreviewResponse,
|
||||
ChallengesSaveRequest,
|
||||
)
|
||||
from app.services.gpt import GPTService
|
||||
from app.services.gpt import gpt_service
|
||||
|
||||
router = APIRouter(tags=["challenges"])
|
||||
|
||||
gpt_service = GPTService()
|
||||
|
||||
|
||||
async def get_challenge_or_404(db, challenge_id: int) -> Challenge:
|
||||
result = await db.execute(
|
||||
@@ -215,22 +213,35 @@ async def preview_challenges(marathon_id: int, current_user: CurrentUser, db: Db
|
||||
if not games:
|
||||
raise HTTPException(status_code=400, detail="No approved games in marathon")
|
||||
|
||||
preview_challenges = []
|
||||
# Filter games that don't have challenges yet
|
||||
games_to_generate = []
|
||||
game_map = {}
|
||||
for game in games:
|
||||
# Check if game already has challenges
|
||||
existing = await db.scalar(
|
||||
select(Challenge.id).where(Challenge.game_id == game.id).limit(1)
|
||||
)
|
||||
if existing:
|
||||
continue # Skip if already has challenges
|
||||
if not existing:
|
||||
games_to_generate.append({
|
||||
"id": game.id,
|
||||
"title": game.title,
|
||||
"genre": game.genre
|
||||
})
|
||||
game_map[game.id] = game.title
|
||||
|
||||
try:
|
||||
challenges_data = await gpt_service.generate_challenges(game.title, game.genre)
|
||||
if not games_to_generate:
|
||||
return ChallengesPreviewResponse(challenges=[])
|
||||
|
||||
# Generate challenges for all games in one API call
|
||||
preview_challenges = []
|
||||
try:
|
||||
challenges_by_game = await gpt_service.generate_challenges(games_to_generate)
|
||||
|
||||
for game_id, challenges_data in challenges_by_game.items():
|
||||
game_title = game_map.get(game_id, "Unknown")
|
||||
for ch_data in challenges_data:
|
||||
preview_challenges.append(ChallengePreview(
|
||||
game_id=game.id,
|
||||
game_title=game.title,
|
||||
game_id=game_id,
|
||||
game_title=game_title,
|
||||
title=ch_data.title,
|
||||
description=ch_data.description,
|
||||
type=ch_data.type,
|
||||
@@ -241,9 +252,8 @@ async def preview_challenges(marathon_id: int, current_user: CurrentUser, db: Db
|
||||
proof_hint=ch_data.proof_hint,
|
||||
))
|
||||
|
||||
except Exception as e:
|
||||
# Log error but continue with other games
|
||||
print(f"Error generating challenges for {game.title}: {e}")
|
||||
except Exception as e:
|
||||
print(f"Error generating challenges: {e}")
|
||||
|
||||
return ChallengesPreviewResponse(challenges=preview_challenges)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user