mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-07 19:10: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
@@ -1,10 +1,11 @@
|
||||
import { Command, Quest, Session } from '@linux-odyssey/models'
|
||||
import { Command, Session } from '@linux-odyssey/models'
|
||||
import { matchedData } from 'express-validator'
|
||||
import type { Request, Response } from 'express'
|
||||
import CommandHandler from '../../game/commandHandler.js'
|
||||
import { pushToSession } from '../socket.js'
|
||||
import { finishSession } from '../../models/sessionManager.js'
|
||||
import { asyncHandler } from '../../middleware/error.js'
|
||||
import { questManager } from '../../models/quest.js'
|
||||
|
||||
export const newCommand = asyncHandler(async (req: Request, res: Response) => {
|
||||
const { command, pwd, output, error, params } = matchedData(req)
|
||||
@@ -37,7 +38,7 @@ export const newCommand = asyncHandler(async (req: Request, res: Response) => {
|
||||
|
||||
await c.save()
|
||||
|
||||
const quest = await Quest.findById(session.quest)
|
||||
const quest = await questManager.get(session.quest)
|
||||
if (!quest) {
|
||||
res.status(404).json({ message: 'quest not found' })
|
||||
return
|
||||
|
||||
@@ -22,10 +22,7 @@ function sessionSummary(session: ISession) {
|
||||
function sessionDetail(session: ISession) {
|
||||
return {
|
||||
...sessionSummary(session),
|
||||
hints: session.hints,
|
||||
tasks: session.tasks,
|
||||
responses: session.responses,
|
||||
graph: session.graph,
|
||||
stages: session.stages,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,14 +2,13 @@ import minimist from 'minimist'
|
||||
import { HydratedDocument } from 'mongoose'
|
||||
import { FileGraph } from '@linux-odyssey/file-graph'
|
||||
import {
|
||||
ICommand,
|
||||
ICondition,
|
||||
IFileCondition,
|
||||
IQuest,
|
||||
ISession,
|
||||
IStage,
|
||||
IException,
|
||||
} from '@linux-odyssey/models'
|
||||
Condition,
|
||||
FileCondition,
|
||||
Quest,
|
||||
Stage,
|
||||
Exception,
|
||||
} from '@linux-odyssey/constants'
|
||||
import { ICommand, ISession } from '@linux-odyssey/models'
|
||||
import { pushToSession } from '../api/socket.js'
|
||||
import SessionHandler, { ExecuteResult } from './sessionHandler.js'
|
||||
import { checkFile } from '../containers/cli.js'
|
||||
@@ -31,7 +30,7 @@ export default class CommandHandler extends SessionHandler {
|
||||
|
||||
constructor(
|
||||
session: HydratedDocument<ISession>,
|
||||
quest: HydratedDocument<IQuest>,
|
||||
quest: Quest,
|
||||
commandInput: ICommand,
|
||||
params: Record<string, any>
|
||||
) {
|
||||
@@ -82,7 +81,7 @@ export default class CommandHandler extends SessionHandler {
|
||||
}
|
||||
}
|
||||
|
||||
async isMatch(condition: ICondition): Promise<boolean> {
|
||||
async isMatch(condition: Condition): Promise<boolean> {
|
||||
return (
|
||||
this.checkKeys(condition) &&
|
||||
(await this.checkFiles(condition.files)) &&
|
||||
@@ -91,7 +90,7 @@ export default class CommandHandler extends SessionHandler {
|
||||
)
|
||||
}
|
||||
|
||||
checkKeys(condition: ICondition) {
|
||||
checkKeys(condition: Condition) {
|
||||
return (
|
||||
checkMatch(condition.command, this.commandInput.command) &&
|
||||
checkMatch(condition.output, this.commandInput.output) &&
|
||||
@@ -100,7 +99,7 @@ export default class CommandHandler extends SessionHandler {
|
||||
)
|
||||
}
|
||||
|
||||
async checkFiles(files?: IFileCondition[]): Promise<boolean> {
|
||||
async checkFiles(files?: FileCondition[]): Promise<boolean> {
|
||||
if (!files || files.length === 0) {
|
||||
return true
|
||||
}
|
||||
@@ -117,14 +116,14 @@ export default class CommandHandler extends SessionHandler {
|
||||
}
|
||||
}
|
||||
|
||||
async checkNot(condition?: ICondition): Promise<boolean> {
|
||||
async checkNot(condition?: Condition): Promise<boolean> {
|
||||
if (!condition) {
|
||||
return true
|
||||
}
|
||||
return !(await this.isMatch(condition))
|
||||
}
|
||||
|
||||
async checkOr(conditions?: ICondition[]): Promise<boolean> {
|
||||
async checkOr(conditions?: Condition[]): Promise<boolean> {
|
||||
if (!conditions || conditions.length === 0) {
|
||||
return true
|
||||
}
|
||||
@@ -132,7 +131,7 @@ export default class CommandHandler extends SessionHandler {
|
||||
return matches.some((m) => m === true)
|
||||
}
|
||||
|
||||
async checkException(stage: IStage): Promise<IException | null> {
|
||||
async checkException(stage: Stage): Promise<Exception | null> {
|
||||
const exceptions = stage.exceptions || []
|
||||
for (const exception of exceptions) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
@@ -200,7 +199,7 @@ export default class CommandHandler extends SessionHandler {
|
||||
logger.error('CommandHandler error', {
|
||||
error: (err as Error).message,
|
||||
session: this.session._id,
|
||||
quest: this.quest._id,
|
||||
quest: this.quest.id,
|
||||
command: this.commandInput,
|
||||
})
|
||||
return null
|
||||
|
||||
@@ -1,18 +1,16 @@
|
||||
import { HydratedDocument } from 'mongoose'
|
||||
import type {
|
||||
IException,
|
||||
IQuest,
|
||||
IRequirement,
|
||||
IResponse,
|
||||
ISession,
|
||||
IStage,
|
||||
ITask,
|
||||
} from '@linux-odyssey/models'
|
||||
Quest,
|
||||
Response,
|
||||
Requirement,
|
||||
Stage,
|
||||
Task,
|
||||
Exception,
|
||||
} from '@linux-odyssey/constants'
|
||||
import { ISession } from '@linux-odyssey/models'
|
||||
|
||||
export interface ExecuteResult {
|
||||
responses: IResponse[]
|
||||
hints: string[]
|
||||
tasks?: ITask[]
|
||||
responses: Response[]
|
||||
status: string
|
||||
stage?: string
|
||||
end?: boolean
|
||||
@@ -20,12 +18,9 @@ export interface ExecuteResult {
|
||||
|
||||
export default class SessionHandler {
|
||||
session: HydratedDocument<ISession>
|
||||
quest: HydratedDocument<IQuest>
|
||||
quest: Quest
|
||||
|
||||
constructor(
|
||||
session: HydratedDocument<ISession>,
|
||||
quest: HydratedDocument<IQuest>
|
||||
) {
|
||||
constructor(session: HydratedDocument<ISession>, quest: Quest) {
|
||||
this.session = session
|
||||
this.quest = quest
|
||||
}
|
||||
@@ -34,7 +29,7 @@ export default class SessionHandler {
|
||||
return this.session
|
||||
}
|
||||
|
||||
getTask(stage: Partial<IStage>): ITask | undefined {
|
||||
getTask(stage: Partial<Stage>): Task | undefined {
|
||||
return this.session.tasks.find((t) => t.id === stage.id)
|
||||
}
|
||||
|
||||
@@ -43,7 +38,9 @@ export default class SessionHandler {
|
||||
return task ? task.completed : false
|
||||
}
|
||||
|
||||
getUnlockedComponents<T extends IRequirement>(components: T[]): T[] {
|
||||
getUnlockedComponents<T extends { requirements: Requirement }>(
|
||||
components: T[]
|
||||
): T[] {
|
||||
return components.filter((component) =>
|
||||
component.requirements.every((r: string) => this.isCompleted({ id: r }))
|
||||
)
|
||||
@@ -67,39 +64,30 @@ export default class SessionHandler {
|
||||
this.session.tasks.push(...newTasks)
|
||||
}
|
||||
|
||||
execute(stage: IStage): ExecuteResult {
|
||||
execute(stage: Stage): ExecuteResult {
|
||||
this.session.tasks
|
||||
.filter((t) => t.id === stage.id)
|
||||
.forEach((t) => {
|
||||
t.completed = true
|
||||
})
|
||||
|
||||
if (stage.hints && stage.hints.length > 0)
|
||||
this.session.hints.push(stage.hints)
|
||||
|
||||
this.addNewTasks()
|
||||
|
||||
this.session.responses.push(stage.responses)
|
||||
|
||||
if (stage.id === 'END') {
|
||||
this.session.status = 'finished'
|
||||
}
|
||||
|
||||
return {
|
||||
responses: stage.responses,
|
||||
hints: stage.hints,
|
||||
tasks: this.session.tasks,
|
||||
status: this.session.status,
|
||||
}
|
||||
}
|
||||
|
||||
executeException(exception: IException): ExecuteResult {
|
||||
const { hints, responses } = exception
|
||||
if (hints && hints.length > 0) this.session.hints.push(hints)
|
||||
executeException(exception: Exception): ExecuteResult {
|
||||
const { responses } = exception
|
||||
|
||||
return {
|
||||
responses,
|
||||
hints,
|
||||
status: this.session.status,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import { TRPCError } from '@trpc/server'
|
||||
import { Quest } from '@linux-odyssey/models'
|
||||
import { QuestDetailResponse } from '@linux-odyssey/constants'
|
||||
import { z } from 'zod'
|
||||
import { router, publicProcedure } from '../trpc.js'
|
||||
import { questManager } from '../models/quest.js'
|
||||
|
||||
export const questRouter = router({
|
||||
getQuests: publicProcedure.query(async () => {
|
||||
const quests = await Quest.find({})
|
||||
return quests.map(({ title, _id, requirements }) => ({
|
||||
_id,
|
||||
const quests = await questManager.getAll()
|
||||
return quests.map(({ title, id, requirements }) => ({
|
||||
id,
|
||||
title,
|
||||
requirements,
|
||||
}))
|
||||
@@ -16,15 +16,15 @@ export const questRouter = router({
|
||||
getQuestDetail: publicProcedure
|
||||
.input(z.string())
|
||||
.query(async (opts): Promise<QuestDetailResponse> => {
|
||||
const id = opts.input
|
||||
const quest = await Quest.findById(id)
|
||||
const questId = opts.input
|
||||
const quest = await questManager.get(questId)
|
||||
if (!quest) {
|
||||
throw new TRPCError({
|
||||
code: 'NOT_FOUND',
|
||||
message: 'Quest not found.',
|
||||
})
|
||||
}
|
||||
const { _id, title, instruction, requirements } = quest
|
||||
return { _id, title, instruction, requirements }
|
||||
const { id, title, instruction, requirements } = quest
|
||||
return { id, title, instruction, requirements }
|
||||
}),
|
||||
})
|
||||
@@ -34,10 +34,7 @@ export const sessionRouter = router({
|
||||
status: session.status,
|
||||
createdAt: session.createdAt,
|
||||
lastActivityAt: session.lastActivityAt,
|
||||
hints: session.hints,
|
||||
tasks: session.tasks,
|
||||
responses: session.responses,
|
||||
graph: session.graph,
|
||||
stages: session.stages,
|
||||
}
|
||||
}
|
||||
throw new TRPCError({
|
||||
|
||||
Reference in new issue
Block a user