活动:弹出商店接口

This commit is contained in:
qiaoxin
2021-05-19 21:02:26 +08:00
parent 19d2021ac4
commit ca19f04282
4 changed files with 293 additions and 0 deletions

View File

@@ -0,0 +1,81 @@
import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
/**
* 购买记录
*/
export class BuyRecord {
@prop({ required: true })
id: number; //物品id标识
@prop({ required: true })
time: Date; //购买时间
}
/**
* 活动系统 - 弹出商店
*/
@index({ roleId: 1 })
export default class ActivityPopUpShop extends BaseModel {
@prop({ required: true })
serverId: number; // 服id
@prop({ required: true })
activityId: number; // 活动Id
@prop({ required: true })
roleId: string; // 用户Id
@prop({ required: true })
beginTime: Date; // 开始时间
@prop({ required: true })
endTime: Date; // 结束时间
@prop({ required: true })
taskId: number; // 任务id
@prop({ required: true })
records: BuyRecord[]; // 购买记录
//购买奖励的记录
public static async addRecord(serverId: number, activityId: number, roleId: string, taskId: number, id: number) {
let nowDate = new Date();
let result: ActivityPopUpShopModelType = await ActivityPopUpShopModel.findOneAndUpdate({
serverId, roleId, activityId, taskId,
beginTime: { $lt: nowDate }, endTime: { $gt: nowDate }
},
{ $push: { records: { id, time: new Date() } } }, { upsert: true, new: true }).lean(true);
return result;
}
//查询现在正在进行的活动数据
public static async findAllOpenData(serverId: number, activityId: number, roleId: string) {
let nowDate = new Date();
let result: ActivityPopUpShopModelType[] = await ActivityPopUpShopModel.find({
serverId, roleId, activityId,
beginTime: { $lt: nowDate }, endTime: { $gt: nowDate }
}).lean(true);
return result;
}
//根据活动taskId查询现在正在进行的活动数据
public static async findOpenDataByTaskId(serverId: number, activityId: number, roleId: string, taskId: number) {
let nowDate = new Date();
let result: ActivityPopUpShopModelType = await ActivityPopUpShopModel.findOne({
serverId, roleId, activityId, taskId,
beginTime: { $lt: nowDate }, endTime: { $gt: nowDate }
}).lean(true);
return result;
}
//删除活动领取记录
public static async deleteActivity(serverId: number, activityId: number, roleId: string, taskId: number) {
let nowDate = new Date();
await ActivityPopUpShopModel.deleteMany({
roleId, activityId, taskId,
beginTime: { $lt: nowDate }, endTime: { $gt: nowDate }
});
}
}
export const ActivityPopUpShopModel = getModelForClass(ActivityPopUpShop);
export interface ActivityPopUpShopModelType extends Pick<DocumentType<ActivityPopUpShop>, keyof ActivityPopUpShop> { }
export type ActivityPopUpShopModelTypeParam = Partial<ActivityPopUpShopModelType>; // 将所有字段变成可选项

View File

@@ -0,0 +1,72 @@
import { DAILYRMBGIFTS_DAYS } from '../../consts';
import { ActivityModelType } from '../../db/Activity';
import { ActivityPopUpShopModelType } from '../../db/ActivityPopUpShop';
import { deltaDays } from '../../pubUtils/util';
import { ActivityBase } from './activityField';
// 商品数据
export class PopUpShopItem {
id: number; // 第几个从1开始
productID: string; // 商品id支付时使用
reward: string; //任务奖励,格式:1&3&1(类型&id&数量) 类型定义:1.英雄2.物品
countMax: number = 0; //可购买的最大次数0表示不限制
name: string; //名字
buyCount: number = 0; //购买过的次数
constructor(data: any) {
this.id = data.id;
this.reward = data.reward;
this.countMax = data.countMax;
this.productID = data.productID;
this.name = data.name;
}
}
// 弹框商店
export class PopUpShopData {
name: string = '';//活动名称
taskId: number = 0;//id
list: Array<PopUpShopItem> = [];//每件商品信息
public findItem(id: number) {
let index = this.list.findIndex(obj => { return obj && obj.id === id });
return (index != -1) ? this.list[index] : null
}
//全部领取完成
public isComplete() {
for (let item of this.list) {
if (item.countMax == 0 ||
(item.countMax > 0 && item.buyCount < item.countMax)) {
return false
}
}
return true;
}
//解析玩家购买记录
public setPlayerRecords(data: ActivityPopUpShopModelType) {
if (!data) {
return;
}
for (let item of this.list) {
let buyRecords = data.records.filter(obj => { return obj && obj.id === item.id });
item.buyCount = buyRecords.length;
}
}
public initData(data: any) {
this.taskId = data.taskId;
let arr = data.data;
for (let obj of arr) {
this.list.push(new PopUpShopItem(obj))
}
}
constructor(activityData: any) {
this.initData(activityData)
}
}