fel kesz game backend
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
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. Error rate: ${boardData.totalErrorRate}%`);
|
||||
|
||||
} 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: [],
|
||||
border: [],
|
||||
validationResults: {},
|
||||
totalErrorRate: 100,
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user