Files
ZYZ/game-server/app/servers/activity/handler/refreshShopHandler.ts
2021-07-16 11:29:26 +08:00

93 lines
3.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { Application, BackendSession, HandlerService, } from 'pinus';
import { deltaDays, resResult } from '../../../pubUtils/util';
import { ACTIVITY_TYPE, STATUS } from '../../../consts';
import { getRefreshShopActivity, getPlayerRefreshShopData } from '../../../services/activity/refreshShopService';
import { addReward, stringToConsumeParam, stringToRewardParam } from '../../../services/activity/giftPackageService';
import { ActivityRefreshShopModel } from '../../../db/ActivityRefreshShop';
import { RoleModel } from '../../../db/Role';
import { handleCost } from '../../../services/rewardService';
import moment = require('moment');
export default function (app: Application) {
new HandlerService(app, {});
return new RefreshShopHandler(app);
}
export class RefreshShopHandler {
constructor(private app: Application) {
}
/************************通用的刷新商店分页可刷新限制购买次数支持rmb与资源兑换****************************/
/**
* @description 获取商店数据
* @param {{ activityId:number}} msg
* @param {BackendSession} session
* @memberof RefreshShopHandler
*/
async getRefreshShopActivity(msg: { activityId: number }, session: BackendSession) {
const { activityId } = msg;
const roleId = session.get('roleId');
const serverId = session.get('serverId');
let playerData = await getPlayerRefreshShopData(activityId, serverId, roleId);
if (!playerData) {
return resResult(STATUS.ACTIVITY_MISSING);
}
return resResult(STATUS.SUCCESS, { playerData });
}
/**
* @description 购买
* @param {{ activityId: number, roundIndex: number, id: number, pageIndex: number}} msg
* @param {BackendSession} session
* @memberof RefreshShopHandler
*/
async buyGood(msg: { activityId: number, roundIndex: number, id: number, pageIndex: number }, session: BackendSession) {
const { activityId, roundIndex, id, pageIndex } = 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 playerData = await getPlayerRefreshShopData(activityId, serverId, roleId);
if (!playerData) {
return resResult(STATUS.ACTIVITY_MISSING);
}
if (playerData.roundIndex != roundIndex) {
return resResult(STATUS.ACTIVITY_EXPIRE);
}
let item = playerData.findItem(id, pageIndex);
if (!item) {
return resResult(STATUS.ACTIVITY_ID_ERROR);
}
if (item.countMax > 0 && item.buyCount >= item.countMax) {
return resResult(STATUS.ACTIVITY_MAX_COUNT);
}
if (item.price > 0) {
return resResult(STATUS.ACTIVITY_NEED_PAY);
}
//检查资源
let consume = stringToConsumeParam(item.consume)
let consumeResult = await handleCost(roleId, sid, consume);
if (!consumeResult) return resResult(STATUS.ROLE_MATERIAL_NOT_ENOUGH);
let rewardArray = stringToRewardParam(item.reward)
let result = await addReward(roleId, roleName, sid, serverId, funcs, rewardArray);
await ActivityRefreshShopModel.addRecord(activityId, roleId, roundIndex, pageIndex, id);
item.buyCount += 1;
return resResult(STATUS.SUCCESS, Object.assign(result, {
param: { activityId, roundIndex, id },
item: item
}));
}
}