TrackManiaControl/application/core/Callbacks/TrackManiaCallbacks.php
Steffen Schröder cf345dfdc3 improved trackmania callbacks
added legacy trackmania callbacks
2014-05-24 18:32:09 +02:00

129 lines
4.1 KiB
PHP

<?php
namespace ManiaControl\Callbacks;
use ManiaControl\Callbacks\Models\RecordCallback;
use ManiaControl\ManiaControl;
/**
* Class handling and parsing TrackMania Callbacks
*
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class TrackManiaCallbacks implements CallbackListener {
/*
* Private Properties
*/
private $maniaControl = null;
/**
* Create a new TrackMania Callbacks Instance
*
* @param ManiaControl $maniaControl
* @param CallbackManager $callbackManager
*/
public function __construct(ManiaControl $maniaControl, CallbackManager $callbackManager) {
$this->maniaControl = $maniaControl;
// Register for callbacks
$callbackManager->registerCallbackListener(Callbacks::ONWAYPOINT, $this, 'handleOnWayPointCallback');
$callbackManager->registerCallbackListener(CallbackManager::CB_TM_PLAYERCHECKPOINT, $this, 'handlePlayerCheckpointCallback');
$callbackManager->registerCallbackListener(CallbackManager::CB_TM_PLAYERFINISH, $this, 'handlePlayerFinishCallback');
}
/**
* Handle OnWayPoint Callback
*
* @param array $callback
*/
public function handleOnWayPointCallback(array $callback) {
$login = $callback[0];
$player = $this->maniaControl->playerManager->getPlayer($login);
if (!$player) {
return;
}
// Build callback
$wayPointCallback = new RecordCallback();
$wayPointCallback->rawCallback = $callback;
$wayPointCallback->setPlayer($player);
$wayPointCallback->blockId = $callback[1];
$wayPointCallback->time = (int)$callback[2];
$wayPointCallback->checkpoint = (int)$callback[3];
$wayPointCallback->isEndRace = (bool)$callback[4];
$wayPointCallback->lapTime = (int)$callback[5];
$wayPointCallback->lapCheckpoint = (int)$callback[6];
$wayPointCallback->lap = 0;
$wayPointCallback->isEndLap = (bool)$callback[7];
if ($wayPointCallback->checkpoint > 0) {
$currentMap = $this->maniaControl->mapManager->getCurrentMap();
$wayPointCallback->lap += $wayPointCallback->checkpoint / $currentMap->nbCheckpoints;
}
if ($wayPointCallback->isEndRace) {
$wayPointCallback->name = $wayPointCallback::FINISH;
} else if ($wayPointCallback->isEndLap) {
$wayPointCallback->name = $wayPointCallback::LAPFINISH;
} else {
$wayPointCallback->name = $wayPointCallback::CHECKPOINT;
}
$this->maniaControl->callbackManager->triggerCallback($wayPointCallback);
}
/**
* Handle Hard-Coded Player Checkpoint Callback
*
* @param array $callback
*/
public function handlePlayerCheckpointCallback(array $callback) {
$data = $callback[1];
$login = $data[1];
$player = $this->maniaControl->playerManager->getPlayer($login);
if (!$player) {
return;
}
$checkpointCallback = new RecordCallback();
$checkpointCallback->name = $checkpointCallback::CHECKPOINT;
$checkpointCallback->isLegacyCallback = true;
$checkpointCallback->setPlayer($player);
$checkpointCallback->time = (int)$data[2];
$checkpointCallback->lap = (int)$data[3];
$checkpointCallback->checkpoint = (int)$data[4];
$checkpointCallback->lapCheckpoint = $checkpointCallback->checkpoint;
if ($checkpointCallback->lap > 0) {
$currentMap = $this->maniaControl->mapManager->getCurrentMap();
$checkpointCallback->lapCheckpoint -= $checkpointCallback->lap * $currentMap->nbCheckpoints;
}
$this->maniaControl->callbackManager->triggerCallback($checkpointCallback);
}
/**
* Handle Hard-Coded Player Finish Callback
*
* @param array $callback
*/
public function handlePlayerFinishCallback(array $callback) {
$data = $callback[1];
$login = $data[1];
$player = $this->maniaControl->playerManager->getPlayer($login);
if (!$player) {
return;
}
$finishCallback = new RecordCallback();
$finishCallback->name = $finishCallback::FINISH;
$finishCallback->isLegacyCallback = true;
$finishCallback->setPlayer($player);
$finishCallback->time = (int)$data[2];
$this->maniaControl->callbackManager->triggerCallback($finishCallback);
}
}