106 lines
3.9 KiB
TypeScript
106 lines
3.9 KiB
TypeScript
// 节日活动 - 七夕鹊桥
|
||
import { ActivityModelType } from '../../db/Activity';
|
||
import { ActivityMidAutumnRecModelType, MidAutumnRecord } from '../../db/ActivityMidAutumnRec';
|
||
import { ActivityBase } from './activityField';
|
||
|
||
// 后台格式
|
||
interface MidAutumnSceneDataInDb {
|
||
dayIndex: number; // 第几天
|
||
id: number; // 场景
|
||
img: string; // 图片
|
||
}
|
||
|
||
interface MidAutumnDataInDb {
|
||
buyCost: string; // 购买一次次数的消耗,type&id&count
|
||
dailyBuyCnt: number; // 每天可以购买的次数
|
||
freeCnt: number; // 每天可以免费的次数
|
||
scenes: MidAutumnSceneDataInDb[];
|
||
rewards: string; // 单局奖励
|
||
}
|
||
|
||
export class MidAutumnData extends ActivityBase {
|
||
buyCost: string; // 购买一次次数的消耗,type&id&count
|
||
dailyBuyCnt: number; // 每天可以购买的次数
|
||
freeCntDaily: number; // 每天可以免费的次数
|
||
rewards: string; // 奖励
|
||
|
||
freeCnt: number = 0; // 累积到现在可以免费的次数
|
||
maxBuyCnt: number = 0; // 累积到现在可以购买的次数
|
||
buyCnt: number = 0; // 累积到现在已经购买了的次数
|
||
todayPlayCnt: number = 0; // 今天玩的次数
|
||
playCnt: number = 0; // 总玩的次数
|
||
sceneId: number = 0; // 今天玩的场景id
|
||
img: string = '&'; // 场景的图片
|
||
|
||
recordTodayIndex: number = 0;
|
||
records: MidAutumnRecord[] = [];
|
||
|
||
constructor(activityData: ActivityModelType, createTime: number, serverTime: number) {
|
||
super(activityData, createTime, serverTime)
|
||
this.initData(activityData.data)
|
||
}
|
||
|
||
public initData(data: string): void {
|
||
let dataObj: MidAutumnDataInDb = JSON.parse(data);
|
||
if (!dataObj) return;
|
||
|
||
this.buyCost = dataObj.buyCost || '&';
|
||
this.dailyBuyCnt = dataObj.dailyBuyCnt || 0;
|
||
this.freeCntDaily = dataObj.freeCnt || 0;
|
||
this.rewards = dataObj.rewards || '&';
|
||
this.freeCnt = this.freeCntDaily * this.todayIndex;
|
||
this.maxBuyCnt = this.todayIndex * this.dailyBuyCnt;
|
||
this.recordTodayIndex = this.todayIndex;
|
||
|
||
let curScene = dataObj.scenes.find(cur => cur.dayIndex == this.todayIndex);
|
||
if(curScene) {
|
||
this.sceneId = curScene.id;
|
||
this.img = curScene.img;
|
||
}
|
||
}
|
||
|
||
public setPlayerRecords(playerData: ActivityMidAutumnRecModelType) {
|
||
this.updatePlayerRecord(playerData);
|
||
}
|
||
|
||
public updatePlayerRecord(playerData: ActivityMidAutumnRecModelType) {
|
||
if (!playerData) return;
|
||
this.buyCnt = playerData.buyCnt || 0;
|
||
this.todayPlayCnt = 0;
|
||
this.playCnt = 0;
|
||
this.recordTodayIndex = this.todayIndex;
|
||
if(playerData.record) {
|
||
playerData.record.sort((a, b) => a.time.getTime() - b.time.getTime());
|
||
for(let data of playerData.record) {
|
||
let { todayIndex, isSuccess } = data;
|
||
if(todayIndex == this.todayIndex && isSuccess) this.todayPlayCnt ++;
|
||
if(isSuccess) this.playCnt ++;
|
||
this.records.push(data);
|
||
}
|
||
if(this.todayPlayCnt >= this.freeCntDaily && this.playCnt < this.freeCnt) {
|
||
for(let i = 1; i <= this.todayIndex; i++) {
|
||
let times = playerData.record.filter(cur => cur.todayIndex == i && cur.isSuccess).length;
|
||
if(times < this.freeCntDaily) {
|
||
this.recordTodayIndex = i; break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
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,
|
||
sceneId: this.sceneId,
|
||
img: this.img,
|
||
rewards: this.rewards,
|
||
}
|
||
}
|
||
} |