Skip to content

Commit

Permalink
Merge pull request #450 from chesslablab/issue/449-Implement-the-good…
Browse files Browse the repository at this point in the history
…_pgn-command

Issue/449 implement the good pgn command
  • Loading branch information
programarivm authored Feb 7, 2025
2 parents a20633c + 5485230 commit 8ceecee
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/Command/Game/Blocking/TutorGoodPgnCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace ChessServer\Command\Game\Blocking;

use ChessServer\Command\AbstractBlockingCommand;
use ChessServer\Socket\AbstractSocket;

class TutorGoodPgnCommand extends AbstractBlockingCommand
{
public function __construct()
{
$this->name = '/tutor_good_pgn';
$this->description = "Explains the why of a good move in terms of chess concepts.";
}

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

public function run(AbstractSocket $socket, array $argv, int $id)
{
$game = $socket->getGameModeStorage()->getById($id)->getGame();

if (!isset($game->state()->end)) {
$this->pool->add(new TutorGoodPgnTask($game->getBoard()))
->then(function ($result) use ($socket, $id, $game) {
return $socket->getClientStorage()->send([$id], [
$this->name => $result,
]);
});
}
}
}
38 changes: 38 additions & 0 deletions src/Command/Game/Blocking/TutorGoodPgnTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace ChessServer\Command\Game\Blocking;

use Chess\Eval\CompleteFunction;
use Chess\Tutor\GoodPgnEvaluation;
use Chess\UciEngine\UciEngine;
use Chess\UciEngine\Details\Limit;
use Chess\Variant\Classical\Board;
use ChessServer\Command\AbstractBlockingTask;
use ChessServer\Socket\AbstractSocket;

class TutorGoodPgnTask extends AbstractBlockingTask
{
private Board $board;

public function __construct(Board $board)
{
parent::__construct();

$this->board = $board;
}

public function run()
{
$limit = new Limit();
$limit->depth = 12;
$stockfish = new UciEngine('/usr/games/stockfish');
$f = new CompleteFunction();

$goodPgnEvaluation = new GoodPgnEvaluation($limit, $stockfish, $f, $this->board);

return [
'pgn' => $goodPgnEvaluation->pgn,
'paragraph' => implode(' ', $goodPgnEvaluation->paragraph),
];
}
}
2 changes: 2 additions & 0 deletions src/Command/Game/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use ChessServer\Command\Game\Blocking\ResignCommand;
use ChessServer\Command\Game\Blocking\RestartCommand;
use ChessServer\Command\Game\Blocking\StockfishCommand;
use ChessServer\Command\Game\Blocking\TutorGoodPgnCommand;
use ChessServer\Command\Game\NonBlocking\AcceptPlayRequestCommand;
use ChessServer\Command\Game\NonBlocking\AsciiCommand;
use ChessServer\Command\Game\NonBlocking\DrawCommand;
Expand Down Expand Up @@ -58,5 +59,6 @@ public function __construct(Pool $pool)
$this->commands->attach(new StartCommand());
$this->commands->attach((new StockfishCommand())->setPool($pool));
$this->commands->attach(new TutorFenCommand());
$this->commands->attach((new TutorGoodPgnCommand())->setPool($pool));
}
}

0 comments on commit 8ceecee

Please sign in to comment.