Skip to content

Commit

Permalink
Implemented ChessServer\Command\Binary\TransmitCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
programarivm committed Aug 21, 2024
1 parent f41d9e0 commit 9dadbed
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
/ssl/*.key

/storage/*.log
/storage/tmp/*
!/storage/tmp/.gitkeep

/.phpunit.result.cache

Expand Down
13 changes: 9 additions & 4 deletions src/Command/Binary/TransmitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
}
}
15 changes: 8 additions & 7 deletions src/Socket/Workerman/ClientStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
}
}
Expand Down
Empty file added storage/tmp/.gitkeep
Empty file.

0 comments on commit 9dadbed

Please sign in to comment.