From 9dadbedcc087654162668f3bc86b310c124a4473 Mon Sep 17 00:00:00 2001 From: programarivm Date: Wed, 21 Aug 2024 17:22:07 +0200 Subject: [PATCH] Implemented ChessServer\Command\Binary\TransmitCommand --- .gitignore | 2 ++ src/Command/Binary/TransmitCommand.php | 13 +++++++++---- src/Socket/Workerman/ClientStorage.php | 15 ++++++++------- storage/tmp/.gitkeep | 0 4 files changed, 19 insertions(+), 11 deletions(-) create mode 100644 storage/tmp/.gitkeep diff --git a/.gitignore b/.gitignore index 79389490..311665f7 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,8 @@ /ssl/*.key /storage/*.log +/storage/tmp/* +!/storage/tmp/.gitkeep /.phpunit.result.cache diff --git a/src/Command/Binary/TransmitCommand.php b/src/Command/Binary/TransmitCommand.php index 966eaf2f..42ff4b02 100644 --- a/src/Command/Binary/TransmitCommand.php +++ b/src/Command/Binary/TransmitCommand.php @@ -2,11 +2,15 @@ namespace ChessServer\Command\Binary; +use Chess\Media\BoardToPng; +use Chess\Variant\Classical\FEN\StrToBoard as ClassicalStrToBoard; use ChessServer\Command\AbstractCommand; use ChessServer\Socket\AbstractSocket; class TransmitCommand extends AbstractCommand { + const OUTPUT_FOLDER = __DIR__.'/../../../storage/tmp'; + public function __construct() { $this->name = '/transmit'; @@ -25,10 +29,11 @@ public function run(AbstractSocket $socket, array $argv, int $id) { $params = json_decode(stripslashes($argv[1]), true); - // TODO + $board = (new ClassicalStrToBoard($params['fen']))->create(); + $filename = (new BoardToPng($board, $params['flip']))->output(self::OUTPUT_FOLDER); + $contents = file_get_contents(self::OUTPUT_FOLDER . "/$filename"); + $base64 = base64_encode($contents); - return $socket->getClientStorage()->sendToOne($id, [ - $this->name => 'TODO', - ]); + return $socket->getClientStorage()->sendToOne($id, $base64); } } diff --git a/src/Socket/Workerman/ClientStorage.php b/src/Socket/Workerman/ClientStorage.php index 3cdf36ff..516b9e29 100644 --- a/src/Socket/Workerman/ClientStorage.php +++ b/src/Socket/Workerman/ClientStorage.php @@ -35,10 +35,11 @@ public function sendToOne(int $id, $res): void $this->rewind(); while ($this->valid()) { if ($id === $this->current()->id) { - $this->current()->send(json_encode($res)); + $res = is_array($res) ? json_encode($res) : $res; + $this->current()->send($res); $this->logger->info('Sent message', [ 'id' => $id, - 'cmd' => array_keys($res), + 'cmd' => is_array($res) ? array_keys($res) : [$res], ]); } $this->next(); @@ -47,14 +48,14 @@ public function sendToOne(int $id, $res): void public function sendToMany(array $ids, $res): void { - $json = json_encode($res); + $res = is_array($res) ? json_encode($res) : $res; $this->rewind(); while ($this->valid()) { if (in_array($this->current()->id, $ids)) { - $this->current()->send($json); + $this->current()->send($res); $this->logger->info('Sent message', [ 'ids' => $ids, - 'cmd' => array_keys($res), + 'cmd' => is_array($res) ? array_keys($res) : [$res], ]); } $this->next(); @@ -63,10 +64,10 @@ public function sendToMany(array $ids, $res): void public function sendToAll($res): void { - $json = json_encode($res); + $res = is_array($res) ? json_encode($res) : $res; $this->rewind(); while ($this->valid()) { - $this->current()->send($json); + $this->current()->send($res); $this->next(); } } diff --git a/storage/tmp/.gitkeep b/storage/tmp/.gitkeep new file mode 100644 index 00000000..e69de29b