From 18735960de45781a574601b603dbe803f5c490c1 Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Wed, 26 Apr 2023 19:00:48 +0100 Subject: [PATCH] LibChess: Add the ucinewgame command This command is sent from the GUI to tell the engine that the next position will be from a different game. --- Userland/Libraries/LibChess/UCICommand.cpp | 13 +++++++++++++ Userland/Libraries/LibChess/UCICommand.h | 12 ++++++++++++ Userland/Libraries/LibChess/UCIEndpoint.cpp | 4 ++++ Userland/Libraries/LibChess/UCIEndpoint.h | 1 + 4 files changed, 30 insertions(+) diff --git a/Userland/Libraries/LibChess/UCICommand.cpp b/Userland/Libraries/LibChess/UCICommand.cpp index d83aa08864..aa885acba9 100644 --- a/Userland/Libraries/LibChess/UCICommand.cpp +++ b/Userland/Libraries/LibChess/UCICommand.cpp @@ -348,4 +348,17 @@ ErrorOr QuitCommand::to_string() const return "quit\n"_short_string; } +ErrorOr> UCINewGameCommand::from_string(StringView command) +{ + auto tokens = command.split_view(' '); + VERIFY(tokens[0] == "ucinewgame"); + VERIFY(tokens.size() == 1); + return adopt_nonnull_own_or_enomem(new (nothrow) UCINewGameCommand); +} + +ErrorOr UCINewGameCommand::to_string() const +{ + return "ucinewgame\n"_string; +} + } diff --git a/Userland/Libraries/LibChess/UCICommand.h b/Userland/Libraries/LibChess/UCICommand.h index ea5dab15bd..8a99204108 100644 --- a/Userland/Libraries/LibChess/UCICommand.h +++ b/Userland/Libraries/LibChess/UCICommand.h @@ -281,4 +281,16 @@ public: virtual ErrorOr to_string() const override; }; +class UCINewGameCommand : public Command { +public: + explicit UCINewGameCommand() + : Command(Command::Type::UCINewGame) + { + } + + static ErrorOr> from_string(StringView command); + + virtual ErrorOr to_string() const override; +}; + } diff --git a/Userland/Libraries/LibChess/UCIEndpoint.cpp b/Userland/Libraries/LibChess/UCIEndpoint.cpp index 3ab72461a2..c4ab15610e 100644 --- a/Userland/Libraries/LibChess/UCIEndpoint.cpp +++ b/Userland/Libraries/LibChess/UCIEndpoint.cpp @@ -56,6 +56,8 @@ void Endpoint::event(Core::Event& event) return handle_info(static_cast(event)); case Command::Type::Quit: return handle_quit(); + case Command::Type::UCINewGame: + return handle_ucinewgame(); default: Object::event(event); break; @@ -115,6 +117,8 @@ NonnullOwnPtr Endpoint::read_command() return InfoCommand::from_string(line).release_value_but_fixme_should_propagate_errors(); } else if (line.starts_with("quit"sv)) { return QuitCommand::from_string(line).release_value_but_fixme_should_propagate_errors(); + } else if (line.starts_with("ucinewgame"sv)) { + return UCINewGameCommand::from_string(line).release_value_but_fixme_should_propagate_errors(); } dbgln("command line: {}", line); diff --git a/Userland/Libraries/LibChess/UCIEndpoint.h b/Userland/Libraries/LibChess/UCIEndpoint.h index 18f4f4db34..0ee5e1330d 100644 --- a/Userland/Libraries/LibChess/UCIEndpoint.h +++ b/Userland/Libraries/LibChess/UCIEndpoint.h @@ -31,6 +31,7 @@ public: virtual void handle_bestmove(BestMoveCommand const&) { } virtual void handle_info(InfoCommand const&) { } virtual void handle_quit() { } + virtual void handle_ucinewgame() { } virtual void handle_unexpected_eof() { } void send_command(Command const&);