Files
SerpentRace/SerpentRace_Backend/src/Application/Game/commands/GenerateBoardCommandHandler.ts
T
2025-09-26 17:01:45 +02:00

63 lines
2.3 KiB
TypeScript

import { GenerateBoardCommand } from './GenerateBoardCommand';
import { BoardGenerationService } from '../BoardGenerationService';
import { RedisService } from '../../Services/RedisService';
import { logOther, logError } from '../../Services/Logger';
import { BoardData } from '../../../Domain/Game/GameAggregate';
export class GenerateBoardCommandHandler {
constructor(
private readonly boardGenerationService: BoardGenerationService,
private readonly redisService: RedisService
) {}
async execute(cmd: GenerateBoardCommand): Promise<void> {
try {
logOther(`Starting board generation for game ${cmd.gameId}`);
const startTime = Date.now();
// Generate board with 20-30 rule validation
const boardData = await this.boardGenerationService.generateBoard(
cmd.positiveFieldCount,
cmd.negativeFieldCount,
cmd.luckFieldCount
);
// Store in Redis
const boardDataWithMetadata: BoardData = {
...boardData,
gameId: cmd.gameId,
generatedAt: new Date(),
generationComplete: true
};
await this.redisService.setWithExpiry(
`game_board_${cmd.gameId}`,
JSON.stringify(boardDataWithMetadata),
24 * 60 * 60 // 24 hours
);
const executionTime = Date.now() - startTime;
logOther(`Board generation completed for game ${cmd.gameId} in ${executionTime}ms using pattern-based approach`);
} catch (error) {
logError(`Board generation failed for game ${cmd.gameId}:`, error as Error);
// Store error state in Redis
const errorData: BoardData = {
gameId: cmd.gameId,
fields: [],
generationComplete: false,
error: error instanceof Error ? error.message : 'Unknown error',
generatedAt: new Date()
};
await this.redisService.setWithExpiry(
`game_board_${cmd.gameId}`,
JSON.stringify(errorData),
24 * 60 * 60
);
throw error;
}
}
}