feat: 充值抢钻石活动
This commit is contained in:
parent
122041f404
commit
2fc85853dc
45
src/api_s2c/event/payForDiamond/ApiCanReceive.ts
Normal file
45
src/api_s2c/event/payForDiamond/ApiCanReceive.ts
Normal 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);
|
||||
}
|
||||
}
|
113
src/api_s2c/event/payForDiamond/ApiReceive.ts
Normal file
113
src/api_s2c/event/payForDiamond/ApiReceive.ts
Normal file
@ -0,0 +1,113 @@
|
||||
import { ApiCall } from "tsrpc";
|
||||
import { ReqReceive, ResReceive } from '../../../shared/protocols/event/payForDiamond/PtlReceive';
|
||||
import { playerCanReceive } from './ApiCanReceive';
|
||||
import { PublicShared } from "../../../shared/public/public";
|
||||
import { ChatFun } from "../../../public/chat";
|
||||
|
||||
type diamondWeightGroup = {
|
||||
weight: number;
|
||||
numrange: [ min: number, max: number];
|
||||
};
|
||||
|
||||
/**
|
||||
* @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, maxAmount };
|
||||
} else {
|
||||
currSum += group.weight;
|
||||
}
|
||||
}
|
||||
const length = groups.length;
|
||||
return { group: groups[length - 1], maxAmount };
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算玩家分得的钻石数量
|
||||
* @param remaining
|
||||
* @param group
|
||||
* @param maxAmount
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
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, maxAmount } = randomWithWeight(activityData['groupConf']['arr']);
|
||||
const gotAmount = calcDiamondGot(remaining, group, maxAmount);
|
||||
// 减去余额
|
||||
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']) {
|
||||
ChatFun.newMsg({
|
||||
type: 'local',
|
||||
msg: G.gc.pmd.get_hero_star5_pmd,
|
||||
time: G.time,
|
||||
sender: 'system',
|
||||
otherData: {
|
||||
pmd: true,
|
||||
args: [gotAmount]
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@ -14,5 +14,7 @@
|
||||
//黑帮争霸争霸赛第二名
|
||||
hbzb_pmd2: 'intr_pmd_hbzb2',
|
||||
//黑帮争霸争霸赛第三名
|
||||
hbzb_pmd3: 'intr_pmd_hbzb3'
|
||||
hbzb_pmd3: 'intr_pmd_hbzb3',
|
||||
// 充值抢钻石 - 抢到大量钻石
|
||||
pay_for_diamond: 'intr_pmd_pfd'
|
||||
}
|
@ -52,7 +52,10 @@ export type eventType = {
|
||||
jierihuodong: Omit<ResOpenJierihuodong, 'taskFinish'> & { refreshTime: number; };
|
||||
kaifujingsai: ResOpenKaifujingsai;
|
||||
zhoumolibao: ResOpenZhoumolibao & { refreshTime: number; }
|
||||
pobinglibao: ResOpenPobinglibao
|
||||
pobinglibao: ResOpenPobinglibao;
|
||||
payForDiamond: {
|
||||
[time: number]: number[]
|
||||
}
|
||||
} & {
|
||||
[k: `${number}jijin`]: ResOpenYuedujijin;
|
||||
[k: `yangchengmubiao${number}`]: yangchengmubiao;
|
||||
|
@ -0,0 +1,9 @@
|
||||
export type ReqCanReceive = {
|
||||
activityId: number;
|
||||
};
|
||||
|
||||
export type ResCanReceive = {
|
||||
payNum: number;
|
||||
remaining?: number;
|
||||
result: boolean;
|
||||
};
|
8
src/shared/protocols/event/payForDiamond/PtlReceive.ts
Normal file
8
src/shared/protocols/event/payForDiamond/PtlReceive.ts
Normal file
@ -0,0 +1,8 @@
|
||||
export type ReqReceive = {
|
||||
activityId: number;
|
||||
};
|
||||
|
||||
export type ResReceive = {
|
||||
amount: number,
|
||||
timesRemaining: number;
|
||||
};
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user