2013-11-09 17:24:03 +01:00
|
|
|
<?php
|
|
|
|
|
2013-11-12 15:48:25 +01:00
|
|
|
namespace ManiaControl\Callbacks;
|
|
|
|
|
|
|
|
use ManiaControl\ManiaControl;
|
2013-11-09 17:24:03 +01:00
|
|
|
|
|
|
|
/**
|
2014-01-09 20:38:45 +01:00
|
|
|
* Class for managing Server and ManiaControl Callbacks
|
2013-11-09 17:24:03 +01:00
|
|
|
*
|
2013-11-12 15:48:25 +01:00
|
|
|
* @author steeffeen & kremsy
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2013-11-12 15:48:25 +01:00
|
|
|
class CallbackManager {
|
2013-11-09 17:24:03 +01:00
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
*/
|
|
|
|
// ManiaControl callbacks
|
2014-01-19 23:18:48 +01:00
|
|
|
const CB_MC_1_SECOND = 'ManiaControl.1Second';
|
|
|
|
const CB_MC_5_SECOND = 'ManiaControl.5Second';
|
|
|
|
const CB_MC_1_MINUTE = 'ManiaControl.1Minute';
|
|
|
|
const CB_MC_ONINIT = 'ManiaControl.OnInit';
|
|
|
|
const CB_MC_ONSHUTDOWN = 'ManiaControl.OnShutdown';
|
2013-11-10 02:55:08 +01:00
|
|
|
const CB_MC_CLIENTUPDATED = 'ManiaControl.ClientUpdated';
|
2014-01-19 23:18:48 +01:00
|
|
|
const CB_MC_BEGINMAP = 'ManiaControl.BeginMap';
|
|
|
|
const CB_MC_ENDMAP = 'ManiaControl.EndMap';
|
2013-11-09 17:24:03 +01:00
|
|
|
// ManiaPlanet callbacks
|
2014-01-19 23:18:48 +01:00
|
|
|
const CB_MP_SERVERSTART = 'ManiaPlanet.ServerStart';
|
|
|
|
const CB_MP_SERVERSTOP = 'ManiaPlanet.ServerStop';
|
|
|
|
const CB_MP_BEGINMATCH = 'ManiaPlanet.BeginMatch';
|
|
|
|
const CB_MP_ENDMATCH = 'ManiaPlanet.EndMatch';
|
|
|
|
const CB_MP_MAPLISTMODIFIED = 'ManiaPlanet.MapListModified';
|
|
|
|
const CB_MP_ECHO = 'ManiaPlanet.Echo';
|
|
|
|
const CB_MP_BILLUPDATED = 'ManiaPlanet.BillUpdated';
|
|
|
|
const CB_MP_PLAYERCHAT = 'ManiaPlanet.PlayerChat';
|
|
|
|
const CB_MP_PLAYERCONNECT = 'ManiaPlanet.PlayerConnect';
|
|
|
|
const CB_MP_PLAYERDISCONNECT = 'ManiaPlanet.PlayerDisconnect';
|
2013-11-09 17:24:03 +01:00
|
|
|
const CB_MP_PLAYERMANIALINKPAGEANSWER = 'ManiaPlanet.PlayerManialinkPageAnswer';
|
2014-01-19 23:18:48 +01:00
|
|
|
const CB_MP_PLAYERINFOCHANGED = 'ManiaPlanet.PlayerInfoChanged';
|
|
|
|
const CB_MP_PLAYERALLIESCHANGED = 'ManiaPlanet.PlayerAlliesChanged';
|
|
|
|
const CB_MP_VOTEUPDATED = 'ManiaPlanet.VoteUpdated';
|
|
|
|
const CB_MP_STATUSCHANGED = 'ManiaPlanet.StatusChanged';
|
|
|
|
const CB_MP_MODESCRIPTCALLBACK = 'ManiaPlanet.ModeScriptCallback';
|
|
|
|
const CB_MP_MODESCRIPTCALLBACKARRAY = 'ManiaPlanet.ModeScriptCallbackArray';
|
|
|
|
const CB_MP_TUNNELDATARECEIVED = 'ManiaPlanet.TunnelDataReceived';
|
2013-11-09 17:24:03 +01:00
|
|
|
// TrackMania callbacks
|
2014-01-19 23:18:48 +01:00
|
|
|
const CB_TM_PLAYERCHECKPOINT = 'TrackMania.PlayerCheckpoint';
|
|
|
|
const CB_TM_PLAYERFINISH = 'TrackMania.PlayerFinish';
|
2013-11-09 17:24:03 +01:00
|
|
|
const CB_TM_PLAYERINCOHERENCE = 'TrackMania.PlayerIncoherence';
|
2014-01-19 23:18:48 +01:00
|
|
|
|
2013-11-09 17:24:03 +01:00
|
|
|
/**
|
2014-01-09 20:38:45 +01:00
|
|
|
* Private Properties
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2013-11-10 02:55:08 +01:00
|
|
|
private $maniaControl = null;
|
2013-11-12 15:48:25 +01:00
|
|
|
private $callbackListeners = array();
|
2013-11-25 19:46:29 +01:00
|
|
|
private $scriptCallbackListener = array();
|
2013-11-09 17:24:03 +01:00
|
|
|
private $last1Second = -1;
|
|
|
|
private $last5Second = -1;
|
|
|
|
private $last1Minute = -1;
|
2013-12-28 22:43:57 +01:00
|
|
|
private $mapEnded = false;
|
2014-01-19 23:18:48 +01:00
|
|
|
private $mapBegan = false;
|
2014-01-09 20:38:45 +01:00
|
|
|
|
2013-11-09 17:24:03 +01:00
|
|
|
/**
|
2014-01-09 20:38:45 +01:00
|
|
|
* Construct a new Callbacks Manager
|
2013-11-10 02:55:08 +01:00
|
|
|
*
|
2014-01-09 20:38:45 +01:00
|
|
|
* @param \ManiaControl\ManiaControl $maniaControl
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2013-11-10 02:55:08 +01:00
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
|
|
|
$this->maniaControl = $maniaControl;
|
2014-01-19 23:18:48 +01:00
|
|
|
$this->last1Second = time();
|
|
|
|
$this->last5Second = time();
|
|
|
|
$this->last1Minute = time();
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-01-09 20:38:45 +01:00
|
|
|
* Register a new Callback Listener
|
2013-11-12 15:48:25 +01:00
|
|
|
*
|
2014-01-19 23:18:48 +01:00
|
|
|
* @param string $callbackName
|
2014-01-09 20:38:45 +01:00
|
|
|
* @param \ManiaControl\Callbacks\CallbackListener $listener
|
2014-01-19 23:18:48 +01:00
|
|
|
* @param string $method
|
2013-11-12 15:48:25 +01:00
|
|
|
* @return bool
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2013-11-12 15:48:25 +01:00
|
|
|
public function registerCallbackListener($callbackName, CallbackListener $listener, $method) {
|
2014-01-19 23:18:48 +01:00
|
|
|
if(!method_exists($listener, $method)) {
|
2014-01-09 20:38:45 +01:00
|
|
|
trigger_error("Given listener (" . get_class($listener) . ") can't handle callback '{$callbackName}' (no method '{$method}')!");
|
2013-11-12 15:48:25 +01:00
|
|
|
return false;
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
2014-01-19 23:18:48 +01:00
|
|
|
if(!array_key_exists($callbackName, $this->callbackListeners)) {
|
2013-11-12 15:48:25 +01:00
|
|
|
$this->callbackListeners[$callbackName] = array();
|
|
|
|
}
|
|
|
|
array_push($this->callbackListeners[$callbackName], array($listener, $method));
|
|
|
|
return true;
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
|
2013-11-25 19:46:29 +01:00
|
|
|
/**
|
2014-01-09 20:38:45 +01:00
|
|
|
* Register a new Script Callback Listener
|
2013-11-25 19:46:29 +01:00
|
|
|
*
|
2014-01-19 23:18:48 +01:00
|
|
|
* @param string $callbackName
|
2014-01-09 20:38:45 +01:00
|
|
|
* @param CallbackListener $listener
|
2014-01-19 23:18:48 +01:00
|
|
|
* @param string $method
|
2013-11-25 19:46:29 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function registerScriptCallbackListener($callbackName, CallbackListener $listener, $method) {
|
2014-01-19 23:18:48 +01:00
|
|
|
if(!method_exists($listener, $method)) {
|
2014-01-09 20:38:45 +01:00
|
|
|
trigger_error("Given listener (" . get_class($listener) . ") can't handle script callback '{$callbackName}' (no method '{$method}')!");
|
2013-11-25 19:46:29 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-01-19 23:18:48 +01:00
|
|
|
if(!array_key_exists($callbackName, $this->scriptCallbackListener)) {
|
2013-11-25 19:46:29 +01:00
|
|
|
$this->scriptCallbackListener[$callbackName] = array();
|
|
|
|
}
|
|
|
|
array_push($this->scriptCallbackListener[$callbackName], array($listener, $method));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-14 23:27:15 +01:00
|
|
|
/**
|
|
|
|
* Remove a Callback Listener
|
|
|
|
*
|
2014-01-09 20:38:45 +01:00
|
|
|
* @param CallbackListener $listener
|
2013-12-14 23:27:15 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function unregisterCallbackListener(CallbackListener $listener) {
|
|
|
|
$removed = false;
|
2014-01-19 23:18:48 +01:00
|
|
|
foreach($this->callbackListeners as &$listeners) {
|
|
|
|
foreach($listeners as $key => &$listenerCallback) {
|
|
|
|
if($listenerCallback[0] != $listener) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-01-09 20:38:45 +01:00
|
|
|
unset($listeners[$key]);
|
|
|
|
$removed = true;
|
2013-12-14 23:27:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $removed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a Script Callback Listener
|
|
|
|
*
|
2014-01-09 20:38:45 +01:00
|
|
|
* @param CallbackListener $listener
|
2013-12-14 23:27:15 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function unregisterScriptCallbackListener(CallbackListener $listener) {
|
|
|
|
$removed = false;
|
2014-01-19 23:18:48 +01:00
|
|
|
foreach($this->scriptCallbackListener as &$listeners) {
|
|
|
|
foreach($listeners as $key => &$listenerCallback) {
|
|
|
|
if($listenerCallback[0] != $listener) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-01-09 20:38:45 +01:00
|
|
|
unset($listeners[$key]);
|
|
|
|
$removed = true;
|
2013-12-14 23:27:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $removed;
|
|
|
|
}
|
|
|
|
|
2013-11-09 17:24:03 +01:00
|
|
|
/**
|
2014-01-09 20:38:45 +01:00
|
|
|
* Trigger a specific Callback
|
2013-11-12 15:48:25 +01:00
|
|
|
*
|
2014-01-09 20:38:45 +01:00
|
|
|
* @param string $callbackName
|
2014-01-19 23:18:48 +01:00
|
|
|
* @param array $callback
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2013-11-12 15:48:25 +01:00
|
|
|
public function triggerCallback($callbackName, array $callback) {
|
2014-01-19 23:18:48 +01:00
|
|
|
if(!array_key_exists($callbackName, $this->callbackListeners)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
foreach($this->callbackListeners[$callbackName] as $listener) {
|
2013-11-12 15:48:25 +01:00
|
|
|
call_user_func(array($listener[0], $listener[1]), $callback);
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
2013-11-12 15:48:25 +01:00
|
|
|
}
|
|
|
|
|
2013-11-25 19:46:29 +01:00
|
|
|
/**
|
2014-01-09 20:38:45 +01:00
|
|
|
* Trigger a specific Script Callback
|
2013-11-25 19:46:29 +01:00
|
|
|
*
|
2014-01-09 20:38:45 +01:00
|
|
|
* @param string $callbackName
|
2014-01-19 23:18:48 +01:00
|
|
|
* @param array $callback
|
2013-11-25 19:46:29 +01:00
|
|
|
*/
|
|
|
|
public function triggerScriptCallback($callbackName, array $callback) {
|
2014-01-19 23:18:48 +01:00
|
|
|
if(!array_key_exists($callbackName, $this->scriptCallbackListener)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
foreach($this->scriptCallbackListener[$callbackName] as $listener) {
|
2013-11-25 19:46:29 +01:00
|
|
|
call_user_func(array($listener[0], $listener[1]), $callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-12 15:48:25 +01:00
|
|
|
/**
|
2014-01-09 20:38:45 +01:00
|
|
|
* Trigger internal Callbacks and manage Server Callbacks
|
2013-11-12 15:48:25 +01:00
|
|
|
*/
|
|
|
|
public function manageCallbacks() {
|
2014-01-09 20:38:45 +01:00
|
|
|
// Timed callbacks
|
2013-11-12 15:48:25 +01:00
|
|
|
$this->manageTimedCallbacks();
|
2014-01-19 23:18:48 +01:00
|
|
|
|
2014-01-09 20:38:45 +01:00
|
|
|
// Server Callbacks
|
2014-01-19 23:18:48 +01:00
|
|
|
if(!$this->maniaControl->client) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-16 18:49:52 +01:00
|
|
|
$callbacks = $this->maniaControl->client->executeCallbacks();
|
2014-01-19 23:18:48 +01:00
|
|
|
if(!is_array($callbacks)) {
|
2013-11-10 02:55:08 +01:00
|
|
|
trigger_error("Error reading server callbacks. " . $this->maniaControl->getClientErrorText());
|
2013-11-09 17:24:03 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-01-19 23:18:48 +01:00
|
|
|
|
2013-11-09 17:24:03 +01:00
|
|
|
// Handle callbacks
|
2014-01-19 23:18:48 +01:00
|
|
|
foreach($callbacks as $callback) {
|
2013-11-09 17:24:03 +01:00
|
|
|
$callbackName = $callback[0];
|
2014-01-19 23:18:48 +01:00
|
|
|
switch($callbackName) {
|
2013-11-12 15:48:25 +01:00
|
|
|
case 'ManiaPlanet.BeginMap':
|
2014-01-19 23:18:48 +01:00
|
|
|
if(!$this->mapBegan) {
|
|
|
|
$this->triggerCallback(self::CB_MC_BEGINMAP, $callback);
|
|
|
|
$this->mapBegan = true;
|
|
|
|
$this->mapEnded = false;
|
|
|
|
}
|
2014-01-09 20:38:45 +01:00
|
|
|
break;
|
2013-11-12 15:48:25 +01:00
|
|
|
case 'ManiaPlanet.EndMap':
|
2014-01-19 23:18:48 +01:00
|
|
|
if(!$this->mapEnded) {
|
2014-01-09 20:38:45 +01:00
|
|
|
$this->triggerCallback(self::CB_MC_ENDMAP, $callback);
|
|
|
|
$this->mapEnded = true;
|
2014-01-19 23:18:48 +01:00
|
|
|
$this->mapBegan = false;
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
2014-01-09 20:38:45 +01:00
|
|
|
break;
|
2013-11-12 15:48:25 +01:00
|
|
|
case self::CB_MP_MODESCRIPTCALLBACK:
|
2014-01-09 20:38:45 +01:00
|
|
|
$this->handleScriptCallback($callback);
|
|
|
|
$this->triggerCallback(self::CB_MP_MODESCRIPTCALLBACK, $callback);
|
|
|
|
break;
|
2013-11-28 02:08:01 +01:00
|
|
|
case self::CB_MP_MODESCRIPTCALLBACKARRAY:
|
2014-01-09 20:38:45 +01:00
|
|
|
$this->handleScriptCallback($callback);
|
|
|
|
$this->triggerCallback(self::CB_MP_MODESCRIPTCALLBACKARRAY, $callback);
|
|
|
|
break;
|
2013-11-09 17:24:03 +01:00
|
|
|
default:
|
2014-01-09 20:38:45 +01:00
|
|
|
$this->triggerCallback($callbackName, $callback);
|
|
|
|
break;
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-01-09 20:38:45 +01:00
|
|
|
* Handle the given Script Callback
|
2013-11-09 17:24:03 +01:00
|
|
|
*
|
2014-01-09 20:38:45 +01:00
|
|
|
* @param array $callback
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2013-11-12 15:48:25 +01:00
|
|
|
private function handleScriptCallback(array $callback) {
|
2013-11-25 19:46:29 +01:00
|
|
|
$scriptCallbackData = $callback[1];
|
|
|
|
$scriptCallbackName = $scriptCallbackData[0];
|
2014-01-19 23:18:48 +01:00
|
|
|
switch($scriptCallbackName) {
|
2014-01-19 22:48:33 +01:00
|
|
|
case 'BeginMap':
|
|
|
|
case 'LibXmlRpc_BeginMap':
|
|
|
|
$this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData);
|
2014-01-19 23:18:48 +01:00
|
|
|
if(!$this->mapBegan) {
|
|
|
|
$this->triggerCallback(self::CB_MC_BEGINMAP, $callback);
|
|
|
|
$this->mapBegan = true;
|
|
|
|
$this->mapEnded = false;
|
2013-11-12 15:48:25 +01:00
|
|
|
}
|
2014-01-09 20:38:45 +01:00
|
|
|
break;
|
2014-01-19 23:18:48 +01:00
|
|
|
case 'EndMap':
|
2013-11-12 15:48:25 +01:00
|
|
|
case 'LibXmlRpc_EndMap':
|
2014-01-09 20:38:45 +01:00
|
|
|
$this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData);
|
2014-01-19 23:18:48 +01:00
|
|
|
if(!$this->mapEnded) {
|
2014-01-09 20:38:45 +01:00
|
|
|
$this->triggerCallback(self::CB_MC_ENDMAP, $callback);
|
|
|
|
$this->mapEnded = true;
|
2014-01-19 23:18:48 +01:00
|
|
|
$this->mapBegan = false;
|
2013-11-12 15:48:25 +01:00
|
|
|
}
|
2014-01-09 20:38:45 +01:00
|
|
|
break;
|
2013-11-12 15:48:25 +01:00
|
|
|
default:
|
2014-01-09 20:38:45 +01:00
|
|
|
$this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData);
|
|
|
|
break;
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-01-09 20:38:45 +01:00
|
|
|
* Manage recurring timed Callbacks
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2013-11-12 15:48:25 +01:00
|
|
|
private function manageTimedCallbacks() {
|
|
|
|
// 1 second
|
2014-01-19 23:18:48 +01:00
|
|
|
if($this->last1Second > time() - 1) {
|
|
|
|
return;
|
|
|
|
}
|
2013-11-12 15:48:25 +01:00
|
|
|
$this->last1Second = time();
|
|
|
|
$this->triggerCallback(self::CB_MC_1_SECOND, array(self::CB_MC_1_SECOND));
|
2014-01-19 23:18:48 +01:00
|
|
|
|
2013-11-12 15:48:25 +01:00
|
|
|
// 5 second
|
2014-01-19 23:18:48 +01:00
|
|
|
if($this->last5Second > time() - 5) {
|
|
|
|
return;
|
|
|
|
}
|
2013-11-12 15:48:25 +01:00
|
|
|
$this->last5Second = time();
|
|
|
|
$this->triggerCallback(self::CB_MC_5_SECOND, array(self::CB_MC_5_SECOND));
|
2014-01-19 23:18:48 +01:00
|
|
|
|
2013-11-12 15:48:25 +01:00
|
|
|
// 1 minute
|
2014-01-19 23:18:48 +01:00
|
|
|
if($this->last1Minute > time() - 60) {
|
|
|
|
return;
|
|
|
|
}
|
2013-11-12 15:48:25 +01:00
|
|
|
$this->last1Minute = time();
|
|
|
|
$this->triggerCallback(self::CB_MC_1_MINUTE, array(self::CB_MC_1_MINUTE));
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
}
|