Skip to content

Commit

Permalink
[BE - #80 ] pinCode check API 구현 (#81)
Browse files Browse the repository at this point in the history
* fix: participant emit 값 변경

* feat: pinCode 체크 api 구현
  • Loading branch information
nowChae authored Nov 19, 2024
1 parent db446f1 commit c4aec8e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
3 changes: 2 additions & 1 deletion packages/server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { GameGateway } from './module/game/game.gateway';
import { RedisService } from './config/database/redis/redis.service';
import { RedisModule } from '@nestjs-modules/ioredis'; // 추가
import { GameService } from './module/game/games/game.service';
import { GameController } from './module/game/games/game.controller';
@Module({
imports: [
UserModule,
Expand All @@ -36,7 +37,7 @@ import { GameService } from './module/game/games/game.service';
url: process.env.REDIS_URL,
}),
],
controllers: [AppController],
controllers: [AppController, GameController],
providers: [AppService, GameGateway, RedisService, GameService],
exports: [RedisService],
})
Expand Down
9 changes: 4 additions & 5 deletions packages/server/src/module/game/game.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,18 @@ export class GameGateway implements OnGatewayConnection, OnGatewayDisconnect {
const { pinCode, nickname } = payload;
const clientInfo = { pinCode, nickname };

client.join(pinCode); // 같은 룸에 들어오게 된것
client.join(pinCode);

const participantSid = uuidv4();
this.redisService.set(`participant_sid=${participantSid}`, JSON.stringify(clientInfo));
client.emit('session', participantSid);

// 레디스 갱신
const gameInfo = JSON.parse(await this.redisService.get(`gameId=${pinCode}`));
const newParticipantList = gameInfo.participantList.push(nickname);
gameInfo.participantList.push(nickname);
this.redisService.set(`gameId=${pinCode}`, JSON.stringify(gameInfo));

client.emit('nickname', newParticipantList);
client.to(pinCode).emit('nickname', newParticipantList);
client.emit('nickname', gameInfo.participantList);
client.to(pinCode).emit('nickname', gameInfo.participantList);

return;
}
Expand Down
13 changes: 13 additions & 0 deletions packages/server/src/module/game/games/game.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Controller, Get, Param, Post, UsePipes, ValidationPipe } from '@nestjs/common';
import { GameService } from './game.service';

@Controller('api')
export class GameController {
constructor(private readonly gameService: GameService) {}

@Get('games/:pinCode')
@UsePipes(ValidationPipe)
async checkPinCode(@Param('pinCode') pinCode: string) {
return await this.gameService.checkPinCode(pinCode);
}
}
15 changes: 15 additions & 0 deletions packages/server/src/module/game/games/game.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,33 @@ import { Injectable, HttpException, HttpStatus, Param } from '@nestjs/common';
import { ChoiceRepository } from '../../quiz/quizzes/repositories/choice.repository';
import { ClassRepository } from '../../quiz/quizzes/repositories/class.repository';
import { QuizRepository } from '../../quiz/quizzes/repositories/quiz.repository';
import { RedisService } from '../../../config/database/redis/redis.service';

@Injectable()
export class GameService {
constructor(
private readonly classRepository: ClassRepository,
private readonly quizRepository: QuizRepository,
private readonly choiceRepository: ChoiceRepository,
private readonly redisService: RedisService,
) {}

async cachingQuizData(classId: number) {
const classWithRelations = await this.classRepository.findClassWithRelations(classId);

return classWithRelations;
}

async checkPinCode(pinCode: string) {
try {
const result = await this.redisService.get(`gameId=${pinCode}`);

if (result) {
return { isExist: true, message: 'pinCode exists.' };
}
return { isExist: false, message: 'pinCode not exists.' };
} catch (error) {
console.error('error: ', error);
}
}
}

0 comments on commit c4aec8e

Please sign in to comment.