Skip to content

Commit

Permalink
Merge pull request #196 from boostcampwm-2024/feature/#192
Browse files Browse the repository at this point in the history
[Feat] ๊ฐ์ข… ๋ฒ„๊ทธ ์ˆ˜์ • ๋ฐ ๋กœ๊ทธ์ธ์‹œ ํด๋ผ์ธก์—์„œ id๋ฐ›๋Š” ๋กœ์ง ์ถ”๊ฐ€
  • Loading branch information
zizonyoungjun authored Nov 28, 2024
2 parents 5852b32 + d37f62d commit 09bd7e8
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 98 deletions.
9 changes: 6 additions & 3 deletions packages/backend/gameserver/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { Controller, Get } from '@nestjs/common';
import { Controller, Get, Query } from '@nestjs/common';
import { AuthService } from './auth.service';

@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}

@Get('guest-login')
guestLogin(): { userId: string; password: string } {
return this.authService.guestLogin();
guestLogin(@Query('userId') userId: string): {
userId: string;
password: string;
} {
return this.authService.guestLogin(userId || '๊ฒŒ์ŠคํŠธ');
}
}
20 changes: 11 additions & 9 deletions packages/backend/gameserver/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { Injectable } from '@nestjs/common';
import { v4 as uuidv4 } from 'uuid';

interface GuestCredentials {
userId: string;
password: string;
}

@Injectable()
export class AuthService {
private guestSessions: Map<string, string> = new Map();
private guestCounter: number = 1;
private userCounters: Map<string, number> = new Map();

guestLogin(): GuestCredentials {
const userId = `๊ฒŒ์ŠคํŠธ_${this.guestCounter++}`;
const password = uuidv4();
guestLogin(requestedUserId: string): { userId: string; password: string } {
let userId = requestedUserId;

// ์ด๋ฏธ ์กด์žฌํ•˜๋Š” userId์ธ ๊ฒฝ์šฐ ์นด์šดํ„ฐ๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ ์ƒˆ๋กœ์šด userId ์ƒ์„ฑ
if (this.guestSessions.has(userId)) {
const counter = (this.userCounters.get(requestedUserId) || 1) + 1;
this.userCounters.set(requestedUserId, counter);
userId = `${requestedUserId}_${counter}`;
}

const password = uuidv4();
this.guestSessions.set(userId, password);
return { userId, password };
}
Expand Down
134 changes: 54 additions & 80 deletions packages/backend/gameserver/src/game/constants/words.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,13 @@ export const GAME_WORDS: WordTheme[] = [
'ํ‚ค๋ณด๋“œ',
'๋ชจ๋‹ˆํ„ฐ',
'ํ”„๋ฆฐํ„ฐ',
'์ฑ…์ƒ',
'์˜์ž',
'์นจ๋Œ€',
'์†ŒํŒŒ',
'๋ƒ‰์žฅ๊ณ ',
'ํ—ค์–ด๋“œ๋ผ์ด์–ด',
'์นซ์†”',
'์น˜์•ฝ',
'์ƒดํ‘ธ',
'์ˆ˜๊ฑด',
'๊ฑฐ์šธ',
'๋น—',
'๋ฌผ๋ณ‘',
'ํ…€๋ธ”๋Ÿฌ',
'์ถฉ์ „๊ธฐ',
'์ด์–ดํฐ',
'๋…ธํŠธ๋ถ',
Expand All @@ -47,16 +40,11 @@ export const GAME_WORDS: WordTheme[] = [
'ํ…Œ์ดํ”„',
'๊ฐ€์œ„',
'์Šคํ…Œ์ดํ”Œ๋Ÿฌ',
'๋ฉ€ํ‹ฐํƒญ',
'์ฒญ์†Œ๊ธฐ',
'์ „์ž๋ ˆ์ธ์ง€',
'๋ฐฅ์†ฅ',
'์ปคํŠผ',
'์ฑ…์žฅ',
'์˜ท๊ฑธ์ด',
'ํ™”๋ถ„',
'์“ฐ๋ ˆ๊ธฐํ†ต',
'๋„๋งˆ',
'์นผ',
'ํ›„๋ผ์ดํŒฌ',
'๋ƒ„๋น„',
Expand All @@ -76,11 +64,6 @@ export const GAME_WORDS: WordTheme[] = [
'์„ ํ’๊ธฐ',
'๊ณต๊ธฐ์ฒญ์ •๊ธฐ',
'๊ฐ€์Šต๊ธฐ',
'์ œ์Šต๊ธฐ',
'๊ฑด์กฐ๊ธฐ',
'๋‹ค๋ฆฌ๋ฏธ',
'์˜ท์†”',
'๊ตฌ๋‘์†”',
],
},
{
Expand Down Expand Up @@ -112,7 +95,6 @@ export const GAME_WORDS: WordTheme[] = [
'์•„์ด์Šคํฌ๋ฆผ',
'์ƒŒ๋“œ์œ„์น˜',
'๋งŒ๋‘',
'์Œˆ๋ฐฅ',
'๋ถ€๋Œ€์ฐŒ๊ฐœ',
'๊น€์น˜์ฐŒ๊ฐœ',
'๋œ์žฅ์ฐŒ๊ฐœ',
Expand All @@ -121,8 +103,6 @@ export const GAME_WORDS: WordTheme[] = [
'์žก์ฑ„',
'๋ถˆ๊ณ ๊ธฐ',
'๊ณฑ์ฐฝ',
'๋งˆ๋ผํƒ•',
'ํ› ๊ถˆ',
'์ดˆ์ฝœ๋ฆฟ',
'๊ณผ์ž',
'์‚ฌํƒ•',
Expand All @@ -146,17 +126,8 @@ export const GAME_WORDS: WordTheme[] = [
'๊ณ„๋ž€๋ง์ด',
'์˜ค๋ฏ€๋ผ์ด์Šค',
'๋‹ญ๋ณถ์Œํƒ•',
'๋‚™์ง€๋ณถ์Œ',
'์ญˆ๊พธ๋ฏธ๋ณถ์Œ',
'์กฑ๋ฐœ',
'๋ณด์Œˆ',
'๋ง‰์ฐฝ',
'๋Œ€์ฐฝ',
'์–‘๊ผฌ์น˜',
'๋งˆ๋ผ์ƒน๊ถˆ',
'๋”ค์„ฌ',
'์›”๋‚จ์Œˆ',
'๋‚˜์‹œ๊ณ ๋žญ',
],
},
{
Expand Down Expand Up @@ -192,7 +163,6 @@ export const GAME_WORDS: WordTheme[] = [
'๋น„๋ฒ„',
'๊ณ ์Šด๋„์น˜',
'๊ณต์ž‘์ƒˆ',
'ํ”Œ๋ผ๋ฐ๊ณ ',
'๋ฐฑ์กฐ',
'๋ฌผ๊ฐœ',
'๋ฐ”๋‹ค์‚ฌ์ž',
Expand All @@ -202,38 +172,27 @@ export const GAME_WORDS: WordTheme[] = [
'์˜ค์ง•์–ด',
'ํ•ดํŒŒ๋ฆฌ',
'๋ถˆ๊ฐ€์‚ฌ๋ฆฌ',
'๊ธฐ๋‹ˆํ”ผ๊ทธ',
'์นด๋ฉœ๋ ˆ์˜จ',
'์ด๊ตฌ์•„๋‚˜',
'๋ฑ€',
'๋„๋งˆ๋ฑ€',
'๊ธˆ๋ถ•์–ด',
'์—ด๋Œ€์–ด',
'์ž‰์–ด',
'๋ฉ”์ถ”๋ผ๊ธฐ',
'์–‘',
'๋ง',
'์†Œ',
'๋‹ญ',
'์˜ค๋ฆฌ',
'๊ฑฐ์œ„',
'๊ณต์ž‘',
'ํƒ€์กฐ',
'๋ฑ€',
'๋„๋งˆ๋ฑ€',
'๊ฐœ๊ตฌ๋ฆฌ',
'๋‘๋”์ง€',
'์ˆœ๋ก',
'์‚ฌ์Šด',
'๊ณ ๋ผ๋‹ˆ',
'์กฑ์ œ๋น„',
'๋‘๋”์ง€',
'๋ฐ•์ฅ',
'์•ŒํŒŒ์นด',
'๋ผ๋งˆ',
'๋ฏธ์–ด์บฃ',
'์ˆ˜๋‹ฌ',
'๋žซ์„œํŒ๋‹ค',
'๊ธฐ๋Ÿฌ๊ธฐ',
'์™œ๊ฐ€๋ฆฌ',
'๋‘๋ฃจ๋ฏธ',
'๋”ฑ๋”ฐ๊ตฌ๋ฆฌ',
'์ฐธ์ƒˆ',
'๊นŒ์น˜',
'๊นŒ๋งˆ๊ท€',
'๊นŒ์น˜',
'๋งค',
'์ฐธ์น˜',
'๊ฒŒ',
'์ƒˆ์šฐ',
'์กฐ๊ฐœ',
],
},
{
Expand All @@ -259,38 +218,16 @@ export const GAME_WORDS: WordTheme[] = [
'ํ™”๊ฐ€',
'๋ฏธ์šฉ์‚ฌ',
'๊ฑด์ถ•๊ฐ€',
'ํŒŒ์ผ๋Ÿฟ',
'์Šน๋ฌด์›',
'ํƒ์‹œ๊ธฐ์‚ฌ',
'๋ฒ„์Šค๊ธฐ์‚ฌ',
'๋†๋ถ€',
'์–ด๋ถ€',
'๋ชฉ์ˆ˜',
'์ „๊ธฐ๊ธฐ์‚ฌ',
'ํ”Œ๋กœ๋ฆฌ์ŠคํŠธ',
'์ œ๋นต์‚ฌ',
'์ˆ˜์˜์‚ฌ',
'์•ฝ์‚ฌ',
'์น˜๊ณผ์˜์‚ฌ',
'๋ฌผ๋ฆฌ์น˜๋ฃŒ์‚ฌ',
'์‹ฌ๋ฆฌ์ƒ๋‹ด์‚ฌ',
'ํ†ต์—ญ์‚ฌ',
'๋ฒˆ์—ญ๊ฐ€',
'์‚ฌ์ง„์ž‘๊ฐ€',
'์˜ํ™”๊ฐ๋…',
'์œ ํŠœ๋ฒ„',
'๋ฐ”๋ฆฌ์Šคํƒ€',
'์กฐ๋ฆฌ์‚ฌ',
'์›น๋””์ž์ด๋„ˆ',
'์ผ๋Ÿฌ์ŠคํŠธ๋ ˆ์ดํ„ฐ',
'๋ฉ”์ดํฌ์—…์•„ํ‹ฐ์ŠคํŠธ',
'ํ—ค์–ด๋””์ž์ด๋„ˆ',
'๋„ค์ผ์•„ํ‹ฐ์ŠคํŠธ',
'๋ณดํ—˜์„ค๊ณ„์‚ฌ',
'๋ถ€๋™์‚ฐ์ค‘๊ฐœ์‚ฌ',
'๋น„ํ–‰๊ธฐ์ •๋น„์‚ฌ',
'์†Œ๋ฏˆ๋ฆฌ์—',
'๋ฐ”ํ…๋”',
],
},
{
Expand Down Expand Up @@ -362,10 +299,47 @@ export const GAME_WORDS: WordTheme[] = [
'๋‹ค์ด๋น™',
'์„œํ•‘',
'ํด๋ผ์ด๋ฐ',
'์Šค์ฟผ์‹œ',
'ํฌ๋กœ์Šคํ•',
'์ค„๋„˜๊ธฐ',
'ํ•˜์ดํ‚น',
],
},
{
theme: '๊ฐ์ •/์ƒํƒœ',
words: [
'ํ–‰๋ณต',
'์Šฌํ””',
'ํ™”๋‚จ',
'๊ธฐ์จ',
'์šฐ์šธ',
'์„ค๋ ˜',
'๋ถˆ์•ˆ',
'ํ‰ํ™”๋กœ์›€',
'ํฅ๋ถ„',
'์™ธ๋กœ์›€',
'ํ”ผ๊ณค',
'์‚ฌ๋ž‘',
'๋ฏธ์›€',
'๊ธด์žฅ',
'์ŠคํŠธ๋ ˆ์Šค',
'๋งŒ์กฑ',
'ํ›„ํšŒ',
'๊ธฐ๋Œ€๊ฐ',
'์•ˆ๋„',
'์ถฉ๊ฒฉ',
],
},
{
theme: '๊ฐ€๊ตฌ',
words: [
'์ฑ…์ƒ',
'์˜์ž',
'์นจ๋Œ€',
'์†ŒํŒŒ',
'๋ƒ‰์žฅ๊ณ ',
'์ปคํŠผ',
'์‹ํƒ',
'ํ™”์žฅ๋Œ€',
'์„œ๋ž์žฅ',
'์˜ท์žฅ',
'์‹ฑํฌ๋Œ€',
],
},
];
2 changes: 1 addition & 1 deletion packages/backend/gameserver/src/game/game.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export class GameService {
return game.isGuessed;
}

async endGame(gsid: string): Promise<void> {
endGame(gsid: string) {
this.games.delete(gsid);
}
}
12 changes: 9 additions & 3 deletions packages/backend/gameserver/src/gateway/gateway.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,12 @@ export class GatewayGateway
speakerId: gameState.speakerQueue[0],
allUserIds: gameState.userIds,
};
room.isPlaying = true;

this.logger.logSocketEvent('send', 'start_game_success', {
personalGameState,
});

this.server
.to(this.getSocketIdByUserId(uid))
.emit('start_game_success', personalGameState);
Expand Down Expand Up @@ -330,6 +332,11 @@ export class GatewayGateway
isGuessed: false,
guessingWord: '',
});

this.gameService.endGame(gsid);
const room = this.roomService.getRoom(gsid);
room.readyUserIds.clear();
room.isPlaying = false;
}, 3000);
}
}
Expand Down Expand Up @@ -380,9 +387,8 @@ export class GatewayGateway
// ๊ฒŒ์ž„ ์ข…๋ฃŒ ๋ฐ ์ดˆ๊ธฐํ™”
await this.gameService.endGame(gsid);
const room = this.roomService.getRoom(gsid);
if (room) {
room.readyUserIds.clear();
}
room.readyUserIds.clear();
room.isPlaying = false;
} catch (error) {
this.logger.logError('guessing_error', error);
this.emitError(client, error.message);
Expand Down
13 changes: 11 additions & 2 deletions packages/backend/gameserver/src/room/room.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ export class RoomService {
private MAX_ROOM_SIZE = 6;

async createRoom(userId: string): Promise<string> {
const gsid = uuidv4();
let gsid: string;
do {
gsid = uuidv4().substring(0, 4);
} while (this.rooms.has(gsid));

const roomInfo: IRoomInfo = {
gsid,
userIds: new Set([userId]),
readyUserIds: new Set(),
hostUserId: userId,
isPlaying: false,
};

this.rooms.set(gsid, roomInfo);
Expand All @@ -26,10 +31,14 @@ export class RoomService {
throw new Error('์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋ฐฉ์ž…๋‹ˆ๋‹ค.');
}

if (room.userIds.size > this.MAX_ROOM_SIZE) {
if (room.userIds.size >= this.MAX_ROOM_SIZE) {
throw new Error('๋ฐฉ์ด ๊ฐ€๋“ ์ฐผ์Šต๋‹ˆ๋‹ค.');
}

if (room.isPlaying) {
throw new Error('๊ฒŒ์ž„์ด ์ง„ํ–‰์ค‘์ž…๋‹ˆ๋‹ค.');
}

room.userIds.add(userId);

return {
Expand Down
1 change: 1 addition & 0 deletions packages/backend/gameserver/src/room/types/room.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export interface IRoomInfo {
userIds: Set<string>;
readyUserIds: Set<string>;
hostUserId: string;
isPlaying: boolean;
}

export interface IRoomEventPayload {
Expand Down

0 comments on commit 09bd7e8

Please sign in to comment.