Skip to content

Commit

Permalink
refactor: split web app
Browse files Browse the repository at this point in the history
### Description

- Split web app & create a `chessboard` package
  • Loading branch information
Neosoulink committed Oct 19, 2024
1 parent 716d3c8 commit 6cc487a
Show file tree
Hide file tree
Showing 63 changed files with 210 additions and 38 deletions.
9 changes: 9 additions & 0 deletions .changeset/eighty-ligers-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"web": minor
"@chess-d/chessboard": patch
---

# Logs

- refactor: split web app
- Split web app & create a `chessboard` package
2 changes: 1 addition & 1 deletion .changeset/many-walls-work.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
"web": patch
"@chess-d/chessboard": patch
---

# Logs
Expand Down
22 changes: 11 additions & 11 deletions apps/web/package.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
{
"name": "web",
"private": true,
"version": "0.1.1",
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite --clearScreen false --host",
"build": "lint && vite build",
"build": "pnpm run lint && vite build",
"preview": "vite preview",
"lint": "tsc && eslint \"src/**/*.ts\"",
"test": "jest --passWithNoTests"
},
"devDependencies": {
"@chess-d/configs": "workspace:*",
"@types/three": "^0.166.0",
"reflect-metadata": "^0.2.0",
"tsyringe": "^4.8.0"
},
"dependencies": {
"@chess-d/rapier-physics": "workspace:*",
"@chess-d/ai": "workspace:*",
"@chess-d/chessboard": "workspace:*",
"@quick-threejs/reactive": "^0.1.14",
"@quick-threejs/utils": "^0.1.9",
"chess.js": "1.0.0-beta.8",
"rxjs": "^7.8.1",
"threads": "^1.7.0",
"three": "^0.166.1",
"typescript-ioc": "^3.2.2"
"three": "^0.166.1"
},
"devDependencies": {
"@chess-d/configs": "workspace:*",
"@types/three": "^0.166.0",
"reflect-metadata": "^0.2.0",
"tsyringe": "^4.8.0"
}
}
28 changes: 28 additions & 0 deletions apps/web/src/ai.worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { expose } from "threads/worker";
import { Subject } from "rxjs";
import { Chess } from "chess.js";
import { register, SupportedAiModel } from "@chess-d/ai";
import { AppLifecycleState } from "@quick-threejs/reactive";
import { ExposedAppModule } from "@quick-threejs/reactive/worker";

const lifecycle$$ = new Subject<AppLifecycleState>();
const lifecycle$ = lifecycle$$.pipe();

export const performAI = () => {
console.log("AI is performing...");

const game = new Chess();
const aiModel = register(SupportedAiModel.zeyu, game);

const bestMove = aiModel?.getMove(game.turn());

if (bestMove) game.move(bestMove);

console.log("Best move:", bestMove);
};

performAI();

expose({
lifecycle$: () => lifecycle$
} satisfies ExposedAppModule);
29 changes: 22 additions & 7 deletions apps/web/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
import "reflect-metadata";

import { register } from "@quick-threejs/reactive";
import { register, RegisterModule } from "@quick-threejs/reactive";
import { GUI } from "three/examples/jsm/libs/lil-gui.module.min.js";

import "./assets/styles/main.css";

const location = new URL(
"./main.worker.ts",
import.meta.url
) as unknown as string;
const enableDebug = !!import.meta.env?.DEV;

const onReady = (app: RegisterModule) => {
const gui = app.gui() as GUI | undefined;
gui?.close();

app.workerPool().run({
payload: {
path: new URL("./ai.worker.ts", import.meta.url) as unknown as string,
subject: {}
}
});
};

register({
location: new URL("./main.worker.ts", import.meta.url) as unknown as string,
enableDebug: !!import.meta.env?.DEV,
location,
enableDebug,
axesSizes: 5,
gridSizes: 10,
withMiniCamera: true,
onReady: (app) => {
const gui = app.gui() as GUI | undefined;
gui?.close();
}
onReady
});
7 changes: 2 additions & 5 deletions apps/web/src/main.worker.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import "reflect-metadata";

import { launchApp } from "@quick-threejs/reactive/worker";
import { setup as onReady } from "@chess-d/chessboard";

import { setupCoreModule } from "./core/core.util";

launchApp({
onReady: (app) => setupCoreModule(app)
});
launchApp({ onReady });
File renamed without changes.
27 changes: 27 additions & 0 deletions packages/chessboard/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import path from "node:path";
import { fileURLToPath } from "node:url";
import js from "@eslint/js";
import { FlatCompat } from "@eslint/eslintrc";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
allConfig: js.configs.all
});

/** @type {import("eslint").Linter.Config} */
const configs = {
files: ["./src/**/*.js?(x)", "./src/**/*.ts?(x)"],
languageOptions: {
parserOptions: {
project: true
}
}
};

export default [
...compat.extends("./node_modules/@chess-d/configs/eslint/base.js"),
configs
];
6 changes: 6 additions & 0 deletions packages/chessboard/jest.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import configs from "@chess-d/configs/jest/base.js";

/** @type {import('jest').Config} */
export default {
...configs
};
34 changes: 34 additions & 0 deletions packages/chessboard/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@chess-d/chessboard",
"private": true,
"version": "0.1.1",
"type": "module",
"scripts": {
"dev": "tsc --watch",
"build": "tsc",
"test": "jest --passWithNoTests"
},
"main": "./dist/main.js",
"types": "./dist/main.d.ts",
"dependencies": {
"chess.js": "1.0.0-beta.8",
"@quick-threejs/utils": "^0.1.9",
"@chess-d/rapier-physics": "workspace:*",
"@quick-threejs/reactive": "^0.1.14",
"rxjs": "^7.8.1",
"threads": "^1.7.0",
"three": "^0.166.1",
"typescript-ioc": "^3.2.2"
},
"devDependencies": {
"@chess-d/configs": "workspace:*",
"@types/three": "^0.166.0",
"reflect-metadata": "^0.2.0",
"tsyringe": "^4.8.0",
"typescript": "^5.5.3"
},
"keywords": [
"chess",
"chessboard"
]
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Physics, RapierPhysics } from "@chess-d/rapier-physics";
import { CoreModule } from "./core.module";
import { Chess } from "chess.js";

export const setupCoreModule = async (app: AppModule) => {
export const setup = async (app: AppModule) => {
if (!isObject(app))
throw new Error("Unable to retrieve the application context.");

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions packages/chessboard/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./core/core.util";
export * from "./shared";
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
ENGINE_SQUARE_KEY_NUMBERS,
ENGINE_SQUARE_NUMBER_KEYS
} from "../constants";
import { ColorVariant, PieceType } from "../enums";
import { ColorVariant } from "../enums";
import { BoardCoord } from "../interfaces";

/**
Expand Down Expand Up @@ -35,8 +35,6 @@ export const squareToCoord = (square: Square): BoardCoord => {
export const getPieceSymbolColor = (
piece: PieceSymbol | Capitalize<PieceSymbol>
) => {
if (!PieceType[piece.toLowerCase()]) return undefined;

if (piece === piece.toLowerCase()) return ColorVariant.black;

return ColorVariant.white;
Expand Down
File renamed without changes.
13 changes: 13 additions & 0 deletions packages/chessboard/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "@chess-d/configs/typescript/base.json",
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true,

"isolatedModules": false,

"outDir": "./dist"
},
"include": ["src"],
"exclude": ["*.config.cjs", "*.config.js", "*.config.ts"]
}
63 changes: 53 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6cc487a

Please sign in to comment.