🐞 fix(shop): 商店购买数据回滚
This commit is contained in:
@@ -86,11 +86,14 @@ export class ShopHandler {
|
|||||||
if(!costResult) return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH);
|
if(!costResult) return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH);
|
||||||
|
|
||||||
// 次数
|
// 次数
|
||||||
userShop = await UserShopModel.protectedPurchase(roleId, roleName, activityId, dicShopItem, count, seasonNum, maxAlreadyBuyTimes);
|
userShop = await UserShopModel.purchase(roleId, roleName, activityId, dicShopItem, count, seasonNum);
|
||||||
if (!userShop) {
|
if (!userShop || (userShop && userShop.count > totalCanBuyTimes)) {
|
||||||
// rollback 消耗
|
// rollback 消耗
|
||||||
let role = await RoleModel.findByRoleId(roleId);
|
let role = await RoleModel.findByRoleId(roleId);
|
||||||
addItems(roleId, role.roleName, sid, cost, ITEM_CHANGE_REASON.SHOP_PURCHASE);
|
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);
|
return resResult(STATUS.BUY_COUNT_OVER);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -202,7 +202,8 @@ export async function resetTrain(code: string, serverId: number) {
|
|||||||
|
|
||||||
await GuildTrainModel.resetGuildTrain(code);//将开启的练兵场锁定
|
await GuildTrainModel.resetGuildTrain(code);//将开启的练兵场锁定
|
||||||
await unlockTrain(code, guild.trainId);//开启练兵场1级
|
await unlockTrain(code, guild.trainId);//开启练兵场1级
|
||||||
await UserGuildModel.resetTrainUserGuild(code);//重置玩家的挑战次数和购买挑战次数
|
// 只重置上周加入军团的玩家,本周加入的不重置
|
||||||
|
await UserGuildModel.resetTrainUserGuildLastWeekJoinedIn(code);//重置玩家的挑战次数和购买挑战次数
|
||||||
return trainId
|
return trainId
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import BaseModel from './BaseModel';
|
import BaseModel from './BaseModel';
|
||||||
import { index, getModelForClass, prop, DocumentType, Ref } from '@typegoose/typegoose';
|
import { index, getModelForClass, prop, DocumentType, Ref } from '@typegoose/typegoose';
|
||||||
import Role, { RoleType } from './Role';
|
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 { getZeroPointD, nowSeconds, isToday } from '../pubUtils/timeUtil';
|
||||||
import { ARMY } from '../pubUtils/dicParam';
|
import { ARMY } from '../pubUtils/dicParam';
|
||||||
class ActiveRecord {
|
class ActiveRecord {
|
||||||
@@ -242,6 +242,19 @@ export default class UserGuild extends BaseModel {
|
|||||||
return result;
|
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) {
|
public static async donateFund(roleId: string, donateCnt: number, lean = true) {
|
||||||
const result = await UserGuildModel.findOneAndUpdate({ roleId, status: USER_GUILD_STATUS.ON},
|
const result = await UserGuildModel.findOneAndUpdate({ roleId, status: USER_GUILD_STATUS.ON},
|
||||||
{$inc: { donateCnt }}, {new: true}).lean(lean);
|
{$inc: { donateCnt }}, {new: true}).lean(lean);
|
||||||
|
|||||||
@@ -96,30 +96,24 @@ export default class UserShop extends BaseModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 防止并发操作的购买
|
* 修改已购买次数
|
||||||
*
|
*
|
||||||
* @static
|
* @static
|
||||||
* @param {string} roleId
|
* @param {string} roleId
|
||||||
* @param {string} roleName
|
|
||||||
* @param {number} activityId
|
|
||||||
* @param {{ id: number, goodId: number, refreshType: number, shop: number, type: number, createTime?: number }} dicShopItem
|
* @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} seasonNum
|
||||||
* @param {number} maxAlreadyBuyTimes: 最大已买次数,通过这个参数防止并发购买
|
|
||||||
* @return {*}
|
* @return {*}
|
||||||
* @memberof UserShop
|
* @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) {
|
public static async updateCount(roleId: string, dicShopItem: { id: number, goodId: number, refreshType: number, shop: number, type: number, createTime?: number }, inc: number, seasonNum: number) {
|
||||||
let code = genCode(8);
|
let { id, shop, type, createTime = 0 } = dicShopItem;
|
||||||
let timeCondition = this.getRefreshCondition(seasonNum);
|
let timeCondition = this.getRefreshCondition(seasonNum);
|
||||||
let { id, goodId, refreshType, shop, type, createTime = 0 } = dicShopItem;
|
return await UserShopModel.findOneAndUpdate(
|
||||||
|
{ roleId, itemId: id, $or: timeCondition, shop, type, createTime },
|
||||||
let rec: UserShopType = await UserShopModel.findOneAndUpdate(
|
{ $inc: { count: inc } },
|
||||||
{ roleId, itemId: id, $or: timeCondition, shop, type, createTime, count: {$lte: maxAlreadyBuyTimes} },
|
{ new: true }
|
||||||
{ $setOnInsert: { roleName, code, goodId, refreshType, seasonNum }, $inc: { count: inc }, $set: { activityId } },
|
|
||||||
{ new: true, upsert: true }
|
|
||||||
).lean();
|
).lean();
|
||||||
return rec;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async deleteAccount(roleId: string) {
|
public static async deleteAccount(roleId: string) {
|
||||||
|
|||||||
Reference in New Issue
Block a user