Files
ZYZ/game-server/app/servers/role/handler/equipHandler.ts
mamengke01 d1925a221a 格式化
2020-12-21 17:43:25 +08:00

517 lines
21 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, parseReward, getRandomByLen, deepCopy } from "../../../pubUtils/util";
import { addItems, handleCost } from "../../../services/rewardService";
import { EquipModel, RandSe } from "../../../db/Equip";
import { HeroModel, EPlace } from "../../../db/Hero";
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 } from "../../../consts/constModules/itemConst";
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.ROLE_INFO_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);
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.ROLE_INFO_NOT_FOUND)
}
let { successRate } = dicRefine;
for (let { id, count } of material) {
let dicGoods = gameData.goods.get(id);
if (!dicGoods) return resResult(STATUS.ROLE_INFO_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);
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.EQUIP_HAVE_NO_RANDSE);
}
if (lock) { // 仅在上锁时消耗,根据已有的锁的数量判断消耗
let lockNum = randSe.filter(cur => cur.locked).length;
let consumes: Array<{ id: number, count: number }> = [];
if (lockNum == 0) {
consumes = parseReward(EQUIP.EQUIP_ONE_LOCKED);
} else if (lockNum == 1) {
consumes = parseReward(EQUIP.EQUIP_TWO_LOCKED);
} else if (lockNum == 2) {
consumes = parseReward(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.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 } = equip;
if (!randSe || randSe.length <= 0) {
return resResult(STATUS.EQUIP_HAVE_NO_RANDSE);
}
let dicGoods = gameData.goods.get(id);
if (!dicGoods) return resResult(STATUS.ROLE_INFO_NOT_FOUND);
let { randomEffect } = dicGoods;
let pool = randomEffect.map(cur => gameData.randomEffectPool.get(cur));
let chosen = randSe.map(cur => cur.seid); // 上一轮和这一轮随机出来的
let hasReset = false, lockNum = 0;
for (let i = 0; i < randSe.length; i++) {
if (!randSe[i].locked) {
let newPool = pool.filter(cur => !chosen.includes(cur.id));
let random = getRandomByLen(newPool);
if (!random) { break };
let rand = 0;
if (random.id > 0) rand = Math.floor(Math.random() * (random.Max - random.Min) + random.Min);
randSe[i].seid = random.id;
randSe[i].rand = rand;
hasReset = true;
} else {
lockNum++;
}
}
if (!hasReset) {
return resResult(STATUS.ROLE_EQUIP_CANNOT_RESTRENGTHEN);
}
// 消耗
let consumes: Array<{ id: number, count: number }> = [];
if (lockNum == 0) {
consumes = parseReward(EQUIP.EQUIP_ONE_REFORGED);
} else if (lockNum == 1) {
consumes = parseReward(EQUIP.EQUIP_TWO_REFORGED);
} else if (lockNum == 2) {
consumes = parseReward(EQUIP.EQUIP_THREE_REFORGED);
} else {
consumes = parseReward(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
}
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, [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 goodInfo = getGoodById(equip.id);
let obj = ITID.get(goodInfo.itid);
let ePlaceId = 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 (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);
if (!!equip.hid)
return resResult(STATUS.EQUIP_IS_EQUIPED);
let index = _.findIndex(hero.ePlace, { id: ePlaceId });
if (index < 0)
return resResult(STATUS.WRONG_PARMS);
let objectId = <string>hero.ePlace[index].equip;
if (!!objectId) {
let lastEquip = await EquipModel.updateEquipInfobyObjectId(objectId, { hid: 0, ePlaceId: 0 });
curEquips.push({
seqId: lastEquip.seqId,
hid: lastEquip.hid,
ePlaceId: lastEquip.ePlaceId
});
}
await HeroModel.addEquip(roleId, hid, ePlaceId, equip._id);
curEquips.push({
seqId: eid,
hid: hid,
ePlaceId: ePlaceId
});
} else if (type == 2) {
if (!equip.hid)
return resResult(STATUS.EQUIP_NOT_EQUIPED);
let index = _.findIndex(hero.ePlace, { id: ePlaceId });
if (index < 0)
return resResult(STATUS.WRONG_PARMS);
hero.ePlace[index].equip = null;
await HeroModel.updateHeroInfo(roleId, hid, { ePlace: hero.ePlace });
equip = await EquipModel.updateEquipInfo(eid, { hid: 0, ePlaceId: 0 });
curEquips.push({
seqId: equip.seqId,
hid: equip.hid,
ePlaceId: equip.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 = parseReward(EQUIP.EQUIP_ONE_HOLE);
} else if (id == 2) {
consumes = parseReward(EQUIP.EQUIP_TWO_HOLE);
} else if (id == 3) {
consumes = parseReward(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 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_NOT_DUG);
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 });
return resResult(STATUS.SUCCESS, { curEquip: { seqId: eid, holes: equip.holes } });
}
//宝石合成
public async composeJewel(msg: { jewel: number, count: number, consumes: Array<{ id: number, count: number }>, speConsumes: Array<{ id: number, count: number }> }, session: BackendSession) {
let { count, consumes, jewel, speConsumes } = msg;
let roleId: string = session.get('roleId');
let roleName: string = session.get('roleName');
let sid: string = session.get('sid');
let goodInfo = getGoodById(jewel);
let { type } = ITID.get(goodInfo.itid);
if (type != CONSUME_TYPE.JEWEL)
return resResult(STATUS.WRONG_PARMS);
//检查宝石消耗是否合法TODO
let comJewelMap = {};
let ways = [deepCopy(speConsumes)];
for (let { id, count } of consumes) {
let jewelInfo = getJewelById(id);
if (!jewelInfo) {
return resResult(STATUS.WRONG_PARMS);
}
count = Math.floor((count + (comJewelMap[jewelInfo.good_id] || 0)) / jewelInfo.count);
if (count < 1) {
return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH);
}
let speCount = jewelInfo.specialMaterial.count * count;
if (jewelInfo.specialMaterial.ids.length) {
let copyWays = deepCopy(ways);
ways = [];
for (let id of jewelInfo.specialMaterial.ids) {
for (let way of copyWays) {
let index = _.findIndex(way, { id });
if (index && way[index].count >= speCount) {
way[index].count = way[index].count - speCount;
ways.push(way);
}
}
}
if (!ways.length)
return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH);
}
delete comJewelMap[jewelInfo.good_id];
comJewelMap[jewelInfo.nextJewelId] = count + (comJewelMap[jewelInfo.nextJewelId] || 0);
}
if (comJewelMap[jewel] != count || Object.keys(comJewelMap).length != 1)
return resResult(STATUS.WRONG_PARMS);
let res = await handleCost(roleId, sid, consumes.concat(speConsumes));
if (!res)
return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH);
let result = await addItems(roleId, roleName, sid, [{ id: jewel, count: count }]);
return resResult(STATUS.SUCCESS, { goods: result });
}
//宝石卸下
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);
if (!equip.holes[index].jewel)
return resResult(STATUS.EQUIP_NOT_FILL_HOLE);
goods.push({ id: equip.holes[index].jewel, count: 1 });
equip.holes[index].jewel = 0;
await EquipModel.updateEquipInfo(eid, { holes: equip.holes });
await addItems(roleId, roleName, sid, goods);
return resResult(STATUS.SUCCESS, { curEquip: { seqId: eid, holes: equip.holes } });
}
}