From f17f4182a9f92ab166b993a864d7157b3c971132 Mon Sep 17 00:00:00 2001 From: dy Date: Fri, 22 Dec 2023 11:39:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B6=88=E8=B4=B9=E7=AB=9E=E8=B5=9B=E5=AE=9A?= =?UTF-8?q?=E6=97=B6=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api_s2c/event/xiaofeijingsai/ApiOpen.ts | 51 ++++++++--- src/public/scheduler/scheduler.ts | 5 +- .../scheduler/scheduler_xiaofeijingsai.ts | 68 ++++++++++++++ .../protocols/event/xiaofeijingsai/PtlOpen.ts | 13 ++- src/shared/protocols/serviceProto.ts | 88 ++++++++----------- 5 files changed, 160 insertions(+), 65 deletions(-) create mode 100644 src/public/scheduler/scheduler_xiaofeijingsai.ts diff --git a/src/api_s2c/event/xiaofeijingsai/ApiOpen.ts b/src/api_s2c/event/xiaofeijingsai/ApiOpen.ts index 1473ee6..940eec1 100644 --- a/src/api_s2c/event/xiaofeijingsai/ApiOpen.ts +++ b/src/api_s2c/event/xiaofeijingsai/ApiOpen.ts @@ -1,16 +1,15 @@ import {ApiCall} from "tsrpc"; import {ReqOpen, ResOpen} from "../../../shared/protocols/event/xiaofeijingsai/PtlOpen"; -import {PayFun} from "../../../public/pay"; import {HuoDongFun} from "../../../public/huodongfun"; -import {PublicShared} from "../../../shared/public/public"; /** * 消费竞赛 - * redis缓存3分钟 + * redis缓存120秒 * 返回活动日期内的 * @param call */ export default async function (call: ApiCall) { + let ioList = await G.ioredis.get(`rank:xiaofeijingsai`); if (ioList) { let myData = await getMyData(call, JSON.parse(ioList)) @@ -31,18 +30,42 @@ export default async function (call: ApiCall) { let list: any = rmbuse.map(i => ({...i, total: R.negate(i.total)})) - let users = await G.mongodb.collection('user').find({uid: {$in: list.map(i => i._id)}}).toArray() - list = list.map(i => ({...i, player: users.find(v => v.uid == i._id)})) + let rankList = sortRankList(_hd.data.rank, list) + + let users = await G.mongodb.collection('user').find({uid: {$in: rankList.map(i => i._id).filter(i => i._id != 'system')}}).toArray() + + rankList = rankList.map(i => ({...i, player: users.find(v => v.uid == i._id) || {}})) // 活动结束前半小时,缓存过期时间改为10秒 let exTime = (G.time + 1800) > _hd.etime ? 10 : 180 - G.ioredis.setex(`rank:xiaofeijingsai`, exTime, JSON.stringify(list)); + G.ioredis.setex(`rank:xiaofeijingsai`, exTime, JSON.stringify(rankList)); - let myData = await getMyData(call, list, _hd) + let myData = await getMyData(call, rankList, _hd) - call.succ({list, myData}) + call.succ({list: rankList, myData}) +} + +/** + * 需要满足消耗条件才能添加到排行榜,不满足的时候排行榜加system占位 + * @param rank + * @param list + */ +export function sortRankList(rank, list) { + let rankList = [] + let ccc = 0 + rank.map(i => { + for (let k = i.rank[0] - 1; k < i.rank[1]; k++) { + if (list[k]?.total >= i.need[0].n) { + rankList.push({...list[k - ccc], rank: k}) + } else { + rankList.push({_id: 'system', total: 0, rank: k, player: {}}) + ccc += 1 + } + } + }) + return R.sort((a, b) => a.rank - b.rank)(rankList) } // 获取自己的信息 @@ -53,10 +76,16 @@ async function getMyData(call, rankList, _hd?) { if (!_hd) { _hd = (await HuoDongFun.gethdList(call, 11))[0] } - let a = await G.mongodb.collection('rmbuse').aggregate([ + let myCut = (await G.mongodb.collection('rmbuse').aggregate([ {$match: {uid: call.uid, isAdd: false, cTime: {$gte: _hd.stime, $lte: _hd.etime}}}, {$group: {_id: "$uid", total: {$sum: "$change"}}} - ]).toArray() + ]).toArray())[0] + let myUser = await G.mongodb.collection('user').findOne({uid: call.uid}) - return {player: myUser, ...a} + + if (!myCut) { + myCut = {_id: myUser.uid, total: 0} + } + + return {player: myUser, ...myCut, rank: -1} } \ No newline at end of file diff --git a/src/public/scheduler/scheduler.ts b/src/public/scheduler/scheduler.ts index a738ed2..a8b93a7 100644 --- a/src/public/scheduler/scheduler.ts +++ b/src/public/scheduler/scheduler.ts @@ -37,7 +37,8 @@ export type schedulerType = | 'hbzb_zbs_clear' | 'hbzb_zbs_group' | 'wzry_zuanshi16to8' - | "cross_email_pull"; + | "cross_email_pull" + | "xiaofeijingsai_local_ctor"; export class SchedulerManage { static logTime = false; @@ -102,7 +103,7 @@ export abstract class Scheduler { } - init(){ + init() { Scheduler.schedulers.push(this); this.read().then(_ => { this.log(`state: 准备完毕 预计下次执行时间:${new Date(this.startTime * 1000).format("YYYY-MM-DD hh:mm:ss")}`); diff --git a/src/public/scheduler/scheduler_xiaofeijingsai.ts b/src/public/scheduler/scheduler_xiaofeijingsai.ts new file mode 100644 index 0000000..fb50c51 --- /dev/null +++ b/src/public/scheduler/scheduler_xiaofeijingsai.ts @@ -0,0 +1,68 @@ +import {Scheduler, schedulerType} from './scheduler'; +import {PublicShared} from "../../shared/public/public"; +import {Wjjl} from "../../module/collection_wjjl"; +import {ZhanLingTasks} from "../zhanling"; +import {setGud} from '../gud'; +import {HuoDongFun} from "../huodongfun"; +import {EmailFun} from "../email"; +import {sortRankList} from "../../api_s2c/event/xiaofeijingsai/ApiOpen"; + + +export class SchedulerXiaofeijingsaiLocalCtor extends Scheduler { + id: schedulerType = 'xiaofeijingsai_local_ctor'; + time = 300; + name = '消费竞赛定时器发奖'; + type: 'day' | 'week' = 'day'; + + // todo 测试 + // get nextTime() { + // return G.time + 60; + // } + + async read() { + await this.ctorStartTime(); + this.isReady = false; + } + + async start() { + let _hd = (await G.mongodb.collection('hdinfo').find({ + htype: 11, + ttype: 0, + etime: {$lt: G.time, $gt: 99999999}, + isSendPrize: {$or: [{$exists: false}, {$eq: false}]}, + }).sort({etime: -1}).limit(1).toArray())[0] + + if (!_hd) return + + let limit = _hd.data?.rank?.slice(-1)?.[0]?.rank?.slice(-1)?.[0] || 100 + let rmbuse = await G.mongodb.collection('rmbuse').aggregate([ + {$match: {isAdd: false, cTime: {$gte: _hd.stime, $lte: _hd.etime}}}, + {$group: {_id: "$uid", total: {$sum: "$change"}}}, + {$sort: {total: 1}}, + {$limit: limit} + ]).toArray() + + let list: any = rmbuse.map(i => ({...i, total: R.negate(i.total)})) + + let ranklist = sortRankList(_hd.data.rank, list) + + R.forEach(i => { + let users = R.slice(i.rank[0] - 1, i.rank[1])(ranklist) + users.map(v=>{ + EmailFun.addEmail({ + uid: v._id, + type: 'system', + title: 'email.titel', + content: 'email.content', + prize: i.prize, + // contentInsertArr: [conf.ph[0] + index] + }); + }) + })(_hd.data.rank) + + await G.mongodb.collection('hdinfo').findOneAndUpdate({hdid: _hd.hdid}, {$set: {isSendPrize: true}}) + + console.log(_hd) + + } +} diff --git a/src/shared/protocols/event/xiaofeijingsai/PtlOpen.ts b/src/shared/protocols/event/xiaofeijingsai/PtlOpen.ts index 675b479..150e12a 100644 --- a/src/shared/protocols/event/xiaofeijingsai/PtlOpen.ts +++ b/src/shared/protocols/event/xiaofeijingsai/PtlOpen.ts @@ -6,6 +6,13 @@ export type ReqOpen = { }; export type ResOpen = { - list: { _id: string, total: number, player: any }[] - myData: { _id: string, total: number, player: any } -}; \ No newline at end of file + list: data[] + myData: data +}; + +type data = { + _id: string, + total: number, + rank: number, + player: any +} \ No newline at end of file diff --git a/src/shared/protocols/serviceProto.ts b/src/shared/protocols/serviceProto.ts index 1f614dd..0c851c1 100644 --- a/src/shared/protocols/serviceProto.ts +++ b/src/shared/protocols/serviceProto.ts @@ -10541,7 +10541,8 @@ export const serviceProto: ServiceProto = { "name": "limit", "type": { "type": "Number" - } + }, + "optional": true } ] }, @@ -10554,30 +10555,8 @@ export const serviceProto: ServiceProto = { "type": { "type": "Array", "elementType": { - "type": "Interface", - "properties": [ - { - "id": 0, - "name": "_id", - "type": { - "type": "String" - } - }, - { - "id": 1, - "name": "total", - "type": { - "type": "Number" - } - }, - { - "id": 2, - "name": "player", - "type": { - "type": "Any" - } - } - ] + "type": "Reference", + "target": "event/xiaofeijingsai/PtlOpen/data" } } }, @@ -10585,30 +10564,41 @@ export const serviceProto: ServiceProto = { "id": 1, "name": "myData", "type": { - "type": "Interface", - "properties": [ - { - "id": 0, - "name": "_id", - "type": { - "type": "String" - } - }, - { - "id": 1, - "name": "total", - "type": { - "type": "Number" - } - }, - { - "id": 2, - "name": "player", - "type": { - "type": "Any" - } - } - ] + "type": "Reference", + "target": "event/xiaofeijingsai/PtlOpen/data" + } + } + ] + }, + "event/xiaofeijingsai/PtlOpen/data": { + "type": "Interface", + "properties": [ + { + "id": 0, + "name": "_id", + "type": { + "type": "String" + } + }, + { + "id": 1, + "name": "total", + "type": { + "type": "Number" + } + }, + { + "id": 2, + "name": "rank", + "type": { + "type": "Number" + } + }, + { + "id": 3, + "name": "player", + "type": { + "type": "Any" } } ]