feat: 抢红包分钻石接口 初版

This commit is contained in:
chenkai 2023-12-21 19:52:19 +08:00
parent 0ba9688238
commit 82b4b61725
4 changed files with 166 additions and 0 deletions

View File

@ -0,0 +1,45 @@
import { ApiCall } from "tsrpc";
import { HuoDongFun } from "../../../public/huodongfun";
import { ReqCanReceive, ResCanReceive } from '../../../shared/protocols/event/payForDiamond/PtlCanReceive';
import { PublicShared } from "../../../shared/public/public";
export async function playerCanReceive(call: ApiCall) {
const activityInfo = await HuoDongFun.getHdidInfo(call, call.req.activityId);
if (!activityInfo) {
return call.error('No such activity');
}
const remaining = activityInfo.data['remaining'];
const zeroTime = PublicShared.getToDayZeroTime();
const dayPayInfo = await G.mongodb.collection('dayPay').findOne({ uid: call.uid, time: zeroTime });
if (!dayPayInfo || !dayPayInfo.payNum) {
return {
payNum: 0, remaining, result: false, activityInfo
};
}
const payNum = dayPayInfo.payNum;
// 玩家充值未达标或者奖池余额耗尽则不能领取
if (payNum < activityInfo.data['price'] || remaining <= 0) {
return {
payNum, remaining, result: false, activityInfo
}
}
// 检查玩家今日是否已经领取
const playerActivityInfo = G.mongodb.cEvent('payForDiamond').findOne({ uid: call.uid });
if (playerActivityInfo) {
if (playerActivityInfo[zeroTime]?.length) {
return {
payNum, remaining, result: false, activityInfo
};
}
}
return {
payNum, remaining, result: true, activityInfo
};
}
export default async function (call: ApiCall<ReqCanReceive, ResCanReceive>) {
const canReceiveResult = await playerCanReceive(call);
if (canReceiveResult) {
call.succ(canReceiveResult);
}
}

View File

@ -0,0 +1,104 @@
import { ApiCall } from "tsrpc";
import { HuoDongFun } from "../../../public/huodongfun";
import { ReqReceive, ResReceive } from '../../../shared/protocols/event/payForDiamond/PtlReceive';
import { playerCanReceive } from './ApiCanReceive';
import { PublicShared } from "../../../shared/public/public";
type diamondWeightGroup = {
weight: number;
numrange: [ min: number, max: number];
};
/**
* @param groups
*/
function randomWithWeight(groups: diamondWeightGroup[]) {
let minAmount: number;
let totalWeights = 0;
for (const group of groups) {
totalWeights += group.weight;
if (!minAmount) {
minAmount = group.numrange[0];
} else {
minAmount = Math.min(minAmount, group.numrange[0]);
}
}
const randomValue = Math.random() * totalWeights; // 随机值落在[0, totalWeights) 之间
let currSum = 0;
for (const group of groups) {
if (currSum <= randomValue && randomValue < currSum + group.weight) {
return { group, minAmount };
} else {
currSum += group.weight;
}
}
const length = groups.length;
return { group: groups[length - 1], minAmount };
}
/**
*
* @param remaining
* @param group
* @param minAmount
*/
function calcDiamondGot(remaining: number, group: diamondWeightGroup, minAmount: number) {
const [min, max] = group.numrange;
const randomAmount = Math.floor(Math.random() * (max - min)) + min + 1; // max 值应能够取到, 故 +1
// 剩余数额小于组内随机得到的值, 全给吧
if (randomAmount > remaining) {
return remaining;
}
// 玩家分过后的余额不足给到下一个玩家分配的最小额度, 也把当前剩的全给
if (remaining - randomAmount < minAmount) {
return remaining;
}
return randomAmount;
}
export default async function (call: ApiCall<ReqReceive, ResReceive>) {
const canReceiveResult = await playerCanReceive(call);
if (canReceiveResult) {
if (!canReceiveResult.result) {
return call.succ({
amount: 0,
timesRemaining: 0
});
}
const activityData = canReceiveResult.activityInfo.data;
const remaining = activityData['remaining']? activityData['remaining'] : activityData['totalmoney'];
const { group, minAmount } = randomWithWeight(activityData['groupConf']['arr']);
const gotAmount = calcDiamondGot(remaining, group, minAmount);
// 减去余额
const filter = {
hdid: call.req.activityId,
$or: [
{'data.remaining': { $exists: false }},
{$expr: {$gte: [ `$data.remaining - ${gotAmount}`, 0 ]}},
],
};
const updateDoc = {
$set: { 'data.remaining': `$data.remaining - ${gotAmount}` }
}
const updateResult = await G.mongodb.collection('hdinfo').updateOne(filter, updateDoc);
// 更新成功
if (updateResult.modifiedCount) {
// 请求返回
call.succ({
amount: gotAmount,
timesRemaining: 0
});
// 添加玩家领取记录
const zeroTime = PublicShared.getToDayZeroTime();
const setObj = {};
setObj[zeroTime] = gotAmount;
G.mongodb.cEvent('payForDiamond').updateOne({ uid: call.uid }, {
$set: setObj
});
}
// 发送炫耀
if (gotAmount >= activityData['groupConf']['base']['loglimit']) {
}
}
}

View File

@ -0,0 +1,9 @@
export type ReqCanReceive = {
activityId: number;
};
export type ResCanReceive = {
payNum: number;
remaining?: number;
result: boolean;
};

View File

@ -0,0 +1,8 @@
export type ReqReceive = {
activityId: number;
};
export type ResReceive = {
amount: number,
timesRemaining: number;
};