Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor/57] 매칭 상태 수정 API 변경 반영 #58

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 100 additions & 61 deletions socket/apis/matchApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,78 +10,117 @@ const API_SERVER_URL = config.apiServerUrl;
* @param {*} socket
* @returns
*/
async function fetchMatching(socket, request) {
try {

const gameStyleIdList = [request.gameStyle1, request.gameStyle2, request.gameStyle3];
console.log(socket.token);
const response = await axios.post(`${API_SERVER_URL}/v1/matching/priority`, {

gameMode: request.gameMode,
mike: request.mike,
matchingType: request.matchingType,
mainP: request.mainP,
subP: request.subP,
wantP: request.wantP,
gameStyleIdList: gameStyleIdList
async function fetchMatchingApi(socket, request) {
try {
const gameStyleIdList = [request.gameStyle1, request.gameStyle2, request.gameStyle3];
const response = await axios.post(
`${API_SERVER_URL}/v1/matching/priority`,
{
gameMode: request.gameMode,
mike: request.mike,
matchingType: request.matchingType,
mainP: request.mainP,
subP: request.subP,
wantP: request.wantP,
gameStyleIdList: gameStyleIdList,
},
{
headers: {
Authorization: `Bearer ${socket.token}`, // Include JWT token in header
},
{
headers: {
Authorization: `Bearer ${socket.token}`, // Include JWT token in header
},
});
if (response.data.isSuccess) {
return response.data.result;
}
} catch (error) {
if (error.response && error.response.data) {
const data = error.response.data;
if (["JWT400", "JWT401", "JWT404"].includes(data.code)) {
console.error("JWT token Error: ", data.message);
throw new JWTTokenError(`JWT token Error: ${data.message}`, data.code);
}
console.error("Failed POST matching API ", data.message);
throw new Error(`Failed POST matching API: ${data.message}`);
} else {
throw new Error(`Request failed: ${error.message}`);
}
}
);
if (response.data.isSuccess) {
return response.data.result;
}
} catch (error) {
if (error.response && error.response.data) {
const data = error.response.data;
if (["JWT400", "JWT401", "JWT404"].includes(data.code)) {
console.error("JWT token Error: ", data.message);
throw new JWTTokenError(`JWT token Error: ${data.message}`, data.code);
}
console.error("Failed POST matching API ", data.message);
throw new Error(`Failed POST matching API: ${data.message}`);
} else {
throw new Error(`Request failed: ${error.message}`);
}
}
}

/**
* 매칭 상태를 수정하는 API
* 나+상대방의 매칭 상태를 수정하는 API
* @param {*} socket
* @param {*} request
* @param {*} status
* @param {*} targetMemberId
* @returns
*/
async function updateMatchingStatus(socket, status) {
try {
const response = await axios.put(`${API_SERVER_URL}/v1/matching`, {
status: status
}, {
headers: {
Authorization: `Bearer ${socket.token}`, // Include JWT token in header
},
});
async function updateBothMatchingStatusApi(socket, status, targetMemberId) {
try {
const response = await axios.patch(
`${API_SERVER_URL}/v1/matching/status/target/${targetMemberId}`,
{
status: status,
},
{
headers: {
Authorization: `Bearer ${socket.token}`, // Include JWT token in header
},
}
);

if (response.data.isSuccess) {
return response.data.result;
}
} catch (error) {
if (error.response && error.response.data) {
const data = error.response.data;
if (["JWT400", "JWT401", "JWT404"].includes(data.code)) {
console.error("JWT token Error: ", data.message);
throw new JWTTokenError(`JWT token Error: ${data.message}`, data.code);
}
console.error("Failed PUT matching status API ", data.message);
throw new Error(`Failed PUT matching status API: ${data.message}`);
} else {
throw new Error(`Request failed: ${error.message}`);
}
if (response.data.isSuccess) {
return response.data.result;
}
} catch (error) {
if (error.response && error.response.data) {
const data = error.response.data;
if (["JWT400", "JWT401", "JWT404"].includes(data.code)) {
console.error("JWT token Error: ", data.message);
throw new JWTTokenError(`JWT token Error: ${data.message}`, data.code);
}
console.error("Failed PUT matching status API ", data.message);
throw new Error(`Failed PUT matching status API: ${data.message}`);
} else {
throw new Error(`Request failed: ${error.message}`);
}
}
}

async function updateMatchingStatusApi(socket, status) {
try {
const response = await axios.patch(
`${API_SERVER_URL}/v1/matching/status`,
{
status: status,
},
{
headers: {
Authorization: `Bearer ${socket.token}`, // Include JWT token in header
},
}
);

if (response.data.isSuccess) {
return response.data.result;
}
} catch (error) {
if (error.response && error.response.data) {
const data = error.response.data;
if (["JWT400", "JWT401", "JWT404"].includes(data.code)) {
console.error("JWT token Error: ", data.message);
throw new JWTTokenError(`JWT token Error: ${data.message}`, data.code);
}
console.error("Failed PUT matching status API ", data.message);
throw new Error(`Failed PUT matching status API: ${data.message}`);
} else {
throw new Error(`Request failed: ${error.message}`);
}
}
}

module.exports = {
fetchMatching, updateMatchingStatus
fetchMatchingApi,
updateMatchingStatusApi,
updateBothMatchingStatusApi,
};
4 changes: 2 additions & 2 deletions socket/common/memberSocketMapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ async function getSocketIdsByMemberIds(io, memberIdList) {
*/
async function getSocketIdByMemberId(io, memberId) {
const connectedSockets = await io.fetchSockets();
console.log("requested memberId: ", memberId);
//console.log("requested memberId: ", memberId);
for (const connSocket of connectedSockets) {
console.log("connSocket.memberId: ", connSocket.memberId);
//console.log("connSocket.memberId: ", connSocket.memberId);
if (memberId == connSocket.memberId) {
return connSocket;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ async function findMatching(socket, io, value) {
// 우선순위 값이 value를 넘는 모든 소켓 확인하기
while (socket.highestPriorityNode.priorityValue >= value) {
const otherSocket = await getSocketIdByMemberId(io, socket.highestPriorityNode.memberId);
console.log("해당 소켓에 우선순위 값이 value를 넘는 다른 소켓이 있다.", socket.memberId, " ", otherSocket.memberId);
console.log(
"해당 소켓에 우선순위 값이 value를 넘는 다른 소켓이 있다. socket.memberId:",
socket.memberId,
", otherSocket.memberId:",
otherSocket.memberId
);
if (!otherSocket.highestPriorityNode) {
return null;
} else if (otherSocket.highestPriorityNode.priorityValue >= value) {
Expand Down
17 changes: 9 additions & 8 deletions socket/handlers/matching/matchingListeners.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { fetchMatching, updateMatchingStatus } = require("../../apis/matchApi");
const { fetchMatchingApi, updateMatchingStatusApi, updateBothMatchingStatusApi } = require("../../apis/matchApi");
const {
updateOtherPriorityTrees,
updatePriorityTree,
Expand All @@ -21,8 +21,8 @@ async function setupMatchListeners(socket, io) {

// 2) socket.id가 소켓 룸 "GAMEMODE_" + gameMode에 있는지 확인
const usersInRoom = io.sockets.adapter.rooms.get(roomName) || new Set();
console.log(socket.id, " ", socket.memberId);
console.log(usersInRoom);
// console.log(socket.id, " ", socket.memberId);
//console.log(usersInRoom);
if (usersInRoom.has(socket.id)) {
console.log("ERROR : 이미 매칭을 시도한 소켓입니다.");
emitError(socket, "You are already in the matching room for this game mode.");
Expand All @@ -34,7 +34,7 @@ async function setupMatchListeners(socket, io) {

try {
// 4) 8080서버에 우선순위 계산 API 요청
const result = await fetchMatching(socket, request);
const result = await fetchMatchingApi(socket, request);

// 5) 내 우선순위 트리 갱신
updatePriorityTree(socket, result.myPriorityList);
Expand All @@ -51,8 +51,7 @@ async function setupMatchListeners(socket, io) {
deleteSocketFromMatching(socket, io, otherSocket, roomName);

// 12) 8080서버에 매칭 status 변경 API 요청
await updateMatchingStatus(socket, "FOUND");
await updateMatchingStatus(otherSocket, "FOUND");
await updateBothMatchingStatusApi(socket, "FOUND", otherSocket.memberId);

console.log("Matching Found");
} else {
Expand All @@ -65,8 +64,10 @@ async function setupMatchListeners(socket, io) {
// TODO: matching_found emit 보내기 (나 & 상대방 둘 다 보내야함)
// (#21-10) ~ (#21-12) room leave 및 socket priorityTree 초기화
deleteSocketFromMatching(socket, io, otherSocket, roomName);
await updateMatchingStatus(socket, "FOUND");
await updateMatchingStatus(otherSocket, "FOUND");

// (#21-13) 8080서버에 매칭 status 변경 API 요청
await updateBothMatchingStatusApi(socket, "FOUND", otherSocket.memberId);

console.log("Matching Found after 2 mins : ", socket.memberId, " & ", otherSocket.memberId);
}
}, 1 * 15 * 1000); // 2분 = 120,000ms
Expand Down
Loading