diff --git a/application/core/Callbacks/CallbackManager.php b/application/core/Callbacks/CallbackManager.php index 39b560da..fa61069a 100644 --- a/application/core/Callbacks/CallbackManager.php +++ b/application/core/Callbacks/CallbackManager.php @@ -17,9 +17,6 @@ class CallbackManager { const CB_ONINIT = 'ManiaControl.OnInit'; const CB_AFTERINIT = 'ManiaControl.AfterInit'; const CB_ONSHUTDOWN = 'ManiaControl.OnShutdown'; - const CB_CLIENTUPDATED = 'ManiaControl.ClientUpdated'; - const CB_BEGINMAP = 'ManiaControl.BeginMap'; - const CB_ENDMAP = 'ManiaControl.EndMap'; // ManiaPlanet callbacks const CB_MP_SERVERSTART = 'ManiaPlanet.ServerStart'; @@ -52,11 +49,6 @@ class CallbackManager { private $maniaControl = null; private $callbackListeners = array(); private $scriptCallbackListener = array(); - private $last1Second = -1; - private $last5Second = -1; - private $last1Minute = -1; - private $mapEnded = false; - private $mapBegan = false; /** * Construct a new Callbacks Manager @@ -65,10 +57,6 @@ class CallbackManager { */ public function __construct(ManiaControl $maniaControl) { $this->maniaControl = $maniaControl; - $this->last1Second = time(); - $this->last5Second = time(); - $this->last1Minute = time(); - } /** @@ -200,29 +188,23 @@ class CallbackManager { // Handle callbacks foreach($callbacks as $callback) { $callbackName = $callback[0]; - switch($callbackName) { + switch ($callbackName) { case 'ManiaPlanet.BeginMap': - if (!$this->mapBegan) { - $this->triggerCallback(self::CB_BEGINMAP, $callback); - $this->mapBegan = true; - $this->mapEnded = false; - } + $this->maniaControl->mapManager->handleBeginMap($callback); + $this->triggerCallback($callbackName, $callback); break; case 'ManiaPlanet.EndMatch': //TODO temporary fix case 'ManiaPlanet.EndMap': - if (!$this->mapEnded) { - $this->triggerCallback(self::CB_ENDMAP, $callback); - $this->mapEnded = true; - $this->mapBegan = false; - } + $this->maniaControl->mapManager->handleEndMap($callback); + $this->triggerCallback($callbackName, $callback); break; case self::CB_MP_MODESCRIPTCALLBACK: $this->handleScriptCallback($callback); - $this->triggerCallback(self::CB_MP_MODESCRIPTCALLBACK, $callback); + $this->triggerCallback($callbackName, $callback); break; case self::CB_MP_MODESCRIPTCALLBACKARRAY: $this->handleScriptCallback($callback); - $this->triggerCallback(self::CB_MP_MODESCRIPTCALLBACKARRAY, $callback); + $this->triggerCallback($callbackName, $callback); break; default: $this->triggerCallback($callbackName, $callback); @@ -242,21 +224,13 @@ class CallbackManager { switch($scriptCallbackName) { case 'BeginMap': case 'LibXmlRpc_BeginMap': + $this->maniaControl->mapManager->handleScriptBeginMap($callback); $this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData); - if (!$this->mapBegan) { - $this->triggerCallback(self::CB_BEGINMAP, $callback); - $this->mapBegan = true; - $this->mapEnded = false; - } break; case 'EndMap': case 'LibXmlRpc_EndMap': + $this->maniaControl->mapManager->handleScriptEndMap($callback); $this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData); - if (!$this->mapEnded) { - $this->triggerCallback(self::CB_ENDMAP, $callback); - $this->mapEnded = true; - $this->mapBegan = false; - } break; default: $this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData); diff --git a/application/core/Maps/MapManager.php b/application/core/Maps/MapManager.php index 019b5ed4..22233174 100644 --- a/application/core/Maps/MapManager.php +++ b/application/core/Maps/MapManager.php @@ -21,11 +21,12 @@ use Maniaplanet\DedicatedServer\Xmlrpc\Exception; * @author kremsy & steeffeen */ class MapManager implements CallbackListener { - /** + /* * Constants */ const TABLE_MAPS = 'mc_maps'; const CB_BEGINMAP = 'MapManager.BeginMap'; + const CB_ENDMAP = 'MapManager.EndMap'; const CB_MAPS_UPDATED = 'MapManager.MapsUpdated'; const CB_KARMA_UPDATED = 'MapManager.KarmaUpdated'; const SETTING_PERMISSION_ADD_MAP = 'Add Maps'; @@ -35,7 +36,7 @@ class MapManager implements CallbackListener { const SETTING_PERMISSION_SKIP_MAP = 'Skip Map'; const SETTING_PERMISSION_RESTART_MAP = 'Restart Map'; - /** + /* * Public Properties */ public $mapQueue = null; @@ -44,16 +45,18 @@ class MapManager implements CallbackListener { public $mxList = null; public $mxManager = null; - /** + /* * Private Properties */ private $maniaControl = null; private $maps = array(); /** @var Map $currentMap */ private $currentMap = null; + private $mapEnded = false; + private $mapBegan = false; /** - * Construct map manager + * Construct a new Map Manager * * @param \ManiaControl\ManiaControl $maniaControl */ @@ -70,10 +73,9 @@ class MapManager implements CallbackListener { // Register for callbacks $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_ONINIT, $this, 'handleOnInit'); - $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_BEGINMAP, $this, 'handleBeginMap'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_MAPLISTMODIFIED, $this, 'mapsModified'); - //Define Rights + // Define Rights $this->maniaControl->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_ADD_MAP, AuthenticationManager::AUTH_LEVEL_ADMIN); $this->maniaControl->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_REMOVE_MAP, AuthenticationManager::AUTH_LEVEL_ADMIN); $this->maniaControl->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_SHUFFLE_MAPS, AuthenticationManager::AUTH_LEVEL_ADMIN); @@ -83,7 +85,7 @@ class MapManager implements CallbackListener { } /** - * Initialize necessary database tables + * Initialize necessary Database Tables * * @return bool */ @@ -242,7 +244,6 @@ class MapManager implements CallbackListener { unset($this->maps[$uid]); } - /** * Restructures the Maplist */ @@ -368,23 +369,24 @@ class MapManager implements CallbackListener { } /** - * Fetch current Map + * Freshly fetch current Map * - * @return bool + * @return Map */ - private function fetchCurrentMap() { + public function fetchCurrentMap() { $rpcMap = $this->maniaControl->client->getCurrentMapInfo(); if (array_key_exists($rpcMap->uId, $this->maps)) { $this->currentMap = $this->maps[$rpcMap->uId]; + // TODO: why set numbers? shouldn't they be set already? $this->currentMap->nbCheckpoints = $rpcMap->nbCheckpoints; $this->currentMap->nbLaps = $rpcMap->nbLaps; - return true; + return $this->currentMap; } - $map = $this->initializeMap($rpcMap); - $this->maps[$map->uid] = $map; - $this->currentMap = $map; - return true; + + $this->currentMap = $this->initializeMap($rpcMap); + $this->maps[$this->currentMap->uid] = $this->currentMap; + return $this->currentMap; } /** @@ -429,26 +431,73 @@ class MapManager implements CallbackListener { * @param array $callback */ public function handleBeginMap(array $callback) { - if (!isset($callback[1][0]["UId"])) { //TODO why this can happen? + if ($this->mapBegan) { + return; + } + $this->mapBegan = true; + $this->mapEnded = false; + + if (!isset($callback[1][0]["UId"])) { + // TODO: why can this even happen? return; } if (array_key_exists($callback[1][0]["UId"], $this->maps)) { // Map already exists, only update index $this->currentMap = $this->maps[$callback[1][0]["UId"]]; } else { - // can this ever happen? + // TODO: can this ever happen? $this->fetchCurrentMap(); } - - //Restructure MapList if id is over 15 + + // Restructure MapList if id is over 15 $this->restructureMapList(); - - //Update the mx of the map (for update checks, etc.) + + // Update the mx of the map (for update checks, etc.) $this->mxManager->fetchManiaExchangeMapInformations($this->currentMap); - + // Trigger own BeginMap callback $this->maniaControl->callbackManager->triggerCallback(self::CB_BEGINMAP, $this->currentMap); + } + + /** + * Handle Script BeginMap callback + * + * @param array $callback + */ + public function handleScriptBeginMap(array $callback) { + // ignored + } + + /** + * Handle EndMap Callback + * + * @param array $callback + */ + public function handleEndMap(array $callback) { + if ($this->mapEnded) { + return; + } + $this->mapEnded = true; + $this->mapBegan = false; + // Trigger own EndMap callback + $this->maniaControl->callbackManager->triggerCallback(self::CB_ENDMAP, $this->currentMap); + } + + /** + * Handle Script EndMap Callback + * + * @param array $callback + */ + public function handleScriptEndMap(array $callback) { + if ($this->mapEnded) { + return; + } + $this->mapEnded = true; + $this->mapBegan = false; + + // Trigger own EndMap callback + $this->maniaControl->callbackManager->triggerCallback(self::CB_ENDMAP, $this->currentMap); } /** @@ -461,6 +510,8 @@ class MapManager implements CallbackListener { } /** + * Get all Maps + * * @return array */ public function getMaps() { diff --git a/application/core/Maps/MapQueue.php b/application/core/Maps/MapQueue.php index 4acb2064..38edd2b0 100644 --- a/application/core/Maps/MapQueue.php +++ b/application/core/Maps/MapQueue.php @@ -42,7 +42,7 @@ class MapQueue implements CallbackListener, CommandListener { public function __construct(ManiaControl $maniaControl) { $this->maniaControl = $maniaControl; - $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_ENDMAP, $this, 'endMap'); + $this->maniaControl->callbackManager->registerCallbackListener(MapManager::CB_ENDMAP, $this, 'endMap'); // Init settings $this->maniaControl->settingManager->initSetting($this, self::SETTING_SKIP_MAP_ON_LEAVE, true); @@ -140,9 +140,9 @@ class MapQueue implements CallbackListener, CommandListener { /** * Called on endmap * - * @param array $callback + * @param Map $map */ - public function endMap(array $callback) { + public function endMap(Map $map) { $this->nextMap = null; if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_SKIP_MAP_ON_LEAVE) == TRUE) { diff --git a/application/plugins/Endurance.php b/application/plugins/Endurance.php index 4048d01f..f70bd56c 100644 --- a/application/plugins/Endurance.php +++ b/application/plugins/Endurance.php @@ -4,6 +4,7 @@ use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Maps\Map; use ManiaControl\Plugins\Plugin; +use ManiaControl\Maps\MapManager; /** * Plugin for the TM Game Mode 'Endurance' by TGYoshi @@ -46,7 +47,7 @@ class EndurancePlugin implements CallbackListener, Plugin { // Register for callbacks $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_ONINIT, $this, 'callback_OnInit'); - $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_BEGINMAP, $this, 'callback_BeginMap'); + $this->maniaControl->callbackManager->registerCallbackListener(MapManager::CB_BEGINMAP, $this, 'callback_BeginMap'); $this->maniaControl->callbackManager->registerScriptCallbackListener(self::CB_CHECKPOINT, $this, 'callback_Checkpoint'); return true; @@ -115,10 +116,10 @@ class EndurancePlugin implements CallbackListener, Plugin { /** * Handle BeginMap callback * - * @param array $callback + * @param Map $map */ - public function callback_BeginMap(array $callback) { - $this->currentMap = $this->maniaControl->mapManager->getCurrentMap(); + public function callback_BeginMap(Map $map) { + $this->currentMap = $map; $this->playerLapTimes = array(); } diff --git a/application/plugins/Karma.php b/application/plugins/Karma.php index 4992cdee..6c0c439a 100644 --- a/application/plugins/Karma.php +++ b/application/plugins/Karma.php @@ -12,6 +12,7 @@ use ManiaControl\ManiaControl; use ManiaControl\Maps\Map; use ManiaControl\Players\Player; use ManiaControl\Plugins\Plugin; +use ManiaControl\Maps\MapManager; /** * ManiaControl Karma Plugin @@ -76,7 +77,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin { // Register for callbacks $this->maniaControl->timerManager->registerTimerListening($this, 'handle1Second', 1000); - $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_BEGINMAP, $this, 'handleBeginMap'); + $this->maniaControl->callbackManager->registerCallbackListener(MapManager::CB_BEGINMAP, $this, 'handleBeginMap'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCONNECT, $this, 'handlePlayerConnect'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCHAT, $this, 'handlePlayerChat'); @@ -202,9 +203,9 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin { /** * Handle BeginMap ManiaControl callback * - * @param array $callback + * @param Map $map */ - public function handleBeginMap(array $callback) { + public function handleBeginMap(Map $map) { $this->updateManialink = true; } diff --git a/application/plugins/LocalRecords.php b/application/plugins/LocalRecords.php index 505a5c23..a0759fa4 100644 --- a/application/plugins/LocalRecords.php +++ b/application/plugins/LocalRecords.php @@ -13,6 +13,7 @@ use FML\Controls\Control; use FML\Controls\Frame; use FML\Controls\Label; use FML\Controls\Quad; +use ManiaControl\Maps\MapManager; /** * ManiaControl Local Records Plugin @@ -79,9 +80,7 @@ class LocalRecordsPlugin implements CallbackListener, TimerListener, Plugin { // Register for callbacks $this->maniaControl->timerManager->registerTimerListening($this, 'handle1Second', 1000); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_AFTERINIT, $this, 'handleAfterInit'); - $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_BEGINMAP, $this, 'handleMapBegin'); - $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_CLIENTUPDATED, $this, - 'handleClientUpdated'); + $this->maniaControl->callbackManager->registerCallbackListener(MapManager::CB_BEGINMAP, $this, 'handleMapBegin'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_TM_PLAYERFINISH, $this, 'handlePlayerFinish'); @@ -189,9 +188,9 @@ class LocalRecordsPlugin implements CallbackListener, TimerListener, Plugin { /** * Handle BeginMap callback * - * @param array $callback + * @param Map $map */ - public function handleMapBegin(array $callback) { + public function handleMapBegin(Map $map) { $this->updateManialink = true; } @@ -270,15 +269,6 @@ class LocalRecordsPlugin implements CallbackListener, TimerListener, Plugin { } } - /** - * Handle ClientUpdated callback - * - * @param array $callback - */ - public function handleClientUpdated(array $callback) { - $this->updateManialink = true; - } - /** * Build the local records manialink * diff --git a/application/plugins/ServerRanking.php b/application/plugins/ServerRanking.php index 4e6ddf37..8310079c 100644 --- a/application/plugins/ServerRanking.php +++ b/application/plugins/ServerRanking.php @@ -10,6 +10,8 @@ use ManiaControl\Plugins\Plugin; use ManiaControl\Statistics\StatisticCollector; use ManiaControl\Statistics\StatisticManager; use Maniaplanet\DedicatedServer\Structures\AbstractStructure; +use ManiaControl\Maps\MapManager; +use ManiaControl\Maps\Map; class ServerRankingPlugin implements Plugin, CallbackListener, CommandListener { /** @@ -84,7 +86,7 @@ class ServerRankingPlugin implements Plugin, CallbackListener, CommandListener { //Register CallbackListeners $this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERCONNECT, $this, 'handlePlayerConnect'); - $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_ENDMAP, $this, 'handleEndMap'); + $this->maniaControl->callbackManager->registerCallbackListener(MapManager::CB_ENDMAP, $this, 'handleEndMap'); //Register CommandListener $this->maniaControl->commandManager->registerCommandListener('rank', $this, 'command_showRank', false); @@ -285,9 +287,9 @@ class ServerRankingPlugin implements Plugin, CallbackListener, CommandListener { /** * Shows Ranks on endMap * - * @param array $callback + * @param Map $map */ - public function handleEndMap(array $callback) { + public function handleEndMap(Map $map) { $this->resetRanks(); foreach($this->maniaControl->playerManager->getPlayers() as $player) { diff --git a/application/plugins/WidgetPlugin.php b/application/plugins/WidgetPlugin.php index 6d8480f5..2e76f7b1 100644 --- a/application/plugins/WidgetPlugin.php +++ b/application/plugins/WidgetPlugin.php @@ -16,6 +16,8 @@ use ManiaControl\Manialinks\IconManager; use ManiaControl\Players\Player; use ManiaControl\Players\PlayerManager; use ManiaControl\Plugins\Plugin; +use ManiaControl\Maps\MapManager; +use ManiaControl\Maps\Map; /** * ManiaControl Widget Plugin @@ -95,8 +97,8 @@ class WidgetPlugin implements CallbackListener, TimerListener, Plugin { $this->maniaControl->manialinkManager->customUIManager->setChallengeInfoVisible(false); // Register for callbacks - $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_BEGINMAP, $this, 'handleOnBeginMap'); - $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_ENDMAP, $this, 'handleOnEndMap'); + $this->maniaControl->callbackManager->registerCallbackListener(MapManager::CB_BEGINMAP, $this, 'handleOnBeginMap'); + $this->maniaControl->callbackManager->registerCallbackListener(MapManager::CB_ENDMAP, $this, 'handleOnEndMap'); $this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERCONNECT, $this, 'handlePlayerConnect'); $this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERDISCONNECT, $this, 'handlePlayerDisconnect'); @@ -379,9 +381,9 @@ class WidgetPlugin implements CallbackListener, TimerListener, Plugin { /** * Handle on Begin Map * - * @param array $callback + * @param Map $map */ - public function handleOnBeginMap(array $callback) { + public function handleOnBeginMap(Map $map) { // Display Map Widget if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_MAP_WIDGET_ACTIVATED)) { $this->displayMapWidget(); @@ -403,9 +405,9 @@ class WidgetPlugin implements CallbackListener, TimerListener, Plugin { /** * Handle on End Map * - * @param array $callback + * @param Map $map */ - public function handleOnEndMap(array $callback) { + public function handleOnEndMap(Map $map) { // Display Map Widget if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_NEXTMAP_WIDGET_ACTIVATED)) { $this->displayNextMapWidget();