Add ProjectZomboidChatLog (chat.txt)
Handles both chat-engine events (bracketed level prefix) and bare
server-alert lines via an optional level group. Detectors: filename
match on _chat.txt plus content signatures for ChatMessage{...} log
entries and the chat-server initialization banner. CHAT_MESSAGE and
SERVER_ALERT named-group regexes ride along on ChatPattern for analyser
extraction in phase B.
This commit is contained in:
47
src/Log/ProjectZomboid/ProjectZomboidChatLog.php
Normal file
47
src/Log/ProjectZomboid/ProjectZomboidChatLog.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\ChatPattern;
|
||||
|
||||
class ProjectZomboidChatLog extends ProjectZomboidEventLog
|
||||
{
|
||||
public static function getDefaultParser(): ParserInterface
|
||||
{
|
||||
return static::makePatternParser(
|
||||
ChatPattern::LINE,
|
||||
[PatternParser::TIME, PatternParser::LEVEL]
|
||||
);
|
||||
}
|
||||
|
||||
public static function getDefaultAnalyser(): AnalyserInterface
|
||||
{
|
||||
return new PatternAnalyser();
|
||||
}
|
||||
|
||||
public static function getDetectors(): array
|
||||
{
|
||||
return [
|
||||
(new FilenameDetector())
|
||||
->setPattern('/_chat\.txt$/')
|
||||
->setWeight(0.95),
|
||||
(new WeightedSinglePatternDetector())
|
||||
->setPattern('/Got message:ChatMessage\{chat=\w+/')
|
||||
->setWeight(0.95),
|
||||
(new WeightedSinglePatternDetector())
|
||||
->setPattern('/Start chat server initialization/')
|
||||
->setWeight(0.85),
|
||||
];
|
||||
}
|
||||
|
||||
public function getTitle(): string
|
||||
{
|
||||
return "Project Zomboid Chat Log";
|
||||
}
|
||||
}
|
||||
24
src/Pattern/ProjectZomboid/ChatPattern.php
Normal file
24
src/Pattern/ProjectZomboid/ChatPattern.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace IndifferentKetchup\Codex\Pattern\ProjectZomboid;
|
||||
|
||||
/**
|
||||
* Regex constants for the Project Zomboid chat.txt format.
|
||||
*
|
||||
* Two row variants share the file: bracket-level chat-engine events
|
||||
* (e.g. [time][info] message) and bare server-alert messages
|
||||
* (e.g. [time] Server alert ...). The LINE pattern accepts both via an
|
||||
* optional non-capturing wrapper around the level.
|
||||
*
|
||||
* LINE captures, in order:
|
||||
* 1. time (DD-MM-YY HH:MM:SS.mmm)
|
||||
* 2. level (info | warn | error | empty for server alerts)
|
||||
*/
|
||||
class ChatPattern
|
||||
{
|
||||
public const string LINE = '/^\[(\d{2}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3})\](?:\[(\w+)\])?\s+.*$/';
|
||||
|
||||
public const string CHAT_MESSAGE = '/ChatMessage\{chat=(?<channel>\w+), author=\'(?<author>[^\']+)\', text=\'(?<text>.*?)\'\}/';
|
||||
|
||||
public const string SERVER_ALERT = '/^Server alert message:\s*\'(?<text>.*?)\'\s+sent\.\.?$/';
|
||||
}
|
||||
13
test/src/Games/ProjectZomboid/fixtures/chat-minimal.txt
Normal file
13
test/src/Games/ProjectZomboid/fixtures/chat-minimal.txt
Normal file
@@ -0,0 +1,13 @@
|
||||
[16-04-26 16:16:34.105][info] Start chat server initialization....
|
||||
[16-04-26 16:16:34.108][info] General chat has id = 0.
|
||||
[16-04-26 16:16:34.108][info] Say chat has id = 1.
|
||||
[16-04-26 16:16:34.108][info] Shout chat has id = 2.
|
||||
[16-04-26 16:16:34.108][info] Radio chat has id = 3.
|
||||
[16-04-26 17:05:03.280][info] Got message:ChatMessage{chat=Local, author='Player1', text='hello'}.
|
||||
[16-04-26 17:05:03.282][info] Message ChatMessage{chat=Local, author='Player1', text='hello'} sent to chat (id = 2) members.
|
||||
[16-04-26 17:22:55.182][info] Got message:ChatMessage{chat=Local, author='Player2', text='hi there'}.
|
||||
[16-04-26 17:22:55.184][info] Got message:ChatMessage{chat=Global, author='Player1', text='who is online'}.
|
||||
[16-04-26 17:22:55.186][info] Got message:ChatMessage{chat=Local, author='AdminUser', text='cleanup soon'}.
|
||||
[16-04-26 18:25:19.523] Server alert message: 'Test Server: Server will restart in 5 seconds for mod updates.' sent..
|
||||
[16-04-26 18:25:21.323] Server alert message: 'Test Server: Server will restart in 4 seconds for mod updates.' sent..
|
||||
[16-04-26 18:25:23.123] Server alert message: 'Test Server: Server will restart in 3 seconds for mod updates.' sent..
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace IndifferentKetchup\Codex\Test\Tests\Games\ProjectZomboid\Log;
|
||||
|
||||
use IndifferentKetchup\Codex\Detective\Detective;
|
||||
use IndifferentKetchup\Codex\Log\File\PathLogFile;
|
||||
use IndifferentKetchup\Codex\Log\Level;
|
||||
use IndifferentKetchup\Codex\Log\ProjectZomboid\ProjectZomboidChatLog;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ProjectZomboidChatLogTest extends TestCase
|
||||
{
|
||||
private function fixturePath(): string
|
||||
{
|
||||
return __DIR__ . '/../../../../src/Games/ProjectZomboid/fixtures/chat-minimal.txt';
|
||||
}
|
||||
|
||||
public function testParsesEachLineAsAnEntry(): void
|
||||
{
|
||||
$log = (new ProjectZomboidChatLog())->setLogFile(new PathLogFile($this->fixturePath()));
|
||||
$log->parse();
|
||||
|
||||
$this->assertCount(13, $log->getEntries());
|
||||
}
|
||||
|
||||
public function testInfoBracketIsParsedAsLevel(): void
|
||||
{
|
||||
$log = (new ProjectZomboidChatLog())->setLogFile(new PathLogFile($this->fixturePath()));
|
||||
$log->parse();
|
||||
|
||||
$first = $log->getEntries()[0];
|
||||
$this->assertSame(Level::INFO, $first->getLevel());
|
||||
$this->assertNotNull($first->getTime());
|
||||
}
|
||||
|
||||
public function testServerAlertLinesParseWithoutLevel(): void
|
||||
{
|
||||
$log = (new ProjectZomboidChatLog())->setLogFile(new PathLogFile($this->fixturePath()));
|
||||
$log->parse();
|
||||
|
||||
$alert = $log->getEntries()[10];
|
||||
$this->assertStringContainsString('Server alert message', (string) $alert);
|
||||
}
|
||||
|
||||
public function testDetectiveDispatchesByContent(): void
|
||||
{
|
||||
$detective = (new Detective())
|
||||
->setLogFile(new PathLogFile($this->fixturePath()))
|
||||
->addPossibleLogClass(ProjectZomboidChatLog::class);
|
||||
|
||||
$this->assertInstanceOf(ProjectZomboidChatLog::class, $detective->detect());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user