增加一个获取更多题目的接口
This commit is contained in:
parent
e062aff8d6
commit
00866d0f7d
37
doc/api.md
37
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: 图形
|
||||
}]
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
}
|
||||
})
|
||||
}
|
||||
|
24
src/services/QCategoryCache.ts
Normal file
24
src/services/QCategoryCache.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import { singleton } from '../decorators/singleton'
|
||||
import { PuzzleCategory } from '../models/content/PuzzleCategory'
|
||||
|
||||
/**
|
||||
* 用于缓存所有题目的分类
|
||||
*/
|
||||
@singleton
|
||||
export class QCategoryCache {
|
||||
private _cache:Map<string, string> = 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) : ''
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user