Some repatches

This commit is contained in:
aronmal 2023-04-23 19:01:55 +02:00
parent dab3abdda2
commit c895bcef19
Signed by: aronmal
GPG key ID: 816B7707426FC612
10 changed files with 106 additions and 42 deletions

View file

@ -1,18 +1,17 @@
import { gameContext } from "../../pages/_app"
import Icon from "./Icon"
import Player from "./Player"
import { Fragment, useContext, useEffect, useState } from "react"
import useGameState from "@lib/hooks/useGameState"
import { Fragment, useEffect, useState } from "react"
function LobbyFrame({ openSettings }: { openSettings: () => void }) {
const [gameProps, setGameProps] = useContext(gameContext)
const { enemy } = gameProps
const { gameProps } = useGameState()
const [dots, setDots] = useState(1)
useEffect(() => {
if (enemy) return
if (gameProps.enemy) return
const interval = setInterval(() => setDots((e) => (e % 3) + 1), 1000)
return () => clearInterval(interval)
}, [enemy])
}, [gameProps.enemy])
return (
<div className="mx-32 flex flex-col self-stretch rounded-3xl bg-gray-400">
@ -33,8 +32,11 @@ function LobbyFrame({ openSettings }: { openSettings: () => void }) {
edit={true}
/>
<p className="font-farro m-4 text-6xl font-semibold">VS</p>
{enemy ? (
<Player src="player_red.png" text={enemy.name ?? "Spieler 2"} />
{gameProps.enemy ? (
<Player
src="player_red.png"
text={gameProps.enemy.name ?? "Spieler 2"}
/>
) : (
<p className="font-farro w-96 text-center text-4xl font-medium">
Warte auf Spieler 2 {Array.from(Array(dots), () => ".").join("")}

View file

@ -1,4 +1,4 @@
import logging, { Logging } from "../logging"
import logging, { Logging } from "./logging"
import { NextApiRequest, NextApiResponse } from "next"
export interface Result<T> {

View file

@ -1,3 +1,4 @@
import { GameState } from "@prisma/client"
import { z } from "zod"
export const createSchema = z.object({
@ -5,7 +6,7 @@ export const createSchema = z.object({
id: z.string(),
createdAt: z.date(),
updatedAt: z.date(),
running: z.boolean(),
state: z.nativeEnum(GameState),
}),
pin: z.string().optional(),
player: z.object({

View file

@ -1,5 +1,5 @@
import "../styles/App.scss"
import "../styles/globals.css"
import "../styles/globals.scss"
import "../styles/grid2.scss"
import "../styles/grid.scss"
import { SessionProvider } from "next-auth/react"

View file

@ -8,7 +8,6 @@ import {
uniqueNamesGenerator,
Config,
adjectives,
colors,
animals,
NumberDictionary,
} from "unique-names-generator"
@ -22,7 +21,6 @@ const customConfig: Config = {
}
const options: NextAuthOptions = {
debug: true,
providers: [
EmailProvider({
server: process.env.EMAIL_SERVER,

View file

@ -1,5 +1,7 @@
import { authOptions } from "../auth/[...nextauth]"
import { getAnyRunningGame } from "./running"
import sendResponse from "@backend/sendResponse"
import { rejectionErrors } from "@lib/backend/errors"
import prisma from "@lib/prisma"
import { createSchema } from "@lib/zodSchemas"
import type { NextApiRequest, NextApiResponse } from "next"
@ -14,20 +16,8 @@ export default async function create(
) {
const session = await getServerSession(req, res, authOptions)
if (!session) {
return sendResponse(req, res, {
message: "Unauthorized",
statusCode: 401,
type: ["error"],
})
}
if (!session.user) {
return sendResponse(req, res, {
message: "Unauthorized - No User",
statusCode: 401,
type: ["error"],
})
if (!session?.user) {
return sendResponse(req, res, rejectionErrors.unauthorized)
}
const { email, id, name } = session.user
@ -38,20 +28,7 @@ export default async function create(
let created = false
let game = await prisma.game.findFirst({
where: {
running: true,
users: {
some: {
userId: id,
},
},
},
include: {
gamePin: true,
users: true,
},
})
let game = await getAnyRunningGame(id)
if (!game) {
created = true
game = await prisma.game.create({

View file

@ -0,0 +1,58 @@
import { authOptions } from "../auth/[...nextauth]"
import sendResponse from "@backend/sendResponse"
import { rejectionErrors } from "@lib/backend/errors"
import prisma from "@lib/prisma"
import { Game } from "@prisma/client"
import type { NextApiRequest, NextApiResponse } from "next"
import { getServerSession } from "next-auth"
type Data = { game: Game }
export const getAnyRunningGame = (id: string) =>
prisma.game.findFirst({
where: {
NOT: {
state: "ended",
},
users: {
some: {
userId: id,
},
},
},
include: {
gamePin: true,
users: true,
},
})
export default async function create(
req: NextApiRequest,
res: NextApiResponse<Data>
) {
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"],
})
return sendResponse(req, res, {
message: `User <${email}> asked for game: ${game.id}`,
statusCode: 200,
body: {
game,
},
type: ["debug", "infoCyan"],
})
}

View file

@ -35,6 +35,19 @@ export function isAuthenticated(res: Response) {
return Promise.reject()
}
const handleConfirmation = () => {
const toastId = "confirm"
toast.warn(
<div id="toast-confirm">
<h4>You are already in another round, do you want to:</h4>
<button onClick={() => toast.dismiss(toastId)}>Join</button>
or
<button onClick={() => toast.dismiss(toastId)}>Leave</button>
</div>,
{ autoClose: false, toastId }
)
}
export default function Home({ q }: Props) {
const [otp, setOtp] = useState("")
const { gameProps, setGameProps } = useGameState()

View file

@ -69,11 +69,17 @@ enum TokenType {
ACCESS
}
enum GameState {
launching
running
ended
}
model Game {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
running Boolean @default(true)
state GameState @default(launching)
gamePin Gamepin?
users User_Game[]
}

View file

@ -26,6 +26,15 @@ button {
cursor: none;
}
#toast-confirm {
button {
margin: 0 1rem;
padding: 0 1rem;
border: 2px solid gray;
border-radius: 0.5rem;
}
}
@media (pointer: fine) {
button {
cursor: pointer;