1
0
Fork 0
mirror of https://codeberg.org/demostf/api.git synced 2026-06-04 02:14:06 +02:00

upload and tests

This commit is contained in:
Robin Appelman 2017-04-10 00:58:50 +02:00
commit 03d3acebf5
21 changed files with 1026 additions and 152 deletions

View file

@ -12,7 +12,7 @@ class Demo implements \JsonSerializable {
private $name;
/** @var string */
private $server;
/** @var int */
/** @var float */
private $duration;
/** @var string */
private $nick;
@ -44,7 +44,7 @@ class Demo implements \JsonSerializable {
string $url,
string $name,
string $server,
int $duration,
float $duration,
string $nick,
string $map,
\DateTime $time,
@ -89,7 +89,7 @@ class Demo implements \JsonSerializable {
return $this->server;
}
public function getDuration(): int {
public function getDuration(): float {
return $this->duration;
}
@ -157,6 +157,9 @@ class Demo implements \JsonSerializable {
);
}
/**
* @return DemoPlayer[]
*/
public function getPlayers(): array {
return $this->players;
}

90
src/Demo/DemoSaver.php Normal file
View file

@ -0,0 +1,90 @@
<?php declare(strict_types=1);
namespace Demostf\API\Demo;
use Demostf\API\Data\Kill;
use Demostf\API\Data\ParsedDemo;
use Demostf\API\Data\Player;
use Demostf\API\Data\StoredDemo;
use Demostf\API\Data\Upload;
use Demostf\API\Providers\ChatProvider;
use Demostf\API\Providers\DemoProvider;
use Demostf\API\Providers\KillProvider;
use Demostf\API\Providers\PlayerProvider;
use Demostf\API\Providers\UserProvider;
class DemoSaver {
/** @var KillProvider */
private $killProvider;
/** @var PlayerProvider */
private $playerProvider;
/** @var ChatProvider */
private $chatProvider;
/** @var UserProvider */
private $userProvider;
/** @var DemoProvider */
private $demoProvider;
public function __construct(KillProvider $killProvider, PlayerProvider $playerProvider, ChatProvider $chatProvider, UserProvider $userProvider, DemoProvider $demoProvider) {
$this->killProvider = $killProvider;
$this->playerProvider = $playerProvider;
$this->chatProvider = $chatProvider;
$this->userProvider = $userProvider;
$this->demoProvider = $demoProvider;
}
public function saveDemo(ParsedDemo $demo, Header $header, StoredDemo $storedDemo, Upload $upload): int {
/** @var int[] $userMap [$demoUserId => $dbUserId] */
$userMap = [0 => 0];
$demoId = $this->demoProvider->storeDemo(new Demo(
0,
$storedDemo->getUrl(),
$upload->getName(),
$header->getServer(),
$header->getDuration(),
$header->getNick(),
$header->getMap(),
new \DateTime(),
$upload->getRed(),
$upload->getBlue(),
$demo->getRedScore(),
$demo->getBlueScore(),
count($demo->getPlayers()),
$upload->getUploaderId(),
$upload->getHash()
), $storedDemo->getBackend(), $storedDemo->getPath());
foreach ($demo->getPlayers() as $player) {
$userId = $this->userProvider->getUserId($player->getSteamId());
$userMap[$player->getDemoUserId()] = $userId;
$this->playerProvider->store(new Player(
0,
$demoId,
$player->getDemoUserId(),
$userId,
$player->getName(),
$player->getTeam(),
$player->getClass()
));
}
foreach ($demo->getKills() as $kill) {
$this->killProvider->store(new Kill(
0,
$demoId,
$userMap[$kill->getAttackerDemoId()],
$userMap[$kill->getAssisterDemoId()],
$userMap[$kill->getVictimDemoId()],
$kill->getWeapon()
));
}
foreach ($demo->getChat() as $chat) {
$this->chatProvider->storeChatMessage($demoId, $chat);
}
return $demoId;
}
}

View file

@ -1,5 +1,7 @@
<?php namespace Demostf\API\Demo;
use Demostf\API\Data\StoredDemo;
class DemoStore {
/** @var string */
private $root;
@ -11,13 +13,13 @@ class DemoStore {
$this->webroot = $webroot;
}
public function store(string $sourcePath, string $name): string {
public function store(string $sourcePath, string $name): StoredDemo {
$target = $this->generatePath($name);
if (!is_dir(dirname($target))) {
mkdir(dirname($target), 0777, true);
}
rename($sourcePath, $target);
return $this->getUrl($name);
return new StoredDemo($this->getUrl($name), 'static', $target);
}
private function generatePath(string $name): string {

View file

@ -61,21 +61,30 @@ class Header {
*/
protected $sigon;
/**
* @param array $info
*/
public function __construct($info) {
$this->type = $info['type'];
$this->version = $info['version'];
$this->protocol = $info['protocol'];
$this->server = $info['server'];
$this->nick = $info['nick'];
$this->map = $info['map'];
$this->game = $info['game'];
$this->duration = $info['duration'];
$this->ticks = $info['ticks'];
$this->frames = $info['frames'];
$this->sigon = $info['sigon'];
public function __construct(
string $type,
int $version,
int $protocol,
string $server,
string $nick,
string $map,
string $game,
float $duration,
int $ticks,
int $frames,
int $sigon
) {
$this->type = $type;
$this->version = $version;
$this->protocol = $protocol;
$this->server = $server;
$this->nick = $nick;
$this->map = $map;
$this->game = $game;
$this->duration = $duration;
$this->ticks = $ticks;
$this->frames = $frames;
$this->sigon = $sigon;
}
public function getDuration(): float {
@ -122,4 +131,19 @@ class Header {
return $this->version;
}
public static function fromArray(array $info) {
return new Header(
$info['type'],
$info['version'],
$info['protocol'],
$info['server'],
$info['nick'],
$info['map'],
$info['game'],
$info['duration'],
$info['ticks'],
$info['frames'],
$info['sigon']
);
}
}

View file

@ -14,7 +14,7 @@ class HeaderParser {
if (!isset($info['type']) || $info['type'] !== 'HL2DEMO') {
throw new \InvalidArgumentException('Not an HL2 demo');
}
return new Header($info);
return Header::fromArray($info);
}
/**

View file

@ -2,6 +2,11 @@
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
*
@ -27,7 +32,7 @@ class Parser {
$this->rawParser = $rawParser;
}
public function analyse(string $path): array {
public function analyse(string $path): ParsedDemo {
$data = $this->rawParser->parse($path);
if (!is_array($data)) {
throw new \InvalidArgumentException('Error parsing demo');
@ -35,11 +40,13 @@ class Parser {
return $this->handleData($data);
}
private function handleData(array $data) {
private function handleData(array $data): ParsedDemo {
$intervalPerTick = $data['intervalPerTick'];
$red = 0;
$blue = 0;
/** @var ChatMessage[] $chat */
$chat = [];
/** @var ParsedPlayer[] $players */
$players = [];
foreach ($data['rounds'] as $round) {
if ($round['winner'] === 'red') {
@ -51,11 +58,7 @@ class Parser {
foreach ($data['chat'] as $message) {
if (isset($message['from'])) {
$chat[] = [
'time' => floor(($message['tick'] - $data['startTick']) * $intervalPerTick),
'from' => $message['from'],
'text' => $message['text']
];
$chat[] = new ChatMessage($message['from'], (int)floor(($message['tick'] - $data['startTick']) * $intervalPerTick), $message['text']);
}
}
@ -69,28 +72,59 @@ class Parser {
}
}
if ($class && $player['steamId']) {//skip spectators
$players[] = [
'name' => $player['name'],
'demo_user_id' => $player['userId'],
'steam_id' => $player['steamId'],
'team' => $player['team'],
'class' => $this->getClassName($class)
];
$players[] = new ParsedPlayer(
$player['name'],
$player['userId'],
$this->convertSteamIdToCommunityId($player['steamId']),
$player['team'],
$this->getClassName($class)
);
}
}
return [
'score' => [
'red' => $red,
'blue' => $blue
],
'chat' => $chat,
'players' => $players,
'kills' => $data['deaths']
];
$kills = array_map(function (array $death) {
return new ParsedKill($death['killer'] ?? 0, $death['assister'] ?? 0, $death['victim'] ?? 0, $death['weapon']);
}, $data['deaths']);
return new ParsedDemo(
$red,
$blue,
$chat,
$players,
$kills
);
}
private function getClassName(int $classId): string {
return self::CLASSES[$classId] ?? 'Unknown';
}
/**
* 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
* @throws \InvalidArgumentException if the SteamID doesn't have the correct
* format
*/
public function convertSteamIdToCommunityId($steamId) {
if ($steamId === 'STEAM_ID_LAN' || $steamId === 'BOT') {
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));
$steamId = $steamParts[0] + $steamParts[1] * 2 + 1197960265728;
return '7656' . $steamId;
} else if (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.");
}
}
}