活动:刷新商店支持根据权重筛选商品

This commit is contained in:
qiaoxin
2021-06-25 20:44:40 +08:00
parent 4ac0e3ae09
commit a6ce67b2c5
3 changed files with 89 additions and 1 deletions
@@ -27,6 +27,10 @@ export async function getRefreshShopActivity(serverId: number, roleId: string) {
let playerData = new RefreshShopData(activityData); let playerData = new RefreshShopData(activityData);
let playerRecord: ActivityRefreshShopModelType = await ActivityRefreshShopModel.findData(activityData.activityId, roleId, playerData.roundIndex); let playerRecord: ActivityRefreshShopModelType = await ActivityRefreshShopModel.findData(activityData.activityId, roleId, playerData.roundIndex);
if (!playerRecord) {//生成商店可购买商品的数据
let randomGoods = playerData.randomGoodsItems();
playerRecord = await ActivityRefreshShopModel.createShop(activityData.activityId, roleId, playerData.roundIndex, randomGoods);
}
playerData.setPlayerRecords(playerRecord); playerData.setPlayerRecords(playerRecord);
return playerData; return playerData;
} }
@@ -44,6 +48,10 @@ export async function getPlayerRefreshShopData(activityId: number, serverId: num
let playerData = new RefreshShopData(activityData); let playerData = new RefreshShopData(activityData);
let playerRecord: ActivityRefreshShopModelType = await ActivityRefreshShopModel.findData(activityId, roleId, playerData.roundIndex); let playerRecord: ActivityRefreshShopModelType = await ActivityRefreshShopModel.findData(activityId, roleId, playerData.roundIndex);
if (!playerRecord) {//生成商店可购买商品的数据
let randomGoods = playerData.randomGoodsItems();
playerRecord = await ActivityRefreshShopModel.createShop(activityData.activityId, roleId, playerData.roundIndex, randomGoods);
}
playerData.setPlayerRecords(playerRecord); playerData.setPlayerRecords(playerRecord);
return playerData; return playerData;
} }
@@ -61,6 +69,10 @@ export async function getPlayerRefreshShopDataByRoundIndex(activityId: number, s
let playerData = new RefreshShopData(activityData); let playerData = new RefreshShopData(activityData);
playerData.roundIndex = roundIndex; playerData.roundIndex = roundIndex;
let playerRecord: ActivityRefreshShopModelType = await ActivityRefreshShopModel.findData(activityId, roleId, playerData.roundIndex); let playerRecord: ActivityRefreshShopModelType = await ActivityRefreshShopModel.findData(activityId, roleId, playerData.roundIndex);
if (!playerRecord) {//生成商店可购买商品的数据
let randomGoods = playerData.randomGoodsItems();
playerRecord = await ActivityRefreshShopModel.createShop(activityData.activityId, roleId, playerData.roundIndex, randomGoods);
}
playerData.setPlayerRecords(playerRecord); playerData.setPlayerRecords(playerRecord);
return playerData; return playerData;
} }
+20
View File
@@ -16,6 +16,17 @@ export class BuyRecord {
} }
/**
* 可购买商品
*/
export class GoodsInfo {
@prop({ required: true })
pageIndex: number; //第几页
@prop({ required: true })
id: number; //物品id标识
}
/** /**
* 活动系统 - 新手限定、每日限购、每周限购 * 活动系统 - 新手限定、每日限购、每周限购
*/ */
@@ -30,6 +41,8 @@ export default class Activity_Refresh_Shop extends BaseModel {
roundIndex: number; // 第几个周期,从1开始 roundIndex: number; // 第几个周期,从1开始
@prop({ required: true }) @prop({ required: true })
records: BuyRecord[]; // 购买记录 records: BuyRecord[]; // 购买记录
@prop({ required: true })
goods: GoodsInfo[]; // 可购买商品
//购买领取奖励的记录 //购买领取奖励的记录
public static async addRecord(activityId: number, roleId: string, roundIndex: number, pageIndex: number, id: number) { public static async addRecord(activityId: number, roleId: string, roundIndex: number, pageIndex: number, id: number) {
@@ -38,6 +51,13 @@ export default class Activity_Refresh_Shop extends BaseModel {
return result; return result;
} }
//购买领取奖励的记录
public static async createShop(activityId: number, roleId: string, roundIndex: number, goods: GoodsInfo[]) {
let result: ActivityRefreshShopModelType = await ActivityRefreshShopModel.findOneAndUpdate({ roleId, activityId, roundIndex },
{ $push: { goods: { $each: goods } } }, { upsert: true, new: true }).lean(true);
return result;
}
//根据活动id查询活动数据 //根据活动id查询活动数据
public static async findData(activityId: number, roleId: string, roundIndex: number) { public static async findData(activityId: number, roleId: string, roundIndex: number) {
let result: ActivityRefreshShopModelType = await ActivityRefreshShopModel.findOne({ roleId, activityId, roundIndex }).lean(true); let result: ActivityRefreshShopModelType = await ActivityRefreshShopModel.findOne({ roleId, activityId, roundIndex }).lean(true);
@@ -1,7 +1,7 @@
import moment = require('moment'); import moment = require('moment');
import { REFRESH_TIME } from '../../consts'; import { REFRESH_TIME } from '../../consts';
import { ActivityModelType } from '../../db/Activity'; import { ActivityModelType } from '../../db/Activity';
import { ActivityRefreshShopModelType } from '../../db/ActivityRefreshShop'; import { ActivityRefreshShopModelType, GoodsInfo } from '../../db/ActivityRefreshShop';
import { ActivityBase } from './activityField'; import { ActivityBase } from './activityField';
// 每个商品的内容 // 每个商品的内容
@@ -16,6 +16,9 @@ export class RefreshShopItem {
imageName: string; imageName: string;
discount: number; //折扣 discount: number; //折扣
pageIndex: number; // 第几页 pageIndex: number; // 第几页
levelLimitMin: number; //玩家最低等级 0:不限制
levelLimitMax: number; //玩家最高等级 0:不限制
weight: number; //随机权重
buyCount: number = 0; //购买过的次数 buyCount: number = 0; //购买过的次数
@@ -30,6 +33,9 @@ export class RefreshShopItem {
this.imageName = data.imageName; this.imageName = data.imageName;
this.discount = data.discount ? data.discount : 0; this.discount = data.discount ? data.discount : 0;
this.consume = data.consume ? data.consume : ''; this.consume = data.consume ? data.consume : '';
this.levelLimitMin = data.levelLimitMin ? data.levelLimitMin : 0;
this.levelLimitMax = data.levelLimitMax ? data.levelLimitMax : 0;
this.weight = data.weight ? data.weight : 0;
this.buyCount = 0; this.buyCount = 0;
} }
} }
@@ -39,6 +45,7 @@ export class RefreshShopItem {
export class RefreshShopPage { export class RefreshShopPage {
pageIndex: number; // 第几页 pageIndex: number; // 第几页
name: string; //名字 name: string; //名字
viewCount: number; //随机可购买的商品个数
items: Array<RefreshShopItem> = [];//商品列表 items: Array<RefreshShopItem> = [];//商品列表
constructor(data: any) { constructor(data: any) {
this.pageIndex = data.pageIndex; this.pageIndex = data.pageIndex;
@@ -46,6 +53,7 @@ export class RefreshShopPage {
for (let item of data.items) { for (let item of data.items) {
this.items.push(new RefreshShopItem(item, data.pageIndex)) this.items.push(new RefreshShopItem(item, data.pageIndex))
} }
this.viewCount = data.viewCount ? data.viewCount : this.items.length;
} }
} }
@@ -58,6 +66,45 @@ export class RefreshShopData extends ActivityBase {
nextRefreshTime: number;//下次刷新时间 nextRefreshTime: number;//下次刷新时间
roundIndex: number = 1;//周期数从1开始 roundIndex: number = 1;//周期数从1开始
//随机出商店可购买的商品数据
public randomGoodsItems() {
let goods: GoodsInfo[] = [];
for (let pageData of this.list) {
let pageIndex = pageData.pageIndex;
let randomCount = pageData.viewCount;
pageData.items = this.randomGoods(pageData.items, randomCount);
for (let item of pageData.items) {
goods.push({
pageIndex: pageIndex,
id: item.id
})
}
}
return goods;
}
//根据权重随机物品
public randomGoods(allGoodsArray: RefreshShopItem[], maxNum: number) {
let maxCount = 100;//防止策划数值填错...出现过这个情况
let goodsArray = [];
while ((goodsArray.length < maxNum) && (allGoodsArray.length > 0) && (maxCount >= 0)) {
let totalWeight = allGoodsArray.reduce((pre, cur) => {
return pre + cur.weight || 1;
}, 0);
let random = Math.floor(totalWeight * Math.random());
for (let index = 0; index < allGoodsArray.length; index++) {
random -= allGoodsArray[index].weight;
if (random < 0) {
let rmObj = allGoodsArray.splice(index, 1)
goodsArray.push(rmObj[0]);
break;
}
}
maxCount--;
}
return goodsArray;
}
public findItemByProductID(productID: string): RefreshShopItem { public findItemByProductID(productID: string): RefreshShopItem {
for (let pageData of this.list) { for (let pageData of this.list) {
let index = pageData.items.findIndex(obj => { return obj && obj.productID === productID }); let index = pageData.items.findIndex(obj => { return obj && obj.productID === productID });
@@ -91,6 +138,15 @@ export class RefreshShopData extends ActivityBase {
item.buyCount = buyRecords.length; item.buyCount = buyRecords.length;
} }
} }
let goods = data.goods ? data.goods : [];
for (let pageData of this.list) {
pageData.items = pageData.items.filter(item => {
let index = goods.findIndex(obj => { return obj.pageIndex == pageData.pageIndex && obj.id == item.id });
return (index != -1);
})
}
} }