事件本检查时间刷新并推送
This commit is contained in:
@@ -71,14 +71,14 @@ export class WarReward {
|
||||
|
||||
private handleFixReward(num: number) {
|
||||
if(this.isSuccess) { // 成功了才给固定奖励
|
||||
let {fixReward} = this.warInfo;
|
||||
let {fixReward = ''} = this.warInfo;
|
||||
let reward = decodeStr('fixReward', fixReward);
|
||||
for(let obj of reward) this.rewards.push({type: BATTLE_REWARD_TYPE.FIX_REWARD, ...obj, count: obj.count * num});
|
||||
}
|
||||
}
|
||||
private handleConditionReward(num: number) {
|
||||
if(this.isSuccess) {
|
||||
let {conditionReward} = this.warInfo;
|
||||
let {conditionReward = ''} = this.warInfo;
|
||||
let reward = decodeStr('conditionReward', conditionReward);
|
||||
for(let obj of reward) {
|
||||
if(this.condition.get(obj.condition)) {
|
||||
@@ -90,7 +90,8 @@ export class WarReward {
|
||||
|
||||
private async handleRandomReward(num: number) {
|
||||
if(this.isSuccess) {
|
||||
let {randomReward} = this.warInfo;
|
||||
let {RandomReward:randomReward = ''} = this.warInfo;
|
||||
|
||||
let reward = decodeStr('randomReward', randomReward);
|
||||
for(let obj of reward) {
|
||||
let { gid, frequency } = obj;
|
||||
|
||||
@@ -0,0 +1,153 @@
|
||||
import { Application, BackendSession, FrontendOrBackendSession } from 'pinus';
|
||||
import { getGamedata } from '../../../util/gamedata';
|
||||
import { EventRecordModel } from '../../../db/EventRecord';
|
||||
import { RoleModel } from '../../../db/Role';
|
||||
import { genCode } from '../../../util/util';
|
||||
|
||||
export default function(app: Application) {
|
||||
return new EventBattleHandler(app);
|
||||
}
|
||||
|
||||
export class EventBattleHandler {
|
||||
constructor(private app: Application) {
|
||||
}
|
||||
|
||||
// 获取关卡列表
|
||||
async receiveEventReward(msg: { eventCode: string, isSuccess: boolean }, session: BackendSession) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export async function setBattleStatus(roleId: string, battleId: number , isSuccess: boolean, battleCode: string, eventStatus: number = 0) {
|
||||
let now = new Date();
|
||||
let refTime = eventStatus == 2? getEventTime(now): 0;
|
||||
|
||||
let result = await EventRecordModel.setBattleStatus(roleId, battleId, refTime, isSuccess?1:0, battleCode);
|
||||
if(eventStatus == 1) {
|
||||
await RoleModel.setEventStatus(roleId, 2);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export function getEventTime(now: Date) {
|
||||
let curTime = Number(now);
|
||||
let todayA = now.setHours(12, 0, 0, 0); // 每天12点
|
||||
let todayB = now.setHours(18, 0, 0, 0); // 每天18点
|
||||
let yesterdayA = todayA - 86400000; // 前一天12点
|
||||
let t = 0;
|
||||
if(curTime < todayA) {
|
||||
t = yesterdayA;
|
||||
} else if (curTime >= todayA && curTime < todayB) {
|
||||
t = todayA;
|
||||
} else if (curTime >= todayB) {
|
||||
t = todayB;
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
export async function startEvent(app: Application, session: FrontendOrBackendSession) {
|
||||
|
||||
let roleId = session.get('roleId');
|
||||
let roleName = session.get('roleName');
|
||||
let channelName = roleId;
|
||||
let event = await refreshEvent(1, roleId, roleName, 0);
|
||||
await RoleModel.setEventStatus(roleId, 1);
|
||||
session.set('eventStatus', 1);
|
||||
session.push('eventStatus', () => {});
|
||||
// console.log('*******setEventStatus')
|
||||
pushEventMsg(app, roleId, channelName, { event }); // 推送
|
||||
|
||||
}
|
||||
|
||||
export async function checkEvent(app: Application, session: FrontendOrBackendSession) {
|
||||
|
||||
try {
|
||||
|
||||
let roleId = session.get('roleId');
|
||||
if(roleId) {
|
||||
|
||||
let roleName = session.get('roleName');
|
||||
let channelName = roleId;
|
||||
let eventStatus = session.get('eventStatus')||0;
|
||||
|
||||
let eventTime = session.get('getEventTime')||0;
|
||||
let now = new Date();
|
||||
let t = getEventTime(now);
|
||||
|
||||
|
||||
let channel = app.get('channelService').getChannel(channelName, false);
|
||||
console.log('****channel', channelName, !!channel, eventTime, t, eventStatus)
|
||||
|
||||
if(!!channel && eventTime < t) { // 第一次登陆后可以刷新了
|
||||
session.set('getEventTime', t);
|
||||
session.push('getEventTime', () => {});
|
||||
|
||||
if (eventStatus == 1) {
|
||||
let event = await EventRecordModel.getEventRecordByTime(roleId, 0);
|
||||
pushEventMsg(app, roleId, channelName, { event }); // 推送
|
||||
} else if( eventStatus == 2 ) {
|
||||
|
||||
let event = await EventRecordModel.getEventRecordByTime(roleId, t);
|
||||
if(event.length == 0) { // 刷新
|
||||
const num = 3; // 每次刷3个
|
||||
event = await refreshEvent(num, roleId, roleName, t);
|
||||
}
|
||||
|
||||
console.log(event)
|
||||
// 推送
|
||||
pushEventMsg(app, roleId, channelName, { event });
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}catch(err) {
|
||||
console.log(err.stack);
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
async function refreshEvent(num: number, roleId: string, roleName: string, t) {
|
||||
let event = new Array();
|
||||
let dicEvent = getGamedata('dic_zyz_event');
|
||||
let historyRecord = await EventRecordModel.getHostoryEventRecord(roleId);
|
||||
let {history, turn} = historyRecord;
|
||||
let randomList = dicEvent.filter(cur => {
|
||||
return history.find(ccur => {
|
||||
return ccur.eventId == cur.eventID;
|
||||
});
|
||||
});
|
||||
|
||||
for(let i = 0; i < num; i++) {
|
||||
if(randomList.length == 0) { // 刷过新的一轮了
|
||||
turn ++;
|
||||
randomList = [...dicEvent];
|
||||
}
|
||||
|
||||
let index = Math.floor(Math.random() * randomList.length);
|
||||
let dic = randomList[index];
|
||||
let eventCode = genCode(8);
|
||||
let data = await EventRecordModel.saveEventRecord(eventCode, {
|
||||
roleId, refTime: t, eventId: dic.eventID,
|
||||
roleName, turn, type: dic.eventType, battleId: dic.warId||0, quality: dic.quality
|
||||
});
|
||||
event.push(data)
|
||||
randomList.splice(index, 1);
|
||||
}
|
||||
return event;
|
||||
}
|
||||
|
||||
function pushEventMsg(app, roleId, channelName, msg ) {
|
||||
console.log('***pushEventMsg', channelName)
|
||||
let channelService = app.get('channelService');
|
||||
|
||||
let param = { msg };
|
||||
let channel = channelService.getChannel(channelName, false);
|
||||
if(!!channel) {
|
||||
let tsid = channel.getMember(roleId)['sid'];
|
||||
|
||||
channelService.pushMessageByUids('onSpecialEvent', param, [{
|
||||
uid: roleId,
|
||||
sid: tsid
|
||||
}]);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,13 @@
|
||||
import { Application, BackendSession } from 'pinus';
|
||||
import { BattleRecordModel } from '../../../db/BattleRecord';
|
||||
import { BattleSweepRecordModel } from '../../../db/BattleSweepRecord';
|
||||
import { getWarById, getGoodById } from '../../../util/gamedata';
|
||||
import { getWarById, } from '../../../util/gamedata';
|
||||
import { genCode } from '../../../util/util';
|
||||
import { getAp, setAp, WarReward } from './battleUtils';
|
||||
import { WAR_TYPE } from '../../../consts/consts';
|
||||
import { WAR_TYPE, EVENT_START_BATTLE } from '../../../consts/consts';
|
||||
import { checkDaily, checkDailyAndIncrease } from './dailyBattleHandler';
|
||||
import { setBattleStatus, startEvent } from './eventBattleHandler';
|
||||
import { queryMethod } from '@typegoose/typegoose';
|
||||
|
||||
export default function(app: Application) {
|
||||
return new NormalBattleHandler(app);
|
||||
@@ -129,6 +131,9 @@ export class NormalBattleHandler {
|
||||
return {code: 202, data: checkResult.msg}
|
||||
}
|
||||
dailyNum = { type: checkResult.type, count: checkResult.count, sum: checkResult.sum };
|
||||
} else if (warInfo.war_type == WAR_TYPE.EVENT) {
|
||||
// 记录事件状态
|
||||
await setBattleStatus(roleId, battleId, isSuccess, battleCode, session.get('eventStatus'));
|
||||
}
|
||||
|
||||
let warReward = new WarReward(roleId, roleName, battleId, isSuccess);
|
||||
@@ -165,6 +170,12 @@ export class NormalBattleHandler {
|
||||
const updateResult = await BattleRecordModel.updateBattleRecordByCode(battleCode, params, true);
|
||||
let { status } = updateResult;
|
||||
|
||||
// 主线关卡某个关卡触发事件开启
|
||||
let eventStatus = session.get('eventStatus')||0;
|
||||
if(battleId == EVENT_START_BATTLE && eventStatus == 0) {
|
||||
// console.log('*******startEvent')
|
||||
await startEvent(this.app, session);
|
||||
}
|
||||
return {
|
||||
code: 200,
|
||||
data: {
|
||||
|
||||
Reference in New Issue
Block a user