Skip to content

Commit

Permalink
Implemented StockfishAsyncTask
Browse files Browse the repository at this point in the history
  • Loading branch information
programarivm committed Oct 2, 2024
1 parent b1dcc5d commit d0fa9f1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Command/Game/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(Pool $pool, Db $db)
$this->commands->attach(new ResignCommand($db));
$this->commands->attach(new RestartCommand($db));
$this->commands->attach(new StartCommand($db));
$this->commands->attach(new StockfishCommand());
$this->commands->attach((new StockfishCommand())->setPool($pool));
$this->commands->attach(new TutorFenCommand());
}

Expand Down
31 changes: 31 additions & 0 deletions src/Command/Game/StockfishAsyncTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace ChessServer\Command\Game;

use Spatie\Async\Task;

class StockfishAsyncTask extends Task
{
private $params;

private $gameMode;

private $command;

public function __construct($params, $gameMode, $command)
{
$this->params = $params;
$this->gameMode = $gameMode;
$this->command = $command;

}

public function configure()
{
}

public function run()
{
return $this->gameMode->res($this->params, $this->command);
}
}
11 changes: 8 additions & 3 deletions src/Command/Game/StockfishCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ public function run(AbstractSocket $socket, array $argv, int $id)
$params = json_decode(stripslashes($argv[1]), true);
$gameMode = $socket->getGameModeStorage()->getById($id);

return $socket->getClientStorage()->send([$id],
$gameMode->res($params, $this)
);
$this->pool->add(new StockfishAsyncTask($params, $gameMode, $this))
->then(function ($result) use ($socket, $id) {
return $socket->getClientStorage()->send([$id], [
$this->name => $result,
]);
});

// $this->pool->wait();
}
}

0 comments on commit d0fa9f1

Please sign in to comment.