mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
feat: check if a quest is unlocked before creating session
This commit is contained in:
1 parent
c2dac8d385
commit
92181d960f
4 files changed
+32
-8
No files matched your search
@@ -3,6 +3,7 @@ import { Session } from '@linux-odyssey/models'
|
||||
import {
|
||||
createNewSession,
|
||||
getOrCreateActiveSession,
|
||||
isQuestUnlocked,
|
||||
} from '../../game/sessionManager.js'
|
||||
import { asyncHandler } from '../../middleware/error.js'
|
||||
|
||||
@@ -39,8 +40,12 @@ export const getSessionList = asyncHandler(async (req, res) => {
|
||||
|
||||
export const createSession = asyncHandler(async (req, res) => {
|
||||
const { questId } = matchedData(req)
|
||||
const session = await createNewSession(req.user, questId)
|
||||
res.status(201).json(sessionDetail(session))
|
||||
if (await isQuestUnlocked(req.user, questId)) {
|
||||
const session = await createNewSession(req.user, questId)
|
||||
res.status(201).json(sessionDetail(session))
|
||||
} else {
|
||||
res.status(403).json({ message: 'Quest is locked.' })
|
||||
}
|
||||
})
|
||||
|
||||
export const getSessionById = asyncHandler(async (req, res) => {
|
||||
@@ -60,6 +65,10 @@ export const getSessionById = asyncHandler(async (req, res) => {
|
||||
|
||||
export const getOrCreateSession = asyncHandler(async (req, res) => {
|
||||
const { questId } = matchedData(req)
|
||||
const session = await getOrCreateActiveSession(req.user, questId)
|
||||
res.json(sessionDetail(session))
|
||||
if (await isQuestUnlocked(req.user, questId)) {
|
||||
const session = await getOrCreateActiveSession(req.user, questId)
|
||||
res.json(sessionDetail(session))
|
||||
} else {
|
||||
res.status(403).json({ message: 'Quest is locked.' })
|
||||
}
|
||||
})
|
||||
@@ -5,7 +5,3 @@ export const checkQuestId = () =>
|
||||
check('questId').notEmpty().isString().isAlphanumeric().isLength({ max: 64 })
|
||||
|
||||
export const questIdValidator = [checkQuestId(), noError]
|
||||
|
||||
export default {
|
||||
questIdValidator,
|
||||
}
|
||||
@@ -99,3 +99,18 @@ export async function finishSession(session) {
|
||||
}
|
||||
await userProfile.save()
|
||||
}
|
||||
|
||||
export async function isQuestUnlocked(user, questId) {
|
||||
const userProfile = await UserProfile.findOne({ user: user._id })
|
||||
if (!userProfile) {
|
||||
throw new Error(`UserProfile ${user._id} not found`)
|
||||
}
|
||||
const quest = await Quest.findById(questId)
|
||||
if (!quest) {
|
||||
throw new Error(`Quest ${questId} not found`)
|
||||
}
|
||||
return quest.requirements.every((requiredQuestId) => {
|
||||
const progress = userProfile.progress.get(requiredQuestId)
|
||||
return progress && progress.completed
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user