new EchoManager to handle Echo Callbacks

This commit is contained in:
kremsy 2015-06-19 01:37:31 +02:00
parent 56833021c6
commit 7527561263
6 changed files with 189 additions and 8 deletions

View File

@ -0,0 +1,12 @@
<?php
namespace ManiaControl\Callbacks;
/**
* Interface for EchoListener
*
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014-2015 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
interface EchoListener {
}

View File

@ -0,0 +1,135 @@
<?php
namespace ManiaControl\Callbacks;
use ManiaControl\ManiaControl;
/**
* Class for managing Echo Callbacks
*
* @author ManiaControl Team <mail@maniacontrol.com>
* @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);
}
}
}

View File

@ -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
*

View File

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

View File

@ -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);
});
}
/**

View File

@ -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);