fix: 允许玩家领取到的钻石数小于配置的最低档位额度

This commit is contained in:
chenkai 2023-12-21 20:33:47 +08:00
parent 2ae2a1362d
commit 6808b53dd4
2 changed files with 385 additions and 306 deletions

View File

@ -13,46 +13,35 @@ type diamondWeightGroup = {
* @param groups * @param groups
*/ */
function randomWithWeight(groups: diamondWeightGroup[]) { function randomWithWeight(groups: diamondWeightGroup[]) {
let minAmount: number;
let totalWeights = 0; let totalWeights = 0;
for (const group of groups) { for (const group of groups) {
totalWeights += group.weight; totalWeights += group.weight;
if (!minAmount) {
minAmount = group.numrange[0];
} else {
minAmount = Math.min(minAmount, group.numrange[0]);
}
} }
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, minAmount }; return group;
} else { } else {
currSum += group.weight; currSum += group.weight;
} }
} }
const length = groups.length; const length = groups.length;
return { group: groups[length - 1], minAmount }; return groups[length - 1];
} }
/** /**
* *
* @param remaining * @param remaining
* @param group * @param group
* @param minAmount
*/ */
function calcDiamondGot(remaining: number, group: diamondWeightGroup, minAmount: number) { function calcDiamondGot(remaining: number, group: diamondWeightGroup) {
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 (remaining - randomAmount < minAmount) {
return remaining;
}
return randomAmount; return randomAmount;
} }
@ -67,8 +56,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, minAmount } = randomWithWeight(activityData['groupConf']['arr']); const group = randomWithWeight(activityData['groupConf']['arr']);
const gotAmount = calcDiamondGot(remaining, group, minAmount); const gotAmount = calcDiamondGot(remaining, group);
// 减去余额 // 减去余额
const filter = { const filter = {
hdid: call.req.activityId, hdid: call.req.activityId,

File diff suppressed because it is too large Load Diff