From 49679a6bf5e84b0bccfe41720eb3f58bb092eb59 Mon Sep 17 00:00:00 2001 From: mamengke01 <794347210@qq.com> Date: Fri, 8 Jan 2021 18:18:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E9=A1=BA=E5=BA=8F?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../servers/connector/handler/entryHandler.ts | 3 +- shared/db/Item.ts | 102 +++++++++--------- 2 files changed, 53 insertions(+), 52 deletions(-) diff --git a/game-server/app/servers/connector/handler/entryHandler.ts b/game-server/app/servers/connector/handler/entryHandler.ts index bf4e90505..51da11221 100644 --- a/game-server/app/servers/connector/handler/entryHandler.ts +++ b/game-server/app/servers/connector/handler/entryHandler.ts @@ -77,9 +77,10 @@ export class EntryHandler { let heros = await HeroModel.findByRole(role.roleId); let equips = await EquipModel.findbyRole(role.roleId); let items = await ItemModel.findbyRole(role.roleId); - await loginRefresh(role.roleId); await chackFunOpenWhenLogin(role, session.get('funcs')); + await loginRefresh(role.roleId); + role['heros'] = heros.map(cur => returnHeroCeRatio(cur)); role['equips'] = equips; role['consumeGoods'] = items; diff --git a/shared/db/Item.ts b/shared/db/Item.ts index 45aa80159..9612e18e6 100644 --- a/shared/db/Item.ts +++ b/shared/db/Item.ts @@ -1,7 +1,7 @@ import BaseModel from './BaseModel'; import { index, getModelForClass, prop, DocumentType, modelOptions } from '@typegoose/typegoose'; const _ = require('underscore'); -// const Transaction = require('mongoose-transactions'); +const Transaction = require('mongoose-transactions'); @index({ roleId: 1, id: 1 }) @index({ seqId: 1 }) @modelOptions({ schemaOptions: { id: false } }) @@ -50,59 +50,59 @@ 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, 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, upsert: 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, 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 } - } - } - // public static async decreaseItems(roleId: string, items: Array<{ id: number, count: number, ratio?: number}>, lean = true) { - // const transaction = new Transaction(); - // let hasError: boolean = false, result = new Array(); - // try { - // for (let { id, count, ratio } of items) { - // if (ratio) ratio = -1; - // await transaction.update({ roleId, id, count: { $gte: count } }, { $inc: { count: ratio * count } }, { new: true}); - // const rec: ItemType = await transaction.findOneAndUpdate({ roleId, id, count: { $gte: count } }, { $inc: { count: ratio * count } }, { new: true}).lean(lean); - // if (!!rec) { - // result.push({ id: rec.id, count: rec.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, upsert: true }).lean(lean); // } - // await transaction.run(); - // return {hasError, result}; - // } catch (error) { - // await transaction.rollback().catch(console.error); - // transaction.clean(); - // return {error}; + // 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, 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 } // } // } + public static async decreaseItems(roleId: string, items: Array<{ id: number, count: number, ratio?: number}>, lean = true) { + const transaction = new Transaction(); + let hasError: boolean = false, result = new Array(); + try { + for (let { id, count, ratio } of items) { + if (ratio) ratio = -1; + await transaction.update({ roleId, id, count: { $gte: count } }, { $inc: { count: ratio * count } }, { new: true}); + const rec: ItemType = await transaction.findOneAndUpdate({ roleId, id, count: { $gte: count } }, { $inc: { count: ratio * count } }, { new: true}).lean(lean); + if (!!rec) { + result.push({ id: rec.id, count: rec.count }); + } else { + hasError = true; break; + } + } + await transaction.run(); + return {hasError, result}; + } catch (error) { + await transaction.rollback().catch(console.error); + transaction.clean(); + return {error}; + } + } public static async deleteAccount(roleId: string) { let result = await ItemModel.deleteMany({ roleId });