86 lines
3.3 KiB
TypeScript
86 lines
3.3 KiB
TypeScript
import { ACTIVITY_TYPE, 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';
|
|
|
|
/**
|
|
* 获取活动数据
|
|
*
|
|
* @param {number} serverId 区Id
|
|
* @param {number} type 活动类型 ACTIVITY_TYPE
|
|
* @param {string} roleId 角色Id
|
|
*
|
|
*/
|
|
|
|
export async function popUpShopActivity(serverId: number, roleId: string) {
|
|
let activityArray: ActivityModelType[] = await ActivityModel.findOpenActivityByType(serverId, ACTIVITY_TYPE.POP_UP_SHOP, 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 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(serverId, activityId, true);
|
|
let playerRecords: ActivityPopUpShopModelType[] = await ActivityPopUpShopModel.findAllOpenData(serverId, activityId, roleId);
|
|
|
|
let allPlayerShop = [];
|
|
let allTaskData = JSON.parse(activityData.data);
|
|
for (let record of playerRecords) {
|
|
if (!record.isBuy) {//没有购买
|
|
let index = allTaskData.findIndex(obj => { return obj && obj.id === record.id })
|
|
if (index != -1) {
|
|
let playerData = new PopUpShopData(allTaskData[index]);
|
|
playerData.isBuy == false;
|
|
allPlayerShop.push(playerData)
|
|
}
|
|
}
|
|
}
|
|
return allPlayerShop;
|
|
}
|
|
|
|
/**
|
|
* 结算购买礼包的奖励
|
|
*
|
|
* @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(serverId, activityId, true);
|
|
if (!activityData) {
|
|
return null;
|
|
}
|
|
let allTaskData: any[] = JSON.parse(activityData.data);
|
|
let taskIndex = allTaskData.findIndex(obj => { return obj && obj.data.productID == productID });
|
|
if (taskIndex == -1) {
|
|
return null;
|
|
}
|
|
let playerData = new PopUpShopData(allTaskData[taskIndex]);
|
|
let playerRecords: ActivityPopUpShopModelType = await ActivityPopUpShopModel.addRecord(serverId, activityId, roleId, playerData.id);
|
|
if (!playerRecords) {
|
|
return null;
|
|
}
|
|
|
|
let rewardParamArr: Array<RewardParam> = stringToRewardParam(playerData.reward);
|
|
let result = await addReward(roleId, roleName, sid, serverId, funcs, rewardParamArr)
|
|
return result
|
|
} |