fix: 添加领取数额的最大值配置
This commit is contained in:
parent
6808b53dd4
commit
ba8653dca9
@ -13,35 +13,46 @@ type diamondWeightGroup = {
|
|||||||
* @param groups 各分组及其权重
|
* @param groups 各分组及其权重
|
||||||
*/
|
*/
|
||||||
function randomWithWeight(groups: diamondWeightGroup[]) {
|
function randomWithWeight(groups: diamondWeightGroup[]) {
|
||||||
|
let maxAmount: number;
|
||||||
let totalWeights = 0;
|
let totalWeights = 0;
|
||||||
for (const group of groups) {
|
for (const group of groups) {
|
||||||
totalWeights += group.weight;
|
totalWeights += group.weight;
|
||||||
|
if (!maxAmount) {
|
||||||
|
maxAmount = group.numrange[1];
|
||||||
|
} else {
|
||||||
|
maxAmount = Math.min(maxAmount, group.numrange[1]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const randomValue = Math.random() * totalWeights; // 随机值落在[0, totalWeights) 之间
|
const randomValue = Math.random() * totalWeights; // 随机值落在[0, totalWeights) 之间
|
||||||
let currSum = 0;
|
let currSum = 0;
|
||||||
for (const group of groups) {
|
for (const group of groups) {
|
||||||
if (currSum <= randomValue && randomValue < currSum + group.weight) {
|
if (currSum <= randomValue && randomValue < currSum + group.weight) {
|
||||||
return group;
|
return { group, maxAmount };
|
||||||
} else {
|
} else {
|
||||||
currSum += group.weight;
|
currSum += group.weight;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const length = groups.length;
|
const length = groups.length;
|
||||||
return groups[length - 1];
|
return { group: groups[length - 1], maxAmount };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 计算玩家分得的钻石数量
|
* 计算玩家分得的钻石数量
|
||||||
* @param remaining
|
* @param remaining
|
||||||
* @param group
|
* @param group
|
||||||
|
* @param maxAmount
|
||||||
*/
|
*/
|
||||||
function calcDiamondGot(remaining: number, group: diamondWeightGroup) {
|
function calcDiamondGot(remaining: number, group: diamondWeightGroup, maxAmount: number) {
|
||||||
const [min, max] = group.numrange;
|
const [min, max] = group.numrange;
|
||||||
const randomAmount = Math.floor(Math.random() * (max - min)) + min + 1; // max 值应能够取到, 故 +1
|
const randomAmount = Math.floor(Math.random() * (max - min)) + min + 1; // max 值应能够取到, 故 +1
|
||||||
// 剩余数额小于组内随机得到的值, 全给吧
|
// 剩余数额小于组内随机得到的值, 全给吧
|
||||||
if (randomAmount > remaining) {
|
if (randomAmount > remaining) {
|
||||||
return remaining;
|
return remaining;
|
||||||
}
|
}
|
||||||
|
// 随机值大于最大值, 取最大值
|
||||||
|
if (randomAmount > maxAmount) {
|
||||||
|
return maxAmount;
|
||||||
|
}
|
||||||
return randomAmount;
|
return randomAmount;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,8 +67,8 @@ export default async function (call: ApiCall<ReqReceive, ResReceive>) {
|
|||||||
}
|
}
|
||||||
const activityData = canReceiveResult.activityInfo.data;
|
const activityData = canReceiveResult.activityInfo.data;
|
||||||
const remaining = activityData['remaining']? activityData['remaining'] : activityData['totalmoney'];
|
const remaining = activityData['remaining']? activityData['remaining'] : activityData['totalmoney'];
|
||||||
const group = randomWithWeight(activityData['groupConf']['arr']);
|
const { group, maxAmount } = randomWithWeight(activityData['groupConf']['arr']);
|
||||||
const gotAmount = calcDiamondGot(remaining, group);
|
const gotAmount = calcDiamondGot(remaining, group, maxAmount);
|
||||||
// 减去余额
|
// 减去余额
|
||||||
const filter = {
|
const filter = {
|
||||||
hdid: call.req.activityId,
|
hdid: call.req.activityId,
|
||||||
|
Loading…
Reference in New Issue
Block a user