import { systemEvents, EVENTS } from "@shared/lib/events"; import { questService } from "@shared/modules/quest/quest.service"; import { dashboardService } from "@shared/modules/dashboard/dashboard.service"; /** * Registers all domain event listeners. * Must be called once at startup before any domain events are emitted. * * This wiring replaces dynamic imports that were previously used to avoid * circular dependencies between services (e.g., economy -> quest -> economy). */ export function registerDomainEventListeners() { // --- Quest progress tracking (awaited via emitAsync to preserve tx atomicity) --- systemEvents.on(EVENTS.DOMAIN.BALANCE_CHANGED, async ({ userId, type, tx }) => { await questService.handleEvent(userId, type, 1, tx); }); systemEvents.on(EVENTS.DOMAIN.XP_GAINED, async ({ userId, amount, tx }) => { await questService.handleEvent(userId, 'XP_GAIN', amount, tx); }); systemEvents.on(EVENTS.DOMAIN.ITEM_COLLECTED, async ({ userId, itemId, quantity, tx }) => { await questService.handleEvent(userId, `ITEM_COLLECT:${itemId}`, quantity, tx); }); systemEvents.on(EVENTS.DOMAIN.ITEM_USED, async ({ userId, itemId, tx }) => { await questService.handleEvent(userId, `ITEM_USE:${itemId}`, 1, tx); }); // --- Dashboard event recording (fire-and-forget) --- systemEvents.on(EVENTS.DOMAIN.TRANSFER_COMPLETED, async ({ username, amount, toUserId }) => { await dashboardService.recordEvent({ type: 'info', message: `${username} transferred ${amount.toLocaleString()} AU to User ID ${toUserId}`, icon: '💸' }); }); systemEvents.on(EVENTS.DOMAIN.DAILY_CLAIMED, async ({ username, amount }) => { await dashboardService.recordEvent({ type: 'success', message: `${username} claimed daily reward: ${amount.toLocaleString()} AU`, icon: '☀️' }); }); systemEvents.on(EVENTS.DOMAIN.TRIVIA_STARTED, async ({ username, difficulty }) => { await dashboardService.recordEvent({ type: 'info', message: `${username} started a trivia game (${difficulty})`, icon: '🎯' }); }); systemEvents.on(EVENTS.DOMAIN.TRIVIA_WON, async ({ username, reward }) => { await dashboardService.recordEvent({ type: 'success', message: `${username} won ${reward.toLocaleString()} AU from trivia!`, icon: '🎉' }); }); systemEvents.on(EVENTS.DOMAIN.EXAM_PASSED, async ({ username, reward }) => { await dashboardService.recordEvent({ type: 'success', message: `${username} passed their exam: ${reward.toLocaleString()} AU`, icon: '🎓' }); }); }