Add ProjectZomboidItemLog (item.txt)
Per-line item gain/loss event log. Parser captures only the timestamp; analysers decompose location/delta/item via ItemPattern::FIELDS. Detectors: filename match plus content signature on the container/floor/inventory location verbs paired with a signed delta.
This commit is contained in:
44
src/Log/ProjectZomboid/ProjectZomboidItemLog.php
Normal file
44
src/Log/ProjectZomboid/ProjectZomboidItemLog.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?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\ItemPattern;
|
||||
|
||||
class ProjectZomboidItemLog extends ProjectZomboidEventLog
|
||||
{
|
||||
public static function getDefaultParser(): ParserInterface
|
||||
{
|
||||
return static::makePatternParser(
|
||||
ItemPattern::LINE,
|
||||
[PatternParser::TIME]
|
||||
);
|
||||
}
|
||||
|
||||
public static function getDefaultAnalyser(): AnalyserInterface
|
||||
{
|
||||
return new PatternAnalyser();
|
||||
}
|
||||
|
||||
public static function getDetectors(): array
|
||||
{
|
||||
return [
|
||||
(new FilenameDetector())
|
||||
->setPattern('/_item\.txt$/')
|
||||
->setWeight(0.95),
|
||||
(new WeightedSinglePatternDetector())
|
||||
->setPattern('/^\[[^\]]+\] \d{17} "[^"]+" (?:container|floor|inventory) [+\-]\d+ /m')
|
||||
->setWeight(0.90),
|
||||
];
|
||||
}
|
||||
|
||||
public function getTitle(): string
|
||||
{
|
||||
return "Project Zomboid Item Log";
|
||||
}
|
||||
}
|
||||
15
src/Pattern/ProjectZomboid/ItemPattern.php
Normal file
15
src/Pattern/ProjectZomboid/ItemPattern.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace IndifferentKetchup\Codex\Pattern\ProjectZomboid;
|
||||
|
||||
/**
|
||||
* Regex constants for the Project Zomboid item.txt format.
|
||||
*
|
||||
* [time] steamid "player" location ±N x,y,z [Base.ItemId].
|
||||
*/
|
||||
class ItemPattern
|
||||
{
|
||||
public const string LINE = '/^\[(\d{2}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3})\] \d{17} "[^"]+" \S+ [+\-]\d+ \d+,\d+,\d+ \[[^\]]+\]\.$/';
|
||||
|
||||
public const string FIELDS = '/^\[\d{2}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}\] (?<steamid>\d{17}) "(?<player>[^"]+)" (?<location>\S+) (?<delta>[+\-]\d+) (?<x>\d+),(?<y>\d+),(?<z>\d+) \[(?<item>[^\]]+)\]\.$/';
|
||||
}
|
||||
10
test/src/Games/ProjectZomboid/fixtures/item-minimal.txt
Normal file
10
test/src/Games/ProjectZomboid/fixtures/item-minimal.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
[16-04-26 19:01:00.277] 76561198000000001 "Player1" container -1 0,0,0 [Base.LouisvilleMap5].
|
||||
[16-04-26 19:07:58.669] 76561198000000001 "Player1" floor +1 1000,2000,0 [Base.Dung_Sheep].
|
||||
[16-04-26 19:10:52.112] 76561198000000001 "Player1" floor +1 1001,2001,0 [Base.Dung_Cow].
|
||||
[16-04-26 19:28:34.852] 76561198000000002 "Player2" floor +1 1010,2010,0 [Base.Dung_Pig].
|
||||
[16-04-26 19:28:34.856] 76561198000000002 "Player2" floor +1 1010,2010,0 [Base.Dung_Cow].
|
||||
[16-04-26 19:30:01.100] 76561198000000003 "AdminUser" inventory +1 1020,2020,0 [Base.Hammer].
|
||||
[16-04-26 19:31:15.221] 76561198000000003 "AdminUser" inventory -1 1020,2020,0 [Base.Hammer].
|
||||
[16-04-26 19:35:42.812] 76561198000000001 "Player1" container -1 1002,2002,0 [Base.WaterBottleFull].
|
||||
[16-04-26 19:40:00.514] 76561198000000002 "Player2" floor +1 1011,2011,0 [Base.Bandage].
|
||||
[16-04-26 19:42:25.223] 76561198000000002 "Player2" inventory +5 1011,2011,0 [Base.Bullets9mm].
|
||||
@@ -0,0 +1,44 @@
|
||||
<?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\ProjectZomboidItemLog;
|
||||
use IndifferentKetchup\Codex\Pattern\ProjectZomboid\ItemPattern;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ProjectZomboidItemLogTest extends TestCase
|
||||
{
|
||||
private function fixturePath(): string
|
||||
{
|
||||
return __DIR__ . '/../../../../src/Games/ProjectZomboid/fixtures/item-minimal.txt';
|
||||
}
|
||||
|
||||
public function testParsesEachLineAsAnEntry(): void
|
||||
{
|
||||
$log = (new ProjectZomboidItemLog())->setLogFile(new PathLogFile($this->fixturePath()));
|
||||
$log->parse();
|
||||
|
||||
$this->assertCount(10, $log->getEntries());
|
||||
}
|
||||
|
||||
public function testFieldsRegexExtractsItemAndDelta(): void
|
||||
{
|
||||
$line = '[16-04-26 19:42:25.223] 76561198000000002 "Player2" inventory +5 1011,2011,0 [Base.Bullets9mm].';
|
||||
$this->assertSame(1, preg_match(ItemPattern::FIELDS, $line, $m));
|
||||
$this->assertSame('Player2', $m['player']);
|
||||
$this->assertSame('inventory', $m['location']);
|
||||
$this->assertSame('+5', $m['delta']);
|
||||
$this->assertSame('Base.Bullets9mm', $m['item']);
|
||||
}
|
||||
|
||||
public function testDetectiveDispatchesByContent(): void
|
||||
{
|
||||
$detective = (new Detective())
|
||||
->setLogFile(new PathLogFile($this->fixturePath()))
|
||||
->addPossibleLogClass(ProjectZomboidItemLog::class);
|
||||
|
||||
$this->assertInstanceOf(ProjectZomboidItemLog::class, $detective->detect());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user