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

Implemented RankingCommand #375

Merged
merged 1 commit into from
Sep 17, 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
3 changes: 2 additions & 1 deletion src/Command/Data/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ public function __construct(Db $db)
$this->commands->attach(new AutocompleteBlackCommand($db));
$this->commands->attach(new AutocompleteEventCommand($db));
$this->commands->attach(new AutocompleteWhiteCommand($db));
$this->commands->attach(new RankingCommand($db));
$this->commands->attach(new ResultCommand($db));
$this->commands->attach(new ResultEventCommand($db));
$this->commands->attach(new ResultPlayerCommand($db));
$this->commands->attach(new ResultCommand($db));
$this->commands->attach(new SearchCommand($db));
}

Expand Down
34 changes: 34 additions & 0 deletions src/Command/Data/RankingCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace ChessServer\Command\Data;

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

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

$this->name = '/ranking';
$this->description = 'Top players by ELO.';
}

public function validate(array $argv)
{
return count($argv) - 1 === 0;
}

public function run(AbstractSocket $socket, array $argv, int $id)
{
$sql = "SELECT username, elo FROM users WHERE lastLoginAt IS NOT NULL ORDER BY elo DESC LIMIT 100";

$arr = $this->db->query($sql)->fetchAll(\PDO::FETCH_ASSOC);

return $socket->getClientStorage()->send([$id], [
$this->name => $arr,
]);
}
}