Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/410 parallelize the start command #417

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
7 changes: 2 additions & 5 deletions src/Command/Game/LeaveCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@

namespace ChessServer\Command\Game;

use ChessServer\Db;
use ChessServer\Command\AbstractCommand;
use ChessServer\Repository\UserRepository;
use ChessServer\Socket\AbstractSocket;

class LeaveCommand extends AbstractCommand
{
public function __construct(Db $db)
public function __construct()
{
parent::__construct($db);

$this->name = '/leave';
$this->description = 'Leaves a game.';
$this->params = [
Expand All @@ -31,7 +28,7 @@ public function run(AbstractSocket $socket, array $argv, int $id)

if ($gameMode = $socket->getGameModeStorage()->getById($id)) {
$gameMode->getGame()->setAbandoned($params['color']);
(new UserRepository($this->db))->updateElo(
(new UserRepository())->updateElo(
$gameMode->getGame()->state()->end['result'],
$gameMode->getJwtDecoded()
);
Expand Down
8 changes: 2 additions & 6 deletions src/Command/Game/Mode/PlayMode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace ChessServer\Command\Game\Mode;

use Chess\Variant\Classical\PGN\AN\Color;
use ChessServer\Db;
use ChessServer\Command\Game\Game;
use ChessServer\Command\Game\PlayLanCommand;
use ChessServer\Repository\UserRepository;
Expand All @@ -18,8 +17,6 @@ class PlayMode extends AbstractMode
const SUBMODE_FRIEND = 'friend';
const SUBMODE_ONLINE = 'online';

protected Db $db;

protected string $status;

protected int $startedAt;
Expand All @@ -28,11 +25,10 @@ class PlayMode extends AbstractMode

protected array $timer;

public function __construct(Game $game, array $resourceIds, string $jwt, Db $db)
public function __construct(Game $game, array $resourceIds, string $jwt)
{
parent::__construct($game, $resourceIds, $jwt);

$this->db = $db;
$this->status = self::STATUS_PENDING;
}

Expand Down Expand Up @@ -106,7 +102,7 @@ public function res($params, $cmd)
$isValid = $this->game->playLan($params['color'], $params['lan']);
if ($isValid) {
if (isset($this->game->state()->end)) {
(new UserRepository($this->db))->updateElo(
(new UserRepository())->updateElo(
$this->game->state()->end['result'],
$this->getJwtDecoded()
);
Expand Down
7 changes: 2 additions & 5 deletions src/Command/Game/ResignCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@

namespace ChessServer\Command\Game;

use ChessServer\Db;
use ChessServer\Command\AbstractCommand;
use ChessServer\Repository\UserRepository;
use ChessServer\Socket\AbstractSocket;

class ResignCommand extends AbstractCommand
{
public function __construct(Db $db)
public function __construct()
{
parent::__construct($db);

$this->name = '/resign';
$this->description = 'Resigns a game.';
$this->params = [
Expand All @@ -30,7 +27,7 @@ public function run(AbstractSocket $socket, array $argv, int $id)
$params = json_decode(stripslashes($argv[1]), true);
$gameMode = $socket->getGameModeStorage()->getById($id);
$gameMode->getGame()->setResignation($params['color']);
(new UserRepository($this->db))->updateElo(
(new UserRepository())->updateElo(
$gameMode->getGame()->state()->end['result'],
$gameMode->getJwtDecoded()
);
Expand Down
8 changes: 2 additions & 6 deletions src/Command/Game/RestartCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Chess\Variant\Chess960\FEN\StrToBoard as Chess960FenStrToBoard;
use Chess\Variant\Classical\PGN\AN\Color;
use ChessServer\Db;
use ChessServer\Command\AbstractCommand;
use ChessServer\Command\Game\Game;
use ChessServer\Command\Game\Mode\PlayMode;
Expand All @@ -13,10 +12,8 @@

class RestartCommand extends AbstractCommand
{
public function __construct(Db $db)
public function __construct()
{
parent::__construct($db);

$this->name = '/restart';
$this->description = 'Restarts an existing game.';
$this->params = [
Expand Down Expand Up @@ -47,8 +44,7 @@ public function run(AbstractSocket $socket, array $argv, int $id)
$newGameMode = (new PlayMode(
$game,
$gameMode->getResourceIds(),
JWT::encode((array) $decoded, $_ENV['JWT_SECRET'], 'HS256'),
$this->db
JWT::encode((array) $decoded, $_ENV['JWT_SECRET'], 'HS256')
))->setStatus(PlayMode::STATUS_ACCEPTED)
->setStartedAt(time())
->setUpdatedAt(time())
Expand Down
8 changes: 2 additions & 6 deletions src/Command/Game/StartCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Chess\Variant\Dunsany\Board as DunsanyBoard;
use Chess\Variant\Losing\Board as LosingBoard;
use Chess\Variant\RacingKings\Board as RacingKingsBoard;
use ChessServer\Db;
use ChessServer\Command\AbstractCommand;
use ChessServer\Command\Game\Mode\AnalysisMode;
use ChessServer\Command\Game\Mode\PlayMode;
Expand All @@ -22,10 +21,8 @@

class StartCommand extends AbstractCommand
{
public function __construct(Db $db)
public function __construct()
{
parent::__construct($db);

$this->name = '/start';
$this->description = 'Starts a new game.';
$this->params = [
Expand Down Expand Up @@ -162,8 +159,7 @@ public function run(AbstractSocket $socket, array $argv, int $id)
$gameMode = new PlayMode(
$game,
[$id],
JWT::encode($payload, $_ENV['JWT_SECRET'], 'HS256'),
$this->db
JWT::encode($payload, $_ENV['JWT_SECRET'], 'HS256')
);
$socket->getGameModeStorage()->set($gameMode);
if ($params['settings']['submode'] === PlayMode::SUBMODE_ONLINE) {
Expand Down
46 changes: 46 additions & 0 deletions src/Repository/UpdateEloAsyncTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace ChessServer\Repository;

use ChessServer\Db;
use Spatie\Async\Task;

class UpdateEloAsyncTask extends Task
{
private array $params;

private array $env;

private Db $db;

public function __construct(array $params, array $env)
{
$this->params = $params;
$this->env = $env;
}

public function configure()
{
$this->db = new Db($this->env['db']);
}

public function run()
{
$sql = "UPDATE users SET elo = :elo WHERE username = :username";

$values= [
[
'param' => ":username",
'value' => $this->params['username'],
'type' => \PDO::PARAM_STR,
],
[
'param' => ":elo",
'value' => $this->params['elo'],
'type' => \PDO::PARAM_INT,
],
];

return $this->db->query($sql, $values);
}
}
Loading