装备:合成地玉石

This commit is contained in:
luying
2022-02-17 15:48:37 +08:00
parent ee32f1cdee
commit 17b437a618
5 changed files with 242 additions and 659 deletions
+69 -3
View File
@@ -33,6 +33,7 @@ import { updateEplaces } from './equipService';
export class CheckMeterial {
private roleId: string;
private itemsIndb: Map<number, number> = new Map(); // 玩家已有的东西
private notEnoughItems: Map<number, number> = new Map(); // 缺少的数量
private consumes: ItemInter[] = []; // 消耗,正向数
private goldId = CURRENCY_BY_TYPE.get(CURRENCY_TYPE.GOLD);
@@ -51,18 +52,36 @@ export class CheckMeterial {
}
}
private pushToNotEnoughItems(id: number, count: number) {
if(!this.notEnoughItems.has(id)) {
this.notEnoughItems.set(id, 0);
}
this.notEnoughItems.set(id, this.notEnoughItems.get(id) + count);
}
private getNotEnoughItems() {
let map = new Map<number, number>();
for(let [ id, count ] of this.notEnoughItems) {
map.set(id, count);
}
return map;
}
public async decrease(goods: {id: number, count: number}[]) {
this.notEnoughItems.clear();
let { items, gold, coin } = sortItems(goods, HANDLE_REWARD_TYPE.COST);
let isEnough = true;
for(let { id, count} of items) {
if(!this.itemsIndb.has(id)) {
let item = await ItemModel.findbyRoleAndGidAndCount(this.roleId, id, count);
let item = await ItemModel.findbyRoleAndGid(this.roleId, id);
if(!item) {
this.pushToNotEnoughItems(id, count);
isEnough = false; break;
}
this.itemsIndb.set(id, item.count);
}
if(this.itemsIndb.get(id) < count) {
this.pushToNotEnoughItems(id, count - this.itemsIndb.get(id));
isEnough = false; break;
}
this.itemsIndb.set(id, this.itemsIndb.get(id) - count);
@@ -75,7 +94,10 @@ export class CheckMeterial {
this.itemsIndb.set(this.coinId, role.coin);
}
let goldCost = gold.reduce((pre, cur) => { return pre + cur.count }, 0);
if(this.itemsIndb.get(this.goldId) < goldCost) isEnough = false;
if(this.itemsIndb.get(this.goldId) < goldCost) {
this.pushToNotEnoughItems(this.goldId, goldCost - this.itemsIndb.get(this.goldId));
isEnough = false;
}
}
if(isEnough && coin.length > 0) {
if(!this.itemsIndb.has(this.coinId)) {
@@ -84,13 +106,57 @@ export class CheckMeterial {
this.itemsIndb.set(this.coinId, role.coin);
}
let coinCost = coin.reduce((pre, cur) => pre + cur, 0);
if(this.itemsIndb.get(this.coinId) < coinCost) isEnough = false;
if(this.itemsIndb.get(this.coinId) < coinCost) {
this.pushToNotEnoughItems(this.coinId, coinCost - this.itemsIndb.get(this.coinId));
isEnough = false;
}
}
if(isEnough) this.consumes.push(...goods);
return isEnough;
}
private getMaterialEnough(materials: RewardInter[], notEnoughItems: Map<number, number>) {
let newMaterials: RewardInter[] = [];
for(let {id, count} of materials) {
if(notEnoughItems.has(id)) {
newMaterials.push({ id, count: count - notEnoughItems.get(id) });
} else {
newMaterials.push({ id, count });
}
}
return newMaterials;
}
// 检查地玉石是否可以合成
public async composeStone(id: number, count: number) {
let dicStone = gameData.stone.get(id);
if(!dicStone || dicStone.composeMaterial.length <= 0) return false; // 1阶石头不能再从下合成返回不行
let materials = dicStone.composeMaterial.map(cur => ({...cur, count: cur.count * count }));
console.log('#######materials', materials)
let isEnough = await this.decrease(materials);
console.log('#####isEnough', isEnough, this.notEnoughItems)
if(!isEnough) {
let notEnoughItems = this.getNotEnoughItems();
let newMaterials = this.getMaterialEnough(materials, notEnoughItems);
console.log('#######newMaterials', newMaterials, this.notEnoughItems)
let isEnough = await this.decrease(newMaterials); // 消耗掉除了不足的部分以外的其他部分
if(!isEnough) return false;
let isAllOK = true; // 如果有石头不足,向下补充,isAllOK标识不足的都能补充上
for(let [id, count] of notEnoughItems) {
let isEnough = await this.composeStone(id, count);
console.log('####')
if(!isEnough) {
isAllOK = false; break;
}
}
return isAllOK;
} else {
return true;
}
}
public getConsume() {
return this.consumes;
}