diff --git a/src/Analyzers/BodyAnalyzer.php b/src/Analyzers/BodyAnalyzer.php index fe46e5e..2e5e66e 100644 --- a/src/Analyzers/BodyAnalyzer.php +++ b/src/Analyzers/BodyAnalyzer.php @@ -175,11 +175,17 @@ protected function run() $version = $this->version; $players = $this->get(PlayerMetaAnalyzer::class); + + // The player number is used for chat messages. + $playersByNumber = []; + // The player index is used for game actions. $playersByIndex = []; foreach ($players as $player) { + $playersByNumber[$player->number] = $player; $playersByIndex[$player->index] = $player; } - $this->playersByIndex = $playersByIndex; + + $this->playersByNumber = $playersByNumber; $size = strlen($this->body); $this->position = 0; @@ -392,10 +398,12 @@ private function processChatMessage() if (substr($chat, 3, 2) == '--' && substr($chat, -2) == '--') { // Skip messages like "--Warning: You are under attack... --" return; - } else if (!empty($this->playersByIndex[$chat[2]])) { - $player = $this->playersByIndex[$chat[2]]; + } else if (!empty($this->playersByNumber[$chat[2]])) { + $player = $this->playersByNumber[$chat[2]]; } else { - // This player left before the game started. + // Shouldn't happen, but we'll let the ChatMessage factory + // create a fake player for this message. + // TODO that auto-create behaviour is probably not desirable… $player = null; } $this->chatMessages[] = ChatMessage::create( diff --git a/src/Analyzers/PlayerMetaAnalyzer.php b/src/Analyzers/PlayerMetaAnalyzer.php index 1bce41b..4b52eea 100644 --- a/src/Analyzers/PlayerMetaAnalyzer.php +++ b/src/Analyzers/PlayerMetaAnalyzer.php @@ -27,7 +27,7 @@ protected function run() $players = []; for ($i = 0; $i <= 8; $i += 1) { - $player = $this->readPlayerMeta(); + $player = $this->readPlayerMeta($i); if ($player->humanRaw === 0 || $player->humanRaw === 1) { continue; } @@ -51,9 +51,10 @@ protected function run() * * @return \RecAnalyst\Model\Player */ - protected function readPlayerMeta() + protected function readPlayerMeta($i) { $player = new Player($this->rec); + $player->number = $i; $player->index = $this->readHeader('l', 4); $human = $this->readHeader('l', 4); $length = $this->readHeader('L', 4); diff --git a/src/Model/Player.php b/src/Model/Player.php index 3a3b041..d650ebe 100644 --- a/src/Model/Player.php +++ b/src/Model/Player.php @@ -35,6 +35,17 @@ class Player */ public $index = -1; + /** + * The player number. This is different from the index. The player number + * is always a sequential unique ID. The player index is the same for + * multiple players in the case of a co-op game. + * + * @var int + * @internal Might be public later, but should just be used internally for + * now. + */ + public $number = -1; + /** * Defines if the player is a human. *