2015-06-19 01:37:31 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace ManiaControl\Callbacks;
|
|
|
|
|
|
|
|
|
|
|
|
use ManiaControl\ManiaControl;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class for managing Echo Callbacks
|
|
|
|
*
|
|
|
|
* @author ManiaControl Team <mail@maniacontrol.com>
|
2017-02-04 11:49:23 +01:00
|
|
|
* @copyright 2014-2017 ManiaControl Team
|
2015-06-19 01:37:31 +02:00
|
|
|
* @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');
|
|
|
|
}
|
|
|
|
|
2015-06-19 18:30:44 +02:00
|
|
|
/**
|
|
|
|
* Sends an Echo Message
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param mixed $data (can be array, object or string)
|
|
|
|
* @return bool
|
|
|
|
* @throws \Maniaplanet\DedicatedServer\InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function sendEcho($name, $data) {
|
|
|
|
if (is_string($data)) {
|
|
|
|
$success = $this->maniaControl->getClient()->dedicatedEcho($data, $name);
|
|
|
|
} else {
|
|
|
|
$success = $this->maniaControl->getClient()->dedicatedEcho(json_encode($data), $name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $success;
|
|
|
|
}
|
|
|
|
|
2015-06-19 01:37:31 +02:00
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
|
|
|
|
2017-03-24 22:49:43 +01:00
|
|
|
$listening = new Listening($listener, $method);
|
|
|
|
$this->echoListenings[$echoName] = $listening;
|
2015-06-19 01:37:31 +02:00
|
|
|
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);
|
|
|
|
|
2015-06-19 18:30:44 +02:00
|
|
|
//var_dump($params);
|
2015-06-19 01:37:31 +02:00
|
|
|
foreach ($this->echoListenings[$callbackName] as $listening) {
|
|
|
|
/** @var Listening $listening */
|
|
|
|
$listening->triggerCallbackWithParams($params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the given Callback
|
|
|
|
*
|
|
|
|
* @param array $callback
|
|
|
|
*/
|
|
|
|
public function handleEchos($param) {
|
2015-06-19 18:30:44 +02:00
|
|
|
$name = $param[1][0];
|
|
|
|
if (is_object($decode = json_decode($param[1][1]))) {
|
|
|
|
$message = $decode;
|
|
|
|
} else {
|
|
|
|
$message = $param[1][1];
|
|
|
|
}
|
|
|
|
|
2015-06-19 01:37:31 +02:00
|
|
|
switch ($name) {
|
|
|
|
case 'ManiaControl.Restart':
|
|
|
|
$this->maniaControl->restart($message);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$this->triggerEchoCallback($name, $message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|