Files
ZYZ/game-server/app/services/equipService.ts
mamengke01 66a9bf7809 战力
2020-12-28 09:49:53 +08:00

86 lines
3.9 KiB
TypeScript

import { mergeSameGoods } from '../pubUtils/util';
import { EquipModel } from "../db/Equip";
import { HeroModel, EPlace, HeroType } from "../db/Hero";
import { ITID } from "../consts/constModules/itemConst";
import { getHeroJob, getGoodById, gameData, getJewelById, getHeroEquipByClassId } from "../pubUtils/data";
import { calPlayerCeAndSave } from "./playerCeService";
import { HERO_SYSTEM_TYPE } from "../consts";
import { EquipType } from "../db/Equip";
import { calEquipSeids } from '../pubUtils/playerCe';
const _ = require('underscore');
export function checkMaterialEnough(consumes:Array<{id: number, count: number}>, jewel: number, jewelCount: number) {
let comJewelMap = {};
consumes = mergeSameGoods(consumes);
let needConsumes: Array<{id: number, count: number}> = [];
let gidJewelInfo = getGoodById(jewel);
for (let { id, count } of consumes) {
let jewelInfo = getJewelById(id);
if (!jewelInfo || !jewelInfo.count)
continue;
needConsumes.push({ id, count});
comJewelMap[jewelInfo.good_id] = count + (comJewelMap[jewelInfo.good_id] || 0);
for (let i = jewelInfo.lvLimited; i < gidJewelInfo.lvLimited; i++) {
if (((comJewelMap[jewelInfo.good_id] || 0) % jewelInfo.count != 0)) {
break;
}
let comcount = Math.floor(((comJewelMap[jewelInfo.good_id] || 0)) / jewelInfo.count);
if (comcount < 1) {
break;
}
comJewelMap[jewelInfo.nextJewelId] = comcount + (comJewelMap[jewelInfo.nextJewelId] || 0);
delete comJewelMap[jewelInfo.good_id];
if (!!jewelInfo.specialCount)
needConsumes.push({ count: jewelInfo.specialCount * comcount, id: jewelInfo.nextSpecialId});
jewelInfo = getJewelById(jewelInfo.nextJewelId);
}
}
if (comJewelMap[jewel] != jewelCount || Object.keys(comJewelMap).length != 1)
return false;
return needConsumes;
}
export async function changeEquip(roleId: string, sid: string, equip: EquipType, hid: number, id: number, seqId: number) {
let hero;
if (!!hid) //需要卸下或者替换的武将
hero = await HeroModel.findByHidAndRole(hid, roleId);
if (!!equip) {//需要卸下的装备
if (!!hero) {//判断是否替换装备
let goodInfo = getGoodById(equip.id);
if (goodInfo.lvLimited > hero.lv)
return takeOffEquip(equip.seqId);
let { jobid } = gameData.hero.get(hid);
let { job_class } = getHeroJob(jobid);
let { classId } = getHeroEquipByClassId(goodInfo.itid);
if (_.indexOf(classId, job_class) < 0)
return takeOffEquip(equip.seqId);
return dressEquip(roleId, sid, hero, equip);
} else {
return takeOffEquip(equip.seqId);
}
} else if (!!hero) {//从穿戴装备的武将上卸下装备
await takeOffEquipAndCalPlayerCe(roleId, sid, seqId, hero, id);//卸下装备并重算战力
}
}
export async function takeOffEquipAndCalPlayerCe(roleId: string, sid: string, seqId: number, hero:HeroType, id: number ) {
let index = _.findIndex(hero.ePlace, { id });
if (index < 0)
return;
await EquipModel.updateEquipInfo(seqId, { hid: 0 });
hero.ePlace[index].equip = null;
let args = calEquipSeids(hero);
await calPlayerCeAndSave(sid, roleId, [hero], HERO_SYSTEM_TYPE.EQUIP, args);
return { seqId, hid: 0};
}
export async function dressEquip(roleId: string, sid: string, hero:HeroType, equip: EquipType) {
hero = await HeroModel.addEquip(roleId, hero.hid, equip.ePlaceId, equip._id);
let args = calEquipSeids(hero);
await calPlayerCeAndSave(sid, roleId, [hero], HERO_SYSTEM_TYPE.EQUIP, args);
return { seqId: equip.seqId, hid: hero.hid };
}
async function takeOffEquip(seqId: number) {
await EquipModel.updateEquipInfo(seqId, { hid: 0 });
return { seqId, hid: 0};
}