Add events history
This commit is contained in:
@@ -11,7 +11,7 @@ from app.core.config import settings
|
||||
from app.models import (
|
||||
Marathon, MarathonStatus, Game, Challenge, Participant,
|
||||
Assignment, AssignmentStatus, Activity, ActivityType,
|
||||
EventType, Difficulty
|
||||
EventType, Difficulty, User
|
||||
)
|
||||
from app.schemas import (
|
||||
SpinResult, AssignmentResponse, CompleteResult, DropResult,
|
||||
@@ -130,6 +130,8 @@ async def spin_wheel(marathon_id: int, current_user: CurrentUser, db: DbSession)
|
||||
activity_data = {
|
||||
"game": game.title,
|
||||
"challenge": challenge.title,
|
||||
"difficulty": challenge.difficulty,
|
||||
"points": challenge.points,
|
||||
}
|
||||
if active_event:
|
||||
activity_data["event_type"] = active_event.type
|
||||
@@ -328,6 +330,7 @@ async def complete_assignment(
|
||||
db, active_event, participant.id, current_user.id
|
||||
)
|
||||
total_points += common_enemy_bonus
|
||||
print(f"[COMMON_ENEMY] bonus={common_enemy_bonus}, closed={common_enemy_closed}, winners={common_enemy_winners}")
|
||||
|
||||
# Update assignment
|
||||
assignment.status = AssignmentStatus.COMPLETED.value
|
||||
@@ -342,7 +345,9 @@ async def complete_assignment(
|
||||
|
||||
# Log activity
|
||||
activity_data = {
|
||||
"game": full_challenge.game.title,
|
||||
"challenge": challenge.title,
|
||||
"difficulty": challenge.difficulty,
|
||||
"points": total_points,
|
||||
"streak": participant.current_streak,
|
||||
}
|
||||
@@ -367,6 +372,24 @@ async def complete_assignment(
|
||||
# If common enemy event auto-closed, log the event end with winners
|
||||
if common_enemy_closed and common_enemy_winners:
|
||||
from app.schemas.event import EVENT_INFO, COMMON_ENEMY_BONUSES
|
||||
# Load winner nicknames
|
||||
winner_user_ids = [w["user_id"] for w in common_enemy_winners]
|
||||
users_result = await db.execute(
|
||||
select(User).where(User.id.in_(winner_user_ids))
|
||||
)
|
||||
users_map = {u.id: u.nickname for u in users_result.scalars().all()}
|
||||
|
||||
winners_data = [
|
||||
{
|
||||
"user_id": w["user_id"],
|
||||
"nickname": users_map.get(w["user_id"], "Unknown"),
|
||||
"rank": w["rank"],
|
||||
"bonus_points": COMMON_ENEMY_BONUSES.get(w["rank"], 0),
|
||||
}
|
||||
for w in common_enemy_winners
|
||||
]
|
||||
print(f"[COMMON_ENEMY] Creating event_end activity with winners: {winners_data}")
|
||||
|
||||
event_end_activity = Activity(
|
||||
marathon_id=marathon_id,
|
||||
user_id=current_user.id, # Last completer triggers the close
|
||||
@@ -375,14 +398,7 @@ async def complete_assignment(
|
||||
"event_type": EventType.COMMON_ENEMY.value,
|
||||
"event_name": EVENT_INFO.get(EventType.COMMON_ENEMY, {}).get("name", "Общий враг"),
|
||||
"auto_closed": True,
|
||||
"winners": [
|
||||
{
|
||||
"user_id": w["user_id"],
|
||||
"rank": w["rank"],
|
||||
"bonus_points": COMMON_ENEMY_BONUSES.get(w["rank"], 0),
|
||||
}
|
||||
for w in common_enemy_winners
|
||||
],
|
||||
"winners": winners_data,
|
||||
},
|
||||
)
|
||||
db.add(event_end_activity)
|
||||
@@ -440,7 +456,9 @@ async def drop_assignment(assignment_id: int, current_user: CurrentUser, db: DbS
|
||||
|
||||
# Log activity
|
||||
activity_data = {
|
||||
"game": assignment.challenge.game.title,
|
||||
"challenge": assignment.challenge.title,
|
||||
"difficulty": assignment.challenge.difficulty,
|
||||
"penalty": penalty,
|
||||
}
|
||||
if active_event:
|
||||
|
||||
@@ -157,13 +157,16 @@ class EventService:
|
||||
- winners_list: list of winners if event closed, None otherwise
|
||||
"""
|
||||
if event.type != EventType.COMMON_ENEMY.value:
|
||||
print(f"[COMMON_ENEMY] Event type mismatch: {event.type}")
|
||||
return 0, False, None
|
||||
|
||||
data = event.data or {}
|
||||
completions = data.get("completions", [])
|
||||
print(f"[COMMON_ENEMY] Current completions count: {len(completions)}")
|
||||
|
||||
# Check if already completed
|
||||
if any(c["participant_id"] == participant_id for c in completions):
|
||||
print(f"[COMMON_ENEMY] Participant {participant_id} already completed")
|
||||
return 0, False, None
|
||||
|
||||
# Add completion
|
||||
@@ -174,6 +177,7 @@ class EventService:
|
||||
"completed_at": datetime.utcnow().isoformat(),
|
||||
"rank": rank,
|
||||
})
|
||||
print(f"[COMMON_ENEMY] Added completion for user {user_id}, rank={rank}")
|
||||
|
||||
# Update event data - need to flag_modified for SQLAlchemy to detect JSON changes
|
||||
event.data = {**data, "completions": completions}
|
||||
@@ -189,6 +193,7 @@ class EventService:
|
||||
event.end_time = datetime.utcnow()
|
||||
event_closed = True
|
||||
winners_list = completions[:3] # Top 3
|
||||
print(f"[COMMON_ENEMY] Event auto-closed! Winners: {winners_list}")
|
||||
|
||||
await db.commit()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user