mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 11:30:13 +08:00
wip: migrate backend to new game package
This commit is contained in:
1 parent
1d843f7355
commit
1616f62755
94 files changed
+263
-3055
No files matched your search
@@ -16,6 +16,19 @@ class QuestValidationError extends Error {
|
||||
this.questId = questId
|
||||
this.error = error
|
||||
}
|
||||
|
||||
toString() {
|
||||
if (this.error?.issues) {
|
||||
const issues = this.error.issues
|
||||
.map((issue: any) => {
|
||||
const fieldPath = issue.path.join('.')
|
||||
return `- ${fieldPath}: ${issue.message}`
|
||||
})
|
||||
.join('\n')
|
||||
return `Error in quest "${this.questId}":\n${issues}`
|
||||
}
|
||||
return `Error in quest "${this.questId}": ${this.message}`
|
||||
}
|
||||
}
|
||||
|
||||
class QuestManager {
|
||||
@@ -49,9 +62,18 @@ class QuestManager {
|
||||
}
|
||||
|
||||
private async loadGlobalExceptions() {
|
||||
const exceptionsPath = path.join(this.questDirectory, 'exceptions.yml')
|
||||
const body = await fs.readFile(exceptionsPath, 'utf-8')
|
||||
return globalExceptionSchema.array().parse(yaml.parse(body))
|
||||
try {
|
||||
const exceptionsPath = path.join(this.questDirectory, 'exceptions.yml')
|
||||
const body = await fs.readFile(exceptionsPath, 'utf-8')
|
||||
return globalExceptionSchema.array().parse(yaml.parse(body))
|
||||
} catch (error) {
|
||||
logger.error('Error loading global exceptions', { error })
|
||||
throw new QuestValidationError(
|
||||
'Failed to load global exceptions',
|
||||
'global',
|
||||
error
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
async loadAndUpdateQuests() {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { HydratedDocument, Types } from 'mongoose'
|
||||
import { Session, Quest, UserProfile } from '@linux-odyssey/models'
|
||||
import { Session, 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'
|
||||
import { questManager } from './quest.js'
|
||||
|
||||
async function deactivateSessions(userId: Types.ObjectId, quest: string) {
|
||||
const sessions = await Session.find({
|
||||
@@ -31,13 +31,13 @@ export async function createNewSession(
|
||||
user: HydratedDocument<IUser>,
|
||||
questId: string
|
||||
): Promise<ISession> {
|
||||
const quest = await Quest.findById(questId)
|
||||
const quest = await questManager.get(questId)
|
||||
if (!quest) {
|
||||
throw new Error(`Quest ${questId} not found`)
|
||||
}
|
||||
|
||||
// deactivate all active sessions
|
||||
deactivateSessions(user._id, quest._id).catch((err) => {
|
||||
deactivateSessions(user._id, quest.id).catch((err) => {
|
||||
logger.error(err)
|
||||
})
|
||||
|
||||
@@ -53,31 +53,24 @@ export async function createNewSession(
|
||||
containerId: container.id,
|
||||
})
|
||||
|
||||
const sessionHandler = new SessionHandler(newSession, quest)
|
||||
|
||||
sessionHandler.addNewTasks()
|
||||
|
||||
const session = sessionHandler.getSession()
|
||||
await session.save()
|
||||
|
||||
const userProfile = await UserProfile.findOne({ user: user._id })
|
||||
if (!userProfile) {
|
||||
throw new Error(`UserProfile ${user._id} not found`)
|
||||
}
|
||||
|
||||
const progress = userProfile.progress.get(quest._id)
|
||||
const progress = userProfile.progress.get(quest.id)
|
||||
if (!progress) {
|
||||
userProfile.progress.set(quest._id, {
|
||||
quest: quest._id,
|
||||
sessions: [session._id],
|
||||
userProfile.progress.set(quest.id, {
|
||||
quest: quest.id,
|
||||
sessions: [newSession._id],
|
||||
startedAt: new Date(),
|
||||
completed: false,
|
||||
})
|
||||
} else {
|
||||
progress.sessions.push(session._id)
|
||||
progress.sessions.push(newSession._id)
|
||||
}
|
||||
await userProfile.save()
|
||||
return session
|
||||
return newSession
|
||||
}
|
||||
|
||||
export async function finishSession(
|
||||
@@ -110,7 +103,7 @@ export async function isQuestUnlocked(
|
||||
if (!userProfile) {
|
||||
throw new Error(`UserProfile ${user.id} not found`)
|
||||
}
|
||||
const quest = await Quest.findById(questId)
|
||||
const quest = await questManager.get(questId)
|
||||
if (!quest) {
|
||||
throw new Error(`Quest ${questId} not found`)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user