活动:寻宝骑兵-每日物资
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
import { Application, BackendSession } from 'pinus';
|
||||
import { resResult } from '../../../pubUtils/util';
|
||||
import { STATUS, ACTIVITY_RESOURCES_TYPE, ACTIVITY_TYPE } from '../../../consts';
|
||||
import { getPlayerTreasureHuntData, getTreasureHuntData } from '../../../services/treasureHuntService';
|
||||
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) {
|
||||
@@ -38,10 +42,14 @@ export class TreasureHuntHandler {
|
||||
* @param {BackendSession} session
|
||||
* @memberof TreasureHuntHandler
|
||||
*/
|
||||
async buyGoods(msg: { activityId: number }, session: BackendSession) {
|
||||
const { activityId } = msg;
|
||||
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, {});
|
||||
@@ -50,9 +58,34 @@ export class TreasureHuntHandler {
|
||||
return resResult(STATUS.ACTIVITY_MISSING, {});
|
||||
}
|
||||
|
||||
let playerData = await getPlayerTreasureHuntData(activityId, serverId, roleId, huntRoundIndex, huntBeginTime, huntEndTime,);
|
||||
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, {});
|
||||
}
|
||||
|
||||
return resResult(STATUS.SUCCESS, playerData);
|
||||
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<RewardParam> = 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,
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,15 +36,29 @@ export async function treasureHuntActivity(serverId: number, roleId: string) {
|
||||
*/
|
||||
export async function getPlayerTreasureHuntData(activityId: number, serverId: number, roleId: string, huntRoundIndex: number, huntBeginTime: Date, huntEndTime: Date) {
|
||||
let activityData: ActivityModelType = await ActivityModel.findActivity(serverId, activityId, true);
|
||||
let dayIndex = 0;
|
||||
let playerRecord: ActivityTreasureHuntShopModelType = await ActivityTreasureHuntShopModel.findTreasureData(serverId, activityId, roleId, huntRoundIndex, dayIndex);
|
||||
|
||||
let playerData = new TreasureHuntData(activityData);
|
||||
playerData.beginTime = moment(huntBeginTime).valueOf();
|
||||
playerData.endTime = moment(huntEndTime).valueOf();
|
||||
playerData.todayIndex = deltaDays(huntBeginTime, new Date) + 1;;
|
||||
playerData.roundIndex = huntRoundIndex;
|
||||
// playerData.setPlayerRecords(playerRecord);
|
||||
|
||||
let playerShopRecord: ActivityTreasureHuntShopModelType = await ActivityTreasureHuntShopModel.findTreasureData(serverId, activityId, roleId, huntRoundIndex, playerData.todayIndex);
|
||||
playerData.shop.setPlayerRecords(playerShopRecord);
|
||||
return playerData;
|
||||
}
|
||||
|
||||
export async function getPlayerTreasureHuntShopData(activityId: number, serverId: number, roleId: string, huntRoundIndex: number, huntBeginTime: Date, huntEndTime: Date) {
|
||||
let activityData: ActivityModelType = await ActivityModel.findActivity(serverId, activityId, true);
|
||||
|
||||
let playerData = new TreasureHuntData(activityData);
|
||||
playerData.beginTime = moment(huntBeginTime).valueOf();
|
||||
playerData.endTime = moment(huntEndTime).valueOf();
|
||||
playerData.todayIndex = deltaDays(huntBeginTime, new Date) + 1;;
|
||||
playerData.roundIndex = huntRoundIndex;
|
||||
|
||||
let playerShopRecord: ActivityTreasureHuntShopModelType = await ActivityTreasureHuntShopModel.findTreasureData(serverId, activityId, roleId, huntRoundIndex, playerData.todayIndex);
|
||||
playerData.shop.setPlayerRecords(playerShopRecord);
|
||||
return playerData;
|
||||
}
|
||||
|
||||
@@ -56,12 +70,18 @@ export async function getTreasureHuntData(serverId: number) {
|
||||
|
||||
if (!tempData) {//开始新周期
|
||||
let { huntActivityId, huntBeginTime, huntEndTime, huntRoundIndex } = await getNewActivityData(serverId);
|
||||
if (huntActivityId === 0) {
|
||||
return { huntActivityId, huntBeginTime, huntEndTime, huntRoundIndex, activityData }
|
||||
}
|
||||
tempData = await ServerTempModel.updateTreasureHuntData(serverId, huntActivityId, huntBeginTime, huntEndTime, huntRoundIndex);
|
||||
activityData = await ActivityModel.findActivity(serverId, huntActivityId, true);
|
||||
return { huntActivityId, huntBeginTime, huntEndTime, huntRoundIndex, activityData }
|
||||
}
|
||||
if (now > tempData.huntEndTime) {//活动过期
|
||||
let { huntActivityId, huntBeginTime, huntEndTime, huntRoundIndex } = await getNextActivityData(serverId, tempData.huntActivityId, tempData.huntEndTime, tempData.huntRoundIndex);
|
||||
if (huntActivityId === 0) {
|
||||
return { huntActivityId, huntBeginTime, huntEndTime, huntRoundIndex, activityData }
|
||||
}
|
||||
tempData = await ServerTempModel.updateTreasureHuntData(serverId, huntActivityId, huntBeginTime, huntEndTime, huntRoundIndex);
|
||||
activityData = await ActivityModel.findActivity(serverId, huntActivityId, true);
|
||||
return { huntActivityId, huntBeginTime, huntEndTime, huntRoundIndex, activityData }
|
||||
|
||||
Reference in New Issue
Block a user