Skip to content

Commit

Permalink
Merge pull request #443 from chesslablab/issue/Implement-the-extract-…
Browse files Browse the repository at this point in the history
…command

Issue/implement the extract command
  • Loading branch information
programarivm authored Jan 11, 2025
2 parents e4bd69b + 3405e8a commit 5e7f6c7
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 5 deletions.
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions src/Command/Game/Async/ExtractCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace ChessServer\Command\Game\Async;

use ChessServer\Command\AbstractAsyncCommand;
use ChessServer\Socket\AbstractSocket;

class ExtractCommand extends AbstractAsyncCommand
{
public function __construct()
{
$this->name = '/extract';
$this->description = 'Extracts oscillations data from a game.';
$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 = json_decode(stripslashes($argv[1]), true);

$this->pool->add(new ExtractTask($params))
->then(function ($result) use ($socket, $id) {
return $socket->getClientStorage()->send([$id], [
$this->name => $result,
]);
});
}
}
46 changes: 46 additions & 0 deletions src/Command/Game/Async/ExtractTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace ChessServer\Command\Game\Async;

use Chess\FenToBoardFactory;
use Chess\SanExtractor;
use Chess\Function\CompleteFunction;
use Chess\Variant\Capablanca\Board as CapablancaBoard;
use Chess\Variant\CapablancaFischer\Board as CapablancaFischerBoard;
use Chess\Variant\Chess960\Board as Chess960Board;
use Chess\Variant\Classical\Board as ClassicalBoard;
use ChessServer\Command\AbstractAsyncTask;

class ExtractTask extends AbstractAsyncTask
{
public function run()
{
$f = new CompleteFunction();

if ($this->params['variant'] === Chess960Board::VARIANT) {
$startPos = str_split($this->params['startPos']);
$board = isset($this->params['fen'])
? FenToBoardFactory::create($this->params['fen'], new Chess960Board($startPos))
: new Chess960Board($startPos);
} elseif ($this->params['variant'] === CapablancaBoard::VARIANT) {
$board = isset($this->params['fen'])
? FenToBoardFactory::create($this->params['fen'], new CapablancaBoard())
: new CapablancaBoard();
} elseif ($this->params['variant'] === CapablancaFischerBoard::VARIANT) {
$startPos = str_split($this->params['startPos']);
$board = isset($this->params['fen'])
? FenToBoardFactory::create($this->params['fen'], new CapablancaFischerBoard($startPos))
: new CapablancaFischerBoard($startPos);
} elseif ($this->params['variant'] === ClassicalBoard::VARIANT) {
$board = isset($this->params['fen'])
? FenToBoardFactory::create($this->params['fen'], new ClassicalBoard())
: new ClassicalBoard();
}

return [
'steinitz' => SanExtractor::steinitz($f, $board->clone(), $this->params['movetext']),
'mean' => SanExtractor::mean($f, $board->clone(), $this->params['movetext']),
'sd' => SanExtractor::sd($f, $board->clone(), $this->params['movetext']),
];
}
}
2 changes: 2 additions & 0 deletions src/Command/Game/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ChessServer\Command\Game;

use ChessServer\Command\AbstractCli;
use ChessServer\Command\Game\Async\ExtractCommand;
use ChessServer\Command\Game\Async\HeuristicCommand;
use ChessServer\Command\Game\Async\LeaveCommand;
use ChessServer\Command\Game\Async\PlayCommand;
Expand Down Expand Up @@ -43,6 +44,7 @@ public function __construct(Pool $pool)
$this->commands->attach(new TakebackCommand());
// param-based commands
$this->commands->attach(new AcceptPlayRequestCommand());
$this->commands->attach((new ExtractCommand())->setPool($pool));
$this->commands->attach((new HeuristicCommand())->setPool($pool));
$this->commands->attach((new LeaveCommand())->setPool($pool));
$this->commands->attach(new LegalCommand());
Expand Down

0 comments on commit 5e7f6c7

Please sign in to comment.