添加主动获取奇遇事件,奇遇添加答题逻辑

This commit is contained in:
luying
2020-10-15 18:31:36 +08:00
parent 764de8669a
commit 85940779d2
5 changed files with 1102 additions and 27 deletions
@@ -1,9 +1,9 @@
import { Application, BackendSession } from 'pinus';
import { getGamedata } from '../../../util/gamedata';
import { EventRecordModel } from '../../../db/EventRecord';
import EventRecord, { EventRecordModel } from '../../../db/EventRecord';
import { RoleModel } from '../../../db/Role';
import { EVENT_STATUS, EVENT_RECORD_STATUS, EVENT_TYPE } from '../../../consts/consts';
import { checkEvent } from '../../../services/eventSercive';
import { EVENT_STATUS, EVENT_RECORD_STATUS } from '../../../consts/consts';
import { checkEvent, getEventTime, refreshEvent, checkEventStatus, getEventSuccessStatus } from '../../../services/eventSercive';
import { handleFixedReward } from '../../../services/rewardService';
export default function(app: Application) {
@@ -14,9 +14,38 @@ 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 now = new Date();
let t = getEventTime(now);
console.log(t, eventStatus)
let event: Array<EventRecord>;
if (eventStatus == EVENT_STATUS.STARTING) {
event = await EventRecordModel.getEventRecordByTime(roleId, 0);
} else if( eventStatus == EVENT_STATUS.OPEN ) {
event = await EventRecordModel.getEventRecordByTime(roleId, t);
if(event.length == 0) { // 刷新
const num = 3; // 每次刷3个
event = await refreshEvent(num, roleId, roleName, t);
}
}
return {
code: 200,
data: {
event
}
}
}
// 获取关卡列表
async receiveEventReward(msg: { eventId: number, eventCode: string, isSuccess: boolean }, session: BackendSession) {
const {eventId, eventCode, isSuccess} = msg;
async receiveEventReward(msg: { eventId: number, eventCode: string, answer: number }, session: BackendSession) {
const {eventId, eventCode, answer} = msg;
let roleId = session.get('roleId');
let roleName = session.get('roleName');
let eventStatus = session.get('eventStatus');
@@ -25,22 +54,13 @@ export class EventBattleHandler {
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;
}
}
let { status, type, eventId: dataEventId, question } = event;
let flag = checkEventStatus(type, status);
if(!flag) {
return { code: 202, data: '状态错误' }
}
let isSuccess = getEventSuccessStatus(type, status, {id:question?question.id:0, answer});
if(dataEventId != eventId) {
return { code: 202, data: '事件id错误' }
}
@@ -66,6 +86,7 @@ export class EventBattleHandler {
return {
code: 200,
data: {
isSuccess,
eventCode: result.eventCode,
eventId: result.eventId,
status: result.status,
+68 -5
View File
@@ -2,8 +2,8 @@ import { Application, FrontendOrBackendSession } from 'pinus';
import { getGamedata } from '../util/gamedata';
import { EventRecordModel } from '../db/EventRecord';
import { RoleModel } from '../db/Role';
import { genCode, decodeStrSingle } from '../util/util';
import { EVENT_STATUS, EVENT_RECORD_STATUS } from '../consts/consts';
import { genCode, decodeStrSingle, decodeStr } from '../util/util';
import { EVENT_STATUS, EVENT_RECORD_STATUS, EVENT_TYPE } from '../consts/consts';
export async function setBattleStatus(app: Application, session: FrontendOrBackendSession, roleId: string, battleId: number , isSuccess: boolean, battleCode: string) {
@@ -78,6 +78,7 @@ export async function checkEvent(app: Application, session: FrontendOrBackendSes
} else if( eventStatus == EVENT_STATUS.OPEN ) {
let event = await EventRecordModel.getEventRecordByTime(roleId, t);
if(event.length == 0) { // 刷新
const num = 3; // 每次刷3个
event = await refreshEvent(num, roleId, roleName, t);
@@ -98,7 +99,7 @@ export async function checkEvent(app: Application, session: FrontendOrBackendSes
}
}
async function refreshEvent(num: number, roleId: string, roleName: string, t) {
export 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);
@@ -125,18 +126,27 @@ async function refreshEvent(num: number, roleId: string, roleName: string, t) {
let index = Math.floor(Math.random() * randomList.length);
let dic = randomList[index];
let position = randomPosition(dic.position);
let question = dic.eventType == EVENT_TYPE.QUIZ?randomQuestion(): undefined;
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,
status: EVENT_RECORD_STATUS.WAITING
status: EVENT_RECORD_STATUS.WAITING,
position, question
});
event.push(data)
event.push(data);
randomList.splice(index, 1);
}
return event;
}
function randomPosition(positionStr: string) {
let positionArr = decodeStr('position', positionStr);
let index = Math.floor(Math.random() * positionArr.length);
return positionArr[index]
}
function pushEventMsg(app, roleId, channelName, msg ) {
console.log('***pushEventMsg', channelName)
let channelService = app.get('channelService');
@@ -151,4 +161,57 @@ function pushEventMsg(app, roleId, channelName, msg ) {
sid: tsid
}]);
}
}
export function checkEventStatus(type: number, status: number) {
let flag: boolean; // 检查状态是否正确
if(type == EVENT_TYPE.BATTLE) {
if(status == EVENT_RECORD_STATUS.BATTLE_SUCCESS) {
flag = true;
} else if (status == EVENT_RECORD_STATUS.BATTLE_FAIL) {
flag = true;
} else {
flag = false;
}
} else {
if(status == EVENT_RECORD_STATUS.WAITING) {
flag = true;
} else {
flag = false;
}
}
return flag
}
export function getEventSuccessStatus(type: number, status: number, question: {id: number, answer: number}) {
let isSuccess: boolean; // 检查状态是否正确
if(type == EVENT_TYPE.BATTLE) {
if(status == EVENT_RECORD_STATUS.BATTLE_SUCCESS) {
isSuccess = true;
} else if (status == EVENT_RECORD_STATUS.BATTLE_FAIL) {
isSuccess = false;
}
} else if(type == EVENT_TYPE.BOX) {
isSuccess = true;
} else if (type == EVENT_TYPE.QUIZ) {
isSuccess = checkQuiz(question)
}
return isSuccess
}
function randomQuestion() {
let questions = getGamedata('Questions');
let index = Math.floor(Math.random() * questions.length);
let result = questions[index];
let {id, question, a1, a2, a3, a4, correct} = result;
return {id, question, answer: [a1, a2, a3, a4], correct};
}
function checkQuiz(obj: {id: number, answer: number }) {
let {id, answer} = obj;
let questions = getGamedata('Questions');
let result = questions[id - 1];
return !!result && result.correct == answer;
}
+6
View File
@@ -63,6 +63,12 @@ const moment = require('moment');
result = { id: parseInt(id), value: parseInt(value) };
break;
}
case 'position': {
let [x, y] = arr;
if(isNaN(x) || isNaN(y)) throw new Error('格式错误');
result = { x: parseInt(x), y: parseInt(y) };
break;
}
}
return result;
};