Skip to content

Commit

Permalink
Replaced hash with uid
Browse files Browse the repository at this point in the history
  • Loading branch information
programarivm committed Sep 17, 2024
1 parent f432ab2 commit d983048
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/Command/Game/AcceptPlayRequestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function run(AbstractSocket $socket, array $argv, int $id)
{
$params = json_decode(stripslashes($argv[1]), true);

$gameMode = $socket->getGameModeStorage()->getByHash($params['hash']);
$gameMode = $socket->getGameModeStorage()->getByUid($params['uid']);

if (!$gameMode) {
return $socket->getClientStorage()->send([$id], [
Expand Down Expand Up @@ -64,8 +64,8 @@ public function run(AbstractSocket $socket, array $argv, int $id)
}
return $socket->getClientStorage()->send($ids, [
$this->name => [
'uid' => $gameMode->getUid(),
'jwt' => $gameMode->getJwt(),
'hash' => hash('adler32', $gameMode->getJwt()),
'timer' => $gameMode->getTimer(),
'startedAt' => $gameMode->getStartedAt(),
],
Expand Down
6 changes: 3 additions & 3 deletions src/Command/Game/GameModeStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public function getById(int $resourceId): ?AbstractMode
return null;
}

public function getByHash(string $hash): ?AbstractMode
public function getByUid(string $uid): ?AbstractMode
{
$this->rewind();
while ($this->valid()) {
if ($hash === $this->current()->getHash()) {
if ($uid === $this->current()->getUid()) {
return $this->current();
}
$this->next();
Expand Down Expand Up @@ -55,7 +55,7 @@ public function decodeByPlayMode(string $status, string $submode): array
if ($this->current()->getStatus() === $status) {
$decoded = $this->current()->getJwtDecoded();
if ($decoded->submode === $submode) {
$decoded->hash = $this->current()->getHash();
$decoded->uid = $this->current()->getUid();
$decoded->jwt = $this->current()->getJwt();
$items[] = $decoded;
}
Expand Down
9 changes: 4 additions & 5 deletions src/Command/Game/Mode/AbstractMode.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ abstract class AbstractMode

protected string $jwt;

protected string $hash;
protected string $uid;

public function __construct(Game $game, array $resourceIds, string $jwt = '')
{
$this->jwt = $jwt;
$this->hash = $jwt ? hash('adler32', $jwt) : '';
$this->uid = $jwt ? hash('adler32', $jwt) : '';
$this->game = $game;
$this->resourceIds = $resourceIds;
}
Expand Down Expand Up @@ -60,7 +60,6 @@ public function getJwt()
public function setJwt(array $payload)
{
$this->jwt = JWT::encode($payload, $_ENV['JWT_SECRET'], 'HS256');
$this->hash = hash('adler32', $this->jwt);

return $this;
}
Expand All @@ -70,9 +69,9 @@ public function getJwtDecoded()
return JWT::decode($this->jwt, new Key($_ENV['JWT_SECRET'], 'HS256'));
}

public function getHash()
public function getUid()
{
return $this->hash;
return $this->uid;
}

public function res($params, $cmd)
Expand Down
3 changes: 2 additions & 1 deletion src/Command/Game/StartCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ public function run(AbstractSocket $socket, array $argv, int $id)
: []
),
];
$payload['uid'] = hash('adler32', json_encode($payload));
$gameMode = new PlayMode(
$game,
[$id],
Expand All @@ -177,11 +178,11 @@ public function run(AbstractSocket $socket, array $argv, int $id)
}
return $socket->getClientStorage()->send([$id], [
$this->name => [
'uid' => $gameMode->getUid(),
'variant' => $game->getVariant(),
'mode' => $game->getMode(),
'fen' => $game->getBoard()->toFen(),
'jwt' => $gameMode->getJwt(),
'hash' => $gameMode->getHash(),
...($params['variant'] === Game::VARIANT_960
? ['startPos' => implode('', $game->getBoard()->getStartPos())]
: []
Expand Down

0 comments on commit d983048

Please sign in to comment.