Skip to content

Commit

Permalink
Merge pull request #89 from Gamegoo-repo/feat/84
Browse files Browse the repository at this point in the history
[Feat/84] 시스템 메세지 구현
  • Loading branch information
rimi3226 authored Sep 23, 2024
2 parents 3e4bd58 + 6cc7caa commit 417f846
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 10 deletions.
46 changes: 46 additions & 0 deletions app/controller/systemMessageController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const { successResponse, failResponse } = require("../common/responseFormatter");

function countUsersInMatching(io) {
return async (req, res) => {

const { tier } = req.query; // 쿼리 파라미터에서 데이터 받기

// 현재 연결된 socket 중 해당 memberId를 가진 socket 객체 list 추출
let numberOfSocketsInRoom=0;
try {
const connectedSockets = await io.fetchSockets();

for (let i = 1; i <= 4; i++) {

// 매칭 room에 들어가있는 소켓들 필터링
const roomName = "GAMEMODE_" + i;
const socketsInRoom = connectedSockets.filter(socket => socket.rooms.has(roomName));

// Tier 값에 따라 요청한 Tier 에 해당하는 매칭 사용자 수 계산
if(tier){
for (const connSocket of socketsInRoom) {
if(connSocket.myMatchingInfo.tier == tier){
numberOfSocketsInRoom++;
}
}
}
// Tier 상관 없이 전체 매칭 사용자 수 계산
else{
numberOfSocketsInRoom = numberOfSocketsInRoom + socketsInRoom.length;
}
}
res.status(200).json(successResponse({number:numberOfSocketsInRoom}));

} catch (error) {
res.status(500).json(failResponse("SOCKET510", "매칭 중인 인원을 불러오는 도중 에러가 발생했습니다."));
}


};
}

module.exports = (io) => {
return {
countUsersInMatching: countUsersInMatching(io),
};
};
7 changes: 7 additions & 0 deletions app/routers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = (io) => {
const { login } = require("../controller/loginController")(io); // io 객체를 전달
const { logout } = require("../controller/logoutController")(io);
const { socketRoomJoin, emitSystemMessage } = require("../controller/socketController")(io);
const { countUsersInMatching, countUsersInMatchingByTier } = require("../controller/systemMessageController")(io);

router.use("/img", express.static(path.join(__dirname, "../../public/img")));

Expand All @@ -23,5 +24,11 @@ module.exports = (io) => {
// 8080 -> 3000 으로 보낼 api. 특정 memberId를 갖는 socket 모두에게 시스템 메시지 event emit
router.post("/socket/sysmessage", emitSystemMessage);

// 시스템 메시지 : 지금 몇 명이 매칭을 기다리고 있는지
// 엔드포인트 뒤에 query로 tier 붙이면 각 티어마다 몇 명 있는지 조회
// ex) /socket/message?tier=BRONZE
router.get('/socket/message',countUsersInMatching);


return router;
};
24 changes: 15 additions & 9 deletions socket/handlers/matching/matchingSocketListeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,10 @@ async function setupMatchSocketListeners(socket, io) {
console.log("================= matching_reject ======================");
const otherSocket = await getSocketIdByMemberId(io, socket.matchingTarget);

// 26) 매칭 REJECT API 요청 (상대, 나 둘 다 status 변경하기)
await updateBothMatchingStatusApi(socket, "FAIL", socket.matchingTarget);
if(socket.gameMode !=null){
// 26) 매칭 REJECT API 요청 (상대, 나 둘 다 status 변경하기)
await updateBothMatchingStatusApi(socket, "FAIL", socket.matchingTarget);
}

// 27) 상대 client에게 matching-fail emit
if (otherSocket) {
Expand All @@ -159,10 +161,10 @@ async function setupMatchSocketListeners(socket, io) {

socket.on("matching-fail", async (request) => {
console.log("================= matching_fail ======================");

// 24) 매칭 FAIL API 요청 (나의 status만 변경)
await updateMatchingStatusApi(socket, "FAIL");

if(socket.gameMode !=null){
// 24) 매칭 FAIL API 요청 (나의 status만 변경)
await updateMatchingStatusApi(socket, "FAIL");
}
// 26) socket.target 제거
socket.matchingTarget = null;

Expand All @@ -173,8 +175,10 @@ async function setupMatchSocketListeners(socket, io) {
socket.on("matching-quit", async (request) => {
console.log("================= matching_quit ======================");

// 2) 매칭 FAIL API 요청 (나의 status만 변경)
await updateMatchingStatusApi(socket, "QUIT");
if(socket.gameMode !=null){
// 2) 매칭 FAIL API 요청 (나의 status만 변경)
await updateMatchingStatusApi(socket, "QUIT");
}

// 4~6) room leave, 다른 socket들의 priorityTree에서 제거, 두 socket의 priorityTree 초기화
const roomName = "GAMEMODE_" + socket.gameMode;
Expand Down Expand Up @@ -211,7 +215,9 @@ async function setupMatchSocketListeners(socket, io) {

// 17) matching_status 변경
try {
const result = await updateMatchingStatusApi(socket, "FAIL");
if(socket.gameMode !=null){
const result = await updateMatchingStatusApi(socket, "FAIL");
}
if (result) {
console.log("Matching Not Found 처리 완료");
}
Expand Down
4 changes: 3 additions & 1 deletion socket/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ function initializeSocket(server) {
// 해당 socket이 memberId를 가질 때에만(로그인한 소켓인 경우에만)
if (socket.memberId) {
// (#6-2) 매칭 status 변경 API 요청
await updateMatchingStatusApi(socket, "QUIT");
if(socket.gameMode !=null){
await updateMatchingStatusApi(socket, "QUIT");
}

// (#6-4) 매칭 room에 join 되어 있는 경우, 해당 room의 모든 소켓의 priorityTree 에서 해당 소켓 노드 제거
const roomName = "GAMEMODE_" + socket.gameMode;
Expand Down

0 comments on commit 417f846

Please sign in to comment.