134 lines
6.0 KiB
TypeScript
134 lines
6.0 KiB
TypeScript
import { Application, BackendSession, HandlerService, } from 'pinus';
|
|
import { getRandSingleEelm, parseNumberList, resResult } from '@pubUtils/util';
|
|
import { ITEM_CHANGE_REASON, STATUS } from '../../../consts';
|
|
import { addReward, stringToConsumeParam, stringToRewardParam } from '../../../services/activity/giftPackageService';
|
|
import { getPlayerEntertainData, getPlayerEntertainDataShow } from '../../../services/activity/entertainService';
|
|
import { ActivityEntertainRecModel } from '@db/ActivityEntertainRec';
|
|
import { handleCost } from '../../../services/role/rewardService';
|
|
|
|
export default function (app: Application) {
|
|
new HandlerService(app, {});
|
|
return new EntertainHandler(app);
|
|
}
|
|
|
|
export class EntertainHandler {
|
|
constructor(private app: Application) {
|
|
}
|
|
|
|
/**
|
|
* @description 获取火神祭祀活动数据
|
|
* @param {{ activityId: number}} msg
|
|
* @param {BackendSession} session
|
|
* @memberof EntertainHandler
|
|
*/
|
|
async getData(msg: { activityId: number }, session: BackendSession) {
|
|
const { activityId } = msg;
|
|
const roleId = session.get('roleId');
|
|
const serverId = session.get('serverId');
|
|
|
|
let playerData = await getPlayerEntertainDataShow(activityId, serverId, roleId);
|
|
|
|
if (!playerData) return resResult(STATUS.ACTIVITY_MISSING);
|
|
|
|
return resResult(STATUS.SUCCESS, playerData);
|
|
}
|
|
|
|
/**
|
|
* @description 宴请
|
|
* @param {{ activityId: number }} msg
|
|
* @param {BackendSession} session
|
|
* @memberof EntertainHandler
|
|
*/
|
|
async invite(msg: { activityId: number }, session: BackendSession) {
|
|
const { activityId } = msg;
|
|
const roleId = session.get('roleId');
|
|
const roleName = session.get('roleName');
|
|
const sid = session.get('sid');
|
|
const serverId = session.get('serverId');
|
|
|
|
let playerData = await getPlayerEntertainData(activityId, serverId, roleId);
|
|
if (!playerData) return resResult(STATUS.ACTIVITY_MISSING);
|
|
// 挑战次数
|
|
if(playerData.playCnt >= playerData.freeCnt + playerData.buyCnt) return resResult(STATUS.ACTIVITY_ENTERTAIN_NO_NUM);
|
|
|
|
let pool = playerData.heroes.filter(hero => {
|
|
if(hero.num >= hero.maxNum) return false;
|
|
for(let condition of hero.conditionArr) {
|
|
if(condition.type == 1 && playerData.invitedHeroNum < condition.param) return false;
|
|
if(condition.type == 2 && playerData.invitedHids.indexOf(condition.param) == -1) return false;
|
|
}
|
|
return true;
|
|
});
|
|
if(pool.length <= 0) return resResult(STATUS.ACTIVITY_ENTERTAIN_NO_NUM);
|
|
let randResult = getRandSingleEelm(pool);
|
|
if(!randResult) return resResult(STATUS.ACTIVITY_ENTERTAIN_NO_NUM);
|
|
|
|
let rewards = stringToRewardParam(randResult.reward);
|
|
let todayIndex = playerData.todayIndex; // 免费挑战算在第几天
|
|
if(playerData.todayPlayCnt >= playerData.freeCntDaily && playerData.playCnt < playerData.freeCnt) {
|
|
let playerRecord = await ActivityEntertainRecModel.findData(serverId, activityId, playerData.roundIndex, roleId);
|
|
let records = playerRecord?.record||[];
|
|
for(let i = 1; i <= todayIndex; i++) {
|
|
let times = records.filter(cur => cur.todayIndex == i).length;
|
|
playerData.playCnt += times;
|
|
if(times < playerData.freeCntDaily) {
|
|
todayIndex = i; break;
|
|
}
|
|
}
|
|
}
|
|
|
|
let playerRecord = await ActivityEntertainRecModel.record(serverId, activityId, playerData.roundIndex, roleId, { todayIndex, id: randResult.id, hid: randResult.hid, index: randResult.index, time: new Date(), reward: randResult.reward })
|
|
let { goods } = await addReward(roleId, roleName, sid, serverId, rewards, ITEM_CHANGE_REASON.ACT_ENTERTAIN);
|
|
playerData.todayPlayCnt = 0;
|
|
playerData.playCnt = 0;
|
|
playerData.freeCnt = playerData.freeCntDaily * playerData.todayIndex;
|
|
for(let hero of playerData.heroes) hero.num = 0;
|
|
playerData.setPlayerRecords(playerRecord);
|
|
let newRandResult = playerData.heroes.find(cur => cur.id == randResult.id);
|
|
|
|
return resResult(STATUS.SUCCESS, {
|
|
activityId,
|
|
todayPlayCnt: playerData.todayPlayCnt,
|
|
playCnt: playerData.playCnt,
|
|
freeCnt: playerData.freeCnt,
|
|
curHero: newRandResult?.getShowResult(),
|
|
goods
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @description 购买次数
|
|
* @param {{ activityId: number, id: number, count: number}} msg
|
|
* @param {BackendSession} session
|
|
* @memberof EntertainHandler
|
|
*/
|
|
async buyCnt(msg: { activityId: number, count: number }, session: BackendSession) {
|
|
const { activityId, count } = msg;
|
|
const roleId = session.get('roleId');
|
|
const sid = session.get('sid');
|
|
const serverId = session.get('serverId');
|
|
|
|
let playerData = await getPlayerEntertainData(activityId, serverId, roleId);
|
|
if (!playerData) return resResult(STATUS.ACTIVITY_MISSING);
|
|
|
|
// 可购买次数
|
|
if(playerData.buyCnt + count > playerData.maxBuyCnt) return resResult(STATUS.ACTIVITY_ENTERTAIN_BUY_COUNT_OVER);
|
|
// if(playerData.todayPlayCnt < playerData.freeCnt) return resResult(STATUS.ACTIVITY_DRAGON_BOAT_CANNOT_BUY);
|
|
// 扣材料
|
|
let costResult = await handleCost(roleId, sid, stringToConsumeParam(playerData.buyCost), ITEM_CHANGE_REASON.ACT_ENTERTAIN_BUY_COST);
|
|
if(!costResult) return resResult(STATUS.ROLE_MATERIAL_NOT_ENOUGH);
|
|
|
|
// 保存数据
|
|
let buildResult = await ActivityEntertainRecModel.buyCnt(serverId, activityId, playerData.roundIndex, roleId, count);
|
|
// // 更新数据
|
|
playerData.updateBuyCnt(buildResult);
|
|
|
|
return resResult(STATUS.SUCCESS, {
|
|
activityId,
|
|
maxBuyCnt: playerData.maxBuyCnt,
|
|
buyCnt: playerData.buyCnt
|
|
});
|
|
}
|
|
}
|
|
|