From c3a1d68aae9664d5a2a4b146d832621cad378ccf Mon Sep 17 00:00:00 2001 From: mamengke01 <794347210@qq.com> Date: Thu, 24 Dec 2020 11:38:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9D=E7=9F=B3=E5=90=88=E6=88=90=E5=B9=B6?= =?UTF-8?q?=E7=A9=BF=E6=88=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/servers/role/handler/equipHandler.ts | 20 ++++----- game-server/app/services/rewardService.ts | 9 ++++ shared/db/Item.ts | 44 ++++++++++++------- 3 files changed, 46 insertions(+), 27 deletions(-) diff --git a/game-server/app/servers/role/handler/equipHandler.ts b/game-server/app/servers/role/handler/equipHandler.ts index 604cfeb83..3b9a2c5b4 100644 --- a/game-server/app/servers/role/handler/equipHandler.ts +++ b/game-server/app/servers/role/handler/equipHandler.ts @@ -3,7 +3,7 @@ import { STATUS, EQUIP_STRENGTHEN_TYPE, CURRENCY_BY_TYPE, CURRENCY_TYPE, HERO_SY import { ItemInter } from "../../../pubUtils/interface"; import { resResult, parseGoodStr, getRandomByLen, deepCopy, mergeSameGoods, getRandValueByMinMax, getRandEelm } from "../../../pubUtils/util"; -import { addItems, handleCost } from "../../../services/rewardService"; +import { addItems, handleCost, decreaseItems } from "../../../services/rewardService"; import { checkMaterialEnough } from "../../../services/equipService"; import { EquipModel, RandSe } from "../../../db/Equip"; import { HeroModel, EPlace } from "../../../db/Hero"; @@ -536,7 +536,7 @@ export class EquipHandler { items.push({id: item.id, count: item.count, ratio: 1}) } items = items.concat(needConsumes); - let {hasError} = await ItemModel.decreaseItems(roleId, items); + 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 }]); @@ -554,7 +554,7 @@ export class EquipHandler { } 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'); @@ -570,28 +570,26 @@ export class EquipHandler { if (!!eid) { equip = await EquipModel.getEquip(eid); let index = _.findIndex(equip.holes,{id}); - if (index) { - equip.holes[id].jewel = jewel; + if (!!equip.holes[index] && equip.holes[index].jewel == goodInfo.composeMaterial[0].id) { + equip.holes[index].jewel = jewel; needUpdate = true; - consumes.push({id:jewel, count: 1, ratio: 1}); + 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 ItemModel.decreaseItems(roleId, consumes); + let hasError = await decreaseItems(roleId, sid, consumes); if (!!hasError) return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH); - let res = await handleCost(roleId, sid, consumes); - if (!res) - 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}] }); } - return resResult(STATUS.SUCCESS, { goods: result }); } } \ No newline at end of file diff --git a/game-server/app/services/rewardService.ts b/game-server/app/services/rewardService.ts index 68b84001d..75a9e310f 100644 --- a/game-server/app/services/rewardService.ts +++ b/game-server/app/services/rewardService.ts @@ -207,4 +207,13 @@ function sortItems (goods: Array, bags: Array, skins: Array } } } +} + +export async function decreaseItems(roleId: string, sid: string, bags: Array<{id: number, count:number, ratio?: number}>) { + let uids = [{uid: roleId, sid}]; + if(bags.length > 0) { + let {hasError, result} = await ItemModel.decreaseItems(roleId, bags); + if(hasError) return true; + pinus.app.get('channelService').pushMessageByUids('onItemUpdate', resResult(STATUS.SUCCESS, {goods: result} ), uids); + } } \ No newline at end of file diff --git a/shared/db/Item.ts b/shared/db/Item.ts index 8055264ac..b60a38603 100644 --- a/shared/db/Item.ts +++ b/shared/db/Item.ts @@ -1,6 +1,7 @@ import BaseModel from './BaseModel'; import { index, getModelForClass, prop, DocumentType, modelOptions, mongoose } from '@typegoose/typegoose'; import Transaction from "mongoose-transactions/src/main"; +const _ = require('underscore'); @index({ roleId: 1, id: 1 }) @index({ seqId: 1 }) @@ -50,24 +51,35 @@ export default class Item extends BaseModel { return items; } - public static async decreaseItems(roleId: string, items: Array<{id: number, count: number, ratio?: number}>, lean = true) { - let updateItems = new Array<{id: number, count: number}>(), hasError: boolean = false, result = new Array(); - for (let { id, count, ratio } of items) { - const rec: ItemType = await ItemModel.findOneAndUpdate({roleId, id, count: {$gte: count} },{ $inc: { count: -1 * count }}, {new: true}).lean(lean); - if(rec) { - result.push({id: rec.id, count: rec.count}); - updateItems.push({id, count}); - } else { - hasError = true; break; - } + public static async decreaseItems(roleId: string, items: Array<{ id: number, count: number, ratio?: number }>, lean = true) { + let updateItems = new Array<{ id: number, count: number, ratio?: number }>(), hasError: boolean = false, result = new Array(); + for (let { id, count, ratio } of items) { + let rec: ItemType; + if (!ratio) { + rec = await ItemModel.findOneAndUpdate({ roleId, id, count: { $gte: count } }, { $inc: { count: -1 * count } }, { new: true }).lean(lean); + } else { + rec = await ItemModel.findOneAndUpdate({ roleId, id }, { $inc: { count: ratio * count } }, { new: true }).lean(lean); + } + if (!!rec) { + let index = _.findIndex(result,{id}) + if (!!result[index]) { + result[index] = { id: rec.id, count: rec.count } + } else { + result.push({ id: rec.id, count: rec.count }); + } + updateItems.push({ id, count, ratio }); + } else { + hasError = true; break; + } } - if(hasError) { // 数量不足 - for(let {id, count} of updateItems) { - await ItemModel.findOneAndUpdate({roleId, id },{ $inc: { count }}, {new: true}).lean(lean); - } - return {hasError: true} + if (hasError) { // 数量不足 + for (let { id, count, ratio } of updateItems) { + if (!ratio) ratio = -1; + await ItemModel.findOneAndUpdate({ roleId, id }, { $inc: { count: -ratio * count } }, { new: true }).lean(lean); + } + return { hasError: true } } else { - return {hasError: false, result} + return { hasError: false, result } } } // public static async decreaseItems(roleId: string, items: Array<{ id: number, count: number, ratio?: number}>, lean = true) {