Skip to content

Commit

Permalink
fix: 실험 2
Browse files Browse the repository at this point in the history
  • Loading branch information
tomatoziyun committed Dec 19, 2023
1 parent bbcfecd commit 1b4f657
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 35 deletions.
6 changes: 6 additions & 0 deletions src/game/dto/emit-event-invitation-reply.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class EmitEventInvitationReplyDto {
targetUserId: number;
targetUserChannelSocketId: string;
isAccepted: boolean;
gameId: number | null;
}
2 changes: 1 addition & 1 deletion src/game/game.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class GameController {
gameType: invitationRequestDto.gameType,
};

await this.gameService.createInvitation(invitationParamDto);
return await this.gameService.createInvitation(invitationParamDto);
}

/**
Expand Down
72 changes: 38 additions & 34 deletions src/game/game.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
BadRequestException,
ImATeapotException,
Injectable,
} from '@nestjs/common';
import { BadRequestException, Injectable } from '@nestjs/common';
import { GameRepository } from './game.repository';
import { UsersRepository } from '../users/users.repository';
import { CreateGameInvitationParamDto } from './dto/create-invitation-param.dto';
Expand All @@ -13,10 +9,10 @@ import { GameInvitationRepository } from './game-invitation.repository';
import { DeleteGameInvitationParamDto } from './dto/delete-invitation-param.dto';
import { GameStatus, UserStatus } from '../common/enum';
import { acceptGameParamDto } from './dto/accept-game-param.dto';
import { GatewaySendInvitationReplyDto } from './dto/gateway-send-invitation-reaponse.dto';
import { User } from '../users/entities/user.entity';
import * as moment from 'moment';
import { BlocksRepository } from '../users/blocks.repository';
import { EmitEventInvitationReplyDto } from './dto/emit-event-invitation-reply.dto';

@Injectable()
export class GameService {
Expand All @@ -40,13 +36,13 @@ export class GameService {
// 상대가 존재하는 유저인지
const invitedUser = await this.checkUserExist(invitedUserId);

// 상대가 ONLINE 인지 => 접속 중인 유저가 아니라면 없던 일이 됨
// 상대가 OFFLINE / INGAME이면 없던 일이 됨
if (
invitedUser.status === UserStatus.OFFLINE ||
invitedUser.status !== UserStatus.ONLINE ||
!invitedUser.channelSocketId
)
throw new ImATeapotException(
`초대된 유저 ${invitedUserId} 는 OFFLINE 상태입니다`,
throw new BadRequestException(
`초대된 유저 ${invitedUserId} 는 OFFLINE 상태이거나 게임 중입니다`,
);

const isBlocked = await this.blocksRepository.findOne({
Expand All @@ -68,7 +64,7 @@ export class GameService {
const diff = currentTime.diff(olderInvitation.createdAt, 'seconds');

if (diff < 10) {
throw new ImATeapotException(
throw new BadRequestException(
`아직 응답을 기다리고 있는 초대입니다`,
);
} else {
Expand All @@ -93,6 +89,11 @@ export class GameService {
gameType: gameType,
};
await this.gameGateway.inviteGame(gatewayInvitationParamDto);

const createInvitationResponseDto = {
gameInvitationId: gameInvitation.id,
};
return createInvitationResponseDto;
}

async createGame(createGameParamDto: acceptGameParamDto) {
Expand All @@ -106,7 +107,7 @@ export class GameService {
},
});
if (!invitation)
throw new ImATeapotException(
throw new BadRequestException(
`해당하는 invitation id ${invitationId} 가 없습니다`,
);

Expand All @@ -115,28 +116,28 @@ export class GameService {
const diff = currentTime.diff(invitation.createdAt, 'seconds');

if (diff >= 10) {
throw new ImATeapotException(
`해당하는 invitation id ${invitationId}시간초과로 유효하지 않습니다`,
throw new BadRequestException(
`시간초과로 유효하지 않은 요청입니다`,
);
}

// 두 유저 모두 ONLINE 인지 확인
if (
invitedUser.status === UserStatus.OFFLINE ||
invitedUser.status !== UserStatus.ONLINE ||
!invitedUser.channelSocketId
)
throw new ImATeapotException(
`초대된 유저 ${invitedUser.id} 는 OFFLINE 상태입니다`,
throw new BadRequestException(
`초대된 유저 ${invitedUser.id} 는 OFFLINE 상태이거나 게임중입니다`,
);
const invitingUser = await this.checkUserExist(
invitation.invitingUserId,
);
if (
invitingUser.status === UserStatus.OFFLINE ||
invitingUser.status !== UserStatus.ONLINE ||
!invitingUser.channelSocketId
)
throw new ImATeapotException(
`초대한 유저 ${invitingUser.id} 는 OFFLINE 상태입니다`,
throw new BadRequestException(
`초대한 유저 ${invitingUser.id} 는 OFFLINE 상태이거나 게임중입니다`,
);

/* TODO: DB에 쌓인 유효하지 않은 초대장들은 어떻게 지우지 ?
Expand Down Expand Up @@ -168,14 +169,13 @@ export class GameService {
await this.gameInvitationRepository.softDelete(invitationId);

// 두 유저에게 game id emit
const invitationReplyToInvitingUserDto: GatewaySendInvitationReplyDto =
{
targetUserId: invitingUser.id,
targetUserChannelSocketId: invitingUser.channelSocketId,
isAccepted: true,
gameId: game.id,
};
const invitationReplyToInvitedUserDto: GatewaySendInvitationReplyDto = {
const invitationReplyToInvitingUserDto: EmitEventInvitationReplyDto = {
targetUserId: invitingUser.id,
targetUserChannelSocketId: invitingUser.channelSocketId,
isAccepted: true,
gameId: game.id,
};
const invitationReplyToInvitedUserDto: EmitEventInvitationReplyDto = {
targetUserId: invitedUser.id,
targetUserChannelSocketId: invitedUser.channelSocketId,
isAccepted: true,
Expand Down Expand Up @@ -203,7 +203,7 @@ export class GameService {
},
});
if (!invitation)
throw new ImATeapotException(
throw new BadRequestException(
`해당하는 invitation id ${invitationId} 가 없습니다`,
);

Expand All @@ -212,7 +212,9 @@ export class GameService {
const diff = currentTime.diff(invitation.createdAt, 'seconds');

if (diff >= 10) {
throw new ImATeapotException(`시간초과로 유효하지 않은 요청입니다`);
throw new BadRequestException(
`시간초과로 유효하지 않은 요청입니다`,
);
}

await this.gameInvitationRepository.softDelete(invitationId);
Expand All @@ -231,15 +233,17 @@ export class GameService {
},
});
if (!invitation)
throw new ImATeapotException(
throw new BadRequestException(
`해당하는 invitation id ${invitationId} 가 없습니다`,
);
// 10초 지나면 유효하지 않은 취소 (악의 취소)
const currentTime = moment();
const diff = currentTime.diff(invitation.createdAt, 'seconds');

if (diff >= 10) {
throw new ImATeapotException(`시간초과로 유효하지 않은 요청입니다`);
throw new BadRequestException(
`시간초과로 유효하지 않은 요청입니다`,
);
}

await this.gameInvitationRepository.softDelete(invitationId);
Expand All @@ -253,11 +257,11 @@ export class GameService {
invitingUser.status === UserStatus.OFFLINE ||
!invitingUser.channelSocketId
)
throw new ImATeapotException(
throw new BadRequestException(
`초대한 유저 ${invitingUser.id} 는 OFFLINE 상태입니다`,
);

const sendInvitationReplyDto: GatewaySendInvitationReplyDto = {
const sendInvitationReplyDto: EmitEventInvitationReplyDto = {
targetUserId: invitingUser.id,
targetUserChannelSocketId: invitingUser.channelSocketId,
isAccepted: false,
Expand Down

0 comments on commit 1b4f657

Please sign in to comment.