Skip to content

Commit

Permalink
fix: Add retry logic for fetching words in startGame (#198)
Browse files Browse the repository at this point in the history
Co-authored-by: 유미라 <[email protected]>
  • Loading branch information
ssum1ra and 유미라 authored Dec 4, 2024
1 parent 320046f commit 229b512
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
1 change: 0 additions & 1 deletion server/src/game/game.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export class GameGateway implements OnGatewayDisconnect {

private disconnectTimeouts: Map<string, NodeJS.Timeout> = new Map();
private readonly DISCONNECT_TIMEOUT = 10000;
private finalDrawing: any;

constructor(
private readonly gameService: GameService,
Expand Down
25 changes: 23 additions & 2 deletions server/src/game/game.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ export class GameService {
totalRounds: 5,
drawTime: 35,
};
private static readonly DEFAULT_WORDS = [
'아이언맨',
'토르',
'스파이더맨',
'호빵맨',
'도라에몽',
'짱구',
'레옹',
'토토로',
'가오나시',
'개발자',
'대통령',
];
private words: string[] = [];

constructor(
Expand Down Expand Up @@ -221,8 +234,16 @@ export class GameService {
}

const roomSettings = await this.gameRepository.getRoomSettings(roomId);
this.words = await this.fetchWords(roomSettings.totalRounds);
}

this.words = await this.clovaClient.getDrawingWords(Difficulty.HARD, roomSettings.totalRounds);
private async fetchWords(totalRounds: number): Promise<string[]> {
let attempts = 0;
while (attempts++ < 10) {
const words = await this.clovaClient.getDrawingWords(Difficulty.HARD, totalRounds);
if (words.length === totalRounds) return words;
}
return GameService.DEFAULT_WORDS.slice(0, totalRounds);
}

async setupRound(roomId: string) {
Expand All @@ -241,7 +262,7 @@ export class GameService {

const roomUpdates = {
status: RoomStatus.DRAWING,
currentWord: this.words.shift(),
currentWord: this.words[room.currentRound],
currentRound: room.currentRound + 1,
};
await this.gameRepository.updateRoom(roomId, { ...roomUpdates });
Expand Down

0 comments on commit 229b512

Please sign in to comment.