From 770ae1351758db990a30eb4b515db8d31b5656a5 Mon Sep 17 00:00:00 2001 From: GuillaumeSD Date: Sat, 17 Feb 2024 00:19:40 +0100 Subject: [PATCH] feat : init game eval --- package-lock.json | 186 ++++++++++-------- package.json | 4 +- .../stockfish => public/engines}/stockfish.js | 0 .../engines}/stockfish.wasm | Bin .../engines}/stockfish.wasm.js | 0 .../{stockfish/index.ts => stockfish.ts} | 34 +++- src/sections/index/index.state.ts | 4 + src/sections/index/reviewPanelBody.tsx | 28 ++- .../index/selectGame/gameOrigin.state.ts | 3 + src/sections/index/selectGame/inputGame.tsx | 21 ++ .../index/selectGame/selectGameOrigin.tsx | 9 +- src/types/eval.ts | 6 +- 12 files changed, 200 insertions(+), 95 deletions(-) rename {src/lib/engine/stockfish => public/engines}/stockfish.js (100%) rename {src/lib/engine/stockfish => public/engines}/stockfish.wasm (100%) rename {src/lib/engine/stockfish => public/engines}/stockfish.wasm.js (100%) rename src/lib/engine/{stockfish/index.ts => stockfish.ts} (79%) create mode 100644 src/sections/index/index.state.ts create mode 100644 src/sections/index/selectGame/gameOrigin.state.ts diff --git a/package-lock.json b/package-lock.json index cf72028..8993265 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,7 +8,9 @@ "name": "freechess", "version": "0.1.0", "dependencies": { - "next": "13.4.5", + "chess.js": "^1.0.0-beta.7", + "jotai": "^2.6.4", + "next": "13.5.6", "react": "18.2.0", "react-dom": "18.2.0" }, @@ -137,9 +139,9 @@ "dev": true }, "node_modules/@next/env": { - "version": "13.4.5", - "resolved": "https://registry.npmjs.org/@next/env/-/env-13.4.5.tgz", - "integrity": "sha512-SG/gKH6eij4vwQy87b/3mbpQ1X3x2vUdnpwq6/qL2IQWjtq58EY/UuNAp9CoEZoC9sI4L9AD1r+73Z9r4d3uug==" + "version": "13.5.6", + "resolved": "https://registry.npmjs.org/@next/env/-/env-13.5.6.tgz", + "integrity": "sha512-Yac/bV5sBGkkEXmAX5FWPS9Mmo2rthrOPRQQNfycJPkjUAUclomCPH7QFVCDQ4Mp2k2K1SSM6m0zrxYrOwtFQw==" }, "node_modules/@next/eslint-plugin-next": { "version": "13.4.5", @@ -151,9 +153,9 @@ } }, "node_modules/@next/swc-darwin-arm64": { - "version": "13.4.5", - "resolved": "https://registry.npmjs.org/@next/swc-darwin-arm64/-/swc-darwin-arm64-13.4.5.tgz", - "integrity": "sha512-XvTzi2ASUN5bECFIAAcBiSoDb0xsq+KLj4F0bof4d4rdc+FgOqLvseGQaOXwVi1TIh5bHa7o4b6droSJMO5+2g==", + "version": "13.5.6", + "resolved": "https://registry.npmjs.org/@next/swc-darwin-arm64/-/swc-darwin-arm64-13.5.6.tgz", + "integrity": "sha512-5nvXMzKtZfvcu4BhtV0KH1oGv4XEW+B+jOfmBdpFI3C7FrB/MfujRpWYSBBO64+qbW8pkZiSyQv9eiwnn5VIQA==", "cpu": [ "arm64" ], @@ -166,9 +168,9 @@ } }, "node_modules/@next/swc-darwin-x64": { - "version": "13.4.5", - "resolved": "https://registry.npmjs.org/@next/swc-darwin-x64/-/swc-darwin-x64-13.4.5.tgz", - "integrity": "sha512-NQdqal/VKAqlJTuzhjZmNtdo8QSqwmfO7b2xJSAengTEVxQvsH76oGEzQeIv8Ci4NP6DysAFtFrJq++TmIxcUA==", + "version": "13.5.6", + "resolved": "https://registry.npmjs.org/@next/swc-darwin-x64/-/swc-darwin-x64-13.5.6.tgz", + "integrity": "sha512-6cgBfxg98oOCSr4BckWjLLgiVwlL3vlLj8hXg2b+nDgm4bC/qVXXLfpLB9FHdoDu4057hzywbxKvmYGmi7yUzA==", "cpu": [ "x64" ], @@ -181,9 +183,9 @@ } }, "node_modules/@next/swc-linux-arm64-gnu": { - "version": "13.4.5", - "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-13.4.5.tgz", - "integrity": "sha512-nB8TjtpJCXtzIFjYOMbnQu68ajkA8QK58TreHjTGojSQjsF0StDqo5zFHglVVVHrd8d3N/+EjC18yFNSWnd/ZA==", + "version": "13.5.6", + "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-13.5.6.tgz", + "integrity": "sha512-txagBbj1e1w47YQjcKgSU4rRVQ7uF29YpnlHV5xuVUsgCUf2FmyfJ3CPjZUvpIeXCJAoMCFAoGnbtX86BK7+sg==", "cpu": [ "arm64" ], @@ -196,9 +198,9 @@ } }, "node_modules/@next/swc-linux-arm64-musl": { - "version": "13.4.5", - "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-13.4.5.tgz", - "integrity": "sha512-W126XUW599OV3giSH9Co40VpT8VAOT47xONVHXZaYEpeca0qEevjj6WUr5IJu/8u+XGWm5xI1S0DYWjR6W+olw==", + "version": "13.5.6", + "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-13.5.6.tgz", + "integrity": "sha512-cGd+H8amifT86ZldVJtAKDxUqeFyLWW+v2NlBULnLAdWsiuuN8TuhVBt8ZNpCqcAuoruoSWynvMWixTFcroq+Q==", "cpu": [ "arm64" ], @@ -211,9 +213,9 @@ } }, "node_modules/@next/swc-linux-x64-gnu": { - "version": "13.4.5", - "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-13.4.5.tgz", - "integrity": "sha512-ZbPLO/oztQdtjGmWvGhRmtkZ6j9kQqg65kiO7F7Ijj7ojTtu3hh/vY+XRsHa/4Cse6HgyJ8XGZJMGoLb8ecQfQ==", + "version": "13.5.6", + "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-13.5.6.tgz", + "integrity": "sha512-Mc2b4xiIWKXIhBy2NBTwOxGD3nHLmq4keFk+d4/WL5fMsB8XdJRdtUlL87SqVCTSaf1BRuQQf1HvXZcy+rq3Nw==", "cpu": [ "x64" ], @@ -226,9 +228,9 @@ } }, "node_modules/@next/swc-linux-x64-musl": { - "version": "13.4.5", - "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-13.4.5.tgz", - "integrity": "sha512-f+/h8KMNixVUoRB+2vza8I+jsthJ4KcvopGUsDIUHe7Q4t+m8nKwGFBeyNu9qNIenYK5g5QYEsSwYFEqZylrTQ==", + "version": "13.5.6", + "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-13.5.6.tgz", + "integrity": "sha512-CFHvP9Qz98NruJiUnCe61O6GveKKHpJLloXbDSWRhqhkJdZD2zU5hG+gtVJR//tyW897izuHpM6Gtf6+sNgJPQ==", "cpu": [ "x64" ], @@ -241,9 +243,9 @@ } }, "node_modules/@next/swc-win32-arm64-msvc": { - "version": "13.4.5", - "resolved": "https://registry.npmjs.org/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-13.4.5.tgz", - "integrity": "sha512-dvtPQZ5+J+zUE1uq7gP853Oj63e+n0T1ydZ/yRdVh7d8zW9ZFuC9fFrg3MqP1cv1NPPur8rrTqDKN2mRBkSSBw==", + "version": "13.5.6", + "resolved": "https://registry.npmjs.org/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-13.5.6.tgz", + "integrity": "sha512-aFv1ejfkbS7PUa1qVPwzDHjQWQtknzAZWGTKYIAaS4NMtBlk3VyA6AYn593pqNanlicewqyl2jUhQAaFV/qXsg==", "cpu": [ "arm64" ], @@ -256,9 +258,9 @@ } }, "node_modules/@next/swc-win32-ia32-msvc": { - "version": "13.4.5", - "resolved": "https://registry.npmjs.org/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-13.4.5.tgz", - "integrity": "sha512-gK9zwGe25x31S4AjPy3Bf2niQvHIAbmwgkzmqWG3OmD4K2Z/Dh2ju4vuyzPzIt0pwQe4B520meP9NizTBmVWSg==", + "version": "13.5.6", + "resolved": "https://registry.npmjs.org/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-13.5.6.tgz", + "integrity": "sha512-XqqpHgEIlBHvzwG8sp/JXMFkLAfGLqkbVsyN+/Ih1mR8INb6YCc2x/Mbwi6hsAgUnqQztz8cvEbHJUbSl7RHDg==", "cpu": [ "ia32" ], @@ -271,9 +273,9 @@ } }, "node_modules/@next/swc-win32-x64-msvc": { - "version": "13.4.5", - "resolved": "https://registry.npmjs.org/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-13.4.5.tgz", - "integrity": "sha512-iyNQVc7eGehrik9RJt9xGcnO6b/pi8C7GCfg8RGenx1IlalEKbYRgBJloF7DQzwlrV47E9bQl8swT+JawaNcKA==", + "version": "13.5.6", + "resolved": "https://registry.npmjs.org/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-13.5.6.tgz", + "integrity": "sha512-Cqfe1YmOS7k+5mGu92nl5ULkzpKuxJrP3+4AEuPmrpFZ3BHxTY3TnHmU1On3bFmFFs6FbTcdF58CCUProGpIGQ==", "cpu": [ "x64" ], @@ -347,9 +349,9 @@ "dev": true }, "node_modules/@swc/helpers": { - "version": "0.5.1", - "resolved": "https://registry.npmjs.org/@swc/helpers/-/helpers-0.5.1.tgz", - "integrity": "sha512-sJ902EfIzn1Fa+qYmjdQqh8tPsoxyBz+8yBKC2HKUxyezKJFwPGOn7pv4WY6QuQW//ySQi5lJjA/ZT9sNWWNTg==", + "version": "0.5.2", + "resolved": "https://registry.npmjs.org/@swc/helpers/-/helpers-0.5.2.tgz", + "integrity": "sha512-E4KcWTpoLHqwPHLxidpOqQbcrZVgi0rsmmZXUle1jXmJfuIf/UWpczUJ7MZZ5tlxytgJXyp0w4PGkkeLiuIdZw==", "dependencies": { "tslib": "^2.4.0" } @@ -376,13 +378,13 @@ "version": "15.7.5", "resolved": "https://registry.npmjs.org/@types/prop-types/-/prop-types-15.7.5.tgz", "integrity": "sha512-JCB8C6SnDoQf0cNycqd/35A7MjcnK+ZTqE7judS6o7utxUCg6imJg3QK2qzHKszlTjcj2cn+NwMB2i96ubpj7w==", - "dev": true + "devOptional": true }, "node_modules/@types/react": { "version": "18.2.11", "resolved": "https://registry.npmjs.org/@types/react/-/react-18.2.11.tgz", "integrity": "sha512-+hsJr9hmwyDecSMQAmX7drgbDpyE+EgSF6t7+5QEBAn1tQK7kl1vWZ4iRf6SjQ8lk7dyEULxUmZOIpN0W5baZA==", - "dev": true, + "devOptional": true, "dependencies": { "@types/prop-types": "*", "@types/scheduler": "*", @@ -402,7 +404,7 @@ "version": "0.16.3", "resolved": "https://registry.npmjs.org/@types/scheduler/-/scheduler-0.16.3.tgz", "integrity": "sha512-5cJ8CB4yAx7BH1oMvdU0Jh9lrEXyPkar6F9G/ERswkCuvP4KQZfZkSjcMbAICCpQTN4OuZn8tz0HiKv9TGZgrQ==", - "dev": true + "devOptional": true }, "node_modules/@types/semver": { "version": "7.5.0", @@ -954,6 +956,11 @@ "url": "https://github.com/chalk/chalk?sponsor=1" } }, + "node_modules/chess.js": { + "version": "1.0.0-beta.7", + "resolved": "https://registry.npmjs.org/chess.js/-/chess.js-1.0.0-beta.7.tgz", + "integrity": "sha512-2qG/f3M9Lil/hBey2uoJy9zhNr1BWIXZngRUJFt7j1QqS5Y5Ywq8Rd3l7ZBCbyJjhsLhxTcVhv6xy7BfjC19Dw==" + }, "node_modules/client-only": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/client-only/-/client-only-0.0.1.tgz", @@ -1001,7 +1008,7 @@ "version": "3.1.2", "resolved": "https://registry.npmjs.org/csstype/-/csstype-3.1.2.tgz", "integrity": "sha512-I7K1Uu0MBPzaFKg4nI5Q7Vs2t+3gWWW648spaF+Rg7pI9ds18Ugn+lvg4SHczUdKlHI5LWBXyqfS8+DufyBsgQ==", - "dev": true + "devOptional": true }, "node_modules/damerau-levenshtein": { "version": "1.0.8", @@ -1534,9 +1541,9 @@ } }, "node_modules/eslint-plugin-import/node_modules/semver": { - "version": "6.3.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz", - "integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==", + "version": "6.3.1", + "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz", + "integrity": "sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==", "dev": true, "bin": { "semver": "bin/semver.js" @@ -1573,9 +1580,9 @@ } }, "node_modules/eslint-plugin-jsx-a11y/node_modules/semver": { - "version": "6.3.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz", - "integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==", + "version": "6.3.1", + "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz", + "integrity": "sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==", "dev": true, "bin": { "semver": "bin/semver.js" @@ -1673,9 +1680,9 @@ } }, "node_modules/eslint-plugin-react/node_modules/semver": { - "version": "6.3.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz", - "integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==", + "version": "6.3.1", + "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz", + "integrity": "sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==", "dev": true, "bin": { "semver": "bin/semver.js" @@ -2660,6 +2667,26 @@ "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==", "dev": true }, + "node_modules/jotai": { + "version": "2.6.4", + "resolved": "https://registry.npmjs.org/jotai/-/jotai-2.6.4.tgz", + "integrity": "sha512-RniwQPX4893YlNR1muOtyUGHYaTD1fhEN4qnOuZJSrDHj6xdEMrqlRSN/hCm2fshwk78ruecB/P2l+NCVWe6TQ==", + "engines": { + "node": ">=12.20.0" + }, + "peerDependencies": { + "@types/react": ">=17.0.0", + "react": ">=17.0.0" + }, + "peerDependenciesMeta": { + "@types/react": { + "optional": true + }, + "react": { + "optional": true + } + } + }, "node_modules/js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", @@ -2854,9 +2881,9 @@ "dev": true }, "node_modules/nanoid": { - "version": "3.3.6", - "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.6.tgz", - "integrity": "sha512-BGcqMMJuToF7i1rt+2PWSNVnWIkGCU78jBG3RxO/bZlnZPK2Cmi2QaffxGO/2RvWi9sL+FAiRiXMgsyxQ1DIDA==", + "version": "3.3.7", + "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.7.tgz", + "integrity": "sha512-eSRppjcPIatRIMC1U6UngP8XFcz8MQWGQdt1MTBQ7NaAmvXDfvNxbvWV3x2y6CdEUciCSsDHDQZbhYaB8QEo2g==", "funding": [ { "type": "github", @@ -2883,39 +2910,37 @@ "dev": true }, "node_modules/next": { - "version": "13.4.5", - "resolved": "https://registry.npmjs.org/next/-/next-13.4.5.tgz", - "integrity": "sha512-pfNsRLVM9e5Y1/z02VakJRfD6hMQkr24FaN2xc9GbcZDBxoOgiNAViSg5cXwlWCoMhtm4U315D7XYhgOr96Q3Q==", + "version": "13.5.6", + "resolved": "https://registry.npmjs.org/next/-/next-13.5.6.tgz", + "integrity": "sha512-Y2wTcTbO4WwEsVb4A8VSnOsG1I9ok+h74q0ZdxkwM3EODqrs4pasq7O0iUxbcS9VtWMicG7f3+HAj0r1+NtKSw==", "dependencies": { - "@next/env": "13.4.5", - "@swc/helpers": "0.5.1", + "@next/env": "13.5.6", + "@swc/helpers": "0.5.2", "busboy": "1.6.0", "caniuse-lite": "^1.0.30001406", - "postcss": "8.4.14", + "postcss": "8.4.31", "styled-jsx": "5.1.1", - "watchpack": "2.4.0", - "zod": "3.21.4" + "watchpack": "2.4.0" }, "bin": { "next": "dist/bin/next" }, "engines": { - "node": ">=16.8.0" + "node": ">=16.14.0" }, "optionalDependencies": { - "@next/swc-darwin-arm64": "13.4.5", - "@next/swc-darwin-x64": "13.4.5", - "@next/swc-linux-arm64-gnu": "13.4.5", - "@next/swc-linux-arm64-musl": "13.4.5", - "@next/swc-linux-x64-gnu": "13.4.5", - "@next/swc-linux-x64-musl": "13.4.5", - "@next/swc-win32-arm64-msvc": "13.4.5", - "@next/swc-win32-ia32-msvc": "13.4.5", - "@next/swc-win32-x64-msvc": "13.4.5" + "@next/swc-darwin-arm64": "13.5.6", + "@next/swc-darwin-x64": "13.5.6", + "@next/swc-linux-arm64-gnu": "13.5.6", + "@next/swc-linux-arm64-musl": "13.5.6", + "@next/swc-linux-x64-gnu": "13.5.6", + "@next/swc-linux-x64-musl": "13.5.6", + "@next/swc-win32-arm64-msvc": "13.5.6", + "@next/swc-win32-ia32-msvc": "13.5.6", + "@next/swc-win32-x64-msvc": "13.5.6" }, "peerDependencies": { "@opentelemetry/api": "^1.1.0", - "fibers": ">= 3.1.0", "react": "^18.2.0", "react-dom": "^18.2.0", "sass": "^1.3.0" @@ -2924,9 +2949,6 @@ "@opentelemetry/api": { "optional": true }, - "fibers": { - "optional": true - }, "sass": { "optional": true } @@ -3242,9 +3264,9 @@ } }, "node_modules/postcss": { - "version": "8.4.14", - "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.4.14.tgz", - "integrity": "sha512-E398TUmfAYFPBSdzgeieK2Y1+1cpdxJx8yXbK/m57nRhKSmk1GB2tO4lbLBtlkfPQTDKfe4Xqv1ASWPpayPEig==", + "version": "8.4.31", + "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.4.31.tgz", + "integrity": "sha512-PS08Iboia9mts/2ygV3eLpY5ghnUcfLV/EXTOW1E2qYxJKGGBUtNjN76FYHnMs36RmARn41bC0AZmn+rR0OVpQ==", "funding": [ { "type": "opencollective", @@ -3253,10 +3275,14 @@ { "type": "tidelift", "url": "https://tidelift.com/funding/github/npm/postcss" + }, + { + "type": "github", + "url": "https://github.com/sponsors/ai" } ], "dependencies": { - "nanoid": "^3.3.4", + "nanoid": "^3.3.6", "picocolors": "^1.0.0", "source-map-js": "^1.0.2" }, @@ -4128,14 +4154,6 @@ "funding": { "url": "https://github.com/sponsors/sindresorhus" } - }, - "node_modules/zod": { - "version": "3.21.4", - "resolved": "https://registry.npmjs.org/zod/-/zod-3.21.4.tgz", - "integrity": "sha512-m46AKbrzKVzOzs/DZgVnG5H55N1sv1M8qZU3A8RIKbs3mrACDNeIOeilDymVb2HdmP8uwshOCF4uJ8uM9rCqJw==", - "funding": { - "url": "https://github.com/sponsors/colinhacks" - } } } } diff --git a/package.json b/package.json index e9c6098..f5a66ca 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,9 @@ "deploy": "firebase deploy --project=freechess --only hosting" }, "dependencies": { - "next": "13.4.5", + "chess.js": "^1.0.0-beta.7", + "jotai": "^2.6.4", + "next": "13.5.6", "react": "18.2.0", "react-dom": "18.2.0" }, diff --git a/src/lib/engine/stockfish/stockfish.js b/public/engines/stockfish.js similarity index 100% rename from src/lib/engine/stockfish/stockfish.js rename to public/engines/stockfish.js diff --git a/src/lib/engine/stockfish/stockfish.wasm b/public/engines/stockfish.wasm similarity index 100% rename from src/lib/engine/stockfish/stockfish.wasm rename to public/engines/stockfish.wasm diff --git a/src/lib/engine/stockfish/stockfish.wasm.js b/public/engines/stockfish.wasm.js similarity index 100% rename from src/lib/engine/stockfish/stockfish.wasm.js rename to public/engines/stockfish.wasm.js diff --git a/src/lib/engine/stockfish/index.ts b/src/lib/engine/stockfish.ts similarity index 79% rename from src/lib/engine/stockfish/index.ts rename to src/lib/engine/stockfish.ts index a2fab78..5b2f3f4 100644 --- a/src/lib/engine/stockfish/index.ts +++ b/src/lib/engine/stockfish.ts @@ -1,14 +1,16 @@ +import { GameEval, MoveEval } from "@/types/eval"; + export class Stockfish { private worker: Worker; + private ready: boolean = false; constructor() { this.worker = new Worker( - this.isWasmSupported() ? "./stockfish.wasm.js" : "./stockfish.js" + this.isWasmSupported() + ? "engines/stockfish.wasm.js" + : "engines/stockfish.js" ); - this.sendCommands(["uci"], "uciok"); - this.sendCommands(["setoption name MultiPV value 2", "isready"], "readyok"); - console.log("Stockfish created"); } @@ -21,6 +23,25 @@ export class Stockfish { ); } + public async init(): Promise { + await this.sendCommands(["uci"], "uciok"); + await this.sendCommands( + ["setoption name MultiPV value 2", "isready"], + "readyok" + ); + this.ready = true; + } + + public shutdown(): void { + this.ready = false; + this.worker.postMessage("quit"); + this.worker.terminate(); + } + + public isReady(): boolean { + return this.ready; + } + private async sendCommands( commands: string[], finalMessage: string @@ -42,15 +63,20 @@ export class Stockfish { } public async evaluateGame(fens: string[], depth = 16): Promise { + this.ready = false; + console.log("Evaluating game"); await this.sendCommands(["ucinewgame", "isready"], "readyok"); this.worker.postMessage("position startpos"); const moves: MoveEval[] = []; for (const fen of fens) { + console.log(`Evaluating position: ${fen}`); const result = await this.evaluatePosition(fen, depth); moves.push(result); } + this.ready = true; + console.log("Game evaluated"); return { moves }; } diff --git a/src/sections/index/index.state.ts b/src/sections/index/index.state.ts new file mode 100644 index 0000000..010a40f --- /dev/null +++ b/src/sections/index/index.state.ts @@ -0,0 +1,4 @@ +import { GameEval } from "@/types/eval"; +import { atom } from "jotai"; + +export const gameReviewAtom = atom(undefined); diff --git a/src/sections/index/reviewPanelBody.tsx b/src/sections/index/reviewPanelBody.tsx index 51c7249..a674028 100644 --- a/src/sections/index/reviewPanelBody.tsx +++ b/src/sections/index/reviewPanelBody.tsx @@ -1,8 +1,34 @@ +import { useEffect, useState } from "react"; import ReviewResult from "./reviewResult"; import SelectDepth from "./selectDepth"; import SelectGameOrigin from "./selectGame/selectGameOrigin"; +import { Stockfish } from "@/lib/engine/stockfish"; +import { useAtomValue } from "jotai"; +import { gameFensAtom } from "./selectGame/gameOrigin.state"; export default function ReviewPanelBody() { + const [engine, setEngine] = useState(null); + const gameFens = useAtomValue(gameFensAtom); + + useEffect(() => { + const engine = new Stockfish(); + engine.init().then(() => { + console.log("Engine initialized"); + }); + setEngine(engine); + + return () => { + engine.shutdown(); + console.log("Engine shutdown"); + }; + }, []); + + const handleAnalyse = () => { + if (engine?.isReady() && gameFens) { + engine.evaluateGame(gameFens); + } + }; + return (

@@ -13,7 +39,7 @@ export default function ReviewPanelBody() { diff --git a/src/sections/index/selectGame/gameOrigin.state.ts b/src/sections/index/selectGame/gameOrigin.state.ts new file mode 100644 index 0000000..b18485b --- /dev/null +++ b/src/sections/index/selectGame/gameOrigin.state.ts @@ -0,0 +1,3 @@ +import { atom } from "jotai"; + +export const gameFensAtom = atom([]); diff --git a/src/sections/index/selectGame/inputGame.tsx b/src/sections/index/selectGame/inputGame.tsx index d295f70..6bf3a12 100644 --- a/src/sections/index/selectGame/inputGame.tsx +++ b/src/sections/index/selectGame/inputGame.tsx @@ -1,8 +1,27 @@ +import { GameOrigin } from "@/types/enums"; +import { useSetAtom } from "jotai"; +import { gameFensAtom } from "./gameOrigin.state"; +import { useEffect, useState } from "react"; +import { Chess } from "chess.js"; + interface Props { + gameOrigin: GameOrigin; placeholder?: string; } export default function InputGame({ placeholder }: Props) { + const [gamePgn, setGamePgn] = useState(""); + const setGameFens = useSetAtom(gameFensAtom); + + useEffect(() => { + const chess = new Chess(); + chess.loadPgn(gamePgn); + const fens = chess.history({ verbose: true }).map((move) => { + return move.after; + }); + setGameFens(fens); + }, [gamePgn]); + return (