宝石合成并穿戴

This commit is contained in:
mamengke01
2020-12-24 11:38:05 +08:00
parent 6a11bfb170
commit c3a1d68aae
3 changed files with 46 additions and 27 deletions

View File

@@ -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 });
}
}

View File

@@ -207,4 +207,13 @@ function sortItems (goods: Array<ItemInter>, bags: Array<BagInter>, 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);
}
}

View File

@@ -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) {