refactor: game

This commit is contained in:
wancat committed 2024-02-19 23:13:37 +08:00
1 parent 2d05149cea
commit 4c421b4cb3
9 files changed
+154 -58

No files matched your search

+1 -2
View File
@@ -3,10 +3,9 @@ import { conditionSchema, ICondition } from './condition.js'
import { responseSchema, IResponse } from './response.js'
import { hintSchema, IHint } from './hint.js'
export interface ICommandResponder {
export interface ICommandResponder extends IHint {
condition?: ICondition
responses: IResponse[]
hints?: IHint[]
}
export const commandResponderSchema = new Schema<ICommandResponder>({
+5 -1
View File
@@ -2,10 +2,14 @@ import mongoose from 'mongoose'
export { User, IUser } from './user.js'
export { Quest, IQuest } from './quest.js'
export { Session, ISession } from './session.js'
export { Session, ISession, ITask } from './session.js'
export { Command, ICommand } from './command.js'
export { UserProfile, IUserProfile } 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
View File
@@ -42,6 +42,7 @@ const taskSchema = new Schema<ITask>({
})
export interface ISession {
_id: Types.ObjectId
user: Types.ObjectId
quest: string
containerId: string | null
+1
View File
@@ -49,6 +49,7 @@
"@types/express": "^4.17.21",
"@types/express-session": "^1.17.10",
"@types/jsonwebtoken": "^9.0.5",
"@types/minimist": "^1.2.5",
"@types/node-schedule": "^2.1.6",
"@types/passport": "^1.0.16",
"@types/passport-github2": "^1.2.9",
@@ -1,5 +1,6 @@
import { Command, Session } from '@linux-odyssey/models'
import { Command, Quest, 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'
@@ -7,10 +8,10 @@ import { asyncHandler } from '../../middleware/error.js'
const commandCompleteCallbacks = new Map()
export const newCommand = asyncHandler(async (req, res) => {
export const newCommand = asyncHandler(async (req: Request, res: Response) => {
const { command, pwd, output, error, params } = matchedData(req)
const { sessionId } = req.user
const { sessionId } = req.user as any
const session = await Session.findById(sessionId)
if (!session) {
res.status(404).json({ message: 'session not found' })
@@ -38,7 +39,13 @@ export const newCommand = asyncHandler(async (req, res) => {
await c.save()
const commandHandler = new CommandHandler(session, c, params)
const quest = await Quest.findById(session.quest)
if (!quest) {
res.status(404).json({ message: 'quest not found' })
return
}
const commandHandler = new CommandHandler(session, quest, c, params)
const response = await commandHandler.run()
if (response) {
c.stage = response.stage
@@ -49,14 +56,17 @@ export const newCommand = asyncHandler(async (req, res) => {
}
commandCompleteCallbacks.set(session.id, response.callback)
pushToSession(session.id, 'status', 'pending')
res.status(201).json(response)
} else {
res.status(200).end()
}
res.status(201).json(response)
await session.save()
})
export const completedCommand = asyncHandler(async (req, res) => {
const { sessionId } = req.user
export const completedCommand = asyncHandler(
async (req: Request, res: Response) => {
const { sessionId } = req.user as any
const session = await Session.findById(sessionId)
@@ -78,4 +88,5 @@ export const completedCommand = asyncHandler(async (req, res) => {
callback()
commandCompleteCallbacks.delete(session.id)
res.json({ message: 'ok' })
})
}
)
@@ -1,23 +1,41 @@
import minimist from 'minimist'
import { HydratedDocument } from 'mongoose'
import { FileGraph } from '@linux-odyssey/file-graph'
import { Quest } from '@linux-odyssey/models'
import {
ICommand,
ICondition,
IFileCondition,
IQuest,
ISession,
IStage,
IException,
} from '@linux-odyssey/models'
import { pushToSession } from '../api/socket.js'
import SessionHandler from './sessionHandler.js'
import SessionHandler, { ExecuteResult } from './sessionHandler.js'
import { checkFile } from '../containers/cli.js'
import logger from '../utils/logger.js'
// 檢查 pattern 是否符合 input
const checkMatch = (pattern, input) => {
const checkMatch = (pattern?: string[], input?: string) => {
if (!pattern || pattern.length === 0) return true
return pattern.some((p) => {
const regex = new RegExp(p)
return regex.test(input)
return input && regex.test(input)
})
}
export default class CommandHandler extends SessionHandler {
constructor(session, commandInput, params) {
super(session)
commandInput: ICommand
argv: minimist.ParsedArgs
params: Record<string, any>
constructor(
session: HydratedDocument<ISession>,
quest: HydratedDocument<IQuest>,
commandInput: ICommand,
params: Record<string, any>
) {
super(session, quest)
this.commandInput = commandInput
this.argv = minimist(this.commandInput.command.split(' '))
@@ -56,7 +74,7 @@ export default class CommandHandler extends SessionHandler {
this.session.graph = graph
} catch (e) {
logger.error('Update file graph failed', {
error: e.message,
error: (e as Error).message,
session: this.session.id,
graph: this.session.graph,
discover: this.params.discover,
@@ -64,7 +82,7 @@ export default class CommandHandler extends SessionHandler {
}
}
async isMatch(condition) {
async isMatch(condition: ICondition): Promise<boolean> {
return (
this.checkKeys(condition) &&
(await this.checkFiles(condition.files)) &&
@@ -73,18 +91,25 @@ export default class CommandHandler extends SessionHandler {
)
}
checkKeys(condition) {
const keys = ['command', 'output', 'error', 'pwd']
return keys.every((k) => checkMatch(condition[k], this.commandInput[k]))
checkKeys(condition: ICondition) {
return (
checkMatch(condition.command, this.commandInput.command) &&
checkMatch(condition.output, this.commandInput.output) &&
checkMatch(condition.error, this.commandInput.error) &&
checkMatch(condition.pwd, this.commandInput.pwd)
)
}
async checkFiles(files) {
if (files.length === 0) {
async checkFiles(files?: IFileCondition[]): Promise<boolean> {
if (!files || files.length === 0) {
return true
}
try {
const checks = await Promise.all(
files.map((f) => checkFile(this.session.containerId, f))
files.map(
(f) =>
this.session.containerId && checkFile(this.session.containerId, f)
)
)
return checks.every((c) => c === true)
} catch (e) {
@@ -92,22 +117,22 @@ export default class CommandHandler extends SessionHandler {
}
}
async checkNot(condition) {
async checkNot(condition?: ICondition): Promise<boolean> {
if (!condition) {
return true
}
return !(await this.isMatch(condition))
}
async checkOr(conditions) {
if (conditions.length === 0) {
async checkOr(conditions?: ICondition[]): Promise<boolean> {
if (!conditions || conditions.length === 0) {
return true
}
const matches = await Promise.all(conditions.map((c) => this.isMatch(c)))
return matches.some((m) => m === true)
}
async checkException(stage) {
async checkException(stage: IStage): Promise<IException | null> {
const exceptions = stage.exceptions || []
for (const exception of exceptions) {
// eslint-disable-next-line no-await-in-loop
@@ -119,22 +144,25 @@ export default class CommandHandler extends SessionHandler {
return null
}
async run() {
async run(): Promise<ExecuteResult | null> {
try {
this.quest = await Quest.findById(this.session.quest)
const stages = this.getStages()
if (stages.length === 0) {
logger.warn('stage not found', { session: this.session.id })
return {}
return null
}
this.handleEvent()
this.handleCommand()
console.log(stages)
const matches = await Promise.all(
stages.map((stage) => this.isMatch(stage.condition))
stages.map((stage) => stage.condition && this.isMatch(stage.condition))
)
console.log(matches)
const stage = stages.find((_, i) => matches[i])
if (stage) {
const response = this.execute(stage)
@@ -161,22 +189,25 @@ export default class CommandHandler extends SessionHandler {
this.quest.exceptions
)
for (const exception of globalExceptions) {
if (
exception.condition &&
// eslint-disable-next-line no-await-in-loop
if (await this.isMatch(exception.condition)) {
(await this.isMatch(exception.condition))
) {
return this.executeException(exception)
}
}
}
return {}
return null
} catch (err) {
logger.error('CommandHandler error', {
error: err.message,
error: (err as Error).message,
session: this.session._id,
quest: this.quest._id,
command: this.commandInput,
})
return {}
return null
}
}
}
@@ -1,27 +1,51 @@
import { HydratedDocument } from 'mongoose'
import type {
IException,
IQuest,
IRequirement,
IResponse,
ISession,
IStage,
ITask,
} from '@linux-odyssey/models'
import { pushToSession } from '../api/socket.js'
export default class SessionHandler {
constructor(session) {
this.session = session
this.quest = session.quest
export interface ExecuteResult {
responses: IResponse[]
hints: string[]
callback: () => void
stage?: string
end?: boolean
}
getSession() {
export default class SessionHandler {
session: HydratedDocument<ISession>
quest: HydratedDocument<IQuest>
constructor(
session: HydratedDocument<ISession>,
quest: HydratedDocument<IQuest>
) {
this.session = session
this.quest = quest
}
getSession(): HydratedDocument<ISession> {
return this.session
}
getTask(stage) {
getTask(stage: Partial<IStage>): ITask | undefined {
return this.session.tasks.find((t) => t.id === stage.id)
}
isCompleted(stage) {
isCompleted(stage: Partial<IStage>): boolean {
const task = this.getTask(stage)
return task ? task.completed : false
}
getUnlockedComponents(components) {
getUnlockedComponents<T extends IRequirement>(components: T[]): T[] {
return components.filter((component) =>
component.requirements.every((r) => this.isCompleted({ id: r }))
component.requirements.every((r: string) => this.isCompleted({ id: r }))
)
}
@@ -43,7 +67,7 @@ export default class SessionHandler {
this.session.tasks.push(...newTasks)
}
execute(stage) {
execute(stage: IStage): ExecuteResult {
this.session.tasks
.filter((t) => t.id === stage.id)
.forEach((t) => {
@@ -67,7 +91,7 @@ export default class SessionHandler {
}
}
executeException(exception) {
executeException(exception: IException): ExecuteResult {
const { hints, responses } = exception
if (hints && hints.length > 0) this.session.hints.push(hints)
@@ -1,28 +1,36 @@
import { HydratedDocument, Types } from 'mongoose'
import { Session, Quest, UserProfile } from '@linux-odyssey/models'
import type { ISession, IUser } from '@linux-odyssey/models'
import { createContainer, deleteContainer } from '../containers/docker.js'
import SessionHandler from '../game/sessionHandler.js'
import logger from '../utils/logger.js'
async function deactivateSessions(user, quest) {
async function deactivateSessions(userId: Types.ObjectId, quest: string) {
const sessions = await Session.find({
user,
user: userId,
quest,
status: 'active',
})
return Promise.all(
sessions.map((session) => {
return deleteContainer(session.containerId)
return (
session.containerId &&
deleteContainer(session.containerId)
.catch((err) => logger.error('Failed to delete container', err))
.finally(() => {
return Session.findByIdAndUpdate(session._id, {
$set: { status: 'inactive', containerId: null },
}).catch((err) => logger.error('Failed to update session', err))
})
)
})
)
}
export async function createNewSession(user, questId) {
export async function createNewSession(
user: HydratedDocument<IUser>,
questId: string
): Promise<ISession> {
const quest = await Quest.findById(questId)
if (!quest) {
throw new Error(`Quest ${questId} not found`)
@@ -38,13 +46,13 @@ export async function createNewSession(user, questId) {
quest.image
)
const sessionHandler = new SessionHandler(
new Session({
const newSession = new Session({
user,
quest,
containerId: container.id,
})
)
const sessionHandler = new SessionHandler(newSession, quest)
sessionHandler.addNewTasks()
@@ -62,6 +70,7 @@ export async function createNewSession(user, questId) {
quest: quest._id,
sessions: [session._id],
startedAt: new Date(),
completed: false,
})
} else {
progress.sessions.push(session._id)
@@ -70,7 +79,10 @@ export async function createNewSession(user, questId) {
return session
}
export async function getOrCreateActiveSession(user, questId) {
export async function getOrCreateActiveSession(
user: HydratedDocument<IUser>,
questId: string
): Promise<ISession> {
const session = await Session.findOne({
user,
quest: questId,
@@ -82,7 +94,9 @@ export async function getOrCreateActiveSession(user, questId) {
return createNewSession(user, questId)
}
export async function finishSession(session) {
export async function finishSession(
session: HydratedDocument<ISession>
): Promise<void> {
session.status = 'finished'
await session.save()
@@ -102,7 +116,10 @@ export async function finishSession(session) {
await userProfile.save()
}
export async function isQuestUnlocked(user, questId) {
export async function isQuestUnlocked(
user: HydratedDocument<IUser>,
questId: string
): Promise<boolean> {
const userProfile = await UserProfile.findOne({ user: user._id })
if (!userProfile) {
throw new Error(`UserProfile ${user._id} not found`)
+8
View File
@@ -1545,6 +1545,13 @@ __metadata:
languageName: node
linkType: hard
"@types/minimist@npm:^1.2.5":
version: 1.2.5
resolution: "@types/minimist@npm:1.2.5"
checksum: 10c0/3f791258d8e99a1d7d0ca2bda1ca6ea5a94e5e7b8fc6cde84dd79b0552da6fb68ade750f0e17718f6587783c24254bbca0357648dd59dc3812c150305cabdc46
languageName: node
linkType: hard
"@types/mongoose@npm:^5.11.97":
version: 5.11.97
resolution: "@types/mongoose@npm:5.11.97"
@@ -8614,6 +8621,7 @@ __metadata:
"@types/express": "npm:^4.17.21"
"@types/express-session": "npm:^1.17.10"
"@types/jsonwebtoken": "npm:^9.0.5"
"@types/minimist": "npm:^1.2.5"
"@types/node-schedule": "npm:^2.1.6"
"@types/passport": "npm:^1.0.16"
"@types/passport-github2": "npm:^1.2.9"