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-var': 'error',
|
||||||
'no-plusplus': 'off',
|
'no-plusplus': 'off',
|
||||||
'prettier/prettier': ['error', { endOfLine: 'auto', semi: false }],
|
'prettier/prettier': ['error', { endOfLine: 'auto', semi: false }],
|
||||||
|
'import/prefer-default-export': 'off',
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -81,6 +81,7 @@ networks:
|
|||||||
internal:
|
internal:
|
||||||
players:
|
players:
|
||||||
name: linux-odyssey-players
|
name: linux-odyssey-players
|
||||||
|
internal: true
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
mongo:
|
mongo:
|
||||||
@@ -76,3 +76,4 @@ networks:
|
|||||||
internal:
|
internal:
|
||||||
players:
|
players:
|
||||||
name: linux-odyssey-players
|
name: linux-odyssey-players
|
||||||
|
internal: true
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
import { Session, Quest } from '@linux-odyssey/models'
|
import { Session } from '@linux-odyssey/models'
|
||||||
import { createContainer, deleteContainer } from '../../containers/docker.js'
|
import { createNewSession } from '../../game/sessionManager.js'
|
||||||
import SessionHandler from '../../game/sessionHandler.js'
|
|
||||||
|
|
||||||
function sessionSummary(session) {
|
function sessionSummary(session) {
|
||||||
return {
|
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) {
|
export async function createSession(req, res) {
|
||||||
const quest = await Quest.findById(req.body.quest_id)
|
const { quest_id } = req.body
|
||||||
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)
|
|
||||||
})
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const container = await createContainer(
|
const session = await createNewSession(req.user, quest_id)
|
||||||
`quest-${quest.id}-${req.user.username}-${Date.now()}`
|
res.status(201).json(sessionDetail(session))
|
||||||
)
|
|
||||||
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))
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(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)
|
// const network = engine.getNetwork(config.dockerNetwork)
|
||||||
|
|
||||||
export async function createContainer(name) {
|
export function createContainer(name) {
|
||||||
const container = await engine.createContainer({
|
return engine.createContainer({
|
||||||
...containerOptions,
|
...containerOptions,
|
||||||
name,
|
name,
|
||||||
})
|
})
|
||||||
return container
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getAndStartContainer(id) {
|
export async function getAndStartContainer(id) {
|
||||||
console.log(`Getting container: ${id}`)
|
console.log(`Getting container: ${id}`)
|
||||||
const container = engine.getContainer(id)
|
const container = engine.getContainer(id)
|
||||||
|
if (!container) {
|
||||||
|
throw new Error(`Container ${id} not found`)
|
||||||
|
}
|
||||||
if (!(await container.inspect()).State.Running) {
|
if (!(await container.inspect()).State.Running) {
|
||||||
await container.start()
|
await container.start()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,10 @@ export default class SessionHandler {
|
|||||||
this.quest = session.quest
|
this.quest = session.quest
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getSession() {
|
||||||
|
return this.session
|
||||||
|
}
|
||||||
|
|
||||||
getTask(stage) {
|
getTask(stage) {
|
||||||
return this.session.tasks.find((t) => t.id === stage.id)
|
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