refactor: remove unused quest-related schemas and consolidate exports

This commit is contained in:
Justin Lin committed 2025-04-21 21:42:54 +08:00
1 parent d2cdd1304a
commit 1d843f7355
29 files changed
+35 -161

No files matched your search

-1
View File
@@ -1,6 +1,5 @@
import { z } from 'zod'
export * from './quest/index.js'
export * from './session.js'
export const passwordPolicy = {
@@ -1,11 +0,0 @@
import { z } from 'zod'
import { conditionSchema } from './condition.js'
import { responseSchema } from './response.js'
export const commandResponderSchema = z.object({
condition: conditionSchema.optional(),
responses: z.array(responseSchema),
hints: z.array(z.string()),
})
export type CommandResponder = z.infer<typeof commandResponderSchema>
-21
View File
@@ -1,21 +0,0 @@
import { z } from 'zod'
import { fileConditionSchema } from './file-condition.js'
import { toArray } from '../utils.js'
const conditionSchemaBase = z.object({
command: 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()),
pwd: z.preprocess(toArray, z.array(z.string()).optional()),
files: z.array(fileConditionSchema).optional(),
})
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>
-22
View File
@@ -1,22 +0,0 @@
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,9 +0,0 @@
import { z } from 'zod'
export const fileConditionSchema = z.object({
path: z.string(),
type: z.string().optional(),
exists: z.boolean().default(true),
})
export type FileCondition = z.infer<typeof fileConditionSchema>
-5
View File
@@ -1,5 +0,0 @@
import { z } from 'zod'
export const hintSchema = z.array(z.string())
export type Hint = z.infer<typeof hintSchema>
-11
View File
@@ -1,11 +0,0 @@
export * from './requirement.js'
export * from './hint.js'
export * from './file-condition.js'
export * from './condition.js'
export * from './response.js'
export * from './command-responder.js'
export * from './stage-exception.js'
export * from './stage.js'
export * from './exception.js'
export * from './quest.js'
export * from './quest-detail-response.js'
@@ -1,10 +0,0 @@
import { z } from 'zod'
export const questDetailResponseSchema = z.object({
id: z.string(),
title: z.string(),
instruction: z.string(),
requirements: z.array(z.string()),
})
export type QuestDetailResponse = z.infer<typeof questDetailResponseSchema>
-16
View File
@@ -1,16 +0,0 @@
import { z } from 'zod'
import { stageSchema } from './stage.js'
import { globalExceptionSchema } from './exception.js'
import { requirementSchema } from './requirement.js'
export const questSchema = z.object({
id: z.string(),
title: z.string(),
image: z.string(),
instruction: z.string(),
requirements: requirementSchema,
stages: z.array(stageSchema),
exceptions: z.array(globalExceptionSchema).default([]),
})
export type Quest = z.infer<typeof questSchema>
@@ -1,5 +0,0 @@
import { z } from 'zod'
export const requirementSchema = z.array(z.string()).default([])
export type Requirement = z.infer<typeof requirementSchema>
-9
View File
@@ -1,9 +0,0 @@
import { z } from 'zod'
import { toArray } from '../utils.js'
export const responseSchema = z.object({
type: z.enum(['narrative', 'dialogue']).default('narrative'),
content: z.preprocess(toArray, z.array(z.string())),
})
export type Response = z.infer<typeof responseSchema>
-18
View File
@@ -1,18 +0,0 @@
import { z } from 'zod'
import { stageExceptionSchema } from './exception.js'
import { conditionSchema } from './condition.js'
import { responseSchema } from './response.js'
import { hintSchema } from './hint.js'
import { requirementSchema } from './requirement.js'
export const stageSchema = z.object({
id: z.string(),
task: z.string(),
exceptions: stageExceptionSchema.array().default([]),
requirements: requirementSchema,
condition: conditionSchema,
responses: z.array(responseSchema),
hints: hintSchema.default([]),
})
export type Stage = z.infer<typeof stageSchema>
+4 -1
View File
@@ -1,7 +1,10 @@
{
"name": "@linux-odyssey/game",
"version": "1.0.0",
"main": "dist/index.ts",
"main": "dist/index.js",
"exports": {
".": "./dist/index.js"
},
"license": "MIT",
"scripts": {
"build": "tsc -b"
@@ -1,4 +1,4 @@
import { ICommand } from '../types.js'
import { ICommand } from '../schema'
import { Matcher } from './Matcher.js'
export class CommandMatcher implements Matcher {
+2 -2
View File
@@ -1,8 +1,8 @@
import {
ICommand,
IFileExistenceChecker,
IFileInput,
ICondition,
IFileExistenceInput,
} from '../schema'
import { PwdMatcher } from './PwdMatcher.js'
import { CommandMatcher } from './CommandMatcher.js'
@@ -14,7 +14,7 @@ import { NotMatcher } from './NotMatcher.js'
import { checkFiles } from './FileMatcher.js'
export class Condition {
private files: IFileInput[] = []
private files: IFileExistenceInput[] = []
private matchers: Matcher[] = []
constructor({ command, output, error, pwd, or, not, files }: ICondition) {
+1 -1
View File
@@ -1,5 +1,5 @@
import { Matcher } from './Matcher.js'
import { ICommand } from '../types.js'
import { ICommand } from '../schema'
export class ErrorMatcher implements Matcher {
private matcher?: RegExp
+2 -2
View File
@@ -1,8 +1,8 @@
import { FileExistenceInput, IFileExistenceChecker } from '../types'
import { IFileExistenceInput, IFileExistenceChecker } from '../schema'
export async function checkFiles(
checker: IFileExistenceChecker,
files: FileExistenceInput[]
files: IFileExistenceInput[]
) {
const existences = await Promise.all(
files.map((file) => checker.exists(file))
+1 -1
View File
@@ -1,4 +1,4 @@
import { ICommand } from '../types.js'
import { ICommand } from '../schema'
export interface Matcher {
match(command: ICommand): boolean
+1 -1
View File
@@ -1,4 +1,4 @@
import { ICommand } from '../types.js'
import { ICommand } from '../schema'
import { Matcher } from './Matcher.js'
export class NotMatcher implements Matcher {
+1 -1
View File
@@ -1,4 +1,4 @@
import { ICommand } from '../types.js'
import { ICommand } from '../schema'
import { Matcher } from './Matcher.js'
export class OrMatcher implements Matcher {
+1 -1
View File
@@ -1,4 +1,4 @@
import { ICommand } from '../types.js'
import { ICommand } from '../schema'
import { Matcher } from './Matcher.js'
export class OutputMatcher implements Matcher {
+1 -1
View File
@@ -1,4 +1,4 @@
import { ICommand } from '../types.js'
import { ICommand } from '../schema'
import { Matcher } from './Matcher.js'
export class PwdMatcher implements Matcher {
+6
View File
@@ -0,0 +1,6 @@
export * from './schema'
export * from './Quest'
export * from './Session'
export * from './Event'
export * from './Stage'
export * from './Exception'
+5 -5
View File
@@ -1,9 +1,9 @@
export * from './condition.js'
export { stageSchema, IStage } from './stage.js'
export { requirementsSchema, IRequirements } from './requirements.js'
export { responseSchema, IResponse } from './response.js'
export { questSchema, IQuest } from './quest.js'
export { exceptionSchema, IException } from './exception.js'
export * from './stage.js'
export * from './requirements.js'
export * from './response.js'
export * from './quest.js'
export * from './exception.js'
export interface ICommand {
command?: string
+1
View File
@@ -3,6 +3,7 @@ import { z } from 'zod'
export const responseSchema = z.object({
type: z.enum(['narrative', 'dialogue']),
content: z.string(),
hint: z.string().optional(),
})
export type IResponse = z.infer<typeof responseSchema>
View File
Whitespace-only changes.
+2 -1
View File
@@ -2,7 +2,8 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src"
"rootDir": "./src",
"composite": true
},
"include": ["src"]
}
+1
View File
@@ -14,6 +14,7 @@
},
"dependencies": {
"@linux-odyssey/constants": "*",
"@linux-odyssey/game": "*",
"@linux-odyssey/file-graph": "*",
"@linux-odyssey/models": "*",
"@linux-odyssey/utils": "*",
+5 -5
View File
@@ -3,7 +3,7 @@ import fs from 'fs/promises'
import path from 'path'
import yaml from 'yaml'
import { Quest, questSchema } from '@linux-odyssey/constants'
import { questSchema, globalExceptionSchema, IQuest } from '@linux-odyssey/game'
import logger from '../utils/logger.js'
class QuestValidationError extends Error {
@@ -19,7 +19,7 @@ class QuestValidationError extends Error {
}
class QuestManager {
private quests = new Map<string, Quest>()
private quests = new Map<string, IQuest>()
private questDirectory = path.join(process.cwd(), '..', 'quests')
get(id: string) {
@@ -51,11 +51,11 @@ class QuestManager {
private async loadGlobalExceptions() {
const exceptionsPath = path.join(this.questDirectory, 'exceptions.yml')
const body = await fs.readFile(exceptionsPath, 'utf-8')
return yaml.parse(body)
return globalExceptionSchema.array().parse(yaml.parse(body))
}
async loadAndUpdateQuests() {
const { exceptions } = await this.loadGlobalExceptions()
const exceptions = await this.loadGlobalExceptions()
return this.mapQuests(async (id, questPath) => {
const files = await fs.readdir(questPath)
if (!files.includes('game.yml')) {
@@ -78,7 +78,7 @@ class QuestManager {
image,
...questData,
})
quest.exceptions.push(...exceptions)
quest.exceptions = [...exceptions]
this.quests.set(id, quest)
} catch (error) {
logger.error(`Error parsing quest ${id}`, { error })