diff --git a/game-server/app/servers/role/handler/heroHandler.ts b/game-server/app/servers/role/handler/heroHandler.ts index 1fb161123..ec2116ca0 100644 --- a/game-server/app/servers/role/handler/heroHandler.ts +++ b/game-server/app/servers/role/handler/heroHandler.ts @@ -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); diff --git a/game-server/app/services/checkParam.ts b/game-server/app/services/checkParam.ts index 89dad6346..9a9edd7e5 100644 --- a/game-server/app/services/checkParam.ts +++ b/game-server/app/services/checkParam.ts @@ -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": diff --git a/game-server/app/services/role/checkMaterial.ts b/game-server/app/services/role/checkMaterial.ts index 006c9201f..ad62481d3 100644 --- a/game-server/app/services/role/checkMaterial.ts +++ b/game-server/app/services/role/checkMaterial.ts @@ -10,6 +10,7 @@ export class CheckMeterial { private itemsIndb: Map = new Map(); // 玩家已有的东西 private notEnoughItems: Map = 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) { diff --git a/shared/consts/constModules/sysConst.ts b/shared/consts/constModules/sysConst.ts index f3f478297..b5f6a18bf 100644 --- a/shared/consts/constModules/sysConst.ts +++ b/shared/consts/constModules/sysConst.ts @@ -556,6 +556,7 @@ export const FILENAME = { DIC_GK_LADDER: 'dic_zyz_gk_ladder', DIC_LADDER_MATCH: 'dic_zyz_ladderMatch', DIC_GK_BRANCH_ELITE: 'dic_zyz_gk_branchElite', + DIC_GENERAL_GOODS: 'dic_zyz_general_goods', } export const WAR_RELATE_TABLES = [ diff --git a/shared/pubUtils/data.ts b/shared/pubUtils/data.ts index c5c2846a9..3229eaa0e 100644 --- a/shared/pubUtils/data.ts +++ b/shared/pubUtils/data.ts @@ -110,6 +110,7 @@ import { dicMainWarRewardByChapter, loadMainWarReward } from './dictionary/DicMa import { dicLadderMatch, loadLadderMatch } from "./dictionary/DicLadderMatch"; import { dicLadderDifficultRatio, loadLadderDifficultRatio } from "./dictionary/DicLadderDifficultRatio"; import { dicLadderRankReward, loadLadderRankReward } from "./dictionary/DicLadderRankReward"; +import { dicGeneralGoods, loadGeneralGoods } from "./dictionary/DicGeneralGoods"; export const gameData = { daily: dicDaily, @@ -276,6 +277,7 @@ export const gameData = { ladderRankReward: dicLadderRankReward, comBattleRewardTime: new Array<{from: string, to: string}>(), comBattleReward: dicComBattleReward, + relationGoods: dicGeneralGoods, }; // 在此提供一些原先在gamedata中提供的方法,以便更方便获取gameData数据 @@ -1219,6 +1221,7 @@ function loadDatas() { loadLadderMatch(); loadLadderDifficultRatio(); loadLadderRankReward(); + loadGeneralGoods(); } // 重载dicParam diff --git a/shared/pubUtils/dictionary/DicGeneralGoods.ts b/shared/pubUtils/dictionary/DicGeneralGoods.ts new file mode 100644 index 000000000..975db3f7b --- /dev/null +++ b/shared/pubUtils/dictionary/DicGeneralGoods.ts @@ -0,0 +1,14 @@ +// 礼包奖励表 +import { readFileAndParse } from '../util'; +import { FILENAME } from '../../consts'; + +export const dicGeneralGoods = new Map(); +export function loadGeneralGoods() { + dicGeneralGoods.clear(); + let arr = readFileAndParse(FILENAME.DIC_GENERAL_GOODS); + + arr.forEach(o => { + dicGeneralGoods.set(o.goodId, o.relationGoodId); + }); + arr = undefined; +} \ No newline at end of file diff --git a/shared/resource/jsons/dic_zyz_general_goods.json b/shared/resource/jsons/dic_zyz_general_goods.json new file mode 100644 index 000000000..4685f0c1e --- /dev/null +++ b/shared/resource/jsons/dic_zyz_general_goods.json @@ -0,0 +1,282 @@ +[ + { + "id": 1, + "goodId": 17002, + "name": "一品白虎丹", + "relationGoodId": 17059, + "relationName": "奉魁丹" + }, + { + "id": 2, + "goodId": 17003, + "name": "一品玄武丹", + "relationGoodId": 17059, + "relationName": "奉魁丹" + }, + { + "id": 3, + "goodId": 17004, + "name": "一品青龙丹", + "relationGoodId": 17059, + "relationName": "奉魁丹" + }, + { + "id": 4, + "goodId": 17005, + "name": "一品朱雀丹", + "relationGoodId": 17059, + "relationName": "奉魁丹" + }, + { + "id": 5, + "goodId": 17006, + "name": "一品麒麟丹", + "relationGoodId": 17059, + "relationName": "奉魁丹" + }, + { + "id": 6, + "goodId": 17007, + "name": "二品白虎丹", + "relationGoodId": 17059, + "relationName": "奉魁丹" + }, + { + "id": 7, + "goodId": 17008, + "name": "二品玄武丹", + "relationGoodId": 17059, + "relationName": "奉魁丹" + }, + { + "id": 8, + "goodId": 17009, + "name": "二品青龙丹", + "relationGoodId": 17059, + "relationName": "奉魁丹" + }, + { + "id": 9, + "goodId": 17010, + "name": "二品朱雀丹", + "relationGoodId": 17059, + "relationName": "奉魁丹" + }, + { + "id": 10, + "goodId": 17011, + "name": "二品麒麟丹", + "relationGoodId": 17059, + "relationName": "奉魁丹" + }, + { + "id": 11, + "goodId": 17012, + "name": "三品白虎丹", + "relationGoodId": 17059, + "relationName": "奉魁丹" + }, + { + "id": 12, + "goodId": 17013, + "name": "三品玄武丹", + "relationGoodId": 17059, + "relationName": "奉魁丹" + }, + { + "id": 13, + "goodId": 17014, + "name": "三品青龙丹", + "relationGoodId": 17059, + "relationName": "奉魁丹" + }, + { + "id": 14, + "goodId": 17015, + "name": "三品朱雀丹", + "relationGoodId": 17059, + "relationName": "奉魁丹" + }, + { + "id": 15, + "goodId": 17016, + "name": "三品麒麟丹", + "relationGoodId": 17059, + "relationName": "奉魁丹" + }, + { + "id": 16, + "goodId": 17017, + "name": "四品白虎丹", + "relationGoodId": 17059, + "relationName": "奉魁丹" + }, + { + "id": 17, + "goodId": 17018, + "name": "四品玄武丹", + "relationGoodId": 17059, + "relationName": "奉魁丹" + }, + { + "id": 18, + "goodId": 17019, + "name": "四品青龙丹", + "relationGoodId": 17059, + "relationName": "奉魁丹" + }, + { + "id": 19, + "goodId": 17020, + "name": "四品朱雀丹", + "relationGoodId": 17059, + "relationName": "奉魁丹" + }, + { + "id": 20, + "goodId": 17021, + "name": "四品麒麟丹", + "relationGoodId": 17059, + "relationName": "奉魁丹" + }, + { + "id": 21, + "goodId": 17022, + "name": "五品白虎丹", + "relationGoodId": 17060, + "relationName": "承杓丹" + }, + { + "id": 22, + "goodId": 17023, + "name": "五品玄武丹", + "relationGoodId": 17060, + "relationName": "承杓丹" + }, + { + "id": 23, + "goodId": 17024, + "name": "五品青龙丹", + "relationGoodId": 17060, + "relationName": "承杓丹" + }, + { + "id": 24, + "goodId": 17025, + "name": "五品朱雀丹", + "relationGoodId": 17060, + "relationName": "承杓丹" + }, + { + "id": 25, + "goodId": 17026, + "name": "五品麒麟丹", + "relationGoodId": 17060, + "relationName": "承杓丹" + }, + { + "id": 26, + "goodId": 17027, + "name": "六品白虎丹", + "relationGoodId": 17060, + "relationName": "承杓丹" + }, + { + "id": 27, + "goodId": 17028, + "name": "六品玄武丹", + "relationGoodId": 17060, + "relationName": "承杓丹" + }, + { + "id": 28, + "goodId": 17029, + "name": "六品青龙丹", + "relationGoodId": 17060, + "relationName": "承杓丹" + }, + { + "id": 29, + "goodId": 17030, + "name": "六品朱雀丹", + "relationGoodId": 17060, + "relationName": "承杓丹" + }, + { + "id": 30, + "goodId": 17031, + "name": "六品麒麟丹", + "relationGoodId": 17060, + "relationName": "承杓丹" + }, + { + "id": 31, + "goodId": 17032, + "name": "七品白虎丹", + "relationGoodId": 17060, + "relationName": "承杓丹" + }, + { + "id": 32, + "goodId": 17033, + "name": "七品玄武丹", + "relationGoodId": 17060, + "relationName": "承杓丹" + }, + { + "id": 33, + "goodId": 17034, + "name": "七品青龙丹", + "relationGoodId": 17060, + "relationName": "承杓丹" + }, + { + "id": 34, + "goodId": 17035, + "name": "七品朱雀丹", + "relationGoodId": 17060, + "relationName": "承杓丹" + }, + { + "id": 35, + "goodId": 17036, + "name": "七品麒麟丹", + "relationGoodId": 17060, + "relationName": "承杓丹" + }, + { + "id": 36, + "goodId": 17037, + "name": "八品白虎丹", + "relationGoodId": 17060, + "relationName": "承杓丹" + }, + { + "id": 37, + "goodId": 17038, + "name": "八品玄武丹", + "relationGoodId": 17060, + "relationName": "承杓丹" + }, + { + "id": 38, + "goodId": 17039, + "name": "八品青龙丹", + "relationGoodId": 17060, + "relationName": "承杓丹" + }, + { + "id": 39, + "goodId": 17040, + "name": "八品朱雀丹", + "relationGoodId": 17060, + "relationName": "承杓丹" + }, + { + "id": 40, + "goodId": 17041, + "name": "八品麒麟丹", + "relationGoodId": 17060, + "relationName": "承杓丹" + } +] \ No newline at end of file