This commit is contained in:
kremsy 2014-01-19 23:18:48 +01:00 committed by Steffen Schröder
parent 0390b8794d
commit a34f47c082

View File

@ -56,6 +56,7 @@ class CallbackManager {
private $last5Second = -1;
private $last1Minute = -1;
private $mapEnded = false;
private $mapBegan = false;
/**
* Construct a new Callbacks Manager
@ -78,11 +79,11 @@ class CallbackManager {
* @return bool
*/
public function registerCallbackListener($callbackName, CallbackListener $listener, $method) {
if (!method_exists($listener, $method)) {
if(!method_exists($listener, $method)) {
trigger_error("Given listener (" . get_class($listener) . ") can't handle callback '{$callbackName}' (no method '{$method}')!");
return false;
}
if (!array_key_exists($callbackName, $this->callbackListeners)) {
if(!array_key_exists($callbackName, $this->callbackListeners)) {
$this->callbackListeners[$callbackName] = array();
}
array_push($this->callbackListeners[$callbackName], array($listener, $method));
@ -98,11 +99,11 @@ class CallbackManager {
* @return bool
*/
public function registerScriptCallbackListener($callbackName, CallbackListener $listener, $method) {
if (!method_exists($listener, $method)) {
if(!method_exists($listener, $method)) {
trigger_error("Given listener (" . get_class($listener) . ") can't handle script callback '{$callbackName}' (no method '{$method}')!");
return false;
}
if (!array_key_exists($callbackName, $this->scriptCallbackListener)) {
if(!array_key_exists($callbackName, $this->scriptCallbackListener)) {
$this->scriptCallbackListener[$callbackName] = array();
}
array_push($this->scriptCallbackListener[$callbackName], array($listener, $method));
@ -117,9 +118,11 @@ class CallbackManager {
*/
public function unregisterCallbackListener(CallbackListener $listener) {
$removed = false;
foreach ($this->callbackListeners as &$listeners) {
foreach ($listeners as $key => &$listenerCallback) {
if ($listenerCallback[0] != $listener) continue;
foreach($this->callbackListeners as &$listeners) {
foreach($listeners as $key => &$listenerCallback) {
if($listenerCallback[0] != $listener) {
continue;
}
unset($listeners[$key]);
$removed = true;
}
@ -135,9 +138,11 @@ class CallbackManager {
*/
public function unregisterScriptCallbackListener(CallbackListener $listener) {
$removed = false;
foreach ($this->scriptCallbackListener as &$listeners) {
foreach ($listeners as $key => &$listenerCallback) {
if ($listenerCallback[0] != $listener) continue;
foreach($this->scriptCallbackListener as &$listeners) {
foreach($listeners as $key => &$listenerCallback) {
if($listenerCallback[0] != $listener) {
continue;
}
unset($listeners[$key]);
$removed = true;
}
@ -152,8 +157,10 @@ class CallbackManager {
* @param array $callback
*/
public function triggerCallback($callbackName, array $callback) {
if (!array_key_exists($callbackName, $this->callbackListeners)) return;
foreach ($this->callbackListeners[$callbackName] as $listener) {
if(!array_key_exists($callbackName, $this->callbackListeners)) {
return;
}
foreach($this->callbackListeners[$callbackName] as $listener) {
call_user_func(array($listener[0], $listener[1]), $callback);
}
}
@ -165,8 +172,10 @@ class CallbackManager {
* @param array $callback
*/
public function triggerScriptCallback($callbackName, array $callback) {
if (!array_key_exists($callbackName, $this->scriptCallbackListener)) return;
foreach ($this->scriptCallbackListener[$callbackName] as $listener) {
if(!array_key_exists($callbackName, $this->scriptCallbackListener)) {
return;
}
foreach($this->scriptCallbackListener[$callbackName] as $listener) {
call_user_func(array($listener[0], $listener[1]), $callback);
}
}
@ -179,26 +188,32 @@ class CallbackManager {
$this->manageTimedCallbacks();
// Server Callbacks
if (!$this->maniaControl->client) return;
if(!$this->maniaControl->client) {
return;
}
$callbacks = $this->maniaControl->client->executeCallbacks();
if (!is_array($callbacks)) {
if(!is_array($callbacks)) {
trigger_error("Error reading server callbacks. " . $this->maniaControl->getClientErrorText());
return;
}
// Handle callbacks
foreach ($callbacks as $callback) {
foreach($callbacks as $callback) {
$callbackName = $callback[0];
switch ($callbackName) {
switch($callbackName) {
case 'ManiaPlanet.BeginMap':
if(!$this->mapBegan) {
$this->triggerCallback(self::CB_MC_BEGINMAP, $callback);
$this->mapBegan = true;
$this->mapEnded = false;
}
break;
case 'ManiaPlanet.EndMap':
if (!$this->mapEnded) {
if(!$this->mapEnded) {
$this->triggerCallback(self::CB_MC_ENDMAP, $callback);
$this->mapEnded = true;
$this->mapBegan = false;
}
break;
case self::CB_MP_MODESCRIPTCALLBACK:
@ -224,27 +239,23 @@ class CallbackManager {
private function handleScriptCallback(array $callback) {
$scriptCallbackData = $callback[1];
$scriptCallbackName = $scriptCallbackData[0];
switch ($scriptCallbackName) {
switch($scriptCallbackName) {
case 'BeginMap':
$this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData);
$this->triggerCallback(self::CB_MC_BEGINMAP, $callback);
break;
case 'LibXmlRpc_BeginMap':
$this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData);
if(!$this->mapBegan) {
$this->triggerCallback(self::CB_MC_BEGINMAP, $callback);
break;
case 'EndMap':
$this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData);
if (!$this->mapEnded) {
$this->triggerCallback(self::CB_MC_ENDMAP, $callback);
$this->mapEnded = true;
$this->mapBegan = true;
$this->mapEnded = false;
}
break;
case 'EndMap':
case 'LibXmlRpc_EndMap':
$this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData);
if (!$this->mapEnded) {
if(!$this->mapEnded) {
$this->triggerCallback(self::CB_MC_ENDMAP, $callback);
$this->mapEnded = true;
$this->mapBegan = false;
}
break;
default:
@ -258,17 +269,23 @@ class CallbackManager {
*/
private function manageTimedCallbacks() {
// 1 second
if ($this->last1Second > time() - 1) return;
if($this->last1Second > time() - 1) {
return;
}
$this->last1Second = time();
$this->triggerCallback(self::CB_MC_1_SECOND, array(self::CB_MC_1_SECOND));
// 5 second
if ($this->last5Second > time() - 5) return;
if($this->last5Second > time() - 5) {
return;
}
$this->last5Second = time();
$this->triggerCallback(self::CB_MC_5_SECOND, array(self::CB_MC_5_SECOND));
// 1 minute
if ($this->last1Minute > time() - 60) return;
if($this->last1Minute > time() - 60) {
return;
}
$this->last1Minute = time();
$this->triggerCallback(self::CB_MC_1_MINUTE, array(self::CB_MC_1_MINUTE));
}