Spaces:
Running
Running
Add chess-frontend
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- chess-frontend/.gitignore +41 -0
- chess-frontend/README.md +36 -0
- chess-frontend/actions/auth.ts +81 -0
- chess-frontend/actions/session.ts +38 -0
- chess-frontend/app/(auth)/layout.tsx +18 -0
- chess-frontend/app/(auth)/login/_components/login-form.tsx +103 -0
- chess-frontend/app/(auth)/login/page.tsx +37 -0
- chess-frontend/app/favicon.ico +0 -0
- chess-frontend/app/game/[id]/_components/clock.tsx +35 -0
- chess-frontend/app/game/[id]/_components/connection-status.tsx +23 -0
- chess-frontend/app/game/[id]/_components/game-over-overlay.tsx +39 -0
- chess-frontend/app/game/[id]/_components/gameboard.tsx +102 -0
- chess-frontend/app/game/[id]/_components/move-list.tsx +119 -0
- chess-frontend/app/game/[id]/_components/player-card.tsx +36 -0
- chess-frontend/app/game/[id]/page.tsx +11 -0
- chess-frontend/app/game/layout.tsx +33 -0
- chess-frontend/app/game/page.tsx +9 -0
- chess-frontend/app/globals.css +129 -0
- chess-frontend/app/layout.tsx +25 -0
- chess-frontend/app/page.tsx +7 -0
- chess-frontend/bun.lock +0 -0
- chess-frontend/components.json +25 -0
- chess-frontend/components/game/game-view.tsx +52 -0
- chess-frontend/components/game/gameboard.tsx +125 -0
- chess-frontend/components/game/lobby-client.tsx +86 -0
- chess-frontend/components/game/searching-modal.tsx +40 -0
- chess-frontend/components/ui/button.tsx +65 -0
- chess-frontend/components/ui/field.tsx +238 -0
- chess-frontend/components/ui/input.tsx +19 -0
- chess-frontend/components/ui/label.tsx +24 -0
- chess-frontend/components/ui/separator.tsx +28 -0
- chess-frontend/eslint.config.mjs +18 -0
- chess-frontend/events/chess.ts +64 -0
- chess-frontend/hooks/use-websocket.ts +105 -0
- chess-frontend/lib/clients/baseApi.ts +59 -0
- chess-frontend/lib/clients/server.ts +72 -0
- chess-frontend/lib/constants/error-simplifier.ts +20 -0
- chess-frontend/lib/constants/get-move-duration.ts +29 -0
- chess-frontend/lib/service/auth.ts +59 -0
- chess-frontend/lib/utils.ts +6 -0
- chess-frontend/next.config.ts +7 -0
- chess-frontend/package.json +38 -0
- chess-frontend/postcss.config.mjs +7 -0
- chess-frontend/proxy.ts +94 -0
- chess-frontend/store/socket-provider.tsx +44 -0
- chess-frontend/store/use-game-store.ts +110 -0
- chess-frontend/tsconfig.json +34 -0
- chess-frontend/types/auth.ts +54 -0
- chess-frontend/types/chess.ts +123 -0
- chess-monolith +1 -1
chess-frontend/.gitignore
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
| 2 |
+
|
| 3 |
+
# dependencies
|
| 4 |
+
/node_modules
|
| 5 |
+
/.pnp
|
| 6 |
+
.pnp.*
|
| 7 |
+
.yarn/*
|
| 8 |
+
!.yarn/patches
|
| 9 |
+
!.yarn/plugins
|
| 10 |
+
!.yarn/releases
|
| 11 |
+
!.yarn/versions
|
| 12 |
+
|
| 13 |
+
# testing
|
| 14 |
+
/coverage
|
| 15 |
+
|
| 16 |
+
# next.js
|
| 17 |
+
/.next/
|
| 18 |
+
/out/
|
| 19 |
+
|
| 20 |
+
# production
|
| 21 |
+
/build
|
| 22 |
+
|
| 23 |
+
# misc
|
| 24 |
+
.DS_Store
|
| 25 |
+
*.pem
|
| 26 |
+
|
| 27 |
+
# debug
|
| 28 |
+
npm-debug.log*
|
| 29 |
+
yarn-debug.log*
|
| 30 |
+
yarn-error.log*
|
| 31 |
+
.pnpm-debug.log*
|
| 32 |
+
|
| 33 |
+
# env files (can opt-in for committing if needed)
|
| 34 |
+
.env*
|
| 35 |
+
|
| 36 |
+
# vercel
|
| 37 |
+
.vercel
|
| 38 |
+
|
| 39 |
+
# typescript
|
| 40 |
+
*.tsbuildinfo
|
| 41 |
+
next-env.d.ts
|
chess-frontend/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app).
|
| 2 |
+
|
| 3 |
+
## Getting Started
|
| 4 |
+
|
| 5 |
+
First, run the development server:
|
| 6 |
+
|
| 7 |
+
```bash
|
| 8 |
+
npm run dev
|
| 9 |
+
# or
|
| 10 |
+
yarn dev
|
| 11 |
+
# or
|
| 12 |
+
pnpm dev
|
| 13 |
+
# or
|
| 14 |
+
bun dev
|
| 15 |
+
```
|
| 16 |
+
|
| 17 |
+
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
|
| 18 |
+
|
| 19 |
+
You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.
|
| 20 |
+
|
| 21 |
+
This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel.
|
| 22 |
+
|
| 23 |
+
## Learn More
|
| 24 |
+
|
| 25 |
+
To learn more about Next.js, take a look at the following resources:
|
| 26 |
+
|
| 27 |
+
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
|
| 28 |
+
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
|
| 29 |
+
|
| 30 |
+
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js) - your feedback and contributions are welcome!
|
| 31 |
+
|
| 32 |
+
## Deploy on Vercel
|
| 33 |
+
|
| 34 |
+
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
|
| 35 |
+
|
| 36 |
+
Check out our [Next.js deployment documentation](https://nextjs.org/docs/app/building-your-application/deploying) for more details.
|
chess-frontend/actions/auth.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"use server";
|
| 2 |
+
|
| 3 |
+
import { api } from "@/lib/clients/server";
|
| 4 |
+
import {
|
| 5 |
+
signIn,
|
| 6 |
+
} from "@/lib/service/auth";
|
| 7 |
+
|
| 8 |
+
import { cookies } from "next/headers";
|
| 9 |
+
import { redirect } from "next/navigation";
|
| 10 |
+
import z from "zod";
|
| 11 |
+
import { LoginInput, loginSchema } from "@/types/auth";
|
| 12 |
+
import { simplifyZodErrors } from "@/lib/constants/error-simplifier";
|
| 13 |
+
|
| 14 |
+
export async function login(redirectTo: string, _: any, formData: FormData) {
|
| 15 |
+
const data = Object.fromEntries(formData) as LoginInput;
|
| 16 |
+
const result = loginSchema.safeParse(data);
|
| 17 |
+
|
| 18 |
+
if (!result.success) {
|
| 19 |
+
const formattedErrors = z.treeifyError(result.error);
|
| 20 |
+
|
| 21 |
+
return {
|
| 22 |
+
success: false,
|
| 23 |
+
message: "Validation failed",
|
| 24 |
+
errors: simplifyZodErrors(formattedErrors),
|
| 25 |
+
inputs: {
|
| 26 |
+
email: data.email,
|
| 27 |
+
password: "",
|
| 28 |
+
},
|
| 29 |
+
};
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
try {
|
| 33 |
+
await signIn(result.data);
|
| 34 |
+
} catch (error: any) {
|
| 35 |
+
if (error instanceof Error) {
|
| 36 |
+
return {
|
| 37 |
+
success: false,
|
| 38 |
+
message: error.message,
|
| 39 |
+
errors: {
|
| 40 |
+
email: "",
|
| 41 |
+
password: "",
|
| 42 |
+
},
|
| 43 |
+
inputs: {
|
| 44 |
+
email: data.email,
|
| 45 |
+
password: "",
|
| 46 |
+
},
|
| 47 |
+
};
|
| 48 |
+
}
|
| 49 |
+
return {
|
| 50 |
+
success: false,
|
| 51 |
+
message: error.errors[0].message as string,
|
| 52 |
+
errors: {
|
| 53 |
+
email: "",
|
| 54 |
+
password: "",
|
| 55 |
+
},
|
| 56 |
+
inputs: {
|
| 57 |
+
email: data.email,
|
| 58 |
+
password: "",
|
| 59 |
+
},
|
| 60 |
+
};
|
| 61 |
+
}
|
| 62 |
+
redirect(redirectTo);
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
export async function logout() {
|
| 67 |
+
try {
|
| 68 |
+
await api("/auth/sign-out", {
|
| 69 |
+
method: "POST",
|
| 70 |
+
body: {},
|
| 71 |
+
headers: {
|
| 72 |
+
Origin: process.env.NEXT_PUBLIC_ORIGIN_URL || "http://localhost:3000",
|
| 73 |
+
},
|
| 74 |
+
});
|
| 75 |
+
} catch (error) { }
|
| 76 |
+
const cookieStore = await cookies();
|
| 77 |
+
cookieStore.getAll().forEach((cookie) => {
|
| 78 |
+
cookieStore.delete(cookie.name);
|
| 79 |
+
});
|
| 80 |
+
redirect("/login");
|
| 81 |
+
}
|
chess-frontend/actions/session.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { User } from "@/types/auth";
|
| 2 |
+
import { cookies } from "next/headers";
|
| 3 |
+
|
| 4 |
+
export async function getTokenFromSession(): Promise<string | null> {
|
| 5 |
+
const cookieStore = await cookies();
|
| 6 |
+
const token = cookieStore.get("better-auth.session_token");
|
| 7 |
+
if (!token) return null;
|
| 8 |
+
|
| 9 |
+
return token.value;
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
export async function getUserFromSession(): Promise<User | null> {
|
| 13 |
+
const cookieStore = await cookies();
|
| 14 |
+
const sessionData = cookieStore.get("better-auth.session_data");
|
| 15 |
+
const sessionToken = cookieStore.get("better-auth.session_token");
|
| 16 |
+
|
| 17 |
+
if (!sessionData || !sessionToken) {
|
| 18 |
+
return null;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
try {
|
| 22 |
+
const jsonString = Buffer.from(sessionData.value, "base64").toString(
|
| 23 |
+
"utf-8",
|
| 24 |
+
);
|
| 25 |
+
const data = JSON.parse(jsonString);
|
| 26 |
+
|
| 27 |
+
const expiresAt = new Date(data.session.expiresAt);
|
| 28 |
+
if (new Date() > expiresAt) {
|
| 29 |
+
throw new Error("Session expired");
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
return data.session.user;
|
| 33 |
+
} catch (error) {
|
| 34 |
+
cookieStore.delete("better-auth.session_data");
|
| 35 |
+
cookieStore.delete("better-auth.session_token");
|
| 36 |
+
return null;
|
| 37 |
+
}
|
| 38 |
+
}
|
chess-frontend/app/(auth)/layout.tsx
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type { Metadata } from "next";
|
| 2 |
+
|
| 3 |
+
export const metadata: Metadata = {
|
| 4 |
+
title: "Chess",
|
| 5 |
+
description: "Sign in or create an account",
|
| 6 |
+
};
|
| 7 |
+
|
| 8 |
+
export default async function AuthLayout({
|
| 9 |
+
children,
|
| 10 |
+
}: {
|
| 11 |
+
children: React.ReactNode;
|
| 12 |
+
}) {
|
| 13 |
+
return (
|
| 14 |
+
<div className="min-h-lvh bg-linear-to-br from-blue-500 via-blue-700 to-indigo-800 flex items-center justify-center p-4">
|
| 15 |
+
<div className="w-full max-w-xl">{children}</div>
|
| 16 |
+
</div>
|
| 17 |
+
);
|
| 18 |
+
}
|
chess-frontend/app/(auth)/login/_components/login-form.tsx
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"use client";
|
| 2 |
+
|
| 3 |
+
import { useActionState } from "react";
|
| 4 |
+
import Link from "next/link";
|
| 5 |
+
import { useSearchParams } from "next/navigation";
|
| 6 |
+
|
| 7 |
+
import { login } from "@/actions/auth";
|
| 8 |
+
import { Field, FieldLabel, FieldError } from "@/components/ui/field";
|
| 9 |
+
import { Input } from "@/components/ui/input";
|
| 10 |
+
import { Loader2 } from "lucide-react";
|
| 11 |
+
|
| 12 |
+
const initialState = {
|
| 13 |
+
success: false,
|
| 14 |
+
message: "",
|
| 15 |
+
errors: {
|
| 16 |
+
email: "",
|
| 17 |
+
password: "",
|
| 18 |
+
},
|
| 19 |
+
inputs: {
|
| 20 |
+
email: "",
|
| 21 |
+
password: "",
|
| 22 |
+
},
|
| 23 |
+
};
|
| 24 |
+
|
| 25 |
+
export const LoginForm = () => {
|
| 26 |
+
const searchParams = useSearchParams();
|
| 27 |
+
const redirectTo = searchParams.get("redirect") ?? "/";
|
| 28 |
+
|
| 29 |
+
const [state, action, pending] = useActionState(
|
| 30 |
+
login.bind(null, redirectTo),
|
| 31 |
+
initialState,
|
| 32 |
+
);
|
| 33 |
+
|
| 34 |
+
return (
|
| 35 |
+
<form action={action} className="space-y-6">
|
| 36 |
+
{state.message && (
|
| 37 |
+
<div
|
| 38 |
+
className={`p-4 rounded-md text-sm ${state.success
|
| 39 |
+
? "bg-green-50 text-green-700"
|
| 40 |
+
: "bg-red-50 text-red-700"
|
| 41 |
+
}`}
|
| 42 |
+
>
|
| 43 |
+
{state.message}
|
| 44 |
+
</div>
|
| 45 |
+
)}
|
| 46 |
+
|
| 47 |
+
<Field className="gap-0">
|
| 48 |
+
<FieldLabel className="mb-1" htmlFor="email">
|
| 49 |
+
Email address
|
| 50 |
+
</FieldLabel>
|
| 51 |
+
<Input
|
| 52 |
+
id="email"
|
| 53 |
+
name="email"
|
| 54 |
+
type="email"
|
| 55 |
+
autoComplete="email"
|
| 56 |
+
disabled={pending}
|
| 57 |
+
defaultValue={state.inputs.email}
|
| 58 |
+
placeholder="you@example.com"
|
| 59 |
+
/>
|
| 60 |
+
<FieldError className="my-1 text-xs">{state.errors.email}</FieldError>
|
| 61 |
+
</Field>
|
| 62 |
+
|
| 63 |
+
<Field className="gap-0">
|
| 64 |
+
<div className="flex items-center justify-between mb-1">
|
| 65 |
+
<FieldLabel htmlFor="password">Password</FieldLabel>
|
| 66 |
+
<Link
|
| 67 |
+
href="/forgot-password"
|
| 68 |
+
className="text-sm text-blue-500 hover:text-blue-700 transition-colors"
|
| 69 |
+
>
|
| 70 |
+
Forgot password?
|
| 71 |
+
</Link>
|
| 72 |
+
</div>
|
| 73 |
+
<Input
|
| 74 |
+
id="password"
|
| 75 |
+
name="password"
|
| 76 |
+
type="password"
|
| 77 |
+
disabled={pending}
|
| 78 |
+
defaultValue={state.inputs.password}
|
| 79 |
+
placeholder="•••••••••••••••••"
|
| 80 |
+
/>
|
| 81 |
+
<FieldError className="my-1 text-xs">
|
| 82 |
+
{state.errors.password}
|
| 83 |
+
</FieldError>
|
| 84 |
+
</Field>
|
| 85 |
+
|
| 86 |
+
<button
|
| 87 |
+
type="submit"
|
| 88 |
+
disabled={pending}
|
| 89 |
+
className="w-full bg-blue-500 hover:bg-blue-700 text-white font-semibold py-3 px-4 rounded-lg transition-all duration-200 disabled:opacity-50 disabled:cursor-not-allowed flex items-center justify-center"
|
| 90 |
+
>
|
| 91 |
+
{pending ? (
|
| 92 |
+
<>
|
| 93 |
+
<Loader2 className="size-5 animate-spin mr-2" />
|
| 94 |
+
|
| 95 |
+
Signing in…
|
| 96 |
+
</>
|
| 97 |
+
) : (
|
| 98 |
+
"Sign In"
|
| 99 |
+
)}
|
| 100 |
+
</button>
|
| 101 |
+
</form>
|
| 102 |
+
);
|
| 103 |
+
};
|
chess-frontend/app/(auth)/login/page.tsx
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import Link from "next/link";
|
| 2 |
+
import { LoginForm } from "./_components/login-form";
|
| 3 |
+
import { Suspense } from "react";
|
| 4 |
+
|
| 5 |
+
export default function LoginPage() {
|
| 6 |
+
return (
|
| 7 |
+
<div className="bg-white rounded-2xl shadow-2xl p-8">
|
| 8 |
+
<div className="text-center mb-8">
|
| 9 |
+
<h1 className="text-3xl font-bold text-gray-900 mb-2">Welcome Back</h1>
|
| 10 |
+
<p className="text-gray-500">Sign in to your account to continue</p>
|
| 11 |
+
</div>
|
| 12 |
+
<Suspense fallback={<div>Loading...</div>}>
|
| 13 |
+
<LoginForm />
|
| 14 |
+
</Suspense>
|
| 15 |
+
|
| 16 |
+
<div className="relative my-6">
|
| 17 |
+
<div className="absolute inset-0 flex items-center">
|
| 18 |
+
<div className="w-full border-t border-gray-300"></div>
|
| 19 |
+
</div>
|
| 20 |
+
<div className="relative flex justify-center text-sm">
|
| 21 |
+
<span className="px-2 bg-white text-gray-500">
|
| 22 |
+
Don't have an account?
|
| 23 |
+
</span>
|
| 24 |
+
</div>
|
| 25 |
+
</div>
|
| 26 |
+
|
| 27 |
+
<div className="text-center">
|
| 28 |
+
<Link
|
| 29 |
+
href="/sign-up"
|
| 30 |
+
className="text-blue-500 hover:text-blue-700 font-semibold transition-colors duration-200"
|
| 31 |
+
>
|
| 32 |
+
Create an account
|
| 33 |
+
</Link>
|
| 34 |
+
</div>
|
| 35 |
+
</div>
|
| 36 |
+
);
|
| 37 |
+
}
|
chess-frontend/app/favicon.ico
ADDED
|
|
chess-frontend/app/game/[id]/_components/clock.tsx
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"use client";
|
| 2 |
+
import { useEffect, useState } from "react";
|
| 3 |
+
|
| 4 |
+
export function Clock({ timeMs, isRunning }: { timeMs: number; isRunning: boolean }) {
|
| 5 |
+
const [displayMs, setDisplayMs] = useState(timeMs);
|
| 6 |
+
|
| 7 |
+
// Snap to server-authoritative time on every move update
|
| 8 |
+
useEffect(() => { setDisplayMs(timeMs); }, [timeMs]);
|
| 9 |
+
|
| 10 |
+
// Local tick
|
| 11 |
+
useEffect(() => {
|
| 12 |
+
if (!isRunning) return;
|
| 13 |
+
const id = setInterval(() => setDisplayMs((p) => Math.max(0, p - 100)), 100);
|
| 14 |
+
return () => clearInterval(id);
|
| 15 |
+
}, [isRunning]);
|
| 16 |
+
|
| 17 |
+
const totalSec = Math.ceil(displayMs / 1000);
|
| 18 |
+
const m = Math.floor(totalSec / 60);
|
| 19 |
+
const s = totalSec % 60;
|
| 20 |
+
const isCritical = totalSec <= 10;
|
| 21 |
+
const isLow = totalSec <= 30;
|
| 22 |
+
|
| 23 |
+
return (
|
| 24 |
+
<div
|
| 25 |
+
className={`font-mono text-base font-bold tabular-nums px-2.5 py-1 rounded-md transition-colors ${isCritical
|
| 26 |
+
? "bg-red-900/50 text-red-300 animate-pulse"
|
| 27 |
+
: isLow
|
| 28 |
+
? "bg-orange-900/30 text-orange-300"
|
| 29 |
+
: "bg-zinc-800 text-zinc-100"
|
| 30 |
+
}`}
|
| 31 |
+
>
|
| 32 |
+
{String(m).padStart(2, "0")}:{String(s).padStart(2, "0")}
|
| 33 |
+
</div>
|
| 34 |
+
);
|
| 35 |
+
}
|
chess-frontend/app/game/[id]/_components/connection-status.tsx
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"use client";
|
| 2 |
+
import { useGameStore } from "@/store/use-game-store";
|
| 3 |
+
|
| 4 |
+
export function ConnectionStatus() {
|
| 5 |
+
const status = useGameStore((s) => s.connectionStatus);
|
| 6 |
+
|
| 7 |
+
const statusColors = {
|
| 8 |
+
idle: "bg-zinc-500",
|
| 9 |
+
connecting: "bg-yellow-500 animate-pulse",
|
| 10 |
+
connected: "bg-green-500 shadow-[0_0_8px_rgba(34,197,94,0.6)]",
|
| 11 |
+
disconnected: "bg-red-500",
|
| 12 |
+
error: "bg-red-600",
|
| 13 |
+
};
|
| 14 |
+
|
| 15 |
+
return (
|
| 16 |
+
<div className="absolute top-14 right-4 flex items-center gap-2 bg-zinc-900/80 px-3 py-1.5 rounded-full border border-zinc-800 backdrop-blur-sm z-50">
|
| 17 |
+
<div className={`w-2 h-2 rounded-full ${statusColors[status]}`} />
|
| 18 |
+
<span className="text-xs font-mono text-zinc-300 uppercase tracking-wider">
|
| 19 |
+
{status}
|
| 20 |
+
</span>
|
| 21 |
+
</div>
|
| 22 |
+
);
|
| 23 |
+
}
|
chess-frontend/app/game/[id]/_components/game-over-overlay.tsx
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"use client";
|
| 2 |
+
import { useRouter } from "next/navigation";
|
| 3 |
+
import { useGameStore } from "@/store/use-game-store";
|
| 4 |
+
import { GameOverState, GameStatus } from "@/types/chess";
|
| 5 |
+
|
| 6 |
+
function getResult(gameOver: GameOverState, userId: string) {
|
| 7 |
+
if (gameOver.status === GameStatus.DRAW)
|
| 8 |
+
return { emoji: "🤝", headline: "Draw", color: "text-yellow-400" };
|
| 9 |
+
const won = gameOver.winnerId === userId;
|
| 10 |
+
return {
|
| 11 |
+
emoji: won ? "🏆" : "💀",
|
| 12 |
+
headline: won ? "You Won!" : "You Lost",
|
| 13 |
+
color: won ? "text-green-400" : "text-red-400",
|
| 14 |
+
};
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
export function GameOverOverlay({ gameOver, userId }: { gameOver: GameOverState; userId: string }) {
|
| 18 |
+
const router = useRouter();
|
| 19 |
+
const resetGame = useGameStore((s) => s.resetGame);
|
| 20 |
+
const { emoji, headline, color } = getResult(gameOver, userId);
|
| 21 |
+
|
| 22 |
+
return (
|
| 23 |
+
<div className="absolute inset-0 bg-black/65 backdrop-blur-[2px] flex items-center justify-center rounded-sm z-10">
|
| 24 |
+
<div className="bg-zinc-900 border border-zinc-700 rounded-2xl px-10 py-8 flex flex-col items-center gap-3 shadow-2xl text-center">
|
| 25 |
+
<span className="text-5xl">{emoji}</span>
|
| 26 |
+
<h2 className={`text-3xl font-bold ${color}`}>{headline}</h2>
|
| 27 |
+
{gameOver.reason && (
|
| 28 |
+
<p className="text-zinc-500 text-sm capitalize">{gameOver.reason}</p>
|
| 29 |
+
)}
|
| 30 |
+
<button
|
| 31 |
+
onClick={() => { resetGame(); router.push("/game"); }}
|
| 32 |
+
className="mt-3 px-7 py-2.5 bg-green-700 hover:bg-green-600 text-white font-semibold rounded-lg transition-colors text-sm"
|
| 33 |
+
>
|
| 34 |
+
New Game
|
| 35 |
+
</button>
|
| 36 |
+
</div>
|
| 37 |
+
</div>
|
| 38 |
+
);
|
| 39 |
+
}
|
chess-frontend/app/game/[id]/_components/gameboard.tsx
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"use client";
|
| 2 |
+
import { useGameStore } from "@/store/use-game-store";
|
| 3 |
+
import { Chessboard } from "react-chessboard";
|
| 4 |
+
import { User } from "@/types/auth";
|
| 5 |
+
|
| 6 |
+
import { GameStatus } from "@/types/chess";
|
| 7 |
+
import { useSocket } from "@/store/socket-provider";
|
| 8 |
+
import { PlayerCard } from "./player-card";
|
| 9 |
+
import { GameOverOverlay } from "./game-over-overlay";
|
| 10 |
+
import { MoveList } from "./move-list";
|
| 11 |
+
import { LobbyClient } from "@/components/game/lobby-client";
|
| 12 |
+
|
| 13 |
+
interface GameboardProps {
|
| 14 |
+
gameId: string;
|
| 15 |
+
user: User;
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
export function Gameboard({ gameId, user }: GameboardProps) {
|
| 19 |
+
const game = useGameStore((s) => s.activeGame);
|
| 20 |
+
const gameOver = useGameStore((s) => s.gameOver);
|
| 21 |
+
const showAnimations = useGameStore((s) => s.showAnimations);
|
| 22 |
+
const rejectedReason = useGameStore((s) => s.lastMoveRejectedReason);
|
| 23 |
+
const { makeMove, resign } = useSocket();
|
| 24 |
+
|
| 25 |
+
const isWhite = game?.playerColor === "w";
|
| 26 |
+
|
| 27 |
+
const topColor = isWhite ? "b" : "w";
|
| 28 |
+
const bottomColor = isWhite ? "w" : "b";
|
| 29 |
+
const topId = topColor === "w" ? game?.whiteId : game?.blackId;
|
| 30 |
+
const bottomId = bottomColor === "w" ? game?.whiteId : game?.blackId;
|
| 31 |
+
|
| 32 |
+
if (!game || game.gameId !== gameId) {
|
| 33 |
+
return (
|
| 34 |
+
<LobbyClient user={user} />
|
| 35 |
+
);
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
return (
|
| 39 |
+
<div className="min-h-screen flex items-center justify-center p-4">
|
| 40 |
+
<div className="flex gap-4 items-start w-full max-w-5xl">
|
| 41 |
+
|
| 42 |
+
<div className="flex flex-col gap-2 min-w-0 flex-1 max-w-[560px]">
|
| 43 |
+
<PlayerCard
|
| 44 |
+
userId={topId ?? "?"}
|
| 45 |
+
color={topColor}
|
| 46 |
+
timeMs={topColor === "w" ? game.whiteTimeMs : game.blackTimeMs}
|
| 47 |
+
isActive={game.turn === topColor && game.status === GameStatus.IN_PROGRESS}
|
| 48 |
+
/>
|
| 49 |
+
|
| 50 |
+
<div className="relative">
|
| 51 |
+
<Chessboard
|
| 52 |
+
options={{
|
| 53 |
+
position: game.fen,
|
| 54 |
+
showAnimations: true,
|
| 55 |
+
boardOrientation: isWhite ? "white" : "black",
|
| 56 |
+
onPieceDrop: ({ sourceSquare, targetSquare }) => {
|
| 57 |
+
if (!targetSquare) return false;
|
| 58 |
+
makeMove(game.gameId, sourceSquare, targetSquare);
|
| 59 |
+
return true;
|
| 60 |
+
},
|
| 61 |
+
darkSquareStyle: { backgroundColor: "#4a7c59" },
|
| 62 |
+
lightSquareStyle: { backgroundColor: "#f0d9b5" },
|
| 63 |
+
}}
|
| 64 |
+
/>
|
| 65 |
+
|
| 66 |
+
{rejectedReason && (
|
| 67 |
+
<div className="absolute top-2 left-1/2 -translate-x-1/2 bg-red-600/90 text-white text-xs px-3 py-1.5 rounded-full whitespace-nowrap">
|
| 68 |
+
{rejectedReason}
|
| 69 |
+
</div>
|
| 70 |
+
)}
|
| 71 |
+
|
| 72 |
+
{gameOver && (
|
| 73 |
+
<GameOverOverlay gameOver={gameOver} userId={user.id} />
|
| 74 |
+
)}
|
| 75 |
+
</div>
|
| 76 |
+
|
| 77 |
+
<PlayerCard
|
| 78 |
+
userId={bottomId ?? user.username}
|
| 79 |
+
color={bottomColor}
|
| 80 |
+
timeMs={bottomColor === "w" ? game.whiteTimeMs : game.blackTimeMs}
|
| 81 |
+
isActive={game.turn === bottomColor && game.status === GameStatus.IN_PROGRESS}
|
| 82 |
+
/>
|
| 83 |
+
</div>
|
| 84 |
+
|
| 85 |
+
<div className="flex flex-col gap-3 w-60 flex-shrink-0">
|
| 86 |
+
<MoveList pgn={game.pgn} />
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
{!gameOver && (
|
| 91 |
+
<button
|
| 92 |
+
onClick={() => resign(game.gameId)}
|
| 93 |
+
className="w-full py-2.5 rounded-lg bg-zinc-900 hover:bg-red-950/60 border border-zinc-900 hover:border-red-800 text-zinc-400 hover:text-red-400 text-sm font-medium transition-all duration-150"
|
| 94 |
+
>
|
| 95 |
+
Resign
|
| 96 |
+
</button>
|
| 97 |
+
)}
|
| 98 |
+
</div>
|
| 99 |
+
</div>
|
| 100 |
+
</div>
|
| 101 |
+
);
|
| 102 |
+
}
|
chess-frontend/app/game/[id]/_components/move-list.tsx
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { useEffect, useRef } from "react";
|
| 2 |
+
import { Chess } from "chess.js";
|
| 3 |
+
|
| 4 |
+
interface MoveListProps {
|
| 5 |
+
pgn: string;
|
| 6 |
+
}
|
| 7 |
+
|
| 8 |
+
interface MoveDetail {
|
| 9 |
+
san: string;
|
| 10 |
+
timeSpent?: string;
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
interface MovePair {
|
| 14 |
+
number: number;
|
| 15 |
+
white: MoveDetail;
|
| 16 |
+
black?: MoveDetail;
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
function clkToSeconds(clk: string): number {
|
| 20 |
+
const match = clk.match(/(\d+):(\d{2}):(\d{2})/);
|
| 21 |
+
if (!match) return 0;
|
| 22 |
+
return parseInt(match[1]) * 3600 + parseInt(match[2]) * 60 + parseInt(match[3]);
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
function parsePgnWithTimes(pgn: string): MovePair[] {
|
| 26 |
+
if (!pgn) return [];
|
| 27 |
+
try {
|
| 28 |
+
const chess = new Chess();
|
| 29 |
+
chess.loadPgn(pgn);
|
| 30 |
+
const history = chess.history();
|
| 31 |
+
|
| 32 |
+
const clkMatches = [...pgn.matchAll(/\[%clk (\d+:\d{2}:\d{2})\]/g)].map(m => clkToSeconds(m[1]));
|
| 33 |
+
|
| 34 |
+
const pairs: MovePair[] = [];
|
| 35 |
+
let lastWhiteClk = 300;
|
| 36 |
+
let lastBlackClk = 300;
|
| 37 |
+
|
| 38 |
+
for (let i = 0; i < history.length; i += 2) {
|
| 39 |
+
const whiteClk = clkMatches[i];
|
| 40 |
+
const blackClk = clkMatches[i + 1];
|
| 41 |
+
|
| 42 |
+
const whiteDiff = whiteClk ? lastWhiteClk - whiteClk : 0;
|
| 43 |
+
const blackDiff = blackClk ? lastBlackClk - blackClk : 0;
|
| 44 |
+
|
| 45 |
+
pairs.push({
|
| 46 |
+
number: Math.floor(i / 2) + 1,
|
| 47 |
+
white: {
|
| 48 |
+
san: history[i],
|
| 49 |
+
timeSpent: whiteDiff > 0 ? `${whiteDiff}s` : undefined
|
| 50 |
+
},
|
| 51 |
+
black: history[i + 1] ? {
|
| 52 |
+
san: history[i + 1],
|
| 53 |
+
timeSpent: blackDiff > 0 ? `${blackDiff}s` : undefined
|
| 54 |
+
} : undefined,
|
| 55 |
+
});
|
| 56 |
+
|
| 57 |
+
lastWhiteClk = whiteClk || lastWhiteClk;
|
| 58 |
+
lastBlackClk = blackClk || lastBlackClk;
|
| 59 |
+
}
|
| 60 |
+
return pairs;
|
| 61 |
+
} catch {
|
| 62 |
+
return [];
|
| 63 |
+
}
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
export function MoveList({ pgn }: MoveListProps) {
|
| 67 |
+
const bottomRef = useRef<HTMLDivElement>(null);
|
| 68 |
+
const pairs = parsePgnWithTimes(pgn);
|
| 69 |
+
|
| 70 |
+
useEffect(() => {
|
| 71 |
+
bottomRef.current?.scrollIntoView({ behavior: "smooth" });
|
| 72 |
+
}, [pairs.length]);
|
| 73 |
+
|
| 74 |
+
return (
|
| 75 |
+
<div className="flex flex-col h-full">
|
| 76 |
+
<p className="text-zinc-500 text-[10px] font-bold tracking-widest uppercase px-1 pb-2">
|
| 77 |
+
Move History
|
| 78 |
+
</p>
|
| 79 |
+
<div className="flex-1 overflow-y-auto pr-1 space-y-0.5 scrollbar-thin bg-gray-900 rounded-md">
|
| 80 |
+
{pairs.length === 0 ? (
|
| 81 |
+
<p className="text-zinc-700 text-xs italic px-1 pt-1">Waiting for first move...</p>
|
| 82 |
+
) : (
|
| 83 |
+
pairs.map((pair) => (
|
| 84 |
+
<div key={pair.number} className="flex items-center text-sm group">
|
| 85 |
+
<span className="w-8 text-zinc-200 text-[10px] tabular-nums text-right pr-2">
|
| 86 |
+
{pair.number}.
|
| 87 |
+
</span>
|
| 88 |
+
|
| 89 |
+
{/* White Move */}
|
| 90 |
+
<div className="flex-1 flex justify-between items-center px-2 py-1 hover:bg-zinc-800/50 rounded transition-colors group/m">
|
| 91 |
+
<span className="text-zinc-200 font-medium">{pair.white.san}</span>
|
| 92 |
+
{pair.white.timeSpent && (
|
| 93 |
+
<span className="text-[9px] text-zinc-600 font-mono opacity-0 group-hover/m:opacity-100 transition-opacity">
|
| 94 |
+
{pair.white.timeSpent}
|
| 95 |
+
</span>
|
| 96 |
+
)}
|
| 97 |
+
</div>
|
| 98 |
+
|
| 99 |
+
{/* Black Move */}
|
| 100 |
+
{pair.black ? (
|
| 101 |
+
<div className="flex-1 flex justify-between items-center px-2 py-1 hover:bg-zinc-800/50 rounded transition-colors group/m">
|
| 102 |
+
<span className="text-zinc-200 font-medium">{pair.black.san}</span>
|
| 103 |
+
{pair.black.timeSpent && (
|
| 104 |
+
<span className="text-[9px] text-zinc-600 font-mono opacity-0 group-hover/m:opacity-100 transition-opacity">
|
| 105 |
+
{pair.black.timeSpent}
|
| 106 |
+
</span>
|
| 107 |
+
)}
|
| 108 |
+
</div>
|
| 109 |
+
) : (
|
| 110 |
+
<div className="flex-1" />
|
| 111 |
+
)}
|
| 112 |
+
</div>
|
| 113 |
+
))
|
| 114 |
+
)}
|
| 115 |
+
<div ref={bottomRef} />
|
| 116 |
+
</div>
|
| 117 |
+
</div>
|
| 118 |
+
);
|
| 119 |
+
}
|
chess-frontend/app/game/[id]/_components/player-card.tsx
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"use client";
|
| 2 |
+
|
| 3 |
+
import { Clock } from "./clock";
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
interface PlayerCardProps {
|
| 7 |
+
userId: string;
|
| 8 |
+
color: "w" | "b";
|
| 9 |
+
timeMs: number;
|
| 10 |
+
isActive: boolean;
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
export function PlayerCard({ userId, color, timeMs, isActive }: PlayerCardProps) {
|
| 14 |
+
return (
|
| 15 |
+
<div
|
| 16 |
+
className={`flex items-center justify-between px-3 py-2 rounded-lg border transition-colors duration-200 ${isActive
|
| 17 |
+
? "bg-zinc-800 border-zinc-600"
|
| 18 |
+
: "bg-zinc-900/60 border-zinc-800"
|
| 19 |
+
}`}
|
| 20 |
+
>
|
| 21 |
+
<div className="flex items-center gap-2 min-w-0">
|
| 22 |
+
<div
|
| 23 |
+
className={`w-6 h-6 rounded-full border flex-shrink-0 ${color === "w"
|
| 24 |
+
? "bg-zinc-100 border-zinc-300"
|
| 25 |
+
: "bg-zinc-800 border-zinc-600"
|
| 26 |
+
}`}
|
| 27 |
+
/>
|
| 28 |
+
<span className="text-sm font-medium text-zinc-200 truncate">{userId}</span>
|
| 29 |
+
{isActive && (
|
| 30 |
+
<span className="w-1.5 h-1.5 rounded-full bg-green-400 animate-pulse flex-shrink-0" />
|
| 31 |
+
)}
|
| 32 |
+
</div>
|
| 33 |
+
<Clock timeMs={timeMs} isRunning={isActive} />
|
| 34 |
+
</div>
|
| 35 |
+
);
|
| 36 |
+
}
|
chess-frontend/app/game/[id]/page.tsx
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { getUserFromSession } from "@/actions/session";
|
| 2 |
+
import { redirect } from "next/navigation";
|
| 3 |
+
import { Gameboard } from "./_components/gameboard";
|
| 4 |
+
|
| 5 |
+
export default async function GamePage({ params }: { params: Promise<{ id: string }> }) {
|
| 6 |
+
const { id } = await params;
|
| 7 |
+
const user = await getUserFromSession();
|
| 8 |
+
if (!user) redirect("/login");
|
| 9 |
+
|
| 10 |
+
return <Gameboard gameId={id} user={user} />;
|
| 11 |
+
}
|
chess-frontend/app/game/layout.tsx
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { getUserFromSession } from "@/actions/session";
|
| 2 |
+
|
| 3 |
+
import { SocketProvider } from "@/store/socket-provider";
|
| 4 |
+
import { ConnectionStatus } from "./[id]/_components/connection-status";
|
| 5 |
+
import { SearchingModal } from "@/components/game/searching-modal";
|
| 6 |
+
import { redirect } from "next/navigation";
|
| 7 |
+
|
| 8 |
+
export default async function RootLayout({
|
| 9 |
+
children,
|
| 10 |
+
}: {
|
| 11 |
+
children: React.ReactNode;
|
| 12 |
+
}) {
|
| 13 |
+
const user = await getUserFromSession();
|
| 14 |
+
|
| 15 |
+
if (!user) redirect("/login");
|
| 16 |
+
|
| 17 |
+
return (<>
|
| 18 |
+
|
| 19 |
+
{
|
| 20 |
+
user ? (
|
| 21 |
+
<SocketProvider user={user} >
|
| 22 |
+
<ConnectionStatus />
|
| 23 |
+
<SearchingModal />
|
| 24 |
+
{children}
|
| 25 |
+
</SocketProvider>
|
| 26 |
+
) : (
|
| 27 |
+
children
|
| 28 |
+
)
|
| 29 |
+
}
|
| 30 |
+
</>
|
| 31 |
+
|
| 32 |
+
);
|
| 33 |
+
}
|
chess-frontend/app/game/page.tsx
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { getUserFromSession } from "@/actions/session";
|
| 2 |
+
import { LobbyClient } from "@/components/game/lobby-client";
|
| 3 |
+
import { redirect } from "next/navigation";
|
| 4 |
+
|
| 5 |
+
export default async function HomePage() {
|
| 6 |
+
const user = await getUserFromSession();
|
| 7 |
+
if (!user) redirect("/login");
|
| 8 |
+
return <LobbyClient user={user} />;
|
| 9 |
+
}
|
chess-frontend/app/globals.css
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
@import "tailwindcss";
|
| 2 |
+
@import "tw-animate-css";
|
| 3 |
+
@import "shadcn/tailwind.css";
|
| 4 |
+
|
| 5 |
+
@custom-variant dark (&:is(.dark *));
|
| 6 |
+
|
| 7 |
+
@theme inline {
|
| 8 |
+
--color-background: var(--background);
|
| 9 |
+
--color-foreground: var(--foreground);
|
| 10 |
+
--font-sans: var(--font-geist-sans);
|
| 11 |
+
--font-mono: var(--font-mono);
|
| 12 |
+
--color-sidebar-ring: var(--sidebar-ring);
|
| 13 |
+
--color-sidebar-border: var(--sidebar-border);
|
| 14 |
+
--color-sidebar-accent-foreground: var(--sidebar-accent-foreground);
|
| 15 |
+
--color-sidebar-accent: var(--sidebar-accent);
|
| 16 |
+
--color-sidebar-primary-foreground: var(--sidebar-primary-foreground);
|
| 17 |
+
--color-sidebar-primary: var(--sidebar-primary);
|
| 18 |
+
--color-sidebar-foreground: var(--sidebar-foreground);
|
| 19 |
+
--color-sidebar: var(--sidebar);
|
| 20 |
+
--color-chart-5: var(--chart-5);
|
| 21 |
+
--color-chart-4: var(--chart-4);
|
| 22 |
+
--color-chart-3: var(--chart-3);
|
| 23 |
+
--color-chart-2: var(--chart-2);
|
| 24 |
+
--color-chart-1: var(--chart-1);
|
| 25 |
+
--color-ring: var(--ring);
|
| 26 |
+
--color-input: var(--input);
|
| 27 |
+
--color-border: var(--border);
|
| 28 |
+
--color-destructive: var(--destructive);
|
| 29 |
+
--color-accent-foreground: var(--accent-foreground);
|
| 30 |
+
--color-accent: var(--accent);
|
| 31 |
+
--color-muted-foreground: var(--muted-foreground);
|
| 32 |
+
--color-muted: var(--muted);
|
| 33 |
+
--color-secondary-foreground: var(--secondary-foreground);
|
| 34 |
+
--color-secondary: var(--secondary);
|
| 35 |
+
--color-primary-foreground: var(--primary-foreground);
|
| 36 |
+
--color-primary: var(--primary);
|
| 37 |
+
--color-popover-foreground: var(--popover-foreground);
|
| 38 |
+
--color-popover: var(--popover);
|
| 39 |
+
--color-card-foreground: var(--card-foreground);
|
| 40 |
+
--color-card: var(--card);
|
| 41 |
+
--radius-sm: calc(var(--radius) * 0.6);
|
| 42 |
+
--radius-md: calc(var(--radius) * 0.8);
|
| 43 |
+
--radius-lg: var(--radius);
|
| 44 |
+
--radius-xl: calc(var(--radius) * 1.4);
|
| 45 |
+
--radius-2xl: calc(var(--radius) * 1.8);
|
| 46 |
+
--radius-3xl: calc(var(--radius) * 2.2);
|
| 47 |
+
--radius-4xl: calc(var(--radius) * 2.6);
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
:root {
|
| 51 |
+
--background: oklch(1 0 0);
|
| 52 |
+
--foreground: oklch(0.145 0 0);
|
| 53 |
+
--card: oklch(1 0 0);
|
| 54 |
+
--card-foreground: oklch(0.145 0 0);
|
| 55 |
+
--popover: oklch(1 0 0);
|
| 56 |
+
--popover-foreground: oklch(0.145 0 0);
|
| 57 |
+
--primary: oklch(0.205 0 0);
|
| 58 |
+
--primary-foreground: oklch(0.985 0 0);
|
| 59 |
+
--secondary: oklch(0.97 0 0);
|
| 60 |
+
--secondary-foreground: oklch(0.205 0 0);
|
| 61 |
+
--muted: oklch(0.97 0 0);
|
| 62 |
+
--muted-foreground: oklch(0.556 0 0);
|
| 63 |
+
--accent: oklch(0.97 0 0);
|
| 64 |
+
--accent-foreground: oklch(0.205 0 0);
|
| 65 |
+
--destructive: oklch(0.577 0.245 27.325);
|
| 66 |
+
--border: oklch(0.922 0 0);
|
| 67 |
+
--input: oklch(0.922 0 0);
|
| 68 |
+
--ring: oklch(0.708 0 0);
|
| 69 |
+
--chart-1: oklch(0.809 0.105 251.813);
|
| 70 |
+
--chart-2: oklch(0.623 0.214 259.815);
|
| 71 |
+
--chart-3: oklch(0.546 0.245 262.881);
|
| 72 |
+
--chart-4: oklch(0.488 0.243 264.376);
|
| 73 |
+
--chart-5: oklch(0.424 0.199 265.638);
|
| 74 |
+
--radius: 0.625rem;
|
| 75 |
+
--sidebar: oklch(0.985 0 0);
|
| 76 |
+
--sidebar-foreground: oklch(0.145 0 0);
|
| 77 |
+
--sidebar-primary: oklch(0.205 0 0);
|
| 78 |
+
--sidebar-primary-foreground: oklch(0.985 0 0);
|
| 79 |
+
--sidebar-accent: oklch(0.97 0 0);
|
| 80 |
+
--sidebar-accent-foreground: oklch(0.205 0 0);
|
| 81 |
+
--sidebar-border: oklch(0.922 0 0);
|
| 82 |
+
--sidebar-ring: oklch(0.708 0 0);
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
.dark {
|
| 86 |
+
--background: oklch(0.145 0 0);
|
| 87 |
+
--foreground: oklch(0.985 0 0);
|
| 88 |
+
--card: oklch(0.205 0 0);
|
| 89 |
+
--card-foreground: oklch(0.985 0 0);
|
| 90 |
+
--popover: oklch(0.205 0 0);
|
| 91 |
+
--popover-foreground: oklch(0.985 0 0);
|
| 92 |
+
--primary: oklch(0.922 0 0);
|
| 93 |
+
--primary-foreground: oklch(0.205 0 0);
|
| 94 |
+
--secondary: oklch(0.269 0 0);
|
| 95 |
+
--secondary-foreground: oklch(0.985 0 0);
|
| 96 |
+
--muted: oklch(0.269 0 0);
|
| 97 |
+
--muted-foreground: oklch(0.708 0 0);
|
| 98 |
+
--accent: oklch(0.269 0 0);
|
| 99 |
+
--accent-foreground: oklch(0.985 0 0);
|
| 100 |
+
--destructive: oklch(0.704 0.191 22.216);
|
| 101 |
+
--border: oklch(1 0 0 / 10%);
|
| 102 |
+
--input: oklch(1 0 0 / 15%);
|
| 103 |
+
--ring: oklch(0.556 0 0);
|
| 104 |
+
--chart-1: oklch(0.809 0.105 251.813);
|
| 105 |
+
--chart-2: oklch(0.623 0.214 259.815);
|
| 106 |
+
--chart-3: oklch(0.546 0.245 262.881);
|
| 107 |
+
--chart-4: oklch(0.488 0.243 264.376);
|
| 108 |
+
--chart-5: oklch(0.424 0.199 265.638);
|
| 109 |
+
--sidebar: oklch(0.205 0 0);
|
| 110 |
+
--sidebar-foreground: oklch(0.985 0 0);
|
| 111 |
+
--sidebar-primary: oklch(0.488 0.243 264.376);
|
| 112 |
+
--sidebar-primary-foreground: oklch(0.985 0 0);
|
| 113 |
+
--sidebar-accent: oklch(0.269 0 0);
|
| 114 |
+
--sidebar-accent-foreground: oklch(0.985 0 0);
|
| 115 |
+
--sidebar-border: oklch(1 0 0 / 10%);
|
| 116 |
+
--sidebar-ring: oklch(0.556 0 0);
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
@layer base {
|
| 120 |
+
* {
|
| 121 |
+
@apply border-border outline-ring/50;
|
| 122 |
+
}
|
| 123 |
+
body {
|
| 124 |
+
@apply bg-background text-foreground;
|
| 125 |
+
}
|
| 126 |
+
html {
|
| 127 |
+
@apply font-mono;
|
| 128 |
+
}
|
| 129 |
+
}
|
chess-frontend/app/layout.tsx
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type { Metadata } from "next";
|
| 2 |
+
import { Raleway } from "next/font/google";
|
| 3 |
+
|
| 4 |
+
import { cn } from "@/lib/utils";
|
| 5 |
+
import "./globals.css";
|
| 6 |
+
|
| 7 |
+
const raleway = Raleway({ subsets: ["latin"], variable: "--font-sans" });
|
| 8 |
+
|
| 9 |
+
export const metadata: Metadata = { title: "Chess", description: "Play chess online" };
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
export default async function RootLayout({
|
| 13 |
+
children,
|
| 14 |
+
}: {
|
| 15 |
+
children: React.ReactNode;
|
| 16 |
+
}) {
|
| 17 |
+
|
| 18 |
+
return (
|
| 19 |
+
<html lang="en" className={raleway.variable}>
|
| 20 |
+
<body className={cn("min-h-screen flex flex-col", raleway.className)}>
|
| 21 |
+
{children}
|
| 22 |
+
</body>
|
| 23 |
+
</html>
|
| 24 |
+
);
|
| 25 |
+
}
|
chess-frontend/app/page.tsx
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const Home = () => {
|
| 2 |
+
return (
|
| 3 |
+
<div>Home</div>
|
| 4 |
+
)
|
| 5 |
+
}
|
| 6 |
+
|
| 7 |
+
export default Home
|
chess-frontend/bun.lock
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
chess-frontend/components.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"$schema": "https://ui.shadcn.com/schema.json",
|
| 3 |
+
"style": "radix-lyra",
|
| 4 |
+
"rsc": true,
|
| 5 |
+
"tsx": true,
|
| 6 |
+
"tailwind": {
|
| 7 |
+
"config": "",
|
| 8 |
+
"css": "app/globals.css",
|
| 9 |
+
"baseColor": "neutral",
|
| 10 |
+
"cssVariables": true,
|
| 11 |
+
"prefix": ""
|
| 12 |
+
},
|
| 13 |
+
"iconLibrary": "phosphor",
|
| 14 |
+
"rtl": false,
|
| 15 |
+
"aliases": {
|
| 16 |
+
"components": "@/components",
|
| 17 |
+
"utils": "@/lib/utils",
|
| 18 |
+
"ui": "@/components/ui",
|
| 19 |
+
"lib": "@/lib",
|
| 20 |
+
"hooks": "@/hooks"
|
| 21 |
+
},
|
| 22 |
+
"menuColor": "default",
|
| 23 |
+
"menuAccent": "subtle",
|
| 24 |
+
"registries": {}
|
| 25 |
+
}
|
chess-frontend/components/game/game-view.tsx
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { useWebSocket } from "@/hooks/use-websocket";
|
| 2 |
+
import { useGameStore } from "@/store/use-game-store";
|
| 3 |
+
import { Chessboard } from "react-chessboard";
|
| 4 |
+
import { MoveList } from "../../app/game/[id]/_components/move-list";
|
| 5 |
+
import { useMemo } from "react";
|
| 6 |
+
import { User } from "@/types/auth";
|
| 7 |
+
|
| 8 |
+
export function GameView({ user }: { user: User }) {
|
| 9 |
+
const activeGame = useGameStore((s) => s.activeGame);
|
| 10 |
+
if (!activeGame) return;
|
| 11 |
+
const animations = useGameStore((s) => s.showAnimations);
|
| 12 |
+
|
| 13 |
+
const { makeMove } = useWebSocket(user);
|
| 14 |
+
const boardOptions = useMemo(() => ({
|
| 15 |
+
id: "main-board",
|
| 16 |
+
position: activeGame.fen,
|
| 17 |
+
|
| 18 |
+
boardStyle: {
|
| 19 |
+
width: Math.min(
|
| 20 |
+
512,
|
| 21 |
+
typeof window !== "undefined" ? window.innerWidth - 32 : 512,
|
| 22 |
+
),
|
| 23 |
+
},
|
| 24 |
+
darkSquareStyle: { backgroundColor: "#4a7c59" },
|
| 25 |
+
lightSquareStyle: { backgroundColor: "#f0d9b5" },
|
| 26 |
+
|
| 27 |
+
animationDurationInMs: 120,
|
| 28 |
+
allowDrawingArrows: true,
|
| 29 |
+
allowDragOffBoard: false,
|
| 30 |
+
}), [
|
| 31 |
+
activeGame.fen,
|
| 32 |
+
]);
|
| 33 |
+
|
| 34 |
+
return (
|
| 35 |
+
<div className="layout">
|
| 36 |
+
<Chessboard
|
| 37 |
+
options={{
|
| 38 |
+
position: activeGame.fen,
|
| 39 |
+
id: "main-board",
|
| 40 |
+
showAnimations: animations,
|
| 41 |
+
onPieceDrop: ({ sourceSquare, targetSquare }) => {
|
| 42 |
+
if (!targetSquare) return false;
|
| 43 |
+
makeMove(activeGame.gameId, sourceSquare, targetSquare);
|
| 44 |
+
return true;
|
| 45 |
+
}
|
| 46 |
+
}}
|
| 47 |
+
/>
|
| 48 |
+
<Chessboard options={boardOptions} />
|
| 49 |
+
<MoveList pgn={activeGame.pgn} />
|
| 50 |
+
</div>
|
| 51 |
+
);
|
| 52 |
+
}
|
chess-frontend/components/game/gameboard.tsx
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { Chess, Square } from "chess.js";
|
| 2 |
+
import { useRef, useState } from "react";
|
| 3 |
+
import { Chessboard, SquareHandlerArgs } from "react-chessboard";
|
| 4 |
+
|
| 5 |
+
export const GameBoard = () => {
|
| 6 |
+
|
| 7 |
+
const chessGameRef = useRef(new Chess());
|
| 8 |
+
const chessGame = chessGameRef.current;
|
| 9 |
+
|
| 10 |
+
const [chessPosition, setChessPosition] = useState(chessGame.fen());
|
| 11 |
+
const [moveFrom, setMoveFrom] = useState<string | null>(null);
|
| 12 |
+
const [optionSquares, setOptionSquares] =
|
| 13 |
+
useState<Record<string, React.CSSProperties>>({});
|
| 14 |
+
function makeRandomMove() {
|
| 15 |
+
const possibleMoves = chessGame.moves();
|
| 16 |
+
|
| 17 |
+
if (chessGame.isGameOver()) {
|
| 18 |
+
return;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
const randomMove = possibleMoves[Math.floor(Math.random() * possibleMoves.length)];
|
| 22 |
+
|
| 23 |
+
chessGame.move(randomMove);
|
| 24 |
+
|
| 25 |
+
setChessPosition(chessGame.fen());
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
function getMoveOptions(square: Square) {
|
| 29 |
+
const moves = chessGame.moves({
|
| 30 |
+
square: moveFrom as Square,
|
| 31 |
+
verbose: true
|
| 32 |
+
});
|
| 33 |
+
|
| 34 |
+
if (moves.length === 0) {
|
| 35 |
+
setOptionSquares({});
|
| 36 |
+
return false;
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
const newSquares: Record<string, React.CSSProperties> = {};
|
| 40 |
+
|
| 41 |
+
for (const move of moves) {
|
| 42 |
+
const pieceOnTarget = chessGame.get(move.to);
|
| 43 |
+
|
| 44 |
+
newSquares[move.to] = {
|
| 45 |
+
background:
|
| 46 |
+
pieceOnTarget && pieceOnTarget.color !== chessGame.get(square)?.color
|
| 47 |
+
? "radial-gradient(circle, rgba(0,0,0,.1) 85%, transparent 85%)"
|
| 48 |
+
: "radial-gradient(circle, rgba(0,0,0,.1) 25%, transparent 25%)",
|
| 49 |
+
borderRadius: "50%"
|
| 50 |
+
};
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
newSquares[square] = {
|
| 54 |
+
background: 'rgba(255, 255, 0, 0.4)'
|
| 55 |
+
};
|
| 56 |
+
|
| 57 |
+
setOptionSquares(newSquares);
|
| 58 |
+
|
| 59 |
+
return true;
|
| 60 |
+
}
|
| 61 |
+
function onSquareClick({
|
| 62 |
+
square,
|
| 63 |
+
piece
|
| 64 |
+
}: SquareHandlerArgs) {
|
| 65 |
+
if (!moveFrom && piece) {
|
| 66 |
+
const hasMoveOptions = getMoveOptions(square as Square);
|
| 67 |
+
|
| 68 |
+
if (hasMoveOptions) {
|
| 69 |
+
setMoveFrom(square as Square);
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
return;
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
const moves = chessGame.moves({
|
| 76 |
+
square: moveFrom as Square,
|
| 77 |
+
verbose: true
|
| 78 |
+
});
|
| 79 |
+
const foundMove = moves.find(m => m.from === moveFrom && m.to === square);
|
| 80 |
+
|
| 81 |
+
if (!foundMove) {
|
| 82 |
+
const hasMoveOptions = getMoveOptions(square as Square);
|
| 83 |
+
|
| 84 |
+
setMoveFrom(hasMoveOptions ? square : null);
|
| 85 |
+
|
| 86 |
+
return;
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
try {
|
| 90 |
+
chessGame.move({
|
| 91 |
+
from: moveFrom ?? "",
|
| 92 |
+
to: square,
|
| 93 |
+
promotion: 'q'
|
| 94 |
+
});
|
| 95 |
+
} catch {
|
| 96 |
+
const hasMoveOptions = getMoveOptions(square as Square);
|
| 97 |
+
|
| 98 |
+
if (hasMoveOptions) {
|
| 99 |
+
setMoveFrom(square);
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
return;
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
setChessPosition(chessGame.fen());
|
| 106 |
+
|
| 107 |
+
setTimeout(makeRandomMove, 300);
|
| 108 |
+
|
| 109 |
+
setMoveFrom(null);
|
| 110 |
+
setOptionSquares({});
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
const chessboardOptions = {
|
| 114 |
+
allowDragging: false,
|
| 115 |
+
onSquareClick,
|
| 116 |
+
position: chessPosition,
|
| 117 |
+
squareStyles: optionSquares,
|
| 118 |
+
id: 'click-to-move'
|
| 119 |
+
};
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
return (
|
| 123 |
+
<Chessboard options={chessboardOptions} />
|
| 124 |
+
)
|
| 125 |
+
}
|
chess-frontend/components/game/lobby-client.tsx
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"use client";
|
| 2 |
+
import { useSocket } from "@/store/socket-provider";
|
| 3 |
+
import { User } from "@/types/auth";
|
| 4 |
+
import { Zap, Flame, Clock, Timer } from "lucide-react";
|
| 5 |
+
|
| 6 |
+
const TIME_CONTROLS = [
|
| 7 |
+
{
|
| 8 |
+
category: "Bullet",
|
| 9 |
+
icon: Zap,
|
| 10 |
+
color: "text-yellow-400",
|
| 11 |
+
border: "border-yellow-900/50",
|
| 12 |
+
options: [{ label: "1+0", value: "1+0" }, { label: "2+1", value: "2+1" }, { label: "3+0", value: "3+0" }],
|
| 13 |
+
},
|
| 14 |
+
{
|
| 15 |
+
category: "Blitz",
|
| 16 |
+
icon: Flame,
|
| 17 |
+
color: "text-orange-400",
|
| 18 |
+
border: "border-orange-900/50",
|
| 19 |
+
options: [{ label: "3+2", value: "3+2" }, { label: "5+0", value: "5+0" }, { label: "5+3", value: "5+3" }],
|
| 20 |
+
},
|
| 21 |
+
{
|
| 22 |
+
category: "Rapid",
|
| 23 |
+
icon: Clock,
|
| 24 |
+
color: "text-green-400",
|
| 25 |
+
border: "border-green-900/50",
|
| 26 |
+
options: [{ label: "10+0", value: "10+0" }, { label: "10+5", value: "10+5" }, { label: "15+10", value: "15+10" }],
|
| 27 |
+
},
|
| 28 |
+
{
|
| 29 |
+
category: "Classical",
|
| 30 |
+
icon: Timer,
|
| 31 |
+
color: "text-blue-400",
|
| 32 |
+
border: "border-blue-900/50",
|
| 33 |
+
options: [{ label: "30+0", value: "30+0" }, { label: "30+20", value: "30+20" }],
|
| 34 |
+
},
|
| 35 |
+
];
|
| 36 |
+
|
| 37 |
+
export function LobbyClient({ user }: { user: User }) {
|
| 38 |
+
const { joinQueue } = useSocket();
|
| 39 |
+
|
| 40 |
+
return (
|
| 41 |
+
<div className="min-h-screen flex flex-col">
|
| 42 |
+
<header className="border-b border-zinc-800 px-6 py-4 flex items-center justify-between">
|
| 43 |
+
<div className="flex items-center gap-2">
|
| 44 |
+
<span className="text-2xl select-none">♟</span>
|
| 45 |
+
<span className="font-bold text-lg tracking-tight">Chess</span>
|
| 46 |
+
</div>
|
| 47 |
+
<div className="text-sm text-zinc-400">
|
| 48 |
+
Playing as{" "}
|
| 49 |
+
<span className="text-white font-semibold">{user.username}</span>
|
| 50 |
+
</div>
|
| 51 |
+
</header>
|
| 52 |
+
|
| 53 |
+
<main className="flex-1 flex items-center justify-center p-8">
|
| 54 |
+
<div className="w-full max-w-3xl">
|
| 55 |
+
<h1 className="text-3xl font-bold text-center mb-1">Play Chess</h1>
|
| 56 |
+
<p className="text-zinc-500 text-center text-sm mb-10">Choose a time control to find a match</p>
|
| 57 |
+
|
| 58 |
+
<div className="grid grid-cols-2 md:grid-cols-4 gap-4">
|
| 59 |
+
{TIME_CONTROLS.map(({ category, icon: Icon, color, border, options }) => (
|
| 60 |
+
<div
|
| 61 |
+
key={category}
|
| 62 |
+
className={`bg-zinc-900 border ${border} rounded-xl p-4`}
|
| 63 |
+
>
|
| 64 |
+
<div className={`flex items-center gap-1.5 mb-3 ${color}`}>
|
| 65 |
+
<Icon className="w-3.5 h-3.5" />
|
| 66 |
+
<span className="text-xs font-bold uppercase tracking-widest">{category}</span>
|
| 67 |
+
</div>
|
| 68 |
+
<div className="flex flex-col gap-1.5">
|
| 69 |
+
{options.map((opt) => (
|
| 70 |
+
<button
|
| 71 |
+
key={opt.value}
|
| 72 |
+
onClick={() => joinQueue(opt.value)}
|
| 73 |
+
className="w-full py-2.5 px-3 rounded-lg bg-zinc-800 hover:bg-zinc-700 border border-zinc-700 hover:border-zinc-500 text-white font-mono font-semibold text-sm transition-all duration-100 hover:scale-[1.02] active:scale-[0.97]"
|
| 74 |
+
>
|
| 75 |
+
{opt.label}
|
| 76 |
+
</button>
|
| 77 |
+
))}
|
| 78 |
+
</div>
|
| 79 |
+
</div>
|
| 80 |
+
))}
|
| 81 |
+
</div>
|
| 82 |
+
</div>
|
| 83 |
+
</main>
|
| 84 |
+
</div>
|
| 85 |
+
);
|
| 86 |
+
}
|
chess-frontend/components/game/searching-modal.tsx
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"use client";
|
| 2 |
+
import { useGameStore } from "@/store/use-game-store";
|
| 3 |
+
import { Loader2 } from "lucide-react";
|
| 4 |
+
|
| 5 |
+
export function SearchingModal() {
|
| 6 |
+
const queueStatus = useGameStore((s) => s.queueStatus);
|
| 7 |
+
|
| 8 |
+
if (queueStatus !== "waiting") return null;
|
| 9 |
+
|
| 10 |
+
return (
|
| 11 |
+
<div className="fixed inset-0 bg-black/60 backdrop-blur-sm z-50 flex items-center justify-center">
|
| 12 |
+
<div className="bg-zinc-900 border border-zinc-700 rounded-2xl p-8 flex flex-col items-center gap-5 shadow-2xl w-72">
|
| 13 |
+
<div className="relative">
|
| 14 |
+
<div className="w-16 h-16 rounded-full bg-zinc-800 border border-zinc-700 flex items-center justify-center text-4xl select-none">
|
| 15 |
+
♟
|
| 16 |
+
</div>
|
| 17 |
+
<span className="absolute inset-0 rounded-full border-2 border-green-500/40 animate-ping" />
|
| 18 |
+
</div>
|
| 19 |
+
|
| 20 |
+
<div className="text-center">
|
| 21 |
+
<h2 className="text-lg font-bold mb-1">Finding Opponent</h2>
|
| 22 |
+
<p className="text-zinc-400 text-sm">Searching for a match...</p>
|
| 23 |
+
</div>
|
| 24 |
+
|
| 25 |
+
<div className="flex items-center gap-2 text-zinc-500 text-xs">
|
| 26 |
+
<Loader2 className="w-3.5 h-3.5 animate-spin" />
|
| 27 |
+
<span>In queue</span>
|
| 28 |
+
</div>
|
| 29 |
+
|
| 30 |
+
<button
|
| 31 |
+
disabled
|
| 32 |
+
title="Add leaveQueue to the socket hook to enable this"
|
| 33 |
+
className="text-xs text-zinc-600 cursor-not-allowed underline"
|
| 34 |
+
>
|
| 35 |
+
Cancel
|
| 36 |
+
</button>
|
| 37 |
+
</div>
|
| 38 |
+
</div>
|
| 39 |
+
);
|
| 40 |
+
}
|
chess-frontend/components/ui/button.tsx
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import * as React from "react"
|
| 2 |
+
import { cva, type VariantProps } from "class-variance-authority"
|
| 3 |
+
import { Slot } from "radix-ui"
|
| 4 |
+
|
| 5 |
+
import { cn } from "@/lib/utils"
|
| 6 |
+
|
| 7 |
+
const buttonVariants = cva(
|
| 8 |
+
"group/button inline-flex shrink-0 items-center justify-center rounded-none border border-transparent bg-clip-padding text-xs font-medium whitespace-nowrap transition-all outline-none select-none focus-visible:border-ring focus-visible:ring-1 focus-visible:ring-ring/50 active:translate-y-px disabled:pointer-events-none disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-1 aria-invalid:ring-destructive/20 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
|
| 9 |
+
{
|
| 10 |
+
variants: {
|
| 11 |
+
variant: {
|
| 12 |
+
default: "bg-primary text-primary-foreground [a]:hover:bg-primary/80",
|
| 13 |
+
outline:
|
| 14 |
+
"border-border bg-background hover:bg-muted hover:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:border-input dark:bg-input/30 dark:hover:bg-input/50",
|
| 15 |
+
secondary:
|
| 16 |
+
"bg-secondary text-secondary-foreground hover:bg-secondary/80 aria-expanded:bg-secondary aria-expanded:text-secondary-foreground",
|
| 17 |
+
ghost:
|
| 18 |
+
"hover:bg-muted hover:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:hover:bg-muted/50",
|
| 19 |
+
destructive:
|
| 20 |
+
"bg-destructive/10 text-destructive hover:bg-destructive/20 focus-visible:border-destructive/40 focus-visible:ring-destructive/20 dark:bg-destructive/20 dark:hover:bg-destructive/30 dark:focus-visible:ring-destructive/40",
|
| 21 |
+
link: "text-primary underline-offset-4 hover:underline",
|
| 22 |
+
},
|
| 23 |
+
size: {
|
| 24 |
+
default:
|
| 25 |
+
"h-8 gap-1.5 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",
|
| 26 |
+
xs: "h-6 gap-1 rounded-none px-2 text-xs has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&_svg:not([class*='size-'])]:size-3",
|
| 27 |
+
sm: "h-7 gap-1 rounded-none px-2.5 has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&_svg:not([class*='size-'])]:size-3.5",
|
| 28 |
+
lg: "h-9 gap-1.5 px-2.5 has-data-[icon=inline-end]:pr-3 has-data-[icon=inline-start]:pl-3",
|
| 29 |
+
icon: "size-8",
|
| 30 |
+
"icon-xs": "size-6 rounded-none [&_svg:not([class*='size-'])]:size-3",
|
| 31 |
+
"icon-sm": "size-7 rounded-none",
|
| 32 |
+
"icon-lg": "size-9",
|
| 33 |
+
},
|
| 34 |
+
},
|
| 35 |
+
defaultVariants: {
|
| 36 |
+
variant: "default",
|
| 37 |
+
size: "default",
|
| 38 |
+
},
|
| 39 |
+
}
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
function Button({
|
| 43 |
+
className,
|
| 44 |
+
variant = "default",
|
| 45 |
+
size = "default",
|
| 46 |
+
asChild = false,
|
| 47 |
+
...props
|
| 48 |
+
}: React.ComponentProps<"button"> &
|
| 49 |
+
VariantProps<typeof buttonVariants> & {
|
| 50 |
+
asChild?: boolean
|
| 51 |
+
}) {
|
| 52 |
+
const Comp = asChild ? Slot.Root : "button"
|
| 53 |
+
|
| 54 |
+
return (
|
| 55 |
+
<Comp
|
| 56 |
+
data-slot="button"
|
| 57 |
+
data-variant={variant}
|
| 58 |
+
data-size={size}
|
| 59 |
+
className={cn(buttonVariants({ variant, size, className }))}
|
| 60 |
+
{...props}
|
| 61 |
+
/>
|
| 62 |
+
)
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
export { Button, buttonVariants }
|
chess-frontend/components/ui/field.tsx
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"use client"
|
| 2 |
+
|
| 3 |
+
import { useMemo } from "react"
|
| 4 |
+
import { cva, type VariantProps } from "class-variance-authority"
|
| 5 |
+
|
| 6 |
+
import { cn } from "@/lib/utils"
|
| 7 |
+
import { Label } from "@/components/ui/label"
|
| 8 |
+
import { Separator } from "@/components/ui/separator"
|
| 9 |
+
|
| 10 |
+
function FieldSet({ className, ...props }: React.ComponentProps<"fieldset">) {
|
| 11 |
+
return (
|
| 12 |
+
<fieldset
|
| 13 |
+
data-slot="field-set"
|
| 14 |
+
className={cn(
|
| 15 |
+
"flex flex-col gap-4 has-[>[data-slot=checkbox-group]]:gap-3 has-[>[data-slot=radio-group]]:gap-3",
|
| 16 |
+
className
|
| 17 |
+
)}
|
| 18 |
+
{...props}
|
| 19 |
+
/>
|
| 20 |
+
)
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
function FieldLegend({
|
| 24 |
+
className,
|
| 25 |
+
variant = "legend",
|
| 26 |
+
...props
|
| 27 |
+
}: React.ComponentProps<"legend"> & { variant?: "legend" | "label" }) {
|
| 28 |
+
return (
|
| 29 |
+
<legend
|
| 30 |
+
data-slot="field-legend"
|
| 31 |
+
data-variant={variant}
|
| 32 |
+
className={cn(
|
| 33 |
+
"mb-2.5 font-medium data-[variant=label]:text-xs data-[variant=legend]:text-sm",
|
| 34 |
+
className
|
| 35 |
+
)}
|
| 36 |
+
{...props}
|
| 37 |
+
/>
|
| 38 |
+
)
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
function FieldGroup({ className, ...props }: React.ComponentProps<"div">) {
|
| 42 |
+
return (
|
| 43 |
+
<div
|
| 44 |
+
data-slot="field-group"
|
| 45 |
+
className={cn(
|
| 46 |
+
"group/field-group @container/field-group flex w-full flex-col gap-5 data-[slot=checkbox-group]:gap-3 *:data-[slot=field-group]:gap-4",
|
| 47 |
+
className
|
| 48 |
+
)}
|
| 49 |
+
{...props}
|
| 50 |
+
/>
|
| 51 |
+
)
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
const fieldVariants = cva(
|
| 55 |
+
"group/field flex w-full gap-2 data-[invalid=true]:text-destructive",
|
| 56 |
+
{
|
| 57 |
+
variants: {
|
| 58 |
+
orientation: {
|
| 59 |
+
vertical: "flex-col *:w-full [&>.sr-only]:w-auto",
|
| 60 |
+
horizontal:
|
| 61 |
+
"flex-row items-center has-[>[data-slot=field-content]]:items-start *:data-[slot=field-label]:flex-auto has-[>[data-slot=field-content]]:[&>[role=checkbox],[role=radio]]:mt-px",
|
| 62 |
+
responsive:
|
| 63 |
+
"flex-col *:w-full @md/field-group:flex-row @md/field-group:items-center @md/field-group:*:w-auto @md/field-group:has-[>[data-slot=field-content]]:items-start @md/field-group:*:data-[slot=field-label]:flex-auto [&>.sr-only]:w-auto @md/field-group:has-[>[data-slot=field-content]]:[&>[role=checkbox],[role=radio]]:mt-px",
|
| 64 |
+
},
|
| 65 |
+
},
|
| 66 |
+
defaultVariants: {
|
| 67 |
+
orientation: "vertical",
|
| 68 |
+
},
|
| 69 |
+
}
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
function Field({
|
| 73 |
+
className,
|
| 74 |
+
orientation = "vertical",
|
| 75 |
+
...props
|
| 76 |
+
}: React.ComponentProps<"div"> & VariantProps<typeof fieldVariants>) {
|
| 77 |
+
return (
|
| 78 |
+
<div
|
| 79 |
+
role="group"
|
| 80 |
+
data-slot="field"
|
| 81 |
+
data-orientation={orientation}
|
| 82 |
+
className={cn(fieldVariants({ orientation }), className)}
|
| 83 |
+
{...props}
|
| 84 |
+
/>
|
| 85 |
+
)
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
function FieldContent({ className, ...props }: React.ComponentProps<"div">) {
|
| 89 |
+
return (
|
| 90 |
+
<div
|
| 91 |
+
data-slot="field-content"
|
| 92 |
+
className={cn(
|
| 93 |
+
"group/field-content flex flex-1 flex-col gap-0.5 leading-snug",
|
| 94 |
+
className
|
| 95 |
+
)}
|
| 96 |
+
{...props}
|
| 97 |
+
/>
|
| 98 |
+
)
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
function FieldLabel({
|
| 102 |
+
className,
|
| 103 |
+
...props
|
| 104 |
+
}: React.ComponentProps<typeof Label>) {
|
| 105 |
+
return (
|
| 106 |
+
<Label
|
| 107 |
+
data-slot="field-label"
|
| 108 |
+
className={cn(
|
| 109 |
+
"group/field-label peer/field-label flex w-fit gap-2 leading-snug group-data-[disabled=true]/field:opacity-50 has-data-checked:border-primary/30 has-data-checked:bg-primary/5 has-[>[data-slot=field]]:rounded-none has-[>[data-slot=field]]:border *:data-[slot=field]:p-2 dark:has-data-checked:border-primary/20 dark:has-data-checked:bg-primary/10",
|
| 110 |
+
"has-[>[data-slot=field]]:w-full has-[>[data-slot=field]]:flex-col",
|
| 111 |
+
className
|
| 112 |
+
)}
|
| 113 |
+
{...props}
|
| 114 |
+
/>
|
| 115 |
+
)
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
function FieldTitle({ className, ...props }: React.ComponentProps<"div">) {
|
| 119 |
+
return (
|
| 120 |
+
<div
|
| 121 |
+
data-slot="field-label"
|
| 122 |
+
className={cn(
|
| 123 |
+
"flex w-fit items-center gap-2 text-xs/relaxed leading-snug group-data-[disabled=true]/field:opacity-50",
|
| 124 |
+
className
|
| 125 |
+
)}
|
| 126 |
+
{...props}
|
| 127 |
+
/>
|
| 128 |
+
)
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
function FieldDescription({ className, ...props }: React.ComponentProps<"p">) {
|
| 132 |
+
return (
|
| 133 |
+
<p
|
| 134 |
+
data-slot="field-description"
|
| 135 |
+
className={cn(
|
| 136 |
+
"text-left text-xs/relaxed leading-normal font-normal text-muted-foreground group-has-data-horizontal/field:text-balance [[data-variant=legend]+&]:-mt-1.5",
|
| 137 |
+
"last:mt-0 nth-last-2:-mt-1",
|
| 138 |
+
"[&>a]:underline [&>a]:underline-offset-4 [&>a:hover]:text-primary",
|
| 139 |
+
className
|
| 140 |
+
)}
|
| 141 |
+
{...props}
|
| 142 |
+
/>
|
| 143 |
+
)
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
function FieldSeparator({
|
| 147 |
+
children,
|
| 148 |
+
className,
|
| 149 |
+
...props
|
| 150 |
+
}: React.ComponentProps<"div"> & {
|
| 151 |
+
children?: React.ReactNode
|
| 152 |
+
}) {
|
| 153 |
+
return (
|
| 154 |
+
<div
|
| 155 |
+
data-slot="field-separator"
|
| 156 |
+
data-content={!!children}
|
| 157 |
+
className={cn(
|
| 158 |
+
"relative -my-2 h-5 text-xs group-data-[variant=outline]/field-group:-mb-2",
|
| 159 |
+
className
|
| 160 |
+
)}
|
| 161 |
+
{...props}
|
| 162 |
+
>
|
| 163 |
+
<Separator className="absolute inset-0 top-1/2" />
|
| 164 |
+
{children && (
|
| 165 |
+
<span
|
| 166 |
+
className="relative mx-auto block w-fit bg-background px-2 text-muted-foreground"
|
| 167 |
+
data-slot="field-separator-content"
|
| 168 |
+
>
|
| 169 |
+
{children}
|
| 170 |
+
</span>
|
| 171 |
+
)}
|
| 172 |
+
</div>
|
| 173 |
+
)
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
function FieldError({
|
| 177 |
+
className,
|
| 178 |
+
children,
|
| 179 |
+
errors,
|
| 180 |
+
...props
|
| 181 |
+
}: React.ComponentProps<"div"> & {
|
| 182 |
+
errors?: Array<{ message?: string } | undefined>
|
| 183 |
+
}) {
|
| 184 |
+
const content = useMemo(() => {
|
| 185 |
+
if (children) {
|
| 186 |
+
return children
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
if (!errors?.length) {
|
| 190 |
+
return null
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
const uniqueErrors = [
|
| 194 |
+
...new Map(errors.map((error) => [error?.message, error])).values(),
|
| 195 |
+
]
|
| 196 |
+
|
| 197 |
+
if (uniqueErrors?.length == 1) {
|
| 198 |
+
return uniqueErrors[0]?.message
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
return (
|
| 202 |
+
<ul className="ml-4 flex list-disc flex-col gap-1">
|
| 203 |
+
{uniqueErrors.map(
|
| 204 |
+
(error, index) =>
|
| 205 |
+
error?.message && <li key={index}>{error.message}</li>
|
| 206 |
+
)}
|
| 207 |
+
</ul>
|
| 208 |
+
)
|
| 209 |
+
}, [children, errors])
|
| 210 |
+
|
| 211 |
+
if (!content) {
|
| 212 |
+
return null
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
return (
|
| 216 |
+
<div
|
| 217 |
+
role="alert"
|
| 218 |
+
data-slot="field-error"
|
| 219 |
+
className={cn("text-xs font-normal text-destructive", className)}
|
| 220 |
+
{...props}
|
| 221 |
+
>
|
| 222 |
+
{content}
|
| 223 |
+
</div>
|
| 224 |
+
)
|
| 225 |
+
}
|
| 226 |
+
|
| 227 |
+
export {
|
| 228 |
+
Field,
|
| 229 |
+
FieldLabel,
|
| 230 |
+
FieldDescription,
|
| 231 |
+
FieldError,
|
| 232 |
+
FieldGroup,
|
| 233 |
+
FieldLegend,
|
| 234 |
+
FieldSeparator,
|
| 235 |
+
FieldSet,
|
| 236 |
+
FieldContent,
|
| 237 |
+
FieldTitle,
|
| 238 |
+
}
|
chess-frontend/components/ui/input.tsx
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import * as React from "react"
|
| 2 |
+
|
| 3 |
+
import { cn } from "@/lib/utils"
|
| 4 |
+
|
| 5 |
+
function Input({ className, type, ...props }: React.ComponentProps<"input">) {
|
| 6 |
+
return (
|
| 7 |
+
<input
|
| 8 |
+
type={type}
|
| 9 |
+
data-slot="input"
|
| 10 |
+
className={cn(
|
| 11 |
+
"h-8 w-full min-w-0 rounded-none border border-input bg-transparent px-2.5 py-1 text-xs transition-colors outline-none file:inline-flex file:h-6 file:border-0 file:bg-transparent file:text-xs file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-1 focus-visible:ring-ring/50 disabled:pointer-events-none disabled:cursor-not-allowed disabled:bg-input/50 disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-1 aria-invalid:ring-destructive/20 md:text-xs dark:bg-input/30 dark:disabled:bg-input/80 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40",
|
| 12 |
+
className
|
| 13 |
+
)}
|
| 14 |
+
{...props}
|
| 15 |
+
/>
|
| 16 |
+
)
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
export { Input }
|
chess-frontend/components/ui/label.tsx
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"use client"
|
| 2 |
+
|
| 3 |
+
import * as React from "react"
|
| 4 |
+
import { Label as LabelPrimitive } from "radix-ui"
|
| 5 |
+
|
| 6 |
+
import { cn } from "@/lib/utils"
|
| 7 |
+
|
| 8 |
+
function Label({
|
| 9 |
+
className,
|
| 10 |
+
...props
|
| 11 |
+
}: React.ComponentProps<typeof LabelPrimitive.Root>) {
|
| 12 |
+
return (
|
| 13 |
+
<LabelPrimitive.Root
|
| 14 |
+
data-slot="label"
|
| 15 |
+
className={cn(
|
| 16 |
+
"flex items-center gap-2 text-xs leading-none select-none group-data-[disabled=true]:pointer-events-none group-data-[disabled=true]:opacity-50 peer-disabled:cursor-not-allowed peer-disabled:opacity-50",
|
| 17 |
+
className
|
| 18 |
+
)}
|
| 19 |
+
{...props}
|
| 20 |
+
/>
|
| 21 |
+
)
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
export { Label }
|
chess-frontend/components/ui/separator.tsx
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"use client"
|
| 2 |
+
|
| 3 |
+
import * as React from "react"
|
| 4 |
+
import { Separator as SeparatorPrimitive } from "radix-ui"
|
| 5 |
+
|
| 6 |
+
import { cn } from "@/lib/utils"
|
| 7 |
+
|
| 8 |
+
function Separator({
|
| 9 |
+
className,
|
| 10 |
+
orientation = "horizontal",
|
| 11 |
+
decorative = true,
|
| 12 |
+
...props
|
| 13 |
+
}: React.ComponentProps<typeof SeparatorPrimitive.Root>) {
|
| 14 |
+
return (
|
| 15 |
+
<SeparatorPrimitive.Root
|
| 16 |
+
data-slot="separator"
|
| 17 |
+
decorative={decorative}
|
| 18 |
+
orientation={orientation}
|
| 19 |
+
className={cn(
|
| 20 |
+
"shrink-0 bg-border data-horizontal:h-px data-horizontal:w-full data-vertical:w-px data-vertical:self-stretch",
|
| 21 |
+
className
|
| 22 |
+
)}
|
| 23 |
+
{...props}
|
| 24 |
+
/>
|
| 25 |
+
)
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
export { Separator }
|
chess-frontend/eslint.config.mjs
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { defineConfig, globalIgnores } from "eslint/config";
|
| 2 |
+
import nextVitals from "eslint-config-next/core-web-vitals";
|
| 3 |
+
import nextTs from "eslint-config-next/typescript";
|
| 4 |
+
|
| 5 |
+
const eslintConfig = defineConfig([
|
| 6 |
+
...nextVitals,
|
| 7 |
+
...nextTs,
|
| 8 |
+
// Override default ignores of eslint-config-next.
|
| 9 |
+
globalIgnores([
|
| 10 |
+
// Default ignores of eslint-config-next:
|
| 11 |
+
".next/**",
|
| 12 |
+
"out/**",
|
| 13 |
+
"build/**",
|
| 14 |
+
"next-env.d.ts",
|
| 15 |
+
]),
|
| 16 |
+
]);
|
| 17 |
+
|
| 18 |
+
export default eslintConfig;
|
chess-frontend/events/chess.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { WsMessageType } from "@/types/chess";
|
| 2 |
+
import * as z from "zod";
|
| 3 |
+
|
| 4 |
+
const EmptyPayload = z.undefined();
|
| 5 |
+
|
| 6 |
+
export const JoinQueueSchema = z.object({
|
| 7 |
+
timeControl: z.enum([
|
| 8 |
+
"1+0", "1+1", "2+1", "3+0", "3+2",
|
| 9 |
+
"5+0", "5+3", "10+0", "10+5",
|
| 10 |
+
"15+10", "30+0", "30+20", "60+0", "90+30",
|
| 11 |
+
]),
|
| 12 |
+
});
|
| 13 |
+
|
| 14 |
+
export const MakeMoveSchema = z.object({
|
| 15 |
+
gameId: z.string().uuid(),
|
| 16 |
+
from: z.string().regex(/^[a-h][1-8]$/),
|
| 17 |
+
to: z.string().regex(/^[a-h][1-8]$/),
|
| 18 |
+
promotion: z.enum(["q", "r", "b", "n"]).optional(),
|
| 19 |
+
});
|
| 20 |
+
|
| 21 |
+
export const GameIdOnlySchema = z.object({
|
| 22 |
+
gameId: z.string().uuid(),
|
| 23 |
+
});
|
| 24 |
+
|
| 25 |
+
export const WsMessageSchema = z.discriminatedUnion("type", [
|
| 26 |
+
z.object({
|
| 27 |
+
type: z.literal(WsMessageType.JOIN_QUEUE),
|
| 28 |
+
payload: JoinQueueSchema,
|
| 29 |
+
}),
|
| 30 |
+
z.object({
|
| 31 |
+
type: z.literal(WsMessageType.MAKE_MOVE),
|
| 32 |
+
payload: MakeMoveSchema,
|
| 33 |
+
}),
|
| 34 |
+
z.object({
|
| 35 |
+
type: z.literal(WsMessageType.RESIGN_GAME),
|
| 36 |
+
payload: GameIdOnlySchema,
|
| 37 |
+
}),
|
| 38 |
+
z.object({
|
| 39 |
+
type: z.literal(WsMessageType.GAME_ABORTED),
|
| 40 |
+
payload: GameIdOnlySchema,
|
| 41 |
+
}),
|
| 42 |
+
z.object({
|
| 43 |
+
type: z.literal(WsMessageType.OFFER_DRAW),
|
| 44 |
+
payload: GameIdOnlySchema,
|
| 45 |
+
}),
|
| 46 |
+
z.object({
|
| 47 |
+
type: z.literal(WsMessageType.ACCEPT_DRAW),
|
| 48 |
+
payload: GameIdOnlySchema,
|
| 49 |
+
}),
|
| 50 |
+
z.object({
|
| 51 |
+
type: z.literal(WsMessageType.DECLINE_DRAW),
|
| 52 |
+
payload: GameIdOnlySchema,
|
| 53 |
+
}),
|
| 54 |
+
z.object({
|
| 55 |
+
type: z.literal(WsMessageType.LEAVE_QUEUE),
|
| 56 |
+
payload: EmptyPayload,
|
| 57 |
+
}),
|
| 58 |
+
z.object({
|
| 59 |
+
type: z.literal(WsMessageType.SYNC_GAME),
|
| 60 |
+
payload: EmptyPayload,
|
| 61 |
+
}),
|
| 62 |
+
]);
|
| 63 |
+
|
| 64 |
+
export type WsMessage = z.infer<typeof WsMessageSchema>;
|
chess-frontend/hooks/use-websocket.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { useCallback, useEffect, useRef } from "react";
|
| 2 |
+
import { WsMessageType } from "../types/chess";
|
| 3 |
+
import { useGameStore } from "@/store/use-game-store";
|
| 4 |
+
import { User } from "@/types/auth";
|
| 5 |
+
|
| 6 |
+
const WS_URL = process.env.NEXT_PUBLIC_WS_URL ?? "ws://localhost:8080";
|
| 7 |
+
|
| 8 |
+
export function useWebSocket(user: User) {
|
| 9 |
+
const wsRef = useRef<WebSocket | null>(null);
|
| 10 |
+
|
| 11 |
+
const send = useCallback((type: WsMessageType, payload?: any) => {
|
| 12 |
+
if (wsRef.current?.readyState === WebSocket.OPEN) {
|
| 13 |
+
// Start with just the type
|
| 14 |
+
const message: { type: WsMessageType; payload?: any } = { type };
|
| 15 |
+
|
| 16 |
+
// Only attach payload if it has actual data
|
| 17 |
+
if (payload !== undefined && payload !== null) {
|
| 18 |
+
message.payload = payload;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
wsRef.current.send(JSON.stringify(message));
|
| 22 |
+
} else {
|
| 23 |
+
console.warn(`[WS] Cannot send ${type}, socket is not OPEN.`);
|
| 24 |
+
}
|
| 25 |
+
}, []);
|
| 26 |
+
|
| 27 |
+
const handleMessage = useCallback((event: MessageEvent) => {
|
| 28 |
+
const msg = JSON.parse(event.data);
|
| 29 |
+
const store = useGameStore.getState();
|
| 30 |
+
// Always get the freshest store actions
|
| 31 |
+
|
| 32 |
+
switch (msg.type) {
|
| 33 |
+
case WsMessageType.MOVE_MADE:
|
| 34 |
+
store.handleMoveMade(msg.payload);
|
| 35 |
+
break;
|
| 36 |
+
case WsMessageType.MOVE_REJECTED:
|
| 37 |
+
store.handleMoveRejected(msg.payload.reason);
|
| 38 |
+
break;
|
| 39 |
+
case WsMessageType.GAME_STARTED: store.handleGameStarted(msg.payload); break;
|
| 40 |
+
case WsMessageType.GAME_STATE: store.handleGameState(msg.payload); break;
|
| 41 |
+
case WsMessageType.GAME_OVER: store.handleGameOver(msg.payload); break;
|
| 42 |
+
case WsMessageType.QUEUE_JOINED: store.setQueue("waiting"); break;
|
| 43 |
+
case WsMessageType.QUEUE_LEFT: store.setQueue("idle"); break;
|
| 44 |
+
}
|
| 45 |
+
}, []);
|
| 46 |
+
|
| 47 |
+
const connect = useCallback(() => {
|
| 48 |
+
const store = useGameStore.getState();
|
| 49 |
+
|
| 50 |
+
// FIX 1: Prevent Strict Mode from opening a 2nd connection while the 1st is still connecting
|
| 51 |
+
if (
|
| 52 |
+
wsRef.current &&
|
| 53 |
+
(wsRef.current.readyState === WebSocket.OPEN || wsRef.current.readyState === WebSocket.CONNECTING)
|
| 54 |
+
) {
|
| 55 |
+
return;
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
console.log("[WS] Attempting to connect to:", WS_URL);
|
| 59 |
+
store.setConnection("connecting");
|
| 60 |
+
store.setUser(user);
|
| 61 |
+
|
| 62 |
+
const ws = new WebSocket(WS_URL);
|
| 63 |
+
wsRef.current = ws;
|
| 64 |
+
|
| 65 |
+
ws.onopen = () => {
|
| 66 |
+
console.log("[WS] Connected, requesting sync...");
|
| 67 |
+
store.setConnection("connected");
|
| 68 |
+
|
| 69 |
+
ws.send(JSON.stringify({ type: WsMessageType.SYNC_GAME }));
|
| 70 |
+
};
|
| 71 |
+
|
| 72 |
+
ws.onmessage = handleMessage;
|
| 73 |
+
|
| 74 |
+
// FIX 2: Add explicit error and close logging to catch backend rejections
|
| 75 |
+
ws.onerror = (error) => {
|
| 76 |
+
console.error("[WS] Connection Error:", error);
|
| 77 |
+
store.setConnection("error");
|
| 78 |
+
};
|
| 79 |
+
|
| 80 |
+
ws.onclose = (event) => {
|
| 81 |
+
console.warn(`[WS] Closed. Code: ${event.code}, Reason: ${event.reason || "No reason given"}`);
|
| 82 |
+
store.setConnection("disconnected");
|
| 83 |
+
wsRef.current = null;
|
| 84 |
+
};
|
| 85 |
+
}, [handleMessage, user]); // Added user to dependencies
|
| 86 |
+
|
| 87 |
+
// FIX 3: Cleanup function to close socket when component unmounts
|
| 88 |
+
useEffect(() => {
|
| 89 |
+
return () => {
|
| 90 |
+
if (wsRef.current) {
|
| 91 |
+
console.log("[WS] Component unmounted, closing socket.");
|
| 92 |
+
wsRef.current.close(1000, "Component unmounted");
|
| 93 |
+
wsRef.current = null;
|
| 94 |
+
}
|
| 95 |
+
};
|
| 96 |
+
}, []);
|
| 97 |
+
|
| 98 |
+
return {
|
| 99 |
+
connect,
|
| 100 |
+
joinQueue: (timeControl: string) => send(WsMessageType.JOIN_QUEUE, { timeControl }),
|
| 101 |
+
makeMove: (gameId: string, from: string, to: string, promotion?: string) =>
|
| 102 |
+
send(WsMessageType.MAKE_MOVE, { gameId, from, to, promotion }),
|
| 103 |
+
resign: (gameId: string) => send(WsMessageType.RESIGN_GAME, { gameId }),
|
| 104 |
+
};
|
| 105 |
+
}
|
chess-frontend/lib/clients/baseApi.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const API_URL = process.env.INTERNAL_API_URL;
|
| 2 |
+
const ORIGIN_URL = process.env.NEXT_PUBLIC_ORIGIN_URL;
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
type SmartBody = BodyInit | Record<string, any> | null | undefined;
|
| 6 |
+
|
| 7 |
+
type NextFetchOptions = Omit<RequestInit, "body"> & {
|
| 8 |
+
body?: SmartBody;
|
| 9 |
+
next?: {
|
| 10 |
+
revalidate?: number | false;
|
| 11 |
+
tags?: string[];
|
| 12 |
+
};
|
| 13 |
+
cache?: RequestCache;
|
| 14 |
+
};
|
| 15 |
+
|
| 16 |
+
function normalizeBody(
|
| 17 |
+
body: SmartBody,
|
| 18 |
+
headers: HeadersInit | undefined,
|
| 19 |
+
): { body?: BodyInit; headers: HeadersInit } {
|
| 20 |
+
const finalHeaders: Record<string, string> = {
|
| 21 |
+
...(headers as any),
|
| 22 |
+
};
|
| 23 |
+
|
| 24 |
+
if (
|
| 25 |
+
body &&
|
| 26 |
+
typeof body === "object" &&
|
| 27 |
+
!(body instanceof FormData) &&
|
| 28 |
+
!(body instanceof Blob) &&
|
| 29 |
+
!(body instanceof ArrayBuffer)
|
| 30 |
+
) {
|
| 31 |
+
finalHeaders["Content-Type"] ||= "application/json";
|
| 32 |
+
return {
|
| 33 |
+
body: JSON.stringify(body),
|
| 34 |
+
headers: finalHeaders,
|
| 35 |
+
};
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
return { body: body as BodyInit | undefined, headers: finalHeaders };
|
| 39 |
+
}
|
| 40 |
+
export async function baseApi<T>(
|
| 41 |
+
endpoint: string,
|
| 42 |
+
options: NextFetchOptions = {},
|
| 43 |
+
) {
|
| 44 |
+
const { body, headers, ...rest } = options;
|
| 45 |
+
|
| 46 |
+
const normalized = normalizeBody(body, headers);
|
| 47 |
+
|
| 48 |
+
const res = await fetch(`${API_URL}${endpoint}`, {
|
| 49 |
+
...rest,
|
| 50 |
+
credentials: "omit",
|
| 51 |
+
body: normalized.body,
|
| 52 |
+
headers: {
|
| 53 |
+
...normalized.headers,
|
| 54 |
+
Origin: ORIGIN_URL ?? "https://rohit-ecommerce-microservice.dedyn.io",
|
| 55 |
+
},
|
| 56 |
+
});
|
| 57 |
+
|
| 58 |
+
return res;
|
| 59 |
+
}
|
chess-frontend/lib/clients/server.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { cookies, headers } from "next/headers";
|
| 2 |
+
import { redirect } from "next/navigation";
|
| 3 |
+
|
| 4 |
+
const API_URL = process.env.INTERNAL_API_URL;
|
| 5 |
+
|
| 6 |
+
type SmartBody = BodyInit | Record<string, any> | null | undefined;
|
| 7 |
+
|
| 8 |
+
type NextFetchOptions = Omit<RequestInit, "body"> & {
|
| 9 |
+
body?: SmartBody;
|
| 10 |
+
next?: {
|
| 11 |
+
revalidate?: number | false;
|
| 12 |
+
tags?: string[];
|
| 13 |
+
};
|
| 14 |
+
cache?: RequestCache;
|
| 15 |
+
};
|
| 16 |
+
|
| 17 |
+
function normalizeBody(body: SmartBody, headers: HeadersInit | undefined) {
|
| 18 |
+
const finalHeaders: Record<string, string> = {
|
| 19 |
+
...(headers as any),
|
| 20 |
+
};
|
| 21 |
+
|
| 22 |
+
if (
|
| 23 |
+
body &&
|
| 24 |
+
typeof body === "object" &&
|
| 25 |
+
!(body instanceof FormData) &&
|
| 26 |
+
!(body instanceof Blob) &&
|
| 27 |
+
!(body instanceof ArrayBuffer)
|
| 28 |
+
) {
|
| 29 |
+
finalHeaders["Content-Type"] ||= "application/json";
|
| 30 |
+
return { body: JSON.stringify(body), headers: finalHeaders };
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
return { body: body as BodyInit | undefined, headers: finalHeaders };
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
export async function api(endpoint: string, options: NextFetchOptions = {}) {
|
| 37 |
+
const { body, headers: optionHeaders, ...rest } = options;
|
| 38 |
+
|
| 39 |
+
const { body: finalBody, headers: finalHeaders } = normalizeBody(
|
| 40 |
+
body,
|
| 41 |
+
optionHeaders,
|
| 42 |
+
);
|
| 43 |
+
|
| 44 |
+
const cookieStore = await cookies();
|
| 45 |
+
const cookieHeader = cookieStore.toString();
|
| 46 |
+
if (cookieHeader) {
|
| 47 |
+
finalHeaders["Cookie"] = cookieHeader;
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
const headersList = await headers();
|
| 51 |
+
const incomingOrigin = headersList.get("origin");
|
| 52 |
+
|
| 53 |
+
const origin = incomingOrigin || process.env.NEXT_PUBLIC_ORIGIN_URL;
|
| 54 |
+
|
| 55 |
+
if (origin) {
|
| 56 |
+
finalHeaders["Origin"] = origin;
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
const fullUrl = `${API_URL}${endpoint}`;
|
| 60 |
+
|
| 61 |
+
const res = await fetch(fullUrl, {
|
| 62 |
+
...rest,
|
| 63 |
+
headers: finalHeaders,
|
| 64 |
+
body: finalBody,
|
| 65 |
+
});
|
| 66 |
+
|
| 67 |
+
if (res.status === 401) {
|
| 68 |
+
redirect("/sign-in");
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
return res;
|
| 72 |
+
}
|
chess-frontend/lib/constants/error-simplifier.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
type RawErrors = {
|
| 2 |
+
errors: string[];
|
| 3 |
+
properties?: Record<string, { errors: string[] }>;
|
| 4 |
+
};
|
| 5 |
+
|
| 6 |
+
export function simplifyZodErrors(
|
| 7 |
+
treeError: RawErrors
|
| 8 |
+
): Record<string, string> {
|
| 9 |
+
const simplifiedErrors: Record<string, string> = {};
|
| 10 |
+
|
| 11 |
+
if (treeError.properties) {
|
| 12 |
+
Object.entries(treeError.properties).forEach(([key, value]) => {
|
| 13 |
+
if (value?.errors && value.errors.length > 0) {
|
| 14 |
+
simplifiedErrors[key] = value.errors[0];
|
| 15 |
+
}
|
| 16 |
+
});
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
return simplifiedErrors;
|
| 20 |
+
}
|
chess-frontend/lib/constants/get-move-duration.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
function getMoveDurations(pgn: string, baseTimeMs: number): number[] {
|
| 2 |
+
const clockRegex = /\[%clk (\d+):(\d{2}):(\d{2})\]/g;
|
| 3 |
+
const matches = [...pgn.matchAll(clockRegex)];
|
| 4 |
+
|
| 5 |
+
const clockSnapshots = matches.map(m => {
|
| 6 |
+
const hours = parseInt(m[1]);
|
| 7 |
+
const mins = parseInt(m[2]);
|
| 8 |
+
const secs = parseInt(m[3]);
|
| 9 |
+
return (hours * 3600 + mins * 60 + secs) * 1000;
|
| 10 |
+
});
|
| 11 |
+
|
| 12 |
+
const durations: number[] = [];
|
| 13 |
+
let lastWhiteTime = baseTimeMs;
|
| 14 |
+
let lastBlackTime = baseTimeMs;
|
| 15 |
+
|
| 16 |
+
clockSnapshots.forEach((currentTime, index) => {
|
| 17 |
+
const isWhiteMove = index % 2 === 0;
|
| 18 |
+
|
| 19 |
+
if (isWhiteMove) {
|
| 20 |
+
durations.push(lastWhiteTime - currentTime);
|
| 21 |
+
lastWhiteTime = currentTime;
|
| 22 |
+
} else {
|
| 23 |
+
durations.push(lastBlackTime - currentTime);
|
| 24 |
+
lastBlackTime = currentTime;
|
| 25 |
+
}
|
| 26 |
+
});
|
| 27 |
+
|
| 28 |
+
return durations;
|
| 29 |
+
}
|
chess-frontend/lib/service/auth.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { LoginInput, LoginResponse, User } from "@/types/auth";
|
| 2 |
+
import { cookies } from "next/headers";
|
| 3 |
+
import { baseApi } from "../clients/baseApi";
|
| 4 |
+
import { api } from "../clients/server";
|
| 5 |
+
|
| 6 |
+
export async function signIn(data: LoginInput): Promise<User> {
|
| 7 |
+
const res = await baseApi("/auth/sign-in/email", {
|
| 8 |
+
method: "POST",
|
| 9 |
+
body: data,
|
| 10 |
+
});
|
| 11 |
+
|
| 12 |
+
if (!res.ok) {
|
| 13 |
+
const err = await res.json();
|
| 14 |
+
throw err;
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
const rawSetCookie = res.headers.get("set-cookie") || "";
|
| 18 |
+
const cookieHeader = rawSetCookie
|
| 19 |
+
? rawSetCookie.split(/,(?=\s*[^;]+=[^;]+)/)
|
| 20 |
+
: [];
|
| 21 |
+
|
| 22 |
+
const cookieStore = await cookies();
|
| 23 |
+
cookieHeader.forEach((cookieString: string) => {
|
| 24 |
+
const [nameValue] = cookieString.split(";");
|
| 25 |
+
const [name, ...rest] = nameValue.split("=");
|
| 26 |
+
const value = rest.join("=");
|
| 27 |
+
|
| 28 |
+
if (name && value) {
|
| 29 |
+
const cleanValue = decodeURIComponent(value);
|
| 30 |
+
|
| 31 |
+
cookieStore.set({
|
| 32 |
+
name: name.trim(),
|
| 33 |
+
value: cleanValue,
|
| 34 |
+
httpOnly: true,
|
| 35 |
+
path: "/",
|
| 36 |
+
secure: true,
|
| 37 |
+
sameSite: "lax",
|
| 38 |
+
maxAge: 60 * 60 * 24,
|
| 39 |
+
});
|
| 40 |
+
}
|
| 41 |
+
});
|
| 42 |
+
|
| 43 |
+
const json = (await res.json()) as LoginResponse;
|
| 44 |
+
return json.user;
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
export async function signOut() {
|
| 48 |
+
const res = await api("/auth/sign-out", {
|
| 49 |
+
method: "POST",
|
| 50 |
+
body: {},
|
| 51 |
+
});
|
| 52 |
+
|
| 53 |
+
if (!res.ok) {
|
| 54 |
+
const err = await res.json();
|
| 55 |
+
throw err;
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
return await res.json();
|
| 59 |
+
}
|
chess-frontend/lib/utils.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { clsx, type ClassValue } from "clsx"
|
| 2 |
+
import { twMerge } from "tailwind-merge"
|
| 3 |
+
|
| 4 |
+
export function cn(...inputs: ClassValue[]) {
|
| 5 |
+
return twMerge(clsx(inputs))
|
| 6 |
+
}
|
chess-frontend/next.config.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type { NextConfig } from "next";
|
| 2 |
+
|
| 3 |
+
const nextConfig: NextConfig = {
|
| 4 |
+
/* config options here */
|
| 5 |
+
};
|
| 6 |
+
|
| 7 |
+
export default nextConfig;
|
chess-frontend/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "chess-frontend",
|
| 3 |
+
"version": "0.1.0",
|
| 4 |
+
"private": true,
|
| 5 |
+
"scripts": {
|
| 6 |
+
"dev": "next dev",
|
| 7 |
+
"build": "next build",
|
| 8 |
+
"start": "next start",
|
| 9 |
+
"lint": "eslint"
|
| 10 |
+
},
|
| 11 |
+
"dependencies": {
|
| 12 |
+
"@phosphor-icons/react": "^2.1.10",
|
| 13 |
+
"chess.js": "^1.4.0",
|
| 14 |
+
"class-variance-authority": "^0.7.1",
|
| 15 |
+
"clsx": "^2.1.1",
|
| 16 |
+
"lucide-react": "^0.577.0",
|
| 17 |
+
"next": "16.1.6",
|
| 18 |
+
"radix-ui": "^1.4.3",
|
| 19 |
+
"react": "19.2.3",
|
| 20 |
+
"react-chessboard": "^5.10.0",
|
| 21 |
+
"react-dom": "19.2.3",
|
| 22 |
+
"shadcn": "^4.0.5",
|
| 23 |
+
"tailwind-merge": "^3.5.0",
|
| 24 |
+
"tw-animate-css": "^1.4.0",
|
| 25 |
+
"zod": "^4.3.6",
|
| 26 |
+
"zustand": "^5.0.11"
|
| 27 |
+
},
|
| 28 |
+
"devDependencies": {
|
| 29 |
+
"@tailwindcss/postcss": "^4",
|
| 30 |
+
"@types/node": "^20",
|
| 31 |
+
"@types/react": "^19",
|
| 32 |
+
"@types/react-dom": "^19",
|
| 33 |
+
"eslint": "^9",
|
| 34 |
+
"eslint-config-next": "16.1.6",
|
| 35 |
+
"tailwindcss": "^4",
|
| 36 |
+
"typescript": "^5"
|
| 37 |
+
}
|
| 38 |
+
}
|
chess-frontend/postcss.config.mjs
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const config = {
|
| 2 |
+
plugins: {
|
| 3 |
+
"@tailwindcss/postcss": {},
|
| 4 |
+
},
|
| 5 |
+
};
|
| 6 |
+
|
| 7 |
+
export default config;
|
chess-frontend/proxy.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { NextResponse } from "next/server";
|
| 2 |
+
import type { NextRequest } from "next/server";
|
| 3 |
+
|
| 4 |
+
export function proxy(request: NextRequest) {
|
| 5 |
+
const isServerAction = request.headers.get("Next-Action");
|
| 6 |
+
const isRSCRequest = request.headers.get("RSC");
|
| 7 |
+
const isApiRoute = request.nextUrl.pathname.startsWith("/api");
|
| 8 |
+
|
| 9 |
+
const shouldSkipRedirect = isServerAction || isRSCRequest || isApiRoute;
|
| 10 |
+
|
| 11 |
+
const sessionToken = request.cookies.get("better-auth.session_token");
|
| 12 |
+
const sessionData = request.cookies.get("better-auth.session_data");
|
| 13 |
+
|
| 14 |
+
let isValidSession = false;
|
| 15 |
+
|
| 16 |
+
if (sessionToken && sessionData) {
|
| 17 |
+
try {
|
| 18 |
+
const jsonString = atob(sessionData.value);
|
| 19 |
+
JSON.parse(jsonString);
|
| 20 |
+
isValidSession = true;
|
| 21 |
+
} catch {
|
| 22 |
+
isValidSession = false;
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
// 1. Fix the paths (added the slash to /game)
|
| 27 |
+
const authPaths = ["/login", "/register"];
|
| 28 |
+
const protectedPaths = ["/", "/game"];
|
| 29 |
+
|
| 30 |
+
const isAuthRoute = authPaths.some((path) =>
|
| 31 |
+
request.nextUrl.pathname.startsWith(path)
|
| 32 |
+
);
|
| 33 |
+
|
| 34 |
+
// 2. Fix the Wildcard Trap
|
| 35 |
+
const isProtectedRoute = protectedPaths.some((path) => {
|
| 36 |
+
if (path === "/") {
|
| 37 |
+
return request.nextUrl.pathname === "/"; // Exact match for root
|
| 38 |
+
}
|
| 39 |
+
return request.nextUrl.pathname.startsWith(path);
|
| 40 |
+
});
|
| 41 |
+
|
| 42 |
+
if (shouldSkipRedirect) {
|
| 43 |
+
const response = NextResponse.next();
|
| 44 |
+
if ((sessionToken || sessionData) && !isValidSession) {
|
| 45 |
+
response.cookies.delete("better-auth.session_token");
|
| 46 |
+
response.cookies.delete("better-auth.session_data");
|
| 47 |
+
}
|
| 48 |
+
return response;
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
// 3. Handle Auth Routes FIRST
|
| 52 |
+
if (isAuthRoute) {
|
| 53 |
+
if (isValidSession) {
|
| 54 |
+
// If logged in, send them to the dashboard/home
|
| 55 |
+
const homeUrl = request.nextUrl.clone();
|
| 56 |
+
homeUrl.pathname = "/";
|
| 57 |
+
return NextResponse.redirect(homeUrl);
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
// If cookies are invalid, clear them but let them stay on /login
|
| 61 |
+
if (!isValidSession && (sessionToken || sessionData)) {
|
| 62 |
+
const response = NextResponse.next();
|
| 63 |
+
response.cookies.delete("better-auth.session_token");
|
| 64 |
+
response.cookies.delete("better-auth.session_data");
|
| 65 |
+
return response;
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
return NextResponse.next();
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
// 4. Handle Protected Routes
|
| 72 |
+
if (isProtectedRoute && !isValidSession) {
|
| 73 |
+
const signInUrl = request.nextUrl.clone();
|
| 74 |
+
signInUrl.pathname = "/login";
|
| 75 |
+
signInUrl.searchParams.set("redirect", request.nextUrl.pathname);
|
| 76 |
+
|
| 77 |
+
const response = NextResponse.redirect(signInUrl);
|
| 78 |
+
|
| 79 |
+
if (sessionToken || sessionData) {
|
| 80 |
+
response.cookies.delete("better-auth.session_token");
|
| 81 |
+
response.cookies.delete("better-auth.session_data");
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
return response;
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
return NextResponse.next();
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
// Removed /login from the exclusion list so the proxy can handle redirecting
|
| 91 |
+
// logged-in users away from the login page!
|
| 92 |
+
export const config = {
|
| 93 |
+
matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
|
| 94 |
+
}
|
chess-frontend/store/socket-provider.tsx
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"use client";
|
| 2 |
+
import { createContext, useContext, useEffect, useRef } from "react";
|
| 3 |
+
import { useWebSocket } from "@/hooks/use-websocket";
|
| 4 |
+
import { useGameStore } from "@/store/use-game-store";
|
| 5 |
+
import { useRouter, usePathname } from "next/navigation";
|
| 6 |
+
import { User } from "@/types/auth";
|
| 7 |
+
|
| 8 |
+
type SocketContextType = {
|
| 9 |
+
joinQueue: (timeControl: string) => void;
|
| 10 |
+
makeMove: (gameId: string, from: string, to: string, promotion?: string) => void;
|
| 11 |
+
resign: (gameId: string) => void;
|
| 12 |
+
};
|
| 13 |
+
|
| 14 |
+
const SocketContext = createContext<SocketContextType | null>(null);
|
| 15 |
+
|
| 16 |
+
export function SocketProvider({ user, children }: { user: User; children: React.ReactNode }) {
|
| 17 |
+
const router = useRouter();
|
| 18 |
+
const pathname = usePathname();
|
| 19 |
+
const { connect, joinQueue, makeMove, resign } = useWebSocket(user);
|
| 20 |
+
const activeGameId = useGameStore((s) => s.activeGame?.gameId);
|
| 21 |
+
const redirectedRef = useRef<string | null>(null);
|
| 22 |
+
useEffect(() => { connect(); }, [connect]);
|
| 23 |
+
|
| 24 |
+
useEffect(() => {
|
| 25 |
+
if (!activeGameId) return;
|
| 26 |
+
if (redirectedRef.current === activeGameId) return;
|
| 27 |
+
if (pathname === `/game/${activeGameId}`) return;
|
| 28 |
+
|
| 29 |
+
redirectedRef.current = activeGameId;
|
| 30 |
+
router.push(`/game/${activeGameId}`);
|
| 31 |
+
}, [activeGameId, pathname, router]);
|
| 32 |
+
|
| 33 |
+
return (
|
| 34 |
+
<SocketContext.Provider value={{ joinQueue, makeMove, resign }}>
|
| 35 |
+
{children}
|
| 36 |
+
</SocketContext.Provider>
|
| 37 |
+
);
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
export function useSocket() {
|
| 41 |
+
const ctx = useContext(SocketContext);
|
| 42 |
+
if (!ctx) throw new Error("useSocket must be used within SocketProvider");
|
| 43 |
+
return ctx;
|
| 44 |
+
}
|
chess-frontend/store/use-game-store.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { create } from 'zustand';
|
| 2 |
+
import {
|
| 3 |
+
ActiveGame, GameOverState, DrawOfferState, QueueStatus,
|
| 4 |
+
WsConnectionStatus, GameStatus, GameStartedPayload, GameStatePayload, MoveMadePayload
|
| 5 |
+
} from "@/types/chess";
|
| 6 |
+
import { User } from "@/types/auth"
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
interface GameStore {
|
| 10 |
+
connectionStatus: WsConnectionStatus;
|
| 11 |
+
user: User | null;
|
| 12 |
+
queueStatus: QueueStatus;
|
| 13 |
+
activeGame: ActiveGame | null;
|
| 14 |
+
gameOver: GameOverState | null;
|
| 15 |
+
drawOffer: DrawOfferState | null;
|
| 16 |
+
lastMoveRejectedReason: string | null;
|
| 17 |
+
showAnimations: boolean;
|
| 18 |
+
|
| 19 |
+
setConnection: (status: WsConnectionStatus) => void;
|
| 20 |
+
setUser: (user: User | null) => void;
|
| 21 |
+
setQueue: (status: QueueStatus) => void;
|
| 22 |
+
setAnimations: (enabled: boolean) => void;
|
| 23 |
+
handleGameStarted: (p: GameStartedPayload) => void;
|
| 24 |
+
handleGameState: (p: GameStatePayload) => void;
|
| 25 |
+
handleMoveMade: (p: MoveMadePayload) => void;
|
| 26 |
+
handleMoveRejected: (reason: string) => void;
|
| 27 |
+
handleGameOver: (p: any) => void;
|
| 28 |
+
resetGame: () => void;
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
export const useGameStore = create<GameStore>((set) => ({
|
| 32 |
+
connectionStatus: "idle",
|
| 33 |
+
user: null,
|
| 34 |
+
queueStatus: "idle",
|
| 35 |
+
activeGame: null,
|
| 36 |
+
gameOver: null,
|
| 37 |
+
drawOffer: null,
|
| 38 |
+
lastMoveRejectedReason: null,
|
| 39 |
+
showAnimations: true,
|
| 40 |
+
|
| 41 |
+
setConnection: (connectionStatus) => set({ connectionStatus }),
|
| 42 |
+
setUser: (user) => set({ user }),
|
| 43 |
+
setQueue: (queueStatus) => set({ queueStatus }),
|
| 44 |
+
setAnimations: (showAnimations) => set({ showAnimations }),
|
| 45 |
+
|
| 46 |
+
handleGameStarted: (p) => {
|
| 47 |
+
const [mins] = p.timeControl.split("+").map(Number);
|
| 48 |
+
const baseMs = mins * 60 * 1000;
|
| 49 |
+
set({
|
| 50 |
+
queueStatus: "idle",
|
| 51 |
+
gameOver: null,
|
| 52 |
+
activeGame: {
|
| 53 |
+
gameId: p.gameId,
|
| 54 |
+
fen: p.fen,
|
| 55 |
+
pgn: "",
|
| 56 |
+
turn: "w",
|
| 57 |
+
playerColor: p.playerColor,
|
| 58 |
+
whiteId: p.players.white,
|
| 59 |
+
blackId: p.players.black,
|
| 60 |
+
timeControl: p.timeControl,
|
| 61 |
+
whiteTimeMs: baseMs,
|
| 62 |
+
blackTimeMs: baseMs,
|
| 63 |
+
status: GameStatus.IN_PROGRESS,
|
| 64 |
+
},
|
| 65 |
+
});
|
| 66 |
+
},
|
| 67 |
+
|
| 68 |
+
handleGameState: (p) => set({
|
| 69 |
+
activeGame: {
|
| 70 |
+
gameId: p.gameId,
|
| 71 |
+
fen: p.fen,
|
| 72 |
+
pgn: p.pgn ?? "",
|
| 73 |
+
turn: p.turn,
|
| 74 |
+
playerColor: p.playerColor,
|
| 75 |
+
whiteId: p.whiteId,
|
| 76 |
+
blackId: p.blackId,
|
| 77 |
+
timeControl: p.timeControl,
|
| 78 |
+
whiteTimeMs: p.whiteTimeLeftMs,
|
| 79 |
+
blackTimeMs: p.blackTimeLeftMs,
|
| 80 |
+
status: GameStatus.IN_PROGRESS,
|
| 81 |
+
},
|
| 82 |
+
}),
|
| 83 |
+
|
| 84 |
+
handleMoveMade: (p) => set((state) => {
|
| 85 |
+
if (!state.activeGame || state.activeGame.gameId !== p.gameId) return state;
|
| 86 |
+
return {
|
| 87 |
+
activeGame: {
|
| 88 |
+
...state.activeGame,
|
| 89 |
+
fen: p.fen,
|
| 90 |
+
pgn: p.pgn,
|
| 91 |
+
turn: state.activeGame.turn === "w" ? "b" : "w",
|
| 92 |
+
whiteTimeMs: p.whiteTimeMs,
|
| 93 |
+
blackTimeMs: p.blackTimeMs,
|
| 94 |
+
}
|
| 95 |
+
};
|
| 96 |
+
}),
|
| 97 |
+
|
| 98 |
+
handleMoveRejected: (reason) => {
|
| 99 |
+
set({ lastMoveRejectedReason: reason });
|
| 100 |
+
setTimeout(() => set({ lastMoveRejectedReason: null }), 3000);
|
| 101 |
+
},
|
| 102 |
+
|
| 103 |
+
handleGameOver: (p) => set((state) => ({
|
| 104 |
+
activeGame: state.activeGame ? { ...state.activeGame, status: p.status } : null,
|
| 105 |
+
gameOver: { status: p.status, winnerId: p.winnerId, reason: p.reason },
|
| 106 |
+
drawOffer: null,
|
| 107 |
+
})),
|
| 108 |
+
|
| 109 |
+
resetGame: () => set({ activeGame: null, gameOver: null, queueStatus: "idle" }),
|
| 110 |
+
}));
|
chess-frontend/tsconfig.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"compilerOptions": {
|
| 3 |
+
"target": "ES2017",
|
| 4 |
+
"lib": ["dom", "dom.iterable", "esnext"],
|
| 5 |
+
"allowJs": true,
|
| 6 |
+
"skipLibCheck": true,
|
| 7 |
+
"strict": true,
|
| 8 |
+
"noEmit": true,
|
| 9 |
+
"esModuleInterop": true,
|
| 10 |
+
"module": "esnext",
|
| 11 |
+
"moduleResolution": "bundler",
|
| 12 |
+
"resolveJsonModule": true,
|
| 13 |
+
"isolatedModules": true,
|
| 14 |
+
"jsx": "react-jsx",
|
| 15 |
+
"incremental": true,
|
| 16 |
+
"plugins": [
|
| 17 |
+
{
|
| 18 |
+
"name": "next"
|
| 19 |
+
}
|
| 20 |
+
],
|
| 21 |
+
"paths": {
|
| 22 |
+
"@/*": ["./*"]
|
| 23 |
+
}
|
| 24 |
+
},
|
| 25 |
+
"include": [
|
| 26 |
+
"next-env.d.ts",
|
| 27 |
+
"**/*.ts",
|
| 28 |
+
"**/*.tsx",
|
| 29 |
+
".next/types/**/*.ts",
|
| 30 |
+
".next/dev/types/**/*.ts",
|
| 31 |
+
"**/*.mts"
|
| 32 |
+
],
|
| 33 |
+
"exclude": ["node_modules"]
|
| 34 |
+
}
|
chess-frontend/types/auth.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import * as z from "zod"
|
| 2 |
+
|
| 3 |
+
export const loginSchema = z.object({
|
| 4 |
+
|
| 5 |
+
email: z
|
| 6 |
+
.string({ error: "Email is required" })
|
| 7 |
+
.min(1, "Email is required")
|
| 8 |
+
.trim()
|
| 9 |
+
.toLowerCase()
|
| 10 |
+
.refine((val) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(val), {
|
| 11 |
+
error: "Must be a valid email address",
|
| 12 |
+
}),
|
| 13 |
+
password: z
|
| 14 |
+
.string({ error: "Password is required" })
|
| 15 |
+
.min(1, "Password is required")
|
| 16 |
+
.min(8, "Must be at least 8 characters")
|
| 17 |
+
.max(128, "Must not exceed 128 characters")
|
| 18 |
+
.regex(
|
| 19 |
+
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]/,
|
| 20 |
+
"Must contain uppercase, lowercase, number, and special character",
|
| 21 |
+
),
|
| 22 |
+
});
|
| 23 |
+
|
| 24 |
+
export type LoginInput = z.infer<typeof loginSchema>;
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
export type User = {
|
| 28 |
+
id: string;
|
| 29 |
+
email: string;
|
| 30 |
+
name: string;
|
| 31 |
+
username: string,
|
| 32 |
+
wins: number;
|
| 33 |
+
losses: number;
|
| 34 |
+
draws: number;
|
| 35 |
+
image?: string;
|
| 36 |
+
emailVerified: boolean;
|
| 37 |
+
createdAt: string;
|
| 38 |
+
updatedAt?: string;
|
| 39 |
+
|
| 40 |
+
};
|
| 41 |
+
|
| 42 |
+
export type AuthResponse = {
|
| 43 |
+
user: User;
|
| 44 |
+
session: {
|
| 45 |
+
token: string;
|
| 46 |
+
expiresAt: string;
|
| 47 |
+
};
|
| 48 |
+
};
|
| 49 |
+
|
| 50 |
+
export type LoginResponse = {
|
| 51 |
+
user: User;
|
| 52 |
+
token: string;
|
| 53 |
+
redirect: boolean;
|
| 54 |
+
};
|
chess-frontend/types/chess.ts
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export enum WsMessageType {
|
| 2 |
+
JOIN_QUEUE = "JOIN_QUEUE",
|
| 3 |
+
LEAVE_QUEUE = "LEAVE_QUEUE",
|
| 4 |
+
QUEUE_JOINED = "QUEUE_JOINED",
|
| 5 |
+
QUEUE_LEFT = "QUEUE_LEFT",
|
| 6 |
+
MAKE_MOVE = "MAKE_MOVE",
|
| 7 |
+
MOVE_MADE = "MOVE_MADE",
|
| 8 |
+
MOVE_ACCEPTED = "MOVE_ACCEPTED",
|
| 9 |
+
MOVE_REJECTED = "MOVE_REJECTED",
|
| 10 |
+
MATCHMAKING_TIMEOUT = "MATCHMAKING_TIMEOUT",
|
| 11 |
+
GAME_STARTED = "GAME_STARTED",
|
| 12 |
+
GAME_ENDED = "GAME_ENDED",
|
| 13 |
+
GAME_OVER = "GAME_OVER",
|
| 14 |
+
OFFER_DRAW = "OFFER_DRAW",
|
| 15 |
+
ACCEPT_DRAW = "ACCEPT_DRAW",
|
| 16 |
+
DECLINE_DRAW = "DECLINE_DRAW",
|
| 17 |
+
DRAW_OFFERED = "DRAW_OFFERED",
|
| 18 |
+
DRAW_DECLINED = "DRAW_DECLINED",
|
| 19 |
+
GAME_ABORTED = "GAME_ABORTED",
|
| 20 |
+
RESIGN_GAME = "RESIGN_GAME",
|
| 21 |
+
SYNC_GAME = "SYNC_GAME",
|
| 22 |
+
GAME_STATE = "GAME_STATE",
|
| 23 |
+
ERROR = "ERROR",
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
export enum GameStatus {
|
| 27 |
+
IN_PROGRESS = "IN_PROGRESS",
|
| 28 |
+
CHECKMATE = "CHECKMATE",
|
| 29 |
+
RESIGN = "RESIGN",
|
| 30 |
+
DRAW = "DRAW",
|
| 31 |
+
AGREEMENT = "AGREEMENT",
|
| 32 |
+
STALEMATE = "STALEMATE",
|
| 33 |
+
INSUFFICIENT_MATERIAL = "INSUFFICIENT_MATERIAL",
|
| 34 |
+
FIFTY_MOVE_RULE = "FIFTY_MOVE_RULE",
|
| 35 |
+
THREEFOLD_REPETITION = "THREEFOLD_REPETITION",
|
| 36 |
+
TIME_OUT = "TIME_OUT",
|
| 37 |
+
ABANDONED = "ABANDONED",
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
export type WsConnectionStatus =
|
| 41 |
+
| "idle"
|
| 42 |
+
| "connecting"
|
| 43 |
+
| "connected"
|
| 44 |
+
| "disconnected"
|
| 45 |
+
|
| 46 |
+
export type QueueStatus =
|
| 47 |
+
| "idle"
|
| 48 |
+
| "waiting"
|
| 49 |
+
|
| 50 |
+
export type PlayerColor = "w" | "b"
|
| 51 |
+
|
| 52 |
+
export interface ActiveGame {
|
| 53 |
+
gameId: string
|
| 54 |
+
fen: string
|
| 55 |
+
pgn: string
|
| 56 |
+
turn: PlayerColor
|
| 57 |
+
playerColor: PlayerColor
|
| 58 |
+
whiteId: string
|
| 59 |
+
blackId: string
|
| 60 |
+
timeControl: string
|
| 61 |
+
whiteTimeMs: number
|
| 62 |
+
blackTimeMs: number
|
| 63 |
+
status: GameStatus
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
export interface GameStartedPayload {
|
| 67 |
+
gameId: string
|
| 68 |
+
fen: string
|
| 69 |
+
timeControl: string
|
| 70 |
+
playerColor: PlayerColor
|
| 71 |
+
players: {
|
| 72 |
+
white: string
|
| 73 |
+
black: string
|
| 74 |
+
}
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
export interface GameStatePayload {
|
| 78 |
+
gameId: string
|
| 79 |
+
fen: string
|
| 80 |
+
pgn?: string
|
| 81 |
+
turn: PlayerColor
|
| 82 |
+
playerColor: PlayerColor
|
| 83 |
+
whiteId: string
|
| 84 |
+
blackId: string
|
| 85 |
+
timeControl: string
|
| 86 |
+
whiteTimeLeftMs: number
|
| 87 |
+
blackTimeLeftMs: number
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
export interface MoveMadePayload {
|
| 91 |
+
gameId: string
|
| 92 |
+
fen: string
|
| 93 |
+
pgn: string
|
| 94 |
+
move: string
|
| 95 |
+
turn: PlayerColor
|
| 96 |
+
whiteTimeMs: number
|
| 97 |
+
blackTimeMs: number
|
| 98 |
+
isGameOver: boolean
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
export interface GameOverState {
|
| 102 |
+
status: GameStatus
|
| 103 |
+
winnerId?: string
|
| 104 |
+
reason?: string
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
export interface DrawOfferState {
|
| 108 |
+
gameId: string
|
| 109 |
+
offeredBy: string
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
export type ServerMessage =
|
| 113 |
+
| { type: "GAME_STARTED"; payload: GameStartedPayload }
|
| 114 |
+
| { type: "GAME_STATE"; payload: GameStatePayload }
|
| 115 |
+
| { type: "MOVE_MADE"; payload: MoveMadePayload }
|
| 116 |
+
| { type: "MOVE_REJECTED"; payload: { reason: string } }
|
| 117 |
+
| { type: "GAME_OVER"; payload: GameOverState }
|
| 118 |
+
|
| 119 |
+
export enum GameResult {
|
| 120 |
+
d = "d",
|
| 121 |
+
w = "w",
|
| 122 |
+
b = "b",
|
| 123 |
+
}
|
chess-monolith
CHANGED
|
@@ -1 +1 @@
|
|
| 1 |
-
Subproject commit
|
|
|
|
| 1 |
+
Subproject commit 51b87b4a8e1932f5c61f0a0686bebdcbe5f6cd68
|