Add ProjectZomboidAdminLog (admin.txt)
Free-form English message body with verb-dispatched analyser regexes (ADDED_ITEM, ADDED_XP, GRANTED_ACCESS, CHANGED_OPTION, RELOADED_OPTIONS, TELEPORTED). Parser captures only the timestamp, since the admin name itself can include parentheses or whitespace. Detectors: filename match plus content signatures on 'added item Base.X in Y's inventory' and 'granted ROLE access level on'.
This commit is contained in:
47
src/Log/ProjectZomboid/ProjectZomboidAdminLog.php
Normal file
47
src/Log/ProjectZomboid/ProjectZomboidAdminLog.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\AdminPattern;
|
||||
|
||||
class ProjectZomboidAdminLog extends ProjectZomboidEventLog
|
||||
{
|
||||
public static function getDefaultParser(): ParserInterface
|
||||
{
|
||||
return static::makePatternParser(
|
||||
AdminPattern::LINE,
|
||||
[PatternParser::TIME]
|
||||
);
|
||||
}
|
||||
|
||||
public static function getDefaultAnalyser(): AnalyserInterface
|
||||
{
|
||||
return new PatternAnalyser();
|
||||
}
|
||||
|
||||
public static function getDetectors(): array
|
||||
{
|
||||
return [
|
||||
(new FilenameDetector())
|
||||
->setPattern('/_admin\.txt$/')
|
||||
->setWeight(0.95),
|
||||
(new WeightedSinglePatternDetector())
|
||||
->setPattern('/^\[[^\]]+\] .+? added item Base\.\S+ in .+?\'s inventory/m')
|
||||
->setWeight(0.90),
|
||||
(new WeightedSinglePatternDetector())
|
||||
->setPattern('/^\[[^\]]+\] .+? granted (?:admin|user|moderator|gm|observer) access level on /m')
|
||||
->setWeight(0.85),
|
||||
];
|
||||
}
|
||||
|
||||
public function getTitle(): string
|
||||
{
|
||||
return "Project Zomboid Admin Log";
|
||||
}
|
||||
}
|
||||
28
src/Pattern/ProjectZomboid/AdminPattern.php
Normal file
28
src/Pattern/ProjectZomboid/AdminPattern.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace IndifferentKetchup\Codex\Pattern\ProjectZomboid;
|
||||
|
||||
/**
|
||||
* Regex constants for the Project Zomboid admin.txt format.
|
||||
*
|
||||
* Free-form English: '[time] <admin> <verb> ...' Verb dispatch is left
|
||||
* to the analyser layer. The admin name itself can contain parentheses
|
||||
* (Nathan(Weerd)) or whitespace (silly goose) so the parser captures
|
||||
* only the timestamp.
|
||||
*/
|
||||
class AdminPattern
|
||||
{
|
||||
public const string LINE = '/^\[(\d{2}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3})\] .+\.$/';
|
||||
|
||||
public const string ADDED_ITEM = '/^(?<admin>.+?) added item (?<item>Base\.\S+) in (?<target>.+?)\'s inventory$/';
|
||||
|
||||
public const string ADDED_XP = '/^(?<admin>.+?) added (?<amount>[\d.]+) (?<skill>\S+) xp\'s to (?<target>.+)$/';
|
||||
|
||||
public const string GRANTED_ACCESS = '/^(?<admin>.+?) granted (?<level>\w+) access level on (?<target>.+)$/';
|
||||
|
||||
public const string CHANGED_OPTION = '/^(?<admin>.+?) changed option (?<option>\S+?)=(?<value>.+)$/';
|
||||
|
||||
public const string RELOADED_OPTIONS = '/^(?<admin>.+?) reloaded options$/';
|
||||
|
||||
public const string TELEPORTED = '/^(?<admin>.+?) teleported (?<target>.+?) to (?<x>\d+),(?<y>\d+),(?<z>-?\d+)$/';
|
||||
}
|
||||
12
test/src/Games/ProjectZomboid/fixtures/admin-minimal.txt
Normal file
12
test/src/Games/ProjectZomboid/fixtures/admin-minimal.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
[16-04-26 18:33:34.289] AdminUser added item Base.ShotgunShells in Player1's inventory.
|
||||
[16-04-26 18:33:34.290] AdminUser added item Base.ShotgunShells in Player1's inventory.
|
||||
[16-04-26 18:33:34.291] AdminUser added item Base.Bandage in Player2's inventory.
|
||||
[16-04-26 18:34:00.500] AdminUser added 750.0 Blunt xp's to Player1.
|
||||
[16-04-26 18:34:01.225] AdminUser added 525.0 Aiming xp's to Player2.
|
||||
[16-04-26 18:35:10.000] AdminUser granted admin access level on Player1.
|
||||
[16-04-26 18:35:30.115] AdminUser granted user access level on Player2.
|
||||
[16-04-26 18:36:15.500] AdminUser changed option AnnounceDeath=true.
|
||||
[16-04-26 18:36:30.812] AdminUser changed option PVP=false.
|
||||
[16-04-26 18:37:00.014] AdminUser reloaded options.
|
||||
[16-04-26 18:38:00.225] AdminUser teleported Player1 to 1100,2200,0.
|
||||
[16-04-26 18:39:15.500] AdminUser teleported Player2 to 1100,2200,-1.
|
||||
@@ -0,0 +1,83 @@
|
||||
<?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\ProjectZomboidAdminLog;
|
||||
use IndifferentKetchup\Codex\Pattern\ProjectZomboid\AdminPattern;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ProjectZomboidAdminLogTest extends TestCase
|
||||
{
|
||||
private function fixturePath(): string
|
||||
{
|
||||
return __DIR__ . '/../../../../src/Games/ProjectZomboid/fixtures/admin-minimal.txt';
|
||||
}
|
||||
|
||||
public function testParsesEachLineAsAnEntry(): void
|
||||
{
|
||||
$log = (new ProjectZomboidAdminLog())->setLogFile(new PathLogFile($this->fixturePath()));
|
||||
$log->parse();
|
||||
|
||||
$this->assertCount(12, $log->getEntries());
|
||||
}
|
||||
|
||||
public function testAddedItemRegexExtracts(): void
|
||||
{
|
||||
$msg = "AdminUser added item Base.ShotgunShells in Player1's inventory";
|
||||
$this->assertSame(1, preg_match(AdminPattern::ADDED_ITEM, $msg, $m));
|
||||
$this->assertSame('AdminUser', $m['admin']);
|
||||
$this->assertSame('Base.ShotgunShells', $m['item']);
|
||||
$this->assertSame('Player1', $m['target']);
|
||||
}
|
||||
|
||||
public function testAddedXpRegexExtracts(): void
|
||||
{
|
||||
$msg = "AdminUser added 750.0 Blunt xp's to Player1";
|
||||
$this->assertSame(1, preg_match(AdminPattern::ADDED_XP, $msg, $m));
|
||||
$this->assertSame('750.0', $m['amount']);
|
||||
$this->assertSame('Blunt', $m['skill']);
|
||||
$this->assertSame('Player1', $m['target']);
|
||||
}
|
||||
|
||||
public function testGrantedAccessRegexExtracts(): void
|
||||
{
|
||||
$msg = "AdminUser granted admin access level on Player1";
|
||||
$this->assertSame(1, preg_match(AdminPattern::GRANTED_ACCESS, $msg, $m));
|
||||
$this->assertSame('admin', $m['level']);
|
||||
$this->assertSame('Player1', $m['target']);
|
||||
}
|
||||
|
||||
public function testChangedOptionRegexExtracts(): void
|
||||
{
|
||||
$msg = "AdminUser changed option PVP=false";
|
||||
$this->assertSame(1, preg_match(AdminPattern::CHANGED_OPTION, $msg, $m));
|
||||
$this->assertSame('PVP', $m['option']);
|
||||
$this->assertSame('false', $m['value']);
|
||||
}
|
||||
|
||||
public function testReloadedOptionsRegexExtracts(): void
|
||||
{
|
||||
$msg = "AdminUser reloaded options";
|
||||
$this->assertSame(1, preg_match(AdminPattern::RELOADED_OPTIONS, $msg, $m));
|
||||
$this->assertSame('AdminUser', $m['admin']);
|
||||
}
|
||||
|
||||
public function testTeleportedRegexHandlesNegativeZ(): void
|
||||
{
|
||||
$msg = "AdminUser teleported Player2 to 1100,2200,-1";
|
||||
$this->assertSame(1, preg_match(AdminPattern::TELEPORTED, $msg, $m));
|
||||
$this->assertSame('Player2', $m['target']);
|
||||
$this->assertSame('-1', $m['z']);
|
||||
}
|
||||
|
||||
public function testDetectiveDispatchesByContent(): void
|
||||
{
|
||||
$detective = (new Detective())
|
||||
->setLogFile(new PathLogFile($this->fixturePath()))
|
||||
->addPossibleLogClass(ProjectZomboidAdminLog::class);
|
||||
|
||||
$this->assertInstanceOf(ProjectZomboidAdminLog::class, $detective->detect());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user