Files
ZYZ/shared/db/GVGCity.ts

58 lines
2.0 KiB
TypeScript

import BaseModel from "./BaseModel";
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
@index({ configId: 1, cityId: 1 })
// GVGCity 类,继承自 BaseModel
export default class GVGCity extends BaseModel {
@prop({ required: true, default: 1 })
configId: number; // config唯一id
@prop({ required: true })
cityId: number; // 城池id
@prop({ required: true, default: [] })
leagueCodes: string[]; // 联军
@prop({ required: false })
guardLeague: string; // 占领的联军
@prop({ required: false })
guardLeagueName: string; // 占领的联军
@prop({ required: true, default: 0 })
userCnt: number; // 城池人数
@prop({ required: true, default: 0 })
teamCnt: number; // 城池队伍数
// 创建城市
public static async createCity(configId: number, cityId: number) {
let doc = new GVGCityModel();
let update = Object.assign(doc.toJSON(), { configId, cityId});
const city: GVGCityType | null = await GVGCityModel.findOneAndUpdate({ configId, cityId }, { $setOnInsert: update }, { upsert: true, new: true }).lean();
return city;
}
// 通过 cityId 获取城市
public static async getCityByCityId(configId: number, cityId: number) {
const city: GVGCityType | null = await GVGCityModel.findOne({ configId, cityId }).lean();
return city;
}
// 更新城市字段
public static async updateCityUser(configId: number, cityId: number, userInc: number, teamInc: number) {
const city: GVGCityType | null = await GVGCityModel.findOneAndUpdate({ configId, cityId}, { $inc: {userCnt: userInc, teamCnt: teamInc} }, { new: true }).lean();
return city;
}
}
export const GVGCityModel = getModelForClass(GVGCity);
export interface GVGCityType extends Pick<DocumentType<GVGCity>, keyof GVGCity> {
id: number;
};
export type GVGCityUpdate = Partial<GVGCityType>; // 将所有字段变成可选项