mirror of
https://codeberg.org/demostf/api.git
synced 2026-06-03 18:04:08 +02:00
split parser and add tests
This commit is contained in:
parent
1c37d3513e
commit
ae39548ddb
11 changed files with 1801 additions and 102 deletions
|
|
@ -27,6 +27,7 @@ A seperate PostgreSQL database is required to run the image, the database detail
|
|||
- DB_USERNAME=$database_user
|
||||
- DB_PASSWORD=$database_password
|
||||
- BASE_HOST=$host
|
||||
- PARSER_URL=$parser_host // the full url for the demo parser's upload endpoint
|
||||
|
||||
## Installing
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
<?php namespace Demostf\API\Demo;
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Demostf\API\Demo;
|
||||
|
||||
/**
|
||||
* HL2 demo metadata
|
||||
|
|
@ -76,80 +78,47 @@ class Header {
|
|||
$this->sigon = $info['sigon'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return float
|
||||
*/
|
||||
public function getDuration() {
|
||||
public function getDuration(): float {
|
||||
return $this->duration;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getFrames() {
|
||||
public function getFrames(): int {
|
||||
return $this->frames;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getGame() {
|
||||
public function getGame(): string {
|
||||
return $this->game;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getMap() {
|
||||
public function getMap(): string {
|
||||
return $this->map;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getNick() {
|
||||
public function getNick(): string {
|
||||
return $this->nick;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getProtocol() {
|
||||
public function getProtocol(): int {
|
||||
return $this->protocol;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getServer() {
|
||||
public function getServer(): string {
|
||||
return $this->server;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getSigon() {
|
||||
public function getSigon(): int {
|
||||
return $this->sigon;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getTicks() {
|
||||
public function getTicks(): int {
|
||||
return $this->ticks;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getType() {
|
||||
public function getType(): string {
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getVersion() {
|
||||
public function getVersion(): int {
|
||||
return $this->version;
|
||||
}
|
||||
|
||||
|
|
|
|||
46
src/Demo/HeaderParser.php
Normal file
46
src/Demo/HeaderParser.php
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
<?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
|
||||
*/
|
||||
public function parseString(string $head): Header {
|
||||
$info = @unpack('A8type/Iversion/Iprotocol/A260server/A260nick/A260map/A260game/fduration/Vticks/Vframes/Vsigon',
|
||||
$head);
|
||||
if (!isset($info['type']) || $info['type'] !== 'HL2DEMO') {
|
||||
throw new \InvalidArgumentException('Not an HL2 demo');
|
||||
}
|
||||
return new Header($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse demo info from a stream
|
||||
*
|
||||
* @param resource $stream
|
||||
* @return Header
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function parseStream($stream): Header {
|
||||
$head = fread($stream, 2048);
|
||||
return $this->parseString($head);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse demo info from a local file
|
||||
*
|
||||
* @param string $path
|
||||
* @return Header
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
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,67 +1,31 @@
|
|||
<?php namespace Demostf\API\Demo;
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Demostf\API\Demo;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
/**
|
||||
* Higher level parser
|
||||
*
|
||||
* Processes the raw demo.js output to something more suitable for our purpose
|
||||
*/
|
||||
class Parser {
|
||||
const ANALYSER_BASEURL = 'http://demoserver.azurewebsites.net';
|
||||
/** @var RawParser */
|
||||
private $rawParser;
|
||||
|
||||
/**
|
||||
* @param string $head string containing the demo header binary data
|
||||
* @return Header
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function parseString($head) {
|
||||
set_error_handler(array($this, 'errorHandler'));
|
||||
$info = unpack("A8type/Iversion/Iprotocol/A260server/A260nick/A260map/A260game/fduration/Iticks/Iframes/Isigon",
|
||||
$head);
|
||||
restore_error_handler();
|
||||
if ($info['type'] !== 'HL2DEMO') {
|
||||
throw new \Exception('Not an HL2 demo');
|
||||
public function __construct(RawParser $rawParser) {
|
||||
$this->rawParser = $rawParser;
|
||||
}
|
||||
|
||||
public function analyse(string $path): array {
|
||||
$data = $this->rawParser->parse($path);
|
||||
if (!is_array($data)) {
|
||||
throw new \InvalidArgumentException('Error parsing demo');
|
||||
}
|
||||
return new Header($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse demo info from a stream
|
||||
*
|
||||
* @param resource $stream
|
||||
* @return Header
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function parseStream($stream) {
|
||||
$head = fread($stream, 2048);
|
||||
return $this->parseString($head);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse demo info from a local file
|
||||
*
|
||||
* @param string $path
|
||||
* @return Header
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function parseHeader($path) {
|
||||
if (!is_readable($path)) {
|
||||
throw new \Exception('Unable to open demo: ' . $path);
|
||||
}
|
||||
$fh = fopen($path, 'rb');
|
||||
return $this->parseStream($fh);
|
||||
}
|
||||
|
||||
public function analyse(StoredDemo $storedDemo) {
|
||||
$endPoint = self::ANALYSER_BASEURL . '/url';
|
||||
$client = new Client();
|
||||
$response = $client->post($endPoint, [
|
||||
'body' => $storedDemo->getUrl()
|
||||
]);
|
||||
$data = $response->getBody();
|
||||
return $this->handleData($data);
|
||||
}
|
||||
|
||||
private function handleData($data) {
|
||||
if (!is_array($data)) {
|
||||
throw new \Exception('Error parsing demo');
|
||||
}
|
||||
private function handleData(array $data) {
|
||||
$intervalPerTick = $data['intervalPerTick'];
|
||||
$red = 0;
|
||||
$blue = 0;
|
||||
|
|
@ -94,7 +58,7 @@ class Parser {
|
|||
$class = $classId;
|
||||
}
|
||||
}
|
||||
if ($class and $player['steamId']) {//skip spectators
|
||||
if ($class && $player['steamId']) {//skip spectators
|
||||
$players[] = [
|
||||
'name' => $player['name'],
|
||||
'demo_user_id' => $player['userId'],
|
||||
|
|
@ -116,7 +80,7 @@ class Parser {
|
|||
];
|
||||
}
|
||||
|
||||
private function getClassName($classId) {
|
||||
private function getClassName(int $classId): string {
|
||||
$classes = [
|
||||
1 => 'scout',
|
||||
2 => 'sniper',
|
||||
|
|
@ -128,6 +92,6 @@ class Parser {
|
|||
8 => 'spy',
|
||||
9 => 'engineer'
|
||||
];
|
||||
return isset($classes[$classId]) ? $classes[$classId] : 'Unknown';
|
||||
return $classes[$classId] ?? 'Unknown';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
27
src/Demo/RawParser.php
Normal file
27
src/Demo/RawParser.php
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Demostf\API\Demo;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
/**
|
||||
* Wrapper around demo.js parser
|
||||
*
|
||||
* Doesn't do any post-processing on the result
|
||||
*/
|
||||
class RawParser {
|
||||
/** @var string */
|
||||
private $parserUrl;
|
||||
|
||||
public function __construct(string $parserUrl) {
|
||||
$this->parserUrl = $parserUrl;
|
||||
}
|
||||
|
||||
public function parse(string $path): ?array {
|
||||
$client = new Client();
|
||||
$response = $client->post($this->parserUrl, [
|
||||
'body' => fopen($path, 'r')
|
||||
]);
|
||||
return json_decode($response->getBody(), true);
|
||||
}
|
||||
}
|
||||
|
|
@ -3,7 +3,6 @@
|
|||
namespace Demostf\API\Providers;
|
||||
|
||||
use Demostf\API\Data\User;
|
||||
use Demostf\API\Exception\NotFoundException;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use RandomLib\Generator;
|
||||
|
||||
|
|
|
|||
58
tests/Demo/HeaderParserTest.php
Normal file
58
tests/Demo/HeaderParserTest.php
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Demostf\API\Test\Providers;
|
||||
|
||||
use Demostf\API\Demo\Header;
|
||||
use Demostf\API\Demo\HeaderParser;
|
||||
use Demostf\API\Test\TestCase;
|
||||
|
||||
class HeaderParserTest extends TestCase {
|
||||
public function testParseFile() {
|
||||
$parser = new HeaderParser();
|
||||
|
||||
$expected = new Header([
|
||||
'type' => 'HL2DEMO',
|
||||
'version' => 3,
|
||||
'protocol' => 24,
|
||||
'server' => 'UGC Highlander Match',
|
||||
'nick' => 'SourceTV Demo',
|
||||
'map' => 'koth_product_rc8',
|
||||
'game' => 'tf',
|
||||
'duration' => 778.4849853515625,
|
||||
'ticks' => 51899,
|
||||
'frames' => 25703,
|
||||
'sigon' => 818263
|
||||
]);
|
||||
$parsed = $parser->parseHeader(__DIR__ . '/../data/product.dem');
|
||||
|
||||
$this->assertEquals($expected->getServer(), $parsed->getServer());
|
||||
$this->assertEquals($expected->getDuration(), $parsed->getDuration());
|
||||
$this->assertEquals($expected->getTicks(), $parsed->getTicks());
|
||||
$this->assertEquals($expected->getFrames(), $parsed->getFrames());
|
||||
$this->assertEquals($expected->getGame(), $parsed->getGame());
|
||||
$this->assertEquals($expected->getMap(), $parsed->getMap());
|
||||
$this->assertEquals($expected->getNick(), $parsed->getNick());
|
||||
$this->assertEquals($expected->getProtocol(), $parsed->getProtocol());
|
||||
$this->assertEquals($expected->getSigon(), $parsed->getSigon());
|
||||
$this->assertEquals($expected->getType(), $parsed->getType());
|
||||
$this->assertEquals($expected->getVersion(), $parsed->getVersion());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage Not an HL2 demo
|
||||
*/
|
||||
public function testNonDemoShort() {
|
||||
$parser = new HeaderParser();
|
||||
$parser->parseString("short");
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage Not an HL2 demo
|
||||
*/
|
||||
public function testNonDemoLong() {
|
||||
$parser = new HeaderParser();
|
||||
$parser->parseHeader(__FILE__);
|
||||
}
|
||||
}
|
||||
37
tests/Demo/ParserTest.php
Normal file
37
tests/Demo/ParserTest.php
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Demostf\API\Test\Providers;
|
||||
|
||||
use Demostf\API\Demo\Parser;
|
||||
use Demostf\API\Demo\RawParser;
|
||||
use Demostf\API\Test\TestCase;
|
||||
|
||||
class ParserTest extends TestCase {
|
||||
/** @var RawParser */
|
||||
private $rawParser;
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->rawParser = $this->getMockBuilder(RawParser::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$this->rawParser->expects($this->any())
|
||||
->method('parse')
|
||||
->will($this->returnCallback(function ($path) {
|
||||
$jsonPath = str_replace('.dem', '-raw.json', $path);
|
||||
return json_decode(file_get_contents($jsonPath), true);
|
||||
}));
|
||||
}
|
||||
|
||||
public function testAnalyse() {
|
||||
$parser = new Parser($this->rawParser);
|
||||
|
||||
$result = $parser->analyse(__DIR__ . '/../data/product.dem');
|
||||
|
||||
$expected = json_decode(file_get_contents(__DIR__ . '/../data/product-analyse.json'), true);
|
||||
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
}
|
||||
1597
tests/data/product-analyse.json
Normal file
1597
tests/data/product-analyse.json
Normal file
File diff suppressed because it is too large
Load diff
1
tests/data/product-raw.json
Normal file
1
tests/data/product-raw.json
Normal file
File diff suppressed because one or more lines are too long
BIN
tests/data/product.dem
Normal file
BIN
tests/data/product.dem
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue