89 lines
2.8 KiB
TypeScript
89 lines
2.8 KiB
TypeScript
import { dbconn } from 'decorators/dbconn'
|
|
import { getModelForClass, index, modelOptions, mongoose, pre, prop } from '@typegoose/typegoose'
|
|
import { Severity } from '@typegoose/typegoose/lib/internal/constants'
|
|
import { BaseModule } from './Base'
|
|
import { convert } from 'zutils/utils/number.util'
|
|
import { BASE52_ALPHABET } from 'common/Constants'
|
|
|
|
export enum ChestStatusEnum {
|
|
LOCKED = 0,
|
|
NORMAL = 1,
|
|
NOT_CLAIMED = 2,
|
|
OPENED = 9,
|
|
}
|
|
/**
|
|
* 活动宝箱
|
|
*/
|
|
@dbconn()
|
|
@index({ user: 1, activity: 1 }, { unique: false })
|
|
@index({ shareCode: 1, activity: 1 }, { unique: true })
|
|
@modelOptions({
|
|
schemaOptions: { collection: 'activity_box', timestamps: true },
|
|
options: { allowMixed: Severity.ALLOW },
|
|
})
|
|
@pre<ActivityChestClass>('save', async function () {
|
|
if (!this.shareCode) {
|
|
// 取ObjectId的time和inc段,
|
|
// 将time段倒序(倒序后, 如果以0开始, 则移除0, 随机拼接一个hex字符), 然后拼接inc段, 再转换成52进制
|
|
let timeStr = this.id.slice(0, 8).split('').reverse().join('')
|
|
if (timeStr.indexOf('0') === 0) {
|
|
let randomStr = convert({ numStr: ((Math.random() * 51) | (0 + 1)) + '', base: 10, to: 52 })
|
|
timeStr = randomStr + timeStr.slice(1)
|
|
}
|
|
let shortId = timeStr + this.id.slice(-6)
|
|
// console.log(shortId)
|
|
this.shareCode = convert({ numStr: shortId, base: 16, to: 52, alphabet: BASE52_ALPHABET })
|
|
// console.log(this.id, this.shareCode)
|
|
}
|
|
})
|
|
export class ActivityChestClass extends BaseModule {
|
|
// 盒子的分享码
|
|
@prop()
|
|
public shareCode: string
|
|
// 0 锁定, 1 未开启 9 已开启
|
|
@prop({ enum: ChestStatusEnum, default: ChestStatusEnum.LOCKED })
|
|
public status: ChestStatusEnum
|
|
@prop()
|
|
public user: string
|
|
@prop()
|
|
public activity: string
|
|
// 品级
|
|
@prop({ default: 1 })
|
|
public level: number
|
|
// 最大助力次数
|
|
@prop()
|
|
public maxBounsCount: number
|
|
// 助力积分配置[v1, v2, v3]
|
|
@prop({ type: () => [Number], default: [] })
|
|
public bounsCfg: number[]
|
|
// 基础积分
|
|
@prop({ default: 0 })
|
|
public scoreInit: number
|
|
// 助力积分
|
|
@prop({ default: 0 })
|
|
public scoreBonus: number
|
|
|
|
@prop({ type: () => [String], default: [] })
|
|
public bonusUsers: string[]
|
|
@prop({ type: () => [Number], default: [] })
|
|
public bonusScores: number[]
|
|
// 获得的额外奖励
|
|
@prop({ type: mongoose.Schema.Types.Mixed, default: [] })
|
|
public items: any
|
|
|
|
public toJson() {
|
|
return {
|
|
// @ts-ignore
|
|
id: this.id,
|
|
stat: this.status,
|
|
shareCode: this.shareCode,
|
|
level: this.level,
|
|
maxBounsCount: this.maxBounsCount,
|
|
scoreInit: this.scoreInit,
|
|
scoreBonus: this.scoreBonus,
|
|
bounsCount: this.bonusUsers.length,
|
|
}
|
|
}
|
|
}
|
|
export const ActivityChest = getModelForClass(ActivityChestClass, { existingConnection: ActivityChestClass['db'] })
|