Skip to content

Commit

Permalink
Merge pull request #421 from chesslablab/issue/420-Implement-the-reco…
Browse files Browse the repository at this point in the history
…gnizer-command

Issue/420 implement the recognizer command
  • Loading branch information
programarivm authored Oct 23, 2024
2 parents 017f634 + d9553a0 commit 77d2ec0
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 11 deletions.
18 changes: 9 additions & 9 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/RecognizerCommand.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 RecognizerCommand extends AbstractAsyncCommand
{
public function __construct()
{
$this->name = '/recognizer';
$this->description = 'Returns the piece placement in FEN format of a Base64 encoded image.';
$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 RecognizerTask($params))
->then(function ($result) use ($socket, $id) {
return $socket->getClientStorage()->send([$id], [
$this->name => $result,
]);
});
}
}
17 changes: 17 additions & 0 deletions src/Command/Game/Async/RecognizerTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace ChessServer\Command\Game\Async;

use Chess\Media\FEN\JpgToPiecePlacement;
use ChessServer\Command\AbstractAsyncTask;

class RecognizerTask extends AbstractAsyncTask
{
public function run()
{
$data = base64_decode($this->params['data']);
$image = imagecreatefromstring($data);

return (new JpgToPiecePlacement($image))->predict();
}
}
2 changes: 2 additions & 0 deletions src/Command/Game/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use ChessServer\Command\Game\Async\LeaveCommand;
use ChessServer\Command\Game\Async\PlayLanCommand;
use ChessServer\Command\Game\Async\PlayRavCommand;
use ChessServer\Command\Game\Async\RecognizerCommand;
use ChessServer\Command\Game\Async\ResignCommand;
use ChessServer\Command\Game\Async\RestartCommand;
use ChessServer\Command\Game\Async\StockfishCommand;
Expand Down Expand Up @@ -45,6 +46,7 @@ public function __construct(Pool $pool)
$this->commands->attach((new PlayLanCommand())->setPool($pool));
$this->commands->attach((new PlayRavCommand())->setPool($pool));
$this->commands->attach(new RandomizerCommand());
$this->commands->attach((new RecognizerCommand())->setPool($pool));
$this->commands->attach((new ResignCommand())->setPool($pool));
$this->commands->attach((new RestartCommand())->setPool($pool));
$this->commands->attach(new StartCommand());
Expand Down
2 changes: 1 addition & 1 deletion src/Socket/Ratchet/AbstractWebSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function onOpen(ConnectionInterface $conn)

public function onMessage(ConnectionInterface $from, $msg)
{
if (strlen($msg) > 4096) {
if (strlen($msg) > 128000) {
return $this->clientStorage->send([$from->resourceId], [
'error' => 'Internal server error',
]);
Expand Down
2 changes: 1 addition & 1 deletion src/Socket/Workerman/AbstractWebSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected function connect()
protected function message()
{
$this->worker->onMessage = function ($conn, $msg) {
if (strlen($msg) > 4096) {
if (strlen($msg) > 128000) {
return $this->clientStorage->send([$conn->id], [
'error' => 'Internal server error',
]);
Expand Down

0 comments on commit 77d2ec0

Please sign in to comment.