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

@ -14,36 +14,36 @@ class CallbackManager {
* Constants * Constants
*/ */
// ManiaControl callbacks // ManiaControl callbacks
const CB_MC_1_SECOND = 'ManiaControl.1Second'; const CB_MC_1_SECOND = 'ManiaControl.1Second';
const CB_MC_5_SECOND = 'ManiaControl.5Second'; const CB_MC_5_SECOND = 'ManiaControl.5Second';
const CB_MC_1_MINUTE = 'ManiaControl.1Minute'; const CB_MC_1_MINUTE = 'ManiaControl.1Minute';
const CB_MC_ONINIT = 'ManiaControl.OnInit'; const CB_MC_ONINIT = 'ManiaControl.OnInit';
const CB_MC_ONSHUTDOWN = 'ManiaControl.OnShutdown'; const CB_MC_ONSHUTDOWN = 'ManiaControl.OnShutdown';
const CB_MC_CLIENTUPDATED = 'ManiaControl.ClientUpdated'; const CB_MC_CLIENTUPDATED = 'ManiaControl.ClientUpdated';
const CB_MC_BEGINMAP = 'ManiaControl.BeginMap'; const CB_MC_BEGINMAP = 'ManiaControl.BeginMap';
const CB_MC_ENDMAP = 'ManiaControl.EndMap'; const CB_MC_ENDMAP = 'ManiaControl.EndMap';
// ManiaPlanet callbacks // ManiaPlanet callbacks
const CB_MP_SERVERSTART = 'ManiaPlanet.ServerStart'; const CB_MP_SERVERSTART = 'ManiaPlanet.ServerStart';
const CB_MP_SERVERSTOP = 'ManiaPlanet.ServerStop'; const CB_MP_SERVERSTOP = 'ManiaPlanet.ServerStop';
const CB_MP_BEGINMATCH = 'ManiaPlanet.BeginMatch'; const CB_MP_BEGINMATCH = 'ManiaPlanet.BeginMatch';
const CB_MP_ENDMATCH = 'ManiaPlanet.EndMatch'; const CB_MP_ENDMATCH = 'ManiaPlanet.EndMatch';
const CB_MP_MAPLISTMODIFIED = 'ManiaPlanet.MapListModified'; const CB_MP_MAPLISTMODIFIED = 'ManiaPlanet.MapListModified';
const CB_MP_ECHO = 'ManiaPlanet.Echo'; const CB_MP_ECHO = 'ManiaPlanet.Echo';
const CB_MP_BILLUPDATED = 'ManiaPlanet.BillUpdated'; const CB_MP_BILLUPDATED = 'ManiaPlanet.BillUpdated';
const CB_MP_PLAYERCHAT = 'ManiaPlanet.PlayerChat'; const CB_MP_PLAYERCHAT = 'ManiaPlanet.PlayerChat';
const CB_MP_PLAYERCONNECT = 'ManiaPlanet.PlayerConnect'; const CB_MP_PLAYERCONNECT = 'ManiaPlanet.PlayerConnect';
const CB_MP_PLAYERDISCONNECT = 'ManiaPlanet.PlayerDisconnect'; const CB_MP_PLAYERDISCONNECT = 'ManiaPlanet.PlayerDisconnect';
const CB_MP_PLAYERMANIALINKPAGEANSWER = 'ManiaPlanet.PlayerManialinkPageAnswer'; const CB_MP_PLAYERMANIALINKPAGEANSWER = 'ManiaPlanet.PlayerManialinkPageAnswer';
const CB_MP_PLAYERINFOCHANGED = 'ManiaPlanet.PlayerInfoChanged'; const CB_MP_PLAYERINFOCHANGED = 'ManiaPlanet.PlayerInfoChanged';
const CB_MP_PLAYERALLIESCHANGED = 'ManiaPlanet.PlayerAlliesChanged'; const CB_MP_PLAYERALLIESCHANGED = 'ManiaPlanet.PlayerAlliesChanged';
const CB_MP_VOTEUPDATED = 'ManiaPlanet.VoteUpdated'; const CB_MP_VOTEUPDATED = 'ManiaPlanet.VoteUpdated';
const CB_MP_STATUSCHANGED = 'ManiaPlanet.StatusChanged'; const CB_MP_STATUSCHANGED = 'ManiaPlanet.StatusChanged';
const CB_MP_MODESCRIPTCALLBACK = 'ManiaPlanet.ModeScriptCallback'; const CB_MP_MODESCRIPTCALLBACK = 'ManiaPlanet.ModeScriptCallback';
const CB_MP_MODESCRIPTCALLBACKARRAY = 'ManiaPlanet.ModeScriptCallbackArray'; const CB_MP_MODESCRIPTCALLBACKARRAY = 'ManiaPlanet.ModeScriptCallbackArray';
const CB_MP_TUNNELDATARECEIVED = 'ManiaPlanet.TunnelDataReceived'; const CB_MP_TUNNELDATARECEIVED = 'ManiaPlanet.TunnelDataReceived';
// TrackMania callbacks // TrackMania callbacks
const CB_TM_PLAYERCHECKPOINT = 'TrackMania.PlayerCheckpoint'; const CB_TM_PLAYERCHECKPOINT = 'TrackMania.PlayerCheckpoint';
const CB_TM_PLAYERFINISH = 'TrackMania.PlayerFinish'; const CB_TM_PLAYERFINISH = 'TrackMania.PlayerFinish';
const CB_TM_PLAYERINCOHERENCE = 'TrackMania.PlayerIncoherence'; const CB_TM_PLAYERINCOHERENCE = 'TrackMania.PlayerIncoherence';
/** /**
@ -56,6 +56,7 @@ class CallbackManager {
private $last5Second = -1; private $last5Second = -1;
private $last1Minute = -1; private $last1Minute = -1;
private $mapEnded = false; private $mapEnded = false;
private $mapBegan = false;
/** /**
* Construct a new Callbacks Manager * Construct a new Callbacks Manager
@ -64,25 +65,25 @@ class CallbackManager {
*/ */
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();
} }
/** /**
* 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("Given listener (" . get_class($listener) . ") can't handle callback '{$callbackName}' (no method '{$method}')!"); trigger_error("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)) {
$this->callbackListeners[$callbackName] = array(); $this->callbackListeners[$callbackName] = array();
} }
array_push($this->callbackListeners[$callbackName], array($listener, $method)); array_push($this->callbackListeners[$callbackName], array($listener, $method));
@ -92,17 +93,17 @@ 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("Given listener (" . get_class($listener) . ") can't handle script callback '{$callbackName}' (no method '{$method}')!"); trigger_error("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)) {
$this->scriptCallbackListener[$callbackName] = array(); $this->scriptCallbackListener[$callbackName] = array();
} }
array_push($this->scriptCallbackListener[$callbackName], array($listener, $method)); array_push($this->scriptCallbackListener[$callbackName], array($listener, $method));
@ -117,9 +118,11 @@ class CallbackManager {
*/ */
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) continue; if($listenerCallback[0] != $listener) {
continue;
}
unset($listeners[$key]); unset($listeners[$key]);
$removed = true; $removed = true;
} }
@ -135,9 +138,11 @@ class CallbackManager {
*/ */
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) continue; if($listenerCallback[0] != $listener) {
continue;
}
unset($listeners[$key]); unset($listeners[$key]);
$removed = true; $removed = true;
} }
@ -149,11 +154,13 @@ class CallbackManager {
* 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)) return; if(!array_key_exists($callbackName, $this->callbackListeners)) {
foreach ($this->callbackListeners[$callbackName] as $listener) { return;
}
foreach($this->callbackListeners[$callbackName] as $listener) {
call_user_func(array($listener[0], $listener[1]), $callback); call_user_func(array($listener[0], $listener[1]), $callback);
} }
} }
@ -162,11 +169,13 @@ class CallbackManager {
* 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)) return; if(!array_key_exists($callbackName, $this->scriptCallbackListener)) {
foreach ($this->scriptCallbackListener[$callbackName] as $listener) { return;
}
foreach($this->scriptCallbackListener[$callbackName] as $listener) {
call_user_func(array($listener[0], $listener[1]), $callback); call_user_func(array($listener[0], $listener[1]), $callback);
} }
} }
@ -179,26 +188,32 @@ class CallbackManager {
$this->manageTimedCallbacks(); $this->manageTimedCallbacks();
// Server Callbacks // Server Callbacks
if (!$this->maniaControl->client) return; if(!$this->maniaControl->client) {
return;
}
$callbacks = $this->maniaControl->client->executeCallbacks(); $callbacks = $this->maniaControl->client->executeCallbacks();
if (!is_array($callbacks)) { if(!is_array($callbacks)) {
trigger_error("Error reading server callbacks. " . $this->maniaControl->getClientErrorText()); trigger_error("Error reading server callbacks. " . $this->maniaControl->getClientErrorText());
return; return;
} }
// 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); if(!$this->mapBegan) {
$this->mapEnded = false; $this->triggerCallback(self::CB_MC_BEGINMAP, $callback);
$this->mapBegan = true;
$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;
$this->mapBegan = false;
} }
break; break;
case self::CB_MP_MODESCRIPTCALLBACK: case self::CB_MP_MODESCRIPTCALLBACK:
@ -224,27 +239,23 @@ class CallbackManager {
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 'BeginMap': case 'BeginMap':
$this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData);
$this->triggerCallback(self::CB_MC_BEGINMAP, $callback);
break;
case 'LibXmlRpc_BeginMap': case 'LibXmlRpc_BeginMap':
$this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData); $this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData);
$this->triggerCallback(self::CB_MC_BEGINMAP, $callback); if(!$this->mapBegan) {
break; $this->triggerCallback(self::CB_MC_BEGINMAP, $callback);
case 'EndMap': $this->mapBegan = true;
$this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData); $this->mapEnded = false;
if (!$this->mapEnded) {
$this->triggerCallback(self::CB_MC_ENDMAP, $callback);
$this->mapEnded = true;
} }
break; break;
case 'EndMap':
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;
$this->mapBegan = false;
} }
break; break;
default: default:
@ -258,17 +269,23 @@ class CallbackManager {
*/ */
private function manageTimedCallbacks() { private function manageTimedCallbacks() {
// 1 second // 1 second
if ($this->last1Second > time() - 1) return; if($this->last1Second > time() - 1) {
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) return; if($this->last5Second > time() - 5) {
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) return; if($this->last1Minute > time() - 60) {
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));
} }