活动:寻宝骑兵活动接口
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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 }
|
||||
}
|
||||
Reference in New Issue
Block a user