Improved handling ws connection and redirect

This commit is contained in:
aronmal 2023-06-11 00:37:12 +02:00
parent 53a07b21b0
commit 0317a3343c
Signed by: aronmal
GPG key ID: 816B7707426FC612
10 changed files with 100 additions and 50 deletions

View file

@ -47,13 +47,13 @@ function LobbyFrame({ openSettings }: { openSettings: () => void }) {
)
useEffect(() => {
if (!launching || launchTime >= 1) return
router.push("/gamefield")
if (!launching || launchTime > 0) return
socket.emit("starting")
}, [launching, launchTime, router])
useEffect(() => {
if (!launching) return setLaunchTime(3)
if (launchTime === 0) return
if (launchTime < 0) return
const timeout = setTimeout(() => {
setLaunchTime((e) => e - 1)
@ -67,16 +67,29 @@ function LobbyFrame({ openSettings }: { openSettings: () => void }) {
socket.emit("update", full)
}, [full, payload?.game?.id, isConnected])
useEffect(() => {
if (
typeof payload?.game?.state !== "string" ||
payload?.game?.state === "lobby"
)
return
router.push("gamefield")
})
return (
<div className="mx-32 flex flex-col self-stretch rounded-3xl bg-gray-400">
<div className="flex items-center justify-between border-b-2 border-slate-900">
<Icon src="speech_bubble.png">Chat</Icon>
<h1 className="font-farro text-5xl font-medium">
{launching ? (
<WithDots>{"Game is starting in " + launchTime}</WithDots>
<WithDots>
{launchTime < 0
? "Game starts"
: "Game is starting in " + launchTime}
</WithDots>
) : (
<>
Game-PIN:{" "}
{"Game-PIN: "}
{isConnected ? (
<span className="underline">{payload?.gamePin ?? "----"}</span>
) : (
@ -114,8 +127,8 @@ function LobbyFrame({ openSettings }: { openSettings: () => void }) {
disabled={launching}
onClick={() => {
leave(async () => {
await router.push("/")
reset()
await router.push("/")
})
}}
>