From 501ecde7fc13730a9fcd11e22419744a131b8e50 Mon Sep 17 00:00:00 2001 From: zhl Date: Mon, 31 May 2021 20:29:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8C=91=E6=88=98=E5=AF=B9?= =?UTF-8?q?=E5=BA=97=E9=93=BA=E8=87=AA=E5=AE=9A=E4=B9=89=E9=A2=98=E5=BA=93?= =?UTF-8?q?=E7=9A=84=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/controllers/exam.controller.ts | 14 ++++++++- src/models/shop/ShopExam.ts | 6 ++++ src/models/shop/ShopPuzzle.ts | 39 +++++++++++++++++++++++++- 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/api/controllers/exam.controller.ts b/src/api/controllers/exam.controller.ts index d63df05..f0d82d4 100644 --- a/src/api/controllers/exam.controller.ts +++ b/src/api/controllers/exam.controller.ts @@ -7,6 +7,7 @@ import { PuzzleSession, PuzzleStatusClass } from '../../models/match/PuzzleSessi import { calcExamScore, getRank, transformRecord, updateExamRank } from '../../services/GameLogic' import { Shop, validShopId } from '../../models/shop/Shop' import { UserReward } from '../../models/UserReward' +import { ShopPuzzle } from '../../models/shop/ShopPuzzle' class ExamController extends BaseController { @role('anon') @@ -42,11 +43,22 @@ class ExamController extends BaseController { for (let _r of questions) { history.questions.set(_r.id, _r.compactRecord(true)) } - } else { + } else if (record.source === 1) { results = record.transQuestion() for (let _r of record.questions) { history.questions.set(_r._id + '', _r.compactRecord(true)) } + } else if (record.source === 2) { + if (record.shopCates && record.shopCates.length > 0) { + params = { groups: { $in: record.shopCates } } + } + let questions = await ShopPuzzle.randomQuestions(params, record.qcount) + let _results = [] + for (let _r of questions) { + history.questions.set(_r.id, _r.compactRecord(true)) + _results.push(_r.compactRecord(false)) + } + results = _results } let stat = new PuzzleStatusClass() stat.timeLast = now diff --git a/src/models/shop/ShopExam.ts b/src/models/shop/ShopExam.ts index 6146cdb..753dcc4 100644 --- a/src/models/shop/ShopExam.ts +++ b/src/models/shop/ShopExam.ts @@ -97,6 +97,12 @@ export class ShopExamClass extends BaseModule { */ @prop({ type: () => [String] }) public qtypes: string[] + /** + * 店铺自定义tag + * @type {string[]} + */ + @prop({ type: () => [String] }) + public shopCates: string[] /** * 单次活动题目数量, 0为所有 * @type {number} diff --git a/src/models/shop/ShopPuzzle.ts b/src/models/shop/ShopPuzzle.ts index 250e51e..df10acb 100644 --- a/src/models/shop/ShopPuzzle.ts +++ b/src/models/shop/ShopPuzzle.ts @@ -3,7 +3,9 @@ import { getModelForClass, index, modelOptions, prop, ReturnModelType } from '@t import { BaseModule } from '../Base' import { Base, TimeStamps } from '@typegoose/typegoose/lib/defaultClasses' import { checkJson, noJson } from '../../decorators/nojson' -import { PuzzleClass } from '../content/Puzzle' +import { Puzzle, PuzzleClass } from '../content/Puzzle' +import { QCategoryCache } from '../../services/QCategoryCache' +import { CompactPuzzleClass } from '../match/PuzzleSession' const jsonExcludeKeys = ['updatedAt', '__v'] @@ -150,6 +152,41 @@ export class ShopPuzzleClass extends BaseModule { public static async allTags(shop: string) { return ShopPuzzle.distinct('groups', { shop, deleted: 0 }) } + + public static async randomQuestions(this: ReturnModelType, options: any, count: number) { + let filters = { status: 1, is_hide: 0, deleted: 0 } + Object.assign(filters, options) + let records = await this.aggregate([{ $match: filters }, { $sample: { size: count } }]).exec() + let results = [] + for (let record of records) { + let o = new ShopPuzzle({}) + Object.assign(o, record) + results.push(o) + } + return results + } + + public compactRecord(withAnswer: boolean) { + let answers = [] + for (let i = 1; i <= 4; i++) { + if (this[`a${i}`]) { + answers.push(this[`a${i}`]) + } + } + let result = new CompactPuzzleClass() + result.id = this._id + '' + result.title = this.question + + result.type = this.type + result.category = this.groups.join(',') + result.quality = this.quality + if (!withAnswer) { + answers.randomSort() + } + result.source = 2 + result.answers = answers + return result + } } export const ShopPuzzle = getModelForClass(ShopPuzzleClass, { existingConnection: ShopPuzzleClass.db })