添加主动获取奇遇事件,奇遇添加答题逻辑
This commit is contained in:
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user