|
10 | 10 |
|
11 | 11 | static const char* INITIAL_FEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; |
12 | 12 |
|
13 | | -WiFiManagerESP32::WiFiManagerESP32(BoardDriver* bd, MoveHistory* mh) : boardDriver(bd), moveHistory(mh), server(AP_PORT), wifiSSID(SECRET_SSID), wifiPassword(SECRET_PASS), gameMode("0"), lichessToken(""), botConfig(), scanAllChannels(WIFI_SCAN_ALL_CHANNELS), currentFen(INITIAL_FEN), hasPendingEdit(false), hasPendingResign(false), hasPendingDraw(false), pendingResignColor('?'), hasPendingWiFi(false), boardEvaluation(0.0f), otaUpdater(bd), autoOtaEnabled(false) {} |
| 13 | +WiFiManagerESP32::WiFiManagerESP32(BoardDriver* bd, MoveHistory* mh) : boardDriver(bd), moveHistory(mh), server(AP_PORT), wifiSSID(SECRET_SSID), wifiPassword(SECRET_PASS), gameMode("0"), lichessToken(""), botConfig(), scanAllChannels(WIFI_SCAN_ALL_CHANNELS), currentFen(INITIAL_FEN), hasPendingEdit(false), hasPendingResign(false), hasPendingDraw(false), pendingResignColor('?'), promotion{}, lastBoardPollTime(0), hasPendingWiFi(false), boardEvaluation(0.0f), otaUpdater(bd), autoOtaEnabled(false) { |
| 14 | + promotion.reset(); |
| 15 | +} |
14 | 16 |
|
15 | 17 | void WiFiManagerESP32::begin() { |
16 | 18 | Serial.println("=== Starting OpenChess WiFi Manager (ESP32) ==="); |
@@ -68,6 +70,7 @@ void WiFiManagerESP32::begin() { |
68 | 70 | // Set up web server routes with async handlers |
69 | 71 | server.on("/board-update", HTTP_GET, [this](AsyncWebServerRequest* request) { request->send(200, "application/json", this->getBoardUpdateJSON()); }); |
70 | 72 | server.on("/board-update", HTTP_POST, [this](AsyncWebServerRequest* request) { this->handleBoardEditSuccess(request); }); |
| 73 | + server.on("/promotion", HTTP_POST, [this](AsyncWebServerRequest* request) { this->handlePromotion(request); }); |
71 | 74 | server.on("/resign", HTTP_POST, [this](AsyncWebServerRequest* request) { this->handleResign(request); }); |
72 | 75 | server.on("/draw", HTTP_POST, [this](AsyncWebServerRequest* request) { this->handleDraw(request); }); |
73 | 76 | server.on("/wifi", HTTP_GET, [this](AsyncWebServerRequest* request) { request->send(200, "application/json", this->getWiFiInfoJSON()); }); |
@@ -111,9 +114,14 @@ void WiFiManagerESP32::begin() { |
111 | 114 | } |
112 | 115 |
|
113 | 116 | String WiFiManagerESP32::getBoardUpdateJSON() { |
| 117 | + this->lastBoardPollTime = millis(); |
114 | 118 | JsonDocument doc; |
115 | 119 | doc["fen"] = currentFen; |
116 | 120 | doc["evaluation"] = serialized(String(boardEvaluation, 2)); |
| 121 | + if (promotion.pending) { |
| 122 | + JsonObject promo = doc["promotion"].to<JsonObject>(); |
| 123 | + promo["color"] = String(promotion.color); |
| 124 | + } |
117 | 125 | String output; |
118 | 126 | serializeJson(doc, output); |
119 | 127 | return output; |
@@ -438,6 +446,42 @@ void WiFiManagerESP32::clearPendingDraw() { |
438 | 446 | hasPendingDraw = false; |
439 | 447 | } |
440 | 448 |
|
| 449 | +void WiFiManagerESP32::handlePromotion(AsyncWebServerRequest* request) { |
| 450 | + if (!promotion.pending) { |
| 451 | + request->send(400, "text/plain", "No promotion pending"); |
| 452 | + return; |
| 453 | + } |
| 454 | + if (request->hasArg("piece")) { |
| 455 | + String piece = request->arg("piece"); |
| 456 | + piece.toLowerCase(); |
| 457 | + if (piece == "q" || piece == "r" || piece == "b" || piece == "n") { |
| 458 | + promotion.choice = piece.charAt(0); |
| 459 | + Serial.printf("Promotion choice received from web: %c\n", (char)promotion.choice); |
| 460 | + request->send(200, "text/plain", "OK"); |
| 461 | + } else { |
| 462 | + request->send(400, "text/plain", "Invalid piece (use 'q', 'r', 'b', or 'n')"); |
| 463 | + } |
| 464 | + } else { |
| 465 | + request->send(400, "text/plain", "Missing 'piece' parameter"); |
| 466 | + } |
| 467 | +} |
| 468 | + |
| 469 | +void WiFiManagerESP32::startPromotionWait(char color) { |
| 470 | + promotion.color = color; |
| 471 | + promotion.choice = ' '; |
| 472 | + promotion.pending = true; |
| 473 | + Serial.printf("Promotion wait started for %s\n", color == 'w' ? "White" : "Black"); |
| 474 | +} |
| 475 | + |
| 476 | +void WiFiManagerESP32::clearPromotion() { |
| 477 | + promotion.reset(); |
| 478 | +} |
| 479 | + |
| 480 | +bool WiFiManagerESP32::isWebClientConnected() const { |
| 481 | + // Consider web client connected if it polled within the last 2 seconds |
| 482 | + return lastBoardPollTime > 0 && (millis() - lastBoardPollTime < 2000); |
| 483 | +} |
| 484 | + |
441 | 485 | void WiFiManagerESP32::checkPendingWiFi() { |
442 | 486 | if (!hasPendingWiFi) |
443 | 487 | return; |
|
0 commit comments