Skip to content

Commit

Permalink
Add reading of Move and Interact actions
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed Dec 7, 2016
1 parent a62919a commit e7a61a7
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 2 deletions.
29 changes: 29 additions & 0 deletions src/Analyzers/BodyAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,35 @@ protected function run()
$this->position++;

switch ($command) {
case self::COMMAND_INTERACT:
$playerId = ord($this->body[$this->position++]);
$this->position += 2;
$targetId = $this->readBody('l', 4);
$unitCount = $this->readBody('l', 4);
$this->push(new Actions\InteractAction(
$this->rec,
$this->currentTime,
$playerId,
$targetId,
$this->readBody('f', 4),
$this->readBody('f', 4),
$this->readUnits($unitCount)
));
break;
case self::COMMAND_MOVE:
$playerId = ord($this->body[$this->position++]);
$this->position += 2;
$this->position += 4;
$count = $this->readBody('l', 4);
$this->push(new Actions\MoveAction(
$this->rec,
$this->currentTime,
$playerId,
$this->readBody('f', 4),
$this->readBody('f', 4),
$this->readUnits($count)
));
break;
// player resign
case self::COMMAND_RESIGN:
$playerIndex = ord($this->body[$this->position]);
Expand Down
26 changes: 25 additions & 1 deletion src/Model/Actions/InteractAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,32 @@ class InteractAction extends Action
* @param \RecAnalyst\RecordedGame $rec Recorded game instance.
* @param int $time Recorded game instance.
*/
public function __construct(RecordedGame $rec, $time)
public function __construct(RecordedGame $rec, $time, $playerId, $targetId, $x, $y, $units)
{
parent::__construct($rec, $time);

$this->playerId = $playerId;
$this->targetId = $targetId;
$this->x = $x;
$this->y = $y;
$this->units = $units;
}

/**
* Get a string representation of the action.
*
* @return string
*/
public function __toString()
{
return sprintf(
'Interact(playerId=%d, targetId=%d, x=%.2f, y=%.2f, units[%d]={%s})',
$this->playerId,
$this->targetId,
$this->x,
$this->y,
count($this->units),
implode(', ', $this->units)
);
}
}
52 changes: 51 additions & 1 deletion src/Model/Actions/MoveAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,64 @@ class MoveAction extends Action
*/
const ID = 0x3;

/**
* ID of the player that is executing this action.
*
* @var int
*/
public $playerId;

/**
* X-coordinate to move to.
*
* @var float
*/
public $x;

/**
* Y-coordinate to move to.
*
* @var float
*/
public $y;

/**
* Unit IDs to move.
*
* @var int[]
*/
public $units = [];

/**
* Create a ...
*
* @param \RecAnalyst\RecordedGame $rec Recorded game instance.
* @param int $time Recorded game instance.
*/
public function __construct(RecordedGame $rec, $time)
public function __construct(RecordedGame $rec, $time, $playerId, $x, $y, $units)
{
parent::__construct($rec, $time);

$this->playerId = $playerId;
$this->x = $x;
$this->y = $y;
$this->units = $units;
}

/**
* Get a string representation of the action.
*
* @return string
*/
public function __toString()
{
return sprintf(
'Move(playerId=%d, x=%.2f, y=%.2f, units[%d]={%s})',
$this->playerId,
$this->x,
$this->y,
count($this->units),
implode(', ', $this->units)
);
}
}

0 comments on commit e7a61a7

Please sign in to comment.