diff --git a/src/api/controllers/puzzle.controller.ts b/src/api/controllers/puzzle.controller.ts index aeb1163..23b919c 100644 --- a/src/api/controllers/puzzle.controller.ts +++ b/src/api/controllers/puzzle.controller.ts @@ -6,6 +6,8 @@ import { PuzzleStatusClass } from '../../models/match/PuzzleSession' import { ZError } from '../../common/ZError' +import { BaseConst } from '../../constants/BaseConst' +import { mission_vo } from '../../config/parsers/mission_vo' const transformRecord = function (records: any[]) { return records.map(o => { @@ -29,11 +31,15 @@ class PuzzleController extends BaseController { @router('post /api/:accountid/puzzle/list') async list(req, res) { let { shop, level, accountid } = req.params - let count = 10 + level = +level || 1 + const cfgs: mission_vo[] = Array.from(global.$cfg.get(BaseConst.MISSION).values()) + const cfg = cfgs.find(o=>o.number == level) || cfgs[cfgs.length - 1] + let count = cfg.beforehand_enemy || 10 let records = await Puzzle.randomQuestions({}, count) let history = new PuzzleSession({ shop, level }) history.members.set(accountid, new PuzzleStatusClass()) history.questions = records.map(o => o._id) + history.expire = Date.now() + (cfg.time || 90) * 1000 await history.save() const results = transformRecord(records) return { @@ -53,6 +59,11 @@ class PuzzleController extends BaseController { 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') } @@ -81,6 +92,7 @@ class PuzzleController extends BaseController { statMap.errorCount ++ statMap.comboCount = 0 } + history.status = 1 history.markModified('members') await history.save() return { result, stats: history.members } diff --git a/src/models/match/PuzzleSession.ts b/src/models/match/PuzzleSession.ts index d676cd9..29d4167 100644 --- a/src/models/match/PuzzleSession.ts +++ b/src/models/match/PuzzleSession.ts @@ -2,6 +2,8 @@ import { dbconn } from '../../decorators/dbconn' import { getModelForClass, modelOptions, prop } from '@typegoose/typegoose' import { BaseModule } from '../Base' import { Severity } from '@typegoose/typegoose/lib/internal/constants' +import { Base, TimeStamps } from '@typegoose/typegoose/lib/defaultClasses' + @modelOptions({ options: {allowMixed: Severity.ALLOW} @@ -38,9 +40,13 @@ export class PuzzleStatusClass { @prop({default: 0}) maxCombo: number } +// @ts-ignore +export interface PuzzleSessionClass extends Base, TimeStamps { +} + @dbconn() -@modelOptions({ schemaOptions: { collection: 'puzzle_session' } }) -class PuzzleSessionClass extends BaseModule { +@modelOptions({ schemaOptions: { collection: 'puzzle_session', timestamps: true } }) +export class PuzzleSessionClass extends BaseModule { @prop({ _id: false, type: PuzzleStatusClass, default: new Map() }) public members: Map @@ -57,9 +63,22 @@ class PuzzleSessionClass extends BaseModule { @prop() public room: string - + /** + * 比赛状态 + * 0: 未开始答题 + * 1: 答题进行中 + * 9: 结束 + * @type {number} + */ @prop({default: 0}) public status: number + + @prop() + public expire: number + + public hasExpired(): boolean { + return this.expire < Date.now() + } } export const PuzzleSession = getModelForClass(PuzzleSessionClass, { existingConnection: PuzzleSessionClass.db })