feat: check if a quest is unlocked before creating session

This commit is contained in:
wancat authored and Lin, Hung-Shin committed 2023-11-14 10:45:24 +08:00
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 { 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)
const session = await createNewSession(req.user, questId) if (await isQuestUnlocked(req.user, questId)) {
res.status(201).json(sessionDetail(session)) 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) => { 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)
const session = await getOrCreateActiveSession(req.user, questId) if (await isQuestUnlocked(req.user, questId)) {
res.json(sessionDetail(session)) 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 }) check('questId').notEmpty().isString().isAlphanumeric().isLength({ max: 64 })
export const questIdValidator = [checkQuestId(), noError] export const questIdValidator = [checkQuestId(), noError]
export default {
questIdValidator,
}
+15
View File
@@ -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
})
}
+4
View File
@@ -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.'