180 lines
6.3 KiB
TypeScript
180 lines
6.3 KiB
TypeScript
// 节日活动 - 划龙舟
|
|
import { ActivityModelType } from '../../db/Activity';
|
|
import { ActivityEntertainRecModelType } from '../../db/ActivityEntertainRec';
|
|
// import { ActivityEntertainRecModelType } from '../../db/ActivityEntertainRec';
|
|
import { ActivityBase } from './activityField';
|
|
|
|
// 后台格式
|
|
interface HeroNumInDb {
|
|
id: number;// 第几次拜访
|
|
reward: string; // 奖励,type&id&count
|
|
}
|
|
|
|
interface HeroInDb {
|
|
id: number // 编号
|
|
index: number; // 位置
|
|
name: string; // 名字
|
|
imageName: string; // 图片
|
|
condition: string; // 解锁条件,type1:宴请x名武将,type2:宴请某个人
|
|
hid: number; // 武将id
|
|
num: HeroNumInDb[]; // 第几次的奖励是什么
|
|
}
|
|
|
|
interface EntertainDataInDb {
|
|
buyCost: string; // 购买一次宴请次数的消耗,type&id&count
|
|
dailyBuyCnt: number; // 每天可以购买的次数
|
|
freeCnt: number; // 每天可以免费划船的次数
|
|
heroes: HeroInDb[]; // 可以宴请的武将的次数
|
|
}
|
|
|
|
class HeroData {
|
|
id: number; // 编号
|
|
index: number; // 位置
|
|
name: string; // 名字
|
|
imageName: string; // 图片
|
|
condition: string; // 解锁条件,type1:宴请x名武将;type2:宴请某个人
|
|
conditionArr: {type: number, param: number}[] = [];
|
|
hid: number; // 武将id
|
|
numArr: HeroNumInDb[] = []; // 第几次能获得什么奖励
|
|
|
|
maxNum: number = 0; // 最大次数
|
|
num: number = 0; // 玩家当前第几次拜访
|
|
reward: string = '&'; // 玩家下一次可以获得的奖励
|
|
|
|
constructor(data: HeroInDb) {
|
|
this.id = data.id;
|
|
this.index = data.index;
|
|
this.name = data.name;
|
|
this.imageName = data.imageName;
|
|
this.condition = data.condition;
|
|
let arr = data.condition.split('|');
|
|
for(let str of arr) {
|
|
let obj = str?.split('&')||[];
|
|
if(obj[0]) this.conditionArr.push({ type: parseInt(obj[0]), param: parseInt(obj[1]) });
|
|
}
|
|
this.hid = data.hid;
|
|
this.numArr = data.num;
|
|
this.maxNum = data.num.length;
|
|
this.calReward();
|
|
}
|
|
|
|
public incNum() {
|
|
this.num++;
|
|
this.calReward();
|
|
}
|
|
|
|
public calReward() {
|
|
// console.log('####', this.numArr, this.num)
|
|
let nextReward = this.numArr.find(cur => cur.id == this.num + 1);
|
|
this.reward = nextReward?.reward||'&';
|
|
}
|
|
|
|
public getShowResult() {
|
|
let { id, index, name, imageName, condition, hid, maxNum, num, reward } = this;
|
|
return { id, index, name, imageName, condition, hid, maxNum, num, reward }
|
|
}
|
|
}
|
|
|
|
export class EntertainData extends ActivityBase {
|
|
buyCost: string; // 购买一次划船次数的消耗,type&id&count
|
|
dailyBuyCnt: number; // 每天可以购买的次数
|
|
freeCntDaily: number; // 每天可以免费划船的次数
|
|
heroes: HeroData[] = []; // 宴请武将
|
|
|
|
freeCnt: number = 0; // 累积到现在可以免费的次数
|
|
maxBuyCnt: number = 0; // 累积到现在可以购买的次数
|
|
buyCnt: number = 0; // 累积到现在已经购买了的次数
|
|
todayPlayCnt: number = 0; // 今天玩的次数
|
|
playCnt: number = 0; // 总计玩的次数
|
|
|
|
invitedHeroNum: number = 0; // 宴请了的武将的数量
|
|
invitedHids: number[] = []; // 宴请了的武将的位置
|
|
|
|
constructor(activityData: ActivityModelType, createTime: number, serverTime: number) {
|
|
super(activityData, createTime, serverTime)
|
|
this.initData(activityData.data)
|
|
}
|
|
|
|
public initData(data: string): void {
|
|
let dataObj: EntertainDataInDb = JSON.parse(data);
|
|
if (!dataObj) return;
|
|
|
|
this.buyCost = dataObj.buyCost || '&';
|
|
this.dailyBuyCnt = dataObj.dailyBuyCnt || 0;
|
|
this.freeCntDaily = dataObj.freeCnt || 0;
|
|
this.freeCnt = this.freeCntDaily * this.todayIndex;
|
|
this.maxBuyCnt = this.todayIndex * this.dailyBuyCnt;
|
|
for (let data of (dataObj.heroes || [])) {
|
|
this.heroes.push(new HeroData(data));
|
|
}
|
|
}
|
|
|
|
public findHeroById(id: number) {
|
|
let hero = this.heroes.find(cur => cur.id == id);
|
|
return hero;
|
|
}
|
|
|
|
public setPlayerRecords(playerData: ActivityEntertainRecModelType) {
|
|
if (!playerData) return;
|
|
this.buyCnt = playerData.buyCnt || 0;
|
|
this.todayPlayCnt = 0;
|
|
this.playCnt = 0;
|
|
let recByDay = new Map<number, number>();
|
|
for (let { id, index, todayIndex } of (playerData.record || [])) {
|
|
if (todayIndex == this.todayIndex) {
|
|
this.todayPlayCnt++;
|
|
this.playCnt++;
|
|
} else {
|
|
let n = recByDay.get(todayIndex) || 0;
|
|
if (n >= this.freeCntDaily) { // 不包含之前免费玩的次数
|
|
this.playCnt++;
|
|
} else {
|
|
this.freeCnt--;
|
|
}
|
|
recByDay.set(todayIndex, n + 1);
|
|
}
|
|
|
|
let hero = this.findHeroById(id);
|
|
hero.incNum();
|
|
if(this.invitedHids.indexOf(index) == -1) {
|
|
this.invitedHids.push(index);
|
|
this.invitedHeroNum++;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public updateBuyCnt(playerData: ActivityEntertainRecModelType) {
|
|
if (!playerData) return;
|
|
this.buyCnt = playerData.buyCnt || 0;
|
|
this.todayPlayCnt = 0;
|
|
this.playCnt = 0;
|
|
let recByDay = new Map<number, number>();
|
|
for (let { todayIndex } of (playerData.record || [])) {
|
|
if (todayIndex == this.todayIndex) {
|
|
this.todayPlayCnt++;
|
|
this.playCnt++;
|
|
} else {
|
|
let n = recByDay.get(todayIndex) || 0;
|
|
if (n >= this.freeCnt) { // 不包含之前免费玩的次数
|
|
this.playCnt++;
|
|
}
|
|
recByDay.set(todayIndex, n + 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
public getShowResult() {
|
|
return {
|
|
...this.getBaseKeys(),
|
|
buyCost: this.buyCost,
|
|
dailyBuyCnt: this.dailyBuyCnt,
|
|
freeCnt: this.freeCnt,
|
|
maxBuyCnt: this.maxBuyCnt,
|
|
buyCnt: this.buyCnt,
|
|
todayPlayCnt: this.todayPlayCnt,
|
|
playCnt: this.playCnt,
|
|
heroes: this.heroes.map(route => route.getShowResult()),
|
|
}
|
|
}
|
|
} |