From 75275612630b75f8d1a3a07c88f76530a9be85e5 Mon Sep 17 00:00:00 2001 From: kremsy Date: Fri, 19 Jun 2015 01:37:31 +0200 Subject: [PATCH] new EchoManager to handle Echo Callbacks --- core/Callbacks/EchoListener.php | 12 +++ core/Callbacks/EchoManager.php | 135 ++++++++++++++++++++++++++++++++ core/ManiaControl.php | 15 ++++ core/Players/PlayerActions.php | 21 +++-- core/Players/PlayerManager.php | 10 ++- core/Plugins/PluginManager.php | 4 + 6 files changed, 189 insertions(+), 8 deletions(-) create mode 100644 core/Callbacks/EchoListener.php create mode 100644 core/Callbacks/EchoManager.php diff --git a/core/Callbacks/EchoListener.php b/core/Callbacks/EchoListener.php new file mode 100644 index 00000000..a0f87e2b --- /dev/null +++ b/core/Callbacks/EchoListener.php @@ -0,0 +1,12 @@ + + * @copyright 2014-2015 ManiaControl Team + * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 + */ +interface EchoListener { +} diff --git a/core/Callbacks/EchoManager.php b/core/Callbacks/EchoManager.php new file mode 100644 index 00000000..1a902a6d --- /dev/null +++ b/core/Callbacks/EchoManager.php @@ -0,0 +1,135 @@ + + * @copyright 2014-2015 ManiaControl Team + * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 + */ +class EchoManager implements CallbackListener, EchoListener { + /* + * Private properties + */ + /** @var ManiaControl $maniaControl */ + private $maniaControl = null; + /** @var Listening[] $echoListenings */ + private $echoListenings = array(); + + /** + * Create a new Echo Handler Instance + * + * @param ManiaControl $maniaControl + */ + public function __construct(ManiaControl $maniaControl) { + $this->maniaControl = $maniaControl; + + $this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_ECHO, $this, 'handleEchos'); + } + + /** + * Register a new Echo Listener + * + * @param string $callbackName + * @param EchoListener $listener + * @param string $method + * @return bool + */ + public function registerEchoListener($echoName, EchoListener $listener, $method) { + if (!Listening::checkValidCallback($listener, $method)) { + $listenerClass = get_class($listener); + trigger_error("Given Listener '{$listenerClass}' can't handle Callback '{$echoName}': No callable Method '{$method}'!"); + return false; + } + + if (!array_key_exists($echoName, $this->echoListenings)) { + $this->echoListenings[$echoName] = array(); + } + + $listening = new Listening($listener, $method); + array_push($this->echoListenings[$echoName], $listening); + return true; + } + + /** + * Unregister a Echo Listener + * + * @param EchoListener $listener + * @return bool + */ + public function unregisterEchoListener(EchoListener $listener) { + return $this->removeEchoListener($this->echoListenings, $listener); + } + + /** + * Remove the Echo Listener from the given Listeners Array + * + * @param Listening[] $listeningsArray + * @param EchoListener $listener + * @return bool + */ + private function removeEchoListener(array &$listeningsArray, EchoListener $listener) { + $removed = false; + foreach ($listeningsArray as &$listenings) { + foreach ($listenings as $key => &$listening) { + if ($listening->listener === $listener) { + unset($listenings[$key]); + $removed = true; + } + } + } + return $removed; + } + + + /** + * Trigger a specific Callback + * + * @param mixed $callback + */ + public function triggerEchoCallback($callbackName) { + if (!array_key_exists($callbackName, $this->echoListenings)) { + return; + } + + + $params = func_get_args(); + $params = array_slice($params, 1, null, true); + + var_dump($params); + foreach ($this->echoListenings[$callbackName] as $listening) { + /** @var Listening $listening */ + $listening->triggerCallbackWithParams($params); + } + } + + //TODO temporary testmethod, remove + public function test() { + // $this->maniaControl->getEchoManager()->test(); + $msg = json_encode(array("player" => "abc")); + //$callback = array("test1", "test2"); + $this->maniaControl->getClient()->dedicatedEcho($msg, "ManiaControl.PlayerManager.WarnPlayer"); + } + + /** + * Handle the given Callback + * + * @param array $callback + */ + public function handleEchos($param) { + $name = $param[1][0]; + $message = json_decode($param[1][1]); + switch ($name) { + case 'ManiaControl.Restart': + $this->maniaControl->restart($message); + break; + default: + $this->triggerEchoCallback($name, $message); + } + } +} \ No newline at end of file diff --git a/core/ManiaControl.php b/core/ManiaControl.php index f0912a0d..bf523dc4 100644 --- a/core/ManiaControl.php +++ b/core/ManiaControl.php @@ -8,6 +8,7 @@ use ManiaControl\Bills\BillManager; use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Callbacks\Callbacks; +use ManiaControl\Callbacks\EchoManager; use ManiaControl\Callbacks\TimerListener; use ManiaControl\Callbacks\TimerManager; use ManiaControl\Commands\CommandListener; @@ -164,6 +165,9 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener { */ private $requestQuitMessage = null; + /** @var EchoManager $echoManager */ + private $echoManager = null; + /** * Construct a new ManiaControl instance */ @@ -176,6 +180,7 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener { // Load ManiaControl Modules $this->callbackManager = new CallbackManager($this); + $this->echoManager = new EchoManager($this); $this->timerManager = new TimerManager($this); $this->database = new Database($this); $this->fileReader = new AsynchronousFileReader($this); @@ -194,6 +199,7 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener { $this->pluginManager = new PluginManager($this); $this->updateManager = new UpdateManager($this); + $this->getErrorHandler()->init(); // Permissions @@ -278,6 +284,15 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener { return $this->callbackManager; } + /** + * Return the echo manager + * + * @return EchoManager + */ + public function getEchoManager() { + return $this->echoManager; + } + /** * Return the chat * diff --git a/core/Players/PlayerActions.php b/core/Players/PlayerActions.php index eb319f5d..90b41cf5 100644 --- a/core/Players/PlayerActions.php +++ b/core/Players/PlayerActions.php @@ -290,11 +290,13 @@ class PlayerActions { * @param string $targetLogin */ public function warnPlayer($adminLogin, $targetLogin) { - $admin = $this->maniaControl->getPlayerManager()->getPlayer($adminLogin); - if (!$this->maniaControl->getAuthenticationManager()->checkPermission($admin, self::SETTING_PERMISSION_WARN_PLAYER) - ) { - $this->maniaControl->getAuthenticationManager()->sendNotAllowed($admin); - return; + if ($adminLogin != 'EchoListener') { + $admin = $this->maniaControl->getPlayerManager()->getPlayer($adminLogin); + if (!$this->maniaControl->getAuthenticationManager()->checkPermission($admin, self::SETTING_PERMISSION_WARN_PLAYER) + ) { + $this->maniaControl->getAuthenticationManager()->sendNotAllowed($admin); + return; + } } $target = $this->maniaControl->getPlayerManager()->getPlayer($targetLogin); @@ -360,8 +362,13 @@ class PlayerActions { $this->maniaControl->getManialinkManager()->displayWidget($maniaLink, $target); // Announce warning - $title = $this->maniaControl->getAuthenticationManager()->getAuthLevelName($admin->authLevel); - $chatMessage = $title . ' ' . $admin->getEscapedNickname() . ' warned ' . $target->getEscapedNickname() . '!'; + if ($adminLogin != 'EchoListener') { + $title = $this->maniaControl->getAuthenticationManager()->getAuthLevelName($admin->authLevel); + $chatMessage = $title . ' ' . $admin->getEscapedNickname() . ' warned ' . $target->getEscapedNickname() . '!'; + } else { + $chatMessage = $target->getEscapedNickname() . ' got an Administrative warning!'; + } + $this->maniaControl->getChat()->sendInformation($chatMessage); Logger::log($chatMessage, true); } diff --git a/core/Players/PlayerManager.php b/core/Players/PlayerManager.php index 11e5ec51..0fc8194d 100644 --- a/core/Players/PlayerManager.php +++ b/core/Players/PlayerManager.php @@ -6,6 +6,7 @@ use ManiaControl\Admin\AdminLists; use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Callbacks\Callbacks; +use ManiaControl\Callbacks\EchoListener; use ManiaControl\Callbacks\TimerListener; use ManiaControl\Logger; use ManiaControl\ManiaControl; @@ -20,7 +21,7 @@ use Maniaplanet\DedicatedServer\Xmlrpc\UnknownPlayerException; * @copyright 2014-2015 ManiaControl Team * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 */ -class PlayerManager implements CallbackListener, TimerListener { +class PlayerManager implements CallbackListener, TimerListener, EchoListener { /* * Constants */ @@ -34,6 +35,8 @@ class PlayerManager implements CallbackListener, TimerListener { const STAT_JOIN_COUNT = 'Joins'; const STAT_SERVERTIME = 'Servertime'; + const ECHO_WARN_PLAYER = 'ManiaControl.PlayerManager.WarnPlayer'; + /* * Public properties */ @@ -105,6 +108,11 @@ class PlayerManager implements CallbackListener, TimerListener { // Player stats $this->maniaControl->getStatisticManager()->defineStatMetaData(self::STAT_JOIN_COUNT); $this->maniaControl->getStatisticManager()->defineStatMetaData(self::STAT_SERVERTIME, StatisticManager::STAT_TYPE_TIME); + + // Echo Warn Command (Usage: sendEcho json_encode("player" => "loginName") + $this->maniaControl->getEchoManager()->registerEchoListener(self::ECHO_WARN_PLAYER, $this, function ($params) { + $this->playerActions->warnPlayer("EchoListener", $params->player); + }); } /** diff --git a/core/Plugins/PluginManager.php b/core/Plugins/PluginManager.php index 13f74efc..e51b6c66 100644 --- a/core/Plugins/PluginManager.php +++ b/core/Plugins/PluginManager.php @@ -3,6 +3,7 @@ namespace ManiaControl\Plugins; use ManiaControl\Callbacks\CallbackListener; +use ManiaControl\Callbacks\EchoListener; use ManiaControl\Callbacks\TimerListener; use ManiaControl\Commands\CommandListener; use ManiaControl\Files\FileUtil; @@ -141,6 +142,9 @@ class PluginManager { $plugin->unload(); + if ($plugin instanceof EchoListener) { + $this->maniaControl->getEchoManager()->unregisterEchoListener($plugin); + } if ($plugin instanceof CallbackListener) { $this->maniaControl->getCallbackManager()->unregisterCallbackListener($plugin); $this->maniaControl->getCallbackManager()->unregisterScriptCallbackListener($plugin);