import { Application, BackendSession } from 'pinus'; import { resResult } from '../../../pubUtils/util'; import { STATUS, ACTIVITY_RESOURCES_TYPE, ACTIVITY_TYPE } from '../../../consts'; import { getPlayerTreasureHuntData, getTreasureHuntData, getPlayerTreasureHuntShopData } from '../../../services/treasureHuntService'; import { ActivityTreasureHuntShopModel } from '../../../db/ActivityTreasureHuntShop'; import { handleCost } from '../../../services/rewardService'; import { addReward, stringToConsumeParam, stringToRewardParam } from '../../../services/giftPackageService'; import { RewardParam } from '../../../domain/activityField/rewardField'; export default function (app: Application) { return new TreasureHuntHandler(app); } export class TreasureHuntHandler { constructor(private app: Application) { } /************************寻宝骑兵活动****************************/ /** * @description 获取寻宝骑兵活动的数据 * @param {BackendSession} session * @memberof TreasureHuntHandler */ async getTreasureHuntActivity(msg: { activityId: number }, session: BackendSession) { const { activityId } = msg; const roleId = session.get('roleId'); const serverId = session.get('serverId'); let { huntActivityId, huntBeginTime, huntEndTime, huntRoundIndex, activityData } = await getTreasureHuntData(serverId); if (!activityData) { return resResult(STATUS.ACTIVITY_MISSING, {}); } let playerData = await getPlayerTreasureHuntData(activityId, serverId, roleId, huntRoundIndex, huntBeginTime, huntEndTime); if (!playerData) return resResult(STATUS.ACTIVITY_MISSING); return resResult(STATUS.SUCCESS, playerData); } /** * @description 寻宝骑兵购买每日商店中的商品 * @param {BackendSession} session * @memberof TreasureHuntHandler */ async buyGoods(msg: { activityId: number, cellIndex: number }, session: BackendSession) { const { activityId, cellIndex } = msg; const roleId = session.get('roleId'); const serverId = session.get('serverId'); const sid = session.get('sid'); const roleName = session.get('roleName'); const funcs: number[] = session.get('funcs'); let { huntActivityId, huntBeginTime, huntEndTime, huntRoundIndex, activityData } = await getTreasureHuntData(serverId); if (!activityData) { return resResult(STATUS.ACTIVITY_MISSING, {}); } if (activityId !== huntActivityId) { return resResult(STATUS.ACTIVITY_MISSING, {}); } let playerData = await getPlayerTreasureHuntShopData(activityId, serverId, roleId, huntRoundIndex, huntBeginTime, huntEndTime,); if (!playerData) return resResult(STATUS.ACTIVITY_MISSING); let item = playerData.shop.getItem(cellIndex); if (!item) { return resResult(STATUS.ACTIVITY_MISSING, {}); } if (item.price >= 0) { return resResult(STATUS.ACTIVITY_NEED_PAY, {}); } if (item.buyCount >= item.countMax) { return resResult(STATUS.ACTIVITY_MAX_COUNT, {}); } let consumeStr = item.getConsume(); let consume = stringToConsumeParam(consumeStr) let resourceResult = await handleCost(roleId, sid, consume); if (!resourceResult) return resResult(STATUS.ROLE_MATERIAL_NOT_ENOUGH); //添加购买记录 await ActivityTreasureHuntShopModel.buyShopRecord(activityId, roleId, huntRoundIndex, playerData.todayIndex, cellIndex); let rewardParamArr: Array = stringToRewardParam(item.reward); let result = await addReward(roleId, roleName, sid, serverId, funcs, rewardParamArr) item.buyCount += 1; return resResult(STATUS.SUCCESS, Object.assign(result, { param: { activityId, cellIndex }, item: item, })); } }