feat: auto music control for lottery phases
- Auto-play lottery music when storm starts - Auto-play fanfare when winners revealed - Auto-stop music when lottery completes - Add EventEmitter to broadcast async state changes Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
/**
|
||||
* Admin Service - Manages system phase, voting control, and lottery state
|
||||
*/
|
||||
import { EventEmitter } from 'events';
|
||||
import { redis } from '../config/redis';
|
||||
import { logger } from '../utils/logger';
|
||||
import { prizeConfigService } from './prize-config.service';
|
||||
@@ -21,10 +22,11 @@ import { INITIAL_ADMIN_STATE, PRIZE_CONFIG, DEFAULT_PROGRAMS } from '@gala/share
|
||||
|
||||
const ADMIN_STATE_KEY = 'gala:admin:state';
|
||||
|
||||
class AdminService {
|
||||
class AdminService extends EventEmitter {
|
||||
private state: AdminState;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.state = { ...INITIAL_ADMIN_STATE };
|
||||
}
|
||||
|
||||
@@ -372,6 +374,9 @@ class AdminService {
|
||||
}
|
||||
this.state.lottery.subPhase = 'STORM';
|
||||
this.state.lottery.stormStartedAt = Date.now();
|
||||
// Auto-play lottery music
|
||||
this.state.music.isPlaying = true;
|
||||
this.state.music.track = 'lottery';
|
||||
break;
|
||||
|
||||
case 'stop_reveal':
|
||||
@@ -382,10 +387,17 @@ class AdminService {
|
||||
const winners = this.pickRandomWinners();
|
||||
this.state.lottery.subPhase = 'REVEAL';
|
||||
this.state.lottery.currentWinners = winners;
|
||||
// After reveal animation, set to COMPLETE
|
||||
// Auto-play fanfare music
|
||||
this.state.music.isPlaying = true;
|
||||
this.state.music.track = 'fanfare';
|
||||
// After reveal animation, set to COMPLETE and stop music
|
||||
setTimeout(() => {
|
||||
this.state.lottery.subPhase = 'COMPLETE';
|
||||
this.state.music.isPlaying = false;
|
||||
this.state.music.track = 'none';
|
||||
this.saveState();
|
||||
// Emit event to broadcast state change
|
||||
this.emit('stateChange', this.getState());
|
||||
}, 3000);
|
||||
await this.saveState();
|
||||
return { success: true, data: { winners } };
|
||||
|
||||
@@ -57,6 +57,12 @@ export async function initializeSocket(httpServer: HttpServer): Promise<GalaServ
|
||||
// Initialize admin service
|
||||
await adminService.initialize();
|
||||
|
||||
// Listen for admin state changes (e.g., from setTimeout callbacks)
|
||||
adminService.on('stateChange', (state) => {
|
||||
logger.info('Admin state changed, broadcasting to all clients');
|
||||
io.to(SOCKET_ROOMS.ALL).emit(SOCKET_EVENTS.ADMIN_STATE_SYNC as any, state);
|
||||
});
|
||||
|
||||
// Connection handler
|
||||
io.on('connection', handleConnection);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user