mirror of
https://codeberg.org/demostf/api.git
synced 2026-06-04 02:14:06 +02:00
stricter cs
This commit is contained in:
parent
309ae17036
commit
d9a843ecd6
54 changed files with 346 additions and 168 deletions
|
|
@ -1,10 +1,14 @@
|
|||
<?php namespace Demostf\API\Demo;
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Demostf\API\Demo;
|
||||
|
||||
class ChatMessage {
|
||||
/** @var string */
|
||||
private $user;
|
||||
|
||||
/** @var integer */
|
||||
/** @var int */
|
||||
private $time;
|
||||
|
||||
/** @var string */
|
||||
|
|
@ -14,7 +18,7 @@ class ChatMessage {
|
|||
* ChatMessage constructor.
|
||||
*
|
||||
* @param string $user
|
||||
* @param int $time
|
||||
* @param int $time
|
||||
* @param string $message
|
||||
*/
|
||||
public function __construct(string $user, int $time, string $message) {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,8 @@
|
|||
<?php namespace Demostf\API\Demo;
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Demostf\API\Demo;
|
||||
|
||||
use Demostf\API\Data\DemoPlayer;
|
||||
use Demostf\API\Data\User;
|
||||
|
|
@ -146,21 +150,21 @@ class Demo implements \JsonSerializable {
|
|||
}
|
||||
|
||||
public static function fromRow($row): Demo {
|
||||
return new Demo(
|
||||
(int)$row['id'],
|
||||
return new self(
|
||||
(int) $row['id'],
|
||||
$row['url'],
|
||||
$row['name'],
|
||||
$row['server'],
|
||||
(int)$row['duration'],
|
||||
(int) $row['duration'],
|
||||
$row['nick'],
|
||||
$row['map'],
|
||||
\DateTime::createFromFormat('U', '' . strtotime($row['created_at'])),
|
||||
$row['red'],
|
||||
$row['blu'],
|
||||
(int)$row['scoreRed'],
|
||||
(int)$row['scoreBlue'],
|
||||
(int)$row['playerCount'],
|
||||
(int)$row['uploader'],
|
||||
(int) $row['scoreRed'],
|
||||
(int) $row['scoreBlue'],
|
||||
(int) $row['playerCount'],
|
||||
(int) $row['uploader'],
|
||||
$row['hash'],
|
||||
$row['backend'],
|
||||
$row['path']
|
||||
|
|
@ -208,11 +212,12 @@ class Demo implements \JsonSerializable {
|
|||
'uploader' => $this->uploaderUser ? $this->getUploaderUser()->jsonSerialize() : $this->getUploader(),
|
||||
'hash' => $this->getHash(),
|
||||
'backend' => $this->getBackend(),
|
||||
'path' => $this->getPath()
|
||||
'path' => $this->getPath(),
|
||||
];
|
||||
if ($this->players) {
|
||||
$data['players'] = $this->getPlayers();
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
<?php declare(strict_types=1);
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Demostf\API\Demo;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,8 @@
|
|||
<?php namespace Demostf\API\Demo;
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Demostf\API\Demo;
|
||||
|
||||
use Demostf\API\Data\StoredDemo;
|
||||
|
||||
|
|
@ -20,6 +24,7 @@ class DemoStore {
|
|||
}
|
||||
rename($sourcePath, $target);
|
||||
chmod($target, 0755);
|
||||
|
||||
return new StoredDemo($this->getUrl($name), 'static', $target);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,11 @@
|
|||
<?php declare(strict_types=1);
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Demostf\API\Demo;
|
||||
|
||||
/**
|
||||
* HL2 demo metadata
|
||||
* HL2 demo metadata.
|
||||
*/
|
||||
class Header {
|
||||
/**
|
||||
|
|
@ -132,7 +134,7 @@ class Header {
|
|||
}
|
||||
|
||||
public static function fromArray(array $info) {
|
||||
return new Header(
|
||||
return new self(
|
||||
$info['type'],
|
||||
$info['version'],
|
||||
$info['protocol'],
|
||||
|
|
|
|||
|
|
@ -1,12 +1,16 @@
|
|||
<?php declare(strict_types=1);
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Demostf\API\Demo;
|
||||
|
||||
class HeaderParser {
|
||||
/**
|
||||
* @param string $head string containing the demo header binary data
|
||||
* @return Header
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*
|
||||
* @return Header
|
||||
*/
|
||||
public function parseString(string $head): Header {
|
||||
$info = @unpack(
|
||||
|
|
@ -16,33 +20,40 @@ class HeaderParser {
|
|||
if (!isset($info['type']) || $info['type'] !== 'HL2DEMO') {
|
||||
throw new \InvalidArgumentException('Not an HL2 demo');
|
||||
}
|
||||
|
||||
return Header::fromArray($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse demo info from a stream
|
||||
* Parse demo info from a stream.
|
||||
*
|
||||
* @param resource $stream
|
||||
* @return Header
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*
|
||||
* @return Header
|
||||
*/
|
||||
public function parseStream($stream): Header {
|
||||
$head = fread($stream, 2048);
|
||||
|
||||
return $this->parseString($head);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse demo info from a local file
|
||||
* Parse demo info from a local file.
|
||||
*
|
||||
* @param string $path
|
||||
* @return Header
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*
|
||||
* @return Header
|
||||
*/
|
||||
public function parseHeader(string $path): Header {
|
||||
if (!is_readable($path)) {
|
||||
throw new \InvalidArgumentException('Unable to open demo: ' . $path);
|
||||
}
|
||||
$fh = fopen($path, 'rb');
|
||||
|
||||
return $this->parseStream($fh);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,15 @@
|
|||
<?php declare(strict_types=1);
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Demostf\API\Demo;
|
||||
|
||||
use Demostf\API\Data\ParsedDemo;
|
||||
use Demostf\API\Data\ParsedKill;
|
||||
use Demostf\API\Data\ParsedPlayer;
|
||||
use Demostf\API\Data\Player;
|
||||
|
||||
/**
|
||||
* Higher level parser
|
||||
* Higher level parser.
|
||||
*
|
||||
* Processes the raw demo.js output to something more suitable for our purpose
|
||||
*/
|
||||
|
|
@ -22,7 +23,7 @@ class Parser {
|
|||
6 => 'heavyweapons',
|
||||
7 => 'pyro',
|
||||
8 => 'spy',
|
||||
9 => 'engineer'
|
||||
9 => 'engineer',
|
||||
];
|
||||
|
||||
/** @var RawParser */
|
||||
|
|
@ -37,6 +38,7 @@ class Parser {
|
|||
if (!is_array($data)) {
|
||||
throw new \InvalidArgumentException('Error parsing demo');
|
||||
}
|
||||
|
||||
return $this->handleData($data);
|
||||
}
|
||||
|
||||
|
|
@ -50,15 +52,15 @@ class Parser {
|
|||
$players = [];
|
||||
foreach ($data['rounds'] as $round) {
|
||||
if ($round['winner'] === 'red') {
|
||||
$red++;
|
||||
++$red;
|
||||
} else {
|
||||
$blue++;
|
||||
++$blue;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($data['chat'] as $message) {
|
||||
if (isset($message['from'])) {
|
||||
$chat[] = new ChatMessage($message['from'], (int)floor(($message['tick'] - $data['startTick']) * $intervalPerTick), $message['text']);
|
||||
$chat[] = new ChatMessage($message['from'], (int) floor(($message['tick'] - $data['startTick']) * $intervalPerTick), $message['text']);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -77,7 +79,7 @@ class Parser {
|
|||
$player['userId'],
|
||||
$this->convertSteamIdToCommunityId($player['steamId']),
|
||||
$player['team'],
|
||||
$this->getClassName((int)$class)
|
||||
$this->getClassName((int) $class)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -100,16 +102,18 @@ class Parser {
|
|||
}
|
||||
|
||||
/**
|
||||
* Credit to https://github.com/koraktor/steam-condenser-php
|
||||
* Credit to https://github.com/koraktor/steam-condenser-php.
|
||||
*
|
||||
* Converts a SteamID as reported by game servers to a 64bit numeric
|
||||
* SteamID as used by the Steam Community
|
||||
*
|
||||
* @param string $steamId The SteamID string as used on servers, like
|
||||
* <var>STEAM_0:0:12345</var>
|
||||
* @return string The converted 64bit numeric SteamID
|
||||
* <var>STEAM_0:0:12345</var>
|
||||
*
|
||||
* @throws \InvalidArgumentException if the SteamID doesn't have the correct
|
||||
* format
|
||||
* format
|
||||
*
|
||||
* @return string The converted 64bit numeric SteamID
|
||||
*/
|
||||
public function convertSteamIdToCommunityId($steamId) {
|
||||
if ($steamId === 'STEAM_ID_LAN' || $steamId === 'BOT') {
|
||||
|
|
@ -118,10 +122,12 @@ class Parser {
|
|||
if (preg_match('/^STEAM_[0-1]:[0-1]:[0-9]+$/', $steamId)) {
|
||||
$steamParts = explode(':', substr($steamId, 8));
|
||||
$steamId = $steamParts[0] + $steamParts[1] * 2 + 1197960265728;
|
||||
|
||||
return '7656' . $steamId;
|
||||
} elseif (preg_match('/^\[U:[0-1]:[0-9]+\]$/', $steamId)) {
|
||||
$steamParts = explode(':', substr($steamId, 3, -1));
|
||||
$steamId = $steamParts[0] + $steamParts[1] + 1197960265727;
|
||||
|
||||
return '7656' . $steamId;
|
||||
} else {
|
||||
throw new \InvalidArgumentException("SteamID \"$steamId\" doesn't have the correct format.");
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
<?php declare(strict_types=1);
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Demostf\API\Demo;
|
||||
|
||||
|
|
@ -6,7 +8,7 @@ use GuzzleHttp\Client;
|
|||
use GuzzleHttp\Exception\GuzzleException;
|
||||
|
||||
/**
|
||||
* Wrapper around demo.js parser
|
||||
* Wrapper around demo.js parser.
|
||||
*
|
||||
* Doesn't do any post-processing on the result
|
||||
*/
|
||||
|
|
@ -22,10 +24,10 @@ class RawParser {
|
|||
try {
|
||||
$client = new Client();
|
||||
$response = $client->post($this->parserUrl, [
|
||||
'body' => fopen($path, 'r')
|
||||
'body' => fopen($path, 'r'),
|
||||
]);
|
||||
$result = json_decode($response->getBody()->getContents(), true);
|
||||
if (is_null($result)) {
|
||||
if (null === $result) {
|
||||
throw new \Exception('Failed to parse demo, unexpected result from parser');
|
||||
} else {
|
||||
return $result;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue