寻宝:情谊助战相关逻辑;更新物品表-加入情谊点
This commit is contained in:
41
shared/db/FriendPoint.ts
Normal file
41
shared/db/FriendPoint.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import BaseModel from './BaseModel';
|
||||
import { index, getModelForClass, prop } from '@typegoose/typegoose';
|
||||
|
||||
/**
|
||||
* 情谊点记录
|
||||
*/
|
||||
@index({ roleId: 1, createdAt: -1 })
|
||||
|
||||
export default class FriendPoint extends BaseModel {
|
||||
@prop({ required: true, default: '' })
|
||||
roleId: string; // 角色 id
|
||||
@prop({ required: true, default: '' })
|
||||
roleName: string; // 角色名称
|
||||
@prop({ required: true, default: 0 })
|
||||
cnt: number; // 当天获取的点数
|
||||
|
||||
/**
|
||||
* @description 更新每天的情谊点获取情况,cnt 有可能超过当日最大值,可根据返回值判断
|
||||
* @static
|
||||
* @param {string} roleId
|
||||
* @param {string} roleName
|
||||
* @param {number} cntInc
|
||||
* @param {number} maxPerDay
|
||||
* @param {boolean} [lean=true]
|
||||
* @memberof FriendPoint
|
||||
*/
|
||||
public static async updatePointToday(roleId: string, roleName: string, cntInc: number, maxPerDay: number, lean = true) {
|
||||
const curTime = new Date((new Date()).setHours(0, 0, 0, 0));
|
||||
// 当 oldCnt + cntInc > maxPerDay 时需计算实际可以获得的点数:cntInc - (newCnt - maxPerDay)
|
||||
const rec = await FriendPointModel.findOneAndUpdate({roleId, roleName, createdAt: {$gte: curTime}, cnt: {$lt: maxPerDay}}, {$inc: {cnt: cntInc}}, {upsert: true, new: true}).lean(lean);
|
||||
return rec;
|
||||
}
|
||||
|
||||
public static async getFrdPointRecToday(roleId: string, lean = true) {
|
||||
const curTime = new Date((new Date()).setHours(0, 0, 0, 0));
|
||||
const rec = await FriendPointModel.findOne({roleId, createdAt: {$gte: curTime}}).lean(lean);
|
||||
return rec;
|
||||
}
|
||||
}
|
||||
|
||||
export const FriendPointModel = getModelForClass(FriendPoint);
|
||||
Reference in New Issue
Block a user