mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 11:30:12 +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
+28
-4
No files matched your search
@@ -3,6 +3,7 @@ import { Session } from '@linux-odyssey/models'
|
|||||||
import {
|
import {
|
||||||
createNewSession,
|
createNewSession,
|
||||||
getOrCreateActiveSession,
|
getOrCreateActiveSession,
|
||||||
|
isQuestUnlocked,
|
||||||
} from '../../game/sessionManager.js'
|
} from '../../game/sessionManager.js'
|
||||||
import { asyncHandler } from '../../middleware/error.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) => {
|
export const createSession = asyncHandler(async (req, res) => {
|
||||||
const { questId } = matchedData(req)
|
const { questId } = matchedData(req)
|
||||||
|
if (await isQuestUnlocked(req.user, questId)) {
|
||||||
const session = await createNewSession(req.user, questId)
|
const session = await createNewSession(req.user, questId)
|
||||||
res.status(201).json(sessionDetail(session))
|
res.status(201).json(sessionDetail(session))
|
||||||
|
} else {
|
||||||
|
res.status(403).json({ message: 'Quest is locked.' })
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
export const getSessionById = asyncHandler(async (req, res) => {
|
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) => {
|
export const getOrCreateSession = asyncHandler(async (req, res) => {
|
||||||
const { questId } = matchedData(req)
|
const { questId } = matchedData(req)
|
||||||
|
if (await isQuestUnlocked(req.user, questId)) {
|
||||||
const session = await getOrCreateActiveSession(req.user, questId)
|
const session = await getOrCreateActiveSession(req.user, questId)
|
||||||
res.json(sessionDetail(session))
|
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 })
|
check('questId').notEmpty().isString().isAlphanumeric().isLength({ max: 64 })
|
||||||
|
|
||||||
export const questIdValidator = [checkQuestId(), noError]
|
export const questIdValidator = [checkQuestId(), noError]
|
||||||
|
|
||||||
export default {
|
|
||||||
questIdValidator,
|
|
||||||
}
|
|
||||||
@@ -99,3 +99,18 @@ export async function finishSession(session) {
|
|||||||
}
|
}
|
||||||
await userProfile.save()
|
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
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -79,6 +79,8 @@ paths:
|
|||||||
properties:
|
properties:
|
||||||
message:
|
message:
|
||||||
type: string
|
type: string
|
||||||
|
'403':
|
||||||
|
description: This quest is locked.
|
||||||
|
|
||||||
/sessions/active:
|
/sessions/active:
|
||||||
post:
|
post:
|
||||||
@@ -108,6 +110,8 @@ paths:
|
|||||||
$ref: '#/components/schemas/Session'
|
$ref: '#/components/schemas/Session'
|
||||||
400:
|
400:
|
||||||
description: 'Bad Request. The request is invalid or malformed.'
|
description: 'Bad Request. The request is invalid or malformed.'
|
||||||
|
403:
|
||||||
|
description: This quest is locked.
|
||||||
500:
|
500:
|
||||||
description: 'Internal Server Error.'
|
description: 'Internal Server Error.'
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user