Skip to content

Commit

Permalink
Add: totalAmount, totalUser field into item leaderboard
Browse files Browse the repository at this point in the history
  • Loading branch information
kmc7468 committed Sep 1, 2024
1 parent e7ef57b commit 05126ee
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/lottery/modules/stores/mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,13 @@ const itemSchema = Schema({
required: true,
min: 0,
validate: integerValidator,
},
}, // 의미 없는 값, 기존 코드와의 호환성을 위해 남겨둡니다.
realStock: {
type: Number,
required: true,
min: 1,
validate: integerValidator,
}, // 상품의 실제 재고
itemType: {
type: Number,
enum: [0, 1, 2, 3],
Expand Down
12 changes: 11 additions & 1 deletion src/lottery/routes/docs/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ itemsDocs[`${apiPrefix}/leaderboard/{itemId}`] = {
"application/json": {
schema: {
type: "object",
required: ["leaderboard"],
required: ["leaderboard", "totalAmount", "totalUser"],
properties: {
leaderboard: {
type: "array",
Expand Down Expand Up @@ -139,6 +139,16 @@ itemsDocs[`${apiPrefix}/leaderboard/{itemId}`] = {
},
},
},
totalAmount: {
type: "number",
description: "상품의 총 판매량",
example: 100,
},
totalUser: {
type: "number",
description: "상품을 구입한 유저의 수",
example: 50,
},
rank: {
type: "number",
description: "현재 유저의 리더보드 순위. 1부터 시작합니다.",
Expand Down
13 changes: 9 additions & 4 deletions src/lottery/services/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ const getItemsHandler = async (req, res) => {
};

// 유도 과정은 services/publicNotice.js 파일에 정의된 calculateProbabilityV2 함수의 주석 참조
const calculateWinProbability = (stock, users, amount, totalAmount) => {
if (users.length <= stock) return 1;
const calculateWinProbability = (realStock, users, amount, totalAmount) => {
if (users.length <= realStock) return 1;

const base = Math.pow(1 - stock / users.length, users.length / totalAmount);
const base = Math.pow(
1 - realStock / users.length,
users.length / totalAmount
);
return 1 - Math.pow(base, amount);
};

Expand Down Expand Up @@ -93,7 +96,7 @@ const getItemLeaderboardHandler = async (req, res) => {
userId: user._id,
amount: user.amount,
probability: calculateWinProbability(
item.stock,
item.realStock,
users,
user.amount,
totalAmount
Expand Down Expand Up @@ -121,6 +124,8 @@ const getItemLeaderboardHandler = async (req, res) => {

return res.json({
leaderboard,
totalAmount,
totalUser: users.length,
amount: user?.amount,
probability: user?.probability,
rank: user?.rank,
Expand Down

0 comments on commit 05126ee

Please sign in to comment.