mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
wip: game schema
This commit is contained in:
1 parent
e66598f1a7
commit
19c44fbe52
7 files changed
+70
-2
No files matched your search
@@ -6,5 +6,7 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc -b"
|
"build": "tsc -b"
|
||||||
},
|
},
|
||||||
"dependencies": {}
|
"dependencies": {
|
||||||
|
"zod": "^3.23.8"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Whitespace-only changes.
@@ -0,0 +1,54 @@
|
|||||||
|
import { z } from 'zod'
|
||||||
|
|
||||||
|
export const requirementsSchema = z.object({
|
||||||
|
requirements: z.array(z.string()),
|
||||||
|
})
|
||||||
|
|
||||||
|
export type IRequirements = z.infer<typeof requirementsSchema>
|
||||||
|
|
||||||
|
export const fileInputSchema = z.object({
|
||||||
|
path: z.string(),
|
||||||
|
type: z.enum(['file', 'folder']),
|
||||||
|
})
|
||||||
|
|
||||||
|
export type IFileInput = z.infer<typeof fileInputSchema>
|
||||||
|
|
||||||
|
export const fileExistenceInputSchema = fileInputSchema.extend({
|
||||||
|
exists: z.boolean(),
|
||||||
|
})
|
||||||
|
|
||||||
|
export type IFileExistenceInput = z.infer<typeof fileExistenceInputSchema>
|
||||||
|
|
||||||
|
const baseConditionSchema = z.object({
|
||||||
|
command: z.string().optional(),
|
||||||
|
output: z.string().optional(),
|
||||||
|
error: z.string().optional(),
|
||||||
|
pwd: z.string().optional(),
|
||||||
|
files: z.array(fileExistenceInputSchema).optional(),
|
||||||
|
})
|
||||||
|
|
||||||
|
export type ICondition = z.infer<typeof baseConditionSchema> & {
|
||||||
|
or?: ICondition[]
|
||||||
|
not?: ICondition
|
||||||
|
}
|
||||||
|
|
||||||
|
export const conditionSchema: z.ZodType<ICondition> =
|
||||||
|
baseConditionSchema.extend({
|
||||||
|
or: z.lazy(() => z.array(conditionSchema)).optional(),
|
||||||
|
not: z.lazy(() => conditionSchema).optional(),
|
||||||
|
})
|
||||||
|
|
||||||
|
export const responseSchema = z.object({
|
||||||
|
type: z.enum(['narrative', 'dialogue']),
|
||||||
|
content: z.string(),
|
||||||
|
})
|
||||||
|
|
||||||
|
export const stageSchema = z.object({
|
||||||
|
id: z.string().regex(/^[a-zA-Z0-9_-]+$/),
|
||||||
|
name: z.string().optional(),
|
||||||
|
requirements: z.array(z.string()),
|
||||||
|
condition: conditionSchema,
|
||||||
|
response: responseSchema,
|
||||||
|
})
|
||||||
|
|
||||||
|
export type IStage = z.infer<typeof stageSchema>
|
||||||
Whitespace-only changes.
Whitespace-only changes.
@@ -4,6 +4,7 @@ import { OutputMatcher } from '../src/condition/OutputMatcher'
|
|||||||
import { PwdMatcher } from '../src/condition/PwdMatcher'
|
import { PwdMatcher } from '../src/condition/PwdMatcher'
|
||||||
import { CommandMatcher } from '../src/condition/CommandMatcher'
|
import { CommandMatcher } from '../src/condition/CommandMatcher'
|
||||||
import { ErrorMatcher } from '../src/condition/ErrorMatcher.js'
|
import { ErrorMatcher } from '../src/condition/ErrorMatcher.js'
|
||||||
|
import { conditionSchema } from '../src/schema'
|
||||||
|
|
||||||
test('command match', () => {
|
test('command match', () => {
|
||||||
const matcher = new CommandMatcher('^echo start$')
|
const matcher = new CommandMatcher('^echo start$')
|
||||||
@@ -80,3 +81,14 @@ test('condition with NOT', () => {
|
|||||||
expect(condition.match({ command: 'echo start' })).toBe(false)
|
expect(condition.match({ command: 'echo start' })).toBe(false)
|
||||||
expect(condition.match({ command: 'echo hello' })).toBe(true)
|
expect(condition.match({ command: 'echo hello' })).toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should parse condition', () => {
|
||||||
|
const condition = {
|
||||||
|
command: 'echo start',
|
||||||
|
output: 'start',
|
||||||
|
not: {
|
||||||
|
pwd: '/home/user',
|
||||||
|
},
|
||||||
|
}
|
||||||
|
expect(conditionSchema.safeParse(condition).error).toBeUndefined()
|
||||||
|
})
|
||||||
@@ -9747,5 +9747,5 @@ yocto-queue@^0.1.0:
|
|||||||
|
|
||||||
zod@^3.23.8:
|
zod@^3.23.8:
|
||||||
version "3.23.8"
|
version "3.23.8"
|
||||||
resolved "https://registry.npmjs.org/zod/-/zod-3.23.8.tgz"
|
resolved "https://registry.yarnpkg.com/zod/-/zod-3.23.8.tgz#e37b957b5d52079769fb8097099b592f0ef4067d"
|
||||||
integrity sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g==
|
integrity sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g==
|
||||||
Reference in new issue
Block a user