승급 강등 서버 구현
- 점수 조회 기능 추가
This commit is contained in:
parent
ff9ed0d462
commit
06a93e2ce4
@ -120,21 +120,59 @@ router.post("/addscore", async function (req, res, next) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var userId = req.session.userId;
|
var userId = req.session.userId;
|
||||||
var score = req.body.score;
|
var addScore = req.body.score;
|
||||||
|
|
||||||
// 점수 유효성 검사
|
// 점수 유효성 검사
|
||||||
if(!score || isNaN(score)) {
|
if(!addScore || isNaN(addScore)) {
|
||||||
return res.status(400).send("유효한 점수를 입력해주세요.");
|
return res.status(400).send("유효한 점수를 입력해주세요.");
|
||||||
}
|
}
|
||||||
|
|
||||||
var database = req.app.get('database');
|
var database = req.app.get('database');
|
||||||
var users = database.collection('users');
|
var users = database.collection('users');
|
||||||
|
|
||||||
|
var findUser = await users.findOne({_id: ObjectId.createFromHexString(userId) });
|
||||||
|
var userRating = findUser.rating;
|
||||||
|
var userScore = findUser.score + addScore;
|
||||||
|
|
||||||
|
|
||||||
|
// 급수에 따른 필요 승급 포인트 결정
|
||||||
|
let requiredPoints;
|
||||||
|
if (userRating >= 10 && userRating <= 18) {
|
||||||
|
requiredPoints = 3; // 10~18급은 3점 필요
|
||||||
|
} else if (userRating >= 5 && userRating <= 9) {
|
||||||
|
requiredPoints = 5; // 5~9급은 5점 필요
|
||||||
|
} else if (userRating >= 1 && userRating <= 4) {
|
||||||
|
requiredPoints = 10; // 1~4급은 10점 필요
|
||||||
|
}
|
||||||
|
|
||||||
|
// 승급 확인
|
||||||
|
if (userScore >= requiredPoints) {
|
||||||
|
if (userRating > 1) { // 1급보다 높은 급수인 경우만 승급 가능
|
||||||
|
userRating -= 1; // 급수 상승 (숫자는 작을수록 높은 급수)
|
||||||
|
userScore = 0; // 승급 후 포인트 초기화
|
||||||
|
} else {
|
||||||
|
// 1급인 경우 더 이상 승급 불가능, 최대 포인트로 유지
|
||||||
|
userScore = requiredPoints;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 강등 확인
|
||||||
|
if (userScore <= -requiredPoints) {
|
||||||
|
if (userRating < 18) { // 18급보다 낮은 급수인 경우만 강등 가능
|
||||||
|
userRating += 1; // 급수 하락 (숫자가 커짐)
|
||||||
|
userScore = 0; // 강등 후 포인트 초기화
|
||||||
|
} else {
|
||||||
|
// 18급인 경우 더 이상 강등 불가능, 최소 포인트로 유지
|
||||||
|
userScore = -requiredPoints + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const result = await users.updateOne(
|
const result = await users.updateOne(
|
||||||
{_id: ObjectId.createFromHexString(userId) },
|
{_id: ObjectId.createFromHexString(userId) },
|
||||||
{
|
{
|
||||||
$set: {
|
$set: {
|
||||||
score: Number(score),
|
rating: Number(userRating),
|
||||||
|
score: Number(userScore),
|
||||||
updatedAt: new Date()
|
updatedAt: new Date()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -144,7 +182,11 @@ router.post("/addscore", async function (req, res, next) {
|
|||||||
return res.status(400).send("사용자를 찾을 수 없습니다.");
|
return res.status(400).send("사용자를 찾을 수 없습니다.");
|
||||||
}
|
}
|
||||||
|
|
||||||
res.status(200).json({message: "점수가 성공적으로 업데이트되었습니다."});
|
res.status(200).json({
|
||||||
|
message: "점수가 성공적으로 업데이트되었습니다.",
|
||||||
|
rating: Number(userRating),
|
||||||
|
score: Number(userScore),
|
||||||
|
});
|
||||||
|
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
console.log("점수 추가 중 오류 발생 : ",err);
|
console.log("점수 추가 중 오류 발생 : ",err);
|
||||||
@ -174,6 +216,7 @@ router.get("/score", async function (req, res, next) {
|
|||||||
id: user._id.toString(),
|
id: user._id.toString(),
|
||||||
username: user.username,
|
username: user.username,
|
||||||
nickname: user.nickname,
|
nickname: user.nickname,
|
||||||
|
rating: user.rating,
|
||||||
score: Number(user.score) || 0,
|
score: Number(user.score) || 0,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user