mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
refactor: remove unused quest-related schemas and consolidate exports
This commit is contained in:
1 parent
d2cdd1304a
commit
1d843f7355
29 files changed
+35
-161
No files matched your search
@@ -1,6 +1,5 @@
|
|||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
|
|
||||||
export * from './quest/index.js'
|
|
||||||
export * from './session.js'
|
export * from './session.js'
|
||||||
|
|
||||||
export const passwordPolicy = {
|
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>
|
|
||||||
@@ -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>
|
|
||||||
@@ -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>
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
import { z } from 'zod'
|
|
||||||
|
|
||||||
export const hintSchema = z.array(z.string())
|
|
||||||
|
|
||||||
export type Hint = z.infer<typeof hintSchema>
|
|
||||||
@@ -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>
|
|
||||||
@@ -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>
|
|
||||||
@@ -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>
|
|
||||||
@@ -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>
|
|
||||||
@@ -1,7 +1,10 @@
|
|||||||
{
|
{
|
||||||
"name": "@linux-odyssey/game",
|
"name": "@linux-odyssey/game",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"main": "dist/index.ts",
|
"main": "dist/index.js",
|
||||||
|
"exports": {
|
||||||
|
".": "./dist/index.js"
|
||||||
|
},
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc -b"
|
"build": "tsc -b"
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { ICommand } from '../types.js'
|
import { ICommand } from '../schema'
|
||||||
import { Matcher } from './Matcher.js'
|
import { Matcher } from './Matcher.js'
|
||||||
|
|
||||||
export class CommandMatcher implements Matcher {
|
export class CommandMatcher implements Matcher {
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import {
|
import {
|
||||||
ICommand,
|
ICommand,
|
||||||
IFileExistenceChecker,
|
IFileExistenceChecker,
|
||||||
IFileInput,
|
|
||||||
ICondition,
|
ICondition,
|
||||||
|
IFileExistenceInput,
|
||||||
} from '../schema'
|
} from '../schema'
|
||||||
import { PwdMatcher } from './PwdMatcher.js'
|
import { PwdMatcher } from './PwdMatcher.js'
|
||||||
import { CommandMatcher } from './CommandMatcher.js'
|
import { CommandMatcher } from './CommandMatcher.js'
|
||||||
@@ -14,7 +14,7 @@ import { NotMatcher } from './NotMatcher.js'
|
|||||||
import { checkFiles } from './FileMatcher.js'
|
import { checkFiles } from './FileMatcher.js'
|
||||||
|
|
||||||
export class Condition {
|
export class Condition {
|
||||||
private files: IFileInput[] = []
|
private files: IFileExistenceInput[] = []
|
||||||
private matchers: Matcher[] = []
|
private matchers: Matcher[] = []
|
||||||
|
|
||||||
constructor({ command, output, error, pwd, or, not, files }: ICondition) {
|
constructor({ command, output, error, pwd, or, not, files }: ICondition) {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Matcher } from './Matcher.js'
|
import { Matcher } from './Matcher.js'
|
||||||
import { ICommand } from '../types.js'
|
import { ICommand } from '../schema'
|
||||||
|
|
||||||
export class ErrorMatcher implements Matcher {
|
export class ErrorMatcher implements Matcher {
|
||||||
private matcher?: RegExp
|
private matcher?: RegExp
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { FileExistenceInput, IFileExistenceChecker } from '../types'
|
import { IFileExistenceInput, IFileExistenceChecker } from '../schema'
|
||||||
|
|
||||||
export async function checkFiles(
|
export async function checkFiles(
|
||||||
checker: IFileExistenceChecker,
|
checker: IFileExistenceChecker,
|
||||||
files: FileExistenceInput[]
|
files: IFileExistenceInput[]
|
||||||
) {
|
) {
|
||||||
const existences = await Promise.all(
|
const existences = await Promise.all(
|
||||||
files.map((file) => checker.exists(file))
|
files.map((file) => checker.exists(file))
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { ICommand } from '../types.js'
|
import { ICommand } from '../schema'
|
||||||
|
|
||||||
export interface Matcher {
|
export interface Matcher {
|
||||||
match(command: ICommand): boolean
|
match(command: ICommand): boolean
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { ICommand } from '../types.js'
|
import { ICommand } from '../schema'
|
||||||
import { Matcher } from './Matcher.js'
|
import { Matcher } from './Matcher.js'
|
||||||
|
|
||||||
export class NotMatcher implements Matcher {
|
export class NotMatcher implements Matcher {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { ICommand } from '../types.js'
|
import { ICommand } from '../schema'
|
||||||
import { Matcher } from './Matcher.js'
|
import { Matcher } from './Matcher.js'
|
||||||
|
|
||||||
export class OrMatcher implements Matcher {
|
export class OrMatcher implements Matcher {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { ICommand } from '../types.js'
|
import { ICommand } from '../schema'
|
||||||
import { Matcher } from './Matcher.js'
|
import { Matcher } from './Matcher.js'
|
||||||
|
|
||||||
export class OutputMatcher implements Matcher {
|
export class OutputMatcher implements Matcher {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { ICommand } from '../types.js'
|
import { ICommand } from '../schema'
|
||||||
import { Matcher } from './Matcher.js'
|
import { Matcher } from './Matcher.js'
|
||||||
|
|
||||||
export class PwdMatcher implements Matcher {
|
export class PwdMatcher implements Matcher {
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
export * from './schema'
|
||||||
|
export * from './Quest'
|
||||||
|
export * from './Session'
|
||||||
|
export * from './Event'
|
||||||
|
export * from './Stage'
|
||||||
|
export * from './Exception'
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
export * from './condition.js'
|
export * from './condition.js'
|
||||||
export { stageSchema, IStage } from './stage.js'
|
export * from './stage.js'
|
||||||
export { requirementsSchema, IRequirements } from './requirements.js'
|
export * from './requirements.js'
|
||||||
export { responseSchema, IResponse } from './response.js'
|
export * from './response.js'
|
||||||
export { questSchema, IQuest } from './quest.js'
|
export * from './quest.js'
|
||||||
export { exceptionSchema, IException } from './exception.js'
|
export * from './exception.js'
|
||||||
|
|
||||||
export interface ICommand {
|
export interface ICommand {
|
||||||
command?: string
|
command?: string
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { z } from 'zod'
|
|||||||
export const responseSchema = z.object({
|
export const responseSchema = z.object({
|
||||||
type: z.enum(['narrative', 'dialogue']),
|
type: z.enum(['narrative', 'dialogue']),
|
||||||
content: z.string(),
|
content: z.string(),
|
||||||
|
hint: z.string().optional(),
|
||||||
})
|
})
|
||||||
|
|
||||||
export type IResponse = z.infer<typeof responseSchema>
|
export type IResponse = z.infer<typeof responseSchema>
|
||||||
Whitespace-only changes.
@@ -2,7 +2,8 @@
|
|||||||
"extends": "../../tsconfig.json",
|
"extends": "../../tsconfig.json",
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"outDir": "./dist",
|
"outDir": "./dist",
|
||||||
"rootDir": "./src"
|
"rootDir": "./src",
|
||||||
|
"composite": true
|
||||||
},
|
},
|
||||||
"include": ["src"]
|
"include": ["src"]
|
||||||
}
|
}
|
||||||
@@ -14,6 +14,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@linux-odyssey/constants": "*",
|
"@linux-odyssey/constants": "*",
|
||||||
|
"@linux-odyssey/game": "*",
|
||||||
"@linux-odyssey/file-graph": "*",
|
"@linux-odyssey/file-graph": "*",
|
||||||
"@linux-odyssey/models": "*",
|
"@linux-odyssey/models": "*",
|
||||||
"@linux-odyssey/utils": "*",
|
"@linux-odyssey/utils": "*",
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import fs from 'fs/promises'
|
|||||||
import path from 'path'
|
import path from 'path'
|
||||||
import yaml from 'yaml'
|
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'
|
import logger from '../utils/logger.js'
|
||||||
|
|
||||||
class QuestValidationError extends Error {
|
class QuestValidationError extends Error {
|
||||||
@@ -19,7 +19,7 @@ class QuestValidationError extends Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class QuestManager {
|
class QuestManager {
|
||||||
private quests = new Map<string, Quest>()
|
private quests = new Map<string, IQuest>()
|
||||||
private questDirectory = path.join(process.cwd(), '..', 'quests')
|
private questDirectory = path.join(process.cwd(), '..', 'quests')
|
||||||
|
|
||||||
get(id: string) {
|
get(id: string) {
|
||||||
@@ -51,11 +51,11 @@ class QuestManager {
|
|||||||
private async loadGlobalExceptions() {
|
private async loadGlobalExceptions() {
|
||||||
const exceptionsPath = path.join(this.questDirectory, 'exceptions.yml')
|
const exceptionsPath = path.join(this.questDirectory, 'exceptions.yml')
|
||||||
const body = await fs.readFile(exceptionsPath, 'utf-8')
|
const body = await fs.readFile(exceptionsPath, 'utf-8')
|
||||||
return yaml.parse(body)
|
return globalExceptionSchema.array().parse(yaml.parse(body))
|
||||||
}
|
}
|
||||||
|
|
||||||
async loadAndUpdateQuests() {
|
async loadAndUpdateQuests() {
|
||||||
const { exceptions } = await this.loadGlobalExceptions()
|
const exceptions = await this.loadGlobalExceptions()
|
||||||
return this.mapQuests(async (id, questPath) => {
|
return this.mapQuests(async (id, questPath) => {
|
||||||
const files = await fs.readdir(questPath)
|
const files = await fs.readdir(questPath)
|
||||||
if (!files.includes('game.yml')) {
|
if (!files.includes('game.yml')) {
|
||||||
@@ -78,7 +78,7 @@ class QuestManager {
|
|||||||
image,
|
image,
|
||||||
...questData,
|
...questData,
|
||||||
})
|
})
|
||||||
quest.exceptions.push(...exceptions)
|
quest.exceptions = [...exceptions]
|
||||||
this.quests.set(id, quest)
|
this.quests.set(id, quest)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(`Error parsing quest ${id}`, { error })
|
logger.error(`Error parsing quest ${id}`, { error })
|
||||||
|
|||||||
Reference in new issue
Block a user