合成藏宝图,消耗品单独开表
This commit is contained in:
+38
-26
@@ -1,47 +1,59 @@
|
||||
import BaseModel from './BaseModel';
|
||||
import { index, getModelForClass, prop } from '@typegoose/typegoose';
|
||||
|
||||
|
||||
@index({ roleId: 1, itemid: 1 })
|
||||
@index({ roleId: 1, id: 1 })
|
||||
@index({ seqId: 1 })
|
||||
export default class Item extends BaseModel {
|
||||
@prop({ required: true })
|
||||
@prop({ required: true, default: '' })
|
||||
roleId: string; // 角色 id
|
||||
@prop({ required: true })
|
||||
@prop({ required: true, default: '' })
|
||||
roleName: string; // 角色名称
|
||||
|
||||
@prop({ required: true })
|
||||
itemid: number; // 道具 id
|
||||
@prop({ required: true })
|
||||
type: number; // 装备类型
|
||||
@prop({ required: true })
|
||||
itemName: string; // 道具名称
|
||||
@prop({ required: true })
|
||||
seqId: number; // 道具表自增 id
|
||||
@prop({ required: true, default: '' })
|
||||
id: number; // 道具 id
|
||||
@prop({ required: true, default: '' })
|
||||
type: number; // 道具类型
|
||||
@prop({ required: true, default: '' })
|
||||
itemName: string; // 道具名称
|
||||
@prop({ required: true, default: 0 })
|
||||
count: number; // 道具数量
|
||||
hid: number; // 将魂:武将id,其他:0
|
||||
@prop({ required: true })
|
||||
count: number; // 道具数量
|
||||
|
||||
|
||||
public static async findbyRole(roleId: string, lean = true) {
|
||||
const items = await ItemModel.find({ roleId }).lean(lean);
|
||||
const items = await ItemModel.find({ roleId }).select('id count type').lean(lean);
|
||||
return items;
|
||||
}
|
||||
|
||||
public static async findbyItemid(itemid: number, roleId: string, lean = true) {
|
||||
const equips = await ItemModel.findOne({ itemid, roleId }).lean(lean);
|
||||
return equips;
|
||||
public static async findbyRoleAndGidAndCount(roleId: string, id: number, count: number, lean = true) {
|
||||
const items = await ItemModel.findOne({ roleId, id, $gte: {count} }).select('id count type').lean(lean);
|
||||
return items;
|
||||
}
|
||||
|
||||
public static async changeItemCount(seqId: number, count: number, lean = true) {
|
||||
const equips = await ItemModel.findOneAndUpdate({ seqId }, {$inc:{count}}, {upsert: true, new: true}).lean(lean);
|
||||
return equips;
|
||||
}
|
||||
|
||||
public static async createItem(itemInfo: {roleId: string, roleName: string, itemid: number, seqId: number, itemName: string, count: number, type: number}, lean = true) {
|
||||
public static async increaseItem(roleId: string, id: number, count: number, itemInfo: {roleId: string, roleName: string, id: number, itemName: string, type: number, hid?: number }, lean = true) {
|
||||
const doc = new ItemModel();
|
||||
const update = Object.assign(doc.toJSON(), itemInfo);
|
||||
const item = await ItemModel.findOneAndUpdate({ seqId: itemInfo.seqId }, update, {upsert: true, new: true}).lean(lean);
|
||||
return item;
|
||||
const setOnInsert = Object.assign(doc.toJSON(), itemInfo);
|
||||
const equips = await ItemModel.findOneAndUpdate({roleId, id },{$setOnInsert: setOnInsert, $inc: { count }}, {new: true, upsert: true}).lean(lean);
|
||||
return equips;
|
||||
}
|
||||
|
||||
public static async decreaseItems(roleId: string, items: Array<{id: number, count: number}>, lean = true) {
|
||||
let wrongItems = new Array<{id: number, count: number}>();
|
||||
for(let {id, count} of items) {
|
||||
const rec = await ItemModel.findOneAndUpdate({roleId, id, count: {$gte: count} },{ $inc: { count: -1 * count }}, {new: true}).lean(lean);
|
||||
if(!rec) {
|
||||
wrongItems.push({id, count}); break;
|
||||
}
|
||||
}
|
||||
if(wrongItems.length > 0) { // 数量不足
|
||||
for(let {id, count} of wrongItems) {
|
||||
await ItemModel.findOneAndUpdate({roleId, id },{ $inc: { count }}, {new: true}).lean(lean);
|
||||
}
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
public static async deleteAccount(roleId: string, lean = true) {
|
||||
|
||||
Reference in New Issue
Block a user