Skip to content

Commit

Permalink
Updated the game CLI commands
Browse files Browse the repository at this point in the history
  • Loading branch information
programarivm committed Oct 7, 2024
1 parent e768a8f commit 3add356
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 85 deletions.
14 changes: 2 additions & 12 deletions cli/ratchet/game.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace ChessServer\Cli\Ratchet;

use ChessServer\Db;
use ChessServer\Command\Parser;
use ChessServer\Command\Game\Cli;
use ChessServer\Socket\Ratchet\ClientStorage;
Expand All @@ -13,7 +12,6 @@
use Ratchet\Http\HttpServer;
use Ratchet\Server\IoServer;
use Ratchet\WebSocket\WsServer;
use React\EventLoop\Factory;
use React\Socket\LimitingServer;
use React\Socket\Server;
use React\Socket\SecureServer;
Expand All @@ -26,20 +24,12 @@

$pool = Pool::create();

$db = new Db([
'driver' => $_ENV['DB_DRIVER'],
'host' => $_ENV['DB_HOST'],
'database' => $_ENV['DB_DATABASE'],
'username' => $_ENV['DB_USERNAME'],
'password' => $_ENV['DB_PASSWORD'],
]);

$logger = new Logger('log');
$logger->pushHandler(new StreamHandler(__DIR__.'/../../storage' . '/game.log', Logger::INFO));

$clientStorage = new ClientStorage($logger);
$parser = new Parser(new Cli($pool));

$parser = new Parser(new Cli($pool, $db));
$clientStorage = new ClientStorage($logger);

$webSocket = (new GameWebSocket($parser))->init($clientStorage);

Expand Down
13 changes: 2 additions & 11 deletions cli/ratchet/ws_game.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace ChessServer\Cli\Ratchet;

use ChessServer\Db;
use ChessServer\Command\Parser;
use ChessServer\Command\Game\Cli;
use ChessServer\Socket\Ratchet\ClientStorage;
Expand All @@ -23,20 +22,12 @@

$pool = Pool::create();

$db = new Db([
'driver' => $_ENV['DB_DRIVER'],
'host' => $_ENV['DB_HOST'],
'database' => $_ENV['DB_DATABASE'],
'username' => $_ENV['DB_USERNAME'],
'password' => $_ENV['DB_PASSWORD'],
]);

$logger = new Logger('log');
$logger->pushHandler(new StreamHandler(__DIR__.'/../../storage' . '/game.log', Logger::INFO));

$clientStorage = new ClientStorage($logger);
$parser = new Parser(new Cli($pool));

$parser = new Parser(new Cli($pool, $db));
$clientStorage = new ClientStorage($logger);

$webSocket = (new GameWebSocket($parser))->init($clientStorage);

Expand Down
11 changes: 1 addition & 10 deletions cli/workerman/game.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace ChessServer\Cli\Workerman;

use ChessServer\Db;
use ChessServer\Command\Parser;
use ChessServer\Command\Game\Cli;
use ChessServer\Socket\Workerman\ClientStorage;
Expand All @@ -19,18 +18,10 @@

$pool = Pool::create();

$db = new Db([
'driver' => $_ENV['DB_DRIVER'],
'host' => $_ENV['DB_HOST'],
'database' => $_ENV['DB_DATABASE'],
'username' => $_ENV['DB_USERNAME'],
'password' => $_ENV['DB_PASSWORD'],
]);

$logger = new Logger('game');
$logger->pushHandler(new StreamHandler(GameWebSocket::STORAGE_FOLDER . '/game.log', Logger::INFO));

$parser = new Parser(new Cli($pool, $db));
$parser = new Parser(new Cli($pool));

$clientStorage = new ClientStorage($logger);

Expand Down
11 changes: 5 additions & 6 deletions src/Command/Game/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ class Cli extends AbstractCli
{
private Db $db;

public function __construct(Pool $pool, Db $db)
public function __construct(Pool $pool)
{
parent::__construct();

$this->db = $db;
// text-based commands
$this->commands->attach(new EvalNamesCommand());
$this->commands->attach(new OnlineGamesCommand());
Expand All @@ -26,14 +25,14 @@ public function __construct(Pool $pool, Db $db)
// param-based commands
$this->commands->attach(new AcceptPlayRequestCommand());
$this->commands->attach((new HeuristicCommand())->setPool($pool));
$this->commands->attach(new LeaveCommand($db));
$this->commands->attach((new LeaveCommand())->setPool($pool));
$this->commands->attach(new LegalCommand());
$this->commands->attach(new PlayLanCommand());
$this->commands->attach((new PlayRavCommand())->setPool($pool));
$this->commands->attach(new RandomizerCommand());
$this->commands->attach(new ResignCommand($db));
$this->commands->attach(new RestartCommand($db));
$this->commands->attach(new StartCommand($db));
$this->commands->attach((new ResignCommand())->setPool($pool));
$this->commands->attach((new RestartCommand())->setPool($pool));
$this->commands->attach((new StartCommand())->setPool($pool));
$this->commands->attach((new StockfishCommand())->setPool($pool));
$this->commands->attach(new TutorFenCommand());
}
Expand Down
30 changes: 0 additions & 30 deletions src/Socket/DbReconnectTrait.php

This file was deleted.

7 changes: 0 additions & 7 deletions src/Socket/Ratchet/GameWebSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,14 @@

use ChessServer\Command\Parser;
use ChessServer\Command\Game\GameModeStorage;
use ChessServer\Socket\DbReconnectTrait;
use Ratchet\ConnectionInterface;

class GameWebSocket extends AbstractWebSocket
{
use DbReconnectTrait;

public function __construct(Parser $parser)
{
parent::__construct($parser);

$this->loop->addPeriodicTimer($this->timeInterval, function() {
$this->reconnect();
});

$this->gameModeStorage = new GameModeStorage();
}

Expand Down
9 changes: 0 additions & 9 deletions src/Socket/Workerman/GameWebSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@

use ChessServer\Command\Parser;
use ChessServer\Command\Game\GameModeStorage;
use ChessServer\Socket\DbReconnectTrait;
use Workerman\Timer;

class GameWebSocket extends AbstractWebSocket
{
use DbReconnectTrait;

private GrandmasterMove $gmMove;

private GameModeStorage $gameModeStorage;
Expand All @@ -19,12 +16,6 @@ public function __construct(string $socketName, array $context, Parser $parser)
{
parent::__construct($socketName, $context, $parser);

$this->worker->onWorkerStart = function() {
Timer::add($this->timeInterval, function() {
$this->reconnect();
});
};

$this->gameModeStorage = new GameModeStorage();

$this->connect()->message()->error()->close();
Expand Down

0 comments on commit 3add356

Please sign in to comment.