Files
ZYZ/shared/db/Skin.ts

73 lines
3.0 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType, modelOptions } from '@typegoose/typegoose';
import { gameData } from '../pubUtils/data';
@modelOptions({ schemaOptions: { id: false } })
@index({ roleId: 1, id: 1 })
@index({ seqId: 1 })
export default class Skin extends BaseModel {
@prop({ required: true, default: '' })
roleId: string; // 角色 id
@prop({ required: true, default: '' })
roleName: string; // 角色名称
@prop({ required: true, default: '' })
id: number; // 皮肤idfashions表的goodId字段
@prop({ required: true, default: '' })
skinId: number; // fashions表的heroId字段
@prop({ required: true, default: '', select: false})
skinName: string; // 皮肤名称
@prop({ required: true, default: 0 })
hid: number; // 原始武将id
public static async findbyRole(roleId: string, select = '') {
const rec: SkinType[] = await SkinModel.find({ roleId }).select(select).lean();
return rec;
}
public static async findbyRoleAndHid(roleId: string, hid: number) {
const rec: SkinType[] = await SkinModel.find({ roleId, hid }).select('id skinId').lean();
return rec;
}
public static async findbyRoleAndHids(roleId: string, hids: number[]) {
const rec: SkinType[] = await SkinModel.find({ roleId, hid: { $in: hids } }).lean();
return rec;
}
public static getInitInfo(hid: number): SkinUpdate {
let dicHero = gameData.hero.get(hid);
let dicFashion = gameData.fashion.get(dicHero.initialSkin)
const doc = new SkinModel();
const update = { ...doc.toJSON(), id: dicFashion.id, skinId: dicFashion.heroId, skinName: dicFashion.name, hid};
delete update._id;
return update
}
public static async insertSkins(roleId: string, roleName: string, skinInfos: SkinUpdate[]) {
let insertInfos: SkinUpdate[] = [];
for(let skinInfo of skinInfos) {
insertInfos.push({ ...skinInfo, roleId, roleName });
}
const items: SkinType[] = await SkinModel.insertMany(insertInfos);
return items;
}
public static async increaseSkin(roleId: string, id: number, info: { roleId: string, roleName: string, id: number, skinName: string, hid: number, skinId: number }, lean = true) {
const doc = new SkinModel();
const setOnInsert = Object.assign(doc.toJSON(), info);
const items: SkinType = await SkinModel.findOneAndUpdate({ roleId, id }, { $setOnInsert: setOnInsert }, { new: true, upsert: true }).lean(lean);
return items;
}
public static async deleteByHero(roleId: string, hid: number) {
const rec: SkinType[] = await SkinModel.deleteMany({ roleId, hid }).lean();
return rec;
}
}
export const SkinModel = getModelForClass(Skin);
export interface SkinType extends Pick<DocumentType<Skin>, keyof Skin> {
id: number;
};
export type SkinUpdate = Partial<SkinType>; // 将所有字段变成可选项