Add ProjectZomboidUserLog (user.txt)
Two row variants: low-level Connection events and player join/disconnect events. The LINE regex accepts both shapes; analysers route via CONNECTION (action/index/guid/id) and PLAYER_EVENT (steamid/player/event) named-group regexes. Detectors: filename match plus content signatures on either variant.
This commit is contained in:
47
src/Log/ProjectZomboid/ProjectZomboidUserLog.php
Normal file
47
src/Log/ProjectZomboid/ProjectZomboidUserLog.php
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace IndifferentKetchup\Codex\Log\ProjectZomboid;
|
||||||
|
|
||||||
|
use IndifferentKetchup\Codex\Analyser\AnalyserInterface;
|
||||||
|
use IndifferentKetchup\Codex\Analyser\PatternAnalyser;
|
||||||
|
use IndifferentKetchup\Codex\Detective\FilenameDetector;
|
||||||
|
use IndifferentKetchup\Codex\Detective\WeightedSinglePatternDetector;
|
||||||
|
use IndifferentKetchup\Codex\Parser\ParserInterface;
|
||||||
|
use IndifferentKetchup\Codex\Parser\PatternParser;
|
||||||
|
use IndifferentKetchup\Codex\Pattern\ProjectZomboid\UserPattern;
|
||||||
|
|
||||||
|
class ProjectZomboidUserLog extends ProjectZomboidEventLog
|
||||||
|
{
|
||||||
|
public static function getDefaultParser(): ParserInterface
|
||||||
|
{
|
||||||
|
return static::makePatternParser(
|
||||||
|
UserPattern::LINE,
|
||||||
|
[PatternParser::TIME]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getDefaultAnalyser(): AnalyserInterface
|
||||||
|
{
|
||||||
|
return new PatternAnalyser();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getDetectors(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
(new FilenameDetector())
|
||||||
|
->setPattern('/_user\.txt$/')
|
||||||
|
->setWeight(0.95),
|
||||||
|
(new WeightedSinglePatternDetector())
|
||||||
|
->setPattern('/^\[[^\]]+\] Connection (?:add|disconnect) index=\d+ guid=\d+/m')
|
||||||
|
->setWeight(0.90),
|
||||||
|
(new WeightedSinglePatternDetector())
|
||||||
|
->setPattern('/^\[[^\]]+\] \d{17} "[^"]+" (?:attempting to join|allowed to join)/m')
|
||||||
|
->setWeight(0.85),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getTitle(): string
|
||||||
|
{
|
||||||
|
return "Project Zomboid User Log";
|
||||||
|
}
|
||||||
|
}
|
||||||
22
src/Pattern/ProjectZomboid/UserPattern.php
Normal file
22
src/Pattern/ProjectZomboid/UserPattern.php
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace IndifferentKetchup\Codex\Pattern\ProjectZomboid;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Regex constants for the Project Zomboid user.txt format.
|
||||||
|
*
|
||||||
|
* Two row variants share the file: low-level connection events
|
||||||
|
* [time] Connection {add|disconnect} index=N guid=N id={N|null}.
|
||||||
|
* and player join/leave events
|
||||||
|
* [time] steamid "player" {attempting|allowed} to join.
|
||||||
|
*
|
||||||
|
* Both variants are accepted by LINE, which captures only the timestamp.
|
||||||
|
*/
|
||||||
|
class UserPattern
|
||||||
|
{
|
||||||
|
public const string LINE = '/^\[(\d{2}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3})\] (?:Connection (?:add|disconnect) index=\d+ guid=\d+ id=(?:\d+|null)|\d{17} "[^"]+" .+?)\.?$/';
|
||||||
|
|
||||||
|
public const string CONNECTION = '/^\[\d{2}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}\] Connection (?<action>add|disconnect) index=(?<index>\d+) guid=(?<guid>\d+) id=(?<id>\d+|null)\.?$/';
|
||||||
|
|
||||||
|
public const string PLAYER_EVENT = '/^\[\d{2}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}\] (?<steamid>\d{17}) "(?<player>[^"]+)" (?<event>.+?)\.?$/';
|
||||||
|
}
|
||||||
9
test/src/Games/ProjectZomboid/fixtures/user-minimal.txt
Normal file
9
test/src/Games/ProjectZomboid/fixtures/user-minimal.txt
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
[29-04-26 18:35:41.512] Connection add index=0 guid=144118788000000001 id=null.
|
||||||
|
[29-04-26 18:35:42.802] 76561198000000001 "Player1" attempting to join.
|
||||||
|
[29-04-26 18:35:42.804] 76561198000000001 "Player1" allowed to join.
|
||||||
|
[29-04-26 18:37:14.645] 76561198000000001 "Player1" attempting to join used queue.
|
||||||
|
[29-04-26 18:39:23.923] Connection disconnect index=0 guid=144118788000000001 id=76561198000000001.
|
||||||
|
[29-04-26 18:40:00.100] Connection add index=1 guid=144118788000000002 id=null.
|
||||||
|
[29-04-26 18:40:01.225] 76561198000000002 "Player2" attempting to join.
|
||||||
|
[29-04-26 18:40:01.500] 76561198000000002 "Player2" allowed to join.
|
||||||
|
[29-04-26 18:50:30.010] Connection disconnect index=1 guid=144118788000000002 id=76561198000000002.
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace IndifferentKetchup\Codex\Test\Tests\Games\ProjectZomboid\Log;
|
||||||
|
|
||||||
|
use IndifferentKetchup\Codex\Detective\Detective;
|
||||||
|
use IndifferentKetchup\Codex\Log\File\PathLogFile;
|
||||||
|
use IndifferentKetchup\Codex\Log\ProjectZomboid\ProjectZomboidUserLog;
|
||||||
|
use IndifferentKetchup\Codex\Pattern\ProjectZomboid\UserPattern;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class ProjectZomboidUserLogTest extends TestCase
|
||||||
|
{
|
||||||
|
private function fixturePath(): string
|
||||||
|
{
|
||||||
|
return __DIR__ . '/../../../../src/Games/ProjectZomboid/fixtures/user-minimal.txt';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testParsesEachLineAsAnEntry(): void
|
||||||
|
{
|
||||||
|
$log = (new ProjectZomboidUserLog())->setLogFile(new PathLogFile($this->fixturePath()));
|
||||||
|
$log->parse();
|
||||||
|
|
||||||
|
$this->assertCount(9, $log->getEntries());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testConnectionRegexExtractsAddVariant(): void
|
||||||
|
{
|
||||||
|
$line = '[29-04-26 18:35:41.512] Connection add index=0 guid=144118788000000001 id=null.';
|
||||||
|
$this->assertSame(1, preg_match(UserPattern::CONNECTION, $line, $m));
|
||||||
|
$this->assertSame('add', $m['action']);
|
||||||
|
$this->assertSame('null', $m['id']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testConnectionRegexExtractsDisconnectVariant(): void
|
||||||
|
{
|
||||||
|
$line = '[29-04-26 18:39:23.923] Connection disconnect index=0 guid=144118788000000001 id=76561198000000001.';
|
||||||
|
$this->assertSame(1, preg_match(UserPattern::CONNECTION, $line, $m));
|
||||||
|
$this->assertSame('disconnect', $m['action']);
|
||||||
|
$this->assertSame('76561198000000001', $m['id']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testPlayerEventRegexExtracts(): void
|
||||||
|
{
|
||||||
|
$line = '[29-04-26 18:35:42.802] 76561198000000001 "Player1" attempting to join.';
|
||||||
|
$this->assertSame(1, preg_match(UserPattern::PLAYER_EVENT, $line, $m));
|
||||||
|
$this->assertSame('Player1', $m['player']);
|
||||||
|
$this->assertSame('attempting to join', $m['event']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDetectiveDispatchesByContent(): void
|
||||||
|
{
|
||||||
|
$detective = (new Detective())
|
||||||
|
->setLogFile(new PathLogFile($this->fixturePath()))
|
||||||
|
->addPossibleLogClass(ProjectZomboidUserLog::class);
|
||||||
|
|
||||||
|
$this->assertInstanceOf(ProjectZomboidUserLog::class, $detective->detect());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user