654 lines
27 KiB
TypeScript
654 lines
27 KiB
TypeScript
import { Application, BackendSession } from "pinus";
|
|
import { STATUS, EQUIP_STRENGTHEN_TYPE, CURRENCY_BY_TYPE, CURRENCY_TYPE, HERO_SYSTEM_TYPE, CONSUME_TYPE, GOOD_TYPE, HERO_GROW_MAX } from "../../../consts";
|
|
import { ItemInter } from "../../../pubUtils/interface";
|
|
|
|
import { resResult, parseGoodStr, getRandomByLen, deepCopy, mergeSameGoods, getRandValueByMinMax, getRandEelm } from "../../../pubUtils/util";
|
|
import { addItems, handleCost, decreaseItems } from "../../../services/rewardService";
|
|
import { checkMaterialEnough } from "../../../services/equipService";
|
|
import { EquipModel, RandSe } from "../../../db/Equip";
|
|
import { HeroModel, EPlace } from "../../../db/Hero";
|
|
import { ItemModel } from "../../../db/Item";
|
|
import Role from "../../../db/Role";
|
|
import { calPlayerCeAndSave } from "../../../services/playerCeService";
|
|
import { getHeroJob, getGoodById, gameData, getJewelById, getHeroEquipByClassId } from "../../../pubUtils/data";
|
|
import { EQUIP } from "../../../pubUtils/dicParam";
|
|
import { ITID, SPEICAL_ITEM, RANDOM_SE_COUNT } from "../../../consts/constModules/itemConst";
|
|
import { changeEquip } from "../../../services/equipService";
|
|
|
|
const _ = require('underscore');
|
|
|
|
export default function (app: Application) {
|
|
return new EquipHandler(app);
|
|
}
|
|
|
|
export class EquipHandler {
|
|
|
|
constructor(private app: Application) {
|
|
}
|
|
|
|
|
|
// test接口添加任意道具
|
|
public async addItem(msg: { id: number, count: number }, session: BackendSession) {
|
|
let roleId: string = session.get('roleId');
|
|
let roleName: string = session.get('roleName');
|
|
let sid: string = session.get('sid');
|
|
let { id, count } = msg;
|
|
let goods = await addItems(roleId, roleName, sid, [{ id, count }]);
|
|
return resResult(STATUS.SUCCESS, { goods });
|
|
}
|
|
|
|
// 合成装备
|
|
public async composeEquip(msg: { gid: number, originalEquip: number[] }, session: BackendSession) {
|
|
let roleId: string = session.get('roleId');
|
|
let roleName: string = session.get('roleName');
|
|
let sid: string = session.get('sid');
|
|
// 消耗材料
|
|
// 获得装备
|
|
let { gid, originalEquip } = msg;
|
|
|
|
|
|
let targetGood = gameData.goods.get(gid);
|
|
if (!targetGood) return resResult(STATUS.DIC_DATA_NOT_FOUND);
|
|
|
|
let cost = new Array<ItemInter>();
|
|
if (targetGood.suitId > 0) { // 套装
|
|
cost = cost.concat(targetGood.composeMaterial);
|
|
let specialMaterial = targetGood.specialMaterial;
|
|
let costCount = 0;
|
|
let equips = await EquipModel.getEquips(originalEquip);
|
|
for (let { id, seqId } of equips) {
|
|
if (specialMaterial.ids.includes(id)) {
|
|
costCount++;
|
|
cost.push({ id, seqId, count: 1 });
|
|
}
|
|
}
|
|
if (specialMaterial.count > costCount) {
|
|
return resResult(STATUS.ROLE_MATERIAL_NOT_ENOUGH);
|
|
}
|
|
|
|
} else { // 普通装备
|
|
cost.push({
|
|
id: targetGood.pieceId,
|
|
count: targetGood.pieces
|
|
});
|
|
}
|
|
let result = await handleCost(roleId, sid, cost);
|
|
if (!result) return resResult(STATUS.ROLE_MATERIAL_NOT_ENOUGH);
|
|
|
|
let items = [{ id: gid, count: 1 }];
|
|
let goods = await addItems(roleId, roleName, sid, items);
|
|
return resResult(STATUS.SUCCESS, { goods });
|
|
}
|
|
|
|
// 装备栏强化
|
|
public async strengthen(msg: { hid: number, ePlaceId: number, type: number }, session: BackendSession) {
|
|
let roleId: string = session.get('roleId');
|
|
// let roleName: string = session.get('roleName');
|
|
let sid: string = session.get('sid');
|
|
|
|
let { hid, ePlaceId, type } = msg;
|
|
let hero = await HeroModel.findByHidAndRoleWithEquip(hid, roleId);
|
|
if (!hero) return resResult(STATUS.HERO_NOT_FIND);
|
|
|
|
let { ePlace, lv: playerLv } = hero; // 装备栏
|
|
let strengthenArr = new Array<EPlace>();
|
|
if (type == EQUIP_STRENGTHEN_TYPE.SINGLE || type == EQUIP_STRENGTHEN_TYPE.SINGLE_QUICK) { // 单装备强化
|
|
strengthenArr = ePlace.filter(cur => cur.id == ePlaceId && cur.equip);
|
|
} else if (type == EQUIP_STRENGTHEN_TYPE.ALL_QUICK) { // 全六件(装备中)的强化
|
|
strengthenArr = ePlace.filter(cur => cur.equip);
|
|
}
|
|
if (strengthenArr.length <= 0) {
|
|
return resResult(STATUS.ROLE_EQUIP_PLACE_NOT_ENOUGH);
|
|
}
|
|
let minLv = strengthenArr[0].lv; // 从最低装备的等级开始
|
|
for (let { lv } of strengthenArr) {
|
|
if (lv < minLv) minLv = lv;
|
|
}
|
|
|
|
let { coin } = await Role.findByRoleId(roleId);
|
|
let maxLv = type == EQUIP_STRENGTHEN_TYPE.SINGLE ? minLv + 1 : playerLv;
|
|
if (maxLv > playerLv) maxLv = playerLv;
|
|
if (minLv >= maxLv) {
|
|
return resResult(STATUS.ROLE_EQUIP_REACH_MAX);
|
|
}
|
|
let costCoin = 0; // 消耗铜币
|
|
let flag = false; // 铜币不足
|
|
for (let i = minLv; i < maxLv; i++) {
|
|
for (let s of strengthenArr) {
|
|
if (s.lv == i) {
|
|
let cost = gameData.strengthenCost.get(i + 1);
|
|
if (!cost) { flag = true; break; }
|
|
if (coin < costCoin + cost) { flag = true; break; }
|
|
costCoin += cost;
|
|
|
|
s.lv++;
|
|
}
|
|
}
|
|
if (flag) break;
|
|
}
|
|
|
|
if (costCoin <= 0) { // 连一级都不够升
|
|
return resResult(STATUS.ROLE_COIN_NOT_ENOUGH);
|
|
}
|
|
|
|
let result = await handleCost(roleId, sid, [{
|
|
id: CURRENCY_BY_TYPE.get(CURRENCY_TYPE.COIN),
|
|
count: costCoin
|
|
}]);
|
|
if (!result) return resResult(STATUS.ROLE_MATERIAL_NOT_ENOUGH);
|
|
|
|
hero.ePlace = ePlace;
|
|
|
|
await calPlayerCeAndSave(sid, roleId, [hero], HERO_SYSTEM_TYPE.EQUIP_BASE);
|
|
const curHero = {
|
|
hid,
|
|
ePlace: strengthenArr
|
|
}
|
|
return resResult(STATUS.SUCCESS, { curHero });
|
|
|
|
}
|
|
|
|
// 装备栏一键强化至相应等级
|
|
public async strengthenAll(msg: { hid: number, lv: number }, session: BackendSession) {
|
|
let roleId: string = session.get('roleId');
|
|
// let roleName: string = session.get('roleName');
|
|
let sid: string = session.get('sid');
|
|
|
|
let { hid, lv: maxLv } = msg; // lv: 升到哪一级
|
|
let hero = await HeroModel.findByHidAndRoleWithEquip(hid, roleId);
|
|
if (!hero) return resResult(STATUS.HERO_NOT_FIND);
|
|
|
|
let { ePlace, lv: playerLv } = hero; // 装备栏
|
|
let strengthenArr = ePlace.filter(cur => cur.equip);
|
|
|
|
if (strengthenArr.length <= 0) {
|
|
return resResult(STATUS.ROLE_EQUIP_PLACE_NOT_ENOUGH);
|
|
}
|
|
let minLv = strengthenArr[0].lv; // 从最低装备的等级开始
|
|
for (let { lv } of strengthenArr) {
|
|
if (lv < minLv) minLv = lv;
|
|
}
|
|
|
|
let { coin } = await Role.findByRoleId(roleId);
|
|
|
|
if (maxLv > playerLv) {
|
|
return resResult(STATUS.ROLE_EQUIP_REACH_MAX);
|
|
}
|
|
let costCoin = 0; // 消耗铜币
|
|
for (let i = minLv; i < maxLv; i++) {
|
|
for (let s of strengthenArr) {
|
|
if (s.lv == i) {
|
|
let cost = gameData.strengthenCost.get(i + 1);
|
|
costCoin += cost;
|
|
|
|
s.lv++;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (costCoin > coin) {
|
|
return resResult(STATUS.ROLE_COIN_NOT_ENOUGH);
|
|
}
|
|
|
|
let result = await handleCost(roleId, sid, [{
|
|
id: CURRENCY_BY_TYPE.get(CURRENCY_TYPE.COIN),
|
|
count: costCoin
|
|
}]);
|
|
if (!result) return resResult(STATUS.ROLE_MATERIAL_NOT_ENOUGH);
|
|
|
|
hero.ePlace = ePlace;
|
|
|
|
await calPlayerCeAndSave(sid, roleId, [hero], HERO_SYSTEM_TYPE.EQUIP_BASE);
|
|
const curHero = {
|
|
hid,
|
|
ePlace: strengthenArr
|
|
}
|
|
return resResult(STATUS.SUCCESS, { curHero });
|
|
|
|
}
|
|
// 装备栏精炼
|
|
public async refine(msg: { hid: number, ePlaceId: number, material: { id: number, count: number }[] }, session: BackendSession) {
|
|
let roleId: string = session.get('roleId');
|
|
// let roleName: string = session.get('roleName');
|
|
let sid: string = session.get('sid');
|
|
|
|
let { hid, ePlaceId, material } = msg;
|
|
|
|
let hero = await HeroModel.findByHidAndRoleWithEquip(hid, roleId);
|
|
if (!hero) return resResult(STATUS.HERO_NOT_FIND);
|
|
let { ePlace } = hero; // 装备栏
|
|
|
|
let curEplace = ePlace.find(cur => cur.id == ePlaceId);
|
|
if (!curEplace) {
|
|
return resResult(STATUS.ROLE_EQUIP_PLACE_NOT_ENOUGH);
|
|
}
|
|
let { lv, refineLv } = curEplace; // 强化等级,精炼等级,精炼次数
|
|
|
|
if (lv < HERO_GROW_MAX.EQUIP_STRENGTHEN) {
|
|
return resResult(STATUS.ROLE_EQUIP_NOT_REACH_MAX);
|
|
}
|
|
if (refineLv >= HERO_GROW_MAX.EQUIP_REFINE) {
|
|
return resResult(STATUS.ROLE_EQUIP_REACH_MAX);
|
|
}
|
|
|
|
// 是否成功精炼
|
|
let dicRefine = gameData.refine.get(refineLv + 1);
|
|
if (!dicRefine) {
|
|
return resResult(STATUS.DIC_DATA_NOT_FOUND)
|
|
}
|
|
|
|
let { successRate } = dicRefine;
|
|
for (let { id, count } of material) {
|
|
let dicGoods = gameData.goods.get(id);
|
|
if (!dicGoods) return resResult(STATUS.DIC_DATA_NOT_FOUND);
|
|
if (!SPEICAL_ITEM.REFINE_ADD_RATE.includes(id)) {
|
|
return resResult(STATUS.ROLE_WRONG_ITEM)
|
|
}
|
|
successRate += count * dicGoods.value;
|
|
if (isNaN(successRate)) console.error(id, count, dicGoods.value);
|
|
}
|
|
|
|
let ran = Math.floor(Math.random() * 100);
|
|
let isSuccess = ran <= successRate;
|
|
// 消耗道具提升成功率 每个道具提升10%成功率
|
|
|
|
// 精炼
|
|
if (isSuccess) {
|
|
curEplace.refineLv++;
|
|
}
|
|
|
|
// 消耗
|
|
let cost = dicRefine.material.concat(material);
|
|
let result = await handleCost(roleId, sid, cost);
|
|
if (!result) return resResult(STATUS.ROLE_MATERIAL_NOT_ENOUGH);
|
|
|
|
await calPlayerCeAndSave(sid, roleId, [hero], HERO_SYSTEM_TYPE.EQUIP_BASE);
|
|
|
|
const curHero = {
|
|
hid,
|
|
ePlace: curEplace
|
|
}
|
|
return resResult(STATUS.SUCCESS, { isSuccess, curHero });
|
|
}
|
|
|
|
// 装备洗炼锁定
|
|
public async lockRandSe(msg: { eid: number, id: number, lock: boolean }, session: BackendSession) {
|
|
let roleId: string = session.get('roleId');
|
|
// let roleName: string = session.get('roleName');
|
|
let sid: string = session.get('sid');
|
|
|
|
let { eid, id, lock } = msg;
|
|
let equip = await EquipModel.findbySeqId(eid);
|
|
if (!equip) return resResult(STATUS.EQUIP_NOT_FIND);
|
|
|
|
let { randSe } = equip;
|
|
if (!randSe || randSe.length <= 0) {
|
|
return resResult(STATUS.ROLE_EQUIP_HAVE_NO_RANDSE);
|
|
}
|
|
let curSe = randSe.find(cur => cur.id == id);
|
|
if(!curSe || lock == curSe.locked) {
|
|
return resResult(STATUS.ROLE_EQUIP_DUPLICATE_LOCK);
|
|
}
|
|
|
|
if (lock) { // 仅在上锁时消耗,根据已有的锁的数量判断消耗
|
|
let lockNum = randSe.filter(cur => cur.locked).length;
|
|
let consumes: Array<{ id: number, count: number }> = [];
|
|
if (lockNum == 0) {
|
|
consumes = parseGoodStr(EQUIP.EQUIP_ONE_LOCKED);
|
|
} else if (lockNum == 1) {
|
|
consumes = parseGoodStr(EQUIP.EQUIP_TWO_LOCKED);
|
|
} else if (lockNum == 2) {
|
|
consumes = parseGoodStr(EQUIP.EQUIP_THREE_LOCKED);
|
|
} else {
|
|
return resResult(STATUS.ROLE_ALL_SE_LOCK);
|
|
}
|
|
let result = await handleCost(roleId, sid, consumes);
|
|
if (!result) return resResult(STATUS.ROLE_MATERIAL_NOT_ENOUGH);
|
|
}
|
|
|
|
let result = await EquipModel.lock(roleId, eid, id, lock);
|
|
if (!result) {
|
|
return resResult(STATUS.ROLE_EQUIP_HAVE_NO_RANDSE);
|
|
}
|
|
return resResult(STATUS.SUCCESS, { curEquip: result });
|
|
|
|
}
|
|
|
|
// 装备洗炼
|
|
public async reStrengthen(msg: { eid: number }, session: BackendSession) {
|
|
let roleId: string = session.get('roleId');
|
|
// let roleName: string = session.get('roleName');
|
|
let sid: string = session.get('sid');
|
|
|
|
let { eid } = msg;
|
|
let equip = await EquipModel.findbySeqId(eid);
|
|
if (!equip) return resResult(STATUS.EQUIP_NOT_FIND);
|
|
|
|
let {id, randSe, hid, ePlaceId } = equip;
|
|
if(!randSe || randSe.length <= 0 ) {
|
|
return resResult(STATUS.ROLE_EQUIP_HAVE_NO_RANDSE);
|
|
}
|
|
|
|
let dicGoods = gameData.goods.get(id);
|
|
if (!dicGoods) return resResult(STATUS.DIC_DATA_NOT_FOUND);
|
|
|
|
let { randomEffect } = dicGoods;
|
|
let chosen = randSe.map(cur => cur.seid); // 上一轮随机出来的
|
|
let randomResult: number[] = getRandEelm(randomEffect.filter(cur => !chosen.includes(cur)), randSe.length);
|
|
|
|
let lockNum = 0;
|
|
let removeSeidList = new Array<number>(); // 原装备上的seid [seid, rand, ...]
|
|
|
|
for(let i = 0; i < randSe.length; i++) {
|
|
removeSeidList.push(randSe[i].seid, randSe[i].rand);
|
|
|
|
if(!randSe[i].locked) {
|
|
let random = gameData.randomEffectPool.get(randomResult[i]);
|
|
if (!random) break ;
|
|
let rand = 0;
|
|
if (random.id > 0) rand = getRandValueByMinMax(random.Min, random.Max, 0);
|
|
randSe[i].seid = random.id;
|
|
randSe[i].rand = rand;
|
|
} else {
|
|
lockNum++;
|
|
}
|
|
}
|
|
|
|
if (lockNum >= randSe.length) {
|
|
return resResult(STATUS.ROLE_EQUIP_CANNOT_RESTRENGTHEN);
|
|
}
|
|
|
|
// 消耗
|
|
let consumes: Array<{ id: number, count: number }> = [];
|
|
if (lockNum == 0) {
|
|
consumes = parseGoodStr(EQUIP.EQUIP_ONE_REFORGED);
|
|
} else if (lockNum == 1) {
|
|
consumes = parseGoodStr(EQUIP.EQUIP_TWO_REFORGED);
|
|
} else if (lockNum == 2) {
|
|
consumes = parseGoodStr(EQUIP.EQUIP_THREE_REFORGED);
|
|
} else {
|
|
consumes = parseGoodStr(EQUIP.EQUIP_FOUR_REFORGED);
|
|
}
|
|
|
|
let result = await handleCost(roleId, sid, consumes);
|
|
if (!result) return resResult(STATUS.ROLE_MATERIAL_NOT_ENOUGH);
|
|
|
|
let equipResult = await EquipModel.updateEquipInfo(eid, { randSe })
|
|
let curEquip = {
|
|
seqId: equipResult.seqId,
|
|
id: equipResult.id,
|
|
randSe: equipResult.randSe
|
|
}
|
|
|
|
// 更新战力
|
|
const hero = await HeroModel.findByHidAndRoleWithEquip(hid, roleId);
|
|
await calPlayerCeAndSave(sid, roleId, [hero], HERO_SYSTEM_TYPE.RESTRENGTHEN, [ePlaceId, ...removeSeidList]);
|
|
|
|
return resResult(STATUS.SUCCESS,{curEquip});
|
|
|
|
}
|
|
|
|
//分解装备
|
|
public async decomposeEquip(msg: { originalEquip: Array<number> }, session: BackendSession) {
|
|
let { originalEquip } = msg;
|
|
let roleId: string = session.get('roleId');
|
|
let roleName: string = session.get('roleName');
|
|
let sid: string = session.get('sid');
|
|
let equips = await EquipModel.getEquips(originalEquip);
|
|
if (equips.length < originalEquip.length)
|
|
return resResult(STATUS.EQUIP_NOT_FIND);
|
|
let goods: Array<{ id: number, count: number }> = [];
|
|
for (let equip of equips) {
|
|
if (!!equip.hid)
|
|
return resResult(STATUS.EQUIP_IS_EQUIPED_NOT_DECOMPOSE);
|
|
let goodInfo = getGoodById(equip.id);
|
|
if (!goodInfo)
|
|
return resResult(STATUS.EQUIP_NOT_FIND);
|
|
goods.concat(goodInfo.decomposeItem);
|
|
}
|
|
let uids = [{ uid: roleId, sid }];
|
|
await EquipModel.deleteEquips(originalEquip);
|
|
this.app.get('channelService').pushMessageByUids('onEquipDel', resResult(STATUS.SUCCESS, {equips:originalEquip}), uids);
|
|
let result = await addItems(roleId, roleName, sid, goods);
|
|
return resResult(STATUS.SUCCESS, { goods: result });
|
|
}
|
|
|
|
//穿戴或卸载装备 1-穿上装备(包括替换) 2-脱下装备
|
|
public async putOnOrOff(msg: { eid: number, hid: number, type: number }, session: BackendSession) {
|
|
let { eid, hid, type } = msg;
|
|
let roleId: string = session.get('roleId');
|
|
let equip = await EquipModel.getEquip(eid);
|
|
let sid: string = session.get('sid');
|
|
let goodInfo = getGoodById(equip.id);
|
|
let obj = ITID.get(goodInfo.itid);
|
|
let id = obj.type;
|
|
let curEquips: Array<{ seqId: number, hid: number, ePlaceId: number }> = [];
|
|
let hero = await HeroModel.findByHidAndRole(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);
|
|
let { jobid } = gameData.hero.get(hid);
|
|
let { job_class } = getHeroJob(jobid);
|
|
let { classId } = getHeroEquipByClassId(goodInfo.itid);
|
|
if (_.indexOf(classId, job_class) < 0)
|
|
return resResult(STATUS.EQUIP_NOT_EQUIPED_HERO);
|
|
let index = _.findIndex(hero.ePlace, { id });
|
|
if (index < 0)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
let objectId = <string>hero.ePlace[index].equip;
|
|
let curEquip = await changeEquip(roleId, sid, objectId, equip.hid, id, eid);
|
|
if (!!curEquip)
|
|
curEquips.push(curEquip);
|
|
hero = await HeroModel.addEquip(roleId, hid, id, equip._id);
|
|
await calPlayerCeAndSave(sid, roleId, [hero]);
|
|
curEquips.push({ seqId: eid, hid, ePlaceId:id});
|
|
} else if (type == 2) {
|
|
if (!equip.hid)
|
|
return resResult(STATUS.EQUIP_NOT_EQUIPED);
|
|
let index = _.findIndex(hero.ePlace, { id });
|
|
if (index < 0)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
hero.ePlace[index].equip = null;
|
|
let {seqId, ePlaceId} = await EquipModel.updateEquipInfo(eid, { hid: 0, ePlaceId: 0 });
|
|
await calPlayerCeAndSave(sid, roleId, [hero]);
|
|
curEquips.push({ seqId, hid, ePlaceId });
|
|
}
|
|
return resResult(STATUS.SUCCESS, { curEquips: curEquips });
|
|
}
|
|
|
|
//装备打孔
|
|
public async digHole(msg: { eid: number, id: number }, session: BackendSession) {
|
|
let { eid, id } = msg;
|
|
let roleId: string = session.get('roleId');
|
|
let sid: string = session.get('sid');
|
|
let equip = await EquipModel.getEquip(eid);
|
|
let index = _.findIndex(equip.holes, { id });
|
|
if (index < 0)
|
|
return resResult(STATUS.EQUIP_HOLE_NOT_FIND);
|
|
if (equip.holes[index].isOpen)
|
|
return resResult(STATUS.EQUIP_HOLE_IS_DUG);
|
|
let consumes: Array<{ id: number, count: number }> = [];
|
|
if (id == 1) {
|
|
consumes = parseGoodStr(EQUIP.EQUIP_ONE_HOLE);
|
|
} else if (id == 2) {
|
|
consumes = parseGoodStr(EQUIP.EQUIP_TWO_HOLE);
|
|
} else if (id == 3) {
|
|
consumes = parseGoodStr(EQUIP.EQUIP_THREE_HOLE);
|
|
}
|
|
let result = await handleCost(roleId, sid, consumes);
|
|
if (!result)
|
|
return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH);
|
|
equip.holes[index].isOpen = true;
|
|
await EquipModel.updateEquipInfo(eid, { holes: equip.holes });
|
|
return resResult(STATUS.SUCCESS, { curEquip: { seqId: eid, holes: equip.holes } });
|
|
}
|
|
|
|
//宝石镶嵌
|
|
public async fillHole(msg: { eid: number, id: number, jewel: number }, session: BackendSession) {
|
|
let { eid, id, jewel } = msg;
|
|
let roleId: string = session.get('roleId');
|
|
let sid: string = session.get('sid');
|
|
let consumes: Array<{ id: number, count: number }> = [];
|
|
let goods: Array<{ id: number, count: number }> = [];
|
|
let equip = await EquipModel.getEquip(eid);
|
|
let { itid } = getGoodById(equip.id);
|
|
let {equipJewel} = ITID.get(itid);
|
|
let jewelInfo = getGoodById(jewel);
|
|
if (jewelInfo.itid != equipJewel)
|
|
return resResult(STATUS.EQUIP_NOT_MATCH_JEWEL);
|
|
let index = _.findIndex(equip.holes, { id });
|
|
if (index < 0)
|
|
return resResult(STATUS.EQUIP_HOLE_NOT_FIND);
|
|
if (!equip.holes[index].isOpen)
|
|
return resResult(STATUS.EQUIP_HOLE_IS_NOT_DUG);
|
|
let oldJewel = equip.holes[index].jewel;
|
|
if (!!oldJewel)
|
|
goods.push({id: oldJewel, count:1});
|
|
consumes.push({ id: jewel, count: 1 });
|
|
let result = await handleCost(roleId, sid, consumes);
|
|
if (!result)
|
|
return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH);
|
|
equip.holes[index].jewel = jewel;
|
|
await EquipModel.updateEquipInfo(eid, { holes: equip.holes });
|
|
let roleName: string = session.get('roleName');
|
|
if (goods.length)
|
|
await addItems(roleId, roleName, sid, goods);
|
|
if (!!equip.hid) {
|
|
let hero = await HeroModel.findByHidAndRole(equip.hid, roleId);
|
|
await calPlayerCeAndSave(sid, roleId, [hero], HERO_SYSTEM_TYPE.JEWEL_ON, [jewel, oldJewel]);
|
|
}
|
|
return resResult(STATUS.SUCCESS, { curEquip: { seqId: eid, holes: equip.holes } });
|
|
}
|
|
|
|
//宝石合成(一键放入type:1, 合成type:2)
|
|
public async composeJewel(msg: { jewel: number, count: number, consumes: Array<{ id: number, count: number }>, type: number }, session: BackendSession) {
|
|
let { count, consumes, jewel, type } = msg;
|
|
let roleId: string = session.get('roleId');
|
|
let roleName: string = session.get('roleName');
|
|
let sid: string = session.get('sid');
|
|
let goodInfo = getGoodById(jewel);
|
|
let good = ITID.get(goodInfo.itid);
|
|
if (good.type != CONSUME_TYPE.JEWEL)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
//检查宝石消耗是否合法TODO
|
|
let needConsumes = checkMaterialEnough(consumes, jewel, count);
|
|
if (!needConsumes)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
let res = await handleCost(roleId, sid, needConsumes);
|
|
if (!res)
|
|
return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH);
|
|
let result = await addItems(roleId, roleName, sid, [{ id: jewel, count: count }]);
|
|
if (type == 1)
|
|
return resResult(STATUS.SUCCESS, { goods: result });
|
|
return resResult(STATUS.SUCCESS);
|
|
}
|
|
|
|
//宝石卸下
|
|
public async putOffHole(msg: { eid: number, id: number }, session: BackendSession) {
|
|
let { eid, id } = msg;
|
|
let roleId: string = session.get('roleId');
|
|
let roleName: string = session.get('roleName');
|
|
let sid: string = session.get('sid');
|
|
let goods: Array<{ id: number, count: number }> = [];
|
|
let equip = await EquipModel.getEquip(eid);
|
|
let index = _.findIndex(equip.holes, { id });
|
|
if (index > 0)
|
|
return resResult(STATUS.EQUIP_HOLE_NOT_FIND);
|
|
let jewel = equip.holes[index].jewel;
|
|
if (!jewel)
|
|
return resResult(STATUS.EQUIP_NOT_FILL_HOLE);
|
|
goods.push({ id: jewel, count: 1 });
|
|
equip.holes[index].jewel = 0;
|
|
await EquipModel.updateEquipInfo(eid, { holes: equip.holes });
|
|
await addItems(roleId, roleName, sid, goods);
|
|
if (!!equip.hid) {
|
|
let hero = await HeroModel.findByHidAndRole(equip.hid, roleId);
|
|
await calPlayerCeAndSave(sid, roleId, [hero], HERO_SYSTEM_TYPE.JEWEL_OFF, [jewel]);
|
|
}
|
|
return resResult(STATUS.SUCCESS, { curEquip: { seqId: eid, holes: equip.holes } });
|
|
}
|
|
|
|
//宝石购买并合成
|
|
public async composeAndPurchaseJewel(msg: { jewel: number, count: number, consumes: Array<{ id: number, count: number }>, purchaseGoods: Array<{ id: number, count: number }>}, session: BackendSession) {
|
|
let { count, consumes, jewel, purchaseGoods } = msg;
|
|
let roleId: string = session.get('roleId');
|
|
let roleName: string = session.get('roleName');
|
|
let sid: string = session.get('sid');
|
|
let goodInfo = getGoodById(jewel);
|
|
let good = ITID.get(goodInfo.itid);
|
|
if (good.type != CONSUME_TYPE.JEWEL)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
//检查宝石消耗是否合法TODO
|
|
if (!purchaseGoods)
|
|
purchaseGoods = [];
|
|
if (!consumes)
|
|
consumes = [];
|
|
let needConsumes = checkMaterialEnough(consumes.concat(purchaseGoods), jewel, count);
|
|
if (!needConsumes)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
let items:Array<{id: number, count: number, ratio?:number}> = [];
|
|
for (let item of purchaseGoods) {
|
|
items.push({id: item.id, count: item.count, ratio: 1})
|
|
}
|
|
items = items.concat(needConsumes);
|
|
let hasError = await decreaseItems(roleId, sid, items);
|
|
if (!!hasError)
|
|
return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH);
|
|
await addItems(roleId, roleName, sid, [{ id: jewel, count: count }]);
|
|
return resResult(STATUS.SUCCESS);
|
|
}
|
|
|
|
public async purchaseGoods(msg: { purchaseGoods: Array<{ id: number, count: number }>}, session: BackendSession) {
|
|
let { purchaseGoods } = msg;
|
|
let roleId: string = session.get('roleId');
|
|
let roleName: string = session.get('roleName');
|
|
let sid: string = session.get('sid');
|
|
let result = await addItems(roleId, roleName, sid, purchaseGoods );
|
|
if(!result) {
|
|
return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH);
|
|
}
|
|
return resResult(STATUS.SUCCESS);
|
|
}
|
|
//合成下一级宝石并穿戴(装备镶嵌界面)
|
|
public async composeNextLevelJewel(msg: { jewel: number, count: number, eid: number, id: number }, session: BackendSession) {
|
|
let { count, jewel, eid, id } = msg;
|
|
let roleId: string = session.get('roleId');
|
|
let roleName: string = session.get('roleName');
|
|
let sid: string = session.get('sid');
|
|
let goodInfo = getGoodById(jewel);
|
|
let good = ITID.get(goodInfo.itid);
|
|
let needUpdate = false;
|
|
let consumes: Array<{id: number, count: number, ratio?: number}> = [];
|
|
if (good.type != CONSUME_TYPE.JEWEL)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
let equip;
|
|
if (!!eid) {
|
|
equip = await EquipModel.getEquip(eid);
|
|
let index = _.findIndex(equip.holes,{id});
|
|
if (!!equip.holes[index] && equip.holes[index].jewel == goodInfo.composeMaterial[0].id) {
|
|
equip.holes[index].jewel = jewel;
|
|
needUpdate = true;
|
|
consumes.push({id: goodInfo.composeMaterial[0].id, count: 1, ratio: 1});
|
|
}
|
|
}
|
|
consumes = consumes.concat(goodInfo.composeMaterial);
|
|
if (goodInfo.specialMaterial.count) {
|
|
consumes.push({id: goodInfo.specialMaterial.ids[0], count: goodInfo.specialMaterial.count})
|
|
}
|
|
let hasError = await decreaseItems(roleId, sid, consumes);
|
|
if (!!hasError)
|
|
return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH);
|
|
let result = {};
|
|
if (needUpdate) {
|
|
await EquipModel.updateEquipInfo(eid, { holes: equip.holes });
|
|
return resResult(STATUS.SUCCESS, { curEquip: { seqId: eid, holes: equip.holes } });
|
|
} else {
|
|
result = await addItems(roleId, roleName, sid, [{ id: jewel, count: count }]);
|
|
return resResult(STATUS.SUCCESS, { goods: [{id: jewel, count}] });
|
|
}
|
|
}
|
|
} |