From 00866d0f7d521abd059817b7eafa1fc2d2cbbe6c Mon Sep 17 00:00:00 2001 From: zhl Date: Fri, 30 Apr 2021 12:19:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=B8=AA=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E6=9B=B4=E5=A4=9A=E9=A2=98=E7=9B=AE=E7=9A=84=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/api.md | 37 +++++++++++++++++++++++- src/api.server.ts | 6 ++-- src/api/controllers/puzzle.controller.ts | 31 ++++++++++++++++++++ src/common/GConfig.ts | 2 +- src/services/GameLogic.ts | 6 +++- src/services/QCategoryCache.ts | 24 +++++++++++++++ 6 files changed, 101 insertions(+), 5 deletions(-) create mode 100644 src/services/QCategoryCache.ts diff --git a/doc/api.md b/doc/api.md index b1e143c..ea6b80c 100644 --- a/doc/api.md +++ b/doc/api.md @@ -46,6 +46,7 @@ "刘步蟾", "刘永福" ], + "category": "体育-体育", // 类型 "type": 1 // 题目类型 1: 普通的文字选择题, 2: 图形 }] } @@ -92,7 +93,7 @@ } } ``` -### 3. 上报题目答案 +### 3. 开始匹配 1. Method: POST 2. URI: /api/:accountid/puzzle/match @@ -119,6 +120,40 @@ } ``` +### 4. 获取关卡更多题目 + +1. Method: POST +2. URI: /api/:accountid/puzzle/more + +| 字段 | 说明 | +| -------- | -------------------------------------- | +| accountid | 帐号id | + +> POST参数 + + +| 字段 | 说明 | +| -------- | -------------------------------------- | +| count | 需要获取的数量 | +| session | 当局的id, 从关卡题目列表中获取 | + +3. Response: JSON + +```js +[{ + "id": "6080f330b9655b5c0467ee5e", // 题目id + "title": "“大丈夫为国捐躯,死而无憾!”这话是谁说的?", // 问题 + "answers": [ // 可选答案 + "刘铭传", + "徐骧", + "刘步蟾", + "刘永福" + ], + "category": "体育-体育", // 类型 + "type": 1 // 题目类型 1: 普通的文字选择题, 2: 图形 +}] + + diff --git a/src/api.server.ts b/src/api.server.ts index bcbff10..266cb52 100644 --- a/src/api.server.ts +++ b/src/api.server.ts @@ -10,8 +10,9 @@ import {RouterMap} from 'decorators/router'; import {mongoose} from "@typegoose/typegoose"; import logger from 'logger/logger'; import config from 'config/config'; -import { initData } from './common/GConfig' +import { initCfgData } from './common/GConfig' import { Schedule } from './clock/Schedule' +import { QCategoryCache } from './services/QCategoryCache' const zReqParserPlugin = require('plugins/zReqParser'); @@ -139,7 +140,8 @@ export class ApiServer { self.registerRouter(); self.setErrHandler(); self.setFormatSend(); - initData() + initCfgData(); + await new QCategoryCache().init(); new Schedule().start() this.server.listen({port: config.api.port}, (err: any, address: any) => { if (err) { diff --git a/src/api/controllers/puzzle.controller.ts b/src/api/controllers/puzzle.controller.ts index e43f03d..7273209 100644 --- a/src/api/controllers/puzzle.controller.ts +++ b/src/api/controllers/puzzle.controller.ts @@ -21,6 +21,7 @@ import { startGame, transformRecord } from '../../services/GameLogic' +import { ObjectId } from 'bson' @@ -104,6 +105,36 @@ class PuzzleController extends BaseController { return { result, answer: record.a1, stats: history.members } } + @role('anon') + @router('post /api/:accountid/puzzle/more') + async moreLevelQuestions(req, res) { + let {session,accountid, count} = req.params + count = count ? +count : 10 + if (!session) { + throw new ZError(11, 'param mismatch') + } + let history = await PuzzleSession.findById(session) + if (!history) { + throw new ZError(13, 'not found match info') + } + if (history.status == 9 || history.hasExpired()) { + history.status = 9 + await history.save() + throw new ZError(17, 'match end') + } + if (!history.members.has(accountid)) { + throw new ZError(14, 'not in current match') + } + let ids = history.questions.map(o => new ObjectId(o)) + let options = {'_id': {$nin: ids}} + let records = await Puzzle.randomQuestions(options, count) + const results = transformRecord(records) + history.questions = history.questions.concat(records.map(o => o._id)) + history.markModified('questions') + await history.save() + return results + } + @role('anon') @router('post /api/:accountid/puzzle/match') async joinMultipleGame(req, res) { diff --git a/src/common/GConfig.ts b/src/common/GConfig.ts index cea1a9f..c7d107e 100644 --- a/src/common/GConfig.ts +++ b/src/common/GConfig.ts @@ -4,7 +4,7 @@ import {mission_vo} from "../config/parsers/mission_vo" import { BaseConst } from '../constants/BaseConst' -export function initData() { +export function initCfgData() { const rP = DataParser.regCommonParser.bind(DataParser); rP(BaseConst.COMPOUND, compound_vo); rP(BaseConst.MISSION, mission_vo); diff --git a/src/services/GameLogic.ts b/src/services/GameLogic.ts index 8bcdc1a..260febe 100644 --- a/src/services/GameLogic.ts +++ b/src/services/GameLogic.ts @@ -8,6 +8,7 @@ import { import { Puzzle } from '../models/content/Puzzle' import { Schedule } from '../clock/Schedule' import { BaseConst } from '../constants/BaseConst' +import { QCategoryCache } from './QCategoryCache' export function transformRecord(records: any[]) { @@ -19,11 +20,14 @@ export function transformRecord(records: any[]) { } } answers.randomSort() + let type = new QCategoryCache().getType(o.tag) + let subType = new QCategoryCache().getType(o.sub_tag) return { id: o._id, title: o.question, answers, - type: 1 + type: 1, + category: type + '-' + subType } }) } diff --git a/src/services/QCategoryCache.ts b/src/services/QCategoryCache.ts new file mode 100644 index 0000000..93904ca --- /dev/null +++ b/src/services/QCategoryCache.ts @@ -0,0 +1,24 @@ +import { singleton } from '../decorators/singleton' +import { PuzzleCategory } from '../models/content/PuzzleCategory' + +/** + * 用于缓存所有题目的分类 + */ +@singleton +export class QCategoryCache { + private _cache:Map = new Map() + + + public async init() { + this._cache = await PuzzleCategory.allCateMap() + } + + /** + * 根据id获取分类名 + * @param {string} id + * @return {string} + */ + public getType(id: string): string { + return this._cache.has(id)? this._cache.get(id) : '' + } +}