diff --git a/game-server/app/servers/role/handler/equipHandler.ts b/game-server/app/servers/role/handler/equipHandler.ts index c824ad98d..88867431a 100644 --- a/game-server/app/servers/role/handler/equipHandler.ts +++ b/game-server/app/servers/role/handler/equipHandler.ts @@ -5,18 +5,19 @@ import { ItemInter } from "../../../pubUtils/interface"; import { resResult, parseGoodStr } from "../../../pubUtils/util"; import { addItems, handleCost, decreaseItems, combineItems } from "../../../services/rewardService"; import { EquipModel, EquipType } from "../../../db/Equip"; -import { HeroModel, EPlace } from "../../../db/Hero"; +import { HeroModel, EPlace, HeroType } from "../../../db/Hero"; import Role from "../../../db/Role"; import { calPlayerCeAndSave } from "../../../services/playerCeService"; import { getGoodById, gameData } from "../../../pubUtils/data"; import { BAG, EQUIP } from "../../../pubUtils/dicParam"; import { ITID, QUALITY_TYPE, equipTypeToSortAttr, IT_TYPE, QUENCH_TYPE, REFINE_TYPE } from "../../../consts"; -import { changeEquip, dressEquip, checkMaterialEnough, takeOffEquipAndCalPlayerCe, checkEquipCanPut, quenchOnce, checkQuenchMaxByQualityAndGrade, getRandSeResult, refineOnce, checkRefineReachNextLv, calEquipCe } from "../../../services/equipService"; +import { checkMaterialEnough, checkEquipCanPut, quenchOnce, checkQuenchMaxByQualityAndGrade, getRandSeResult, refineOnce, checkRefineReachNextLv, calEquipCe } from "../../../services/equipService"; import { findIndex, pick } from 'underscore'; import { pushEquipRefineSucMsg, pushNormalEquipMsg, pushNormalItemMsg } from "../../../services/chatService"; import { checkTaskWithHero, checkTaskWithEquip, checkTask, checkTaskWithArgs, checkTaskConditionEquipSuitJewelStage, checkActivityTask } from "../../../services/taskService"; import { QuenchLogParam } from "../../../domain/roleField/equip"; +import { calEquipSeids } from "../../../pubUtils/playerCe"; export default function (app: Application) { new HandlerService(app, {}); @@ -593,47 +594,107 @@ export class EquipHandler { public async putOnOrOff(msg: { eid: number, hid: number, type: number }, session: BackendSession) { let { eid, hid, type } = msg; let roleId: string = session.get('roleId'); - const serverId = session.get('serverId'); - let equip = await EquipModel.getEquip(eid); + let serverId: number = session.get('serverId'); let sid: string = session.get('sid'); let funcs: number[] = session.get('funcs'); + + // 原武将:heroA,原武将穿戴着的装备a,将要穿戴的装备b,装备b被装备着的武将B + let heroA = await HeroModel.findByHidAndRoleWithEquip(hid, roleId); + if (!heroA) return resResult(STATUS.HERO_NOT_FIND); + + let equip = await EquipModel.getEquip(eid); // 装备:1-equipB 2-equipA + if(!equip) return resResult(STATUS.EQUIP_NOT_FIND); + + let argsA = calEquipSeids(heroA); + let heroB: HeroType, equipA: EquipType, equipB: EquipType, argsB: number[] = []; + let heroAPutNum = 0, heroBPutTNum = 0; // 1:多一件 0:没变化 -1:少一件 + let goodInfo = getGoodById(equip.id); let obj = ITID.get(goodInfo.itid); - let id = obj.type; - let curEquips: Array<{ seqId: number, hid: number }> = []; - let hero = await HeroModel.findByHidAndRoleWithEquip(hid, roleId); - if (!hero) - return resResult(STATUS.HERO_NOT_FIND); - if (type == 1) { - if (equip.hid == hid) - return resResult(STATUS.WRONG_PARMS); - if (goodInfo.lvLimited > hero.lv) - return resResult(STATUS.EQUIP_LEVEL_LIMIT); - if(!checkEquipCanPut(hid, equip.id)) - return resResult(STATUS.EQUIP_NOT_EQUIPED_HERO); - let index = findIndex(hero.ePlace, { id }); - if (index < 0) - return resResult(STATUS.WRONG_PARMS); - let equipOffInfo = hero.ePlace[index].equip; - console.log('**********equipOffInfo', equipOffInfo&&equipOffInfo.seqId) - let curEquip = await changeEquip(serverId, roleId, sid, equipOffInfo, equip.hid, id, equip, funcs); - if (!!curEquip) - curEquips.push(...curEquip); - curEquip = await dressEquip(serverId, roleId, sid, hero, equip, funcs); - if (!!curEquip) - curEquips.push(...curEquip); + let eplaceId = obj.type; // 位置 - //任务 - //英雄满装备且都镶嵌相同阶数的宝石 - let { isTask, jewelLevel } = await checkTaskConditionEquipSuitJewelStage(hero) + if (type == 1) { + equipB = equip; // 要穿上的装备 + if (equipB.hid == hid) return resResult(STATUS.WRONG_PARMS); + if(!checkEquipCanPut(heroA, equipB.id)) return resResult(STATUS.EQUIP_NOT_EQUIPED_HERO); + + let index = heroA.ePlace.findIndex(cur => cur.id == eplaceId); + if (index == -1) return resResult(STATUS.WRONG_PARMS); + equipA = heroA.ePlace[index].equip; // 要脱下的装备 + + if(equipB.hid > 0) { + heroB = await HeroModel.findByHidAndRoleWithEquip(equipB.hid, roleId); + argsB = calEquipSeids(heroB); + } + + if(equipA) { + if(equipB.hid > 0) { + if(checkEquipCanPut(heroB, equipA.id)) { // A=>b & B=>a + equipA = await EquipModel.putOnOrOff(equipA._id, equipB.hid); + heroB = await HeroModel.addEquip(roleId, heroB.hid, eplaceId, equipA._id); + } else { // A=>b & B=>0 & 0=>a + equipA = await EquipModel.putOnOrOff(equipA._id, 0); + heroB = await HeroModel.removeEquip(roleId, equipB.hid, eplaceId); + heroBPutTNum = -1; + } + } else { // A=>b & 0=>a + equipA = await EquipModel.putOnOrOff(equipA._id, 0); + } + } else { + if(equipB.hid > 0) { // A=>b & B=>0 + heroB = await HeroModel.removeEquip(roleId, equipB.hid, eplaceId); + heroBPutTNum = -1; + } + // A=>b + heroAPutNum = 1; + } + + heroA = await HeroModel.addEquip(roleId, heroA.hid, eplaceId, equipB._id); + equipB = await EquipModel.putOnOrOff(equipB._id, heroA.hid); + + } else if (type == 2) { // A=>0 & 0=>a + if (!equip.hid) return resResult(STATUS.EQUIP_NOT_EQUIPED); + equipA = await EquipModel.putOnOrOff(equip._id, 0); + heroA = await HeroModel.removeEquip(roleId, heroA.hid, eplaceId); + heroAPutNum = -1; + } + + if(heroA) { + await calPlayerCeAndSave(HERO_SYSTEM_TYPE.EQUIP, sid, roleId, heroA, {}, argsA); + if(heroAPutNum != 0) { + await checkTaskWithHero(roleId, sid, funcs, TASK_TYPE.EQUIP_BY_HERO, heroA, [heroAPutNum]); + } + } + if(heroB) { + await calPlayerCeAndSave(HERO_SYSTEM_TYPE.EQUIP, sid, roleId, heroB, {}, argsB); + if(heroBPutTNum != 0) await checkTaskWithHero(roleId, sid, funcs, TASK_TYPE.EQUIP_BY_HERO, heroA, [heroBPutTNum]); + } + if(heroAPutNum + heroBPutTNum != 0) { + await checkTask(roleId, sid, funcs, TASK_TYPE.EQUIP_SUM, heroAPutNum + heroBPutTNum, true, {}); + await checkActivityTask(serverId, sid, funcs, roleId, TASK_TYPE.EQUIP_SUM, heroAPutNum + heroBPutTNum) + } + + let curEquips: Array<{ seqId: number, hid: number }> = []; + if(equipA) { + curEquips.push(pick(equipA, ['seqId', 'hid'])); + if(equipA.hid == 0) { + await checkTaskWithEquip(roleId, sid, funcs, TASK_TYPE.EQUIP_QUALITY, equipA, [-1]); // equipA必然是脱,如果hid不为0说明从一个人穿到另一个人,不算任务 + await checkActivityTask(serverId, sid, funcs, roleId, TASK_TYPE.EQUIP_QUALITY, 1, { quality: equipA.quality }); + } + } + if(equipB) { + curEquips.push(pick(equipB, ['seqId', 'hid'])); + if(equipB.hid != 0) { + await checkTaskWithEquip(roleId, sid, funcs, TASK_TYPE.EQUIP_QUALITY, equipB, [1]); // equipB必然是穿,如果hid为0说明没有变化,不算任务 + await checkActivityTask(serverId, sid, funcs, roleId, TASK_TYPE.EQUIP_QUALITY, 1, { quality: equipB.quality }); + } + } + + if(type == 1) { + let { isTask, jewelLevel } = await checkTaskConditionEquipSuitJewelStage(heroA)//英雄满装备且都镶嵌相同阶数的宝石 if (isTask) { await checkActivityTask(serverId, sid, funcs, roleId, TASK_TYPE.EQUIP_SUIT_JEWEL_STAGE, 1, { stage: jewelLevel }) } - } else if (type == 2) { - if (!equip.hid) - return resResult(STATUS.EQUIP_NOT_EQUIPED); - let curEquip = await takeOffEquipAndCalPlayerCe(roleId, sid, hero, equip, id, funcs); - curEquips.push(...curEquip); } return resResult(STATUS.SUCCESS, { curEquips: curEquips }); @@ -648,6 +709,8 @@ export class EquipHandler { let funcs: number[] = session.get('funcs'); let hero = await HeroModel.findByHidAndRoleWithEquip(hid, roleId); if (!hero) return resResult(STATUS.HERO_NOT_FIND); + let args = calEquipSeids(hero); + let dicHero = gameData.hero.get(hid); if (!dicHero) return resResult(STATUS.DIC_DATA_NOT_FOUND); let dicJob = gameData.job.get(dicHero.jobid); @@ -668,8 +731,8 @@ export class EquipHandler { let sortAttrId = equipTypeToSortAttr.get(curEPlace.id); let sortedAndSuitEquips = sortEquips.filter(equipInfos => { // 筛选可穿的 let { id, lvLimited, ePlaceId } = equipInfos; - return lv >= lvLimited && checkEquipCanPut(hid, id) && curEPlace.id == ePlaceId; - }) + return lv >= lvLimited && checkEquipCanPut(hero, id) && curEPlace.id == ePlaceId; + }); console.log('sortedAndSuitEquips', JSON.stringify(sortedAndSuitEquips, null, 4)) sortedAndSuitEquips = sortedAndSuitEquips.sort((a, b) => { // 排序按:战力 => 根据位置看个别属性 => 品质 if (b.ce - a.ce != 0) return b.ce - a.ce; @@ -681,8 +744,9 @@ export class EquipHandler { if (sortedAndSuitEquips.length > 0) { let { equip } = sortedAndSuitEquips.shift(); - let curEquip = await dressEquip(serverId, roleId, sid, hero, equip, funcs); - if (!!curEquip) curEquips.push(...curEquip); + hero = await HeroModel.addEquip(roleId, hero.hid, equip.ePlaceId, equip._id); + equip = await EquipModel.putOnOrOff(equip._id, hero.hid); + if (!!equip) curEquips.push(pick(equip, ['seqId', 'hid'])); } } @@ -694,6 +758,8 @@ export class EquipHandler { if (isTask) { await checkActivityTask(serverId, sid, funcs, roleId, TASK_TYPE.EQUIP_SUIT_JEWEL_STAGE, 1, { stage: jewelLevel }) } + + await calPlayerCeAndSave(HERO_SYSTEM_TYPE.EQUIP, sid, roleId, hero, {}, args); } return resResult(STATUS.SUCCESS, { curEquips }); diff --git a/game-server/app/services/equipService.ts b/game-server/app/services/equipService.ts index c834bda7d..43c94d93f 100644 --- a/game-server/app/services/equipService.ts +++ b/game-server/app/services/equipService.ts @@ -66,99 +66,11 @@ export function checkMaterialEnough(consumes: Array<{ id: number, count: number let isEnough = checkCurMeterial(jewel, jewelCount); return isEnough? needConsumes: false; } -/** - * 将装备卸载下来,并检查是否有替换装备的武将,若有则替换 - * 武将A(装备A)&武将B(装备B) ==> 武将A(装备B)&武将B(装备A) - * 武将A(装备A)& 装备B ==> 武将A(装备B)& 装备A - * @param roleId 玩家id - * @param sid - * @param equip 目标武将要卸下的装备,装备A - * @param hid 要装上的装备备原来装备着的武将id, 武将B - * @param id 装备类型 - * @param seqId 要装上的装备的唯一id 装备B - */ -export async function changeEquip(serverId: number, roleId: string, sid: string, equipA: EquipType, hid: number, id: number, equipB: EquipType, funcs: number[]) { - let heroB: HeroType; - if (!!hid) //需要卸下或者替换的武将 - heroB = await HeroModel.findByHidAndRoleWithEquip(hid, roleId);//需要替换的武将 - if (!!equipA) {//需要卸下的装备 - if (!!heroB) { - let goodInfo = getGoodById(equipA.id); - if (goodInfo.lvLimited > heroB.lv || !checkEquipCanPut(hid, equipA.id)) { // 不能穿,就只卸载 - let res = await takeOffEquipAndCalPlayerCe(roleId, sid, heroB, equipB, id, funcs);//卸下装备并重算战力 - return res; - } else { - let res = await dressEquip(serverId, roleId, sid, heroB, equipA, funcs);//替换给武将,并计算战力 - return res; - } - } else { - let res = await takeOffEquipAndCalPlayerCe(roleId, sid, null, equipA, id, funcs);//卸下装备并重算战力 - return res; - } - } else if (!!heroB) {//从穿戴装备的武将上卸下装备 - let res = await takeOffEquipAndCalPlayerCe(roleId, sid, heroB, equipB, id, funcs);//卸下装备并重算战力 - return res - } -} -/** - * 从穿戴装备的武将上卸下装备,并重算战力 - * @param roleId - * @param sid - * @param seqId - * @param hero - * @param id - */ -export async function takeOffEquipAndCalPlayerCe(roleId: string, sid: string, hero: HeroType, equip: EquipType, id: number, funcs: number[]) { - console.log('***takeOffEquipAndCalPlayerCe', roleId, sid, hero&&hero.hid, equip.seqId, id) - if (hero) { - let args = calEquipSeids(hero); - hero = await HeroModel.removeEquip(roleId, hero.hid, id, equip._id); - - await calPlayerCeAndSave(HERO_SYSTEM_TYPE.EQUIP, sid, roleId, hero, {}, args); - await checkTaskWithHero(roleId, sid, funcs, TASK_TYPE.EQUIP_BY_HERO, hero, [-1]); - } else { - await EquipModel.putOnOrOff(equip._id, 0); - } - // 任务 - await checkTask(roleId, sid, funcs, TASK_TYPE.EQUIP_SUM, -1, true, {}); - await checkTaskWithEquip(roleId, sid, funcs, TASK_TYPE.EQUIP_QUALITY, equip, [-1]); - return [{ seqId: equip.seqId, hid: 0, id: equip.id, ePlaceId: equip.ePlaceId }]; -} -/** - * 穿戴装备并重算战力 - * @param roleId - * @param sid - * @param hero - * @param equip - */ -export async function dressEquip(serverId: number, roleId: string, sid: string, hero: HeroType, equip: EquipType, funcs: number[]) { - console.log('***dressEquip', roleId, sid, hero&&hero.hid, equip.seqId) - - let args = calEquipSeids(hero); - let index = hero.ePlace.findIndex(cur => cur.id == equip.ePlaceId); - let equipOffInfo = hero.ePlace[index].equip; - hero = await HeroModel.addEquip(roleId, hero.hid, equip.ePlaceId, equip._id, equipOffInfo?._id); - await calPlayerCeAndSave(HERO_SYSTEM_TYPE.EQUIP, sid, roleId, hero, {}, args); - - // 任务 - await checkTask(roleId, sid, funcs, TASK_TYPE.EQUIP_SUM, 1, true, {}); - await checkTaskWithHero(roleId, sid, funcs, TASK_TYPE.EQUIP_BY_HERO, hero, [1]); - await checkTaskWithEquip(roleId, sid, funcs, TASK_TYPE.EQUIP_QUALITY, equip, [1]); - await checkActivityTask(serverId, sid, funcs, roleId, TASK_TYPE.EQUIP_QUALITY, 1, { quality: equip.quality }) - await checkActivityTask(serverId, sid, funcs, roleId, TASK_TYPE.EQUIP_SUM, 1) - - let res = [ - { seqId: equip.seqId, hid: hero.hid, id: equip.id, ePlaceId: equip.ePlaceId } - ]; - if(equipOffInfo) { - res.push({ seqId: equipOffInfo.seqId, hid: 0, id: equipOffInfo.id, ePlaceId: equipOffInfo.ePlaceId }) - } - return res -} - -export function checkEquipCanPut(hid: number, id: number) { +export function checkEquipCanPut(hero: HeroType, id: number) { + let hid = hero.hid; let dicGood = gameData.goods.get(id); + if(dicGood.lvLimited > hero.lv) return false; let dicHero = gameData.hero.get(hid); let dicJob = gameData.job.get(dicHero.jobid); if(dicGood.jobLimited.indexOf(0) == -1 && dicGood.jobLimited.indexOf(dicJob.job_class) == -1) return false; diff --git a/shared/db/Hero.ts b/shared/db/Hero.ts index 4a59b2016..3fd6b2624 100644 --- a/shared/db/Hero.ts +++ b/shared/db/Hero.ts @@ -182,25 +182,19 @@ export default class Hero extends BaseModel { return hero; } - public static async addEquip(roleId: string, hid: number, ePlaceId: number, equipId: string, equipOffId: string) { + public static async addEquip(roleId: string, hid: number, ePlaceId: number, equipId: string) { const hero: HeroType = await HeroModel.findOneAndUpdate( { roleId, hid, 'ePlace.id': ePlaceId }, { $set: { 'ePlace.$.equip': equipId } }, { new: true }).populate('ePlace.equip').lean(); - await Equip.putOnOrOff(equipId, hero.hid); - if(equipOffId) await Equip.putOnOrOff(equipOffId, 0); return hero; } - public static async removeEquip(roleId: string, hid: number, ePlaceId: number, equipId: string, lean = true) { + public static async removeEquip(roleId: string, hid: number, ePlaceId: number, lean = true) { const hero: HeroType = await HeroModel.findOneAndUpdate( { roleId, hid, 'ePlace.id': ePlaceId }, { $set: { 'ePlace.$.equip': null } }, { new: true }).populate('ePlace.equip').lean(lean); - - if (hero) { - await Equip.putOnOrOff(equipId, 0); - } return hero; }