Files
ZYZ/shared/domain/activityField/rechargeMoneyField.ts
2022-03-04 10:19:54 +08:00

163 lines
4.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { ActivityModelType } from '../../db/Activity';
import { ActivityRechargeMoneyModelType } from '../../db/ActivityRechargeMoney';
import { getRandEelmWithWeight, getRandSingleEelm } from '../../pubUtils/util';
import { ActivityBase } from './activityField';
interface RechargeMoneyPoolInDb {
id: number; // 奖池项唯一id
gid: number;
count: number;
quality: number; // 物品的品级相同的品级平分概率不同品级直接按percent来做概率
}
interface RechargeMoneyWeightInDb {
quality: number; // pool中的品级
weight: number; // 权重,先随机出这个品级的物品的概率,然后再平均随机出这个品级下的物品
}
interface RechargeMoneyPercentInDb {
todayIndex: number; // 第几天和前面排名奖励的填法一样如第1天、第2-10天只需填todayIndex=1和10的两项
data: RechargeMoneyWeightInDb[]; // 这一天的权重
}
interface RechargeMoneyInDb {
pool: RechargeMoneyPoolInDb[];
minPrice: number;
percent: RechargeMoneyPercentInDb[];
}
// 充值记录
export class RechargeMoneyPool {
id: number; // 卡池id
gid: number;
count: number;
quality: number; // 物品的品级
hasGet: boolean = false; // 是否已经获取过了
constructor(data: RechargeMoneyPoolInDb) {
this.id = data.id;
this.gid = data.gid;
this.count = data.count;
this.quality = data.quality;
}
setHasGet(hasGet: boolean) {
this.hasGet = hasGet;
}
}
export class RechargeMoneyWeight {
quality: number;
weight: number;
constructor(data: RechargeMoneyWeightInDb) {
this.quality = data.quality;
this.weight = data.weight;
}
}
export class RechargeMoneyPercent {
todayIndex: number;
data: RechargeMoneyWeight[] = [];
constructor(data: RechargeMoneyPercentInDb) {
this.todayIndex = data.todayIndex;
for(let obj of data.data) {
this.data.push(new RechargeMoneyWeight(obj));
}
}
}
// 累计充值数据
export class RechargeMoneyData extends ActivityBase {
private map: Map<number, number> = new Map(); // poolId => index
private percentMap: Map<number, number> = new Map(); // todayIndex => 的index
pool: RechargeMoneyPool[] = [];
minPrice: number = 0;
percent: RechargeMoneyPercent[] = [];
ticketCnt: number = 0; // 签数
recordArr: ActivityRechargeMoneyModelType[] = [];
private findItemFromPool(poolId: number) {
let index = this.map.get(poolId);
return this.pool[index];
}
private findTodayPercent() {
let index = this.percentMap.get(this.todayIndex);
return this.percent[index];
}
public pull() {
let percent = this.findTodayPercent();
if(!percent) return false;
let randPercent = percent.data.filter(cur => {
let hasAllGet = true;
for(let { quality, hasGet } of this.pool) {
if(quality == cur.quality && !hasGet) hasAllGet = false;
}
return !hasAllGet;
});
if(randPercent.length <=0) return false; // 全部抽完了
let qualityResult = getRandEelmWithWeight(randPercent);
if(!qualityResult) return false;
let elems: RechargeMoneyPool[] = [];
for(let pool of this.pool) {
if(pool && !pool.hasGet && pool.quality == qualityResult.dic.quality) {
elems.push(pool);
}
}
let result = getRandSingleEelm(elems);
return result;
}
//解析玩家购买记录
public setPlayerRecords(data: ActivityRechargeMoneyModelType[]) {
if (!data) return;
for(let record of data) {
if(record.RMB >= this.minPrice) {
if(!record.rewardTime) {
this.ticketCnt++;
this.recordArr.push(record);
}
}
if(record.poolId) {
let pool = this.findItemFromPool(record.poolId);
if(pool) pool.setHasGet(true);
}
}
}
public getShowResult() {
return {
...this.getBaseKeys(),
ticketCnt: this.ticketCnt,
pool: this.pool,
}
}
public initData(data: string) {
let dataObj: RechargeMoneyInDb = JSON.parse(data);
console.log(dataObj);
for(let pool of dataObj.pool) {
let obj = new RechargeMoneyPool(pool);
this.pool.push(obj);
this.map.set(obj.id, this.pool.length - 1);
}
this.minPrice = dataObj.minPrice;
for(let percent of dataObj.percent) {
let obj = new RechargeMoneyPercent(percent);
this.percent.push(obj);
this.percentMap.set(obj.todayIndex, this.percent.length - 1);
}
}
constructor(activityData: ActivityModelType, createTime: number) {
super(activityData, createTime)
this.initData(activityData.data)
}
}