Skip to content

Commit

Permalink
Fix associating chat messages with players in co-op games.
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed Dec 16, 2016
1 parent c4266e2 commit bd06dc5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
16 changes: 12 additions & 4 deletions src/Analyzers/BodyAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
5 changes: 3 additions & 2 deletions src/Analyzers/PlayerMetaAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions src/Model/Player.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit bd06dc5

Please sign in to comment.