123 lines
4.7 KiB
TypeScript
123 lines
4.7 KiB
TypeScript
import BaseModel from './BaseModel';
|
||
import { index, getModelForClass, prop, DocumentType, modelOptions } from '@typegoose/typegoose';
|
||
import { RoleModel } from './Role';
|
||
import { genCode } from '../pubUtils/util';
|
||
|
||
/**
|
||
* 宝物
|
||
*/
|
||
@modelOptions({ schemaOptions: { id: false } })
|
||
@index({ roleId: 1, seqId: 1, id: 1 })
|
||
@index({ roleId: 1, quality: 1 })
|
||
@index({ batchCode: 1 })
|
||
@index({ status: 1 })
|
||
|
||
export default class Artifact extends BaseModel {
|
||
|
||
// 主键: artifact,不同形态的宝物分开词条
|
||
@prop({ required: true })
|
||
seqId: number|string; // 唯一id
|
||
|
||
@prop({ required: true })
|
||
roleId: string; // 玩家id
|
||
|
||
@prop({ required: true })
|
||
roleName: string; // 玩家名
|
||
|
||
@prop({ required: true })
|
||
id: number; // 物品id
|
||
|
||
@prop({ required: true })
|
||
artifactId: number; // 宝物id
|
||
|
||
@prop({ required: true, default: 0 })
|
||
lv: number; // 强化等级
|
||
|
||
@prop({ required: true, default: 0 })
|
||
quality: number; // 品质 1-5 蓝紫橙红金
|
||
|
||
@prop({ required: true, default: 0 })
|
||
qualityStage: number; // 品质+n,0开始
|
||
|
||
@prop({ required: true, default: 0 })
|
||
hid: number; // 装备的武将
|
||
|
||
@prop({ required: false })
|
||
batchCode: string; // 一键合成批处理
|
||
|
||
@prop({ required: true, default: 1 })
|
||
status: number; // 装备 1-生成 0-被合成删除
|
||
|
||
public static async findbySeqIds(roleId: string, seqIds: (number|string)[], select?: string) {
|
||
const result: ArtifactModelType[] = await ArtifactModel.find({ roleId, seqId: { $in: seqIds }, status: 1 }).select(select).lean();
|
||
return result;
|
||
}
|
||
|
||
public static async findbySeqId(roleId: string, seqId: number|string, select?: string) {
|
||
const result: ArtifactModelType = await ArtifactModel.findOne({ roleId, seqId, status: 1 }).select(select).lean();
|
||
return result;
|
||
}
|
||
|
||
public static async findbyHids(roleId: string, hids: number[]) {
|
||
const result: ArtifactModelType[] = await ArtifactModel.find({ roleId, hid: { $in: hids }, status: 1 }).lean();
|
||
return result;
|
||
}
|
||
|
||
public static async findbyRole(roleId: string, select = '') {
|
||
const result: ArtifactModelType[] = await ArtifactModel.find({ roleId, status: 1 }).select(select).lean();
|
||
return result;
|
||
}
|
||
|
||
public static async createArtifact(artifactInfo: ArtifactModelUpdate) {
|
||
const seqId = genCode(10);
|
||
|
||
const doc = new ArtifactModel();
|
||
const artifact = Object.assign(doc.toJSON(), {seqId}, artifactInfo);
|
||
return artifact;
|
||
}
|
||
|
||
public static async createArtifacts(roleId: string, artifactInfos: ArtifactModelUpdate[]) {
|
||
let insertInfo: ArtifactModelType[] = [];
|
||
for (let artifactInfo of artifactInfos) {
|
||
let artifact = await this.createArtifact(artifactInfo);
|
||
insertInfo.push(artifact);
|
||
}
|
||
await ArtifactModel.insertMany(insertInfo);
|
||
await RoleModel.increaseArtifact(roleId, insertInfo.length);
|
||
return insertInfo;
|
||
}
|
||
|
||
public static async putOnOrOff(roleId: string, seqId: number|string, hid: number, lv?: number) {
|
||
let update: ArtifactModelUpdate = { hid };
|
||
if(lv != undefined) update.lv = lv;
|
||
let rec: ArtifactModelType = await ArtifactModel.findOneAndUpdate({ roleId, seqId }, { $set: update }, { new: true }).lean();
|
||
return rec;
|
||
}
|
||
|
||
public static async deleteBySeqIds(roleId: string, seqIds: (number|string)[]) {
|
||
let result: ArtifactModelType[] = await ArtifactModel.findbySeqIds(roleId, seqIds);
|
||
let delResult: { n: number, nModified: number, ok: number } = await ArtifactModel.updateMany({ roleId, seqId: { $in: seqIds } }, { $set: { status: 0 } });
|
||
await RoleModel.increaseArtifact(roleId, -1 * delResult.nModified);
|
||
return result;
|
||
}
|
||
|
||
public static async updateInfoBySeqId(roleId: string, seqId: number|string, update: ArtifactModelUpdate) {
|
||
let rec: ArtifactModelType = await ArtifactModel.findOneAndUpdate({ roleId, seqId }, { $set: update }, { new: true }).lean();
|
||
return rec;
|
||
}
|
||
|
||
public static async findByQuality(roleId: string, quality: number) {
|
||
let result: ArtifactModelType[] = await ArtifactModel.find({ roleId, quality, status: 1, hid: 0, qualityStage: 0 }).lean();
|
||
return result;
|
||
}
|
||
|
||
public static async findByBatchCode(batchCode: string) {
|
||
let result: ArtifactModelType[] = await ArtifactModel.find({ batchCode, status: 1 }).lean();
|
||
return result;
|
||
}
|
||
}
|
||
|
||
export const ArtifactModel = getModelForClass(Artifact);
|
||
|
||
export interface ArtifactModelType extends Pick<DocumentType<Artifact>, keyof Artifact> { }
|
||
export type ArtifactModelUpdate = Partial<ArtifactModelType>; // 将所有字段变成可选项
|