- moved map begin+end callbacks to map manager

- callback manager cleanup
This commit is contained in:
Steffen Schröder 2014-02-19 16:27:56 +01:00
parent ff9c21a8af
commit 71ab8764df
8 changed files with 112 additions and 91 deletions

View File

@ -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();
}
/**
@ -202,27 +190,21 @@ class CallbackManager {
$callbackName = $callback[0];
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);

View File

@ -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,7 +73,6 @@ 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
@ -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,14 +431,21 @@ 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();
}
@ -448,7 +457,47 @@ class MapManager implements CallbackListener {
// 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() {

View File

@ -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) {

View File

@ -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();
}

View File

@ -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;
}

View File

@ -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
*

View File

@ -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) {

View File

@ -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();