diff --git a/src/api/controllers/exam.controller.ts b/src/api/controllers/exam.controller.ts index 3aedca6..5d865ae 100644 --- a/src/api/controllers/exam.controller.ts +++ b/src/api/controllers/exam.controller.ts @@ -31,17 +31,27 @@ class ExamController extends BaseController { throw new ZError(11, '暂时没有可参加的挑战') } let params = {} - if (record.qtypes && record.qtypes.length > 0) { - params = {$or: [{tag: {$in: record.qtypes}}, {sub_tag: {$in: record.qtypes}}]} - } - let questions = await Puzzle.randomQuestions(params, record.qcount) + let results: any[] = [] let history = new PuzzleSession({ shop, level: 0}) + if (record.source === 0) { + if (record.qtypes && record.qtypes.length > 0) { + params = {$or: [{tag: {$in: record.qtypes}}, {sub_tag: {$in: record.qtypes}}]} + } + let questions = await Puzzle.randomQuestions(params, record.qcount) + results = transformRecord(questions) + for (let _r of questions) { + history.questions.set(_r.id, _r.a1) + } + } else { + results = record.transQuestion() + for (let _r of record.questions) { + history.questions.set(_r._id + '', _r.a1) + } + } let stat = new PuzzleStatusClass() stat.timeLast = now history.members.set(accountid, stat) - for (let record of questions) { - history.questions.set(record.id, record.a1) - } + history.expire = Date.now() + (record.qcount * record.timeone || 90) * 1000 history.type = 2 history.total = record.qcount @@ -49,7 +59,6 @@ class ExamController extends BaseController { history.difficultyMode = 0 history.qtypes = record.qtypes await history.save() - const results = transformRecord(questions) return { session: history.id, records: results, diff --git a/src/models/shop/ShopExam.ts b/src/models/shop/ShopExam.ts index 6643b6c..4de6cc5 100644 --- a/src/models/shop/ShopExam.ts +++ b/src/models/shop/ShopExam.ts @@ -7,8 +7,9 @@ import { import { BaseModule } from '../Base' import { noJson } from '../../decorators/nojson' import { Severity } from '@typegoose/typegoose/lib/internal/constants' +import { Base } from '@typegoose/typegoose/lib/defaultClasses' -export class ShopPuzzleClass { +export class ShopPuzzleClass extends Base{ @prop() public question: string @prop() @@ -158,6 +159,26 @@ export class ShopExamClass extends BaseModule { let sort = {_id: -1} return { opt, sort } } + + public transQuestion() { + return this.questions.map(o => { + let answers = [] + for (let i = 1; i <= 4; i++) { + if (o[`a${ i }`]) { + answers.push(o[`a${ i }`]) + } + } + answers.randomSort() + return { + id: o._id, + title: o.question, + answers, + type: 1, + category: '自定义', + quality: 1 + } + }) + } } export const ShopExam = getModelForClass(ShopExamClass, { existingConnection: ShopExamClass.db })