fix: 添加领取数额的最大值配置

This commit is contained in:
chenkai 2023-12-21 21:08:22 +08:00
parent 6808b53dd4
commit ba8653dca9

View File

@ -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<ReqReceive, ResReceive>) {
}
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,