mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
refactor: separate createSession to sessionManager
This commit is contained in:
1 parent
7843020524
commit
11bc793b12
7 files changed
+68
-49
No files matched your search
@@ -27,5 +27,6 @@ module.exports = {
|
||||
'no-var': 'error',
|
||||
'no-plusplus': 'off',
|
||||
'prettier/prettier': ['error', { endOfLine: 'auto', semi: false }],
|
||||
'import/prefer-default-export': 'off',
|
||||
},
|
||||
}
|
||||
@@ -81,6 +81,7 @@ networks:
|
||||
internal:
|
||||
players:
|
||||
name: linux-odyssey-players
|
||||
internal: true
|
||||
|
||||
volumes:
|
||||
mongo:
|
||||
@@ -76,3 +76,4 @@ networks:
|
||||
internal:
|
||||
players:
|
||||
name: linux-odyssey-players
|
||||
internal: true
|
||||
@@ -1,6 +1,5 @@
|
||||
import { Session, Quest } from '@linux-odyssey/models'
|
||||
import { createContainer, deleteContainer } from '../../containers/docker.js'
|
||||
import SessionHandler from '../../game/sessionHandler.js'
|
||||
import { Session } from '@linux-odyssey/models'
|
||||
import { createNewSession } from '../../game/sessionManager.js'
|
||||
|
||||
function sessionSummary(session) {
|
||||
return {
|
||||
@@ -37,53 +36,14 @@ export async function getSessionList(req, res) {
|
||||
}
|
||||
}
|
||||
|
||||
async function deactivateSessions(user, quest) {
|
||||
const sessions = await Session.find({
|
||||
user,
|
||||
quest,
|
||||
status: 'active',
|
||||
})
|
||||
return Promise.all(
|
||||
sessions.map((session) => {
|
||||
return deleteContainer(session.containerId).then(() => {
|
||||
session.status = 'inactive'
|
||||
session.containerId = null
|
||||
return session.save()
|
||||
})
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
export async function createSession(req, res) {
|
||||
const quest = await Quest.findById(req.body.quest_id)
|
||||
if (!quest) {
|
||||
res.status(400).json({ message: 'Quest not found.' })
|
||||
return
|
||||
}
|
||||
|
||||
// deactivate all active sessions
|
||||
deactivateSessions(req.user._id, quest._id).catch((err) => {
|
||||
console.error(err)
|
||||
})
|
||||
|
||||
const { quest_id } = req.body
|
||||
try {
|
||||
const container = await createContainer(
|
||||
`quest-${quest.id}-${req.user.username}-${Date.now()}`
|
||||
)
|
||||
const newSession = new Session({
|
||||
user: req.user,
|
||||
quest,
|
||||
containerId: container.id,
|
||||
})
|
||||
|
||||
const session = new SessionHandler(newSession)
|
||||
session.addNewTasks()
|
||||
|
||||
await newSession.save()
|
||||
res.status(201).json(sessionDetail(newSession))
|
||||
const session = await createNewSession(req.user, quest_id)
|
||||
res.status(201).json(sessionDetail(session))
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
res.status(400).json({ message: err.message })
|
||||
res.status(500).json({ message: err.message })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,17 +25,19 @@ const containerOptions = {
|
||||
|
||||
// const network = engine.getNetwork(config.dockerNetwork)
|
||||
|
||||
export async function createContainer(name) {
|
||||
const container = await engine.createContainer({
|
||||
export function createContainer(name) {
|
||||
return engine.createContainer({
|
||||
...containerOptions,
|
||||
name,
|
||||
})
|
||||
return container
|
||||
}
|
||||
|
||||
export async function getAndStartContainer(id) {
|
||||
console.log(`Getting container: ${id}`)
|
||||
const container = engine.getContainer(id)
|
||||
if (!container) {
|
||||
throw new Error(`Container ${id} not found`)
|
||||
}
|
||||
if (!(await container.inspect()).State.Running) {
|
||||
await container.start()
|
||||
}
|
||||
|
||||
@@ -6,6 +6,10 @@ export default class SessionHandler {
|
||||
this.quest = session.quest
|
||||
}
|
||||
|
||||
getSession() {
|
||||
return this.session
|
||||
}
|
||||
|
||||
getTask(stage) {
|
||||
return this.session.tasks.find((t) => t.id === stage.id)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
import { Session, Quest } from '@linux-odyssey/models'
|
||||
import { createContainer, deleteContainer } from '../containers/docker.js'
|
||||
import SessionHandler from './sessionHandler.js'
|
||||
|
||||
async function deactivateSessions(user, quest) {
|
||||
const sessions = await Session.find({
|
||||
user,
|
||||
quest,
|
||||
status: 'active',
|
||||
})
|
||||
return Promise.all(
|
||||
sessions.map((session) => {
|
||||
return deleteContainer(session.containerId).then(() => {
|
||||
session.status = 'inactive'
|
||||
session.containerId = null
|
||||
return session.save()
|
||||
})
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
export async function createNewSession(user, questId) {
|
||||
const quest = await Quest.findById(questId)
|
||||
if (!quest) {
|
||||
throw new Error(`Quest ${questId} not found`)
|
||||
}
|
||||
|
||||
// deactivate all active sessions
|
||||
deactivateSessions(user._id, quest._id).catch((err) => {
|
||||
console.error(err)
|
||||
})
|
||||
|
||||
const container = await createContainer(
|
||||
`quest-${quest.id}-${user.username}-${Date.now()}`
|
||||
)
|
||||
|
||||
const sessionHandler = new SessionHandler(
|
||||
new Session({
|
||||
user,
|
||||
quest,
|
||||
containerId: container.id,
|
||||
})
|
||||
)
|
||||
|
||||
sessionHandler.addNewTasks()
|
||||
|
||||
const session = sessionHandler.getSession()
|
||||
await session.save()
|
||||
return session
|
||||
}
|
||||
Reference in new issue
Block a user