2014-05-24 17:45:35 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace ManiaControl\Callbacks;
|
|
|
|
|
|
|
|
use ManiaControl\Callbacks\Models\RecordCallback;
|
2017-03-25 20:19:45 +01:00
|
|
|
use ManiaControl\Callbacks\Structures\TrackMania\OnDefaultEventStructure;
|
2017-03-29 13:28:32 +02:00
|
|
|
use ManiaControl\Callbacks\Structures\TrackMania\OnEventStartLineStructure;
|
|
|
|
use ManiaControl\Callbacks\Structures\TrackMania\OnCommandStructure;
|
|
|
|
use ManiaControl\Callbacks\Structures\TrackMania\OnScoresStructure;
|
|
|
|
use ManiaControl\Callbacks\Structures\TrackMania\OnEventWayPointStructure;
|
2014-05-24 17:45:35 +02:00
|
|
|
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>
|
2017-02-04 11:49:23 +01:00
|
|
|
* @copyright 2014-2017 ManiaControl Team
|
2014-05-24 17:45:35 +02:00
|
|
|
* @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;
|
|
|
|
|
2017-03-29 13:28:32 +02:00
|
|
|
|
2014-05-24 17:45:35 +02:00
|
|
|
/**
|
|
|
|
* Create a new TrackMania Callbacks Instance
|
|
|
|
*
|
|
|
|
* @param ManiaControl $maniaControl
|
|
|
|
* @param CallbackManager $callbackManager
|
|
|
|
*/
|
|
|
|
public function __construct(ManiaControl $maniaControl, CallbackManager $callbackManager) {
|
|
|
|
$this->maniaControl = $maniaControl;
|
|
|
|
|
2017-03-25 13:44:03 +01:00
|
|
|
// Register for script callbacks
|
|
|
|
$callbackManager->registerCallbackListener(Callbacks::SCRIPTCALLBACK, $this, 'handleScriptCallbacks');
|
2014-05-24 17:45:35 +02:00
|
|
|
}
|
|
|
|
|
2017-03-25 13:44:03 +01:00
|
|
|
/**
|
|
|
|
* Handle Script Callbacks
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param mixed $data
|
|
|
|
*/
|
|
|
|
public function handleScriptCallbacks($name, $data) {
|
|
|
|
if (!$this->maniaControl->getCallbackManager()->callbackListeningExists($name)) {
|
2017-03-25 20:19:45 +01:00
|
|
|
//return; //Leave that disabled while testing/implementing Callbacks
|
2017-03-25 13:44:03 +01:00
|
|
|
}
|
|
|
|
switch ($name) {
|
|
|
|
//MP4 New Callbacks
|
2017-03-29 13:28:32 +02:00
|
|
|
case Callbacks::TM_SCORES:
|
|
|
|
$this->maniaControl->getCallbackManager()->triggerCallback(Callbacks::TM_SCORES, new OnScoresStructure($this->maniaControl, $data));
|
|
|
|
break;
|
2017-03-25 20:19:45 +01:00
|
|
|
case Callbacks::TM_ONEVENTDEFAULT:
|
|
|
|
$this->maniaControl->getCallbackManager()->triggerCallback(Callbacks::TM_ONEVENTDEFAULT, new OnDefaultEventStructure($this->maniaControl, $data));
|
2017-03-25 13:44:03 +01:00
|
|
|
break;
|
2017-03-29 13:28:32 +02:00
|
|
|
case Callbacks::TM_ONEVENTSTARTLINE:
|
|
|
|
$this->maniaControl->getCallbackManager()->triggerCallback(Callbacks::TM_ONEVENTSTARTLINE, new OnEventStartLineStructure($this->maniaControl, $data));
|
|
|
|
break;
|
|
|
|
case Callbacks::TM_ONCOMMAND:
|
|
|
|
$this->maniaControl->getCallbackManager()->triggerCallback(Callbacks::TM_ONCOMMAND, new OnCommandStructure($this->maniaControl, $data));
|
|
|
|
break;
|
|
|
|
case Callbacks::TM_ONPLAYERADDED:
|
|
|
|
break;
|
|
|
|
case Callbacks::TM_ONPLAYERREMOVED:
|
|
|
|
break;
|
|
|
|
case Callbacks::TM_ONWAYPOINT:
|
|
|
|
$this->maniaControl->getCallbackManager()->triggerCallback(Callbacks::TM_ONWAYPOINT, new OnEventWayPointStructure($this->maniaControl, $data));
|
|
|
|
break;
|
|
|
|
case Callbacks:: TM_ONGIVEUP:
|
|
|
|
break;
|
|
|
|
case Callbacks::TM_ONRESPAWN:
|
|
|
|
break;
|
|
|
|
case Callbacks::TM_ONSTUNT:
|
|
|
|
break;
|
|
|
|
case Callbacks::TM_ONSTARTCOUNTDOWN:
|
|
|
|
break;
|
|
|
|
case Callbacks::TM_WARMUPSTART:
|
|
|
|
break;
|
|
|
|
case Callbacks::TM_WARMUPSTARTROUND:
|
|
|
|
break;
|
|
|
|
case Callbacks::TM_WARMUPENDROUND:
|
|
|
|
break;
|
|
|
|
case Callbacks::TM_WARMUPEND:
|
|
|
|
break;
|
2017-03-25 13:44:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-24 17:45:35 +02:00
|
|
|
/**
|
|
|
|
* Handle OnWayPoint Callback
|
|
|
|
*
|
|
|
|
* @param array $callback
|
|
|
|
*/
|
|
|
|
public function handleOnWayPointCallback(array $callback) {
|
|
|
|
$login = $callback[0];
|
2014-08-13 11:05:52 +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];
|
2017-03-25 13:44:03 +01:00
|
|
|
$wayPointCallback->time = (int) $callback[2];
|
|
|
|
$wayPointCallback->checkpoint = (int) $callback[3];
|
2014-05-24 18:56:20 +02:00
|
|
|
$wayPointCallback->isEndRace = Formatter::parseBoolean($callback[4]);
|
2017-03-25 13:44:03 +01:00
|
|
|
$wayPointCallback->lapTime = (int) $callback[5];
|
|
|
|
$wayPointCallback->lapCheckpoint = (int) $callback[6];
|
2014-05-24 18:32:09 +02:00
|
|
|
$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) {
|
2017-03-25 13:44:03 +01: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-13 11:05:52 +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-13 11:05:52 +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);
|
2017-03-25 13:44:03 +01:00
|
|
|
$checkpointCallback->time = (int) $data[2];
|
|
|
|
$checkpointCallback->lap = (int) $data[3];
|
|
|
|
$checkpointCallback->checkpoint = (int) $data[4];
|
2014-05-24 18:32:09 +02:00
|
|
|
$checkpointCallback->lapCheckpoint = $checkpointCallback->checkpoint;
|
|
|
|
|
|
|
|
if ($checkpointCallback->lap > 0) {
|
2017-03-25 13:44:03 +01: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-13 11:05:52 +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-13 11:05:52 +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);
|
2017-03-25 13:44:03 +01:00
|
|
|
$finishCallback->time = (int) $data[2];
|
2014-05-24 18:32:09 +02:00
|
|
|
|
2014-08-13 11:05:52 +02:00
|
|
|
$this->maniaControl->getCallbackManager()->triggerCallback($finishCallback);
|
2014-05-24 18:32:09 +02:00
|
|
|
}
|
2014-05-24 17:45:35 +02:00
|
|
|
}
|