129 lines
5.3 KiB
TypeScript
129 lines
5.3 KiB
TypeScript
import { Application, BackendSession } from 'pinus';
|
|
import { getGamedata } from '../../../pubUtils/gamedata';
|
|
import { EventRecordModel } from '../../../db/EventRecord';
|
|
import { RoleModel } from '../../../db/Role';
|
|
import { EVENT_STATUS, EVENT_RECORD_STATUS, EVENT_ANSWER_STATUS } from '../../../consts/consts';
|
|
import { checkEvent, checkEventStatus, getEventSuccessStatus, getEvent, checkQuiz } from '../../../services/eventSercive';
|
|
import { handleFixedReward } from '../../../services/rewardService';
|
|
import { STATUS } from '../../../consts/statusCode';
|
|
import { resResult } from '../../../pubUtils/util';
|
|
|
|
export default function(app: Application) {
|
|
return new EventBattleHandler(app);
|
|
}
|
|
|
|
export class EventBattleHandler {
|
|
constructor(private app: Application) {
|
|
}
|
|
|
|
// 主动获取奇遇事件
|
|
async getEvents(msg: { }, session: BackendSession) {
|
|
let roleId = session.get('roleId');
|
|
let roleName = session.get('roleName');
|
|
let eventStatus = session.get('eventStatus');
|
|
|
|
let event = await getEvent(eventStatus, roleId, roleName);
|
|
|
|
return resResult(STATUS.SUCCESS, { list: event });
|
|
}
|
|
|
|
// 回答问题
|
|
async checkAnswer(msg: { eventId: number, eventCode: string, questionId: number, answer: number }, session: BackendSession) {
|
|
const {eventId, eventCode, questionId, answer} = 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 resResult(STATUS.EVENT_RECORD_NOT_FOUND);
|
|
}
|
|
let { status, type, eventId: dataEventId, question } = event;
|
|
|
|
if(status != EVENT_RECORD_STATUS.WAITING) {
|
|
return resResult(STATUS.EVENT_STATUS_ERROR);
|
|
}
|
|
if(dataEventId != eventId) {
|
|
return resResult(STATUS.EVENT_WRONG_ID);
|
|
}
|
|
let curQuestion = question.find(cur => cur.id == questionId);
|
|
if(!curQuestion) return resResult(STATUS.EVENT_QUESTION_NOT_FOUND);
|
|
|
|
let {isCorrect, answerNo} = checkQuiz(questionId, answer);
|
|
let result = await EventRecordModel.saveEventAnswer(eventCode, questionId, isCorrect?EVENT_ANSWER_STATUS.CORRECT:EVENT_ANSWER_STATUS.WRONG);
|
|
if(!result) {
|
|
return resResult(STATUS.EVENT_RECORD_NOT_FOUND);
|
|
}
|
|
let { question: newQuestion } = result;
|
|
let isClear = true, allCorrect = true;
|
|
for(let {status} of newQuestion) {
|
|
if(status) {
|
|
if(status == EVENT_ANSWER_STATUS.WRONG) {
|
|
allCorrect = false;
|
|
}
|
|
}else {
|
|
isClear = false; allCorrect = false;
|
|
}
|
|
}
|
|
if(isClear) {
|
|
await EventRecordModel.setStatusByCode(roleId, eventCode, allCorrect?EVENT_RECORD_STATUS.BATTLE_SUCCESS:EVENT_RECORD_STATUS.BATTLE_FAIL);
|
|
}
|
|
|
|
return resResult(STATUS.SUCCESS, { eventCode, eventId, questionId, isCorrect, answerNo });
|
|
}
|
|
|
|
// 获取关卡列表
|
|
async receiveEventReward(msg: { eventId: number, eventCode: string }, session: BackendSession) {
|
|
const {eventId, eventCode} = 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 resResult(STATUS.EVENT_RECORD_NOT_FOUND);
|
|
}
|
|
let { status, type, eventId: dataEventId, question } = event;
|
|
|
|
let flag = checkEventStatus(type, status);
|
|
if(!flag) {
|
|
return resResult(STATUS.EVENT_STATUS_ERROR);
|
|
}
|
|
|
|
let checkResult = getEventSuccessStatus(type, status, question);
|
|
if(checkResult.status == -1) {
|
|
return resResult(STATUS.EVENT_QUESTION_NO_ANSWER);
|
|
}
|
|
let isSuccess = checkResult.isSuccess;
|
|
|
|
if(dataEventId != eventId) {
|
|
return resResult(STATUS.EVENT_WRONG_ID);
|
|
}
|
|
let dicEvent = getGamedata('dic_zyz_event');
|
|
let curEvent = dicEvent.find(cur => cur.eventID == eventId);
|
|
if(!curEvent) {
|
|
return resResult(STATUS.EVENT_INFO_NOT_FOUND);
|
|
}
|
|
|
|
// 保存状态
|
|
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 goods = await handleFixedReward(roleId, roleName, rewardStr, 1);
|
|
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 resResult(STATUS.SUCCESS, {
|
|
isSuccess,
|
|
eventCode: result.eventCode,
|
|
eventId: result.eventId,
|
|
status: result.status,
|
|
...goods
|
|
});
|
|
}
|
|
} |