From f341c9d8bb78bb6314dd6fa4a2058a78cf55e70f Mon Sep 17 00:00:00 2001 From: fiore Date: Wed, 12 Mar 2025 15:32:20 +0900 Subject: [PATCH 1/4] =?UTF-8?q?=EB=A6=AC=EB=8D=94=EB=B3=B4=EB=93=9C=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 요청한 유저와 같은 급수의 유저에서 랭킹 불러옴 - 승수와 패수를 이용해 승률을 계산 - 승률이 높은 유저부터 정렬 --- routes/leaderboard.js | 42 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/routes/leaderboard.js b/routes/leaderboard.js index 37cb595..610c36b 100644 --- a/routes/leaderboard.js +++ b/routes/leaderboard.js @@ -1,4 +1,5 @@ var express = require('express'); +const {ObjectId} = require("mongodb"); var router = express.Router(); // 랭킹 조회 @@ -11,20 +12,45 @@ router.get("/", async function (req, res, next) { var database = req.app.get('database'); var users = database.collection('users'); - var allUsers = await users - .find({ }, { projection: { username:1, nickname: 1, score: 1 } }) - .sort({ score: -1 }) // 점수가 높은 순으로 정렬 + + // 요청 유저 정보 확인 + var userId = req.session.userId; + const user = await users.findOne({_id: ObjectId.createFromHexString(userId) }); + var userRating = user.rating; + + // 동일한 rating을 가진 유저들만 필터링 + var sameRatingUsers = await users + .find({ rating: userRating }, { projection: { username: 1, nickname: 1, score: 1, win: 1, lose: 1, profileImageIndex:1, rating: 1 } }) .toArray(); - var result = allUsers.map((user) => ( - { + // 승률 계산 및 정렬 + var result = sameRatingUsers.map((user) => { + const totalGames = (user.win || 0) + (user.lose || 0); + const winRate = totalGames > 0 ? ((user.win || 0) / totalGames * 100) : 0; + + return { username: user.username, nickname: user.nickname, score: user.score || 0, - } - )) + win: user.win || 0, + lose: user.lose || 0, + winRate: parseFloat(winRate.toFixed(2)), // 소수점 2자리까지 표시 + totalGames: totalGames, + imageindex: user.profileImageIndex, + }; + }) + .sort((a, b) => { + // 승률이 같은 경우 총 게임 수가 많은 사람이 위에 오도록 정렬 + if (b.winRate === a.winRate) { + return b.totalGames - a.totalGames; + } + return b.winRate - a.winRate; // 승률 내림차순 정렬 + }); - res.json({ leaderboardDatas : result ?? [] }); + res.json({ + rating: userRating, + leaderboardDatas : result ?? [] + }); } catch(err) { console.error(err); res.status(500).send("서버 오류가 발생했습니다."); From 87d2b03329ee713640d3beb5d359078e4b674cd8 Mon Sep 17 00:00:00 2001 From: fiore Date: Wed, 12 Mar 2025 15:47:33 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=EB=B6=88=ED=95=84=EC=9A=94=ED=95=9C=20?= =?UTF-8?q?=EC=A0=95=EB=B3=B4=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routes/leaderboard.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/routes/leaderboard.js b/routes/leaderboard.js index 610c36b..9e4fbb0 100644 --- a/routes/leaderboard.js +++ b/routes/leaderboard.js @@ -20,7 +20,7 @@ router.get("/", async function (req, res, next) { // 동일한 rating을 가진 유저들만 필터링 var sameRatingUsers = await users - .find({ rating: userRating }, { projection: { username: 1, nickname: 1, score: 1, win: 1, lose: 1, profileImageIndex:1, rating: 1 } }) + .find({ rating: userRating }, { projection: { nickname: 1, score: 1, win: 1, lose: 1, profileImageIndex:1, rating: 1 } }) .toArray(); // 승률 계산 및 정렬 @@ -29,7 +29,6 @@ router.get("/", async function (req, res, next) { const winRate = totalGames > 0 ? ((user.win || 0) / totalGames * 100) : 0; return { - username: user.username, nickname: user.nickname, score: user.score || 0, win: user.win || 0, From 6cc00098f9bc04daf5de5870a3fa0d49aa5a8d78 Mon Sep 17 00:00:00 2001 From: fiore Date: Wed, 12 Mar 2025 17:06:38 +0900 Subject: [PATCH 3/4] =?UTF-8?q?HOT=5FFIX=20:=20=EC=A4=91=EB=B3=B5=20?= =?UTF-8?q?=EC=9C=A0=EC=A0=80=20=EA=B2=80=EC=82=AC=EC=8B=9C=20username?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EC=9E=98=EB=AA=BB=20=EA=B2=80=EC=83=89?= =?UTF-8?q?=ED=95=98=EB=8D=98=20=EC=9D=B4=EC=8A=88=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - DB findOne 검사 조건 수정 - 유저 데이터에 코인 정보 추가 --- routes/users.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/routes/users.js b/routes/users.js index 031e40d..54bd24c 100644 --- a/routes/users.js +++ b/routes/users.js @@ -5,7 +5,7 @@ const {ObjectId} = require("mongodb"); var saltRounds = 10; var ResponseType = { - INVALID_USERNAME: 0, + INVALID_EMAIL: 0, INVALID_PASSWORD: 1, SUCCESS: 2 } @@ -37,7 +37,7 @@ router.post('/signup', async function (req, res, next) { var database = req.app.get('database'); var users = database.collection('users'); - const existingUser = await users.findOne({username: email}) + const existingUser = await users.findOne({email: email}) if(existingUser){ return res.status(409).send("이미 존재하는 사용자입니다.") } @@ -55,7 +55,8 @@ router.post('/signup', async function (req, res, next) { rating: 18, score:0, win:0, - lose:0 + lose:0, + coin: 1000 }); res.status(201).send("사용자가 성공적으로 생성되었습니다."); @@ -90,18 +91,20 @@ router.post("/signin", async function (req, res, next) { req.session.profileImageIndex = existingUser.profileImageIndex || 0; req.session.rating = existingUser.rating; req.session.score = existingUser.score; + req.session.coin = existingUser.coin; res.json({ result: ResponseType.SUCCESS, imageindex: existingUser.imageindex, rating: existingUser.rating, score: existingUser.score, + coin: existingUser.coin, }); } else { res.json({result : ResponseType.INVALID_PASSWORD}); } } else { - res.json({result : ResponseType.INVALID_USERNAME}); + res.json({result : ResponseType.INVALID_EMAIL}); } }catch (err){ console.log("로그인 중 오류 발생 : ", err); @@ -246,4 +249,9 @@ router.get("/get-info", async function (req, res, next) { } }) +router.put("/coin/:id", async function (req, res, next) { + +}) + + module.exports = router; From 697629325ce4f87f6d40e2d33f9db08f112dd12d Mon Sep 17 00:00:00 2001 From: fiore Date: Wed, 12 Mar 2025 17:09:03 +0900 Subject: [PATCH 4/4] =?UTF-8?q?=EC=BD=94=EC=9D=B8=20=EC=8B=9C=EC=8A=A4?= =?UTF-8?q?=ED=85=9C=20=EC=A4=80=EB=B9=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routes/users.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/routes/users.js b/routes/users.js index 54bd24c..d1269d6 100644 --- a/routes/users.js +++ b/routes/users.js @@ -249,9 +249,4 @@ router.get("/get-info", async function (req, res, next) { } }) -router.put("/coin/:id", async function (req, res, next) { - -}) - - module.exports = router;