improved callback manager class

This commit is contained in:
Steffen Schröder 2014-01-09 20:38:45 +01:00
parent 74ee8a5aba
commit ed2a5321c8

View File

@ -5,7 +5,7 @@ namespace ManiaControl\Callbacks;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
/** /**
* Class for managing server and controller callbacks * Class for managing Server and ManiaControl Callbacks
* *
* @author steeffeen & kremsy * @author steeffeen & kremsy
*/ */
@ -47,7 +47,7 @@ class CallbackManager {
const CB_TM_PLAYERINCOHERENCE = 'TrackMania.PlayerIncoherence'; const CB_TM_PLAYERINCOHERENCE = 'TrackMania.PlayerIncoherence';
/** /**
* Private properties * Private Properties
*/ */
private $maniaControl = null; private $maniaControl = null;
private $callbackListeners = array(); private $callbackListeners = array();
@ -56,31 +56,30 @@ class CallbackManager {
private $last5Second = -1; private $last5Second = -1;
private $last1Minute = -1; private $last1Minute = -1;
private $mapEnded = false; private $mapEnded = false;
/** /**
* Construct callbacks manager * Construct a new Callbacks Manager
* *
* @param \ManiaControl\ManiaControl $maniaControl * @param \ManiaControl\ManiaControl $maniaControl
*/ */
public function __construct(ManiaControl $maniaControl) { public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl; $this->maniaControl = $maniaControl;
$this->last1Second = time(); $this->last1Second = time();
$this->last5Second = time(); $this->last5Second = time();
$this->last1Minute = time(); $this->last1Minute = time();
$this->last3Minute = time();
} }
/** /**
* Register a new callback listener * Register a new Callback Listener
* *
* @param string $callbackName * @param string $callbackName
* @param \ManiaControl\Callbacks\CallbackListener $listener * @param \ManiaControl\Callbacks\CallbackListener $listener
* @param string $method * @param string $method
* @return bool * @return bool
*/ */
public function registerCallbackListener($callbackName, CallbackListener $listener, $method) { public function registerCallbackListener($callbackName, CallbackListener $listener, $method) {
if (!method_exists($listener, $method)) { if (!method_exists($listener, $method)) {
trigger_error( trigger_error("Given listener (" . get_class($listener) . ") can't handle callback '{$callbackName}' (no method '{$method}')!");
"Given listener (" . get_class($listener) . ") can't handle callback '{$callbackName}' (no method '{$method}')!");
return false; return false;
} }
if (!array_key_exists($callbackName, $this->callbackListeners)) { if (!array_key_exists($callbackName, $this->callbackListeners)) {
@ -91,18 +90,16 @@ class CallbackManager {
} }
/** /**
* Register a new script callback listener * Register a new Script Callback Listener
* *
* @param string $callbackName * @param string $callbackName
* @param CallbackListener $listener * @param CallbackListener $listener
* @param string $method * @param string $method
* @return bool * @return bool
*/ */
public function registerScriptCallbackListener($callbackName, CallbackListener $listener, $method) { public function registerScriptCallbackListener($callbackName, CallbackListener $listener, $method) {
if (!method_exists($listener, $method)) { if (!method_exists($listener, $method)) {
trigger_error( trigger_error("Given listener (" . get_class($listener) . ") can't handle script callback '{$callbackName}' (no method '{$method}')!");
"Given listener (" . get_class($listener) .
") can't handle script callback '{$callbackName}' (no method '{$method}')!");
return false; return false;
} }
if (!array_key_exists($callbackName, $this->scriptCallbackListener)) { if (!array_key_exists($callbackName, $this->scriptCallbackListener)) {
@ -115,17 +112,16 @@ class CallbackManager {
/** /**
* Remove a Callback Listener * Remove a Callback Listener
* *
* @param CallbackListener $listener * @param CallbackListener $listener
* @return bool * @return bool
*/ */
public function unregisterCallbackListener(CallbackListener $listener) { public function unregisterCallbackListener(CallbackListener $listener) {
$removed = false; $removed = false;
foreach ($this->callbackListeners as &$listeners) { foreach ($this->callbackListeners as &$listeners) {
foreach ($listeners as $key => &$listenerCallback) { foreach ($listeners as $key => &$listenerCallback) {
if ($listenerCallback[0] == $listener) { if ($listenerCallback[0] != $listener) continue;
unset($listeners[$key]); unset($listeners[$key]);
$removed = true; $removed = true;
}
} }
} }
return $removed; return $removed;
@ -134,62 +130,56 @@ class CallbackManager {
/** /**
* Remove a Script Callback Listener * Remove a Script Callback Listener
* *
* @param CallbackListener $listener * @param CallbackListener $listener
* @return bool * @return bool
*/ */
public function unregisterScriptCallbackListener(CallbackListener $listener) { public function unregisterScriptCallbackListener(CallbackListener $listener) {
$removed = false; $removed = false;
foreach ($this->scriptCallbackListener as &$listeners) { foreach ($this->scriptCallbackListener as &$listeners) {
foreach ($listeners as $key => &$listenerCallback) { foreach ($listeners as $key => &$listenerCallback) {
if ($listenerCallback[0] == $listener) { if ($listenerCallback[0] != $listener) continue;
unset($listeners[$key]); unset($listeners[$key]);
$removed = true; $removed = true;
}
} }
} }
return $removed; return $removed;
} }
/** /**
* Trigger a specific callback * Trigger a specific Callback
* *
* @param string $callbackName * @param string $callbackName
* @param array $callback * @param array $callback
*/ */
public function triggerCallback($callbackName, array $callback) { public function triggerCallback($callbackName, array $callback) {
if (!array_key_exists($callbackName, $this->callbackListeners)) { if (!array_key_exists($callbackName, $this->callbackListeners)) return;
return;
}
foreach ($this->callbackListeners[$callbackName] as $listener) { foreach ($this->callbackListeners[$callbackName] as $listener) {
call_user_func(array($listener[0], $listener[1]), $callback); call_user_func(array($listener[0], $listener[1]), $callback);
} }
} }
/** /**
* Trigger a specific script callback * Trigger a specific Script Callback
* *
* @param string $callbackName * @param string $callbackName
* @param array $callback * @param array $callback
*/ */
public function triggerScriptCallback($callbackName, array $callback) { public function triggerScriptCallback($callbackName, array $callback) {
if (!array_key_exists($callbackName, $this->scriptCallbackListener)) { if (!array_key_exists($callbackName, $this->scriptCallbackListener)) return;
return;
}
foreach ($this->scriptCallbackListener[$callbackName] as $listener) { foreach ($this->scriptCallbackListener[$callbackName] as $listener) {
call_user_func(array($listener[0], $listener[1]), $callback); call_user_func(array($listener[0], $listener[1]), $callback);
} }
} }
/** /**
* Trigger internal and manage server callbacks * Trigger internal Callbacks and manage Server Callbacks
*/ */
public function manageCallbacks() { public function manageCallbacks() {
// Timed callbacks
$this->manageTimedCallbacks(); $this->manageTimedCallbacks();
// Get server callbacks // Server Callbacks
if (!$this->maniaControl->client) { if (!$this->maniaControl->client) return;
return;
}
$this->maniaControl->client->readCB(); $this->maniaControl->client->readCB();
$callbacks = $this->maniaControl->client->getCBResponses(); $callbacks = $this->maniaControl->client->getCBResponses();
if (!is_array($callbacks)) { if (!is_array($callbacks)) {
@ -199,101 +189,78 @@ class CallbackManager {
// Handle callbacks // Handle callbacks
foreach ($callbacks as $callback) { foreach ($callbacks as $callback) {
$callbackName = $callback[0]; $callbackName = $callback[0];
switch ($callbackName) { switch ($callbackName) {
case 'ManiaPlanet.BeginMap': case 'ManiaPlanet.BeginMap':
{ $this->triggerCallback(self::CB_MC_BEGINMAP, $callback);
$this->triggerCallback(self::CB_MC_BEGINMAP, $callback); $this->mapEnded = false;
$this->mapEnded = false; break;
break;
}
case 'ManiaPlanet.EndMap': case 'ManiaPlanet.EndMap':
{ if (!$this->mapEnded) {
if(!$this->mapEnded){ $this->triggerCallback(self::CB_MC_ENDMAP, $callback);
$this->triggerCallback(self::CB_MC_ENDMAP, $callback); $this->mapEnded = true;
$this->mapEnded = true;
}
break;
} }
break;
case self::CB_MP_MODESCRIPTCALLBACK: case self::CB_MP_MODESCRIPTCALLBACK:
{ $this->handleScriptCallback($callback);
$this->handleScriptCallback($callback); $this->triggerCallback(self::CB_MP_MODESCRIPTCALLBACK, $callback);
$this->triggerCallback(self::CB_MP_MODESCRIPTCALLBACK, $callback); break;
break;
}
case self::CB_MP_MODESCRIPTCALLBACKARRAY: case self::CB_MP_MODESCRIPTCALLBACKARRAY:
{ $this->handleScriptCallback($callback);
$this->handleScriptCallback($callback); $this->triggerCallback(self::CB_MP_MODESCRIPTCALLBACKARRAY, $callback);
$this->triggerCallback(self::CB_MP_MODESCRIPTCALLBACKARRAY, $callback); break;
break;
}
default: default:
{ $this->triggerCallback($callbackName, $callback);
$this->triggerCallback($callbackName, $callback); break;
break;
}
} }
} }
} }
/** /**
* Handle the given script callback * Handle the given Script Callback
* *
* @param array $callback * @param array $callback
*/ */
private function handleScriptCallback(array $callback) { private function handleScriptCallback(array $callback) {
$scriptCallbackData = $callback[1]; $scriptCallbackData = $callback[1];
$scriptCallbackName = $scriptCallbackData[0]; $scriptCallbackName = $scriptCallbackData[0];
switch ($scriptCallbackName) { switch ($scriptCallbackName) {
case 'EndMap': case 'EndMap':
{ $this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData);
$this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData); if (!$this->mapEnded) {
if(!$this->mapEnded){ $this->triggerCallback(self::CB_MC_ENDMAP, $callback);
$this->triggerCallback(self::CB_MC_ENDMAP, $callback); $this->mapEnded = true;
$this->mapEnded = true;
}
break;
} }
break;
case 'LibXmlRpc_EndMap': case 'LibXmlRpc_EndMap':
{ $this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData);
$this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData); if (!$this->mapEnded) {
if(!$this->mapEnded){ $this->triggerCallback(self::CB_MC_ENDMAP, $callback);
$this->triggerCallback(self::CB_MC_ENDMAP, $callback); $this->mapEnded = true;
$this->mapEnded = true;
}
break;
} }
break;
default: default:
{ $this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData);
$this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData); break;
break;
}
} }
} }
/** /**
* Manage recurring timed callbacks * Manage recurring timed Callbacks
*/ */
private function manageTimedCallbacks() { private function manageTimedCallbacks() {
// 1 second // 1 second
if ($this->last1Second > time() - 1) { if ($this->last1Second > time() - 1) return;
return;
}
$this->last1Second = time(); $this->last1Second = time();
$this->triggerCallback(self::CB_MC_1_SECOND, array(self::CB_MC_1_SECOND)); $this->triggerCallback(self::CB_MC_1_SECOND, array(self::CB_MC_1_SECOND));
// 5 second // 5 second
if ($this->last5Second > time() - 5) { if ($this->last5Second > time() - 5) return;
return;
}
$this->last5Second = time(); $this->last5Second = time();
$this->triggerCallback(self::CB_MC_5_SECOND, array(self::CB_MC_5_SECOND)); $this->triggerCallback(self::CB_MC_5_SECOND, array(self::CB_MC_5_SECOND));
// 1 minute // 1 minute
if ($this->last1Minute > time() - 60) { if ($this->last1Minute > time() - 60) return;
return;
}
$this->last1Minute = time(); $this->last1Minute = time();
$this->triggerCallback(self::CB_MC_1_MINUTE, array(self::CB_MC_1_MINUTE)); $this->triggerCallback(self::CB_MC_1_MINUTE, array(self::CB_MC_1_MINUTE));
} }