From f95c8d924d49af3944357ea5507a6d8bc941232b Mon Sep 17 00:00:00 2001 From: programarivm Date: Fri, 11 Oct 2024 23:39:58 +0200 Subject: [PATCH] Implemented TutorFenCommand as an async command --- .../Game/{Sync => Async}/TutorFenCommand.php | 21 ++++++------- src/Command/Game/Async/TutorFenTask.php | 31 +++++++++++++++++++ src/Command/Game/Cli.php | 4 +-- 3 files changed, 42 insertions(+), 14 deletions(-) rename src/Command/Game/{Sync => Async}/TutorFenCommand.php (50%) create mode 100644 src/Command/Game/Async/TutorFenTask.php diff --git a/src/Command/Game/Sync/TutorFenCommand.php b/src/Command/Game/Async/TutorFenCommand.php similarity index 50% rename from src/Command/Game/Sync/TutorFenCommand.php rename to src/Command/Game/Async/TutorFenCommand.php index 88aa84c..8f3568f 100644 --- a/src/Command/Game/Sync/TutorFenCommand.php +++ b/src/Command/Game/Async/TutorFenCommand.php @@ -1,15 +1,11 @@ paragraph; - return $socket->getClientStorage()->send([$id], [ - $this->name => implode(' ', $paragraph), - ]); + $this->pool->add(new TutorFenTask($params)) + ->then(function ($result) use ($socket, $id) { + return $socket->getClientStorage()->send([$id], [ + $this->name => $result, + ]); + }); } } diff --git a/src/Command/Game/Async/TutorFenTask.php b/src/Command/Game/Async/TutorFenTask.php new file mode 100644 index 0000000..bb2263e --- /dev/null +++ b/src/Command/Game/Async/TutorFenTask.php @@ -0,0 +1,31 @@ +params = $params; + } + + public function configure() + { + } + + public function run() + { + $board = FenToBoardFactory::create($this->params['fen'], new Board()); + $paragraph = (new FenEvaluation(new CompleteFunction(), $board))->paragraph; + + return $paragraph; + } +} diff --git a/src/Command/Game/Cli.php b/src/Command/Game/Cli.php index df31cd5..0bbe7c8 100644 --- a/src/Command/Game/Cli.php +++ b/src/Command/Game/Cli.php @@ -10,6 +10,7 @@ use ChessServer\Command\Game\Async\ResignCommand; use ChessServer\Command\Game\Async\RestartCommand; use ChessServer\Command\Game\Async\StockfishCommand; +use ChessServer\Command\Game\Async\TutorFenCommand; use ChessServer\Command\Game\Sync\AcceptPlayRequestCommand; use ChessServer\Command\Game\Sync\DrawCommand; use ChessServer\Command\Game\Sync\EvalNamesCommand; @@ -19,7 +20,6 @@ use ChessServer\Command\Game\Sync\RematchCommand; use ChessServer\Command\Game\Sync\StartCommand; use ChessServer\Command\Game\Sync\TakebackCommand; -use ChessServer\Command\Game\Sync\TutorFenCommand; use ChessServer\Command\Game\Sync\UndoCommand; use Spatie\Async\Pool; @@ -49,6 +49,6 @@ public function __construct(Pool $pool) $this->commands->attach((new RestartCommand())->setPool($pool)); $this->commands->attach(new StartCommand()); $this->commands->attach((new StockfishCommand())->setPool($pool)); - $this->commands->attach(new TutorFenCommand()); + $this->commands->attach((new TutorFenCommand())->setPool($pool)); } }