Skip to content

Commit

Permalink
Refactored TutorFenCommand as non-blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
programarivm committed Jan 16, 2025
1 parent 6acb90c commit 1b07910
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 57 deletions.
35 changes: 0 additions & 35 deletions src/Command/Game/Blocking/TutorFenCommand.php

This file was deleted.

20 changes: 0 additions & 20 deletions src/Command/Game/Blocking/TutorFenTask.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Command/Game/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use ChessServer\Command\Game\Blocking\ResignCommand;
use ChessServer\Command\Game\Blocking\RestartCommand;
use ChessServer\Command\Game\Blocking\StockfishCommand;
use ChessServer\Command\Game\Blocking\TutorFenCommand;
use ChessServer\Command\Game\NonBlocking\AcceptPlayRequestCommand;
use ChessServer\Command\Game\NonBlocking\AsciiCommand;
use ChessServer\Command\Game\NonBlocking\DrawCommand;
Expand All @@ -24,6 +23,7 @@
use ChessServer\Command\Game\NonBlocking\RematchCommand;
use ChessServer\Command\Game\NonBlocking\StartCommand;
use ChessServer\Command\Game\NonBlocking\TakebackCommand;
use ChessServer\Command\Game\NonBlocking\TutorFenCommand;
use ChessServer\Command\Game\NonBlocking\UndoCommand;
use Spatie\Async\Pool;

Expand Down Expand Up @@ -57,6 +57,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())->setPool($pool));
$this->commands->attach(new TutorFenCommand());
}
}
39 changes: 39 additions & 0 deletions src/Command/Game/NonBlocking/TutorFenCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace ChessServer\Command\Game\NonBlocking;

use Chess\FenToBoardFactory;
use Chess\Eval\CompleteFunction;
use Chess\Tutor\FenEvaluation;
use Chess\Variant\Classical\Board;
use ChessServer\Command\AbstractNonBlockingCommand;
use ChessServer\Socket\AbstractSocket;

class TutorFenCommand extends AbstractNonBlockingCommand
{
public function __construct()
{
$this->name = '/tutor_fen';
$this->description = 'Explains a FEN position in terms of chess concepts.';
$this->params = [
'params' => '<string>',
];
}

public function validate(array $argv)
{
return count($argv) - 1 === count($this->params);
}

public function run(AbstractSocket $socket, array $argv, int $id)
{
$params = $this->params($argv[1]);

$board = FenToBoardFactory::create($params['fen'], new Board());
$paragraph = (new FenEvaluation(new CompleteFunction(), $board))->paragraph;

return $socket->getClientStorage()->send([$id], [
$this->name => implode(' ', $paragraph),
]);
}
}

0 comments on commit 1b07910

Please sign in to comment.