Files
ZYZ/shared/domain/activityField/miniGameField.ts
2023-03-27 19:36:16 +08:00

127 lines
4.4 KiB
TypeScript

// 节日活动 - 小游戏
import { pick } from 'underscore';
import { ActivityModelType } from '../../db/Activity';
import { ActivityMiniGameModelType } from '../../db/ActivityMiniGame';
import { ActivityMiniGameRecModelType } from '../../db/ActivityMiniGameRec';
import { ActivityBase } from './activityField';
// 后台格式
interface MiniGameBuyCountInDb {
day: number; // 第几天
buyCnt: number; // 可以购买的次数(累积)
freeCnt: number; // 免费次数(每日刷新)
}
interface MiniGameBoxInDb {
id: number; // 宝箱id
score: number; // 分数
reward: string; // type&id&count
}
interface MiniGameDataInDb {
gameType: number; // 小游戏类型 1-消消乐 2-射箭
buyCounts: MiniGameBuyCountInDb[]; // 可购买次数
consume: string; // 购买次数的花费 type&id&count
reward: string; // 每局的参与奖励 type&id&count
box: MiniGameBoxInDb[]; // 宝箱
}
class MiniGameBox {
id: number; // 宝箱id
score: number; // 分数/成功次数
reward: string; // type&id&count
hasReceived: boolean = false; // 是否已领取
constructor(data: MiniGameBoxInDb) {
this.id = data.id;
this.score = data.score;
this.reward = data.reward;
}
public setReceive(box: number[]) {
if(box.indexOf(this.id) != -1) this.hasReceived = true;
}
}
export class MiniGameData extends ActivityBase {
gameType: number; // 小游戏类型 1-消消乐 2-射箭
consume: string; // 购买次数的消耗 type&id&count
reward: string; // 单局游戏的参与奖励 type&id&count
freeCnt: number = 0; // 今天的免费次数
maxBuyCnt: number = 0; // 今天可以购买的次数
box: MiniGameBox[] = []; // 宝箱
buyCounts: MiniGameBuyCountInDb[];
playCnt: number = 0; // 已玩次数,当 playCnt < freeCnt + buyCnt 时可以玩
todayPlayCnt: number = 0; // 今日已挑战次数
buyCnt: number = 0; // 已购买次数
score: number = 0; // 当前总分
constructor(activityData: ActivityModelType, createTime: number, serverTime: number) {
super(activityData, createTime, serverTime)
this.initData(activityData.data)
}
public initData(data: string): void {
let dataObj: MiniGameDataInDb = JSON.parse(data);
if(!dataObj) return;
this.gameType = dataObj.gameType;
this.consume = dataObj.consume;
this.reward = dataObj.reward;
this.buyCounts = dataObj.buyCounts;
for(let { day, buyCnt, freeCnt } of (dataObj.buyCounts||[])) {
if(day == this.todayIndex) this.freeCnt = freeCnt;
if(day <= this.todayIndex) this.maxBuyCnt += buyCnt;
}
for(let data of (dataObj.box||[])) {
this.box.push(new MiniGameBox(data));
}
}
public setPlayerData(playerData: ActivityMiniGameModelType) {
if(!playerData) return;
this.buyCnt = playerData.buyCnt||0;
this.score = playerData.score||0;
for(let box of this.box) {
box.setReceive(playerData.receivedBox||[]);
}
}
public setPlayerRecords(records: ActivityMiniGameRecModelType[]) {
let recByDay = new Map<number, ActivityMiniGameRecModelType[]>(); // day => recs
for(let record of records) {
if(record.todayIndex == this.todayIndex) {
this.todayPlayCnt ++;
this.playCnt++; // playCnt 当天玩的部分
}
if(!recByDay.has(record.todayIndex)) recByDay.set(record.todayIndex, []);
recByDay.get(record.todayIndex).push(record);
}
for(let { day, freeCnt } of this.buyCounts) {
if(this.todayIndex > day) {
let curRecords = recByDay.get(day)||[];
if(curRecords.length > freeCnt) this.playCnt += curRecords.length - freeCnt;// playCnt 前几天不免费的部分
}
}
}
public incPlayCnt() {
this.playCnt++;
this.todayPlayCnt++;
}
public findBox(boxId: number) {
return this.box.find(cur => cur.id == boxId);
}
public getShowResult() {
return {
...this.getBaseKeys(),
...pick(this, ['gameType', 'consume', 'reward', 'freeCnt', 'maxBuyCnt', 'box', 'playCnt', 'buyCnt', 'score', 'todayPlayCnt'])
}
}
}