179 lines
8.8 KiB
TypeScript
179 lines
8.8 KiB
TypeScript
import { ACTIVITY_TYPE, STATUS, TASK_TYPE } from '../consts';
|
|
import { ActivityModel, ActivityModelType } from '../db/Activity';
|
|
import { PopUpShopData, } from '../domain/activityField/popUpShopField';
|
|
import { RewardParam } from '../domain/activityField/rewardField';
|
|
import { addReward, stringToRewardParam } from './giftPackageService';
|
|
import { ActivityPopUpShopModel, ActivityPopUpShopModelType } from '../db/ActivityPopUpShop';
|
|
import { ServerlistModel } from '../db/Serverlist';
|
|
import moment = require('moment');
|
|
|
|
/**
|
|
* 获取活动数据
|
|
*
|
|
* @param {number} serverId 区Id
|
|
* @param {number} type 活动类型 ACTIVITY_TYPE
|
|
* @param {string} roleId 角色Id
|
|
*
|
|
*/
|
|
|
|
export async function popUpShopActivity(serverId: number, roleId: string) {
|
|
let { activityGroupId } = await ServerlistModel.findByServerId(serverId);
|
|
let activityArray: ActivityModelType[] = await ActivityModel.findOpenActivityByType(activityGroupId, ACTIVITY_TYPE.POP_UP_SHOP, new Date())
|
|
|
|
if (activityArray.length == 0) {
|
|
return null;
|
|
}
|
|
let activityData = activityArray[0];
|
|
let playerData = await getPlayerPopUpShopData(activityData.activityId, serverId, roleId);
|
|
return playerData
|
|
}
|
|
|
|
/**
|
|
* 玩家活动数据
|
|
*
|
|
* @param {number} serverId 区Id
|
|
* @param {number} activityId 活动Id
|
|
* @param {string} roleId 角色Id
|
|
*
|
|
*/
|
|
export async function getPlayerPopUpShopData(activityId: number, serverId: number, roleId: string) {
|
|
let activityData: ActivityModelType = await ActivityModel.findActivity(activityId);
|
|
let playerRecords: ActivityPopUpShopModelType[] = await ActivityPopUpShopModel.findAllOpenData(serverId, activityId, roleId);
|
|
|
|
let allPlayerShop = [];
|
|
let allTaskData = JSON.parse(activityData.data);
|
|
for (let record of playerRecords) {
|
|
if (record.isPush) {//没有购买
|
|
let index = allTaskData.findIndex(obj => { return obj && obj.id === record.id && obj.taskType === record.type })
|
|
if (index != -1) {
|
|
let playerData = new PopUpShopData(allTaskData[index], activityData.activityId);
|
|
playerData.setPlayerRecords(record)
|
|
if (playerData.buyCount < allTaskData[index].count) {
|
|
allPlayerShop.push(playerData)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return allPlayerShop;
|
|
}
|
|
|
|
|
|
|
|
// /**
|
|
// * 弹出礼包任务特殊处理
|
|
// *
|
|
// * @param {number} serverId 区Id
|
|
// * @param {number} activityId 活动Id
|
|
// * @param {string} roleId 角色Id
|
|
// *
|
|
// */
|
|
// export async function checkPopUpActivityTask(serverId: number, sid: number, funcs: number[], roleId: string, taskType: number, addCount: number) {
|
|
// let { activityGroupId } = await ServerlistModel.findByServerId(serverId);
|
|
// let allActivity = await ActivityModel.findOpenActivityByType(activityGroupId, ACTIVITY_TYPE.POP_UP_SHOP, new Date());
|
|
// for (let activity of allActivity) {
|
|
// let allTaskData: any[] = JSON.parse(activity.data);
|
|
// for (let task of allTaskData) {
|
|
// if (task.taskType == taskType) {
|
|
// let popShopData = new PopUpShopData(task, activity.activityId);
|
|
// let taskRecord = await ActivityPopUpShopModel.findDataByTaskId(serverId, activity.activityId, roleId, popShopData.id, popShopData.taskType)
|
|
// let recordData = taskRecord && taskRecord.data ? JSON.parse(taskRecord.data) : null
|
|
|
|
// let beginTime = new Date();
|
|
// let endTime = moment(new Date()).add(task.duration, 'h').toDate();
|
|
// if (taskType == TASK_TYPE.ROLE_TERAPH_STAGE_UP) {//只要触发就弹出礼包商店
|
|
// //推送
|
|
// let playerRecord = await ActivityPopUpShopModel.addTaskPushMessage(serverId, activity.activityId, roleId, task.id, task.taskType, addCount, beginTime, endTime);
|
|
// popShopData.setPlayerRecords(playerRecord)
|
|
// // pushMessage = pushMessage.concat(popShopData);
|
|
// } else if (taskType == TASK_TYPE.EQUIP_QUALITY_COUNT || taskType == TASK_TYPE.GACHA_QUALITY_COUNT) {//每天统计
|
|
|
|
// }
|
|
|
|
// // if (!taskRecord || !taskRecord.isPush) {
|
|
// // let { addCount, record } = isComplete(roleId, task.taskType, task.taskParam, count, parma, recordData);
|
|
// // if (addCount) {
|
|
|
|
// // if (taskType == TASK_TYPE.ROLE_LV || taskType == TASK_TYPE.ROLE_TITLE || taskType == TASK_TYPE.BATTLE_TOWER_LV) {
|
|
// // let playerRecord = await ActivityPopUpShopModel.setTaskCount(serverId, activity.activityId, roleId, task.id, task.taskType, addCount);
|
|
// // //推送
|
|
// // if (task.condition <= playerRecord.totalCount) {
|
|
// // playerRecord = await ActivityPopUpShopModel.pushMessage(serverId, activity.activityId, roleId, task.id, task.taskType, beginTime, endTime);
|
|
// // popShopData.setPlayerRecords(playerRecord)
|
|
// // pushMessage = pushMessage.concat(popShopData);
|
|
// // }
|
|
// // } else if (taskType == TASK_TYPE.ROLE_TERAPH_STAGE_UP) {//只要触发就弹出礼包商店
|
|
// // //推送
|
|
// // let playerRecord = await ActivityPopUpShopModel.addTaskPushMessage(serverId, activity.activityId, roleId, task.id, task.taskType, addCount, beginTime, endTime);
|
|
// // popShopData.setPlayerRecords(playerRecord)
|
|
// // pushMessage = pushMessage.concat(popShopData);
|
|
// // } else if (taskType == TASK_TYPE.EQUIP_QUALITY_COUNT || taskType == TASK_TYPE.GACHA_QUALITY_COUNT) {//每天统计
|
|
|
|
// // } else {
|
|
// // let playerRecord = await ActivityPopUpShopModel.addTaskCount(serverId, activity.activityId, roleId, task.id, task.taskType, addCount);
|
|
// // //推送
|
|
// // if (task.condition <= playerRecord.totalCount) {
|
|
// // playerRecord = await ActivityPopUpShopModel.pushMessage(serverId, activity.activityId, roleId, task.id, task.taskType, beginTime, endTime);
|
|
// // popShopData.setPlayerRecords(playerRecord)
|
|
// // pushMessage = pushMessage.concat(popShopData);
|
|
// // }
|
|
// // }
|
|
// // }
|
|
// // if (record) {
|
|
// // await ActivityPopUpShopModel.addTaskRecord(serverId, activity.activityId, roleId, task.id, task.taskType, JSON.stringify(record));
|
|
// // }
|
|
// // }
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
/**
|
|
* 结算购买礼包的奖励
|
|
*
|
|
* @param {number} serverId 区Id
|
|
* @param {number} activityId 活动Id
|
|
* @param {string} roleId 角色Id
|
|
* @param {string} productID 商品ID
|
|
*
|
|
*/
|
|
export async function makePopUpShopReward(roleId: string, roleName: string, sid: string, serverId: number, funcs: number[],
|
|
activityId: number, productID: string) {
|
|
let activityData: ActivityModelType = await ActivityModel.findActivity(activityId);
|
|
if (!activityData) {
|
|
return STATUS.ACTIVITY_MISSING;
|
|
}
|
|
if (activityData.type !== ACTIVITY_TYPE.POP_UP_SHOP) {
|
|
return STATUS.ACTIVITY_TYPE_ERROR;
|
|
}
|
|
let allTaskData: any[] = JSON.parse(activityData.data);
|
|
let taskIndex = allTaskData.findIndex(obj => { return obj && obj.productID == productID });
|
|
if (taskIndex == -1) {
|
|
return STATUS.ACTIVITY_NO_PRODUCT;
|
|
}
|
|
let playerData = new PopUpShopData(allTaskData[taskIndex], activityId);
|
|
let playerRecord: ActivityPopUpShopModelType = await ActivityPopUpShopModel.findOpenDataByTaskId(serverId, activityId, roleId, playerData.id, playerData.taskType);
|
|
if (!playerRecord) {
|
|
return STATUS.APPLY_ORDER_ERROR;
|
|
}
|
|
let curDate = new Date();
|
|
if (curDate < playerRecord.beginTime || curDate > playerRecord.endTime) {
|
|
return STATUS.ACTIVITY_POP_UP_SHOP_CLOSED;
|
|
}
|
|
if (playerRecord.totalCount < playerData.condition) {
|
|
return STATUS.ACTIVITY_TASK_UNCOMPLETED;
|
|
}
|
|
if (playerRecord.buyCount >= playerData.count) {
|
|
return STATUS.ACTIVITY_REWARDED;
|
|
}
|
|
playerRecord = await ActivityPopUpShopModel.addRecord(serverId, activityId, roleId, playerData.id, playerData.taskType, 1);
|
|
playerData.setPlayerRecords(playerRecord);
|
|
|
|
let rewardParamArr: Array<RewardParam> = stringToRewardParam(playerData.reward);
|
|
let result = await addReward(roleId, roleName, sid, serverId, funcs, rewardParamArr)
|
|
|
|
|
|
return {
|
|
code: 0,
|
|
data: Object.assign(result, { item: playerData, activityId: activityData.activityId })
|
|
}
|
|
} |