From 401d915a0bbebf2398f7983328ab7218a798b33b Mon Sep 17 00:00:00 2001 From: 2ManyProjects Date: Thu, 6 Mar 2025 17:17:09 -0600 Subject: [PATCH] fixed deck not persisting between nodes --- .../ChessEngines/fairy-chess-server/engine.js | 15 ++++++++++ .../ChessEngines/fairy-chess-server/index.js | 30 ++++++++++++++++++- Systems/Cards/Supernova.gd | 2 +- Systems/DeckManager.gd | 20 +++++++++++-- Systems/FairyStockfish/StockfishClient.gd | 1 + Systems/Game/ChessGame.gd | 6 ++-- Systems/Game/DeckManagerScreen.gd | 26 ++++++++++++++-- 7 files changed, 91 insertions(+), 9 deletions(-) diff --git a/Assets/ChessEngines/fairy-chess-server/engine.js b/Assets/ChessEngines/fairy-chess-server/engine.js index 75aa2db..51d372a 100644 --- a/Assets/ChessEngines/fairy-chess-server/engine.js +++ b/Assets/ChessEngines/fairy-chess-server/engine.js @@ -120,6 +120,16 @@ class ChessEngine extends EventEmitter { await new Promise(resolve => this.once('ready', resolve)); } + async setNewGame() { + if (!this.isReady) throw new Error('Engine not ready'); + + this.sendCommand(`ucinewgame`); + + // Ensure engine is ready after position set + this.sendCommand('isready'); + await new Promise(resolve => this.once('ready', resolve)); + } + async getBestMove(options = {}) { if (!this.isReady) throw new Error('Engine not ready'); if (!this.currentFen) throw new Error('Position not set'); @@ -152,6 +162,11 @@ class ChessEngine extends EventEmitter { return { bestMove }; } + async createNewGame(){ + await this.setNewGame(); + return true + } + quit() { if (this.engine) { this.sendCommand('quit'); diff --git a/Assets/ChessEngines/fairy-chess-server/index.js b/Assets/ChessEngines/fairy-chess-server/index.js index ff54931..534ee8c 100644 --- a/Assets/ChessEngines/fairy-chess-server/index.js +++ b/Assets/ChessEngines/fairy-chess-server/index.js @@ -172,6 +172,7 @@ app.post('/validate', (req, res) => { tempBoard.delete(); res.json({ + status: 'ok', isValid, startingFen: ffish.startingFen(variant) }); @@ -181,7 +182,7 @@ app.post('/validate', (req, res) => { }); // New game endpoint -app.post('/new', (req, res) => { +app.post('/new', async (req, res) => { lastResponse = new Date().getTime() const { variant = 'chess' } = req.body; @@ -192,6 +193,7 @@ app.post('/new', (req, res) => { board = new ffish.Board(variant); + const engineAnalysis = await engine.createNewGame(); res.json({ status: 'ok', fen: board.fen(), @@ -311,6 +313,7 @@ app.get('/state', (req, res) => { try { res.json({ + status: 'ok', fen: board.fen(), legalMoves: board.legalMoves().split(' '), legalMovesSan: board.legalMovesSan().split(' '), @@ -329,6 +332,30 @@ app.get('/state', (req, res) => { } }); +/* +createNewGame +*/ +app.post('/newgame', async (req, res) => { + lastResponse = new Date().getTime() + + + try { + + const response = { + status: 'ok', + }; + + // If engine is available, get engine analysis + if (engine && engine.isReady) { + const engineAnalysis = await engine.createNewGame(); + } + + res.json(response); + } catch (error) { + res.status(500).json({ error: error.message }); + } +}); + // Analysis endpoint app.post('/analyze', async (req, res) => { lastResponse = new Date().getTime() @@ -341,6 +368,7 @@ app.post('/analyze', async (req, res) => { // Get basic position analysis const positionAnalysis = { + status: 'ok', fen: board.fen(), isCheck: board.isCheck(), isGameOver: board.isGameOver(), diff --git a/Systems/Cards/Supernova.gd b/Systems/Cards/Supernova.gd index 193c92b..eb938ca 100644 --- a/Systems/Cards/Supernova.gd +++ b/Systems/Cards/Supernova.gd @@ -8,7 +8,7 @@ func _init(): cardName = "Supernova" rank = Rank.RANK_0 effectType = EffectType.PIECE_EFFECT - duration = 5 + duration = 1 description = "All enemy units within 4 radius are captured" unitWhitelist = ["King"] remaining_turns = duration diff --git a/Systems/DeckManager.gd b/Systems/DeckManager.gd index eb56bb5..3aad4e7 100644 --- a/Systems/DeckManager.gd +++ b/Systems/DeckManager.gd @@ -20,7 +20,7 @@ const CARD_BASE_COSTS = { func _init(): print("************************DECK INIT*****************") initializeStartingDeck() - initializeStartingBank() + # initializeStartingBank() func initializeStartingDeck(): deck.clear(); @@ -44,7 +44,6 @@ func initializeStartingBank(): bank.append(DrunkDrivingCard.new()) bank.append(SupernovaCard.new()) - # Add some duplicates for testing for i in range(3): bank.append(HopscotchCard.new()) bank.append(FieryCapeCard.new()) @@ -65,7 +64,6 @@ func drawCard(): if !deck.is_empty() && hand.size() < hand_size: hand.append(deck.pop_back()) - emit_signal("hand_updated", hand) @@ -85,6 +83,22 @@ func playCard(card: Card, target_piece: Pawn, board_flow = null, game_state = nu attached_cards[target_piece.get_instance_id()] = card hand.erase(card) emit_signal("hand_updated", hand) + + # match card.rank: + # Card.Rank.RANK_0: + # print("Rank 3 Burned permanently") + # pass + # Card.Rank.RANK_1: + # print("Rank 3 Burned until next match") + # pass + # Card.Rank.RANK_2: + # print("Rank 2 add to Discard") + # discard.append(card) + # pass + # Card.Rank.RANK_3: + # deck.append(card) + # print("Rank 3 add to Deck") + # pass return true diff --git a/Systems/FairyStockfish/StockfishClient.gd b/Systems/FairyStockfish/StockfishClient.gd index a1fb693..5aeafb6 100644 --- a/Systems/FairyStockfish/StockfishClient.gd +++ b/Systems/FairyStockfish/StockfishClient.gd @@ -170,6 +170,7 @@ func generateMove(think_time_ms: int = 1000) -> void: body ) + diff --git a/Systems/Game/ChessGame.gd b/Systems/Game/ChessGame.gd index 9368039..013a730 100644 --- a/Systems/Game/ChessGame.gd +++ b/Systems/Game/ChessGame.gd @@ -47,7 +47,7 @@ var is_initialized: bool = false @onready var turnIndicator: ColorRect = $TurnIndicator @onready var p1String: RichTextLabel = $Player1Points @onready var p2String: RichTextLabel = $Player2Points -@onready var gold: int = 1000 +@onready var gold: int = 75 @onready var deckManager: DeckManager @onready var tileManager: TileManager @onready var cameraController: CameraController @@ -103,6 +103,8 @@ func _on_new_game_requested(options = {}): turnIndicator.visible = true if options and "fen" in options: currentFen = options.fen + if "elo" in options: + cpuElo = options.elo if cameraController: cameraController.reset_view() if is_initialized: @@ -115,6 +117,7 @@ func _on_new_game_requested(options = {}): stateMachine.transitionToNextState(Constants.WHITE_TURN) else: initialize_game_system() + stockfishController.start_board(cpuElo) func initialize_game_system(): print("Initializing game system") @@ -460,7 +463,6 @@ func resetBoard() -> void: p2String.text = str(p2Points) gamecheckMate = false; gamedraw = false; - deckManager.initializeStartingDeck() areas.clear() specialArea.clear() diff --git a/Systems/Game/DeckManagerScreen.gd b/Systems/Game/DeckManagerScreen.gd index 4d81ba8..e1ff963 100644 --- a/Systems/Game/DeckManagerScreen.gd +++ b/Systems/Game/DeckManagerScreen.gd @@ -55,10 +55,13 @@ func initialize(options = null): populateBank() func loadCards(): + var board = get_node_or_null("/root/Board") as ChessGame + if board and "deckManager" in board: + deckManager = board.deckManager + print("Found deck manager:", deckManager) if deckManager: # Clone the deck to work with currentDeck = deckManager.deck.duplicate() - bankCards = deckManager.bank.duplicate() else: # Fallback with empty collections if deck manager not found @@ -200,4 +203,23 @@ func _on_backButton_pressed(): emit_signal("back_pressed") # Hide this screen - visible = false \ No newline at end of file + visible = false + + + + +# Notification +func _notification(what): + if what == NOTIFICATION_VISIBILITY_CHANGED: + _on_visibility_changed(visible) + +func _on_visibility_changed(is_visible): + + print("DeckManager visibility changed to: ", is_visible) + if is_visible: + loadCards() + setupDeckGrid() + else: + print("DeckManager is now invisible") + + emit_signal("deck_manager_visibility_changed", is_visible) \ No newline at end of file