宝石合成并穿戴
This commit is contained in:
@@ -3,7 +3,7 @@ import { STATUS, EQUIP_STRENGTHEN_TYPE, CURRENCY_BY_TYPE, CURRENCY_TYPE, HERO_SY
|
|||||||
import { ItemInter } from "../../../pubUtils/interface";
|
import { ItemInter } from "../../../pubUtils/interface";
|
||||||
|
|
||||||
import { resResult, parseGoodStr, getRandomByLen, deepCopy, mergeSameGoods, getRandValueByMinMax, getRandEelm } from "../../../pubUtils/util";
|
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 { checkMaterialEnough } from "../../../services/equipService";
|
||||||
import { EquipModel, RandSe } from "../../../db/Equip";
|
import { EquipModel, RandSe } from "../../../db/Equip";
|
||||||
import { HeroModel, EPlace } from "../../../db/Hero";
|
import { HeroModel, EPlace } from "../../../db/Hero";
|
||||||
@@ -536,7 +536,7 @@ export class EquipHandler {
|
|||||||
items.push({id: item.id, count: item.count, ratio: 1})
|
items.push({id: item.id, count: item.count, ratio: 1})
|
||||||
}
|
}
|
||||||
items = items.concat(needConsumes);
|
items = items.concat(needConsumes);
|
||||||
let {hasError} = await ItemModel.decreaseItems(roleId, items);
|
let hasError = await decreaseItems(roleId, sid, items);
|
||||||
if (!!hasError)
|
if (!!hasError)
|
||||||
return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH);
|
return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH);
|
||||||
await addItems(roleId, roleName, sid, [{ id: jewel, count: count }]);
|
await addItems(roleId, roleName, sid, [{ id: jewel, count: count }]);
|
||||||
@@ -554,7 +554,7 @@ export class EquipHandler {
|
|||||||
}
|
}
|
||||||
return resResult(STATUS.SUCCESS);
|
return resResult(STATUS.SUCCESS);
|
||||||
}
|
}
|
||||||
//合成下一级宝石(装备镶嵌界面)
|
//合成下一级宝石并穿戴(装备镶嵌界面)
|
||||||
public async composeNextLevelJewel(msg: { jewel: number, count: number, eid: number, id: number }, session: BackendSession) {
|
public async composeNextLevelJewel(msg: { jewel: number, count: number, eid: number, id: number }, session: BackendSession) {
|
||||||
let { count, jewel, eid, id } = msg;
|
let { count, jewel, eid, id } = msg;
|
||||||
let roleId: string = session.get('roleId');
|
let roleId: string = session.get('roleId');
|
||||||
@@ -570,28 +570,26 @@ export class EquipHandler {
|
|||||||
if (!!eid) {
|
if (!!eid) {
|
||||||
equip = await EquipModel.getEquip(eid);
|
equip = await EquipModel.getEquip(eid);
|
||||||
let index = _.findIndex(equip.holes,{id});
|
let index = _.findIndex(equip.holes,{id});
|
||||||
if (index) {
|
if (!!equip.holes[index] && equip.holes[index].jewel == goodInfo.composeMaterial[0].id) {
|
||||||
equip.holes[id].jewel = jewel;
|
equip.holes[index].jewel = jewel;
|
||||||
needUpdate = true;
|
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);
|
consumes = consumes.concat(goodInfo.composeMaterial);
|
||||||
if (goodInfo.specialMaterial.count) {
|
if (goodInfo.specialMaterial.count) {
|
||||||
consumes.push({id: goodInfo.specialMaterial.ids[0], count: 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)
|
if (!!hasError)
|
||||||
return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH);
|
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 = {};
|
let result = {};
|
||||||
if (needUpdate) {
|
if (needUpdate) {
|
||||||
await EquipModel.updateEquipInfo(eid, { holes: equip.holes });
|
await EquipModel.updateEquipInfo(eid, { holes: equip.holes });
|
||||||
|
return resResult(STATUS.SUCCESS, { curEquip: { seqId: eid, holes: equip.holes } });
|
||||||
} else {
|
} else {
|
||||||
result = await addItems(roleId, roleName, sid, [{ id: jewel, count: count }]);
|
result = await addItems(roleId, roleName, sid, [{ id: jewel, count: count }]);
|
||||||
|
return resResult(STATUS.SUCCESS, { goods: [{id: jewel, count}] });
|
||||||
}
|
}
|
||||||
return resResult(STATUS.SUCCESS, { goods: result });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+28
-16
@@ -1,6 +1,7 @@
|
|||||||
import BaseModel from './BaseModel';
|
import BaseModel from './BaseModel';
|
||||||
import { index, getModelForClass, prop, DocumentType, modelOptions, mongoose } from '@typegoose/typegoose';
|
import { index, getModelForClass, prop, DocumentType, modelOptions, mongoose } from '@typegoose/typegoose';
|
||||||
import Transaction from "mongoose-transactions/src/main";
|
import Transaction from "mongoose-transactions/src/main";
|
||||||
|
const _ = require('underscore');
|
||||||
|
|
||||||
@index({ roleId: 1, id: 1 })
|
@index({ roleId: 1, id: 1 })
|
||||||
@index({ seqId: 1 })
|
@index({ seqId: 1 })
|
||||||
@@ -50,24 +51,35 @@ export default class Item extends BaseModel {
|
|||||||
return items;
|
return items;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async decreaseItems(roleId: string, items: Array<{id: number, count: number, ratio?: number}>, lean = true) {
|
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();
|
let updateItems = new Array<{ id: number, count: number, ratio?: number }>(), hasError: boolean = false, result = new Array();
|
||||||
for (let { id, count, ratio } of items) {
|
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);
|
let rec: ItemType;
|
||||||
if(rec) {
|
if (!ratio) {
|
||||||
result.push({id: rec.id, count: rec.count});
|
rec = await ItemModel.findOneAndUpdate({ roleId, id, count: { $gte: count } }, { $inc: { count: -1 * count } }, { new: true }).lean(lean);
|
||||||
updateItems.push({id, count});
|
} else {
|
||||||
} else {
|
rec = await ItemModel.findOneAndUpdate({ roleId, id }, { $inc: { count: ratio * count } }, { new: true }).lean(lean);
|
||||||
hasError = true; break;
|
}
|
||||||
}
|
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) { // 数量不足
|
if (hasError) { // 数量不足
|
||||||
for(let {id, count} of updateItems) {
|
for (let { id, count, ratio } of updateItems) {
|
||||||
await ItemModel.findOneAndUpdate({roleId, id },{ $inc: { count }}, {new: true}).lean(lean);
|
if (!ratio) ratio = -1;
|
||||||
}
|
await ItemModel.findOneAndUpdate({ roleId, id }, { $inc: { count: -ratio * count } }, { new: true }).lean(lean);
|
||||||
return {hasError: true}
|
}
|
||||||
|
return { hasError: true }
|
||||||
} else {
|
} 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) {
|
// public static async decreaseItems(roleId: string, items: Array<{ id: number, count: number, ratio?: number}>, lean = true) {
|
||||||
|
|||||||
Reference in New Issue
Block a user