Fix common enemy

This commit is contained in:
2026-01-08 05:29:55 +07:00
parent 18fe95effc
commit ca49e42f74
2 changed files with 154 additions and 21 deletions

View File

@@ -47,6 +47,8 @@ class EventService:
created_by_id: int | None = None,
duration_minutes: int | None = None,
challenge_id: int | None = None,
game_id: int | None = None,
is_playthrough: bool = False,
) -> Event:
"""Start a new event"""
# Check no active event
@@ -63,8 +65,12 @@ class EventService:
# Build event data
data = {}
if event_type == EventType.COMMON_ENEMY.value and challenge_id:
data["challenge_id"] = challenge_id
if event_type == EventType.COMMON_ENEMY.value and (challenge_id or game_id):
if is_playthrough and game_id:
data["game_id"] = game_id
data["is_playthrough"] = True
else:
data["challenge_id"] = challenge_id
data["completions"] = [] # Track who completed and when
event = Event(
@@ -79,9 +85,11 @@ class EventService:
db.add(event)
await db.flush() # Get event.id before committing
# Auto-assign challenge to all participants for Common Enemy
if event_type == EventType.COMMON_ENEMY.value and challenge_id:
await self._assign_common_enemy_to_all(db, marathon_id, event.id, challenge_id)
# Auto-assign challenge/playthrough to all participants for Common Enemy
if event_type == EventType.COMMON_ENEMY.value and (challenge_id or game_id):
await self._assign_common_enemy_to_all(
db, marathon_id, event.id, challenge_id, game_id, is_playthrough
)
await db.commit()
await db.refresh(event)
@@ -105,7 +113,9 @@ class EventService:
db: AsyncSession,
marathon_id: int,
event_id: int,
challenge_id: int,
challenge_id: int | None,
game_id: int | None = None,
is_playthrough: bool = False,
) -> None:
"""Create event assignments for all participants in the marathon"""
# Get all participants
@@ -118,7 +128,9 @@ class EventService:
for participant in participants:
assignment = Assignment(
participant_id=participant.id,
challenge_id=challenge_id,
challenge_id=challenge_id if not is_playthrough else None,
game_id=game_id if is_playthrough else None,
is_playthrough=is_playthrough,
status=AssignmentStatus.ACTIVE.value,
event_type=EventType.COMMON_ENEMY.value,
is_event_assignment=True,
@@ -290,6 +302,30 @@ class EventService:
)
return result.scalar_one_or_none()
async def get_common_enemy_game(
self,
db: AsyncSession,
event: Event
):
"""Get the playthrough game for common enemy event (if it's a playthrough)"""
from app.models import Game
if event.type != EventType.COMMON_ENEMY.value:
return None
data = event.data or {}
if not data.get("is_playthrough"):
return None
game_id = data.get("game_id")
if not game_id:
return None
result = await db.execute(
select(Game).where(Game.id == game_id)
)
return result.scalar_one_or_none()
def get_time_remaining(self, event: Event | None) -> int | None:
"""Get remaining time in seconds for an event"""
if not event or not event.end_time: