Base Listening Class for Callback Management

This commit is contained in:
Steffen Schröder 2014-05-13 01:18:47 +02:00
parent 09f61f2ad4
commit 09f11afd6e
8 changed files with 178 additions and 85 deletions

View File

@ -56,38 +56,53 @@ class CallbackManager {
* Private Properties * Private Properties
*/ */
private $maniaControl = null; private $maniaControl = null;
private $callbackListeners = array(); private $callbackListenings = array();
private $scriptCallbackListener = array(); private $scriptCallbackListenings = array();
/** /**
* Construct a new Callbacks Manager * Construct a new Callbacks Manager
* *
* @param \ManiaControl\ManiaControl $maniaControl * @param ManiaControl $maniaControl
*/ */
public function __construct(ManiaControl $maniaControl) { public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl; $this->maniaControl = $maniaControl;
$this->shootManiaCallbacks = new ShootManiaCallbacks($maniaControl, $this); $this->shootManiaCallbacks = new ShootManiaCallbacks($maniaControl, $this);
$this->libXmlRpcCallbacks = new LibXmlRpcCallbackManager($maniaControl, $this); $this->libXmlRpcCallbacks = new LibXmlRpcCallbacks($maniaControl, $this);
} }
/** /**
* Register a new Callback Listener * Register a new Callback Listener
* *
* @param string $callbackName * @param string $callbackName
* @param \ManiaControl\Callbacks\CallbackListener $listener * @param 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 (is_array($callbackName)) {
trigger_error("Given listener (" . get_class($listener) . ") can't handle callback '{$callbackName}' (no method '{$method}')!"); $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; 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; return true;
} }
@ -100,29 +115,56 @@ class CallbackManager {
* @return bool * @return bool
*/ */
public function registerScriptCallbackListener($callbackName, CallbackListener $listener, $method) { public function registerScriptCallbackListener($callbackName, CallbackListener $listener, $method) {
if (!method_exists($listener, $method)) { if (is_array($callbackName)) {
trigger_error("Given listener (" . get_class($listener) . ") can't handle script callback '{$callbackName}' (no method '{$method}')!"); $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; 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; return true;
} }
/** /**
* Remove a Callback Listener * Unregister a Callback Listener
* *
* @param CallbackListener $listener * @param CallbackListener $listener
* @return bool * @return bool
*/ */
public function unregisterCallbackListener(CallbackListener $listener) { 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; $removed = false;
foreach ($this->callbackListeners as &$listeners) { foreach ($listeningsArray as &$listenings) {
foreach ($listeners as $key => &$listenerCallback) { foreach ($listenings as $key => &$listening) {
if ($listenerCallback[0] === $listener) { /** @var Listening $listening */
unset($listeners[$key]); if ($listening->listener === $listener) {
unset($listenings[$key]);
$removed = true; $removed = true;
} }
} }
@ -131,22 +173,13 @@ class CallbackManager {
} }
/** /**
* Remove a Script Callback Listener * Unregister a Script Callback Listener
* *
* @param CallbackListener $listener * @param CallbackListener $listener
* @return bool * @return bool
*/ */
public function unregisterScriptCallbackListener(CallbackListener $listener) { public function unregisterScriptCallbackListener(CallbackListener $listener) {
$removed = false; return $this->removeCallbackListener($this->scriptCallbackListenings, $listener);
foreach ($this->scriptCallbackListener as &$listeners) {
foreach ($listeners as $key => &$listenerCallback) {
if ($listenerCallback[0] === $listener) {
unset($listeners[$key]);
$removed = true;
}
}
}
return $removed;
} }
/** /**
@ -161,9 +194,8 @@ class CallbackManager {
return; return;
} }
$callbacks = $this->maniaControl->client->executeCallbacks();
// Handle callbacks // Handle callbacks
$callbacks = $this->maniaControl->client->executeCallbacks();
foreach ($callbacks as $callback) { foreach ($callbacks as $callback) {
$this->handleCallback($callback); $this->handleCallback($callback);
} }
@ -211,13 +243,16 @@ class CallbackManager {
* @param string $callbackName * @param string $callbackName
*/ */
public function triggerCallback($callbackName) { public function triggerCallback($callbackName) {
if (!array_key_exists($callbackName, $this->callbackListeners)) { if (!array_key_exists($callbackName, $this->callbackListenings)) {
return; return;
} }
$params = func_get_args(); $params = func_get_args();
$params = array_slice($params, 1, count($params), true); $params = array_slice($params, 1, null, true);
foreach ($this->callbackListeners[$callbackName] as $listener) {
call_user_func_array(array($listener[0], $listener[1]), $params); foreach ($this->callbackListenings[$callbackName] as $listening) {
/** @var Listening $listening */
$listening->triggerCallbackWithParams($params);
} }
} }
@ -239,13 +274,16 @@ class CallbackManager {
* @param string $callbackName * @param string $callbackName
*/ */
public function triggerScriptCallback($callbackName) { public function triggerScriptCallback($callbackName) {
if (!array_key_exists($callbackName, $this->scriptCallbackListener)) { if (!array_key_exists($callbackName, $this->scriptCallbackListenings)) {
return; return;
} }
$params = func_get_args(); $params = func_get_args();
$params = array_slice($params, 1, count($params), true); $params = array_slice($params, 1, null, true);
foreach ($this->scriptCallbackListener[$callbackName] as $listener) {
call_user_func_array(array($listener[0], $listener[1]), $params); foreach ($this->scriptCallbackListenings[$callbackName] as $listening) {
/** @var Listening $listening */
$listening->triggerCallbackWithParams($params);
} }
} }
} }

View File

@ -2,17 +2,16 @@
namespace ManiaControl\Callbacks; namespace ManiaControl\Callbacks;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
/** /**
* Class managing & converting LibXmlRpc Callbacks * Class converting LibXmlRpc Callbacks
* *
* @author ManiaControl Team <mail@maniacontrol.com> * @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team * @copyright 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/ */
class LibXmlRpcCallbackManager implements CallbackListener { class LibXmlRpcCallbacks implements CallbackListener {
/* /*
* Private Properties * Private Properties
*/ */
@ -26,16 +25,17 @@ class LibXmlRpcCallbackManager implements CallbackListener {
*/ */
public function __construct(ManiaControl $maniaControl, CallbackManager $callbackManager) { public function __construct(ManiaControl $maniaControl, CallbackManager $callbackManager) {
$this->maniaControl = $maniaControl; $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 string $name
* @param mixed $data * @param mixed $data
*/ */
public function handleScriptCallbacks($name, $data) { public function handleScriptCallback($name, $data) {
switch ($name) { switch ($name) {
case 'LibXmlRpc_BeginMatch': case 'LibXmlRpc_BeginMatch':
$this->maniaControl->callbackManager->triggerCallback(Callbacks::BEGINMATCH, $data[0]); $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 * @param array $data
*/ */

View 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);
}
}

View File

@ -44,7 +44,7 @@ class ShootManiaCallbacks implements CallbackListener {
* Handle Script Callbacks * Handle Script Callbacks
* *
* @param string $name * @param string $name
* @param array $data * @param mixed $data
*/ */
public function handleScriptCallbacks($name, $data) { public function handleScriptCallbacks($name, $data) {
switch ($name) { switch ($name) {

View File

@ -3,18 +3,16 @@
namespace ManiaControl\Callbacks; namespace ManiaControl\Callbacks;
/** /**
* Model Class for a TimerListening * Model Class for a Timer Listening
* *
* @author ManiaControl Team <mail@maniacontrol.com> * @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team * @copyright 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/ */
class TimerListening { class TimerListening extends Listening {
/* /*
* Public Properties * Public Properties
*/ */
public $listener = null;
public $method = null;
public $deltaTime = null; public $deltaTime = null;
public $oneTime = null; public $oneTime = null;
public $lastTrigger = null; public $lastTrigger = null;
@ -24,14 +22,14 @@ class TimerListening {
* Construct a new Timer Listening * Construct a new Timer Listening
* *
* @param TimerListener $listener * @param TimerListener $listener
* @param string $method * @param mixed $method
* @param float $milliSeconds * @param float $milliSeconds
* @param bool $oneTime * @param bool $oneTime
* @param bool $instantCall * @param bool $instantCall
*/ */
public function __construct(TimerListener $listener, $method, $milliSeconds, $oneTime = false, $instantCall = true) { public function __construct(TimerListener $listener, $method, $milliSeconds, $oneTime = false, $instantCall = true) {
$this->listener = $listener; parent::__construct($listener, $method);
$this->method = $method;
$this->deltaTime = $milliSeconds / 1000.; $this->deltaTime = $milliSeconds / 1000.;
$this->oneTime = (bool)$oneTime; $this->oneTime = (bool)$oneTime;
$this->instantCall = (bool)$instantCall; $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 * Check if the desired Time is reached
* *

View File

@ -19,6 +19,7 @@ class CommandManager implements CallbackListener {
*/ */
private $maniaControl = null; private $maniaControl = null;
private $helpManager = array(); private $helpManager = array();
// TODO: use listening class
private $adminCommandListeners = array(); private $adminCommandListeners = array();
private $commandListeners = array(); private $commandListeners = array();
@ -38,7 +39,7 @@ class CommandManager implements CallbackListener {
} }
/** /**
* Register a command listener * Register a Command Listener
* *
* @param string $commandName * @param string $commandName
* @param CommandListener $listener * @param CommandListener $listener
@ -97,7 +98,7 @@ class CommandManager implements CallbackListener {
} }
/** /**
* Remove a Command Listener * Unregister a Command Listener
* *
* @param CommandListener $listener * @param CommandListener $listener
* @return bool * @return bool

View File

@ -40,6 +40,7 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener
* Private Properties * Private Properties
*/ */
private $maniaControl = null; private $maniaControl = null;
// TODO: use listening class
private $pageAnswerListeners = array(); private $pageAnswerListeners = array();
private $pageAnswerRegexListener = array(); private $pageAnswerRegexListener = array();

View File

@ -59,8 +59,8 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, ManialinkP
const SETTING_VOTE_TIME = 'Voting Time'; const SETTING_VOTE_TIME = 'Voting Time';
const SETTING_DEFAULT_PLAYER_RATIO = 'Minimum Player Voters Ratio'; const SETTING_DEFAULT_PLAYER_RATIO = 'Minimum Player Voters Ratio';
const SETTING_DEFAULT_RATIO = 'Default Success Ratio'; const SETTING_DEFAULT_RATIO = 'Default Success Ratio';
const SETTING_SPECTATOR_ALLOW_VOTE = 'Allow Specators to vote'; const SETTING_SPECTATOR_ALLOW_VOTE = 'Allow Spectators to vote';
const SETTING_SPECTATOR_ALLOW_START_VOTE = 'Allow Specators to start a vote'; const SETTING_SPECTATOR_ALLOW_START_VOTE = 'Allow Spectators to start a vote';
const MLID_WIDGET = 'CustomVotesPlugin.WidgetId'; const MLID_WIDGET = 'CustomVotesPlugin.WidgetId';
const MLID_ICON = 'CustomVotesPlugin.IconWidgetId'; const MLID_ICON = 'CustomVotesPlugin.IconWidgetId';
@ -436,7 +436,7 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, ManialinkP
return; 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)) { 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); $this->maniaControl->chat->sendError('Spectators are not allowed to start a vote.', $player->login);
return; return;