forced game clear each turn

This commit is contained in:
2ManyProjects 2025-03-18 00:06:03 -05:00
parent e74f767cdb
commit 4d48ad2414
3 changed files with 15 additions and 111 deletions

View file

@ -65,11 +65,10 @@ try {
const app = express(); const app = express();
const port = 27531; const port = 27531;
let board = null;
let engine = null; let engine = null;
let isReady = false; let isReady = false;
let lastResponse = null let lastResponse = null
const SERVER_WAIT_THRESHOLD = 2 * 60 * 1000; const SERVER_WAIT_THRESHOLD = 1 * 30 * 1000;
const CHECK_INTERVAL = 5000; const CHECK_INTERVAL = 5000;
// Initialize ffish and engine // Initialize ffish and engine
@ -167,7 +166,6 @@ app.post('/validate', (req, res) => {
} }
try { try {
// Create temporary board to validate FEN
const tempBoard = new ffish.Board(variant); const tempBoard = new ffish.Board(variant);
const isValid = tempBoard.setFen(fen); const isValid = tempBoard.setFen(fen);
tempBoard.delete(); tempBoard.delete();
@ -188,21 +186,10 @@ app.post('/new', async (req, res) => {
const { variant = 'chess' } = req.body; const { variant = 'chess' } = req.body;
try { try {
if (board) {
board.delete();
}
board = new ffish.Board(variant);
const engineAnalysis = await engine.createNewGame(); const engineAnalysis = await engine.createNewGame();
res.json({ res.json({
status: 'ok', status: 'ok',
fen: board.fen(),
legalMoves: board.legalMoves().split(' '),
legalMovesSan: board.legalMovesSan().split(' '),
isCheck: board.isCheck(),
turn: board.turn(),
moveStack: board.moveStack()
}); });
} catch (error) { } catch (error) {
res.status(500).json({ error: error.message }); res.status(500).json({ error: error.message });
@ -219,35 +206,16 @@ app.post('/position', async(req, res) => {
//we have a lot of funky rules lets not validate //we have a lot of funky rules lets not validate
// const isValid = ffish.validateFen(fen) == 1 // const isValid = ffish.validateFen(fen) == 1
// if (!isValid) {
// return res.status(400).json({ error: 'Invalid FEN string',
// fen,
// variant,
// boardExists: !!board, reqBody: JSON.stringify(req.body) });
// }else {
// // if (board) {
// // board.delete();
// // }
// board.setFen(fen);
// }
if(start){ if(start){
await engine.startPos() await engine.startPos()
engine.sendCommand('d'); engine.sendCommand('d');
}else if(fen){ }else if(fen){
board.setFen(fen);
await engine.setBoardPosition(fen) await engine.setBoardPosition(fen)
} }
res.json({ res.json({
status: 'ok', status: 'ok',
msg: "Set Position", msg: "Set Position",
legalMoves: board.legalMoves().split(' '),
legalMovesSan: board.legalMovesSan().split(' '),
isCheck: board.isCheck(),
isGameOver: board.isGameOver(),
result: board.result(),
turn: board.turn(),
moveStack: board.moveStack()
}); });
} catch (error) { } catch (error) {
res.status(500).json({ error: error.message, poserr: true }); res.status(500).json({ error: error.message, poserr: true });
@ -259,49 +227,16 @@ app.post('/move', (req, res) => {
lastResponse = new Date().getTime() lastResponse = new Date().getTime()
const { move, notation = 'uci', variant = 'chess' } = req.body; const { move, notation = 'uci', variant = 'chess' } = req.body;
if (!board) {
return res.status(400).json({ error: 'No active board' });
}
if (!move) { if (!move) {
return res.status(400).json({ error: 'Move required' }); return res.status(400).json({ error: 'Move required' });
} }
if (board) {
board.delete();
}
board = new ffish.Board(variant);
try { try {
if (notation === 'san') {
board.pushSan(move);
} else {
board.push(move);
}
const response = { const response = {
status: 'ok', status: 'ok',
fen: board.fen(),
legalMoves: board.legalMoves().split(' '),
legalMovesSan: board.legalMovesSan().split(' '),
isCheck: board.isCheck(),
isGameOver: board.isGameOver(),
result: board.result(),
turn: board.turn(),
moveStack: board.moveStack(),
fullmoveNumber: board.fullmoveNumber(),
halfmoveClock: board.halfmoveClock()
}; };
if (board.isGameOver()) {
response.hasInsufficientMaterial = {
white: board.hasInsufficientMaterial(true), // true for white
black: board.hasInsufficientMaterial(false) // false for black
};
response.gameResult = board.result();
}
res.json(response); res.json(response);
} catch (error) { } catch (error) {
res.status(500).json({ error: error.message }); res.status(500).json({ error: error.message });
@ -311,25 +246,9 @@ app.post('/move', (req, res) => {
// State endpoint // State endpoint
app.get('/state', (req, res) => { app.get('/state', (req, res) => {
lastResponse = new Date().getTime() lastResponse = new Date().getTime()
if (!board) {
return res.status(400).json({ error: 'No active board' });
}
try { try {
res.json({ res.json({
status: 'ok', status: 'ok',
fen: board.fen(),
legalMoves: board.legalMoves().split(' '),
legalMovesSan: board.legalMovesSan().split(' '),
isCheck: board.isCheck(),
isGameOver: board.isGameOver(),
result: board.result(),
turn: board.turn(),
moveStack: board.moveStack(),
fullmoveNumber: board.fullmoveNumber(),
halfmoveClock: board.halfmoveClock(),
variant: board.variant(),
is960: board.is960()
}); });
} catch (error) { } catch (error) {
res.status(500).json({ error: error.message }); res.status(500).json({ error: error.message });
@ -363,9 +282,6 @@ app.post('/newgame', async (req, res) => {
// Analysis endpoint // Analysis endpoint
app.post('/analyze', async (req, res) => { app.post('/analyze', async (req, res) => {
lastResponse = new Date().getTime() lastResponse = new Date().getTime()
if (!board) {
return res.status(400).json({ error: 'No active board' });
}
try { try {
const { depth = 15, movetime = 1000 } = req.body; const { depth = 15, movetime = 1000 } = req.body;
@ -373,22 +289,11 @@ app.post('/analyze', async (req, res) => {
// Get basic position analysis // Get basic position analysis
const positionAnalysis = { const positionAnalysis = {
status: 'ok', status: 'ok',
fen: board.fen(),
isCheck: board.isCheck(),
isGameOver: board.isGameOver(),
result: board.result(),
hasInsufficientMaterial: {
white: board.hasInsufficientMaterial(true), // true for white
black: board.hasInsufficientMaterial(false) // false for black
},
legalMoves: board.legalMoves().split(' '),
legalMovesSan: board.legalMovesSan().split(' '),
moveStack: board.moveStack()
}; };
// If engine is available, get engine analysis // If engine is available, get engine analysis
if (engine && engine.isReady) { if (engine && engine.isReady) {
const engineAnalysis = await engine.getAnalysis(board.fen(), { const engineAnalysis = await engine.getAnalysis("", {
depth, depth,
movetime movetime
}); });
@ -404,9 +309,6 @@ app.post('/analyze', async (req, res) => {
// Engine move endpoint // Engine move endpoint
app.post('/enginemove', async (req, res) => { app.post('/enginemove', async (req, res) => {
lastResponse = new Date().getTime() lastResponse = new Date().getTime()
if (!board) {
return res.status(400).json({ error: 'No active board' });
}
if (!engine || !engine.isReady) { if (!engine || !engine.isReady) {
return res.status(503).json({ return res.status(503).json({
@ -423,26 +325,17 @@ app.post('/enginemove', async (req, res) => {
} = req.body; } = req.body;
try { try {
// const fen = board.fen();
const analysis = await engine.getAnalysis(fen, { const analysis = await engine.getAnalysis(fen, {
depth, depth,
movetime, movetime,
nodes nodes
}); });
if (analysis.bestMove) {
board.push(analysis.bestMove);
}
res.json({ res.json({
status: 'ok', status: 'ok',
move: analysis.bestMove, move: analysis.bestMove,
analysis: analysis, analysis: analysis,
fen: board.fen(),
legalMoves: board.legalMoves().split(' '),
isCheck: board.isCheck(),
isGameOver: board.isGameOver(),
turn: board.turn()
}); });
} catch (error) { } catch (error) {
res.status(500).json({ res.status(500).json({
@ -510,7 +403,6 @@ function startIdleMonitor() {
} }
function closeServer(){ function closeServer(){
if (board) board.delete();
if (engine) engine.quit(); if (engine) engine.quit();
process.exit(0); process.exit(0);
} }

View file

@ -87,6 +87,17 @@ func start_board(elo: int, variant: String = "8x8"):
await http_request.request_completed await http_request.request_completed
setElo(elo, variant) setElo(elo, variant)
func clear_game( ):
if not running:
return
var headers = ["Content-Type: application/json"]
var body = JSON.new().stringify({
"variant": 'chess'
})
print(server_url + "/new")
print(body)
http_request.request(server_url + "/new", headers, HTTPClient.METHOD_POST, body)
await http_request.request_completed
func _exit_tree(): func _exit_tree():
ServerManager.stop_server() ServerManager.stop_server()
disconnect_engine(); disconnect_engine();

View file

@ -20,6 +20,7 @@ func enter(_previous: String, _data := {}) -> void:
game.updateTurnIndicator() game.updateTurnIndicator()
var variant = game.get_board_dimensions(game.currentFen) var variant = game.get_board_dimensions(game.currentFen)
game.stockfishController.setVariant(variant) game.stockfishController.setVariant(variant)
game.stockfishController.clear_game()
# Delay to avoid duplication during animation # Delay to avoid duplication during animation
if game.stockfishController: if game.stockfishController:
moveTimer.start(2) moveTimer.start(2)