mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
wip: migrating game handler
This commit is contained in:
1 parent
61d1871e95
commit
6043226142
26 files changed
+85
-364
No files matched your search
@@ -2,7 +2,7 @@ import { z } from 'zod'
|
|||||||
import { fileConditionSchema } from './file-condition.js'
|
import { fileConditionSchema } from './file-condition.js'
|
||||||
import { toArray } from '../utils.js'
|
import { toArray } from '../utils.js'
|
||||||
|
|
||||||
export const conditionSchema = z.object({
|
const conditionSchemaBase = z.object({
|
||||||
command: z.preprocess(toArray, z.array(z.string()).optional()),
|
command: z.preprocess(toArray, z.array(z.string()).optional()),
|
||||||
output: z.preprocess(toArray, z.array(z.string()).optional()),
|
output: z.preprocess(toArray, z.array(z.string()).optional()),
|
||||||
error: z.preprocess(toArray, z.array(z.string()).optional()),
|
error: z.preprocess(toArray, z.array(z.string()).optional()),
|
||||||
@@ -10,4 +10,12 @@ export const conditionSchema = z.object({
|
|||||||
files: z.array(fileConditionSchema).optional(),
|
files: z.array(fileConditionSchema).optional(),
|
||||||
})
|
})
|
||||||
|
|
||||||
export type Condition = z.infer<typeof conditionSchema>
|
export type Condition = z.infer<typeof conditionSchemaBase> & {
|
||||||
|
not?: Condition
|
||||||
|
or?: Condition[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export const conditionSchema = conditionSchemaBase.extend({
|
||||||
|
not: z.lazy(() => conditionSchema).optional(),
|
||||||
|
or: z.lazy(() => z.array(conditionSchema)).optional(),
|
||||||
|
}) as z.ZodType<Condition>
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
import { z } from 'zod'
|
||||||
|
import { conditionSchema } from './condition.js'
|
||||||
|
import { responseSchema } from './response.js'
|
||||||
|
|
||||||
|
export const exceptionSchema = z.object({
|
||||||
|
condition: conditionSchema,
|
||||||
|
responses: z.array(responseSchema),
|
||||||
|
})
|
||||||
|
|
||||||
|
export type Exception = z.infer<typeof exceptionSchema>
|
||||||
|
|
||||||
|
export const stageExceptionSchema = exceptionSchema.extend({
|
||||||
|
catchAll: z.boolean().default(false),
|
||||||
|
})
|
||||||
|
|
||||||
|
export type StageException = z.infer<typeof stageExceptionSchema>
|
||||||
|
|
||||||
|
export const globalExceptionSchema = exceptionSchema.extend({
|
||||||
|
requirements: z.array(z.string()),
|
||||||
|
})
|
||||||
|
|
||||||
|
export type GlobalException = z.infer<typeof globalExceptionSchema>
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
import { z } from 'zod'
|
|
||||||
import { conditionSchema } from './condition.js'
|
|
||||||
import { responseSchema } from './response.js'
|
|
||||||
|
|
||||||
export const globalExceptionSchema = z.object({
|
|
||||||
condition: conditionSchema,
|
|
||||||
responses: z.array(responseSchema),
|
|
||||||
requirements: z.array(z.string()),
|
|
||||||
})
|
|
||||||
|
|
||||||
export type GlobalException = z.infer<typeof globalExceptionSchema>
|
|
||||||
@@ -6,6 +6,6 @@ export * from './response.js'
|
|||||||
export * from './command-responder.js'
|
export * from './command-responder.js'
|
||||||
export * from './stage-exception.js'
|
export * from './stage-exception.js'
|
||||||
export * from './stage.js'
|
export * from './stage.js'
|
||||||
export * from './global-exception.js'
|
export * from './exception.js'
|
||||||
export * from './quest.js'
|
export * from './quest.js'
|
||||||
export * from './quest-detail-response.js'
|
export * from './quest-detail-response.js'
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
|
|
||||||
export const questDetailResponseSchema = z.object({
|
export const questDetailResponseSchema = z.object({
|
||||||
_id: z.string(),
|
id: z.string(),
|
||||||
title: z.string(),
|
title: z.string(),
|
||||||
instruction: z.string(),
|
instruction: z.string(),
|
||||||
requirements: z.array(z.string()),
|
requirements: z.array(z.string()),
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
import { stageSchema } from './stage.js'
|
import { stageSchema } from './stage.js'
|
||||||
import { globalExceptionSchema } from './global-exception.js'
|
import { globalExceptionSchema } from './exception.js'
|
||||||
import { requirementSchema } from './requirement.js'
|
import { requirementSchema } from './requirement.js'
|
||||||
|
|
||||||
export const questSchema = z.object({
|
export const questSchema = z.object({
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
import { z } from 'zod'
|
|
||||||
import { conditionSchema } from './condition.js'
|
|
||||||
import { responseSchema } from './response.js'
|
|
||||||
|
|
||||||
export const stageExceptionSchema = z.object({
|
|
||||||
condition: conditionSchema,
|
|
||||||
responses: z.array(responseSchema),
|
|
||||||
})
|
|
||||||
|
|
||||||
export type StageException = z.infer<typeof stageExceptionSchema>
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
import { stageExceptionSchema } from './stage-exception.js'
|
import { stageExceptionSchema } from './exception.js'
|
||||||
import { conditionSchema } from './condition.js'
|
import { conditionSchema } from './condition.js'
|
||||||
import { responseSchema } from './response.js'
|
import { responseSchema } from './response.js'
|
||||||
import { hintSchema } from './hint.js'
|
import { hintSchema } from './hint.js'
|
||||||
|
|||||||
@@ -1,22 +0,0 @@
|
|||||||
import { Schema } from 'mongoose'
|
|
||||||
import { conditionSchema, ICondition } from './condition.js'
|
|
||||||
import { responseSchema, IResponse } from './response.js'
|
|
||||||
import { hintSchema, IHint } from './hint.js'
|
|
||||||
|
|
||||||
export interface ICommandResponder extends IHint {
|
|
||||||
condition?: ICondition
|
|
||||||
responses: IResponse[]
|
|
||||||
}
|
|
||||||
|
|
||||||
export const commandResponderSchema = new Schema<ICommandResponder>({
|
|
||||||
condition: {
|
|
||||||
type: conditionSchema,
|
|
||||||
required: false,
|
|
||||||
},
|
|
||||||
responses: {
|
|
||||||
type: [responseSchema],
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
commandResponderSchema.add(hintSchema)
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
import { Schema } from 'mongoose'
|
|
||||||
|
|
||||||
export interface IFileCondition {
|
|
||||||
path: string
|
|
||||||
type: string
|
|
||||||
exists: boolean
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ICondition {
|
|
||||||
command?: string[]
|
|
||||||
output?: string[]
|
|
||||||
error?: string[]
|
|
||||||
pwd?: string[]
|
|
||||||
files?: IFileCondition[]
|
|
||||||
or?: ICondition[]
|
|
||||||
not?: ICondition
|
|
||||||
}
|
|
||||||
|
|
||||||
export const fileConditionSchema = new Schema<IFileCondition>({
|
|
||||||
path: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
type: String,
|
|
||||||
exists: {
|
|
||||||
type: Boolean,
|
|
||||||
default: true,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
export const conditionSchema = new Schema<ICondition>({
|
|
||||||
command: [String],
|
|
||||||
output: [String],
|
|
||||||
error: [String],
|
|
||||||
pwd: [String],
|
|
||||||
files: [fileConditionSchema],
|
|
||||||
})
|
|
||||||
|
|
||||||
conditionSchema.add({
|
|
||||||
or: [conditionSchema],
|
|
||||||
not: conditionSchema,
|
|
||||||
})
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
import { Schema } from 'mongoose'
|
|
||||||
import {
|
|
||||||
commandResponderSchema,
|
|
||||||
ICommandResponder,
|
|
||||||
} from './commandResponder.js'
|
|
||||||
|
|
||||||
export interface IException extends ICommandResponder {}
|
|
||||||
|
|
||||||
export const exceptionSchema = new Schema<IException>({})
|
|
||||||
|
|
||||||
exceptionSchema.add(commandResponderSchema)
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
import { Schema } from 'mongoose'
|
|
||||||
import { exceptionSchema, IException } from './exception.js'
|
|
||||||
import { requirementSchema, IRequirement } from './requirement.js'
|
|
||||||
|
|
||||||
export interface IGlobalException extends IException, IRequirement {}
|
|
||||||
|
|
||||||
export const globalExceptionSchema = new Schema<IGlobalException>({})
|
|
||||||
|
|
||||||
globalExceptionSchema.add(exceptionSchema)
|
|
||||||
globalExceptionSchema.add(requirementSchema)
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
import { Schema } from 'mongoose'
|
|
||||||
|
|
||||||
export interface IHint {
|
|
||||||
hints: string[]
|
|
||||||
}
|
|
||||||
|
|
||||||
export const hintSchema = new Schema({
|
|
||||||
hints: {
|
|
||||||
type: [String],
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
@@ -1,15 +1,9 @@
|
|||||||
import mongoose from 'mongoose'
|
import mongoose from 'mongoose'
|
||||||
|
|
||||||
export { User, IUser } from './user.js'
|
export { User, IUser } from './user.js'
|
||||||
export { Quest, IQuest } from './quest.js'
|
export { Session, ISession } from './session.js'
|
||||||
export { Session, ISession, ITask } from './session.js'
|
|
||||||
export { Command, ICommand } from './command.js'
|
export { Command, ICommand } from './command.js'
|
||||||
export { UserProfile, IUserProfile, IProgress } from './userProfile.js'
|
export { UserProfile, IUserProfile, IProgress } from './userProfile.js'
|
||||||
export { IFileCondition, ICondition } from './condition.js'
|
|
||||||
export { IStage } from './stage.js'
|
|
||||||
export { IRequirement } from './requirement.js'
|
|
||||||
export { IResponse } from './response.js'
|
|
||||||
export { IException } from './exception.js'
|
|
||||||
|
|
||||||
export default async function connectDB(uri: string) {
|
export default async function connectDB(uri: string) {
|
||||||
const db = await mongoose.connect(uri)
|
const db = await mongoose.connect(uri)
|
||||||
|
|||||||
@@ -1,52 +0,0 @@
|
|||||||
import { model, Schema } from 'mongoose'
|
|
||||||
import { IStage, stageSchema } from './stage.js'
|
|
||||||
import { globalExceptionSchema, IGlobalException } from './globalException.js'
|
|
||||||
import { IRequirement } from './requirement.js'
|
|
||||||
|
|
||||||
export interface IQuest extends IRequirement {
|
|
||||||
_id: string
|
|
||||||
title: string
|
|
||||||
image: string
|
|
||||||
instruction: string
|
|
||||||
stages: IStage[]
|
|
||||||
exceptions: IGlobalException[]
|
|
||||||
}
|
|
||||||
|
|
||||||
export const Quest = model<IQuest>(
|
|
||||||
'Quest',
|
|
||||||
new Schema<IQuest>({
|
|
||||||
_id: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
title: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
image: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
requirements: {
|
|
||||||
type: [
|
|
||||||
{
|
|
||||||
type: String,
|
|
||||||
ref: 'Quest',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
instruction: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
stages: {
|
|
||||||
type: [stageSchema],
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
exceptions: {
|
|
||||||
type: [globalExceptionSchema],
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
)
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
import { Schema } from 'mongoose'
|
|
||||||
|
|
||||||
export interface IRequirement {
|
|
||||||
requirements: string[]
|
|
||||||
}
|
|
||||||
|
|
||||||
export const requirementSchema = new Schema<IRequirement>({
|
|
||||||
requirements: [
|
|
||||||
{
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
})
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
import { Schema } from 'mongoose'
|
|
||||||
|
|
||||||
export interface IResponse {
|
|
||||||
type: 'narrative' | 'dialogue'
|
|
||||||
content: string[]
|
|
||||||
speaker?: string
|
|
||||||
color?: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export const responseSchema = new Schema<IResponse>({
|
|
||||||
type: {
|
|
||||||
type: String,
|
|
||||||
enum: ['narrative', 'dialogue'],
|
|
||||||
default: 'dialogue',
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
content: {
|
|
||||||
type: [String],
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
speaker: { type: String, default: 'Ada' },
|
|
||||||
color: String,
|
|
||||||
})
|
|
||||||
@@ -1,7 +1,5 @@
|
|||||||
import { model, Schema, Types } from 'mongoose'
|
import { model, Schema, Types } from 'mongoose'
|
||||||
|
|
||||||
import { IResponse, responseSchema } from './response.js'
|
|
||||||
|
|
||||||
export interface INode {
|
export interface INode {
|
||||||
path: string
|
path: string
|
||||||
type: string
|
type: string
|
||||||
@@ -22,27 +20,6 @@ nodeSchema.add({
|
|||||||
children: [nodeSchema],
|
children: [nodeSchema],
|
||||||
})
|
})
|
||||||
|
|
||||||
export interface ITask {
|
|
||||||
id: string
|
|
||||||
name: string
|
|
||||||
completed: boolean
|
|
||||||
}
|
|
||||||
|
|
||||||
const taskSchema = new Schema<ITask>({
|
|
||||||
id: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
name: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
completed: {
|
|
||||||
type: Boolean,
|
|
||||||
default: false,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
export interface ISession {
|
export interface ISession {
|
||||||
_id: Types.ObjectId
|
_id: Types.ObjectId
|
||||||
user: Types.ObjectId
|
user: Types.ObjectId
|
||||||
@@ -53,9 +30,7 @@ export interface ISession {
|
|||||||
updatedAt: Date
|
updatedAt: Date
|
||||||
finishedAt?: Date
|
finishedAt?: Date
|
||||||
lastActivityAt: Date
|
lastActivityAt: Date
|
||||||
tasks: ITask[]
|
stages: string[]
|
||||||
hints: string[][]
|
|
||||||
responses: IResponse[][]
|
|
||||||
graph: INode
|
graph: INode
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,13 +61,7 @@ export const Session = model<ISession>(
|
|||||||
default: Date.now,
|
default: Date.now,
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
tasks: [taskSchema],
|
stages: [String],
|
||||||
hints: [[String]],
|
|
||||||
responses: {
|
|
||||||
type: [[responseSchema]],
|
|
||||||
required: true,
|
|
||||||
default: [],
|
|
||||||
},
|
|
||||||
graph: {
|
graph: {
|
||||||
type: nodeSchema,
|
type: nodeSchema,
|
||||||
default: {
|
default: {
|
||||||
|
|||||||
@@ -1,31 +0,0 @@
|
|||||||
import { Schema } from 'mongoose'
|
|
||||||
import {
|
|
||||||
commandResponderSchema,
|
|
||||||
ICommandResponder,
|
|
||||||
} from './commandResponder.js'
|
|
||||||
import { stageExceptionSchema, IStageException } from './stageException.js'
|
|
||||||
import { requirementSchema, IRequirement } from './requirement.js'
|
|
||||||
|
|
||||||
export interface IStage extends IRequirement, ICommandResponder {
|
|
||||||
id: string
|
|
||||||
task: string
|
|
||||||
exceptions: IStageException[]
|
|
||||||
}
|
|
||||||
|
|
||||||
export const stageSchema = new Schema<IStage>({
|
|
||||||
id: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
task: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
exceptions: {
|
|
||||||
type: [stageExceptionSchema],
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
stageSchema.add(commandResponderSchema)
|
|
||||||
stageSchema.add(requirementSchema)
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
import { Schema } from 'mongoose'
|
|
||||||
import { exceptionSchema, IException } from './exception.js'
|
|
||||||
|
|
||||||
export interface IStageException extends IException {
|
|
||||||
catchAll: boolean
|
|
||||||
}
|
|
||||||
|
|
||||||
export const stageExceptionSchema = new Schema<IStageException>({
|
|
||||||
catchAll: {
|
|
||||||
type: Boolean,
|
|
||||||
required: true,
|
|
||||||
default: false,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
stageExceptionSchema.add(exceptionSchema)
|
|
||||||
@@ -1,10 +1,11 @@
|
|||||||
import { Command, Quest, Session } from '@linux-odyssey/models'
|
import { Command, Session } from '@linux-odyssey/models'
|
||||||
import { matchedData } from 'express-validator'
|
import { matchedData } from 'express-validator'
|
||||||
import type { Request, Response } from 'express'
|
import type { Request, Response } from 'express'
|
||||||
import CommandHandler from '../../game/commandHandler.js'
|
import CommandHandler from '../../game/commandHandler.js'
|
||||||
import { pushToSession } from '../socket.js'
|
import { pushToSession } from '../socket.js'
|
||||||
import { finishSession } from '../../models/sessionManager.js'
|
import { finishSession } from '../../models/sessionManager.js'
|
||||||
import { asyncHandler } from '../../middleware/error.js'
|
import { asyncHandler } from '../../middleware/error.js'
|
||||||
|
import { questManager } from '../../models/quest.js'
|
||||||
|
|
||||||
export const newCommand = asyncHandler(async (req: Request, res: Response) => {
|
export const newCommand = asyncHandler(async (req: Request, res: Response) => {
|
||||||
const { command, pwd, output, error, params } = matchedData(req)
|
const { command, pwd, output, error, params } = matchedData(req)
|
||||||
@@ -37,7 +38,7 @@ export const newCommand = asyncHandler(async (req: Request, res: Response) => {
|
|||||||
|
|
||||||
await c.save()
|
await c.save()
|
||||||
|
|
||||||
const quest = await Quest.findById(session.quest)
|
const quest = await questManager.get(session.quest)
|
||||||
if (!quest) {
|
if (!quest) {
|
||||||
res.status(404).json({ message: 'quest not found' })
|
res.status(404).json({ message: 'quest not found' })
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -22,10 +22,7 @@ function sessionSummary(session: ISession) {
|
|||||||
function sessionDetail(session: ISession) {
|
function sessionDetail(session: ISession) {
|
||||||
return {
|
return {
|
||||||
...sessionSummary(session),
|
...sessionSummary(session),
|
||||||
hints: session.hints,
|
stages: session.stages,
|
||||||
tasks: session.tasks,
|
|
||||||
responses: session.responses,
|
|
||||||
graph: session.graph,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,14 +2,13 @@ import minimist from 'minimist'
|
|||||||
import { HydratedDocument } from 'mongoose'
|
import { HydratedDocument } from 'mongoose'
|
||||||
import { FileGraph } from '@linux-odyssey/file-graph'
|
import { FileGraph } from '@linux-odyssey/file-graph'
|
||||||
import {
|
import {
|
||||||
ICommand,
|
Condition,
|
||||||
ICondition,
|
FileCondition,
|
||||||
IFileCondition,
|
Quest,
|
||||||
IQuest,
|
Stage,
|
||||||
ISession,
|
Exception,
|
||||||
IStage,
|
} from '@linux-odyssey/constants'
|
||||||
IException,
|
import { ICommand, ISession } from '@linux-odyssey/models'
|
||||||
} from '@linux-odyssey/models'
|
|
||||||
import { pushToSession } from '../api/socket.js'
|
import { pushToSession } from '../api/socket.js'
|
||||||
import SessionHandler, { ExecuteResult } from './sessionHandler.js'
|
import SessionHandler, { ExecuteResult } from './sessionHandler.js'
|
||||||
import { checkFile } from '../containers/cli.js'
|
import { checkFile } from '../containers/cli.js'
|
||||||
@@ -31,7 +30,7 @@ export default class CommandHandler extends SessionHandler {
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
session: HydratedDocument<ISession>,
|
session: HydratedDocument<ISession>,
|
||||||
quest: HydratedDocument<IQuest>,
|
quest: Quest,
|
||||||
commandInput: ICommand,
|
commandInput: ICommand,
|
||||||
params: Record<string, any>
|
params: Record<string, any>
|
||||||
) {
|
) {
|
||||||
@@ -82,7 +81,7 @@ export default class CommandHandler extends SessionHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async isMatch(condition: ICondition): Promise<boolean> {
|
async isMatch(condition: Condition): Promise<boolean> {
|
||||||
return (
|
return (
|
||||||
this.checkKeys(condition) &&
|
this.checkKeys(condition) &&
|
||||||
(await this.checkFiles(condition.files)) &&
|
(await this.checkFiles(condition.files)) &&
|
||||||
@@ -91,7 +90,7 @@ export default class CommandHandler extends SessionHandler {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
checkKeys(condition: ICondition) {
|
checkKeys(condition: Condition) {
|
||||||
return (
|
return (
|
||||||
checkMatch(condition.command, this.commandInput.command) &&
|
checkMatch(condition.command, this.commandInput.command) &&
|
||||||
checkMatch(condition.output, this.commandInput.output) &&
|
checkMatch(condition.output, this.commandInput.output) &&
|
||||||
@@ -100,7 +99,7 @@ export default class CommandHandler extends SessionHandler {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
async checkFiles(files?: IFileCondition[]): Promise<boolean> {
|
async checkFiles(files?: FileCondition[]): Promise<boolean> {
|
||||||
if (!files || files.length === 0) {
|
if (!files || files.length === 0) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -117,14 +116,14 @@ export default class CommandHandler extends SessionHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async checkNot(condition?: ICondition): Promise<boolean> {
|
async checkNot(condition?: Condition): Promise<boolean> {
|
||||||
if (!condition) {
|
if (!condition) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return !(await this.isMatch(condition))
|
return !(await this.isMatch(condition))
|
||||||
}
|
}
|
||||||
|
|
||||||
async checkOr(conditions?: ICondition[]): Promise<boolean> {
|
async checkOr(conditions?: Condition[]): Promise<boolean> {
|
||||||
if (!conditions || conditions.length === 0) {
|
if (!conditions || conditions.length === 0) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -132,7 +131,7 @@ export default class CommandHandler extends SessionHandler {
|
|||||||
return matches.some((m) => m === true)
|
return matches.some((m) => m === true)
|
||||||
}
|
}
|
||||||
|
|
||||||
async checkException(stage: IStage): Promise<IException | null> {
|
async checkException(stage: Stage): Promise<Exception | null> {
|
||||||
const exceptions = stage.exceptions || []
|
const exceptions = stage.exceptions || []
|
||||||
for (const exception of exceptions) {
|
for (const exception of exceptions) {
|
||||||
// eslint-disable-next-line no-await-in-loop
|
// eslint-disable-next-line no-await-in-loop
|
||||||
@@ -200,7 +199,7 @@ export default class CommandHandler extends SessionHandler {
|
|||||||
logger.error('CommandHandler error', {
|
logger.error('CommandHandler error', {
|
||||||
error: (err as Error).message,
|
error: (err as Error).message,
|
||||||
session: this.session._id,
|
session: this.session._id,
|
||||||
quest: this.quest._id,
|
quest: this.quest.id,
|
||||||
command: this.commandInput,
|
command: this.commandInput,
|
||||||
})
|
})
|
||||||
return null
|
return null
|
||||||
|
|||||||
@@ -1,18 +1,16 @@
|
|||||||
import { HydratedDocument } from 'mongoose'
|
import { HydratedDocument } from 'mongoose'
|
||||||
import type {
|
import type {
|
||||||
IException,
|
Quest,
|
||||||
IQuest,
|
Response,
|
||||||
IRequirement,
|
Requirement,
|
||||||
IResponse,
|
Stage,
|
||||||
ISession,
|
Task,
|
||||||
IStage,
|
Exception,
|
||||||
ITask,
|
} from '@linux-odyssey/constants'
|
||||||
} from '@linux-odyssey/models'
|
import { ISession } from '@linux-odyssey/models'
|
||||||
|
|
||||||
export interface ExecuteResult {
|
export interface ExecuteResult {
|
||||||
responses: IResponse[]
|
responses: Response[]
|
||||||
hints: string[]
|
|
||||||
tasks?: ITask[]
|
|
||||||
status: string
|
status: string
|
||||||
stage?: string
|
stage?: string
|
||||||
end?: boolean
|
end?: boolean
|
||||||
@@ -20,12 +18,9 @@ export interface ExecuteResult {
|
|||||||
|
|
||||||
export default class SessionHandler {
|
export default class SessionHandler {
|
||||||
session: HydratedDocument<ISession>
|
session: HydratedDocument<ISession>
|
||||||
quest: HydratedDocument<IQuest>
|
quest: Quest
|
||||||
|
|
||||||
constructor(
|
constructor(session: HydratedDocument<ISession>, quest: Quest) {
|
||||||
session: HydratedDocument<ISession>,
|
|
||||||
quest: HydratedDocument<IQuest>
|
|
||||||
) {
|
|
||||||
this.session = session
|
this.session = session
|
||||||
this.quest = quest
|
this.quest = quest
|
||||||
}
|
}
|
||||||
@@ -34,7 +29,7 @@ export default class SessionHandler {
|
|||||||
return this.session
|
return this.session
|
||||||
}
|
}
|
||||||
|
|
||||||
getTask(stage: Partial<IStage>): ITask | undefined {
|
getTask(stage: Partial<Stage>): Task | undefined {
|
||||||
return this.session.tasks.find((t) => t.id === stage.id)
|
return this.session.tasks.find((t) => t.id === stage.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,7 +38,9 @@ export default class SessionHandler {
|
|||||||
return task ? task.completed : false
|
return task ? task.completed : false
|
||||||
}
|
}
|
||||||
|
|
||||||
getUnlockedComponents<T extends IRequirement>(components: T[]): T[] {
|
getUnlockedComponents<T extends { requirements: Requirement }>(
|
||||||
|
components: T[]
|
||||||
|
): T[] {
|
||||||
return components.filter((component) =>
|
return components.filter((component) =>
|
||||||
component.requirements.every((r: string) => this.isCompleted({ id: r }))
|
component.requirements.every((r: string) => this.isCompleted({ id: r }))
|
||||||
)
|
)
|
||||||
@@ -67,39 +64,30 @@ export default class SessionHandler {
|
|||||||
this.session.tasks.push(...newTasks)
|
this.session.tasks.push(...newTasks)
|
||||||
}
|
}
|
||||||
|
|
||||||
execute(stage: IStage): ExecuteResult {
|
execute(stage: Stage): ExecuteResult {
|
||||||
this.session.tasks
|
this.session.tasks
|
||||||
.filter((t) => t.id === stage.id)
|
.filter((t) => t.id === stage.id)
|
||||||
.forEach((t) => {
|
.forEach((t) => {
|
||||||
t.completed = true
|
t.completed = true
|
||||||
})
|
})
|
||||||
|
|
||||||
if (stage.hints && stage.hints.length > 0)
|
|
||||||
this.session.hints.push(stage.hints)
|
|
||||||
|
|
||||||
this.addNewTasks()
|
this.addNewTasks()
|
||||||
|
|
||||||
this.session.responses.push(stage.responses)
|
|
||||||
|
|
||||||
if (stage.id === 'END') {
|
if (stage.id === 'END') {
|
||||||
this.session.status = 'finished'
|
this.session.status = 'finished'
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
responses: stage.responses,
|
responses: stage.responses,
|
||||||
hints: stage.hints,
|
|
||||||
tasks: this.session.tasks,
|
|
||||||
status: this.session.status,
|
status: this.session.status,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
executeException(exception: IException): ExecuteResult {
|
executeException(exception: Exception): ExecuteResult {
|
||||||
const { hints, responses } = exception
|
const { responses } = exception
|
||||||
if (hints && hints.length > 0) this.session.hints.push(hints)
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
responses,
|
responses,
|
||||||
hints,
|
|
||||||
status: this.session.status,
|
status: this.session.status,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
import { TRPCError } from '@trpc/server'
|
import { TRPCError } from '@trpc/server'
|
||||||
import { Quest } from '@linux-odyssey/models'
|
|
||||||
import { QuestDetailResponse } from '@linux-odyssey/constants'
|
import { QuestDetailResponse } from '@linux-odyssey/constants'
|
||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
import { router, publicProcedure } from '../trpc.js'
|
import { router, publicProcedure } from '../trpc.js'
|
||||||
|
import { questManager } from '../models/quest.js'
|
||||||
|
|
||||||
export const questRouter = router({
|
export const questRouter = router({
|
||||||
getQuests: publicProcedure.query(async () => {
|
getQuests: publicProcedure.query(async () => {
|
||||||
const quests = await Quest.find({})
|
const quests = await questManager.getAll()
|
||||||
return quests.map(({ title, _id, requirements }) => ({
|
return quests.map(({ title, id, requirements }) => ({
|
||||||
_id,
|
id,
|
||||||
title,
|
title,
|
||||||
requirements,
|
requirements,
|
||||||
}))
|
}))
|
||||||
@@ -16,15 +16,15 @@ export const questRouter = router({
|
|||||||
getQuestDetail: publicProcedure
|
getQuestDetail: publicProcedure
|
||||||
.input(z.string())
|
.input(z.string())
|
||||||
.query(async (opts): Promise<QuestDetailResponse> => {
|
.query(async (opts): Promise<QuestDetailResponse> => {
|
||||||
const id = opts.input
|
const questId = opts.input
|
||||||
const quest = await Quest.findById(id)
|
const quest = await questManager.get(questId)
|
||||||
if (!quest) {
|
if (!quest) {
|
||||||
throw new TRPCError({
|
throw new TRPCError({
|
||||||
code: 'NOT_FOUND',
|
code: 'NOT_FOUND',
|
||||||
message: 'Quest not found.',
|
message: 'Quest not found.',
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
const { _id, title, instruction, requirements } = quest
|
const { id, title, instruction, requirements } = quest
|
||||||
return { _id, title, instruction, requirements }
|
return { id, title, instruction, requirements }
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
@@ -34,10 +34,7 @@ export const sessionRouter = router({
|
|||||||
status: session.status,
|
status: session.status,
|
||||||
createdAt: session.createdAt,
|
createdAt: session.createdAt,
|
||||||
lastActivityAt: session.lastActivityAt,
|
lastActivityAt: session.lastActivityAt,
|
||||||
hints: session.hints,
|
stages: session.stages,
|
||||||
tasks: session.tasks,
|
|
||||||
responses: session.responses,
|
|
||||||
graph: session.graph,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw new TRPCError({
|
throw new TRPCError({
|
||||||
|
|||||||
Reference in new issue
Block a user