Files
ZYZ/shared/domain/activityField/forgeField.ts
2023-03-17 16:48:32 +08:00

134 lines
4.7 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 { ActivityForgeModelType } from '../../db/ActivityForge';
import { parseGoodStr } from '../../pubUtils/util';
import { ActivityBase } from './activityField';
// 后台格式
interface ForgeManualInDb {
id: number; // 图谱id
name: string; // 图谱名
imageName: string; // 图片
dayIndex: number; // 第几天解锁
material: string; // 配方比例id&count
totalMaterialCnt: number; // 最大材料数量
quality: number; // 品质
reward: string; // 铸造成功可获得的奖励id&count
freeCnt: number; // 可免费铸造次数(总)
maxBuyCnt: number; // 最大可购买次数(总)
}
interface ForgeHintInDb {
failCnt: number;
hintType: number; // 1-提示总额 2-提示材料多了还是少了 3-材料具体配比提示
}
interface ForgeDataInDb {
manuals: ForgeManualInDb[]; // 图谱
consume: string; // 购买铸造次数消耗的元宝
hint: ForgeHintInDb[]; // 失败提示法
}
// 商品数据
export class ForgeManual {
id: number; // 图谱id
name: string; // 图谱名
imageName: string; // 图片
dayIndex: number; // 第几天解锁
material: string; // id&count正确的配比
reward: string; // 铸造成功可获得的奖励id&count
freeCnt: number; // 可免费铸造次数
maxBuyCnt: number; // 今天可以购买的次数
totalMaterialCnt: number; // 最大材料数量
quality: number; // 品质
buildCnt: number = 0; // 已经铸造了的次数 buildCnt < freeCnt + buyCnt
buyCnt: number = 0; // 今天已购买次数
failCnt: number = 0; // 猜错的次数
hintType: number = 0; // 应该给的提示类型 1-总数提示 2-材料多了少了的提示 3-具体配比提示
constructor(data: ForgeManualInDb) {
this.id = data.id;
this.name = data.name;
this.imageName = data.imageName;
this.dayIndex = data.dayIndex;
this.material = data.material;
this.reward = data.reward;
this.freeCnt = data.freeCnt;
this.maxBuyCnt = data.maxBuyCnt;
this.totalMaterialCnt = data.totalMaterialCnt;
this.quality = data.quality;
}
public setPlayerData(playerData: ActivityForgeModelType, todayIndex: number) {
this.buildCnt = playerData.buildCnt||0;
this.buyCnt = playerData.buyCnt||0;
let todayFailRecord = playerData.record?.filter(cur => cur.todayIndex == todayIndex && cur.isSuccess == false)||[];
this.failCnt = todayFailRecord.length;
}
public calHintType(hintDic: ForgeHintInDb[]) {
for(let { failCnt, hintType } of hintDic) {
if(this.failCnt < failCnt) {
this.hintType = hintType; break;
}
}
}
public checkMaterial(playerMaterial: { id: number, count: number }[]) {
let configMaterial = parseGoodStr(this.material);
for(let { id, count } of configMaterial) {
let playerCount = playerMaterial.find(cur => cur.id == id)?.count||0;
if(playerCount != count) return false;
}
for(let { id, count } of playerMaterial) {
let configCount = configMaterial.find(cur => cur.id == id)?.count||0;
if(configCount != count) return false;
}
return true;
}
}
export class ForgeData extends ActivityBase {
manuals: ForgeManual[] = []; // 图谱
consume: string; // 购买铸造次数的消耗
hint: ForgeHintInDb[];
constructor(activityData: ActivityModelType, createTime: number, serverTime: number) {
super(activityData, createTime, serverTime)
this.initData(activityData.data)
}
public initData(data: string): void {
let dataObj: ForgeDataInDb = JSON.parse(data);
if(!dataObj) return;
this.hint = dataObj.hint||[];
this.consume = dataObj.consume||'';
for(let data of (dataObj.manuals||[])) {
this.manuals.push(new ForgeManual(data));
}
}
public setPlayerRecords(playerData: ActivityForgeModelType[]) {
for(let data of playerData) {
let manual = this.manuals.find(cur => cur.id == data.manualId);
if(manual) manual.setPlayerData(data, this.todayIndex);
manual.calHintType(this.hint);
}
}
public findManual(id: number) {
return this.manuals.find(cur => cur.id == id);
}
public getShowResult() {
return {
...this.getBaseKeys(),
manuals: this.manuals,
consume: this.consume
}
}
}