1
0
Fork 0
mirror of https://codeberg.org/demostf/api.git synced 2026-06-04 02:14:06 +02:00
This commit is contained in:
Robin Appelman 2019-04-12 14:00:20 +02:00
commit 73469d2aa1
29 changed files with 140 additions and 149 deletions

View file

@ -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;

View file

@ -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'],

View file

@ -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(),

View file

@ -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);

View file

@ -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');

View file

@ -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.");
}
}
}

View file

@ -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());
}
}
}