Skip to content

Commit

Permalink
Merge pull request #155 from chesslablab/issue/153-Implement-a-TCP-so…
Browse files Browse the repository at this point in the history
…cket-using-ReactPHP

Issue/153 implement a tcp socket using react php
  • Loading branch information
programarivm authored Sep 7, 2023
2 parents 18d6060 + 53b6376 commit aa390a4
Show file tree
Hide file tree
Showing 34 changed files with 330 additions and 209 deletions.
9 changes: 9 additions & 0 deletions cli/tcp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace ChessServer\Cli;

use ChessServer\Socket\TcpSocket;

require __DIR__ . '/../vendor/autoload.php';

$server = new TcpSocket(8080);
6 changes: 3 additions & 3 deletions cli/ws.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace ChessServer;
namespace ChessServer\Cli;

use ChessServer\Socket;
use ChessServer\Socket\WebSocket;
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
Expand All @@ -12,7 +12,7 @@
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Socket()
new WebSocket()
)
),
8080
Expand Down
6 changes: 3 additions & 3 deletions cli/wss.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace ChessServer;
namespace ChessServer\Cli;

use ChessServer\Socket;
use ChessServer\Socket\WebSocket;
use Ratchet\Http\HttpServer;
use Ratchet\Http\OriginCheck;
use Ratchet\Server\IoServer;
Expand Down Expand Up @@ -33,7 +33,7 @@
$httpServer = new HttpServer(
new OriginCheck(
new WsServer(
new Socket()
new WebSocket()
),
$allowed,
)
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
"firebase/php-jwt": "^6.8",
"vlucas/phpdotenv": "^5.3",
"monolog/monolog": "^2.3",
"rakibtg/sleekdb": "^2.15",
"ratchet/pawl": "^0.4.1"
"rakibtg/sleekdb": "^2.15"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
Expand Down
5 changes: 2 additions & 3 deletions src/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace ChessServer\Command;

use ChessServer\Socket;
use Ratchet\ConnectionInterface;
use ChessServer\Socket\ChessSocket;

abstract class AbstractCommand
{
Expand All @@ -22,5 +21,5 @@ public function __get($property)

abstract public function validate(array $command);

abstract public function run(Socket $socket, array $argv, ConnectionInterface $from);
abstract public function run(ChessSocket $socket, array $argv, int $resourceId);
}
11 changes: 5 additions & 6 deletions src/Command/AcceptPlayRequestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
namespace ChessServer\Command;

use Chess\Variant\Classical\PGN\AN\Color;
use ChessServer\Socket;
use ChessServer\Socket\ChessSocket;
use ChessServer\Exception\InternalErrorException;
use ChessServer\GameMode\PlayMode;
use Ratchet\ConnectionInterface;
use ChessServer\Game\PlayMode;

class AcceptPlayRequestCommand extends AbstractCommand
{
Expand All @@ -24,12 +23,12 @@ public function validate(array $argv)
return count($argv) - 1 === count($this->params);
}

public function run(Socket $socket, array $argv, ConnectionInterface $from)
public function run(ChessSocket $socket, array $argv, int $resourceId)
{
$gameMode = $socket->getGameModeStorage()->getByHash($argv[1]);

if (!$gameMode) {
return $socket->sendToOne($from->resourceId, [
return $socket->sendToOne($resourceId, [
$this->name => [
'mode' => PlayMode::NAME,
'message' => 'This friend request could not be accepted.',
Expand All @@ -39,7 +38,7 @@ public function run(Socket $socket, array $argv, ConnectionInterface $from)

if ($gameMode->getStatus() === PlayMode::STATUS_PENDING) {
$decoded = $gameMode->getJwtDecoded();
$resourceIds = [...$gameMode->getResourceIds(), $from->resourceId];
$resourceIds = [...$gameMode->getResourceIds(), $resourceId];
$gameMode->setResourceIds($resourceIds)
->setStatus(PlayMode::STATUS_ACCEPTED)
->setStartedAt(time())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace ChessServer;
namespace ChessServer\Command;

use ChessServer\Command\AcceptPlayRequestCommand;
use ChessServer\Command\DrawCommand;
Expand Down
6 changes: 3 additions & 3 deletions src/CommandParser.php → src/Command/CommandParser.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace ChessServer;
namespace ChessServer\Command;

use ChessServer\Exception\ParserException;

Expand All @@ -10,9 +10,9 @@ class CommandParser

protected $cli;

public function __construct(CommandContainer $cli)
public function __construct()
{
$this->cli = $cli;
$this->cli = new CommandContainer();
}

public function __get($property)
Expand Down
9 changes: 4 additions & 5 deletions src/Command/DrawCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace ChessServer\Command;

use ChessServer\Socket;
use ChessServer\Socket\ChessSocket;
use ChessServer\Exception\InternalErrorException;
use ChessServer\GameMode\PlayMode;
use Ratchet\ConnectionInterface;
use ChessServer\Game\PlayMode;

class DrawCommand extends AbstractCommand
{
Expand Down Expand Up @@ -38,9 +37,9 @@ public function validate(array $argv)
return false;
}

public function run(Socket $socket, array $argv, ConnectionInterface $from)
public function run(ChessSocket $socket, array $argv, int $resourceId)
{
$gameMode = $socket->getGameModeStorage()->getByResourceId($from->resourceId);
$gameMode = $socket->getGameModeStorage()->getByResourceId($resourceId);

if (!$gameMode) {
throw new InternalErrorException();
Expand Down
11 changes: 5 additions & 6 deletions src/Command/HeuristicsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace ChessServer\Command;

use ChessServer\Socket;
use ChessServer\Socket\ChessSocket;
use ChessServer\Exception\InternalErrorException;
use ChessServer\GameMode\PlayMode;
use Ratchet\ConnectionInterface;
use ChessServer\Game\PlayMode;

class HeuristicsCommand extends AbstractCommand
{
Expand All @@ -24,16 +23,16 @@ public function validate(array $argv)
return count($argv) - 1 === count($this->params);
}

public function run(Socket $socket, array $argv, ConnectionInterface $from)
public function run(ChessSocket $socket, array $argv, int $resourceId)
{
$gameMode = $socket->getGameModeStorage()->getByResourceId($from->resourceId);
$gameMode = $socket->getGameModeStorage()->getByResourceId($resourceId);

if (!$gameMode) {
throw new InternalErrorException();
}

return $socket->sendToOne(
$from->resourceId,
$resourceId,
$gameMode->res($argv, $this)
);
}
Expand Down
25 changes: 12 additions & 13 deletions src/Command/InboxCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
use Chess\Variant\Classical\FEN\StrToBoard as ClassicalFenStrToBoard;
use Chess\Variant\Classical\PGN\Move as ClassicalPgnMove;
use Chess\Variant\Classical\PGN\AN\Color;
use ChessServer\Game;
use ChessServer\Socket;
use ChessServer\GameMode\PlayMode;
use Ratchet\ConnectionInterface;
use ChessServer\Game\Game;
use ChessServer\Socket\ChessSocket;
use ChessServer\Game\PlayMode;

class InboxCommand extends AbstractCommand
{
Expand Down Expand Up @@ -67,7 +66,7 @@ public function validate(array $argv)
return false;
}

public function run(Socket $socket, array $argv, ConnectionInterface $from)
public function run(ChessSocket $socket, array $argv, int $resourceId)
{
if (InboxCommand::ACTION_CREATE === $argv[1]) {
$hash = md5(uniqid());
Expand All @@ -85,7 +84,7 @@ public function run(Socket $socket, array $argv, ConnectionInterface $from)
$board = (new ClassicalFenStrToBoard($fen))->create();
}
} catch (\Exception $e) {
return $socket->sendToOne($from->resourceId, [
return $socket->sendToOne($resourceId, [
$this->name => [
'action' => InboxCommand::ACTION_CREATE,
'message' => 'Invalid FEN, please try again with a different one.',
Expand All @@ -99,10 +98,10 @@ public function run(Socket $socket, array $argv, ConnectionInterface $from)
'fen' => $board->toFen(),
'movetext' => '',
'createdAt' => (new \DateTime())->format('Y-m-d H:i:s'),
'createdBy' => $from->resourceId,
'createdBy' => $resourceId,
];
$socket->getInboxStore()->insert($inbox);
return $socket->sendToOne($from->resourceId, [
return $socket->sendToOne($resourceId, [
$this->name => [
'action' => InboxCommand::ACTION_CREATE,
'hash' => $hash,
Expand All @@ -111,14 +110,14 @@ public function run(Socket $socket, array $argv, ConnectionInterface $from)
]);
} elseif (InboxCommand::ACTION_READ === $argv[1]) {
if ($inbox = $socket->getInboxStore()->findOneBy(['hash', '=', $argv[2]])) {
return $socket->sendToOne($from->resourceId, [
return $socket->sendToOne($resourceId, [
$this->name => [
'action' => InboxCommand::ACTION_READ,
'inbox' => $inbox,
],
]);
} else {
return $socket->sendToOne($from->resourceId, [
return $socket->sendToOne($resourceId, [
$this->name => [
'action' => InboxCommand::ACTION_READ,
'message' => 'This inbox code does not exist.',
Expand Down Expand Up @@ -169,16 +168,16 @@ public function run(Socket $socket, array $argv, ConnectionInterface $from)
$inbox['fen'] = $board->toFen();
$inbox['movetext'] = $board->getMovetext();
$inbox['updatedAt'] = (new \DateTime())->format('Y-m-d H:i:s');
$inbox['updatedBy'] = $from->resourceId;
$inbox['updatedBy'] = $resourceId;
$socket->getInboxStore()->update($inbox);
return $socket->sendToOne($from->resourceId, [
return $socket->sendToOne($resourceId, [
$this->name => [
'action' => InboxCommand::ACTION_REPLY,
'message' => 'Chess move successfully sent.',
],
]);
} catch (\Exception $e) {
return $socket->sendToOne($from->resourceId, [
return $socket->sendToOne($resourceId, [
$this->name => [
'action' => InboxCommand::ACTION_REPLY,
'message' => 'Invalid PGN move, please try again with a different one.',
Expand Down
9 changes: 4 additions & 5 deletions src/Command/LeaveCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace ChessServer\Command;

use ChessServer\Socket;
use ChessServer\Socket\ChessSocket;
use ChessServer\Exception\InternalErrorException;
use ChessServer\GameMode\PlayMode;
use Ratchet\ConnectionInterface;
use ChessServer\Game\PlayMode;

class LeaveCommand extends AbstractCommand
{
Expand All @@ -32,9 +31,9 @@ public function validate(array $argv)
return false;
}

public function run(Socket $socket, array $argv, ConnectionInterface $from)
public function run(ChessSocket $socket, array $argv, int $resourceId)
{
$gameMode = $socket->getGameModeStorage()->getByResourceId($from->resourceId);
$gameMode = $socket->getGameModeStorage()->getByResourceId($resourceId);

if (!$gameMode) {
throw new InternalErrorException();
Expand Down
9 changes: 4 additions & 5 deletions src/Command/LegalCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace ChessServer\Command;

use ChessServer\Socket;
use ChessServer\Socket\ChessSocket;
use ChessServer\Exception\InternalErrorException;
use Ratchet\ConnectionInterface;

class LegalCommand extends AbstractCommand
{
Expand All @@ -22,16 +21,16 @@ public function validate(array $argv)
return count($argv) - 1 === count($this->params);
}

public function run(Socket $socket, array $argv, ConnectionInterface $from)
public function run(ChessSocket $socket, array $argv, int $resourceId)
{
$gameMode = $socket->getGameModeStorage()->getByResourceId($from->resourceId);
$gameMode = $socket->getGameModeStorage()->getByResourceId($resourceId);

if (!$gameMode) {
throw new InternalErrorException();
}

return $socket->sendToOne(
$from->resourceId,
$resourceId,
$gameMode->res($argv, $this)
);
}
Expand Down
9 changes: 4 additions & 5 deletions src/Command/OnlineGamesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace ChessServer\Command;

use ChessServer\Socket;
use ChessServer\GameMode\PlayMode;
use Ratchet\ConnectionInterface;
use ChessServer\Socket\ChessSocket;
use ChessServer\Game\PlayMode;

class OnlineGamesCommand extends AbstractCommand
{
Expand All @@ -19,9 +18,9 @@ public function validate(array $argv)
return count($argv) - 1 === 0;
}

public function run(Socket $socket, array $argv, ConnectionInterface $from)
public function run(ChessSocket $socket, array $argv, int $resourceId)
{
return $socket->sendToOne($from->resourceId, [
return $socket->sendToOne($resourceId, [
$this->name => $socket
->getGameModeStorage()
->decodeByPlayMode(PlayMode::STATUS_PENDING, PlayMode::SUBMODE_ONLINE),
Expand Down
11 changes: 5 additions & 6 deletions src/Command/PlayLanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace ChessServer\Command;

use ChessServer\Socket;
use ChessServer\Socket\ChessSocket;
use ChessServer\Exception\InternalErrorException;
use ChessServer\GameMode\PlayMode;
use Ratchet\ConnectionInterface;
use ChessServer\Game\PlayMode;

class PlayLanCommand extends AbstractCommand
{
Expand All @@ -24,9 +23,9 @@ public function validate(array $argv)
return count($argv) - 1 === count($this->params);
}

public function run(Socket $socket, array $argv, ConnectionInterface $from)
public function run(ChessSocket $socket, array $argv, int $resourceId)
{
$gameMode = $socket->getGameModeStorage()->getByResourceId($from->resourceId);
$gameMode = $socket->getGameModeStorage()->getByResourceId($resourceId);

if (!$gameMode) {
throw new InternalErrorException();
Expand All @@ -40,7 +39,7 @@ public function run(Socket $socket, array $argv, ConnectionInterface $from)
}

return $socket->sendToOne(
$from->resourceId,
$resourceId,
$gameMode->res($argv, $this)
);
}
Expand Down
Loading

0 comments on commit aa390a4

Please sign in to comment.