96 lines
4.7 KiB
TypeScript
96 lines
4.7 KiB
TypeScript
import { Application, BackendSession } from 'pinus';
|
|
import { deltaDays, resResult } from '../../../pubUtils/util';
|
|
import { STATUS, ACTIVITY_RESOURCES_TYPE, ACTIVITY_TYPE, CURRENCY_BY_TYPE, CURRENCY_TYPE } from '../../../consts';
|
|
import { SelfServiceShopData, SelfServiceShopItem, SelfServiceShopItemInfo } from '../../../domain/activityField/selfServiceShopField';
|
|
import { addItems, handleCost } from '../../../services/rewardService';
|
|
import { ActivitySelfServiceShopModel, ActivitySelfServiceShopModelType } from '../../../db/ActivitySelfServiceShop';
|
|
import { ActivitySelfServiceGoodsModel, ActivitySelfServiceGoodsModelType } from '../../../db/ActivitySelfServiceGoods';
|
|
import moment = require('moment');
|
|
import Activity, { ActivityModel, ActivityModelType } from '../../../db/Activity';
|
|
import { addSelfServiceShopGiftReward, getActivityData, getPlayerActivityData } from '../../../services/selfServiceShopActivityService';
|
|
|
|
export default function (app: Application) {
|
|
return new SelfServiceShopHandler(app);
|
|
}
|
|
|
|
export class SelfServiceShopHandler {
|
|
constructor(private app: Application) {
|
|
}
|
|
|
|
/************************自助商店****************************/
|
|
|
|
/**
|
|
* @description 获取自助商店数据
|
|
* @param {{ }} msg
|
|
* @param {BackendSession} session
|
|
* @memberof SelfServiceShopHandler
|
|
*/
|
|
async getSelfServiceShopActivity(msg: {}, session: BackendSession) {
|
|
const { } = msg;
|
|
const roleId = session.get('roleId');
|
|
const serverId = session.get('serverId');
|
|
|
|
let playerData = await getActivityData(serverId, roleId)
|
|
if (!playerData) return resResult(STATUS.ACTIVITY_MISSING);
|
|
let playerGoods = await ActivitySelfServiceGoodsModel.findData(playerData.activityId, roleId, playerData.roundIndex, true);
|
|
return resResult(STATUS.SUCCESS, { playerData, playerGoods });
|
|
}
|
|
|
|
/**
|
|
* @description 购买礼包
|
|
* @param {{ activityId: number, roundIndex: number, index: number, cellIndex: number}} msg
|
|
* @param {BackendSession} session
|
|
* @memberof SelfServiceShopHandler
|
|
*/
|
|
async buyGift(msg: { activityId: number, roundIndex: number, index: number }, session: BackendSession) {
|
|
const { activityId, roundIndex, index } = 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 activityData: ActivityModelType = await ActivityModel.findActivity(activityId);
|
|
let playerData = new SelfServiceShopData(activityData);
|
|
let item = playerData.getItem(index);
|
|
|
|
if (item.countMax > 0) {//限制购买次数
|
|
let playerRecords: ActivitySelfServiceShopModelType[] = await ActivitySelfServiceShopModel.findDataByIndex(serverId, activityId, roleId, playerData.roundIndex, index);
|
|
if (playerRecords.length >= item.countMax) {
|
|
return resResult(STATUS.ACTIVITY_MAX_COUNT);
|
|
}
|
|
}
|
|
|
|
let price = item.price;
|
|
let priceType = item.priceType;
|
|
|
|
//检查资源
|
|
if (priceType === ACTIVITY_RESOURCES_TYPE.GOODS) {//茶晶
|
|
let result = await handleCost(roleId, sid, [{ id: CURRENCY_BY_TYPE.get(CURRENCY_TYPE.COIN), count: price }]);
|
|
if (!result) return resResult(STATUS.ROLE_MATERIAL_NOT_ENOUGH);
|
|
|
|
} else if (priceType === ACTIVITY_RESOURCES_TYPE.RMB) {//RMB购买
|
|
return resResult(STATUS.ACTIVITY_DATA_ERROR);
|
|
}
|
|
let result = await addSelfServiceShopGiftReward(roleId, roleName, sid, serverId, funcs, activityId, roundIndex, index, price, priceType);
|
|
return resResult(STATUS.SUCCESS, Object.assign(result, {}));
|
|
}
|
|
|
|
/**
|
|
* @description 操作礼包
|
|
* @param {{ activityId: number, roundIndex: number, index: number, cellIndex: number, gift: number, rewardIndex: number}} msg
|
|
* @param {BackendSession} session
|
|
* @memberof SelfServiceShopHandler
|
|
*/
|
|
async saveGood(msg: { activityId: number, roundIndex: number, index: number, cellIndex: number, gift: number, rewardIndex: number }, session: BackendSession) {
|
|
const { activityId, roundIndex, index, cellIndex, gift, rewardIndex } = 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');
|
|
await ActivitySelfServiceGoodsModel.addGoods(activityId, roleId, roundIndex, index, cellIndex, gift, rewardIndex);
|
|
return resResult(STATUS.SUCCESS, {});
|
|
}
|
|
}
|