mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
refactor: remove condition tests and update file node interfaces in game session
TODO: replace "folder" with "directory"
This commit is contained in:
1 parent
0685d71299
commit
7e50c8e06c
6 files changed
+46
-46
No files matched your search
@@ -1,13 +0,0 @@
|
||||
import { describe, it, expect } from '@jest/globals'
|
||||
import { conditionSchema } from '../src/index.js'
|
||||
|
||||
describe('parsing condition', () => {
|
||||
it('should parse condition', () => {
|
||||
const condition = {
|
||||
command: ['^echo start$'],
|
||||
output: ['^start$'],
|
||||
}
|
||||
const result = conditionSchema.safeParse(condition)
|
||||
expect(result.error).toBeUndefined()
|
||||
})
|
||||
})
|
||||
@@ -2,9 +2,9 @@ export { default as File, FileObject } from './file.js'
|
||||
export { default as FileNode } from './fileNode.js'
|
||||
export { default as FileGraph, Event } from './fileGraph.js'
|
||||
|
||||
export interface INode {
|
||||
export interface IFileNode {
|
||||
path: string
|
||||
type: string
|
||||
discovered: boolean
|
||||
children?: INode[]
|
||||
children?: IFileNode[]
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { FileGraph, INode } from '@linux-odyssey/file-graph'
|
||||
import { FileGraph, FileNode, IFileNode } from '@linux-odyssey/file-graph'
|
||||
import { Quest } from './Quest'
|
||||
import {
|
||||
IQuest,
|
||||
@@ -15,13 +15,15 @@ export class GameSession {
|
||||
private graph: FileGraph
|
||||
|
||||
constructor(
|
||||
{ completedEvents, graph }: ISession,
|
||||
{ completedEvents, graph }: Partial<ISession>,
|
||||
quest: IQuest,
|
||||
checker: IFileExistenceChecker
|
||||
) {
|
||||
this.completed = completedEvents
|
||||
this.completed = completedEvents || []
|
||||
this.quest = new Quest(quest, checker)
|
||||
this.graph = new FileGraph(graph)
|
||||
this.graph = new FileGraph(
|
||||
graph ?? new FileNode({ path: '/', type: 'root', discovered: false })
|
||||
)
|
||||
}
|
||||
|
||||
get completedEvents() {
|
||||
@@ -56,7 +58,7 @@ export class GameSession {
|
||||
return this.quest.getTasks(this.completed)
|
||||
}
|
||||
|
||||
getGraph(): INode {
|
||||
getGraph(): IFileNode {
|
||||
return this.graph
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { z } from 'zod'
|
||||
|
||||
const fileSchema = z.object({
|
||||
path: z.string(),
|
||||
type: z.enum(['file', 'directory']),
|
||||
discovered: z.boolean(),
|
||||
})
|
||||
|
||||
export const commandParamsSchema = z.object({
|
||||
discover: fileSchema.array().optional(),
|
||||
})
|
||||
|
||||
export type ICommandParams = z.infer<typeof commandParamsSchema>
|
||||
|
||||
export const commandSchema = z.object({
|
||||
command: z.string().optional(),
|
||||
output: z.string().optional(),
|
||||
error: z.string().optional(),
|
||||
pwd: z.string().optional(),
|
||||
params: commandParamsSchema.optional(),
|
||||
})
|
||||
|
||||
export type ICommand = z.infer<typeof commandSchema>
|
||||
@@ -1,4 +1,4 @@
|
||||
import { INode } from '@linux-odyssey/file-graph'
|
||||
import { IFileNode } from '@linux-odyssey/file-graph'
|
||||
|
||||
export * from './condition.js'
|
||||
export * from './stage.js'
|
||||
@@ -6,17 +6,11 @@ export * from './requirements.js'
|
||||
export * from './response.js'
|
||||
export * from './quest.js'
|
||||
export * from './exception.js'
|
||||
|
||||
export interface ICommand {
|
||||
command?: string
|
||||
output?: string
|
||||
error?: string
|
||||
pwd?: string
|
||||
}
|
||||
export * from './command.js'
|
||||
|
||||
export interface ISession {
|
||||
completedEvents: string[]
|
||||
graph: INode
|
||||
graph: IFileNode
|
||||
}
|
||||
|
||||
export interface ITask {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { GameSession } from '@linux-odyssey/game'
|
||||
import { GameSession, commandSchema } from '@linux-odyssey/game'
|
||||
import { Command, Session } from '@linux-odyssey/models'
|
||||
import { matchedData } from 'express-validator'
|
||||
import type { Request, Response } from 'express'
|
||||
import { pushToSession } from '../socket.js'
|
||||
import { finishSession } from '../../models/sessionManager.js'
|
||||
@@ -9,7 +8,7 @@ import { questManager } from '../../models/quest.js'
|
||||
import { CLIFileExistenceChecker } from '../../containers/cli.js'
|
||||
|
||||
export const newCommand = asyncHandler(async (req: Request, res: Response) => {
|
||||
const { command, pwd, output, error, params } = matchedData(req)
|
||||
const command = commandSchema.parse(req.body)
|
||||
|
||||
console.log('command', command)
|
||||
|
||||
@@ -31,16 +30,6 @@ export const newCommand = asyncHandler(async (req: Request, res: Response) => {
|
||||
|
||||
session.lastActivityAt = new Date()
|
||||
|
||||
const c = new Command({
|
||||
session,
|
||||
command,
|
||||
pwd,
|
||||
output,
|
||||
error,
|
||||
})
|
||||
|
||||
await c.save()
|
||||
|
||||
const quest = await questManager.get(session.quest)
|
||||
if (!quest) {
|
||||
res.status(404).json({ message: 'quest not found' })
|
||||
@@ -53,12 +42,17 @@ export const newCommand = asyncHandler(async (req: Request, res: Response) => {
|
||||
new CLIFileExistenceChecker()
|
||||
)
|
||||
|
||||
const event = await gameSession.runCommand(c)
|
||||
const event = await gameSession.runCommand(command)
|
||||
console.log('event', event)
|
||||
if (event) {
|
||||
c.stage = event
|
||||
await c.save()
|
||||
|
||||
const c = new Command({
|
||||
...command,
|
||||
session: sessionId,
|
||||
stage: event,
|
||||
})
|
||||
|
||||
await c.save()
|
||||
if (event) {
|
||||
session.completedEvents = gameSession.completedEvents
|
||||
|
||||
if (gameSession.isFinished()) {
|
||||
|
||||
Reference in new issue
Block a user