mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-07 19:10:12 +08:00
refactor: migrate session management to TRPC and remove legacy API endpoints
This commit is contained in:
1 parent
b4b8f8baee
commit
92173bb7e9
7 files changed
+71
-191
No files matched your search
@@ -1,13 +1,55 @@
|
||||
import { TRPCError } from '@trpc/server'
|
||||
import { Session } from '@linux-odyssey/models'
|
||||
import { ISession, Session } from '@linux-odyssey/models'
|
||||
import { GameSession, VoidFileExistenceChecker } from '@linux-odyssey/game'
|
||||
import { z } from 'zod'
|
||||
import { isQuestUnlocked } from '../models/sessionManager.js'
|
||||
import { createNewSession, isQuestUnlocked } from '../models/sessionManager.js'
|
||||
import config from '../config.js'
|
||||
import { protectedProcedure, router } from '../trpc.js'
|
||||
import { questManager } from '../models/quest.js'
|
||||
|
||||
function sessionDetail(session: ISession) {
|
||||
const quest = questManager.get(session.quest)
|
||||
if (!quest) {
|
||||
throw new TRPCError({
|
||||
code: 'NOT_FOUND',
|
||||
message: 'Quest not found.',
|
||||
})
|
||||
}
|
||||
const gameSession = new GameSession(
|
||||
session,
|
||||
quest,
|
||||
new VoidFileExistenceChecker()
|
||||
)
|
||||
return {
|
||||
_id: session._id.toString(),
|
||||
user: session.user,
|
||||
quest: session.quest,
|
||||
status: session.status,
|
||||
createdAt: session.createdAt,
|
||||
lastActivityAt: session.lastActivityAt,
|
||||
completedEvents: session.completedEvents,
|
||||
responses: gameSession.getResponses(),
|
||||
tasks: gameSession.getTasks(),
|
||||
}
|
||||
}
|
||||
|
||||
export const sessionRouter = router({
|
||||
createSession: protectedProcedure
|
||||
.input(z.object({ questId: z.string() }))
|
||||
.mutation(async (opts) => {
|
||||
const { questId } = opts.input
|
||||
const { user } = opts.ctx
|
||||
console.log('createSession', user, questId)
|
||||
if ((await isQuestUnlocked(user.id, questId)) || config.testing.enabled) {
|
||||
const session = await createNewSession(user.id, questId)
|
||||
return sessionDetail(session)
|
||||
}
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'Quest is locked.',
|
||||
})
|
||||
}),
|
||||
|
||||
getActiveSession: protectedProcedure
|
||||
.input(
|
||||
z.object({
|
||||
@@ -17,42 +59,14 @@ export const sessionRouter = router({
|
||||
.query(async (opts) => {
|
||||
const { questId } = opts.input
|
||||
const { user } = opts.ctx
|
||||
if (
|
||||
user &&
|
||||
((await isQuestUnlocked(user, questId)) || config.testing.enabled)
|
||||
) {
|
||||
const session = await Session.findOne({
|
||||
user: user.id,
|
||||
quest: questId,
|
||||
status: 'active',
|
||||
})
|
||||
if (!session) {
|
||||
return null
|
||||
}
|
||||
const quest = await questManager.get(questId)
|
||||
if (!quest) {
|
||||
return null
|
||||
}
|
||||
const gameSession = new GameSession(
|
||||
session,
|
||||
quest,
|
||||
new VoidFileExistenceChecker()
|
||||
)
|
||||
return {
|
||||
_id: session._id.toString(),
|
||||
user: session.user,
|
||||
quest: session.quest,
|
||||
status: session.status,
|
||||
createdAt: session.createdAt,
|
||||
lastActivityAt: session.lastActivityAt,
|
||||
completedEvents: session.completedEvents,
|
||||
responses: gameSession.getResponses(),
|
||||
tasks: gameSession.getTasks(),
|
||||
}
|
||||
}
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: 'Quest is locked',
|
||||
const session = await Session.findOne({
|
||||
user: user.id,
|
||||
quest: questId,
|
||||
status: 'active',
|
||||
})
|
||||
if (!session) {
|
||||
return null
|
||||
}
|
||||
return sessionDetail(session)
|
||||
}),
|
||||
})
|
||||
Reference in new issue
Block a user