corgi/src/api/controllers/puzzle.controller.ts
2021-04-26 15:33:32 +08:00

49 lines
1.2 KiB
TypeScript

import BaseController from '../../common/base.controller'
import { role, router } from '../../decorators/router'
import { Puzzle } from '../../models/content/Puzzle'
import { ZError } from '../../common/ZError'
class PuzzleController extends BaseController {
@role('anon')
@router('post /api/:accountid/puzzle/list')
async list(req, res) {
let { shop, level } = req.params
let count = 10
let records = await Puzzle.aggregate([
{ $match: { status: 1, is_hide: 0, deleted: 0 } },
{ $sample: { size: count } }
]).exec()
return records.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
}
})
}
@role('anon')
@router('post /api/:accountid/puzzle/answer')
async report(req, res) {
let { id, answer } = req.params
if (!id || !answer) {
throw new ZError(11, 'param mismatch')
}
let record = await Puzzle.findById(id)
if (!record) {
throw new ZError(12, 'question not found')
}
let result = record.a1 == answer ? 1 : 0
return {result}
}
}