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 port = 27531;
let board = null;
let engine = null;
let isReady = false;
let lastResponse = null
const SERVER_WAIT_THRESHOLD = 2 * 60 * 1000;
const SERVER_WAIT_THRESHOLD = 1 * 30 * 1000;
const CHECK_INTERVAL = 5000;
// Initialize ffish and engine
@ -167,7 +166,6 @@ app.post('/validate', (req, res) => {
}
try {
// Create temporary board to validate FEN
const tempBoard = new ffish.Board(variant);
const isValid = tempBoard.setFen(fen);
tempBoard.delete();
@ -188,21 +186,10 @@ app.post('/new', async (req, res) => {
const { variant = 'chess' } = req.body;
try {
if (board) {
board.delete();
}
board = new ffish.Board(variant);
const engineAnalysis = await engine.createNewGame();
res.json({
status: 'ok',
fen: board.fen(),
legalMoves: board.legalMoves().split(' '),
legalMovesSan: board.legalMovesSan().split(' '),
isCheck: board.isCheck(),
turn: board.turn(),
moveStack: board.moveStack()
});
} catch (error) {
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
// 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){
await engine.startPos()
engine.sendCommand('d');
}else if(fen){
board.setFen(fen);
await engine.setBoardPosition(fen)
}
res.json({
status: 'ok',
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) {
res.status(500).json({ error: error.message, poserr: true });
@ -259,49 +227,16 @@ app.post('/move', (req, res) => {
lastResponse = new Date().getTime()
const { move, notation = 'uci', variant = 'chess' } = req.body;
if (!board) {
return res.status(400).json({ error: 'No active board' });
}
if (!move) {
return res.status(400).json({ error: 'Move required' });
}
if (board) {
board.delete();
}
board = new ffish.Board(variant);
try {
if (notation === 'san') {
board.pushSan(move);
} else {
board.push(move);
}
const response = {
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);
} catch (error) {
res.status(500).json({ error: error.message });
@ -311,25 +246,9 @@ app.post('/move', (req, res) => {
// State endpoint
app.get('/state', (req, res) => {
lastResponse = new Date().getTime()
if (!board) {
return res.status(400).json({ error: 'No active board' });
}
try {
res.json({
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) {
res.status(500).json({ error: error.message });
@ -363,9 +282,6 @@ app.post('/newgame', async (req, res) => {
// Analysis endpoint
app.post('/analyze', async (req, res) => {
lastResponse = new Date().getTime()
if (!board) {
return res.status(400).json({ error: 'No active board' });
}
try {
const { depth = 15, movetime = 1000 } = req.body;
@ -373,22 +289,11 @@ app.post('/analyze', async (req, res) => {
// Get basic position analysis
const positionAnalysis = {
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 && engine.isReady) {
const engineAnalysis = await engine.getAnalysis(board.fen(), {
const engineAnalysis = await engine.getAnalysis("", {
depth,
movetime
});
@ -404,9 +309,6 @@ app.post('/analyze', async (req, res) => {
// Engine move endpoint
app.post('/enginemove', async (req, res) => {
lastResponse = new Date().getTime()
if (!board) {
return res.status(400).json({ error: 'No active board' });
}
if (!engine || !engine.isReady) {
return res.status(503).json({
@ -423,26 +325,17 @@ app.post('/enginemove', async (req, res) => {
} = req.body;
try {
// const fen = board.fen();
const analysis = await engine.getAnalysis(fen, {
depth,
movetime,
nodes
});
if (analysis.bestMove) {
board.push(analysis.bestMove);
}
res.json({
status: 'ok',
move: analysis.bestMove,
analysis: analysis,
fen: board.fen(),
legalMoves: board.legalMoves().split(' '),
isCheck: board.isCheck(),
isGameOver: board.isGameOver(),
turn: board.turn()
});
} catch (error) {
res.status(500).json({
@ -510,7 +403,6 @@ function startIdleMonitor() {
}
function closeServer(){
if (board) board.delete();
if (engine) engine.quit();
process.exit(0);
}

View file

@ -87,6 +87,17 @@ func start_board(elo: int, variant: String = "8x8"):
await http_request.request_completed
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():
ServerManager.stop_server()
disconnect_engine();
@ -288,6 +299,6 @@ func _on_request_completed(result: int, response_code: int, headers: PackedStrin
print("HTTP Request failed")
return
if response.status != "ok":
if response.status != "ok":
print("Server error:", response_code, json.parse(body.get_string_from_utf8()),)
return

View file

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