2014-05-24 17:45:35 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace ManiaControl\Callbacks;
|
|
|
|
|
|
|
|
use ManiaControl\Callbacks\Models\RecordCallback;
|
|
|
|
use ManiaControl\ManiaControl;
|
2014-05-24 18:56:20 +02:00
|
|
|
use ManiaControl\Utils\Formatter;
|
2014-05-24 17:45:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
|
|
|
/*
|
2014-08-02 22:31:46 +02:00
|
|
|
* Private properties
|
2014-05-24 17:45:35 +02:00
|
|
|
*/
|
2014-08-02 22:31:46 +02:00
|
|
|
/** @var ManiaControl $maniaControl */
|
2014-05-24 17:45:35 +02:00
|
|
|
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');
|
2014-05-24 18:32:09 +02:00
|
|
|
$callbackManager->registerCallbackListener(CallbackManager::CB_TM_PLAYERCHECKPOINT, $this, 'handlePlayerCheckpointCallback');
|
|
|
|
$callbackManager->registerCallbackListener(CallbackManager::CB_TM_PLAYERFINISH, $this, 'handlePlayerFinishCallback');
|
2014-05-24 17:45:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle OnWayPoint Callback
|
|
|
|
*
|
|
|
|
* @param array $callback
|
|
|
|
*/
|
|
|
|
public function handleOnWayPointCallback(array $callback) {
|
|
|
|
$login = $callback[0];
|
2014-08-05 02:17:41 +02:00
|
|
|
$player = $this->maniaControl->getPlayerManager()
|
|
|
|
->getPlayer($login);
|
2014-05-24 17:45:35 +02:00
|
|
|
if (!$player) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build callback
|
2014-05-24 18:32:09 +02:00
|
|
|
$wayPointCallback = new RecordCallback();
|
|
|
|
$wayPointCallback->rawCallback = $callback;
|
|
|
|
$wayPointCallback->setPlayer($player);
|
|
|
|
$wayPointCallback->blockId = $callback[1];
|
|
|
|
$wayPointCallback->time = (int)$callback[2];
|
|
|
|
$wayPointCallback->checkpoint = (int)$callback[3];
|
2014-05-24 18:56:20 +02:00
|
|
|
$wayPointCallback->isEndRace = Formatter::parseBoolean($callback[4]);
|
2014-05-24 18:32:09 +02:00
|
|
|
$wayPointCallback->lapTime = (int)$callback[5];
|
|
|
|
$wayPointCallback->lapCheckpoint = (int)$callback[6];
|
|
|
|
$wayPointCallback->lap = 0;
|
2014-05-24 18:56:20 +02:00
|
|
|
$wayPointCallback->isEndLap = Formatter::parseBoolean($callback[7]);
|
2014-05-24 18:32:09 +02:00
|
|
|
|
|
|
|
if ($wayPointCallback->checkpoint > 0) {
|
2014-08-05 02:17:41 +02:00
|
|
|
$currentMap = $this->maniaControl->getMapManager()
|
|
|
|
->getCurrentMap();
|
2014-05-24 18:32:09 +02:00
|
|
|
$wayPointCallback->lap += $wayPointCallback->checkpoint / $currentMap->nbCheckpoints;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($wayPointCallback->isEndRace) {
|
|
|
|
$wayPointCallback->name = $wayPointCallback::FINISH;
|
|
|
|
} else if ($wayPointCallback->isEndLap) {
|
|
|
|
$wayPointCallback->name = $wayPointCallback::LAPFINISH;
|
2014-05-24 17:45:35 +02:00
|
|
|
} else {
|
2014-05-24 18:32:09 +02:00
|
|
|
$wayPointCallback->name = $wayPointCallback::CHECKPOINT;
|
|
|
|
}
|
|
|
|
|
2014-08-05 02:17:41 +02:00
|
|
|
$this->maniaControl->getCallbackManager()
|
|
|
|
->triggerCallback($wayPointCallback);
|
2014-05-24 18:32:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle Hard-Coded Player Checkpoint Callback
|
|
|
|
*
|
|
|
|
* @param array $callback
|
|
|
|
*/
|
|
|
|
public function handlePlayerCheckpointCallback(array $callback) {
|
|
|
|
$data = $callback[1];
|
|
|
|
$login = $data[1];
|
2014-08-05 02:17:41 +02:00
|
|
|
$player = $this->maniaControl->getPlayerManager()
|
|
|
|
->getPlayer($login);
|
2014-05-24 18:32:09 +02:00
|
|
|
if (!$player) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-24 18:56:20 +02:00
|
|
|
// Build checkpoint callback
|
2014-05-24 18:32:09 +02:00
|
|
|
$checkpointCallback = new RecordCallback();
|
|
|
|
$checkpointCallback->isLegacyCallback = true;
|
2014-05-24 18:56:20 +02:00
|
|
|
$checkpointCallback->rawCallback = $callback;
|
2014-05-24 18:32:09 +02:00
|
|
|
$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) {
|
2014-08-05 02:17:41 +02:00
|
|
|
$currentMap = $this->maniaControl->getMapManager()
|
|
|
|
->getCurrentMap();
|
2014-05-24 18:32:09 +02:00
|
|
|
$checkpointCallback->lapCheckpoint -= $checkpointCallback->lap * $currentMap->nbCheckpoints;
|
2014-05-24 17:45:35 +02:00
|
|
|
}
|
|
|
|
|
2014-05-24 18:36:08 +02:00
|
|
|
if ($checkpointCallback->lapCheckpoint === 0) {
|
|
|
|
$checkpointCallback->name = $checkpointCallback::LAPFINISH;
|
|
|
|
} else {
|
|
|
|
$checkpointCallback->name = $checkpointCallback::CHECKPOINT;
|
|
|
|
}
|
|
|
|
|
2014-08-05 02:17:41 +02:00
|
|
|
$this->maniaControl->getCallbackManager()
|
|
|
|
->triggerCallback($checkpointCallback);
|
2014-05-24 17:45:35 +02:00
|
|
|
}
|
2014-05-24 18:32:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle Hard-Coded Player Finish Callback
|
|
|
|
*
|
|
|
|
* @param array $callback
|
|
|
|
*/
|
|
|
|
public function handlePlayerFinishCallback(array $callback) {
|
|
|
|
$data = $callback[1];
|
|
|
|
$login = $data[1];
|
2014-08-05 02:17:41 +02:00
|
|
|
$player = $this->maniaControl->getPlayerManager()
|
|
|
|
->getPlayer($login);
|
2014-05-24 18:32:09 +02:00
|
|
|
if (!$player) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-24 18:56:20 +02:00
|
|
|
// Build finish callback
|
2014-05-24 18:32:09 +02:00
|
|
|
$finishCallback = new RecordCallback();
|
|
|
|
$finishCallback->name = $finishCallback::FINISH;
|
|
|
|
$finishCallback->isLegacyCallback = true;
|
2014-05-24 18:56:20 +02:00
|
|
|
$finishCallback->rawCallback = $callback;
|
2014-05-24 18:32:09 +02:00
|
|
|
$finishCallback->setPlayer($player);
|
|
|
|
$finishCallback->time = (int)$data[2];
|
|
|
|
|
2014-08-05 02:17:41 +02:00
|
|
|
$this->maniaControl->getCallbackManager()
|
|
|
|
->triggerCallback($finishCallback);
|
2014-05-24 18:32:09 +02:00
|
|
|
}
|
2014-05-24 17:45:35 +02:00
|
|
|
}
|