Skip to content

Commit

Permalink
Implemented TutorFenCommand as an async command
Browse files Browse the repository at this point in the history
  • Loading branch information
programarivm committed Oct 11, 2024
1 parent 708c527 commit f95c8d9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
<?php

namespace ChessServer\Command\Game\Sync;
namespace ChessServer\Command\Game\Async;

use Chess\FenToBoardFactory;
use Chess\Function\CompleteFunction;
use Chess\Tutor\FenEvaluation;
use Chess\Variant\Classical\Board;
use ChessServer\Command\AbstractSyncCommand;
use ChessServer\Command\AbstractAsyncCommand;
use ChessServer\Socket\AbstractSocket;

class TutorFenCommand extends AbstractSyncCommand
class TutorFenCommand extends AbstractAsyncCommand
{
public function __construct()
{
Expand All @@ -28,11 +24,12 @@ public function validate(array $argv)
public function run(AbstractSocket $socket, array $argv, int $id)
{
$params = json_decode(stripslashes($argv[1]), true);
$board = FenToBoardFactory::create($params['fen'], new Board());
$paragraph = (new FenEvaluation(new CompleteFunction(), $board))->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,
]);
});
}
}
31 changes: 31 additions & 0 deletions src/Command/Game/Async/TutorFenTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace ChessServer\Command\Game\Async;

use Chess\FenToBoardFactory;
use Chess\Function\CompleteFunction;
use Chess\Tutor\FenEvaluation;
use Chess\Variant\Classical\Board;
use Spatie\Async\Task;

class TutorFenTask extends Task
{
private array $params;

public function __construct(array $params)
{
$this->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;
}
}
4 changes: 2 additions & 2 deletions src/Command/Game/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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));
}
}

0 comments on commit f95c8d9

Please sign in to comment.