Files
ZYZ/shared/db/BossInstance.ts
mamengke01 fb56dd00ec 炼器堂
2021-01-26 16:50:39 +08:00

96 lines
3.9 KiB
TypeScript

import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
import { nowSeconds } from '../pubUtils/timeUtil';
class Rank {
@prop({ required: true })
roleId: string;
@prop({ required: true })
score: number;
@prop({ required: true })
time: number;
}
@index({ guildCode: 1 })
export default class BossInstance extends BaseModel {
@prop({ required: true })
guildCode: string;
@prop({ required: true })
warId: number;
@prop({ required: true })
bossHp: number;
@prop({ required: true, type: Rank, default:[]})
ranks:Array<Rank>;
@prop({ required: true })
startTime: number;
@prop({ required: true, default: 0})
num: number;
@prop({ required: true, default:[], type: String, _id: false})
roleIdRecords: Array<string>; //记录提示过胜利boss关的玩家
@prop({ required: true })
winWarId: number; //记录上一次通关的关卡
@prop({ required: true })
winTime: number; //记录上一次通关的时间
@prop({ required: true, default: 0})
winNum: number;
@prop({ required: true, type: Rank, default:[]})
recordRanks:Array<Rank>;
@prop({ required: true, default: false })
winSettled: boolean; //胜利是否结算过
public static async findBossInstance(guildCode: string, lean = true) {
const bossInstance: BossInstanceType = await BossInstanceModel.findOne({ guildCode }).lean(lean);
return bossInstance;
}
public static async openBossInstance(guildCode: string, bossHp: number, warId: number, lean = true) {
const bossInstance: BossInstanceType = await BossInstanceModel.findOneAndUpdate({ guildCode },{ranks:[], time: nowSeconds(), bossHp, warId, $inc: { recordNum: 1 }}, {new: true, upsert: true}).lean(lean);
return bossInstance;
}
//记录玩家boss通关后首次查看boss关卡
public static async recordRoleIdWhenCheck(guildCode: string, roleId: string, lean = true) {
const bossInstance: BossInstanceType = await BossInstanceModel.findOneAndUpdate({ guildCode }, { $push:{ roleIdRecords: roleId } }, { new: true }).lean(lean);
return bossInstance;
}
public static async pushRecordRanks(guildCode: string, recordRank: Rank, lean = true) {
const bossInstance: BossInstanceType = await BossInstanceModel.findOneAndUpdate({ guildCode }, { $push:{ recordRanks: recordRank } }, { new: true }).lean(lean);
return bossInstance;
}
public static async pushRanks(guildCode: string, rank: Rank, lean = true) {
const bossInstance: BossInstanceType = await BossInstanceModel.findOneAndUpdate({ guildCode }, { $push:{ ranks: rank } }, { new: true }).lean(lean);
return bossInstance;
}
public static async updateBossInstance(guildCode: string, update:BossInstanceTypeParam, lean = true) {
const bossInstance: BossInstanceType = await BossInstanceModel.findOneAndUpdate({ guildCode }, { $set:update }, { new: true }).lean(lean);
return bossInstance;
}
public static async updateBossHp(guildCode: string, hp: number, roleId: string, lean = true) {
const bossInstance: BossInstanceType = await BossInstanceModel.findOneAndUpdate({ guildCode, 'ranks.roleId': roleId, bossHp: { $gte: hp } }, { $inc: { bossHp: - hp ,'ranks.$.score': hp}, }, { new: true }).lean(lean);
return bossInstance;
}
public static async updateRank(guildCode: string, roleId: string, lean = true) {
const bossInstance: BossInstanceType = await BossInstanceModel.findOneAndUpdate({ guildCode, 'ranks.roleId': roleId }, { $set:{ 'ranks.$.time': nowSeconds() } }, { new: true }).lean(lean);
return bossInstance;
}
}
export const BossInstanceModel = getModelForClass(BossInstance);
export interface BossInstanceType extends Pick<DocumentType<BossInstance>, keyof BossInstance> {};
export type BossInstanceTypeParam = Partial<BossInstanceType>; // 将所有字段变成可选项