Skip to content

Commit

Permalink
[BE] 사용자는 준비완료 상태를 나타낼 수 있다. (#112)
Browse files Browse the repository at this point in the history
* feat: player-data DTO 추가

* feat: 준비 완료, 강퇴 기능 추가

* test: 준비 완료, 강퇴 테스트

* docs: swagger 정리
  • Loading branch information
student079 authored Nov 18, 2024
1 parent 8975f48 commit 8e73dc4
Show file tree
Hide file tree
Showing 13 changed files with 472 additions and 44 deletions.
2 changes: 2 additions & 0 deletions be/gameServer/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import appConfig from './config/app.config';
import { RoomsModule } from './modules/rooms/rooms.module';
import { GamesModule } from './modules/games/games.module';

@Module({
imports: [
Expand All @@ -10,6 +11,7 @@ import { RoomsModule } from './modules/rooms/rooms.module';
load: [appConfig],
}),
RoomsModule,
GamesModule,
],
})
export class AppModule {}
23 changes: 23 additions & 0 deletions be/gameServer/src/modules/games/games.gateway.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { WebSocketGateway, WebSocketServer } from '@nestjs/websockets';
import { Server } from 'socket.io';
import { RedisService } from '../../redis/redis.service';
import { Logger, UseFilters } from '@nestjs/common';
import { WsExceptionsFilter } from '../../common/filters/ws-exceptions.filter';

@WebSocketGateway({
namespace: '/games',
cors: {
origin: '*',
methods: ['GET', 'POST'],
credentials: true,
},
})
@UseFilters(WsExceptionsFilter)
export class GamesGateway {
private readonly logger = new Logger(GamesGateway.name);

@WebSocketServer()
server: Server;

constructor(private readonly redisService: RedisService) {}
}
10 changes: 10 additions & 0 deletions be/gameServer/src/modules/games/games.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { RedisModule } from '../../redis/redis.module';
import { GamesGateway } from './games.gateway';

@Module({
imports: [RedisModule],
providers: [GamesGateway],
controllers: [],
})
export class GamesModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Controller } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';

@ApiTags('Games (WebSocket: 서버에서 발행하는 이벤트)')
@Controller('games')
export class RoomsWebSocketEmitController {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Controller } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';

@ApiTags('Games (WebSocket: 서버에서 수신하는 이벤트)')
@Controller('games')
export class RoomsWebSocketOnController {}
15 changes: 15 additions & 0 deletions be/gameServer/src/modules/players/dto/player-data.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ApiProperty } from '@nestjs/swagger';

export class PlayerDataDto {
@ApiProperty({
example: 'playerNickname123',
description: '플레이어의 닉네임',
})
playerNickname: string;

@ApiProperty({
example: true,
description: '플레이어의 준비 상태 (true: 준비 완료, false: 대기 중)',
})
isReady: boolean;
}
17 changes: 14 additions & 3 deletions be/gameServer/src/modules/rooms/dto/room-data.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ApiProperty } from '@nestjs/swagger';
import { PlayerDataDto } from '../../players/dto/player-data.dto';

export class RoomDataDto {
@ApiProperty({
Expand All @@ -20,10 +21,20 @@ export class RoomDataDto {
hostNickname: string;

@ApiProperty({
example: ['hostNickname123'],
description: '현재 방에 참여한 플레이어 목록',
type: [PlayerDataDto],
example: [
{
nickname: 'hostNickname123',
ready: true,
},
{
nickname: 'player1',
ready: false,
},
],
description: '현재 방에 참여한 플레이어 목록과 준비 상태',
})
players: string[];
players: PlayerDataDto[];

@ApiProperty({
example: 'waiting',
Expand Down
11 changes: 8 additions & 3 deletions be/gameServer/src/modules/rooms/room-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { RoomDataDto } from './dto/room-data.dto';
import { PlayerDataDto } from '../players/dto/player-data.dto';

export const MAX_PLAYERS = 4;

Expand All @@ -10,16 +11,20 @@ export const isNicknameTaken = (
roomData: RoomDataDto,
playerNickname: string,
): boolean => {
return roomData.players.includes(playerNickname);
return roomData.players.some(
(player: PlayerDataDto) => player.playerNickname === playerNickname,
);
};

export const removePlayerFromRoom = (
roomData: RoomDataDto,
nickname: string,
): void => {
roomData.players = roomData.players.filter((player) => player !== nickname);
roomData.players = roomData.players.filter(
(player: PlayerDataDto) => player.playerNickname !== nickname,
);
};

export const changeRoomHost = (roomData: RoomDataDto): void => {
roomData.hostNickname = roomData.players[0];
roomData.hostNickname = roomData.players[0].playerNickname;
};
Loading

0 comments on commit 8e73dc4

Please sign in to comment.