Base Listening Class for Callback Management
This commit is contained in:
parent
09f61f2ad4
commit
09f11afd6e
@ -56,38 +56,53 @@ class CallbackManager {
|
||||
* Private Properties
|
||||
*/
|
||||
private $maniaControl = null;
|
||||
private $callbackListeners = array();
|
||||
private $scriptCallbackListener = array();
|
||||
private $callbackListenings = array();
|
||||
private $scriptCallbackListenings = array();
|
||||
|
||||
/**
|
||||
* Construct a new Callbacks Manager
|
||||
*
|
||||
* @param \ManiaControl\ManiaControl $maniaControl
|
||||
* @param ManiaControl $maniaControl
|
||||
*/
|
||||
public function __construct(ManiaControl $maniaControl) {
|
||||
$this->maniaControl = $maniaControl;
|
||||
|
||||
$this->shootManiaCallbacks = new ShootManiaCallbacks($maniaControl, $this);
|
||||
$this->libXmlRpcCallbacks = new LibXmlRpcCallbackManager($maniaControl, $this);
|
||||
$this->libXmlRpcCallbacks = new LibXmlRpcCallbacks($maniaControl, $this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a new Callback Listener
|
||||
*
|
||||
* @param string $callbackName
|
||||
* @param \ManiaControl\Callbacks\CallbackListener $listener
|
||||
* @param string $method
|
||||
* @param string $callbackName
|
||||
* @param CallbackListener $listener
|
||||
* @param string $method
|
||||
* @return bool
|
||||
*/
|
||||
public function registerCallbackListener($callbackName, CallbackListener $listener, $method) {
|
||||
if (!method_exists($listener, $method)) {
|
||||
trigger_error("Given listener (" . get_class($listener) . ") can't handle callback '{$callbackName}' (no method '{$method}')!");
|
||||
if (is_array($callbackName)) {
|
||||
$success = true;
|
||||
foreach ($callbackName as $callback) {
|
||||
if (!$this->registerCallbackListener($callback, $listener, $method)) {
|
||||
$success = false;
|
||||
}
|
||||
}
|
||||
return $success;
|
||||
}
|
||||
|
||||
if (!Listening::checkValidCallback($listener, $method)) {
|
||||
$listenerClass = get_class($listener);
|
||||
trigger_error("Given Listener '{$listenerClass}' can't handle Callback '{$callbackName}': No callable Method '{$method}'!");
|
||||
return false;
|
||||
}
|
||||
if (!array_key_exists($callbackName, $this->callbackListeners)) {
|
||||
$this->callbackListeners[$callbackName] = array();
|
||||
|
||||
if (!array_key_exists($callbackName, $this->callbackListenings)) {
|
||||
$this->callbackListenings[$callbackName] = array();
|
||||
}
|
||||
array_push($this->callbackListeners[$callbackName], array($listener, $method));
|
||||
|
||||
$listening = new Listening($listener, $method);
|
||||
array_push($this->callbackListenings[$callbackName], $listening);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -100,29 +115,56 @@ class CallbackManager {
|
||||
* @return bool
|
||||
*/
|
||||
public function registerScriptCallbackListener($callbackName, CallbackListener $listener, $method) {
|
||||
if (!method_exists($listener, $method)) {
|
||||
trigger_error("Given listener (" . get_class($listener) . ") can't handle script callback '{$callbackName}' (no method '{$method}')!");
|
||||
if (is_array($callbackName)) {
|
||||
$success = true;
|
||||
foreach ($callbackName as $callback) {
|
||||
if (!$this->registerScriptCallbackListener($callback, $listener, $method)) {
|
||||
$success = false;
|
||||
}
|
||||
}
|
||||
return $success;
|
||||
}
|
||||
|
||||
if (!Listening::checkValidCallback($listener, $method)) {
|
||||
$listenerClass = get_class($listener);
|
||||
trigger_error("Given Listener '{$listenerClass}' can't handle Script Callback '{$callbackName}': No callable Method '{$method}'!");
|
||||
return false;
|
||||
}
|
||||
if (!array_key_exists($callbackName, $this->scriptCallbackListener)) {
|
||||
$this->scriptCallbackListener[$callbackName] = array();
|
||||
|
||||
if (!array_key_exists($callbackName, $this->scriptCallbackListenings)) {
|
||||
$this->scriptCallbackListenings[$callbackName] = array();
|
||||
}
|
||||
array_push($this->scriptCallbackListener[$callbackName], array($listener, $method));
|
||||
|
||||
$listening = new Listening($listener, $method);
|
||||
array_push($this->scriptCallbackListenings[$callbackName], $listening);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a Callback Listener
|
||||
* Unregister a Callback Listener
|
||||
*
|
||||
* @param CallbackListener $listener
|
||||
* @return bool
|
||||
*/
|
||||
public function unregisterCallbackListener(CallbackListener $listener) {
|
||||
return $this->removeCallbackListener($this->callbackListenings, $listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the Callback Listener from the given Listeners Array
|
||||
*
|
||||
* @param array $listeningsArray
|
||||
* @param CallbackListener $listener
|
||||
* @return bool
|
||||
*/
|
||||
private function removeCallbackListener(array &$listeningsArray, CallbackListener $listener) {
|
||||
$removed = false;
|
||||
foreach ($this->callbackListeners as &$listeners) {
|
||||
foreach ($listeners as $key => &$listenerCallback) {
|
||||
if ($listenerCallback[0] === $listener) {
|
||||
unset($listeners[$key]);
|
||||
foreach ($listeningsArray as &$listenings) {
|
||||
foreach ($listenings as $key => &$listening) {
|
||||
/** @var Listening $listening */
|
||||
if ($listening->listener === $listener) {
|
||||
unset($listenings[$key]);
|
||||
$removed = true;
|
||||
}
|
||||
}
|
||||
@ -131,22 +173,13 @@ class CallbackManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a Script Callback Listener
|
||||
* Unregister a Script Callback Listener
|
||||
*
|
||||
* @param CallbackListener $listener
|
||||
* @return bool
|
||||
*/
|
||||
public function unregisterScriptCallbackListener(CallbackListener $listener) {
|
||||
$removed = false;
|
||||
foreach ($this->scriptCallbackListener as &$listeners) {
|
||||
foreach ($listeners as $key => &$listenerCallback) {
|
||||
if ($listenerCallback[0] === $listener) {
|
||||
unset($listeners[$key]);
|
||||
$removed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $removed;
|
||||
return $this->removeCallbackListener($this->scriptCallbackListenings, $listener);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -161,9 +194,8 @@ class CallbackManager {
|
||||
return;
|
||||
}
|
||||
|
||||
$callbacks = $this->maniaControl->client->executeCallbacks();
|
||||
|
||||
// Handle callbacks
|
||||
$callbacks = $this->maniaControl->client->executeCallbacks();
|
||||
foreach ($callbacks as $callback) {
|
||||
$this->handleCallback($callback);
|
||||
}
|
||||
@ -211,13 +243,16 @@ class CallbackManager {
|
||||
* @param string $callbackName
|
||||
*/
|
||||
public function triggerCallback($callbackName) {
|
||||
if (!array_key_exists($callbackName, $this->callbackListeners)) {
|
||||
if (!array_key_exists($callbackName, $this->callbackListenings)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$params = func_get_args();
|
||||
$params = array_slice($params, 1, count($params), true);
|
||||
foreach ($this->callbackListeners[$callbackName] as $listener) {
|
||||
call_user_func_array(array($listener[0], $listener[1]), $params);
|
||||
$params = array_slice($params, 1, null, true);
|
||||
|
||||
foreach ($this->callbackListenings[$callbackName] as $listening) {
|
||||
/** @var Listening $listening */
|
||||
$listening->triggerCallbackWithParams($params);
|
||||
}
|
||||
}
|
||||
|
||||
@ -239,13 +274,16 @@ class CallbackManager {
|
||||
* @param string $callbackName
|
||||
*/
|
||||
public function triggerScriptCallback($callbackName) {
|
||||
if (!array_key_exists($callbackName, $this->scriptCallbackListener)) {
|
||||
if (!array_key_exists($callbackName, $this->scriptCallbackListenings)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$params = func_get_args();
|
||||
$params = array_slice($params, 1, count($params), true);
|
||||
foreach ($this->scriptCallbackListener[$callbackName] as $listener) {
|
||||
call_user_func_array(array($listener[0], $listener[1]), $params);
|
||||
$params = array_slice($params, 1, null, true);
|
||||
|
||||
foreach ($this->scriptCallbackListenings[$callbackName] as $listening) {
|
||||
/** @var Listening $listening */
|
||||
$listening->triggerCallbackWithParams($params);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,17 +2,16 @@
|
||||
|
||||
namespace ManiaControl\Callbacks;
|
||||
|
||||
|
||||
use ManiaControl\ManiaControl;
|
||||
|
||||
/**
|
||||
* Class managing & converting LibXmlRpc Callbacks
|
||||
* Class converting LibXmlRpc Callbacks
|
||||
*
|
||||
* @author ManiaControl Team <mail@maniacontrol.com>
|
||||
* @copyright 2014 ManiaControl Team
|
||||
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
||||
*/
|
||||
class LibXmlRpcCallbackManager implements CallbackListener {
|
||||
class LibXmlRpcCallbacks implements CallbackListener {
|
||||
/*
|
||||
* Private Properties
|
||||
*/
|
||||
@ -26,16 +25,17 @@ class LibXmlRpcCallbackManager implements CallbackListener {
|
||||
*/
|
||||
public function __construct(ManiaControl $maniaControl, CallbackManager $callbackManager) {
|
||||
$this->maniaControl = $maniaControl;
|
||||
$callbackManager->registerCallbackListener(Callbacks::SCRIPTCALLBACK, $this, 'handleScriptCallbacks');
|
||||
|
||||
$callbackManager->registerCallbackListener(Callbacks::SCRIPTCALLBACK, $this, 'handleScriptCallback');
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle Script Callbacks
|
||||
* Handle the Script Callback
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $data
|
||||
*/
|
||||
public function handleScriptCallbacks($name, $data) {
|
||||
public function handleScriptCallback($name, $data) {
|
||||
switch ($name) {
|
||||
case 'LibXmlRpc_BeginMatch':
|
||||
$this->maniaControl->callbackManager->triggerCallback(Callbacks::BEGINMATCH, $data[0]);
|
||||
@ -82,7 +82,7 @@ class LibXmlRpcCallbackManager implements CallbackListener {
|
||||
}
|
||||
|
||||
/**
|
||||
* Triggers the Ranking of a Player
|
||||
* Trigger the Ranking of a Player
|
||||
*
|
||||
* @param array $data
|
||||
*/
|
76
application/core/Callbacks/Listening.php
Normal file
76
application/core/Callbacks/Listening.php
Normal file
@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace ManiaControl\Callbacks;
|
||||
|
||||
/**
|
||||
* Model Class for a Basic Listening
|
||||
*
|
||||
* @author ManiaControl Team <mail@maniacontrol.com>
|
||||
* @copyright 2014 ManiaControl Team
|
||||
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
||||
*/
|
||||
class Listening {
|
||||
/*
|
||||
* Public Properties
|
||||
*/
|
||||
public $listener = null;
|
||||
public $method = null;
|
||||
|
||||
/**
|
||||
* Construct a new Timer Listening
|
||||
*
|
||||
* @param object $listener
|
||||
* @param mixed $method
|
||||
*/
|
||||
public function __construct($listener, $method) {
|
||||
$this->listener = $listener;
|
||||
$this->method = $method;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the given Listener and Method build a valid Callback
|
||||
*
|
||||
* @param object $listener
|
||||
* @param mixed $method
|
||||
* @return bool
|
||||
*/
|
||||
public static function checkValidCallback($listener, $method) {
|
||||
if (is_callable($method)) {
|
||||
return true;
|
||||
}
|
||||
$listenerCallback = array($listener, $method);
|
||||
if (is_callable($listenerCallback)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trigger the Listener's Method
|
||||
*/
|
||||
public function triggerCallback() {
|
||||
$params = func_get_args();
|
||||
$this->triggerCallbackWithParams($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Trigger the Listener's Method with the given Array of Params
|
||||
*
|
||||
* @param array $params
|
||||
*/
|
||||
public function triggerCallbackWithParams(array $params) {
|
||||
call_user_func_array($this->getUserFunction(), $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Callable User Function
|
||||
*
|
||||
* @return callable
|
||||
*/
|
||||
public function getUserFunction() {
|
||||
if (is_callable($this->method)) {
|
||||
return $this->method;
|
||||
}
|
||||
return array($this->listener, $this->method);
|
||||
}
|
||||
}
|
@ -44,7 +44,7 @@ class ShootManiaCallbacks implements CallbackListener {
|
||||
* Handle Script Callbacks
|
||||
*
|
||||
* @param string $name
|
||||
* @param array $data
|
||||
* @param mixed $data
|
||||
*/
|
||||
public function handleScriptCallbacks($name, $data) {
|
||||
switch ($name) {
|
||||
|
@ -3,18 +3,16 @@
|
||||
namespace ManiaControl\Callbacks;
|
||||
|
||||
/**
|
||||
* Model Class for a TimerListening
|
||||
* Model Class for a Timer Listening
|
||||
*
|
||||
* @author ManiaControl Team <mail@maniacontrol.com>
|
||||
* @copyright 2014 ManiaControl Team
|
||||
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
||||
*/
|
||||
class TimerListening {
|
||||
class TimerListening extends Listening {
|
||||
/*
|
||||
* Public Properties
|
||||
*/
|
||||
public $listener = null;
|
||||
public $method = null;
|
||||
public $deltaTime = null;
|
||||
public $oneTime = null;
|
||||
public $lastTrigger = null;
|
||||
@ -24,14 +22,14 @@ class TimerListening {
|
||||
* Construct a new Timer Listening
|
||||
*
|
||||
* @param TimerListener $listener
|
||||
* @param string $method
|
||||
* @param mixed $method
|
||||
* @param float $milliSeconds
|
||||
* @param bool $oneTime
|
||||
* @param bool $instantCall
|
||||
*/
|
||||
public function __construct(TimerListener $listener, $method, $milliSeconds, $oneTime = false, $instantCall = true) {
|
||||
$this->listener = $listener;
|
||||
$this->method = $method;
|
||||
parent::__construct($listener, $method);
|
||||
|
||||
$this->deltaTime = $milliSeconds / 1000.;
|
||||
$this->oneTime = (bool)$oneTime;
|
||||
$this->instantCall = (bool)$instantCall;
|
||||
@ -51,27 +49,6 @@ class TimerListening {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Trigger the Listener's Method
|
||||
*
|
||||
* @param float $time
|
||||
*/
|
||||
public function triggerCallback($time) {
|
||||
call_user_func($this->getUserFunction(), $time);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Callable User Function
|
||||
*
|
||||
* @return callable
|
||||
*/
|
||||
public function getUserFunction() {
|
||||
if (is_callable($this->method)) {
|
||||
return $this->method;
|
||||
}
|
||||
return array($this->listener, $this->method);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the desired Time is reached
|
||||
*
|
||||
|
@ -19,6 +19,7 @@ class CommandManager implements CallbackListener {
|
||||
*/
|
||||
private $maniaControl = null;
|
||||
private $helpManager = array();
|
||||
// TODO: use listening class
|
||||
private $adminCommandListeners = array();
|
||||
private $commandListeners = array();
|
||||
|
||||
@ -38,7 +39,7 @@ class CommandManager implements CallbackListener {
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a command listener
|
||||
* Register a Command Listener
|
||||
*
|
||||
* @param string $commandName
|
||||
* @param CommandListener $listener
|
||||
@ -97,7 +98,7 @@ class CommandManager implements CallbackListener {
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a Command Listener
|
||||
* Unregister a Command Listener
|
||||
*
|
||||
* @param CommandListener $listener
|
||||
* @return bool
|
||||
|
@ -40,6 +40,7 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener
|
||||
* Private Properties
|
||||
*/
|
||||
private $maniaControl = null;
|
||||
// TODO: use listening class
|
||||
private $pageAnswerListeners = array();
|
||||
private $pageAnswerRegexListener = array();
|
||||
|
||||
|
@ -59,8 +59,8 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, ManialinkP
|
||||
const SETTING_VOTE_TIME = 'Voting Time';
|
||||
const SETTING_DEFAULT_PLAYER_RATIO = 'Minimum Player Voters Ratio';
|
||||
const SETTING_DEFAULT_RATIO = 'Default Success Ratio';
|
||||
const SETTING_SPECTATOR_ALLOW_VOTE = 'Allow Specators to vote';
|
||||
const SETTING_SPECTATOR_ALLOW_START_VOTE = 'Allow Specators to start a vote';
|
||||
const SETTING_SPECTATOR_ALLOW_VOTE = 'Allow Spectators to vote';
|
||||
const SETTING_SPECTATOR_ALLOW_START_VOTE = 'Allow Spectators to start a vote';
|
||||
|
||||
const MLID_WIDGET = 'CustomVotesPlugin.WidgetId';
|
||||
const MLID_ICON = 'CustomVotesPlugin.IconWidgetId';
|
||||
@ -436,7 +436,7 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, ManialinkP
|
||||
return;
|
||||
}
|
||||
|
||||
//Specators are not allowed to start a vote
|
||||
// Spectators are not allowed to start a vote
|
||||
if ($player->isSpectator && !$this->maniaControl->settingManager->getSetting($this, self::SETTING_SPECTATOR_ALLOW_START_VOTE)) {
|
||||
$this->maniaControl->chat->sendError('Spectators are not allowed to start a vote.', $player->login);
|
||||
return;
|
||||
|
Loading…
Reference in New Issue
Block a user