mirror of
https://codeberg.org/demostf/api.git
synced 2026-06-03 18:04:08 +02:00
cleanup
This commit is contained in:
parent
5986d8552d
commit
73469d2aa1
29 changed files with 140 additions and 149 deletions
|
|
@ -4,7 +4,6 @@ declare(strict_types=1);
|
|||
|
||||
namespace Demostf\API;
|
||||
|
||||
use Demostf\API\Controllers\TempController;
|
||||
use Demostf\API\Demo\DemoSaver;
|
||||
use Demostf\API\Demo\DemoStore;
|
||||
use Demostf\API\Demo\HeaderParser;
|
||||
|
|
@ -92,7 +91,7 @@ class Container {
|
|||
}
|
||||
|
||||
public function getRawParser(): RawParser {
|
||||
return new RawParser($this->getParserPath(), new TempController($this->getApiRoot() . '/temp/'));
|
||||
return new RawParser($this->getParserPath());
|
||||
}
|
||||
|
||||
public function getUploadProvider(): UploadProvider {
|
||||
|
|
|
|||
|
|
@ -7,8 +7,10 @@ namespace Demostf\API\Controllers;
|
|||
use Ehesp\SteamLogin\SteamLogin;
|
||||
use Demostf\API\Providers\AuthProvider;
|
||||
use Demostf\API\Providers\UserProvider;
|
||||
use Flight;
|
||||
use flight\net\Request;
|
||||
use flight\net\Response;
|
||||
use SteamId;
|
||||
|
||||
class AuthController extends BaseController {
|
||||
/**
|
||||
|
|
@ -47,7 +49,7 @@ class AuthController extends BaseController {
|
|||
|
||||
public function get($token) {
|
||||
$userData = $this->authProvider->getUser($token);
|
||||
\Flight::json([
|
||||
Flight::json([
|
||||
'token' => $token,
|
||||
'steamid' => $userData['steamid'],
|
||||
'name' => $userData['name'],
|
||||
|
|
@ -59,12 +61,12 @@ class AuthController extends BaseController {
|
|||
$_SESSION['return'] = $this->query('return', 'https://' . $this->host);
|
||||
$steam = new SteamLogin();
|
||||
$url = $steam->url($this->apiRoot . '/auth/handle/' . urlencode($token), $this->apiRoot);
|
||||
\Flight::redirect(str_replace('&', '&', $url)); // headers make no sense
|
||||
Flight::redirect(str_replace('&', '&', $url)); // headers make no sense
|
||||
}
|
||||
|
||||
public function logout($token) {
|
||||
$this->authProvider->logout($token);
|
||||
\Flight::json([
|
||||
Flight::json([
|
||||
'token' => $token,
|
||||
'steamid' => null,
|
||||
'name' => null,
|
||||
|
|
@ -78,10 +80,10 @@ class AuthController extends BaseController {
|
|||
$steam = new SteamLogin();
|
||||
$steamId = $steam->validate();
|
||||
if ($steamId) {
|
||||
$steamIdObject = new \SteamId($steamId);
|
||||
$steamIdObject = new SteamId($steamId);
|
||||
$key = $this->userProvider->store($steamIdObject);
|
||||
$this->authProvider->setUser($token, $steamIdObject, $key);
|
||||
}
|
||||
\Flight::redirect($return);
|
||||
Flight::redirect($return);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,9 @@ use Demostf\API\Providers\DemoListProvider;
|
|||
use Demostf\API\Providers\DemoProvider;
|
||||
use flight\net\Request;
|
||||
use flight\net\Response;
|
||||
use function intval;
|
||||
use InvalidArgumentException;
|
||||
use function is_array;
|
||||
|
||||
class DemoController extends BaseController {
|
||||
/** @var DemoProvider */
|
||||
|
|
@ -45,7 +48,7 @@ class DemoController extends BaseController {
|
|||
* @param string $id
|
||||
*/
|
||||
public function get($id) {
|
||||
$this->json($this->demoProvider->get(\intval($id, 10)));
|
||||
$this->json($this->demoProvider->get(intval($id, 10)));
|
||||
}
|
||||
|
||||
protected function getFilter() {
|
||||
|
|
@ -61,7 +64,7 @@ class DemoController extends BaseController {
|
|||
$filter['backend'] = $backend;
|
||||
}
|
||||
if ($players) {
|
||||
if (!\is_array($players)) {
|
||||
if (!is_array($players)) {
|
||||
$players = explode(',', $players);
|
||||
}
|
||||
$players = array_filter($players);
|
||||
|
|
@ -123,7 +126,7 @@ class DemoController extends BaseController {
|
|||
$url = (string) $this->post('url', '');
|
||||
$editKey = (string) $this->post('key', '');
|
||||
if ($editKey !== $this->editKey || '' === $editKey) {
|
||||
throw new \InvalidArgumentException('Invalid key');
|
||||
throw new InvalidArgumentException('Invalid key');
|
||||
}
|
||||
|
||||
$demo = $this->demoProvider->get((int) $id);
|
||||
|
|
@ -135,7 +138,7 @@ class DemoController extends BaseController {
|
|||
$this->store->remove($demo);
|
||||
}
|
||||
} else {
|
||||
throw new \InvalidArgumentException('Invalid demo hash');
|
||||
throw new InvalidArgumentException('Invalid demo hash');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||
namespace Demostf\API\Controllers;
|
||||
|
||||
use Demostf\API\Providers\InfoProvider;
|
||||
use Flight;
|
||||
use flight\net\Request;
|
||||
use flight\net\Response;
|
||||
|
||||
|
|
@ -18,10 +19,10 @@ class InfoController extends BaseController {
|
|||
}
|
||||
|
||||
public function listMaps() {
|
||||
\Flight::json($this->infoProvider->listMaps());
|
||||
Flight::json($this->infoProvider->listMaps());
|
||||
}
|
||||
|
||||
public function stats() {
|
||||
\Flight::json($this->infoProvider->getStats());
|
||||
Flight::json($this->infoProvider->getStats());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,53 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl>
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace Demostf\API\Controllers;
|
||||
|
||||
class TempController extends BaseController {
|
||||
private $webRoot;
|
||||
|
||||
public function __construct(string $webRoot) {
|
||||
$this->webRoot = $webRoot;
|
||||
}
|
||||
|
||||
public function register(string $key, string $path): string {
|
||||
apcu_store($key, $path);
|
||||
|
||||
return $this->webRoot . $key;
|
||||
}
|
||||
|
||||
public function unregister(string $key) {
|
||||
apcu_dec($key);
|
||||
}
|
||||
|
||||
public function serve(string $key) {
|
||||
$path = apcu_fetch($key);
|
||||
if ($path) {
|
||||
$handle = fopen($path, 'r');
|
||||
fpassthru($handle);
|
||||
fclose($handle);
|
||||
} else {
|
||||
\Flight::response()
|
||||
->status(404)
|
||||
->send();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -5,6 +5,8 @@ declare(strict_types=1);
|
|||
namespace Demostf\API\Controllers;
|
||||
|
||||
use Demostf\API\Providers\UploadProvider;
|
||||
use Exception;
|
||||
use Flight;
|
||||
use flight\net\Request;
|
||||
use flight\net\Response;
|
||||
|
||||
|
|
@ -32,12 +34,12 @@ class UploadController extends BaseController {
|
|||
try {
|
||||
$result = $this->uploadProvider->upload($key, $red, $blu, $name, $demoFile);
|
||||
if ('Invalid key' === $result) {
|
||||
\Flight::response()->status(401)->write($result)->send();
|
||||
Flight::response()->status(401)->write($result)->send();
|
||||
} else {
|
||||
echo $result;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
\Flight::response()
|
||||
} catch (Exception $e) {
|
||||
Flight::response()
|
||||
->status(500)
|
||||
->write($e->getMessage())
|
||||
->send();
|
||||
|
|
|
|||
|
|
@ -6,8 +6,10 @@ namespace Demostf\API\Controllers;
|
|||
|
||||
use Demostf\API\Demo\Parser;
|
||||
use Demostf\API\Providers\UserProvider;
|
||||
use Flight;
|
||||
use flight\net\Request;
|
||||
use flight\net\Response;
|
||||
use InvalidArgumentException;
|
||||
|
||||
class UserController extends BaseController {
|
||||
/**
|
||||
|
|
@ -24,14 +26,14 @@ class UserController extends BaseController {
|
|||
if (!is_numeric($steamId)) {
|
||||
try {
|
||||
$steamId = Parser::convertSteamIdToCommunityId($steamId);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
} catch (InvalidArgumentException $e) {
|
||||
}
|
||||
}
|
||||
\Flight::json($this->userProvider->get($steamId));
|
||||
Flight::json($this->userProvider->get($steamId));
|
||||
}
|
||||
|
||||
public function search() {
|
||||
$query = $this->query('query', '');
|
||||
\Flight::json($this->userProvider->search($query));
|
||||
Flight::json($this->userProvider->search($query));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,9 @@ declare(strict_types=1);
|
|||
|
||||
namespace Demostf\API\Data;
|
||||
|
||||
class DemoPlayer implements \JsonSerializable {
|
||||
use JsonSerializable;
|
||||
|
||||
class DemoPlayer implements JsonSerializable {
|
||||
/** @var int */
|
||||
private $id;
|
||||
/** @var int */
|
||||
|
|
|
|||
|
|
@ -4,7 +4,9 @@ declare(strict_types=1);
|
|||
|
||||
namespace Demostf\API\Data;
|
||||
|
||||
class SteamUser implements \JsonSerializable {
|
||||
use JsonSerializable;
|
||||
|
||||
class SteamUser implements JsonSerializable {
|
||||
/** @var int */
|
||||
private $id;
|
||||
/** @var string */
|
||||
|
|
|
|||
|
|
@ -4,7 +4,9 @@ declare(strict_types=1);
|
|||
|
||||
namespace Demostf\API\Data;
|
||||
|
||||
class User implements \JsonSerializable {
|
||||
use JsonSerializable;
|
||||
|
||||
class User implements JsonSerializable {
|
||||
/** @var int */
|
||||
private $id;
|
||||
/** @var string */
|
||||
|
|
|
|||
|
|
@ -4,7 +4,9 @@ declare(strict_types=1);
|
|||
|
||||
namespace Demostf\API\Demo;
|
||||
|
||||
class ChatMessage implements \JsonSerializable {
|
||||
use JsonSerializable;
|
||||
|
||||
class ChatMessage implements JsonSerializable {
|
||||
/** @var string */
|
||||
private $user;
|
||||
|
||||
|
|
|
|||
|
|
@ -4,10 +4,12 @@ declare(strict_types=1);
|
|||
|
||||
namespace Demostf\API\Demo;
|
||||
|
||||
use DateTime;
|
||||
use Demostf\API\Data\DemoPlayer;
|
||||
use Demostf\API\Data\User;
|
||||
use JsonSerializable;
|
||||
|
||||
class Demo implements \JsonSerializable {
|
||||
class Demo implements JsonSerializable {
|
||||
/** @var int */
|
||||
private $id;
|
||||
/** @var string */
|
||||
|
|
@ -22,7 +24,7 @@ class Demo implements \JsonSerializable {
|
|||
private $nick;
|
||||
/** @var string */
|
||||
private $map;
|
||||
/** @var \DateTime */
|
||||
/** @var DateTime */
|
||||
private $time;
|
||||
/** @var string */
|
||||
private $red;
|
||||
|
|
@ -55,7 +57,7 @@ class Demo implements \JsonSerializable {
|
|||
float $duration,
|
||||
string $nick,
|
||||
string $map,
|
||||
\DateTime $time,
|
||||
DateTime $time,
|
||||
string $red,
|
||||
string $blue,
|
||||
int $redScore,
|
||||
|
|
@ -113,7 +115,7 @@ class Demo implements \JsonSerializable {
|
|||
return $this->map;
|
||||
}
|
||||
|
||||
public function getTime(): \DateTime {
|
||||
public function getTime(): DateTime {
|
||||
return $this->time;
|
||||
}
|
||||
|
||||
|
|
@ -158,7 +160,7 @@ class Demo implements \JsonSerializable {
|
|||
(int) $row['duration'],
|
||||
$row['nick'],
|
||||
$row['map'],
|
||||
\DateTime::createFromFormat('U', '' . strtotime($row['created_at'])),
|
||||
DateTime::createFromFormat('U', '' . strtotime($row['created_at'])),
|
||||
$row['red'],
|
||||
$row['blu'],
|
||||
(int) $row['scoreRed'],
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ declare(strict_types=1);
|
|||
|
||||
namespace Demostf\API\Demo;
|
||||
|
||||
use function count;
|
||||
use DateTime;
|
||||
use Demostf\API\Data\Kill;
|
||||
use Demostf\API\Data\ParsedDemo;
|
||||
use Demostf\API\Data\Player;
|
||||
|
|
@ -59,12 +61,12 @@ class DemoSaver {
|
|||
$header->getDuration(),
|
||||
$header->getNick(),
|
||||
$header->getMap(),
|
||||
new \DateTime(),
|
||||
new DateTime(),
|
||||
$upload->getRed(),
|
||||
$upload->getBlue(),
|
||||
$demo->getRedScore(),
|
||||
$demo->getBlueScore(),
|
||||
\count($demo->getPlayers()),
|
||||
count($demo->getPlayers()),
|
||||
$upload->getUploaderId(),
|
||||
$upload->getHash(),
|
||||
$storedDemo->getBackend(),
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||
namespace Demostf\API\Demo;
|
||||
|
||||
use Demostf\API\Data\StoredDemo;
|
||||
use function dirname;
|
||||
|
||||
class DemoStore {
|
||||
/** @var string */
|
||||
|
|
@ -19,8 +20,8 @@ class DemoStore {
|
|||
|
||||
public function store(string $sourcePath, string $name): StoredDemo {
|
||||
$target = $this->generatePath($name);
|
||||
if (!is_dir(\dirname($target))) {
|
||||
mkdir(\dirname($target), 0777, true);
|
||||
if (!is_dir(dirname($target))) {
|
||||
mkdir(dirname($target), 0777, true);
|
||||
}
|
||||
rename($sourcePath, $target);
|
||||
chmod($target, 0755);
|
||||
|
|
|
|||
|
|
@ -4,11 +4,13 @@ declare(strict_types=1);
|
|||
|
||||
namespace Demostf\API\Demo;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
class HeaderParser {
|
||||
/**
|
||||
* @param string $head string containing the demo header binary data
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
* @throws InvalidArgumentException
|
||||
*
|
||||
* @return Header
|
||||
*/
|
||||
|
|
@ -18,7 +20,7 @@ class HeaderParser {
|
|||
$head
|
||||
);
|
||||
if (!isset($info['type']) || 'HL2DEMO' !== $info['type']) {
|
||||
throw new \InvalidArgumentException('Not an HL2 demo');
|
||||
throw new InvalidArgumentException('Not an HL2 demo');
|
||||
}
|
||||
|
||||
return Header::fromArray($info);
|
||||
|
|
@ -29,7 +31,7 @@ class HeaderParser {
|
|||
*
|
||||
* @param resource $stream
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
* @throws InvalidArgumentException
|
||||
*
|
||||
* @return Header
|
||||
*/
|
||||
|
|
@ -44,13 +46,13 @@ class HeaderParser {
|
|||
*
|
||||
* @param string $path
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
* @throws InvalidArgumentException
|
||||
*
|
||||
* @return Header
|
||||
*/
|
||||
public function parseHeader(string $path): Header {
|
||||
if (!is_readable($path)) {
|
||||
throw new \InvalidArgumentException('Unable to open demo: ' . $path);
|
||||
throw new InvalidArgumentException('Unable to open demo: ' . $path);
|
||||
}
|
||||
$fh = fopen($path, 'r');
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,9 @@ namespace Demostf\API\Demo;
|
|||
use Demostf\API\Data\ParsedDemo;
|
||||
use Demostf\API\Data\ParsedKill;
|
||||
use Demostf\API\Data\ParsedPlayer;
|
||||
use Exception;
|
||||
use InvalidArgumentException;
|
||||
use function is_array;
|
||||
|
||||
/**
|
||||
* Higher level parser.
|
||||
|
|
@ -35,10 +38,10 @@ class Parser {
|
|||
|
||||
public function analyse(string $path): ParsedDemo {
|
||||
$data = $this->rawParser->parse($path);
|
||||
if (\is_array($data) && isset($data['intervalPerTick'])) {
|
||||
if (is_array($data) && isset($data['intervalPerTick'])) {
|
||||
return $this->handleData($data);
|
||||
} else {
|
||||
throw new \InvalidArgumentException('Error parsing demo');
|
||||
throw new InvalidArgumentException('Error parsing demo');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -52,7 +55,7 @@ class Parser {
|
|||
$players = [];
|
||||
|
||||
if (!isset($data['rounds'])) {
|
||||
throw new \Exception("Error while parsing demo, no rounds field found\n" . json_encode($data));
|
||||
throw new Exception("Error while parsing demo, no rounds field found\n" . json_encode($data));
|
||||
}
|
||||
foreach ($data['rounds'] as $round) {
|
||||
if ('red' === $round['winner']) {
|
||||
|
|
@ -63,7 +66,7 @@ class Parser {
|
|||
}
|
||||
|
||||
if (!isset($data['chat'])) {
|
||||
throw new \Exception('Error while parsing demo, no chat field found');
|
||||
throw new Exception('Error while parsing demo, no chat field found');
|
||||
}
|
||||
foreach ($data['chat'] as $message) {
|
||||
if (isset($message['from'])) {
|
||||
|
|
@ -73,7 +76,7 @@ class Parser {
|
|||
}
|
||||
|
||||
if (!isset($data['users'])) {
|
||||
throw new \Exception('Error while parsing demo, no users field found');
|
||||
throw new Exception('Error while parsing demo, no users field found');
|
||||
}
|
||||
|
||||
$deaths = array_filter($data['deaths'], function ($death) {
|
||||
|
|
@ -146,14 +149,14 @@ class Parser {
|
|||
* @param string $steamId The SteamID string as used on servers, like
|
||||
* <var>STEAM_0:0:12345</var>
|
||||
*
|
||||
* @throws \InvalidArgumentException if the SteamID doesn't have the correct
|
||||
* @throws InvalidArgumentException if the SteamID doesn't have the correct
|
||||
* format
|
||||
*
|
||||
* @return string The converted 64bit numeric SteamID
|
||||
*/
|
||||
public static function convertSteamIdToCommunityId(string $steamId): string {
|
||||
if ('STEAM_ID_LAN' === $steamId || 'BOT' === $steamId) {
|
||||
throw new \InvalidArgumentException("Cannot convert SteamID \"$steamId\" to a community ID.");
|
||||
throw new InvalidArgumentException("Cannot convert SteamID \"$steamId\" to a community ID.");
|
||||
}
|
||||
if (preg_match('/^STEAM_[0-1]:[0-1]:[0-9]+$/', $steamId)) {
|
||||
$steamParts = explode(':', substr($steamId, 8));
|
||||
|
|
@ -166,7 +169,7 @@ class Parser {
|
|||
|
||||
return '7656' . $steamId;
|
||||
} else {
|
||||
throw new \InvalidArgumentException("SteamID \"$steamId\" doesn't have the correct format.");
|
||||
throw new InvalidArgumentException("SteamID \"$steamId\" doesn't have the correct format.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace Demostf\API\Demo;
|
||||
|
||||
use Demostf\API\Controllers\TempController;
|
||||
use Exception;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
|
||||
/**
|
||||
|
|
@ -16,25 +16,27 @@ class RawParser {
|
|||
/** @var string */
|
||||
private $parserPath;
|
||||
|
||||
private $tempController;
|
||||
|
||||
public function __construct(string $parserPath, TempController $tempController) {
|
||||
public function __construct(string $parserPath) {
|
||||
$this->parserPath = $parserPath;
|
||||
$this->tempController = $tempController;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @return array|null
|
||||
* @throws Exception
|
||||
*/
|
||||
public function parse(string $path): ?array {
|
||||
try {
|
||||
$command = $this->parserPath . ' ' . escapeshellarg($path);
|
||||
$output = shell_exec($command);
|
||||
$result = \GuzzleHttp\json_decode($output, true);
|
||||
if (null === $result) {
|
||||
throw new \Exception('Failed to parse demo, unexpected result from parser');
|
||||
throw new Exception('Failed to parse demo, unexpected result from parser');
|
||||
} else {
|
||||
return $result;
|
||||
}
|
||||
} catch (RequestException $e) {
|
||||
throw new \Exception('Failed to parse demo, ' . $e->getMessage() . ' ' . $url);
|
||||
throw new Exception('Failed to parse demo, ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ namespace Demostf\API\Providers;
|
|||
|
||||
use Doctrine\DBAL\Connection;
|
||||
use RandomLib\Generator;
|
||||
use SteamId;
|
||||
|
||||
class AuthProvider extends BaseProvider {
|
||||
/**
|
||||
|
|
@ -22,7 +23,7 @@ class AuthProvider extends BaseProvider {
|
|||
return $this->generator->generateString(32, Generator::CHAR_ALNUM);
|
||||
}
|
||||
|
||||
public function setUser(string $token, \SteamId $steamid, string $key) {
|
||||
public function setUser(string $token, SteamId $steamid, string $key) {
|
||||
apcu_store($token, [
|
||||
'name' => $steamid->getNickname(),
|
||||
'steamid' => $steamid->getSteamId64(),
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ class BaseProvider {
|
|||
protected $connection;
|
||||
|
||||
/**
|
||||
* @var \LessQL\Database
|
||||
* @var Database
|
||||
*/
|
||||
protected $db;
|
||||
|
||||
|
|
|
|||
|
|
@ -5,13 +5,14 @@ declare(strict_types=1);
|
|||
namespace Demostf\API\Providers;
|
||||
|
||||
use Demostf\API\Demo\ChatMessage;
|
||||
use PDO;
|
||||
|
||||
class ChatProvider extends BaseProvider {
|
||||
public function getChat(int $demoId) {
|
||||
$query = $this->getQueryBuilder();
|
||||
$query->select('text', '"from"', 'time')
|
||||
->from('chat')
|
||||
->where($query->expr()->eq('demo_id', $query->createNamedParameter($demoId, \PDO::PARAM_INT)));
|
||||
->where($query->expr()->eq('demo_id', $query->createNamedParameter($demoId, PDO::PARAM_INT)));
|
||||
|
||||
$result = $query->execute();
|
||||
|
||||
|
|
@ -28,10 +29,10 @@ class ChatProvider extends BaseProvider {
|
|||
$query = $this->getQueryBuilder();
|
||||
$query->insert('chat')
|
||||
->values([
|
||||
'demo_id' => $query->createNamedParameter($demoId, \PDO::PARAM_INT),
|
||||
'demo_id' => $query->createNamedParameter($demoId, PDO::PARAM_INT),
|
||||
'text' => $query->createNamedParameter($message->getMessage()),
|
||||
'"from"' => $query->createNamedParameter($message->getUser()),
|
||||
'time' => $query->createNamedParameter($message->getTime(), \PDO::PARAM_INT),
|
||||
'time' => $query->createNamedParameter($message->getTime(), PDO::PARAM_INT),
|
||||
'created_at' => 'now()',
|
||||
'updated_at' => 'now()',
|
||||
]);
|
||||
|
|
|
|||
|
|
@ -4,8 +4,11 @@ declare(strict_types=1);
|
|||
|
||||
namespace Demostf\API\Providers;
|
||||
|
||||
use function count;
|
||||
use Demostf\API\Demo\Demo;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use function is_array;
|
||||
use PDO;
|
||||
|
||||
class DemoListProvider extends BaseProvider {
|
||||
public function listUploads(string $steamId, int $page, array $where = []) {
|
||||
|
|
@ -30,14 +33,14 @@ class DemoListProvider extends BaseProvider {
|
|||
->groupBy('demo_id')
|
||||
->having($query->expr()->eq(
|
||||
'COUNT(user_id)',
|
||||
$query->createNamedParameter(\count($userIds, \PDO::PARAM_INT))
|
||||
$query->createNamedParameter(count($userIds, PDO::PARAM_INT))
|
||||
))
|
||||
->orderBy('demo_id', 'desc')
|
||||
->setMaxResults(50)
|
||||
->setFirstResult(((int) $page - 1) * 50);
|
||||
|
||||
$result = $query->execute();
|
||||
$demoIds = $result->fetchAll(\PDO::FETCH_COLUMN);
|
||||
$demoIds = $result->fetchAll(PDO::FETCH_COLUMN);
|
||||
|
||||
$demos = $this->db->demo()->where('id', $demoIds)
|
||||
->where($where)
|
||||
|
|
@ -54,7 +57,7 @@ class DemoListProvider extends BaseProvider {
|
|||
* @return Demo[]
|
||||
*/
|
||||
public function listDemos(int $page, array $where = [], string $order = 'DESC') {
|
||||
if (isset($where['players']) and \is_array($where['players']) and \count($where['players']) > 0) {
|
||||
if (isset($where['players']) and is_array($where['players']) and count($where['players']) > 0) {
|
||||
return $this->listProfile($page, $where);
|
||||
}
|
||||
|
||||
|
|
@ -77,7 +80,7 @@ class DemoListProvider extends BaseProvider {
|
|||
}
|
||||
if (isset($where['uploader'])) {
|
||||
$query->andWhere($query->expr()->in('uploader',
|
||||
$query->createNamedParameter($where['uploader'], \PDO::PARAM_INT)));
|
||||
$query->createNamedParameter($where['uploader'], PDO::PARAM_INT)));
|
||||
}
|
||||
if (isset($where['backend'])) {
|
||||
$query->andWhere($query->expr()->eq('backend',
|
||||
|
|
@ -87,7 +90,7 @@ class DemoListProvider extends BaseProvider {
|
|||
->setMaxResults(50)
|
||||
->setFirstResult($offset);
|
||||
|
||||
$demos = $query->execute()->fetchAll(\PDO::FETCH_ASSOC);
|
||||
$demos = $query->execute()->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
return $this->formatList($demos);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,9 +4,11 @@ declare(strict_types=1);
|
|||
|
||||
namespace Demostf\API\Providers;
|
||||
|
||||
use const DATE_ATOM;
|
||||
use Demostf\API\Data\DemoPlayer;
|
||||
use Demostf\API\Data\User;
|
||||
use Demostf\API\Demo\Demo;
|
||||
use PDO;
|
||||
|
||||
class DemoProvider extends BaseProvider {
|
||||
const VERSION = 4;
|
||||
|
|
@ -33,7 +35,7 @@ class DemoProvider extends BaseProvider {
|
|||
if ($fetchDetails) {
|
||||
$uploader = $demo->user()->via('uploader')->fetch();
|
||||
$playerQuery = $this->query($sql, [$formattedDemo->getId(), $formattedDemo->getId()]);
|
||||
$players = $playerQuery->fetchAll(\PDO::FETCH_ASSOC);
|
||||
$players = $playerQuery->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
$formattedDemo->setUploaderUser(User::fromRow([
|
||||
'id' => $uploader['id'],
|
||||
|
|
@ -74,18 +76,18 @@ class DemoProvider extends BaseProvider {
|
|||
'map' => $query->createNamedParameter($demo->getMap()),
|
||||
'red' => $query->createNamedParameter($demo->getRed()),
|
||||
'blu' => $query->createNamedParameter($demo->getBlue()),
|
||||
'uploader' => $query->createNamedParameter($demo->getUploader(), \PDO::PARAM_INT),
|
||||
'duration' => $query->createNamedParameter((int) $demo->getDuration(), \PDO::PARAM_INT),
|
||||
'created_at' => $query->createNamedParameter($demo->getTime()->format(\DATE_ATOM)),
|
||||
'uploader' => $query->createNamedParameter($demo->getUploader(), PDO::PARAM_INT),
|
||||
'duration' => $query->createNamedParameter((int) $demo->getDuration(), PDO::PARAM_INT),
|
||||
'created_at' => $query->createNamedParameter($demo->getTime()->format(DATE_ATOM)),
|
||||
'updated_at' => 'now()',
|
||||
'backend' => $query->createNamedParameter($backend),
|
||||
'path' => $query->createNamedParameter($path),
|
||||
'"scoreBlue"' => $query->createNamedParameter($demo->getBlueScore(), \PDO::PARAM_INT),
|
||||
'"scoreRed"' => $query->createNamedParameter($demo->getRedScore(), \PDO::PARAM_INT),
|
||||
'version' => $query->createNamedParameter(self::VERSION, \PDO::PARAM_INT),
|
||||
'"scoreBlue"' => $query->createNamedParameter($demo->getBlueScore(), PDO::PARAM_INT),
|
||||
'"scoreRed"' => $query->createNamedParameter($demo->getRedScore(), PDO::PARAM_INT),
|
||||
'version' => $query->createNamedParameter(self::VERSION, PDO::PARAM_INT),
|
||||
'server' => $query->createNamedParameter($demo->getServer()),
|
||||
'nick' => $query->createNamedParameter($demo->getNick()),
|
||||
'"playerCount"' => $query->createNamedParameter($demo->getPlayerCount(), \PDO::PARAM_INT),
|
||||
'"playerCount"' => $query->createNamedParameter($demo->getPlayerCount(), PDO::PARAM_INT),
|
||||
'hash' => $query->createNamedParameter($demo->getHash()),
|
||||
])
|
||||
->execute();
|
||||
|
|
@ -99,7 +101,7 @@ class DemoProvider extends BaseProvider {
|
|||
->set('backend', $query->createNamedParameter($backend))
|
||||
->set('url', $query->createNamedParameter($url))
|
||||
->set('path', $query->createNamedParameter($path))
|
||||
->where($query->expr()->eq('id', $query->createNamedParameter($id, \PDO::PARAM_INT)))
|
||||
->where($query->expr()->eq('id', $query->createNamedParameter($id, PDO::PARAM_INT)))
|
||||
->execute();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,12 +4,14 @@ declare(strict_types=1);
|
|||
|
||||
namespace Demostf\API\Providers;
|
||||
|
||||
use PDO;
|
||||
|
||||
class InfoProvider extends BaseProvider {
|
||||
public function listMaps() {
|
||||
$sql = 'SELECT map, count FROM map_list';
|
||||
$result = $this->query($sql);
|
||||
|
||||
return $result->fetchAll(\PDO::FETCH_COLUMN);
|
||||
return $result->fetchAll(PDO::FETCH_COLUMN);
|
||||
}
|
||||
|
||||
public function getStats() {
|
||||
|
|
|
|||
|
|
@ -7,7 +7,10 @@ namespace Demostf\API\Providers;
|
|||
use Demostf\API\Data\SteamUser;
|
||||
use Demostf\API\Data\User;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use function is_array;
|
||||
use PDO;
|
||||
use RandomLib\Generator;
|
||||
use SteamId;
|
||||
|
||||
class UserProvider extends BaseProvider {
|
||||
/**
|
||||
|
|
@ -20,7 +23,7 @@ class UserProvider extends BaseProvider {
|
|||
$this->generator = $generator;
|
||||
}
|
||||
|
||||
public function store(\SteamId $steamId): string {
|
||||
public function store(SteamId $steamId): string {
|
||||
$token = $this->generator->generateString(64, Generator::EASY_TO_READ);
|
||||
|
||||
$user = $this->get($steamId->getSteamId64());
|
||||
|
|
@ -65,7 +68,7 @@ class UserProvider extends BaseProvider {
|
|||
->setMaxResults(1);
|
||||
|
||||
$result = $query->execute()->fetch();
|
||||
if (\is_array($result)) {
|
||||
if (is_array($result)) {
|
||||
return $result;
|
||||
} else {
|
||||
return null;
|
||||
|
|
@ -86,7 +89,7 @@ class UserProvider extends BaseProvider {
|
|||
}
|
||||
|
||||
$query = $this->getQueryBuilder();
|
||||
$nameParameter = $query->createNamedParameter($search, \PDO::PARAM_STR, ':query');
|
||||
$nameParameter = $query->createNamedParameter($search, PDO::PARAM_STR, ':query');
|
||||
$query->select('user_id', 'name', 'count', 'steamid', "1 - (name <-> $nameParameter) AS sim")
|
||||
->from('name_list')
|
||||
->where($query->expr()->comparison('name', '%', $nameParameter))
|
||||
|
|
@ -94,7 +97,7 @@ class UserProvider extends BaseProvider {
|
|||
->orderBy('count', 'DESC')
|
||||
->setMaxResults(100);
|
||||
$result = $query->execute();
|
||||
$players = $result->fetchAll(\PDO::FETCH_ASSOC);
|
||||
$players = $result->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
usort($players, function ($b, $a) use ($query) {
|
||||
if ($a['steamid'] === $query && $a['steamid'] !== $query) {
|
||||
|
|
@ -138,7 +141,7 @@ class UserProvider extends BaseProvider {
|
|||
return $existing->getId();
|
||||
}
|
||||
|
||||
$this->store(new \SteamId($steamId));
|
||||
$this->store(new SteamId($steamId));
|
||||
|
||||
return $this->get($steamId)->getId();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,7 +30,6 @@ $userController = new Controllers\UserController($container->getRequest(), $cont
|
|||
$container->getUserProvider());
|
||||
$infoController = new Controllers\InfoController($container->getRequest(), $container->getResponse(),
|
||||
$container->getInfoProvider());
|
||||
$tempController = new Controllers\TempController($container->getApiRoot() . '/temp/');
|
||||
|
||||
Flight::route('/*', function () {
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
|
|
@ -67,6 +66,4 @@ Flight::route('/auth/handle/@token', [$authController, 'handle']);
|
|||
Flight::route('/auth/login/@token', [$authController, 'login']);
|
||||
Flight::route('/auth/logout/@token', [$authController, 'logout']);
|
||||
|
||||
Flight::route('/temp/@hash', [$tempController, 'serve']);
|
||||
|
||||
Flight::start();
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
declare(strict_types=1);
|
||||
|
||||
use Demostf\API\Container;
|
||||
use Doctrine\DBAL\DriverManager;
|
||||
|
||||
$autoloader = require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
|
|
@ -21,7 +22,7 @@ $connectionParams = [
|
|||
if ('pgsql' === $connectionParams['driver']) {
|
||||
$connectionParams['driver'] = 'pdo_pgsql';
|
||||
}
|
||||
$db = \Doctrine\DBAL\DriverManager::getConnection($connectionParams);
|
||||
$db = DriverManager::getConnection($connectionParams);
|
||||
$host = getenv('BASE_HOST') ?: '';
|
||||
$storeRoot = getenv('DEMO_ROOT') ?: '';
|
||||
$storeHost = getenv('DEMO_HOST') ?: '';
|
||||
|
|
|
|||
|
|
@ -9,13 +9,10 @@ use Flight;
|
|||
/** @var Container $container */
|
||||
$container = require __DIR__ . '/init.php';
|
||||
|
||||
$tempController = new Controllers\TempController($container->getApiRoot() . '/temp/');
|
||||
|
||||
$uploadController = new Controllers\UploadController(
|
||||
$container->getRequest(),
|
||||
$container->getResponse(),
|
||||
$container->getUploadProvider(),
|
||||
$tempController
|
||||
$container->getUploadProvider()
|
||||
);
|
||||
|
||||
Flight::route('/*', function () {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue