import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';



/**
  * 活动系统 - 大富翁
*/
@index({ roleId: 1, activityId: 1 })

export default class Activity_Monopoly extends BaseModel {
    @prop({ required: true })
    serverId: number;				// 服Id
    @prop({ required: true })
    activityId: number;				// 活动Id
    @prop({ required: true })
    roleId: string;			        // 用户Id
    @prop({ required: true, default: 0 })
    starPosition: number;			// 初始位置
    @prop({ required: true })
    curPosition: number;			// 当前位置
    @prop({ required: true, default: 1 })
    roundIndex: number;			    // 回合数

    //更新坐标
    public static async updatePosition(serverId: number, activityId: number, roleId: string, curPosition: number, roundIndex: number) {
        let result: ActivityMonopolyModelType = await ActivityMonopolyModel.findOneAndUpdate({ serverId, roleId, activityId },
            { $set: { curPosition }, $inc: { roundIndex } }, { upsert: true, new: true }).lean(true);
        return result;
    }

    //查询数据
    public static async findData(serverId: number, activityId: number, roleId: string) {
        let result: ActivityMonopolyModelType = await ActivityMonopolyModel.findOne({ serverId, roleId, activityId }).lean(true);
        return result;
    }

    //删除活动记录
    public static async deleteActivity(serverId: number, activityId: number, roleId: string) {
        await ActivityMonopolyModel.deleteMany({ serverId, roleId, activityId });
    }
}

export const ActivityMonopolyModel = getModelForClass(Activity_Monopoly);

export interface ActivityMonopolyModelType extends Pick<DocumentType<Activity_Monopoly>, keyof Activity_Monopoly> { }
export type ActivityMonopolyModelTypeParam = Partial<ActivityMonopolyModelType>; // 将所有字段变成可选项