diff --git a/src/api_s2c/jiuba/ApiLottery.ts b/src/api_s2c/jiuba/ApiLottery.ts index 968cd79..89266e0 100644 --- a/src/api_s2c/jiuba/ApiLottery.ts +++ b/src/api_s2c/jiuba/ApiLottery.ts @@ -9,6 +9,7 @@ import {ReqLottery, ResLottery} from "../../shared/protocols/jiuba/PtlLottery"; import {PublicShared} from '../../shared/public/public'; import {HongDianChange} from '../hongdian/fun'; import {RankKfjs} from "../../public/rank/rank_kfjs"; +import {PushGiftFun} from "../../public/pushgift"; export default async function (call: ApiCall) { @@ -142,4 +143,8 @@ export default async function (call: ApiCall) { valArr: [await rankKfjs.getRankScore(call.uid) + call.req.type] }); + // 十连抽推送礼包 + if (call.req.type == 10) { + PushGiftFun.chkRecruitGift(call.uid) + } } \ No newline at end of file diff --git a/src/api_s2c/pushgift/ApiOpen.ts b/src/api_s2c/pushgift/ApiOpen.ts new file mode 100644 index 0000000..58d85b2 --- /dev/null +++ b/src/api_s2c/pushgift/ApiOpen.ts @@ -0,0 +1,29 @@ +import {ApiCall} from "tsrpc"; +import {ReqOpen, ResOpen} from "../../shared/protocols/pushgift/PtlOpen"; +import {PushGiftFun} from "../../public/pushgift"; +import {Gift} from "../../module/collection_pushgift"; + +export default async function (call: ApiCall) { + let res: ResOpen = {gifts: []}; + + // 过滤过期的礼包 和购买完毕的礼包 + (await PushGiftFun.getGift(call.uid)).forEach((gift) => { + if (gift.passTime > G.time && chkBuyNum(gift.id, gift)) { + res.gifts.push(gift) + } + }) + + call.succ(res) +} + +function chkBuyNum(gift_id: string, gift_info: Gift) { + let conf = G.gc.tuisonglibao[gift_id]; + for (let i = 0; i < conf.payId.length; i++) { + let pay = G.gc.pay[conf.payId[i]]; + if (pay.buys > 0 && gift_info.buy[i] < pay.buys) { + return true + } + } + + return false +} \ No newline at end of file diff --git a/src/jsonType.ts b/src/jsonType.ts index 43654a4..8788f60 100644 --- a/src/jsonType.ts +++ b/src/jsonType.ts @@ -1773,6 +1773,24 @@ type gc_yuyuemail = { 'prize': { "a": string, "t": string, "n": number, [x: string]: any }[] }; +type gc_push_gift = { + [k: string]: { + /**礼包id*/ + id: number + /**礼包类型*/ + type: number + /**礼包参数*/ + num: any[] + /**购买id*/ + payId: string[] + /**持续时间*/ + time: number + /**冷却时间*/ + displayCD: number + /**显示返利比*/ + scale: number + } +} type gcType = { [key: string]: any @@ -1922,6 +1940,7 @@ type gcType = { kfcb_content: gc_kfcb_content kfcb_prize: gc_kfcb_prize yuyuemail: gc_yuyuemail + tuisonglibao: gc_push_gift } diff --git a/src/module/collection_pushgift.ts b/src/module/collection_pushgift.ts new file mode 100644 index 0000000..c62fff0 --- /dev/null +++ b/src/module/collection_pushgift.ts @@ -0,0 +1,13 @@ +import {ObjectId} from "mongodb"; +import {ResOpen} from "../shared/protocols/pushgift/PtlOpen"; + +export type Gift = { + id: string + uid: string + buy: number[] + ctime: number + passTime: number + ext_data: { [key: string]: any } +} + +export type CollectionPushGift = Gift & { _id: ObjectId }; \ No newline at end of file diff --git a/src/module/mongodb.ts b/src/module/mongodb.ts index f912ad7..5d8c8ac 100644 --- a/src/module/mongodb.ts +++ b/src/module/mongodb.ts @@ -59,6 +59,7 @@ import {CollectionPlayerBehavior} from "./collection_player_behavior"; import {CollectionRmbuse} from "./collection_rmbuse"; import {CollectionFightLog} from "./collection_fightLog"; import {CollectionShop} from "./collection_shop"; +import {CollectionPushGift} from "./collection_pushgift"; export type MongodbCollections = { user: CollectionUser; @@ -131,4 +132,5 @@ export type MongodbCollections = { rmbuse: CollectionRmbuse fightLog: CollectionFightLog shop: CollectionShop + pushgift:CollectionPushGift }; \ No newline at end of file diff --git a/src/public/fight.ts b/src/public/fight.ts index 202369c..c13c029 100644 --- a/src/public/fight.ts +++ b/src/public/fight.ts @@ -9,6 +9,7 @@ import {HeroFun} from './hero'; import {UserFun} from './user'; import {re, string} from "mathjs"; import {getGud} from './gud'; +import {PushGiftFun} from "./pushgift"; type fightType = 'tanxian' | 'pata' | 'jjc' | 'gbtx' | 'qjzzd' | 'meirishilian' | 'wzrycross'; @@ -86,9 +87,16 @@ export class FightFun { /**挑战竞技场 */ static async fightJJc(call: ApiCall, rankInfo: rankInfo) { - if (rankInfo.player.uid.indexOf('npc_') != -1) return await this.fightNpc(call, G.gc.jjc_npc[rankInfo.player.uid].npcId, 'jjc', await this.getPlayerFightData(call.conn.uid)); - return this.fight([await this.getPlayerFightData(call.conn.uid), await this.getPlayerFightData(rankInfo.player.uid)]); + + let result = this.fight([await this.getPlayerFightData(call.conn.uid), await this.getPlayerFightData(rankInfo.player.uid)]); + + // 竞技场 战败触发推送礼包 + if (result.winSide != 0) { + PushGiftFun.chkLoseGift(call.uid) + } + + return result } /**挑战npc */ @@ -106,7 +114,13 @@ export class FightFun { let npc = formatNpcData(npcId); - return this.fight([my, npc], 30, 'pve'); + let result = this.fight([my, npc], 30, 'pve'); + + // 主线 爬塔 战败触发推送礼包 + if (result.winSide != 0 && ["tanxian", "pata"].includes(type)) { + PushGiftFun.chkLoseGift(data[0].player.uid) + } + return result } /** diff --git a/src/public/player.ts b/src/public/player.ts index d8322dd..27d8ddc 100644 --- a/src/public/player.ts +++ b/src/public/player.ts @@ -14,9 +14,10 @@ import {PublicShared} from '../shared/public/public'; import {HeroFun} from './hero'; import {ShiwuFun} from './shiwu'; import {UserFun} from './user'; -import { getItemByItemId, getItemNum } from './item'; -import { getGud, setGud } from './gud'; +import {getItemByItemId, getItemNum} from './item'; +import {getGud, setGud} from './gud'; import {addGameLog} from "../gameLog"; +import {PushGiftFun} from "./pushgift"; export type call = { @@ -35,7 +36,7 @@ export type call = { }; service: { name: string }; addEventMsg: ApiCall['addEventMsg']; - req: { } + req: {} }; export class PlayerFun { @@ -66,6 +67,8 @@ export class PlayerFun { if (has < atn.n) { if (err) { + // 消耗不足 触发推送礼包 + PushGiftFun.chkItemGift(call.uid, atn) throw new TsrpcError('', {code: -104, atn: atn}); } else { return {isOk: false, atn: atn}; @@ -143,15 +146,15 @@ export class PlayerFun { shiwu.length > 0 && this.addShiwu(call, shiwu), peijian.length > 0 && this.addPeijian(call, peijian) ]); - + return prizeList; }; //attr里的指定字段的值,不能小于0 - static fixAttrLteZero(t:string, val:number){ - if(['jinbi','rmbmoney','payExp','nexp'].includes(t) && val < 0){ + static fixAttrLteZero(t: string, val: number) { + if (['jinbi', 'rmbmoney', 'payExp', 'nexp'].includes(t) && val < 0) { return 0 - }else{ + } else { return val; } } @@ -189,6 +192,16 @@ export class PlayerFun { // 修改属性应在相关奖励领取之前,否则奖励内获取的用户数据是旧数据 // await this.changeAttr(call.conn.uid, change); call.addEventMsg('msg_s2c/PlayerChange', change); + + // 等级改变 触发推送礼包 + if (change["lv"]) { + PushGiftFun.chkLvGift(call.uid, change["lv"]) + } + // 关卡改变 触发推送礼包 + if (change["mapId"]) { + PushGiftFun.chkLevelGift(call.uid, change["mapId"]) + } + } static async changeAttrLog(uid: string, change, atn, before) { @@ -207,9 +220,9 @@ export class PlayerFun { static async changeAttr(uid: string, change: Partial) { //for (let k in change) { - //G.redis.set('user', uid, k as any, change[k]); + //G.redis.set('user', uid, k as any, change[k]); //} - setGud(uid,change); + setGud(uid, change); G.mongodb.collection('user').updateOne({uid: uid}, {$set: change}); @@ -290,7 +303,11 @@ export class PlayerFun { G.mongodb.collection('item').updateOne(upObj.filter, upObj.update, upObj.options); call.addEventMsg('msg_s2c/ItemChange', atn.t, data); - addGameLog(call.uid, "_itemChange", {"additem":1}, {"filter": upObj.filter, "update": upObj.update, "options": upObj.options}) + addGameLog(call.uid, "_itemChange", {"additem": 1}, { + "filter": upObj.filter, + "update": upObj.update, + "options": upObj.options + }) } else { if (item.num + atn.n <= 0) { await Promise.all([ @@ -302,13 +319,13 @@ export class PlayerFun { //await G.redis.del('item', call.uid, atn.t); //await G.mongodb.collection('item').deleteOne({uid: call.uid, itemId: atn.t}); call.addEventMsg('msg_s2c/ItemChange', atn.t, {num: 0}); - addGameLog(call.uid, "_itemChange",{"delitem":1}, {"itemId": atn.t}) + addGameLog(call.uid, "_itemChange", {"delitem": 1}, {"itemId": atn.t}) } else { await Promise.all([ ////去掉item Redis相关 //G.redis.set('item', call.uid, atn.t, 'lastTime', upObj.update.$set.lastTime), //G.redis.numIncrBy('item', call.uid, atn.t, 'num', atn.n), - + G.mongodb.collection('item').updateOne(upObj.filter, upObj.update, upObj.options) ]); // await G.redis.set('item', call.uid, atn.t, 'lastTime', upObj.update.$set.lastTime); @@ -318,7 +335,12 @@ export class PlayerFun { num: item.num + atn.n, lastTime: upObj.update.$set.lastTime }); - addGameLog(call.uid, "_itemChange",{"attritem":1}, {"filter": upObj.filter, "update": upObj.update, "options": upObj.options,newNum:item.num + atn.n}) + addGameLog(call.uid, "_itemChange", {"attritem": 1}, { + "filter": upObj.filter, + "update": upObj.update, + "options": upObj.options, + newNum: item.num + atn.n + }) } } } @@ -344,7 +366,7 @@ export class PlayerFun { }).reduce((a, b) => a.concat(b)); let result = await G.mongodb.collection('equip').insertMany(insertData); - addGameLog(call.uid, "_addEquip",{}, insertData) + addGameLog(call.uid, "_addEquip", {}, insertData) insertData.forEach((v, key) => { let id = result.insertedIds[key].toHexString(); @@ -391,7 +413,7 @@ export class PlayerFun { G.mongodb.collection('equip').deleteOne({uid: call.uid, _id: new ObjectId(_id)}); call.addEventMsg('msg_s2c/EquipChange', _id, {num: 0}); - addGameLog(call.uid, "_cutEquip",{}, {_id:_id}) + addGameLog(call.uid, "_cutEquip", {}, {_id: _id}) } } @@ -419,7 +441,7 @@ export class PlayerFun { }); let result = await G.mongodb.collection('hero').insertMany(insertData); - addGameLog(call.uid, "_addHero",{}, insertData) + addGameLog(call.uid, "_addHero", {}, insertData) for (let key = 0; key < insertData.length; key++) { let v = insertData[key] @@ -474,7 +496,7 @@ export class PlayerFun { G.redis.del('hero', call.uid, _id); G.mongodb.collection('hero').deleteOne({uid: call.uid, _id: new ObjectId(_id)}); call.addEventMsg('msg_s2c/HeroChange', _id, {num: 0}); - addGameLog(call.uid, "_cutHero",{}, {_id:_id}) + addGameLog(call.uid, "_cutHero", {}, {_id: _id}) } } @@ -505,7 +527,7 @@ export class PlayerFun { }).reduce((a, b) => a.concat(b)); let result = await G.mongodb.collection('shiwu').insertMany(insertData); - addGameLog(call.uid, "_addShiWu",{}, insertData) + addGameLog(call.uid, "_addShiWu", {}, insertData) insertData.forEach((v, key) => { let id = result.insertedIds[key].toHexString(); @@ -537,7 +559,7 @@ export class PlayerFun { // G.redis.del('shiwu', call.uid, _id); G.mongodb.collection('shiwu').deleteOne({uid: call.uid, _id: new ObjectId(_id)}); call.addEventMsg('msg_s2c/ShiwuChange', _id, {num: 0}); - addGameLog(call.uid, "_cutShiwu",{}, {_id:_id}) + addGameLog(call.uid, "_cutShiwu", {}, {_id: _id}) } } @@ -559,7 +581,7 @@ export class PlayerFun { }).reduce((a, b) => a.concat(b)); let result = await G.mongodb.collection('peijian').insertMany(insertData); - addGameLog(call.uid, "_addPeiJian",{}, insertData) + addGameLog(call.uid, "_addPeiJian", {}, insertData) insertData.forEach((v, key) => { let {_id, uid, ...ops} = v; @@ -600,7 +622,7 @@ export class PlayerFun { G.redis.del('peijian', call.uid, _id); G.mongodb.collection('peijian').deleteOne({uid: call.uid, _id: new ObjectId(_id)}); call.addEventMsg('msg_s2c/PeijianChange', _id, {num: 0}); - addGameLog(call.uid, "_cutPeijian",{}, {_id:_id}) + addGameLog(call.uid, "_cutPeijian", {}, {_id: _id}) } } diff --git a/src/public/pushgift.ts b/src/public/pushgift.ts new file mode 100644 index 0000000..72fcfde --- /dev/null +++ b/src/public/pushgift.ts @@ -0,0 +1,155 @@ +import {PayFun} from "./pay"; + +const PushGiftType = { + LevelGift: 1, // 关卡推送 + ItemGift: 2, // 道具推送 + LoseGift: 3, // 战败推送 + RecruitGift: 4, // 十连推送 + LvGift: 5, // 等级推送 +} + +export class PushGiftFun { + + static async getGift(uid: string) { + // 查询所有礼包 + return (await G.mongodb.collection("pushgift").find({ + uid: uid + }).toArray()).map((i) => { + let {_id, ...gift} = i; + return gift + }) + } + + static async chkItemGift(uid: string, atn: atn) { + let gift_ids = []; + for (let id in G.gc.tuisonglibao) { + let conf = G.gc.tuisonglibao[id]; + if (conf.type == PushGiftType.ItemGift && conf.num[0].a == atn.a && conf.num[0].t == atn.t) { + gift_ids.push(id); + } + } + if (gift_ids.length <= 0) { + return + } else { + this.pushGift(uid, gift_ids) + } + } + + static async chkLevelGift(uid: string, level: number) { + let gift_ids = []; + for (let id in G.gc.tuisonglibao) { + let conf = G.gc.tuisonglibao[id]; + if (conf.type == PushGiftType.LevelGift && conf.num[0] == level) { + gift_ids.push(id); + } + } + if (gift_ids.length <= 0) { + return + } else { + this.pushGift(uid, gift_ids) + } + } + + static async chkLvGift(uid: string, lv: number) { + let gift_ids = []; + for (let id in G.gc.tuisonglibao) { + let conf = G.gc.tuisonglibao[id]; + if (conf.type == PushGiftType.LevelGift && conf.num[0] == lv) { + gift_ids.push(id); + } + } + if (gift_ids.length <= 0) { + return + } else { + this.pushGift(uid, gift_ids) + } + } + + static async chkLoseGift(uid: string) { + let gift_ids = []; + for (let id in G.gc.tuisonglibao) { + let conf = G.gc.tuisonglibao[id]; + if (conf.type == PushGiftType.LoseGift) { + gift_ids.push(id); + } + } + if (gift_ids.length <= 0) { + return + } else { + this.pushGift(uid, gift_ids) + } + } + + static async chkRecruitGift(uid: string) { + let gift_ids = []; + for (let id in G.gc.tuisonglibao) { + let conf = G.gc.tuisonglibao[id]; + if (conf.type == PushGiftType.RecruitGift) { + gift_ids.push(id); + } + } + if (gift_ids.length <= 0) { + return + } else { + this.pushGift(uid, gift_ids) + } + } + + static async pushGift(uid: string, gift_ids: string[]) { + let gifts = {}; + (await this.getGift(uid)).forEach(i => gifts[i.id] = i) + for (let gift_id of gift_ids) { + let conf = G.gc.tuisonglibao[gift_id]; + if (gifts[gift_id] && conf.displayCD > 0 && gifts[gift_id].ctime + conf.displayCD > G.time) { + continue + } + if (conf.type == PushGiftType.RecruitGift) { + if (gifts[gift_id]) { + if (gift_ids[gift_id].ext_data.num + 1 >= conf.num[0]) { + this.addGift(uid, gift_id, {num: 0}) + } else { + G.mongodb.collection("pushgift").updateOne({ + uid: uid, id: gift_id + }, {"$inc": {"ext_data.num": 1}}) + } + } else { + // 招募礼包不存在 初始化数据 记录招募次数 + G.mongodb.collection("pushgift").updateOne({ + uid: uid, id: gift_id + }, { + $set: { + ctime: 0, + passTime: 0, + ext_data: {num: 1}, + buy: conf.payId.map(() => 0), + } + }, {upsert: true}) + } + } else { + this.addGift(uid, gift_id, {}) + } + } + } + + static async addGift(uid: string, giftid: string, ext_data: { [key: string]: any }) { + let conf = G.gc.tuisonglibao[giftid]; + // 更新礼包 + G.mongodb.collection("pushgift").updateOne({ + uid: uid, id: giftid + }, { + $set: { + ctime: G.time, + ext_data: ext_data, + passTime: G.time + conf.time, + buy: conf.payId.map(() => 0), + } + }, {upsert: true}) + + // 删除购买记录 + PayFun.delPayLog(uid, ...conf.payId.map(i => { + return {payId: i, val: []} + })) + // 推送客户端消息 + G.server.sendMsgByUid(uid, "msg_s2c/PushGiftChange", 1); + } +} \ No newline at end of file diff --git a/src/shared/protocols/msg_s2c/MsgPushGiftChange.ts b/src/shared/protocols/msg_s2c/MsgPushGiftChange.ts new file mode 100644 index 0000000..4c04b9b --- /dev/null +++ b/src/shared/protocols/msg_s2c/MsgPushGiftChange.ts @@ -0,0 +1 @@ +export type MsgPushGiftChange = 1; diff --git a/src/shared/protocols/pushgift/PtlOpen.ts b/src/shared/protocols/pushgift/PtlOpen.ts new file mode 100644 index 0000000..829c306 --- /dev/null +++ b/src/shared/protocols/pushgift/PtlOpen.ts @@ -0,0 +1,5 @@ +import {Gift} from "../../../module/collection_pushgift"; + +export type ReqOpen = {} + +export type ResOpen = { gifts: Gift[] }