Skip to content

Commit

Permalink
Updated PlayLanCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
programarivm committed Oct 9, 2024
1 parent 9246c4b commit 2c2e7dc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 32 deletions.
33 changes: 1 addition & 32 deletions src/Command/Game/Mode/PlayMode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use Chess\Variant\Classical\PGN\AN\Color;
use ChessServer\Command\Game\Game;
use ChessServer\Command\Game\PlayLanCommand;
use ChessServer\Repository\User\UserRepository;

class PlayMode extends AbstractMode
{
Expand Down Expand Up @@ -80,7 +78,7 @@ public function setTimer(array $timer)
return $this;
}

protected function updateTimer(string $color)
public function updateTimer(string $color)
{
$now = time();
$diff = $now - $this->updatedAt;
Expand All @@ -94,33 +92,4 @@ protected function updateTimer(string $color)

$this->updatedAt = $now;
}

public function res($params, $cmd)
{
switch (get_class($cmd)) {
case PlayLanCommand::class:
$isValid = $this->game->playLan($params['color'], $params['lan']);
if ($isValid) {
if (isset($this->game->state()->end)) {
(new UserRepository($this->pool))->updateElo(
$this->game->state()->end['result'],
$this->getJwtDecoded()
);
} else {
$this->updateTimer($params['color']);
}
}
return [
$cmd->name => [
...(array) $this->game->state(),
'variant' => $this->game->getVariant(),
'timer' => $this->timer,
'isValid' => $isValid,
],
];

default:
return parent::res($params, $cmd);
}
}
}
21 changes: 21 additions & 0 deletions src/Command/Game/PlayLanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ChessServer\Command\Game;

use ChessServer\Command\AbstractCommand;
use ChessServer\Command\Game\Mode\PlayMode;
use ChessServer\Socket\AbstractSocket;

class PlayLanCommand extends AbstractCommand
Expand All @@ -26,6 +27,26 @@ public function run(AbstractSocket $socket, array $argv, int $id)
$params = json_decode(stripslashes($argv[1]), true);
$gameMode = $socket->getGameModeStorage()->getById($id);

if (get_class($gameMode) === PlayMode::class) {
$isValid = $gameMode->getGame()->playLan($params['color'], $params['lan']);
if ($isValid) {
if (isset($gameMode->getGame()->state()->end)) {
// TODO ...
// Update elo
} else {
$gameMode->updateTimer($params['color']);
}
}
return $socket->getClientStorage()->send($gameMode->getResourceIds(), [
$this->name => [
...(array) $gameMode->getGame()->state(),
'variant' => $gameMode->getGame()->getVariant(),
'timer' => $gameMode->getTimer(),
'isValid' => $isValid,
],
]);
}

return $socket->getClientStorage()->send(
$gameMode->getResourceIds(),
$gameMode->res($params, $this)
Expand Down

0 comments on commit 2c2e7dc

Please sign in to comment.