-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #443 from chesslablab/issue/Implement-the-extract-…
…command Issue/implement the extract command
- Loading branch information
Showing
4 changed files
with
88 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters