✨ feat(通用丹): 增加通用丹功能
https://bantugame.feishu.cn/wiki/wikcnlWkklK1jkQZRHgbQ0nrVNc
This commit is contained in:
@@ -326,12 +326,12 @@ export class HeroHandler {
|
||||
}
|
||||
|
||||
//训练
|
||||
async heroJobTrain(msg: { hid: number, isOneClick: boolean }, session: BackendSession) {
|
||||
async heroJobTrain(msg: { hid: number, isOneClick: boolean, canReplace: boolean }, session: BackendSession) {
|
||||
let roleId: string = session.get('roleId');
|
||||
let sid: string = session.get('sid');
|
||||
|
||||
const serverId = session.get('serverId');
|
||||
let { hid, isOneClick } = msg;
|
||||
let { hid, isOneClick, canReplace = false } = msg;
|
||||
let hero = await HeroModel.findByHidAndRole(hid, roleId);
|
||||
if (!hero) return resResult(STATUS.HERO_NOT_FIND);
|
||||
|
||||
@@ -348,6 +348,7 @@ export class HeroHandler {
|
||||
let max = isOneClick ? dicJob.maxStage: hero.jobStage + 1;
|
||||
let trainCount = 0;
|
||||
let check = new CheckMeterial(roleId);
|
||||
check.setCanReplace(canReplace);
|
||||
for(let i = hero.jobStage; i < max; i++) {
|
||||
let singleConsume = dicJob.trainingConsume[i];
|
||||
if(!singleConsume) break; // 每天消耗不可训练
|
||||
@@ -359,6 +360,7 @@ export class HeroHandler {
|
||||
}
|
||||
if (newJobStage == hero.jobStage) return resResult(STATUS.ROLE_MATERIAL_NOT_ENOUGH);
|
||||
let consumes = check.getConsume();
|
||||
console.log('#### consumes', consumes);
|
||||
let result = await handleCost(roleId, sid, consumes, ITEM_CHANGE_REASON.HERO_JOB_TRAIN);
|
||||
if (!result)
|
||||
return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH);
|
||||
|
||||
@@ -1390,9 +1390,9 @@ export function checkRouteParam(route: string, msg: any) {
|
||||
}
|
||||
case "role.heroHandler.heroJobTrain":
|
||||
{
|
||||
let { hid, isOneClick } = msg;
|
||||
let { hid, isOneClick, canReplace } = msg;
|
||||
if(!checkNaturalNumbers(hid)) return false;
|
||||
if(!checkBoolean(isOneClick)) return false;
|
||||
if(!checkBoolean(isOneClick, canReplace)) return false;
|
||||
break;
|
||||
}
|
||||
case "role.heroHandler.heroJobStageUp":
|
||||
|
||||
@@ -10,6 +10,7 @@ export class CheckMeterial {
|
||||
private itemsIndb: Map<number, number> = new Map(); // 玩家已有的东西
|
||||
private notEnoughItems: Map<number, number> = new Map(); // 缺少的数量
|
||||
private consumes: ItemInter[] = []; // 消耗,正向数
|
||||
private canReplace: boolean = false; // 是否可以被通用的啥代替代替
|
||||
|
||||
private goldId = CURRENCY_BY_TYPE.get(CURRENCY_TYPE.GOLD);
|
||||
private coinId = CURRENCY_BY_TYPE.get(CURRENCY_TYPE.COIN);
|
||||
@@ -45,52 +46,103 @@ export class CheckMeterial {
|
||||
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.findbyRoleAndGid(this.roleId, id);
|
||||
if(!item) {
|
||||
this.pushToNotEnoughItems(id, count);
|
||||
isEnough = false; break;
|
||||
}
|
||||
this.itemsIndb.set(id, item.count);
|
||||
console.log('#####', id, count)
|
||||
let notEnoughCount = await this.decreaseItem(id, count);
|
||||
console.log('##### notEnoughCount', id, notEnoughCount, this.canReplace)
|
||||
if(notEnoughCount > 0) {
|
||||
let isEnough = await this.checkReplaceItem(id, notEnoughCount);
|
||||
if(!isEnough) return false;
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
if(gold.length > 0) {
|
||||
if(!this.itemsIndb.has(this.goldId)) {
|
||||
let role = await RoleModel.findByRoleId(this.roleId, 'gold coin');
|
||||
this.itemsIndb.set(this.goldId, role.gold);
|
||||
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) {
|
||||
this.pushToNotEnoughItems(this.goldId, goldCost - this.itemsIndb.get(this.goldId));
|
||||
isEnough = false;
|
||||
}
|
||||
this.itemsIndb.set(this.goldId, this.itemsIndb.get(this.goldId) - goldCost);
|
||||
let isEnough = await this.decreaseGold(gold);
|
||||
if(!isEnough) return false;
|
||||
}
|
||||
if(isEnough && coin.length > 0) {
|
||||
if(!this.itemsIndb.has(this.coinId)) {
|
||||
let role = await RoleModel.findByRoleId(this.roleId, 'gold coin');
|
||||
this.itemsIndb.set(this.goldId, role.gold);
|
||||
this.itemsIndb.set(this.coinId, role.coin);
|
||||
}
|
||||
let coinCost = coin.reduce((pre, cur) => pre + cur, 0);
|
||||
if(this.itemsIndb.get(this.coinId) < coinCost) {
|
||||
this.pushToNotEnoughItems(this.coinId, coinCost - this.itemsIndb.get(this.coinId));
|
||||
isEnough = false;
|
||||
}
|
||||
this.itemsIndb.set(this.coinId, this.itemsIndb.get(this.coinId) - coinCost);
|
||||
if(coin.length > 0) {
|
||||
let isEnough = await this.decreaseCoin(coin);
|
||||
if(!isEnough) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if(isEnough) this.consumes.push(...goods);
|
||||
return isEnough;
|
||||
public setCanReplace(canReplace: boolean) {
|
||||
this.canReplace = canReplace;
|
||||
}
|
||||
|
||||
private async decreaseItem(id: number, count: number) {
|
||||
console.log('#### decreaseItem 1', id, count);
|
||||
if(!this.itemsIndb.has(id)) {
|
||||
let item = await ItemModel.findbyRoleAndGid(this.roleId, id);
|
||||
console.log('#### decreaseItem', item);
|
||||
if(!item) {
|
||||
this.pushToNotEnoughItems(id, count);
|
||||
return count;
|
||||
}
|
||||
this.itemsIndb.set(id, item.count);
|
||||
}
|
||||
if(this.itemsIndb.get(id) < count) {
|
||||
this.pushToNotEnoughItems(id, count - this.itemsIndb.get(id));
|
||||
|
||||
this.consumes.push({ id, count: this.itemsIndb.get(id) });
|
||||
return count - this.itemsIndb.get(id);
|
||||
}
|
||||
this.itemsIndb.set(id, this.itemsIndb.get(id) - count);
|
||||
this.consumes.push({ id, count });
|
||||
return 0
|
||||
}
|
||||
|
||||
private async checkReplaceItem(id: number, count: number) {
|
||||
if(!this.canReplace) return false;
|
||||
|
||||
let relationGoodId = gameData.relationGoods.get(id);
|
||||
if(!relationGoodId) return false;
|
||||
|
||||
if(relationGoodId == this.goldId) {
|
||||
return await this.decreaseGold([{ count }]);
|
||||
} else if (relationGoodId == this.coinId) {
|
||||
return await this.decreaseCoin([count]);
|
||||
} else {
|
||||
let cnt = await this.decreaseItem(relationGoodId, count);
|
||||
return cnt == 0
|
||||
}
|
||||
}
|
||||
|
||||
private async decreaseGold(gold: { count: number }[]) {
|
||||
|
||||
if(!this.itemsIndb.has(this.goldId)) {
|
||||
let role = await RoleModel.findByRoleId(this.roleId, 'gold coin');
|
||||
this.itemsIndb.set(this.goldId, role.gold);
|
||||
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) {
|
||||
this.pushToNotEnoughItems(this.goldId, goldCost - this.itemsIndb.get(this.goldId));
|
||||
this.consumes.push({ id: this.goldId, count: this.itemsIndb.get(this.goldId) });
|
||||
return false;
|
||||
}
|
||||
this.itemsIndb.set(this.goldId, this.itemsIndb.get(this.goldId) - goldCost);
|
||||
this.consumes.push({ id: this.goldId, count: goldCost });
|
||||
return true
|
||||
}
|
||||
|
||||
private async decreaseCoin(coin: number[]) {
|
||||
|
||||
if(!this.itemsIndb.has(this.coinId)) {
|
||||
let role = await RoleModel.findByRoleId(this.roleId, 'gold coin');
|
||||
this.itemsIndb.set(this.goldId, role.gold);
|
||||
this.itemsIndb.set(this.coinId, role.coin);
|
||||
}
|
||||
let coinCost = coin.reduce((pre, cur) => pre + cur, 0);
|
||||
if(this.itemsIndb.get(this.coinId) < coinCost) {
|
||||
this.pushToNotEnoughItems(this.coinId, coinCost - this.itemsIndb.get(this.coinId));
|
||||
this.consumes.push({ id: this.coinId, count: this.itemsIndb.get(this.coinId) });
|
||||
return false;
|
||||
}
|
||||
this.itemsIndb.set(this.coinId, this.itemsIndb.get(this.coinId) - coinCost);
|
||||
this.consumes.push({ id: this.coinId, count: coinCost });
|
||||
return true;
|
||||
}
|
||||
|
||||
private getMaterialEnough(materials: RewardInter[], notEnoughItems: Map<number, number>) {
|
||||
|
||||
Reference in New Issue
Block a user