mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
refactor: game
This commit is contained in:
1 parent
2d05149cea
commit
4c421b4cb3
9 files changed
+181
-85
No files matched your search
@@ -0,0 +1,108 @@
|
||||
import { HydratedDocument } from 'mongoose'
|
||||
import type {
|
||||
IException,
|
||||
IQuest,
|
||||
IRequirement,
|
||||
IResponse,
|
||||
ISession,
|
||||
IStage,
|
||||
ITask,
|
||||
} from '@linux-odyssey/models'
|
||||
import { pushToSession } from '../api/socket.js'
|
||||
|
||||
export interface ExecuteResult {
|
||||
responses: IResponse[]
|
||||
hints: string[]
|
||||
callback: () => void
|
||||
stage?: string
|
||||
end?: boolean
|
||||
}
|
||||
|
||||
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: Partial<IStage>): ITask | undefined {
|
||||
return this.session.tasks.find((t) => t.id === stage.id)
|
||||
}
|
||||
|
||||
isCompleted(stage: Partial<IStage>): boolean {
|
||||
const task = this.getTask(stage)
|
||||
return task ? task.completed : false
|
||||
}
|
||||
|
||||
getUnlockedComponents<T extends IRequirement>(components: T[]): T[] {
|
||||
return components.filter((component) =>
|
||||
component.requirements.every((r: string) => this.isCompleted({ id: r }))
|
||||
)
|
||||
}
|
||||
|
||||
getStages() {
|
||||
const unlockedStages = this.getUnlockedComponents(this.quest.stages)
|
||||
return unlockedStages.filter((s) => !this.isCompleted(s))
|
||||
}
|
||||
|
||||
getNewTasks() {
|
||||
const stages = this.getStages()
|
||||
return stages
|
||||
.filter((s) => s.task)
|
||||
.filter((s) => !this.session.tasks.some((t) => t.id === s.id))
|
||||
.map((s) => ({ id: s.id, name: s.task, completed: false }))
|
||||
}
|
||||
|
||||
addNewTasks() {
|
||||
const newTasks = this.getNewTasks()
|
||||
this.session.tasks.push(...newTasks)
|
||||
}
|
||||
|
||||
execute(stage: IStage): 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()
|
||||
|
||||
return {
|
||||
responses: stage.responses,
|
||||
hints: stage.hints,
|
||||
callback: () => {
|
||||
pushToSession(this.session.id, 'tasks', this.session.tasks)
|
||||
if (stage.hints && stage.hints.length > 0)
|
||||
pushToSession(this.session.id, 'hints', stage.hints)
|
||||
pushToSession(this.session.id, 'status', this.session.status)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
executeException(exception: IException): ExecuteResult {
|
||||
const { hints, responses } = exception
|
||||
if (hints && hints.length > 0) this.session.hints.push(hints)
|
||||
|
||||
return {
|
||||
responses,
|
||||
hints,
|
||||
callback: () => {
|
||||
if (hints && hints.length > 0)
|
||||
pushToSession(this.session.id, 'hints', hints)
|
||||
pushToSession(this.session.id, 'status', this.session.status)
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user