Skip to content

Commit

Permalink
Merge pull request #291 from boostcampwm-2024/dev-be
Browse files Browse the repository at this point in the history
Dev be to release
  • Loading branch information
NewCodes7 authored Nov 28, 2024
2 parents 0d0654b + 9f24c8c commit 91cefc5
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 20 deletions.
13 changes: 7 additions & 6 deletions BE/src/game/game.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ import { ExceptionMessage } from '../common/constants/exception-message';
@UseInterceptors(GameActivityInterceptor)
@UseFilters(new WsExceptionFilter())
@WebSocketGateway({
// cors: {
// origin: ['*,', 'https://admin.socket.io'], //์ด๋Ÿฌ๋ฉด ๋ฐฐํฌ์—์„œ cors์˜ค๋ฅ˜ ์ƒ๊น€
// credentials: true
// },
cors: {
origin: '*' //์ด๋Ÿฌ๋ฉด ๋ฐฐํฌ์—์„œ admin ui ์ ‘๊ทผ ์•ˆ๋จ
origin: [
'https://news.taskify.shop',
'https://quizground.duckdns.org',
'https://admin.socket.io'
],
credentials: true
},
namespace: '/game'
})
Expand Down Expand Up @@ -179,7 +180,7 @@ export class GameGateway {

setNewPlayerIdToCookie(headers) {
const playerId = uuidv4();
headers['Set-Cookie'] = serialize('playerId', playerId);
headers['Set-Cookie'] = serialize('playerId', playerId, { sameSite: 'none', secure: true });
return playerId;
}

Expand Down
9 changes: 6 additions & 3 deletions BE/src/game/redis/subscribers/player.subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ export class PlayerSubscriber extends RedisSubscriber {
if (!playerId || message !== 'hset') {
return;
}

const key = `Player:${playerId}`;

await this.handlePlayerChanges(key, playerId, server);
});
}
Expand Down Expand Up @@ -62,10 +60,15 @@ export class PlayerSubscriber extends RedisSubscriber {
}

private async handlePlayerJoin(playerId: string, playerData: any, server: Namespace) {
const findRoom = await this.redis.hgetall(REDIS_KEY.ROOM(playerData.gameId));
const findHost = findRoom.host;
const isHost = findHost === playerId;

const newPlayer = {
playerId,
playerName: playerData.playerName,
playerPosition: [parseFloat(playerData.positionX), parseFloat(playerData.positionY)]
playerPosition: [parseFloat(playerData.positionX), parseFloat(playerData.positionY)],
isHost
};

server.to(playerData.gameId).emit(SocketEvents.JOIN_ROOM, {
Expand Down
39 changes: 33 additions & 6 deletions BE/src/game/redis/subscribers/timer.subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ export class TimerSubscriber extends RedisSubscriber {
continue;
}

const selectAnswer = this.calculateAnswer(player.positionX, player.positionY);
const selectAnswer = this.calculateAnswer(
player.positionX,
player.positionY,
quizList.length
);
// this.logger.verbose(selectAnswer);

await this.redis.set(`${REDIS_KEY.PLAYER(clientId)}:Changes`, 'AnswerCorrect');
if (selectAnswer.toString() === quiz.answer) {
Expand Down Expand Up @@ -161,10 +166,32 @@ export class TimerSubscriber extends RedisSubscriber {
this.logger.verbose(`startQuizTime: ${gameId} - ${newQuizNum}`);
}

private calculateAnswer(positionX: string, positionY: string): number {
if (parseFloat(positionY) < 0.5) {
return parseFloat(positionX) < 0.5 ? 1 : 2;
}
return parseFloat(positionX) < 0.5 ? 3 : 4;
private calculateAnswer(positionX: string, positionY: string, quizLen: number): number {
const x = parseFloat(positionX);
const y = parseFloat(positionY);

// ํ–‰์˜ ๊ฐœ์ˆ˜ ๊ณ„์‚ฐ (2์—ด ๊ณ ์ •์ด๋ฏ€๋กœ ์ด ๊ฐœ์ˆ˜์˜ ์ ˆ๋ฐ˜์„ ์˜ฌ๋ฆผ)
const rows = Math.ceil(quizLen / 2);

// Y ์ขŒํ‘œ๋ฅผ ํ–‰ ๋ฒˆํ˜ธ๋กœ ๋ณ€ํ™˜
const rowIndex = Math.floor(y * rows);

// X ์ขŒํ‘œ๋กœ ์™ผ์ชฝ/์˜ค๋ฅธ์ชฝ ๊ฒฐ์ •
const colIndex = x < 0.5 ? 0 : 1;

// ์ตœ์ข… ์„ ํƒ์ง€ ๋ฒˆํ˜ธ ๊ณ„์‚ฐ
const answer = rowIndex * 2 + colIndex + 1;

// ์‹ค์ œ ์„ ํƒ์ง€ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜์ง€ ์•Š๋„๋ก ๋ณด์ •
return Math.min(answer, quizLen);
// return (
// Math.round(
// parseFloat(positionX) + Math.floor(parseFloat(positionY) * Math.ceil(quizLen / 2))
// ) * 2
// );
// if (parseFloat(positionY) < 0.5) {
// return parseFloat(positionX) < 0.5 ? 1 : 2;
// }
// return parseFloat(positionX) < 0.5 ? 3 : 4;
}
}
20 changes: 15 additions & 5 deletions BE/src/game/service/game.room.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,14 @@ export class GameRoomService {
playerPosition: [parseFloat(player.positionX), parseFloat(player.positionY)]
});
}

// ๋ฐฉ์žฅ์˜ id๋ฅผ ๋ณด๋‚ด์ค˜์•ผํ•จ
const findRoom = await this.redis.hgetall(REDIS_KEY.ROOM(gameId));
const findHost = findRoom.host;
const isHost = findHost === clientId;
client.join(gameId);
client.emit(SocketEvents.GET_SELF_ID, { playerId: clientId });
client.emit(SocketEvents.JOIN_ROOM, { players });
client.emit(SocketEvents.JOIN_ROOM, { players, isHost });
return;
}

Expand Down Expand Up @@ -121,16 +126,19 @@ export class GameRoomService {
await this.redis.sadd(REDIS_KEY.ROOM_PLAYERS(gameId), clientId);

const players = [];
const roomData = await this.redis.hgetall(REDIS_KEY.ROOM(gameId));

for (const playerId of currentPlayers) {
const player = await this.redis.hgetall(REDIS_KEY.PLAYER(playerId));
const isHost = roomData.host === playerId;
players.push({
playerId,
playerName: player.playerName,
playerPosition: [parseFloat(player.positionX), parseFloat(player.positionY)]
playerPosition: [parseFloat(player.positionX), parseFloat(player.positionY)],
isHost
});
}

const roomData = await this.redis.hgetall(REDIS_KEY.ROOM(gameId));
client.emit(SocketEvents.UPDATE_ROOM_OPTION, {
title: roomData.title,
gameMode: roomData.gameMode,
Expand Down Expand Up @@ -300,7 +308,9 @@ export class GameRoomService {
const targetPlayer = await this.redis.hgetall(targetPlayerKey);
this.gameValidator.validatePlayerExists(SocketEvents.KICK_ROOM, targetPlayer);
await this.redis.set(`${targetPlayerKey}:Changes`, 'Kicked', 'EX', 6000); // ํ•ด๋‹นํ”Œ๋ ˆ์ด์–ด์˜ ๋ณ€ํ™”์ •๋ณด 10๋ถ„ ํ›„์— ์‚ญ์ œ

await this.handlePlayerExit(kickPlayerId);
await this.redis.hset(targetPlayerKey, {
isAlive: '0'
});
// await this.handlePlayerExit(kickPlayerId);
}
}

0 comments on commit 91cefc5

Please sign in to comment.