🐞 fix(guild): 修复军团退出重进数据重置问题

This commit is contained in:
dingchaolin
2023-02-27 20:31:12 +08:00
parent b75f21fcf3
commit 6f3859df16
4 changed files with 75 additions and 6 deletions

View File

@@ -2,7 +2,7 @@ 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 { getZeroPointD, nowSeconds } from '../pubUtils/timeUtil';
import { getZeroPointD, nowSeconds, isToday } from '../pubUtils/timeUtil';
import { ARMY } from '../pubUtils/dicParam';
class ActiveRecord {
@prop({ required: true })
@@ -145,12 +145,42 @@ export default class UserGuild extends BaseModel {
public static async createUserGuild(guildCode: string, role: RoleType, isLeader: boolean) {
const lastGuild = await UserGuildModel.findMyLastGuild(role.roleId, '+refTimeDaily');
let { receiveBoxs = [], donateCnt = 0, receivedActive = [], encourageCnt = 0, bossChallengeCnt = 0, receivedWishPool = [], refTimeDaily, refBossTime, trainCount, buyTrainCount, trainTime } = lastGuild||{};
let { receiveBoxs = [], donateCnt = 0, receivedActive = [], encourageCnt = 0, bossChallengeCnt = 0, receivedWishPool = [], wishGoods = [], wishDntCnt = 0, refTimeDaily, refBossTime, trainCount, buyTrainCount, trainTime } = lastGuild||{};
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, receiveBoxs, donateCnt, receivedActive, encourageCnt, bossChallengeCnt, receivedWishPool, refTimeDaily, refBossTime, trainCount, buyTrainCount, trainTime });
let update = Object.assign(doc.toJSON(), { guildCode, roleId: role.roleId, role: role._id, job, auth, receiveBoxs, donateCnt, receivedActive, encourageCnt, bossChallengeCnt, receivedWishPool, wishGoods, wishDntCnt, refTimeDaily, refBossTime, trainCount, buyTrainCount, trainTime });
// 【退出军团】 和 【再加入军团】在同一天
if (isToday(role.quitGuildTime)) {
// 同一个军团: 所有数据保留; 不同军团: 许愿池数据清空,当天不允许许愿,其他数据保留
if (guildCode !== lastGuild?.guildCode) {
update.wishGoods = [];
update.receivedWishPool = [];
update.wishDntCnt = 0;
}
} else {
// 【退出军团】 和 【再加入军团】不在同一天; 此处包含: 首次加入军团(lastGuild == null)
if (lastGuild) {
// 之前加入过军团 - 清空之前的数据
update.receiveBoxs = [];
update.donateCnt = 0;
update.receivedActive = [];
update.encourageCnt = 0;
update.bossChallengeCnt = 0;
update.receivedWishPool = [];
update.wishGoods = [];
update.wishDntCnt = 0;
update.refTimeDaily = null;
update.refBossTime = null;
update.trainCount = 0;
update.buyTrainCount = 0;
update.trainTime = 0;
}
}
delete update._id;
const result: UserGuildType = await UserGuildModel.findOneAndUpdate({ roleId: role.roleId, guildCode, status: USER_GUILD_STATUS.ON }, { $set: update }, { upsert: true, new: true })