105 lines
2.3 KiB
TypeScript
105 lines
2.3 KiB
TypeScript
import { authOptions } from "../auth/[...nextauth]"
|
|
import sendResponse from "@backend/sendResponse"
|
|
import { rejectionErrors } from "@lib/backend/errors"
|
|
import { getObjectChecksum } from "@lib/getObjectChecksum"
|
|
import prisma from "@lib/prisma"
|
|
import { GamePropsSchema } from "@lib/zodSchemas"
|
|
import type { NextApiRequest, NextApiResponse } from "next"
|
|
import { getServerSession } from "next-auth"
|
|
|
|
export const gameIncludes = {
|
|
include: {
|
|
gamePin: true,
|
|
users: {
|
|
include: {
|
|
user: {
|
|
select: {
|
|
name: true,
|
|
email: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
export const getAnyGame = (gameId: string) => {
|
|
const game = prisma.game.findFirst({
|
|
where: {
|
|
NOT: {
|
|
state: "ended",
|
|
},
|
|
id: gameId,
|
|
},
|
|
...gameIncludes,
|
|
})
|
|
return game
|
|
}
|
|
|
|
export const getAnyRunningGame = (userId: string) => {
|
|
const game = prisma.game.findFirst({
|
|
where: {
|
|
NOT: {
|
|
state: "ended",
|
|
},
|
|
users: {
|
|
some: {
|
|
userId,
|
|
},
|
|
},
|
|
},
|
|
...gameIncludes,
|
|
})
|
|
return game
|
|
}
|
|
|
|
export function composeBody(
|
|
gameDB: NonNullable<Awaited<ReturnType<typeof getAnyRunningGame>>>
|
|
) {
|
|
const { gamePin, ...game } = gameDB
|
|
const users = gameDB.users.map(({ user, ...props }) => ({
|
|
...props,
|
|
...user,
|
|
}))
|
|
const player1 = users.find((user) => user.index === "player1")
|
|
const player2 = users.find((user) => user.index === "player2")
|
|
const payload = {
|
|
game,
|
|
gamePin,
|
|
player1,
|
|
player2,
|
|
}
|
|
const hash = getObjectChecksum(payload)
|
|
return { payload, hash }
|
|
}
|
|
|
|
export default async function running(
|
|
req: NextApiRequest,
|
|
res: NextApiResponse<GamePropsSchema>
|
|
) {
|
|
const session = await getServerSession(req, res, authOptions)
|
|
|
|
if (!session?.user) {
|
|
return sendResponse(req, res, rejectionErrors.unauthorized)
|
|
}
|
|
|
|
const { email, id } = session.user
|
|
|
|
const game = await getAnyRunningGame(id)
|
|
|
|
if (!game)
|
|
return sendResponse(req, res, {
|
|
message: `User <${email}> is in no game.`,
|
|
statusCode: 204,
|
|
type: ["debug", "infoCyan"],
|
|
})
|
|
|
|
const body = composeBody(game)
|
|
|
|
return sendResponse(req, res, {
|
|
message: `User <${email}> asked for game: ${game.id}`,
|
|
statusCode: 200,
|
|
body,
|
|
type: ["debug", "infoCyan"],
|
|
})
|
|
}
|