🐞 fix(shop): 商店购买数据回滚

This commit is contained in:
dingchaolin
2023-03-06 20:13:35 +08:00
parent b15639b991
commit 10df130ae5
4 changed files with 29 additions and 18 deletions

View File

@@ -86,11 +86,14 @@ export class ShopHandler {
if(!costResult) return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH);
// 次数
userShop = await UserShopModel.protectedPurchase(roleId, roleName, activityId, dicShopItem, count, seasonNum, maxAlreadyBuyTimes);
if (!userShop) {
userShop = await UserShopModel.purchase(roleId, roleName, activityId, dicShopItem, count, seasonNum);
if (!userShop || (userShop && userShop.count > totalCanBuyTimes)) {
// rollback 消耗
let role = await RoleModel.findByRoleId(roleId);
addItems(roleId, role.roleName, sid, cost, ITEM_CHANGE_REASON.SHOP_PURCHASE);
// rollback 购买次数
await UserShopModel.updateCount(roleId, dicShopItem, -count, seasonNum)
return resResult(STATUS.BUY_COUNT_OVER);
}

View File

@@ -202,7 +202,8 @@ export async function resetTrain(code: string, serverId: number) {
await GuildTrainModel.resetGuildTrain(code);//将开启的练兵场锁定
await unlockTrain(code, guild.trainId);//开启练兵场1级
await UserGuildModel.resetTrainUserGuild(code);//重置玩家的挑战次数和购买挑战次数
// 只重置上周加入军团的玩家,本周加入的不重置
await UserGuildModel.resetTrainUserGuildLastWeekJoinedIn(code);//重置玩家的挑战次数和购买挑战次数
return trainId
}
/**

View File

@@ -1,7 +1,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 { GUILD_AUTH, USER_GUILD_STATUS, GUILD_JOB, SHOP_REFRESH_TYPE } from '../consts';
import { getZeroPointD, nowSeconds, isToday } from '../pubUtils/timeUtil';
import { ARMY } from '../pubUtils/dicParam';
class ActiveRecord {
@@ -242,6 +242,19 @@ export default class UserGuild extends BaseModel {
return result;
}
/**
* 重置上周加入的队员的试炼数据(本周加入的不能重置,否则会多领奖励)
*
* @static
* @param {string} guildCode
* @return {*}
* @memberof UserGuild
*/
public static async resetTrainUserGuildLastWeekJoinedIn(guildCode: string) {
const result = await UserGuildModel.updateMany({ guildCode, createdAt: {$lt: getZeroPointD(SHOP_REFRESH_TYPE.WEEKLY)} }, {$set: { trainCount: ARMY.ARMY_TRAIN_BUYTIMES, buyTrainCount: 0, trainTime: nowSeconds(), trainRewards: [] }});
return result;
}
public static async donateFund(roleId: string, donateCnt: number, lean = true) {
const result = await UserGuildModel.findOneAndUpdate({ roleId, status: USER_GUILD_STATUS.ON},
{$inc: { donateCnt }}, {new: true}).lean(lean);

View File

@@ -96,30 +96,24 @@ export default class UserShop extends BaseModel {
}
/**
* 防止并发操作的购买
* 修改已购买次数
*
* @static
* @param {string} roleId
* @param {string} roleName
* @param {number} activityId
* @param {{ id: number, goodId: number, refreshType: number, shop: number, type: number, createTime?: number }} dicShopItem
* @param {number} inc
* @param {number} inc: 变化次数
* @param {number} seasonNum
* @param {number} maxAlreadyBuyTimes: 最大已买次数,通过这个参数防止并发购买
* @return {*}
* @memberof UserShop
*/
public static async protectedPurchase(roleId: string, roleName: string, activityId: number, dicShopItem: { id: number, goodId: number, refreshType: number, shop: number, type: number, createTime?: number }, inc: number, seasonNum: number, maxAlreadyBuyTimes: number) {
let code = genCode(8);
public static async updateCount(roleId: string, dicShopItem: { id: number, goodId: number, refreshType: number, shop: number, type: number, createTime?: number }, inc: number, seasonNum: number) {
let { id, shop, type, createTime = 0 } = dicShopItem;
let timeCondition = this.getRefreshCondition(seasonNum);
let { id, goodId, refreshType, shop, type, createTime = 0 } = dicShopItem;
let rec: UserShopType = await UserShopModel.findOneAndUpdate(
{ roleId, itemId: id, $or: timeCondition, shop, type, createTime, count: {$lte: maxAlreadyBuyTimes} },
{ $setOnInsert: { roleName, code, goodId, refreshType, seasonNum }, $inc: { count: inc }, $set: { activityId } },
{ new: true, upsert: true }
return await UserShopModel.findOneAndUpdate(
{ roleId, itemId: id, $or: timeCondition, shop, type, createTime },
{ $inc: { count: inc } },
{ new: true }
).lean();
return rec;
}
public static async deleteAccount(roleId: string) {