From ba8653dca98515047b940363e9c9e8487c9f999c Mon Sep 17 00:00:00 2001 From: chenkai Date: Thu, 21 Dec 2023 21:08:22 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B7=BB=E5=8A=A0=E9=A2=86=E5=8F=96?= =?UTF-8?q?=E6=95=B0=E9=A2=9D=E7=9A=84=E6=9C=80=E5=A4=A7=E5=80=BC=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api_s2c/event/payForDiamond/ApiReceive.ts | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/api_s2c/event/payForDiamond/ApiReceive.ts b/src/api_s2c/event/payForDiamond/ApiReceive.ts index 8aaf8e6..aaf8a56 100644 --- a/src/api_s2c/event/payForDiamond/ApiReceive.ts +++ b/src/api_s2c/event/payForDiamond/ApiReceive.ts @@ -13,35 +13,46 @@ type diamondWeightGroup = { * @param groups 各分组及其权重 */ function randomWithWeight(groups: diamondWeightGroup[]) { + let maxAmount: number; let totalWeights = 0; for (const group of groups) { totalWeights += group.weight; + if (!maxAmount) { + maxAmount = group.numrange[1]; + } else { + maxAmount = Math.min(maxAmount, group.numrange[1]); + } } const randomValue = Math.random() * totalWeights; // 随机值落在[0, totalWeights) 之间 let currSum = 0; for (const group of groups) { if (currSum <= randomValue && randomValue < currSum + group.weight) { - return group; + return { group, maxAmount }; } else { currSum += group.weight; } } const length = groups.length; - return groups[length - 1]; + return { group: groups[length - 1], maxAmount }; } /** * 计算玩家分得的钻石数量 * @param remaining * @param group + * @param maxAmount */ -function calcDiamondGot(remaining: number, group: diamondWeightGroup) { +function calcDiamondGot(remaining: number, group: diamondWeightGroup, maxAmount: number) { const [min, max] = group.numrange; const randomAmount = Math.floor(Math.random() * (max - min)) + min + 1; // max 值应能够取到, 故 +1 // 剩余数额小于组内随机得到的值, 全给吧 if (randomAmount > remaining) { return remaining; } + // 随机值大于最大值, 取最大值 + if (randomAmount > maxAmount) { + return maxAmount; + } return randomAmount; } @@ -56,8 +67,8 @@ export default async function (call: ApiCall) { } const activityData = canReceiveResult.activityInfo.data; const remaining = activityData['remaining']? activityData['remaining'] : activityData['totalmoney']; - const group = randomWithWeight(activityData['groupConf']['arr']); - const gotAmount = calcDiamondGot(remaining, group); + const { group, maxAmount } = randomWithWeight(activityData['groupConf']['arr']); + const gotAmount = calcDiamondGot(remaining, group, maxAmount); // 减去余额 const filter = { hdid: call.req.activityId,