活动:寻宝骑兵活动接口

This commit is contained in:
qiaoxin
2021-05-25 21:38:23 +08:00
parent bd78b30cee
commit ebeb22f630
9 changed files with 538 additions and 6 deletions

View File

@@ -7,6 +7,7 @@ import { addReward, stringToRewardParam, useGiftPackage } from '../../../service
import { ActivityFirstGiftModel } from '../../../db/ActivityFirstGift';
import { signInActivity, signInVIPActivity } from '../../../services/signInService';
import { growthFundActivity } from '../../../services/growthFundService';
import { newPlayerLimitPackageActivity } from '../../../services/limitPackageService';
export default function (app: Application) {
return new ActivityHandler(app);
@@ -129,7 +130,17 @@ export class ActivityHandler {
});
}
}
//新手限定RMB购买礼包
{
let data = await newPlayerLimitPackageActivity(serverId, roleId);
if (data) {
playerActivityArray.push({
type: ACTIVITY_TYPE.NEW_PLAYER_LIMIT_PACKAGE,
activityId: data.activityId,
data,
});
}
}
return resResult(STATUS.SUCCESS, { playerActivityArray });
}

View File

@@ -0,0 +1,58 @@
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';
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 }, 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, {});
}
if (activityId !== huntActivityId) {
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);
}
}

View File

@@ -14,15 +14,15 @@ import { LimitShopData, ShopItem } from '../domain/activityField/limitShopField'
export async function newPlayerLimitPackageActivity(serverId: number, roleId: string) {
let activityArray: ActivityModelType[] = await ActivityModel.findOpenActivityByType(serverId, ACTIVITY_TYPE.NEW_PLAYER_LIMIT_PACKAGE, new Date)
activityArray = activityArray.sort((a, b) => {
return b.activityId - a.activityId
});
if (activityArray.length == 0) {
return null;
}
let activityData = activityArray[0];
let playerData = await getPlayerLimitPackageData(activityData.activityId, serverId, roleId);
return playerData
let playerData = new LimitShopData(activityData);
let playerRecord: ActivityShopModelType = await ActivityShopModel.findData(activityData.activityId, roleId, playerData.roundIndex);
playerData.setPlayerRecords(playerRecord);
return playerData;
}
/**

View File

@@ -0,0 +1,155 @@
import moment = require('moment');
import { ACTIVITY_TYPE, SERVER_OPEN_TIME, TASK_TYPE } from '../consts';
import { ActivityModel, ActivityModelType } from '../db/Activity';
import { ActivityTreasureHuntShopModel, ActivityTreasureHuntShopModelType } from '../db/ActivityTreasureHuntShop';
import { ServerTempModel, ServerTempModelType } from '../db/ServerTemp';
import { RewardParam } from '../domain/activityField/rewardField';
import { TreasureHuntData } from '../domain/activityField/treasureHuntField';
import { deltaDays } from '../pubUtils/util';
import { addReward, stringToRewardParam } from './giftPackageService';
/**
* 获取活动数据
*
* @param {number} serverId 区Id
* @param {number} type 活动类型 ACTIVITY_TYPE
* @param {string} roleId 角色Id
*
*/
export async function treasureHuntActivity(serverId: number, roleId: string) {
let { huntActivityId, huntBeginTime, huntEndTime, huntRoundIndex, activityData } = await getTreasureHuntData(serverId);
if (!activityData) {
return null;
}
let playerData = await getPlayerTreasureHuntData(activityData.activityId, serverId, roleId, huntRoundIndex, huntBeginTime, huntEndTime);
return playerData
}
/**
* 玩家活动数据
*
* @param {number} serverId 区Id
* @param {number} activityId 活动Id
* @param {string} roleId 角色Id
*
*/
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);
return playerData;
}
//获取当前活动的周期等数据
export async function getTreasureHuntData(serverId: number) {
let tempData: ServerTempModelType = await ServerTempModel.findData(serverId);
let now = moment(new Date()).toDate();
let activityData: ActivityModelType = null; //当前活动数据
if (!tempData) {//开始新周期
let { huntActivityId, huntBeginTime, huntEndTime, huntRoundIndex } = await getNewActivityData(serverId);
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);
tempData = await ServerTempModel.updateTreasureHuntData(serverId, huntActivityId, huntBeginTime, huntEndTime, huntRoundIndex);
activityData = await ActivityModel.findActivity(serverId, huntActivityId, true);
return { huntActivityId, huntBeginTime, huntEndTime, huntRoundIndex, activityData }
}
if (!activityData) {
activityData = await ActivityModel.findActivity(serverId, tempData.huntActivityId, true);
}
return {
huntActivityId: tempData.huntActivityId,
huntBeginTime: tempData.huntBeginTime,
huntEndTime: tempData.huntEndTime,
huntRoundIndex: tempData.huntRoundIndex,
activityData
}
}
async function getNewActivityData(serverId: number) {
let huntActivityId: number = 0; // 寻宝骑兵-活动Id
let huntEndTime: Date = null; // 寻宝骑兵-结束时间
let huntRoundIndex: number = 0; // 寻宝骑兵-周期数
let activityArray: ActivityModelType[] = await ActivityModel.findActivityByType(serverId, ACTIVITY_TYPE.TREASURE_HUNT, 1)//activityId从小到大排序
let treasureHuntDataArray: TreasureHuntData[] = [];
for (let obj of activityArray) {
treasureHuntDataArray.push(new TreasureHuntData(obj));
}
let isOver = false;
let huntBeginTime = moment(SERVER_OPEN_TIME).startOf('d').add(1, 'd').toDate();
let curDate = moment(new Date()).toDate()
while (!isOver) {
huntRoundIndex++;
for (let data of treasureHuntDataArray) {
let endTime = moment(huntBeginTime).add(data.day, 'd').toDate();
if (huntBeginTime < curDate && curDate < endTime) {
huntEndTime = moment(endTime).add(-1, 'm').toDate()
huntActivityId = data.activityId;
isOver = true;
} else {
huntBeginTime = endTime;
}
}
if (huntRoundIndex >= 365) {//max
isOver = true;
}
}
return { huntActivityId, huntBeginTime, huntEndTime, huntRoundIndex }
}
async function getNextActivityData(serverId: number, oldHuntActivityId: number, oldHuntEndTime: Date, oldHuntRoundIndex: number) {
let huntActivityId: number = 0; // 寻宝骑兵-活动Id
let huntEndTime: Date = null; // 寻宝骑兵-结束时间
let huntRoundIndex: number = oldHuntRoundIndex; // 寻宝骑兵-周期数
let activityArray: ActivityModelType[] = await ActivityModel.findActivityByType(serverId, ACTIVITY_TYPE.TREASURE_HUNT, 1)//activityId从小到大排序
let treasureHuntDataArray: TreasureHuntData[] = [];
for (let obj of activityArray) {
treasureHuntDataArray.push(new TreasureHuntData(obj));
}
let isOver = false;
let huntBeginTime = moment(oldHuntEndTime).add(1, 'd').startOf('d').toDate();
let curDate = moment(new Date()).toDate()
let index = treasureHuntDataArray.findIndex(obj => { return obj && obj.activityId === oldHuntActivityId });
for (; index < treasureHuntDataArray.length; index++) {
let data = treasureHuntDataArray[index];
let endTime = moment(huntBeginTime).add(data.day, 'd').toDate();
if (huntBeginTime < curDate && curDate < endTime) {
huntEndTime = moment(endTime).add(-1, 'm').toDate()
huntActivityId = data.activityId;
isOver = true;
} else {
huntBeginTime = endTime;
}
}
while (!isOver) {
huntRoundIndex++;
for (let data of treasureHuntDataArray) {
let endTime = moment(huntBeginTime).add(data.day, 'd').toDate();
if (huntBeginTime < curDate && curDate < endTime) {
huntEndTime = moment(endTime).add(-1, 'm').toDate()
huntActivityId = data.activityId;
isOver = true;
} else {
huntBeginTime = endTime;
}
}
if (huntRoundIndex >= 365) {//max
isOver = true;
}
}
return { huntActivityId, huntBeginTime, huntEndTime, huntRoundIndex }
}