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 { toArray } from '../utils.js'
|
||||
|
||||
export const conditionSchema = z.object({
|
||||
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()),
|
||||
@@ -10,4 +10,12 @@ export const conditionSchema = z.object({
|
||||
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 './stage-exception.js'
|
||||
export * from './stage.js'
|
||||
export * from './global-exception.js'
|
||||
export * from './exception.js'
|
||||
export * from './quest.js'
|
||||
export * from './quest-detail-response.js'
|
||||
@@ -1,7 +1,7 @@
|
||||
import { z } from 'zod'
|
||||
|
||||
export const questDetailResponseSchema = z.object({
|
||||
_id: z.string(),
|
||||
id: z.string(),
|
||||
title: z.string(),
|
||||
instruction: z.string(),
|
||||
requirements: z.array(z.string()),
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { z } from 'zod'
|
||||
import { stageSchema } from './stage.js'
|
||||
import { globalExceptionSchema } from './global-exception.js'
|
||||
import { globalExceptionSchema } from './exception.js'
|
||||
import { requirementSchema } from './requirement.js'
|
||||
|
||||
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 { stageExceptionSchema } from './stage-exception.js'
|
||||
import { stageExceptionSchema } from './exception.js'
|
||||
import { conditionSchema } from './condition.js'
|
||||
import { responseSchema } from './response.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'
|
||||
|
||||
export { User, IUser } from './user.js'
|
||||
export { Quest, IQuest } from './quest.js'
|
||||
export { Session, ISession, ITask } from './session.js'
|
||||
export { Session, ISession } from './session.js'
|
||||
export { Command, ICommand } from './command.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) {
|
||||
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 { IResponse, responseSchema } from './response.js'
|
||||
|
||||
export interface INode {
|
||||
path: string
|
||||
type: string
|
||||
@@ -22,27 +20,6 @@ nodeSchema.add({
|
||||
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 {
|
||||
_id: Types.ObjectId
|
||||
user: Types.ObjectId
|
||||
@@ -53,9 +30,7 @@ export interface ISession {
|
||||
updatedAt: Date
|
||||
finishedAt?: Date
|
||||
lastActivityAt: Date
|
||||
tasks: ITask[]
|
||||
hints: string[][]
|
||||
responses: IResponse[][]
|
||||
stages: string[]
|
||||
graph: INode
|
||||
}
|
||||
|
||||
@@ -86,13 +61,7 @@ export const Session = model<ISession>(
|
||||
default: Date.now,
|
||||
required: true,
|
||||
},
|
||||
tasks: [taskSchema],
|
||||
hints: [[String]],
|
||||
responses: {
|
||||
type: [[responseSchema]],
|
||||
required: true,
|
||||
default: [],
|
||||
},
|
||||
stages: [String],
|
||||
graph: {
|
||||
type: nodeSchema,
|
||||
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)
|
||||
Reference in new issue
Block a user