157 lines
6.2 KiB
TypeScript
157 lines
6.2 KiB
TypeScript
import BaseModel from './BaseModel';
|
|
import { index, getModelForClass, prop, DocumentType, Ref } from '@typegoose/typegoose';
|
|
import Role, { RoleType } from './Role';
|
|
import { GUILD_AUTH, USER_GUILD_STATUS, GUILD_JOB } from '../consts';
|
|
import { nowSeconds } from '../pubUtils/timeUtil';
|
|
import { ARMY } from '../pubUtils/dicParam';
|
|
class ActiveRecord {
|
|
@prop({ required: true })
|
|
id: number;
|
|
@prop({ required: true })
|
|
count: number;
|
|
}
|
|
|
|
@index({ roleId: 1 })
|
|
export default class UserGuild extends BaseModel {
|
|
@prop({ required: true })
|
|
guildCode: string;
|
|
|
|
@prop({ required: true })
|
|
roleId: string;
|
|
|
|
@prop({ required: true })
|
|
role: Ref<Role>;
|
|
|
|
@prop({ required: true, default: GUILD_AUTH.MEMBER, enum: GUILD_AUTH })
|
|
auth: number;
|
|
|
|
@prop({ required: true, default: GUILD_JOB.SHIBING, enum: GUILD_JOB })
|
|
job: number;
|
|
|
|
@prop({ required: true, default: 0 })
|
|
activeWeekly: number;
|
|
|
|
@prop({ required: true, default: 0 })
|
|
activeDaily: number;
|
|
|
|
@prop({ required: true, default: nowSeconds() })
|
|
activeUpdateTime: number;
|
|
|
|
@prop({ required: true, default: USER_GUILD_STATUS.ON, enum: USER_GUILD_STATUS})
|
|
status: number;
|
|
|
|
@prop({ required: true, type: ActiveRecord, default: [], _id: false })
|
|
activeRecord: ActiveRecord[];
|
|
|
|
@prop({ required: true, type: Number, default: [] })
|
|
receivedActive: number[];
|
|
|
|
@prop({ required: true, default: new Date(), select: false })
|
|
refTimeDaily: Date;
|
|
|
|
@prop({ required: true, default: 0 })
|
|
trainCount: number;//每日挑战训练场的次数
|
|
|
|
@prop({ required: true, default: 0 })
|
|
buyTrainCount: number;//每日挑战训练场购买次数
|
|
|
|
@prop({ required: true, default: 0 })
|
|
trainTime: number;//上次刷新挑战训练场次数的时间
|
|
|
|
@prop({ required: true, default: [] })
|
|
trainRewards: Array<number>;//领取过的进阶等级
|
|
|
|
|
|
public static async getMyAuth(roleId: string, guildCode?: string, userGuild?: UserGuildType) {
|
|
let myGuild: UserGuildType;
|
|
if(!userGuild) {
|
|
myGuild = await this.getMyGuild(roleId, 'auth');
|
|
} else {
|
|
myGuild = userGuild;
|
|
}
|
|
|
|
if(guildCode && userGuild && userGuild.guildCode != guildCode) {
|
|
return GUILD_AUTH.OTHERS;
|
|
}
|
|
|
|
return myGuild?myGuild.auth: GUILD_AUTH.OTHERS;
|
|
}
|
|
|
|
public static async getMyGuild(roleId: string, select?: string) {
|
|
|
|
const myGuild: UserGuildType = await UserGuildModel.findOne({ roleId, status: USER_GUILD_STATUS.ON })
|
|
.select(select).lean();
|
|
return myGuild;
|
|
}
|
|
|
|
public static async getListByGuild(guildCode: string, select?: string, sort: { auth?: number, activeWeekly?: number, activeUpdateTime?: number } = {auth: 1}) {
|
|
const userGuilds: UserGuildType[] = await UserGuildModel.find({ guildCode, status: USER_GUILD_STATUS.ON })
|
|
.select(select)
|
|
.sort(sort)
|
|
.populate('role', 'roleId roleName ce headHid sHid lv title loginTime', 'Role')
|
|
.lean();
|
|
return userGuilds;
|
|
}
|
|
|
|
public static async findTopActive(guildCode: string, select?: string) {
|
|
const userGuilds: UserGuildType = await UserGuildModel.findOne({ guildCode, status: USER_GUILD_STATUS.ON, auth: { $ne: GUILD_AUTH.LEADER } })
|
|
.sort({ activeWeekly: -1, activeUpdateTime: 1 })
|
|
.select(select)
|
|
.populate('role', 'roleId roleName ce headHid sHid lv title loginTime', 'Role')
|
|
.lean();
|
|
return userGuilds;
|
|
}
|
|
|
|
public static async createUserGuild(guildCode: string, role: RoleType, isLeader: boolean) {
|
|
const doc = new UserGuildModel();
|
|
let job = isLeader? GUILD_JOB.DAJIANGJUN: GUILD_JOB.SHIBING;
|
|
let auth = isLeader? GUILD_AUTH.LEADER: GUILD_AUTH.MEMBER;
|
|
const update = Object.assign(doc.toJSON(), { guildCode, roleId: role.roleId, role: role._id, job, auth });
|
|
delete update._id;
|
|
const result: UserGuildType = await UserGuildModel.findOneAndUpdate({ roleId: role.roleId, guildCode, status: USER_GUILD_STATUS.ON }, update, { upsert: true, new: true })
|
|
.select('activeDaily activeRecord activeWeekly activeUpdateTime job auth receivedActive')
|
|
.lean();
|
|
|
|
return result;
|
|
}
|
|
|
|
public static async dismiss(guildCode: string) {
|
|
const result = await UserGuildModel.updateMany({guildCode}, {status: USER_GUILD_STATUS.DISMISSED});
|
|
|
|
return result;
|
|
}
|
|
|
|
// 玩家退出公会
|
|
public static async quit(guildCode: string, roleId: string) {
|
|
const result = await UserGuildModel.findOneAndUpdate({ guildCode, roleId, status: USER_GUILD_STATUS.ON }, {status: USER_GUILD_STATUS.QUIT}, {new: true});
|
|
return result;
|
|
}
|
|
|
|
public static async updateInfo(roleId: string, update: UserGuildUpdateParam, incParam: { activeDaily?: number, activeWeekly?: number, trainCount?: number }, select?: string) {
|
|
const result: UserGuildType = await UserGuildModel.findOneAndUpdate({ roleId, status: USER_GUILD_STATUS.ON }, { $set: update, $inc: incParam }, { new: true }).select(select).lean();
|
|
return result;
|
|
}
|
|
|
|
public static async receiveTrainRewards(roleId: string, trainId: number, lean = true) {
|
|
const result = await UserGuildModel.findOneAndUpdate({ roleId, status: USER_GUILD_STATUS.ON},
|
|
{$push:{trainRewards: trainId} },{new: true}).lean(lean);
|
|
return result;
|
|
}
|
|
|
|
public static async addTrainCount(roleId: string, trainCount: number, lean = true) {
|
|
const result = await UserGuildModel.findOneAndUpdate({ roleId, status: USER_GUILD_STATUS.ON},
|
|
{$inc: {trainCount, buyTrainCount: trainCount}}, {new: true}).lean(lean);
|
|
return result;
|
|
}
|
|
|
|
public static async resetTrainUserGuild(guildCode: string) {
|
|
const result = await UserGuildModel.updateMany({ guildCode }, {$set: { trainCount: ARMY.ARMY_TRAIN_BUYTIMES, buyTrainCount: 0, trainTime: nowSeconds(), trainRewards: [] }});
|
|
return result;
|
|
}
|
|
}
|
|
|
|
export const UserGuildModel = getModelForClass(UserGuild);
|
|
|
|
export interface UserGuildType extends Pick<DocumentType<UserGuild>, keyof UserGuild> { };
|
|
export type UserGuildUpdateParam = Partial<UserGuildType>; // 将所有字段变成可选项
|