领取奇遇事件
This commit is contained in:
@@ -1,11 +1,9 @@
|
||||
|
||||
import { ActionPointModel } from '../../../db/ActionPoint';
|
||||
import { BattleDropModel } from '../../../db/BattleDrop';
|
||||
import { CounterModel } from '../../../db/Counter';
|
||||
import { EquipModel } from '../../../db/Equip';
|
||||
|
||||
import { getWarById, getGoodById } from '../../../util/gamedata';
|
||||
import { decodeStr } from '../../../util/util';
|
||||
import { decodeStr, Reward } from '../../../util/util';
|
||||
import { ACTION_POIN, BATTLE_REWARD_TYPE, GOOD_TYPE } from '../../../consts/consts';
|
||||
|
||||
export async function getAp(now: number, roleId: string) {
|
||||
@@ -127,41 +125,9 @@ export class WarReward {
|
||||
await this.handleConditionReward(num);
|
||||
await this.handleRandomReward(num);
|
||||
|
||||
let returnGoods = new Array();
|
||||
for(let goods of this.rewards) {
|
||||
let goodInfo = getGoodById(goods.gid);
|
||||
if(goodInfo.goodType == GOOD_TYPE.EQUIP) { // 装备
|
||||
let result = await this.rewardWeapons(goodInfo, {id: goods.gid, cnt: goods.count });
|
||||
for(let obj of result) {
|
||||
returnGoods.push({dropType: goods.type, ...obj})
|
||||
}
|
||||
}
|
||||
}
|
||||
let rewardObject = new Reward(this.roleId, this.roleName, this.rewards);
|
||||
let returnGoods = await rewardObject.saveReward();
|
||||
return returnGoods;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private async rewardWeapons (dicGood: any, weapon: {id:number,cnt:number }) {
|
||||
|
||||
let weaponsData = [];
|
||||
let cnt = weapon.cnt;
|
||||
while (cnt > 0) {
|
||||
const seqId = await CounterModel.getNewCounter('eid');
|
||||
const equipInfo = {
|
||||
roleId: this.roleId,
|
||||
roleName: this.roleName,
|
||||
eid: weapon.id,
|
||||
eName: dicGood.name,
|
||||
seqId,
|
||||
quality: dicGood.lv,
|
||||
type: dicGood.goodType
|
||||
}
|
||||
const equip = await EquipModel.createEquip(equipInfo);
|
||||
cnt -= 1;
|
||||
weaponsData.push(equip);
|
||||
}
|
||||
return weaponsData;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,7 +2,8 @@ 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';
|
||||
import { genCode, decodeStr, decodeStrSingle, Reward } from '../../../util/util';
|
||||
import { EVENT_STATUS, EVENT_RECORD_STATUS, EVENT_TYPE } from '../../../consts/consts';
|
||||
|
||||
export default function(app: Application) {
|
||||
return new EventBattleHandler(app);
|
||||
@@ -13,19 +14,78 @@ export class EventBattleHandler {
|
||||
}
|
||||
|
||||
// 获取关卡列表
|
||||
async receiveEventReward(msg: { eventCode: string, isSuccess: boolean }, session: BackendSession) {
|
||||
async receiveEventReward(msg: { eventId: number, eventCode: string, isSuccess: boolean }, session: BackendSession) {
|
||||
const {eventId, eventCode, isSuccess} = msg;
|
||||
let roleId = session.get('roleId');
|
||||
let roleName = session.get('roleName');
|
||||
let eventStatus = session.get('eventStatus');
|
||||
|
||||
let event = await EventRecordModel.getEventRecordByCode(roleId, eventCode);
|
||||
if(!event) {
|
||||
return { code: 202, data: '未找到记录' }
|
||||
}
|
||||
let { status, type, eventId: dataEventId } = event;
|
||||
let flag; // 检查状态是否正确
|
||||
if(type == EVENT_TYPE.BATTLE) {
|
||||
if(status == EVENT_RECORD_STATUS.BATTLE_SUCCESS && isSuccess) {
|
||||
flag = true;
|
||||
} else if (status == EVENT_RECORD_STATUS.BATTLE_FAIL && !isSuccess) {
|
||||
flag = true;
|
||||
}
|
||||
} else {
|
||||
if(status == EVENT_RECORD_STATUS.WAITING) {
|
||||
flag = true;
|
||||
}
|
||||
}
|
||||
if(!flag) {
|
||||
return { code: 202, data: '状态错误' }
|
||||
}
|
||||
if(dataEventId != eventId) {
|
||||
return { code: 202, data: '事件id错误' }
|
||||
}
|
||||
let dicEvent = getGamedata('dic_zyz_event');
|
||||
let curEvent = dicEvent.find(cur => cur.eventID == eventId);
|
||||
if(!curEvent) {
|
||||
return { code: 202, data: '未找到该事件' };
|
||||
}
|
||||
|
||||
// 保存状态
|
||||
let result = await EventRecordModel.setStatusByCode(roleId, eventCode, isSuccess?EVENT_RECORD_STATUS.SUCCESS_RECEIVED:EVENT_RECORD_STATUS.FAIL_RECEIVED);
|
||||
// 保存奖励
|
||||
let rewardStr = isSuccess?curEvent.winReward:curEvent.loseReward;
|
||||
let rewards = decodeStr('fixReward', rewardStr);
|
||||
let rewardObject = new Reward(roleId, roleName, rewards);
|
||||
let goods = await rewardObject.saveReward();
|
||||
if(eventStatus == EVENT_STATUS.STARTING) { // 如果是第一次开启的挑战,保存成开启状态
|
||||
await RoleModel.setEventStatus(roleId, EVENT_STATUS.OPEN);
|
||||
// 第一场时间挑战完,开始正常刷新事件,所以刷新时间也重置起来
|
||||
session.set('eventStatus', EVENT_STATUS.OPEN);
|
||||
session.push('eventStatus', () => {});
|
||||
}
|
||||
// 推送消息刷新
|
||||
await checkEvent(this.app, session, true);
|
||||
return {
|
||||
code: 200,
|
||||
data: {
|
||||
eventCode: result.eventCode,
|
||||
eventId: result.eventId,
|
||||
status: result.status,
|
||||
goods
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function setBattleStatus(roleId: string, battleId: number , isSuccess: boolean, battleCode: string, eventStatus: number = 0) {
|
||||
export async function setBattleStatus(app: Application, session: FrontendOrBackendSession, roleId: string, battleId: number , isSuccess: boolean, battleCode: string) {
|
||||
let now = new Date();
|
||||
let refTime = eventStatus == 2? getEventTime(now): 0;
|
||||
let eventStatus = session.get('eventStatus');
|
||||
let refTime = eventStatus == EVENT_STATUS.OPEN? getEventTime(now): 0;
|
||||
console.log('***setBattleStatus', eventStatus, refTime)
|
||||
|
||||
let result = await EventRecordModel.setBattleStatus(roleId, battleId, refTime, isSuccess?1:0, battleCode);
|
||||
if(eventStatus == 1) {
|
||||
await RoleModel.setEventStatus(roleId, 2);
|
||||
}
|
||||
|
||||
let { BATTLE_SUCCESS, BATTLE_FAIL } = EVENT_RECORD_STATUS;
|
||||
let result = await EventRecordModel.setBattleStatus(roleId, battleId, refTime, isSuccess?BATTLE_SUCCESS:BATTLE_FAIL, battleCode);
|
||||
await checkEvent(app, session, true);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -47,19 +107,19 @@ export function getEventTime(now: Date) {
|
||||
|
||||
export async function startEvent(app: Application, session: FrontendOrBackendSession) {
|
||||
|
||||
// console.log('*******setEventStatus')
|
||||
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);
|
||||
let event = await refreshEvent(1, roleId, roleName, 0); // 刷新初始的一件
|
||||
await RoleModel.setEventStatus(roleId, EVENT_STATUS.STARTING);
|
||||
session.set('eventStatus', EVENT_STATUS.STARTING);
|
||||
session.push('eventStatus', () => {});
|
||||
// console.log('*******setEventStatus')
|
||||
pushEventMsg(app, roleId, channelName, { event }); // 推送
|
||||
|
||||
}
|
||||
|
||||
export async function checkEvent(app: Application, session: FrontendOrBackendSession) {
|
||||
export async function checkEvent(app: Application, session: FrontendOrBackendSession, isForce:boolean = false) {
|
||||
|
||||
try {
|
||||
|
||||
@@ -68,7 +128,7 @@ export async function checkEvent(app: Application, session: FrontendOrBackendSes
|
||||
|
||||
let roleName = session.get('roleName');
|
||||
let channelName = roleId;
|
||||
let eventStatus = session.get('eventStatus')||0;
|
||||
let eventStatus = session.get('eventStatus')||EVENT_STATUS.WAITING;
|
||||
|
||||
let eventTime = session.get('getEventTime')||0;
|
||||
let now = new Date();
|
||||
@@ -78,14 +138,14 @@ export async function checkEvent(app: Application, session: FrontendOrBackendSes
|
||||
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(!!channel && (eventTime < t || isForce)) { // 第一次登陆后可以刷新了
|
||||
|
||||
if (eventStatus == 1) {
|
||||
if (eventStatus == EVENT_STATUS.STARTING) {
|
||||
let event = await EventRecordModel.getEventRecordByTime(roleId, 0);
|
||||
pushEventMsg(app, roleId, channelName, { event }); // 推送
|
||||
} else if( eventStatus == 2 ) {
|
||||
session.set('getEventTime', t);
|
||||
session.push('getEventTime', () => {});
|
||||
} else if( eventStatus == EVENT_STATUS.OPEN ) {
|
||||
|
||||
let event = await EventRecordModel.getEventRecordByTime(roleId, t);
|
||||
if(event.length == 0) { // 刷新
|
||||
@@ -96,6 +156,8 @@ export async function checkEvent(app: Application, session: FrontendOrBackendSes
|
||||
console.log(event)
|
||||
// 推送
|
||||
pushEventMsg(app, roleId, channelName, { event });
|
||||
session.set('getEventTime', t);
|
||||
session.push('getEventTime', () => {});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -109,16 +171,23 @@ export async function checkEvent(app: Application, session: FrontendOrBackendSes
|
||||
async function refreshEvent(num: number, roleId: string, roleName: string, t) {
|
||||
let event = new Array();
|
||||
let dicEvent = getGamedata('dic_zyz_event');
|
||||
let role = await RoleModel.findByRoleId(roleId);
|
||||
dicEvent = dicEvent.filter(cur => { // 筛选适合等级
|
||||
let { suitLevel } = cur;
|
||||
suitLevel = decodeStrSingle('eventSuitLevel', suitLevel);
|
||||
return suitLevel.min <= role.lv && suitLevel.max >= role.lv
|
||||
});
|
||||
let historyRecord = await EventRecordModel.getHostoryEventRecord(roleId);
|
||||
let {history, turn} = historyRecord;
|
||||
let randomList = dicEvent.filter(cur => {
|
||||
return history.find(ccur => {
|
||||
return ccur.eventId == cur.eventID;
|
||||
return ccur.eventId != cur.eventID;
|
||||
});
|
||||
});
|
||||
console.log(JSON.stringify(randomList));
|
||||
|
||||
for(let i = 0; i < num; i++) {
|
||||
if(randomList.length == 0) { // 刷过新的一轮了
|
||||
if(randomList.length == 0) { // 一轮刷新过,开始新的一轮,保证所有事件都能刷新一遍
|
||||
turn ++;
|
||||
randomList = [...dicEvent];
|
||||
}
|
||||
@@ -128,7 +197,8 @@ async function refreshEvent(num: number, roleId: string, roleName: string, t) {
|
||||
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
|
||||
roleName, turn, type: dic.eventType, battleId: dic.warId||0, quality: dic.quality,
|
||||
status: EVENT_RECORD_STATUS.WAITING
|
||||
});
|
||||
event.push(data)
|
||||
randomList.splice(index, 1);
|
||||
|
||||
@@ -131,9 +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) {
|
||||
} else if (warInfo.warType == WAR_TYPE.EVENT) {
|
||||
// 记录事件状态
|
||||
await setBattleStatus(roleId, battleId, isSuccess, battleCode, session.get('eventStatus'));
|
||||
await setBattleStatus(this.app, session, roleId, battleId, isSuccess, battleCode);
|
||||
}
|
||||
|
||||
let warReward = new WarReward(roleId, roleName, battleId, isSuccess);
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
|
||||
import { getWarById, getGoodById } from './gamedata';
|
||||
import { CounterModel } from '../db/Counter';
|
||||
import { EquipModel } from '../db/Equip';
|
||||
import { GOOD_TYPE } from '../consts/consts';
|
||||
|
||||
export function genCode(len) {
|
||||
const chars = '123456789ABCDEFGHJKLMNPQRSTWXYZabcdefghijklmnopqrstuvwxyz';
|
||||
@@ -11,29 +15,96 @@
|
||||
}
|
||||
|
||||
export function decodeStr(type, str) {
|
||||
if(str == '&') {
|
||||
if(str == '&'|| !str) {
|
||||
return []
|
||||
} else {
|
||||
return str.split('|').map(cur => {
|
||||
let arr = cur.split('&');
|
||||
let result = {};
|
||||
switch (type) {
|
||||
case 'fixReward': {
|
||||
let [gid, count] = arr;
|
||||
result = { gid: parseInt(gid), count: parseInt(count)};
|
||||
break;
|
||||
}
|
||||
case 'conditionReward': {
|
||||
let [gid, count, condition] = arr;
|
||||
result = { gid: parseInt(gid), count: parseInt(count), condition: parseInt(condition) };
|
||||
break;
|
||||
}
|
||||
case 'randomReward': {
|
||||
let [gid, count, frequency] = arr;
|
||||
result = { gid: parseInt(gid), count: parseInt(count), frequency: parseInt(frequency) };
|
||||
}
|
||||
}
|
||||
return result;
|
||||
return decodeStrSingle(type,cur);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
export function decodeStrSingle(type, str) { //单层,返回对象而不是数组
|
||||
if(str == '&'|| !str) {
|
||||
return {}
|
||||
} else {
|
||||
let arr = str.split('&');
|
||||
let result = {};
|
||||
switch (type) {
|
||||
case 'fixReward': {
|
||||
let [gid, count] = arr;
|
||||
if(isNaN(gid) || isNaN(count)) throw new Error('格式错误');
|
||||
result = { gid: parseInt(gid), count: parseInt(count)};
|
||||
break;
|
||||
}
|
||||
case 'conditionReward': {
|
||||
let [gid, count, condition] = arr;
|
||||
if(isNaN(gid) || isNaN(count)) throw new Error('格式错误');
|
||||
result = { gid: parseInt(gid), count: parseInt(count), condition: parseInt(condition) };
|
||||
break;
|
||||
}
|
||||
case 'randomReward': {
|
||||
let [gid, count, frequency] = arr;
|
||||
if(isNaN(gid) || isNaN(count)) throw new Error('格式错误');
|
||||
result = { gid: parseInt(gid), count: parseInt(count), frequency: parseInt(frequency) };
|
||||
}
|
||||
case 'eventSuitLevel': {
|
||||
let [min, max] = arr;
|
||||
if(isNaN(min) || isNaN(max)) throw new Error('格式错误');
|
||||
result = { min: parseInt(min), max: parseInt(max) };
|
||||
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
// 获取物品
|
||||
export class Reward {
|
||||
roleId: string;
|
||||
roleName: string;
|
||||
rewards: Array<{type?: number, gid: number, count: number}>;
|
||||
|
||||
constructor(roleId: string, roleName: string, rewards: Array<{type?: number, gid: number, count: number}>) {
|
||||
this.roleId = roleId;
|
||||
this.roleName = roleName;
|
||||
this.rewards = rewards;
|
||||
}
|
||||
|
||||
public async saveReward() {
|
||||
|
||||
let returnGoods = new Array();
|
||||
for(let goods of this.rewards) {
|
||||
let goodInfo = getGoodById(goods.gid);
|
||||
if(goodInfo.goodType == GOOD_TYPE.EQUIP) { // 装备
|
||||
let result = await this.rewardWeapons(goodInfo, {id: goods.gid, cnt: goods.count });
|
||||
for(let obj of result) {
|
||||
returnGoods.push({dropType: goods.type, ...obj})
|
||||
}
|
||||
}
|
||||
}
|
||||
return returnGoods;
|
||||
}
|
||||
|
||||
private async rewardWeapons (dicGood: any, weapon: {id:number,cnt:number }) {
|
||||
|
||||
let weaponsData = [];
|
||||
let cnt = weapon.cnt;
|
||||
while (cnt > 0) {
|
||||
const seqId = await CounterModel.getNewCounter('eid');
|
||||
const equipInfo = {
|
||||
roleId: this.roleId,
|
||||
roleName: this.roleName,
|
||||
eid: weapon.id,
|
||||
eName: dicGood.name,
|
||||
seqId,
|
||||
quality: dicGood.lv,
|
||||
type: dicGood.goodType
|
||||
}
|
||||
const equip = await EquipModel.createEquip(equipInfo);
|
||||
cnt -= 1;
|
||||
weaponsData.push(equip);
|
||||
}
|
||||
return weaponsData;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user