TrackManiaControl/application/core/Callbacks/CallbackManager.php

250 lines
7.8 KiB
PHP
Raw Normal View History

2013-11-09 17:24:03 +01:00
<?php
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
*
* @author steeffeen & kremsy
2013-11-09 17:24:03 +01:00
*/
class CallbackManager {
2013-11-09 17:24:03 +01:00
/**
* Constants
*/
// ManiaControl callbacks
2014-02-19 22:22:28 +01:00
const CB_ONINIT = 'ManiaControl.OnInit';
const CB_AFTERINIT = 'ManiaControl.AfterInit';
const CB_ONSHUTDOWN = 'ManiaControl.OnShutdown';
2014-02-19 12:53:06 +01:00
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';
2014-02-19 13:37:13 +01:00
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
*/
private $maniaControl = null;
private $callbackListeners = array();
private $scriptCallbackListener = array();
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
*
2014-01-09 20:38:45 +01:00
* @param \ManiaControl\ManiaControl $maniaControl
2013-11-09 17:24:03 +01:00
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
2013-11-09 17:24:03 +01:00
}
/**
2014-01-09 20:38:45 +01:00
* Register a new Callback Listener
*
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
* @return bool
2013-11-09 17:24:03 +01:00
*/
public function registerCallbackListener($callbackName, CallbackListener $listener, $method) {
2014-01-30 22:35:32 +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}')!");
return false;
2013-11-09 17:24:03 +01:00
}
2014-01-30 22:35:32 +01:00
if (!array_key_exists($callbackName, $this->callbackListeners)) {
$this->callbackListeners[$callbackName] = array();
}
array_push($this->callbackListeners[$callbackName], array($listener, $method));
return true;
2013-11-09 17:24:03 +01:00
}
/**
2014-01-09 20:38:45 +01:00
* Register a new Script Callback Listener
*
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
* @return bool
*/
public function registerScriptCallbackListener($callbackName, CallbackListener $listener, $method) {
2014-01-30 22:35:32 +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}')!");
return false;
}
2014-01-30 22:35:32 +01:00
if (!array_key_exists($callbackName, $this->scriptCallbackListener)) {
$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) {
2014-01-30 22:35:32 +01:00
if ($listenerCallback[0] != $listener) {
2014-01-19 23:18:48 +01:00
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) {
2014-01-30 22:35:32 +01:00
if ($listenerCallback[0] != $listener) {
2014-01-19 23:18:48 +01:00
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
*
2014-01-09 20:38:45 +01:00
* @param string $callbackName
2013-11-09 17:24:03 +01:00
*/
public function triggerCallback($callbackName) {
2014-01-30 22:35:32 +01:00
if (!array_key_exists($callbackName, $this->callbackListeners)) {
2014-01-19 23:18:48 +01:00
return;
}
$params = func_get_args();
$params = array_slice($params, 1, count($params), true);
2014-02-19 22:22:28 +01:00
foreach($this->callbackListeners[$callbackName] as $listener) {
call_user_func_array(array($listener[0], $listener[1]), $params);
2013-11-09 17:24:03 +01:00
}
}
/**
2014-01-09 20:38:45 +01:00
* Trigger a specific Script Callback
*
2014-01-09 20:38:45 +01:00
* @param string $callbackName
*/
public function triggerScriptCallback($callbackName) {
2014-01-30 22:35:32 +01:00
if (!array_key_exists($callbackName, $this->scriptCallbackListener)) {
2014-01-19 23:18:48 +01:00
return;
}
$params = func_get_args();
$params = array_slice($params, 1, count($params), true);
2014-01-19 23:18:48 +01:00
foreach($this->scriptCallbackListener[$callbackName] as $listener) {
call_user_func_array(array($listener[0], $listener[1]), $params);
}
}
2014-02-19 22:22:28 +01:00
/**
2014-01-09 20:38:45 +01:00
* Trigger internal Callbacks and manage Server Callbacks
*/
public function manageCallbacks() {
2014-01-31 00:04:40 +01:00
// Manage Timings
2014-01-30 22:35:32 +01:00
$this->maniaControl->timerManager->manageTimings();
2014-01-19 23:18:48 +01:00
2014-01-09 20:38:45 +01:00
// Server Callbacks
2014-01-30 22:35:32 +01:00
if (!$this->maniaControl->client) {
2014-01-19 23:18:48 +01:00
return;
}
$callbacks = $this->maniaControl->client->executeCallbacks();
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-02-19 22:22:28 +01:00
switch($callbackName) {
case 'ManiaPlanet.BeginMatch':
2014-03-12 13:07:02 +01:00
if ($this->maniaControl->mapManager->getCurrentMap()->getGame() == 'sm') {
2014-02-19 22:22:28 +01:00
$this->triggerCallback($callbackName, $callback);
break;
}
case 'ManiaPlanet.BeginMap':
$this->maniaControl->mapManager->handleBeginMap($callback);
$this->triggerCallback($callbackName, $callback);
2014-01-09 20:38:45 +01:00
break;
2014-02-19 13:37:13 +01:00
case 'ManiaPlanet.EndMatch': //TODO temporary fix
2014-03-12 13:07:02 +01:00
if ($this->maniaControl->mapManager->getCurrentMap()->getGame() == 'sm') {
2014-02-19 22:22:28 +01:00
$this->triggerCallback($callbackName, $callback);
break;
}
case 'ManiaPlanet.EndMap':
$this->maniaControl->mapManager->handleEndMap($callback);
$this->triggerCallback($callbackName, $callback);
2014-01-09 20:38:45 +01:00
break;
case self::CB_MP_MODESCRIPTCALLBACK:
2014-01-09 20:38:45 +01:00
$this->handleScriptCallback($callback);
$this->triggerCallback($callbackName, $callback);
2014-01-09 20:38:45 +01:00
break;
case self::CB_MP_MODESCRIPTCALLBACKARRAY:
2014-01-09 20:38:45 +01:00
$this->handleScriptCallback($callback);
$this->triggerCallback($callbackName, $callback);
2014-01-09 20:38:45 +01:00
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
*/
private function handleScriptCallback(array $callback) {
$scriptCallbackData = $callback[1];
$scriptCallbackName = $scriptCallbackData[0];
2014-01-19 23:18:48 +01:00
switch($scriptCallbackName) {
case 'BeginMap':
case 'LibXmlRpc_BeginMap':
$this->maniaControl->mapManager->handleScriptBeginMap($callback);
$this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData);
2014-01-09 20:38:45 +01:00
break;
2014-01-19 23:18:48 +01:00
case 'EndMap':
case 'LibXmlRpc_EndMap':
$this->maniaControl->mapManager->handleScriptEndMap($callback);
2014-01-09 20:38:45 +01:00
$this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData);
break;
default:
2014-01-09 20:38:45 +01:00
$this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData);
break;
2013-11-09 17:24:03 +01:00
}
}
}