From c3f5de1f2927e7ec151147bddafe2a3b5b3190c6 Mon Sep 17 00:00:00 2001 From: kremsy Date: Mon, 8 May 2017 19:55:07 +0200 Subject: [PATCH] refactored some callback code in trackmania and removed some deprecates --- core/Callbacks/CallbackManager.php | 17 +- core/Callbacks/Callbacks.php | 184 +----------------- core/Callbacks/Models/BaseCallback.php | 38 ---- core/Callbacks/Models/RecordCallback.php | 39 ---- core/Callbacks/ShootManiaCallbacks.php | 74 ------- .../TrackMania/OnWayPointEventStructure.php | 41 ++-- core/Callbacks/TrackManiaCallbacks.php | 110 ++--------- plugins/MCTeam/Dedimania/DedimaniaPlugin.php | 84 +++----- plugins/MCTeam/LocalRecordsPlugin.php | 78 +++----- 9 files changed, 97 insertions(+), 568 deletions(-) delete mode 100644 core/Callbacks/Models/BaseCallback.php delete mode 100644 core/Callbacks/Models/RecordCallback.php diff --git a/core/Callbacks/CallbackManager.php b/core/Callbacks/CallbackManager.php index 1154fa32..b81fd8c9 100644 --- a/core/Callbacks/CallbackManager.php +++ b/core/Callbacks/CallbackManager.php @@ -2,7 +2,6 @@ namespace ManiaControl\Callbacks; -use ManiaControl\Callbacks\Models\BaseCallback; use ManiaControl\General\UsageInformationAble; use ManiaControl\General\UsageInformationTrait; use ManiaControl\ManiaControl; @@ -16,7 +15,7 @@ use ManiaControl\ManiaControl; */ class CallbackManager implements UsageInformationAble { use UsageInformationTrait; - + /* * Constants */ @@ -276,22 +275,16 @@ class CallbackManager implements UsageInformationAble { /** * Trigger a specific Callback * - * @param mixed $callback + * @param mixed $callbackName */ - public function triggerCallback($callback) { - if ($callback instanceof BaseCallback) { - $callbackName = $callback->name; - } else { - $callbackName = $callback; - } + public function triggerCallback($callbackName) { + if (!$this->callbackListeningExists($callbackName)) { return; } $params = func_get_args(); - if (!($callback instanceof BaseCallback)) { - $params = array_slice($params, 1, null, true); - } + $params = array_slice($params, 1, null, true); foreach ($this->callbackListenings[$callbackName] as $listening) { /** @var Listening $listening */ diff --git a/core/Callbacks/Callbacks.php b/core/Callbacks/Callbacks.php index 73a4abd7..fdc0b0e5 100644 --- a/core/Callbacks/Callbacks.php +++ b/core/Callbacks/Callbacks.php @@ -1,6 +1,5 @@ - * @copyright 2014-2017 ManiaControl Team - * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 - */ -abstract class BaseCallback { - /* - * Public Properties - */ - public $name = null; - public $rawCallback = null; - public $isLegacyCallback = null; - - public $pid = null; - public $login = null; - /** @var Player $player */ - public $player = null; - - /** - * Set the corresponding Player - * - * @param Player $player - */ - public function setPlayer(Player $player) { - $this->pid = $player->pid; - $this->login = $player->login; - $this->player = $player; - } -} diff --git a/core/Callbacks/Models/RecordCallback.php b/core/Callbacks/Models/RecordCallback.php deleted file mode 100644 index bb6a1824..00000000 --- a/core/Callbacks/Models/RecordCallback.php +++ /dev/null @@ -1,39 +0,0 @@ - - * @copyright 2014-2017 ManiaControl Team - * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 - */ -class RecordCallback extends BaseCallback { - /* - * Constants - */ - const CHECKPOINT = 'RecordCallback.Checkpoint'; - const FINISH = 'RecordCallback.Finish'; - const LAPFINISH = 'RecordCallback.LapFinish'; - - /* - * Public Properties - */ - public $time = null; - /** @var Player $player */ - public $player = null; - public $racetime = null; - public $lapTime = null; - public $stuntsscore = null; - public $checkpointinrace = null; - public $checkpointinlap = null; - public $isendrace = null; - public $isendlap = null; - public $blockid = null; - public $speed = null; - public $distance = null; -} diff --git a/core/Callbacks/ShootManiaCallbacks.php b/core/Callbacks/ShootManiaCallbacks.php index 028bd91d..22e31369 100644 --- a/core/Callbacks/ShootManiaCallbacks.php +++ b/core/Callbacks/ShootManiaCallbacks.php @@ -2,7 +2,6 @@ namespace ManiaControl\Callbacks; -use ManiaControl\Callbacks\Models\RecordCallback; use ManiaControl\Callbacks\Structures\Common\BasePlayerTimeStructure; use ManiaControl\Callbacks\Structures\Common\UIPropertiesBaseStructure; use ManiaControl\Callbacks\Structures\ShootMania\OnActionCustomEventStructure; @@ -157,79 +156,6 @@ class ShootManiaCallbacks implements CallbackListener { case Callbacks::SM_ROYAL_ROUNDWINNER: $this->maniaControl->getCallbackManager()->triggerCallback($name, new OnRoyalRoundWinnerStructure($this->maniaControl, $data)); break; - - //Old Callbacks - case 'LibXmlRpc_Rankings': - $this->maniaControl->getServer()->getRankingManager()->updateRankings($data[0]); - break; - case 'LibAFK_IsAFK': - $this->triggerAfkStatus($data[0]); - break; - case 'WarmUp_Status': - $this->maniaControl->getCallbackManager()->triggerCallback(Callbacks::WARMUPSTATUS, $data[0]); - break; - case self::CB_TIMEATTACK_ONCHECKPOINT: - $this->handleTimeAttackOnCheckpoint($name, $data); - break; - case self::CB_TIMEATTACK_ONFINISH: - $this->handleTimeAttackOnFinish($name, $data); - break; } } - - /** - * Triggers the AFK Status of an Player - * - * @param string $login - */ - private function triggerAfkStatus($login) { - $player = $this->maniaControl->getPlayerManager()->getPlayer($login); - $this->maniaControl->getCallbackManager()->triggerCallback(Callbacks::AFKSTATUS, $player); - } - - /** - * Handle TimeAttack OnCheckpoint Callback - * - * @param string $name - * @param array $data - */ - public function handleTimeAttackOnCheckpoint($name, array $data) { - $login = $data[0]; - $player = $this->maniaControl->getPlayerManager()->getPlayer($login); - if (!$player) { - return; - } - - // Trigger checkpoint callback - $checkpointCallback = new RecordCallback(); - $checkpointCallback->rawCallback = array($name, $data); - $checkpointCallback->name = $checkpointCallback::CHECKPOINT; - $checkpointCallback->setPlayer($player); - $checkpointCallback->time = (int) $data[1]; - - $this->maniaControl->getCallbackManager()->triggerCallback($checkpointCallback); - } - - /** - * Handle TimeAttack OnFinish Callback - * - * @param string $name - * @param array $data - */ - public function handleTimeAttackOnFinish($name, array $data) { - $login = $data[0]; - $player = $this->maniaControl->getPlayerManager()->getPlayer($login); - if (!$player) { - return; - } - - // Trigger finish callback - $finishCallback = new RecordCallback(); - $finishCallback->rawCallback = array($name, $data); - $finishCallback->name = $finishCallback::FINISH; - $finishCallback->setPlayer($player); - $finishCallback->time = (int) $data[1]; - - $this->maniaControl->getCallbackManager()->triggerCallback($finishCallback); - } } diff --git a/core/Callbacks/Structures/TrackMania/OnWayPointEventStructure.php b/core/Callbacks/Structures/TrackMania/OnWayPointEventStructure.php index f147a59d..915c5521 100644 --- a/core/Callbacks/Structures/TrackMania/OnWayPointEventStructure.php +++ b/core/Callbacks/Structures/TrackMania/OnWayPointEventStructure.php @@ -3,7 +3,6 @@ namespace ManiaControl\Callbacks\Structures\TrackMania; -use ManiaControl\Callbacks\Models\RecordCallback; use ManiaControl\Callbacks\Structures\Common\BasePlayerTimeStructure; use ManiaControl\ManiaControl; use ManiaControl\Utils\Formatter; @@ -27,6 +26,7 @@ class OnWayPointEventStructure extends BasePlayerTimeStructure { private $blockId; private $speed; private $distance; + private $lapNumber; /** * OnWayPointEventStructure constructor. @@ -42,36 +42,16 @@ class OnWayPointEventStructure extends BasePlayerTimeStructure { $this->stuntsScore = $this->getPlainJsonObject()->stuntsscore; $this->checkPointInRace = (int) $this->getPlainJsonObject()->checkpointinrace; $this->checkPointInLap = (int) $this->getPlainJsonObject()->checkpointinlap; - $this->isEndRace = $this->getPlainJsonObject()->isendrace; - $this->isEndLap = $this->getPlainJsonObject()->isendlap; + $this->isEndRace = Formatter::parseBoolean($this->getPlainJsonObject()->isendrace); + $this->isEndLap = Formatter::parseBoolean($this->getPlainJsonObject()->isendlap); $this->blockId = $this->getPlainJsonObject()->blockid; $this->speed = $this->getPlainJsonObject()->speed; $this->distance = $this->getPlainJsonObject()->distance; - // Build callback //TODO remove the old lagacy stuff and update the uses to the new Structure - $wayPointCallback = new RecordCallback(); - $wayPointCallback->rawCallback = $data; - $wayPointCallback->setPlayer($this->getPlayer()); - $wayPointCallback->blockId = $this->blockId; - $wayPointCallback->time = $this->raceTime; - $wayPointCallback->checkpoint = $this->checkPointInRace; - $wayPointCallback->isEndRace = Formatter::parseBoolean($this->isEndRace); - $wayPointCallback->lapTime = $this->lapTime; - $wayPointCallback->lapCheckpoint = $this->checkPointInLap; - $wayPointCallback->lap = 0; - $wayPointCallback->isEndLap = Formatter::parseBoolean($this->isEndLap); - if ($wayPointCallback->checkpoint > 0) { - $currentMap = $this->maniaControl->getMapManager()->getCurrentMap(); - $wayPointCallback->lap += $wayPointCallback->checkpoint / $currentMap->nbCheckpoints; + if ($this->checkPointInRace > 0) { + $currentMap = $this->maniaControl->getMapManager()->getCurrentMap(); + $this->lapNumber = intval($this->checkPointInRace / $currentMap->nbCheckpoints); } - if ($wayPointCallback->isEndRace) { - $wayPointCallback->name = $wayPointCallback::FINISH; - } else if ($wayPointCallback->isEndLap) { - $wayPointCallback->name = $wayPointCallback::LAPFINISH; - } else { - $wayPointCallback->name = $wayPointCallback::CHECKPOINT; - } - $this->maniaControl->getCallbackManager()->triggerCallback($wayPointCallback); } /** @@ -154,4 +134,13 @@ class OnWayPointEventStructure extends BasePlayerTimeStructure { return $this->distance; } + /** + * Gets the Current Lap Number + * + * @return float|int + */ + public function getLapNumber() { + return $this->lapNumber; + } + } \ No newline at end of file diff --git a/core/Callbacks/TrackManiaCallbacks.php b/core/Callbacks/TrackManiaCallbacks.php index 4ef87eab..4b0d8f8b 100644 --- a/core/Callbacks/TrackManiaCallbacks.php +++ b/core/Callbacks/TrackManiaCallbacks.php @@ -2,7 +2,6 @@ namespace ManiaControl\Callbacks; -use ManiaControl\Callbacks\Models\RecordCallback; use ManiaControl\Callbacks\Structures\Common\BasePlayerTimeStructure; use ManiaControl\Callbacks\Structures\Common\UIPropertiesBaseStructure; use ManiaControl\Callbacks\Structures\TrackMania\OnCommandStructure; @@ -15,7 +14,6 @@ use ManiaControl\Callbacks\Structures\TrackMania\OnStuntEventStructure; use ManiaControl\Callbacks\Structures\TrackMania\OnWarmupStartEndRoundStructure; use ManiaControl\Callbacks\Structures\TrackMania\OnWayPointEventStructure; use ManiaControl\ManiaControl; -use ManiaControl\Utils\Formatter; /** * Class handling and parsing TrackMania Callbacks @@ -75,7 +73,9 @@ class TrackManiaCallbacks implements CallbackListener { $this->maniaControl->getCallbackManager()->triggerCallback($name, new BasePlayerTimeStructure($this->maniaControl, $data)); break; case Callbacks::TM_ONWAYPOINT: - $this->maniaControl->getCallbackManager()->triggerCallback($name, new OnWayPointEventStructure($this->maniaControl, $data)); + $this->handleWayPointCallback(new OnWayPointEventStructure($this->maniaControl, $data)); + + //$this->maniaControl->getCallbackManager()->triggerCallback($name, $wayPointStructure); break; case Callbacks::TM_ONRESPAWN: $this->maniaControl->getCallbackManager()->triggerCallback($name, new OnRespawnStructure($this->maniaControl, $data)); @@ -100,106 +100,18 @@ class TrackManiaCallbacks implements CallbackListener { } } - /** - * Handle OnWayPoint Callback + * Trigger the three different Types of Callbacks * - * @param array $callback + * @param \ManiaControl\Callbacks\Structures\TrackMania\OnWayPointEventStructure $structure */ - public function handleOnWayPointCallback(array $callback) { - $login = $callback[0]; - $player = $this->maniaControl->getPlayerManager()->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 = Formatter::parseBoolean($callback[4]); - $wayPointCallback->lapTime = (int) $callback[5]; - $wayPointCallback->lapCheckpoint = (int) $callback[6]; - $wayPointCallback->lap = 0; - $wayPointCallback->isEndLap = Formatter::parseBoolean($callback[7]); - - if ($wayPointCallback->checkpoint > 0) { - $currentMap = $this->maniaControl->getMapManager()->getCurrentMap(); - $wayPointCallback->lap += $wayPointCallback->checkpoint / $currentMap->nbCheckpoints; - } - - if ($wayPointCallback->isEndRace) { - $wayPointCallback->name = $wayPointCallback::FINISH; - } else if ($wayPointCallback->isEndLap) { - $wayPointCallback->name = $wayPointCallback::LAPFINISH; + private function handleWayPointCallback(OnWayPointEventStructure $structure) { + if ($structure->getIsEndRace()) { + $this->maniaControl->getCallbackManager()->triggerCallback(Callbacks::TM_ONFINISHLINE, $structure); + } else if ($structure->getIsEndLap()) { + $this->maniaControl->getCallbackManager()->triggerCallback(Callbacks::TM_ONLAPFINISH, $structure); } else { - $wayPointCallback->name = $wayPointCallback::CHECKPOINT; + $this->maniaControl->getCallbackManager()->triggerCallback(Callbacks::TM_ONWAYPOINT, $structure); } - - $this->maniaControl->getCallbackManager()->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->getPlayerManager()->getPlayer($login); - if (!$player) { - return; - } - - // Build checkpoint callback - $checkpointCallback = new RecordCallback(); - $checkpointCallback->isLegacyCallback = true; - $checkpointCallback->rawCallback = $callback; - $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->getMapManager()->getCurrentMap(); - $checkpointCallback->lapCheckpoint -= $checkpointCallback->lap * $currentMap->nbCheckpoints; - } - - if ($checkpointCallback->lapCheckpoint === 0) { - $checkpointCallback->name = $checkpointCallback::LAPFINISH; - } else { - $checkpointCallback->name = $checkpointCallback::CHECKPOINT; - } - - $this->maniaControl->getCallbackManager()->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->getPlayerManager()->getPlayer($login); - if (!$player) { - return; - } - - // Build finish callback - $finishCallback = new RecordCallback(); - $finishCallback->name = $finishCallback::FINISH; - $finishCallback->isLegacyCallback = true; - $finishCallback->rawCallback = $callback; - $finishCallback->setPlayer($player); - $finishCallback->time = (int) $data[2]; - - $this->maniaControl->getCallbackManager()->triggerCallback($finishCallback); } } diff --git a/plugins/MCTeam/Dedimania/DedimaniaPlugin.php b/plugins/MCTeam/Dedimania/DedimaniaPlugin.php index b2548675..3a79e131 100644 --- a/plugins/MCTeam/Dedimania/DedimaniaPlugin.php +++ b/plugins/MCTeam/Dedimania/DedimaniaPlugin.php @@ -11,7 +11,7 @@ use FML\Script\Features\Paging; use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Callbacks\Callbacks; -use ManiaControl\Callbacks\Models\RecordCallback; +use ManiaControl\Callbacks\Structures\TrackMania\OnWayPointEventStructure; use ManiaControl\Callbacks\TimerListener; use ManiaControl\Commands\CommandListener; use ManiaControl\Files\AsyncHttpRequest; @@ -146,16 +146,16 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene $this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer'); $this->maniaControl->getCallbackManager()->registerCallbackListener(PlayerManager::CB_PLAYERCONNECT, $this, 'handlePlayerConnect'); $this->maniaControl->getCallbackManager()->registerCallbackListener(PlayerManager::CB_PLAYERDISCONNECT, $this, 'handlePlayerDisconnect'); - $this->maniaControl->getCallbackManager()->registerCallbackListener(RecordCallback::CHECKPOINT, $this, 'handleCheckpointCallback'); - //$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::TM_ONWAYPOINT, $this, 'handleCheckpointCallback'); - $this->maniaControl->getCallbackManager()->registerCallbackListener(RecordCallback::LAPFINISH, $this, 'handleLapFinishCallback'); - $this->maniaControl->getCallbackManager()->registerCallbackListener(RecordCallback::FINISH, $this, 'handleFinishCallback'); + $this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::TM_ONWAYPOINT, $this, 'handleCheckpointCallback'); + $this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::TM_ONFINISHLINE, $this, 'handleFinishCallback'); + $this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::TM_ONLAPFINISH, $this, 'handleFinishCallback'); $this->maniaControl->getTimerManager()->registerTimerListening($this, 'updateEverySecond', 1000); $this->maniaControl->getTimerManager()->registerTimerListening($this, 'handleEveryHalfMinute', 1000 * 30); $this->maniaControl->getTimerManager()->registerTimerListening($this, 'updatePlayerList', 1000 * 60 * 3); - $this->maniaControl->getCommandManager()->registerCommandListener(array('dedirecs', 'dedirecords'), $this, 'showDediRecordsList', false, 'Shows a list of Dedimania records of the current map.'); + $this->maniaControl->getCommandManager()->registerCommandListener(array('dedirecs', + 'dedirecords'), $this, 'showDediRecordsList', false, 'Shows a list of Dedimania records of the current map.'); // Open session $serverInfo = $this->maniaControl->getServer()->getInfo(); @@ -463,17 +463,17 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene } if ($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_ENABLE)) { - $manialink = $this->buildManialink(); - $this->maniaControl->getManialinkManager()->sendManialink($manialink); + $this->sendManialink(); } } /** - * Build Manialink - * - * @return \FML\ManiaLink + * Builds and Sends the Manialink */ - private function buildManialink() { + private function sendManialink() { + if (!isset($this->dedimaniaData) || !isset($this->dedimaniaData->records)) { + return null; + } $records = $this->dedimaniaData->records; $title = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_TITLE); @@ -557,7 +557,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene $timeLabel->setTextEmboss(true); } - return $manialink; + $this->maniaControl->getManialinkManager()->sendManialink($manialink); } /** @@ -662,8 +662,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene } if ($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_ENABLE)) { - $manialink = $this->buildManialink(); - $this->maniaControl->getManialinkManager()->sendManialink($manialink, $player->login); + $this->sendManialink(); } }); @@ -863,23 +862,9 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene /** * Handle Checkpoint Callback * - * @param RecordCallback $callback + * @param OnWayPointEventStructure $callback */ - public function handleCheckpointCallback(RecordCallback $callback) { - //var_dump($callback->lapTime); //FIXME, dedimania needs Finish as LastCp, or NbOfCheckpoints wrong? - - if ($callback->isLegacyCallback || !$callback->lapTime) { - return; - } - if (!isset($this->checkpoints[$callback->login])) { - $this->checkpoints[$callback->login] = array(); - } - $this->checkpoints[$callback->login][$callback->lapCheckpoint] = $callback->lapTime; - } - - /*public function handleCheckpointCallback(OnWayPointEventStructure $structure) { - //var_dump($callback->lapTime); //new structure but that change isnt enough - + public function handleCheckpointCallback(OnWayPointEventStructure $structure) { if (!$structure->getLapTime()) { return; } @@ -889,30 +874,19 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene $this->checkpoints[$login] = array(); } $this->checkpoints[$login][$structure->getCheckPointInLap()] = $structure->getLapTime(); - }*/ - - /** - * Handle LapFinish Callback - * - * @param RecordCallback $callback - */ - public function handleLapFinishCallback(RecordCallback $callback) { - $this->handleFinishCallback($callback); } /** * Handle Finish Callback * - * @param RecordCallback $callback + * @param OnWayPointEventStructure $callback */ - public function handleFinishCallback(RecordCallback $callback) { + public function handleFinishCallback(OnWayPointEventStructure $structure) { if (!isset($this->dedimaniaData)) { return; } - if ($callback->isLegacyCallback) { - return; - } - if ($callback->time <= 0) { + + if ($structure->getRaceTime() <= 0) { // Invalid time return; } @@ -926,15 +900,17 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene return; } - $oldRecord = $this->getDedimaniaRecord($callback->login); - if ($oldRecord->nullRecord || $oldRecord && $oldRecord->best > $callback->lapTime) { + $player = $structure->getPlayer(); + + $oldRecord = $this->getDedimaniaRecord($player->login); + if ($oldRecord->nullRecord || $oldRecord && $oldRecord->best > $structure->getLapTime()) { // Save time $newRecord = new RecordData(null); - $checkPoints = $this->getCheckpoints($callback->login); - $checkPoints = $checkPoints . "," . $callback->lapTime; + $checkPoints = $this->getCheckpoints($player->login); + $checkPoints = $checkPoints . "," . $structure->getLapTime(); - $newRecord->constructNewRecord($callback->login, $callback->player->nickname, $callback->lapTime, $checkPoints, true); + $newRecord->constructNewRecord($player->login, $player->nickname, $structure->getLapTime(), $checkPoints, true); if ($this->insertDedimaniaRecord($newRecord, $oldRecord)) { // Get newly saved record @@ -956,9 +932,9 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene // Only improved time $improvement = 'improved his/her'; } - $message = '$390$<$fff' . $callback->player->nickname . '$> ' . $improvement . ' $<$ff0' . $newRecord->rank . '.$> Dedimania Record: $<$fff' . Formatter::formatTime($newRecord->best) . '$>'; + $message = '$390$<$fff' . $player->nickname . '$> ' . $improvement . ' $<$ff0' . $newRecord->rank . '.$> Dedimania Record: $<$fff' . Formatter::formatTime($newRecord->best) . '$>'; if (!$oldRecord->nullRecord) { - $message .= ' ($<$ff0' . $oldRecord->rank . '.$> $<$fff-' . Formatter::formatTime(($oldRecord->best - $callback->lapTime)) . '$>)'; + $message .= ' ($<$ff0' . $oldRecord->rank . '.$> $<$fff-' . Formatter::formatTime(($oldRecord->best - $structure->getLapTime())) . '$>)'; } $this->maniaControl->getChat()->sendInformation($message . '!'); @@ -974,7 +950,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene * @return RecordData $record */ private function getDedimaniaRecord($login) { - if (!isset($this->dedimaniaData) || !$this->dedimaniaData->records) { + if (!isset($this->dedimaniaData) || !isset($this->dedimaniaData->records)) { return new RecordData(null); } $records = $this->dedimaniaData->records; diff --git a/plugins/MCTeam/LocalRecordsPlugin.php b/plugins/MCTeam/LocalRecordsPlugin.php index 4b519e25..0c15457b 100644 --- a/plugins/MCTeam/LocalRecordsPlugin.php +++ b/plugins/MCTeam/LocalRecordsPlugin.php @@ -12,7 +12,7 @@ use ManiaControl\Admin\AuthenticationManager; use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Callbacks\Callbacks; -use ManiaControl\Callbacks\Models\RecordCallback; +use ManiaControl\Callbacks\Structures\TrackMania\OnWayPointEventStructure; use ManiaControl\Callbacks\TimerListener; use ManiaControl\Commands\CommandListener; use ManiaControl\Logger; @@ -60,9 +60,9 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList * Private properties */ /** @var ManiaControl $maniaControl */ - private $maniaControl = null; + private $maniaControl = null; private $updateManialink = false; - private $checkpoints = array(); + private $checkpoints = array(); /** * @see \ManiaControl\Plugins\Plugin::prepare() @@ -135,9 +135,10 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList $this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer'); $this->maniaControl->getCallbackManager()->registerCallbackListener(PlayerManager::CB_PLAYERCONNECT, $this, 'handlePlayerConnect'); - $this->maniaControl->getCallbackManager()->registerCallbackListener(RecordCallback::CHECKPOINT, $this, 'handleCheckpointCallback'); - $this->maniaControl->getCallbackManager()->registerCallbackListener(RecordCallback::LAPFINISH, $this, 'handleLapFinishCallback'); - $this->maniaControl->getCallbackManager()->registerCallbackListener(RecordCallback::FINISH, $this, 'handleFinishCallback'); + + $this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::TM_ONWAYPOINT, $this, 'handleCheckpointCallback'); + $this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::TM_ONFINISHLINE, $this, 'handleFinishCallback'); + $this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::TM_ONLAPFINISH, $this, 'handleFinishCallback'); $this->maniaControl->getCommandManager()->registerCommandListener(array('recs', 'records'), $this, 'showRecordsList', false, 'Shows a list of Local Records on the current map.'); $this->maniaControl->getCommandManager()->registerCommandListener('delrec', $this, 'deleteRecord', true, 'Removes a record from the database.'); @@ -197,8 +198,7 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList } $this->updateManialink = false; - if ($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_ENABLE) - ) { + if ($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_ENABLE)) { $manialink = $this->buildManialink(); $this->maniaControl->getManialinkManager()->sendManialink($manialink); } @@ -343,8 +343,7 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList } switch ($setting->setting) { - case self::SETTING_WIDGET_ENABLE: - { + case self::SETTING_WIDGET_ENABLE: { if ($setting->value) { $this->updateManialink = true; } else { @@ -363,23 +362,12 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList * * @param RecordCallback $callback */ - public function handleCheckpointCallback(RecordCallback $callback) { - if ($callback->isLegacyCallback) { - return; + public function handleCheckpointCallback(OnWayPointEventStructure $structure) { + $playerLogin = $structure->getPlayer()->login; + if (!isset($this->checkpoints[$playerLogin])) { + $this->checkpoints[$playerLogin] = array(); } - if (!isset($this->checkpoints[$callback->login])) { - $this->checkpoints[$callback->login] = array(); - } - $this->checkpoints[$callback->login][$callback->lapCheckpoint] = $callback->lapTime; - } - - /** - * Handle LapFinish Callback - * - * @param RecordCallback $callback - */ - public function handleLapFinishCallback(RecordCallback $callback) { - $this->handleFinishCallback($callback); + $this->checkpoints[$playerLogin][$structure->getCheckPointInLap()] = $structure->getLapTime(); } /** @@ -387,31 +375,30 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList * * @param RecordCallback $callback */ - public function handleFinishCallback(RecordCallback $callback) { - if ($callback->isLegacyCallback) { - return; - } - if ($callback->time <= 0) { + public function handleFinishCallback(OnWayPointEventStructure $structure) { + if ($structure->getRaceTime() <= 0) { // Invalid time return; } $map = $this->maniaControl->getMapManager()->getCurrentMap(); - $checkpointsString = $this->getCheckpoints($callback->player->login); - $this->checkpoints[$callback->login] = array(); + $player = $structure->getPlayer(); + + $checkpointsString = $this->getCheckpoints($player->login); + $this->checkpoints[$player->login] = array(); // Check old record of the player - $oldRecord = $this->getLocalRecord($map, $callback->player); + $oldRecord = $this->getLocalRecord($map, $player); if ($oldRecord) { - if ($oldRecord->time < $callback->time) { + if ($oldRecord->time < $structure->getRaceTime()) { // Not improved return; } - if ($oldRecord->time == $callback->time) { + if ($oldRecord->time == $structure->getRaceTime()) { // Same time // TODO: respect notify-settings - $message = '$<$fff' . $callback->player->nickname . '$> equalized his/her $<$ff0' . $oldRecord->rank . '.$> Local Record: $<$fff' . Formatter::formatTime($oldRecord->time) . '$>!'; + $message = '$<$fff' . $player->nickname . '$> equalized his/her $<$ff0' . $oldRecord->rank . '.$> Local Record: $<$fff' . Formatter::formatTime($oldRecord->time) . '$>!'; $this->maniaControl->getChat()->sendInformation('$3c0' . $message); return; } @@ -426,8 +413,8 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList `checkpoints` ) VALUES ( {$map->index}, - {$callback->player->index}, - {$callback->time}, + {$player->index}, + {$structure->getRaceTime()}, '{$checkpointsString}' ) ON DUPLICATE KEY UPDATE `time` = VALUES(`time`), @@ -440,7 +427,7 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList $this->updateManialink = true; // Announce record - $newRecord = $this->getLocalRecord($map, $callback->player); + $newRecord = $this->getLocalRecord($map, $player); $improvedRank = (!$oldRecord || $newRecord->rank < $oldRecord->rank); $notifyOnlyDriver = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_NOTIFY_ONLY_DRIVER); @@ -450,7 +437,7 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList if ($notifyOnlyDriver) { $message .= 'You'; } else { - $message .= '$<$fff' . $callback->player->nickname . '$>'; + $message .= '$<$fff' . $player->nickname . '$>'; } $message .= ' ' . ($improvedRank ? 'gained' : 'improved') . ' the'; $message .= ' $<$ff0' . $newRecord->rank . '.$> Local Record:'; @@ -461,11 +448,11 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList $message .= '$<$ff0' . $oldRecord->rank . '.$> '; } $timeDiff = $oldRecord->time - $newRecord->time; - $message .= '$<$fff-' . Formatter::formatTime($timeDiff) . '$>)'; + $message .= '$<$fff-' . Formatter::formatTime($timeDiff) . '$>)'; } if ($notifyOnlyDriver) { - $this->maniaControl->getChat()->sendInformation($message, $callback->player); + $this->maniaControl->getChat()->sendInformation($message, $player); } else if (!$notifyOnlyBestRecords || $newRecord->rank <= $notifyOnlyBestRecords) { $this->maniaControl->getChat()->sendInformation($message); } @@ -632,8 +619,7 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList * @param Player $player */ public function deleteRecord(array $chat, Player $player) { - if (!$this->maniaControl->getAuthenticationManager()->checkRight($player, AuthenticationManager::AUTH_LEVEL_MASTERADMIN) - ) { + if (!$this->maniaControl->getAuthenticationManager()->checkRight($player, AuthenticationManager::AUTH_LEVEL_MASTERADMIN)) { $this->maniaControl->getAuthenticationManager()->sendNotAllowed($player); return; } @@ -644,7 +630,7 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList return; } - $recordId = (int)$commandParts[1]; + $recordId = (int) $commandParts[1]; $currentMap = $this->maniaControl->getMapManager()->getCurrentMap(); $records = $this->getLocalRecords($currentMap); if (count($records) < $recordId) {