Skip to content

Commit

Permalink
Merge pull request #1 from csgo-league/develop
Browse files Browse the repository at this point in the history
merge upstream
  • Loading branch information
Shaneajm authored Mar 10, 2021
2 parents 2581aca + 21a72d1 commit 74d4c82
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 76 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ The steps below are all written with the presumption that you're using Ubuntu.
2. Now we need to make our database and a user that can connect to it.
```
CREATE USER 'league'@'%' IDENTIFIED BY '{password}';
CREATE DATABASE panel;
CREATE DATABASE panel CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
GRANT ALL PRIVILEGES ON panel.* TO 'league'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
```
Expand Down
3 changes: 3 additions & 0 deletions app/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ protected function registerRoutes()
// Authorised match endpoints
Route::post('/match/start', MatchController::class . '@startMatch');
Route::get('/match/end/{matchId}', MatchController::class . '@endMatch');
Route::get('/match/status/{matchId}', MatchController::class . '@isMatchLive');
Route::get('/match/status', MatchController::class . '@getMatchesStatus');
Route::get('/match/scoreboard/{matchId}', MatchController::class . '@getMatchScoreboard');
});


Expand Down
63 changes: 17 additions & 46 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@babel/core": "^7.7.5",
"@babel/preset-env": "^7.7.6",
"@babel/register": "^7.7.4",
"axios": "^0.18.1",
"axios": "^0.21.1",
"babel-core": "^6.26.3",
"babel-loader": "^8.0.6",
"babel-polyfill": "^6.26.0",
Expand Down
54 changes: 34 additions & 20 deletions src/Controllers/MatchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,30 +112,44 @@ public function getMatch(string $matchId): string
*/
public function endMatch(string $matchId): string
{
$input = input()->all();
return response()->json(
$this->matchHelper->endMatch($matchId)
);
}

if (empty($input['ip'])) {
return response()->json([
'success' => false,
'error' => 'ip_empty'
]);
} elseif (!array_key_exists('port', $input)) {
return response()->json([
'success' => false,
'error' => 'port_missing'
]);
} elseif (empty($input['port'])) {
return response()->json([
'success' => false,
'error' => 'port_empty'
]);
}
/**
* Check whether the match is live.
*
* @param string $matchId
* @return string
*/
public function isMatchLive(string $matchId): string
{
return response()->json(
$this->matchHelper->checkLive($matchId)
);
}

$ip = $input['ip'];
$port = $input['port'];
/**
* Check whether the matches are live.
*
* @return string
*/
public function getMatchesStatus(): string
{
return response()->json(
$this->matchHelper->getMatchesStatus()
);
}

/**
* @param string $matchId
* @return string
*/
public function getMatchScoreboard(string $matchId): string
{
return response()->json(
$this->matchHelper->endMatch($matchId, $ip, $port)
$this->matchHelper->getMatchScoreboard($matchId)
);
}
}
139 changes: 135 additions & 4 deletions src/Helpers/MatchHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,41 @@ public function getMatchPlayers(string $matchId): ?array
return $matchPlayers ? $this->formatMatchPlayers($matchPlayers) : null;
}

/**
* @param string $matchId
* @return array
*/
public function getMatchScoreboard(string $matchId): array
{
$query = $this->db->query('
SELECT DISTINCT
players.discord,
matches_players.team,
matches_players.kills,
matches_players.deaths,
matches_players.assists,
matches_players.playerscore,
matches_maps.team1_score,
matches_maps.team2_score
FROM matches
LEFT JOIN matches_maps ON matches_maps.matchid = matches.matchid
LEFT JOIN matches_players ON matches_players.matchid = matches.matchid
LEFT JOIN players ON matches_players.steam = players.steam
WHERE matches_maps.matchid = :matchId
ORDER BY matches_players.playerscore DESC
', [
':matchId' => $matchId,
]);

$matchScoreboard = $query->fetchAll();

if (!$matchScoreboard) {
return [];
}

return $this->formatMatchPlayer($matchScoreboard);
}

/**
* @param array $players
* @return array
Expand Down Expand Up @@ -116,7 +151,9 @@ protected function formatMatchPlayer(array $player): array
$player['kdr'] = 0;
}

$player['name'] = htmlspecialchars(substr($player['name'], 0, 32));
if (array_key_exists('name', $player) && !empty($player['name'])) {
$player['name'] = htmlspecialchars(substr($player['name'], 0, 32));
}

return $player;
}
Expand Down Expand Up @@ -191,17 +228,58 @@ public function getMatch(string $matchId): array
* @throws RconAuthException
* @throws RconConnectException
*/
public function endMatch(string $matchId, string $ip, string $port): array
public function endMatch(string $matchId): array
{
$server = new Rcon($ip, $port, env('RCON'));
$query = $this->db->query('
SELECT
end_time,
server_ip,
server_port
FROM matches
WHERE matches.matchid = :matchId
', [
':matchId' => $matchId,
]);

$match = $query->fetch();

if (!$match) {
return [
'success' => false,
'error' => 'Match not found',
];
}

if ($match['end_time']) {
return [
'success' => false,
'error' => 'Match is already over',
];
}

if (!array_key_exists('server_ip', $match) || !$match['server_ip']) {
return [
'success' => false,
'error' => 'server_ip does not exist or is not valid',
];
}

if (!array_key_exists('server_port', $match) || !$match['server_port']) {
return [
'success' => false,
'error' => 'server_port does not exist or is not valid',
];
}

$server = new Rcon($match['server_ip'], $match['server_port'], env('RCON'));
$server->connect();

$server->exec('get5_endmatch; map de_mirage');

$matchConfig = self::MATCHES_CACHE . "/$matchId.json";

return [
'success' => unlink($matchConfig)
'success' => unlink($matchConfig),
];
}

Expand Down Expand Up @@ -339,4 +417,57 @@ protected function getMostRecentMatchId(): int

return $response['match_id'] ?? 1;
}

/**
* @param string $matchId
* @return array
*/
public function checkLive(string $matchId): array
{
return [
'success' => true,
'live' => $this->isMatchLive($matchId)
];
}

/**
* Return whether the match is live.
*
* @param string $matchId
* @return bool
*/
public function isMatchLive(string $matchId): bool
{
$query = $this->db->query('
SELECT
end_time
FROM matches
WHERE matches.matchid = :matchId
AND matches.end_time IS NULL
', [
':matchId' => $matchId,
]);

return $query->rowCount() !== 0;
}

/**
* Return matches status.
*
* @return array
*/
public function getMatchesStatus(): array
{
$query = $this->db->query('SELECT matchid, end_time FROM matches');

$matches = $query->fetchAll();

$response = [];

foreach ($matches as $match) {
$response[$match['matchid']] = !isset($match['end_time']);
}

return $response;
}
}
Loading

0 comments on commit 74d4c82

Please sign in to comment.