增加关卡配置的处理

This commit is contained in:
zhl 2021-04-27 18:37:36 +08:00
parent 5549c8c804
commit 1318b246e2
2 changed files with 35 additions and 4 deletions

View File

@ -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 }

View File

@ -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<string, PuzzleStatusClass>
@ -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 })