From 9760a598369fb2c08fedf8df66fabd8eff109456 Mon Sep 17 00:00:00 2001 From: kremsy Date: Fri, 26 Jun 2015 11:48:21 +0200 Subject: [PATCH] cleanup communicationmanager --- core/Chat.php | 7 ++++--- core/Communication/CommunicationAnswer.php | 22 +++++++++++++++++++++ core/Communication/CommunicationManager.php | 6 ++---- core/ManiaControl.php | 3 ++- core/Players/PlayerActions.php | 18 +++++++++-------- core/Players/PlayerManager.php | 3 ++- 6 files changed, 42 insertions(+), 17 deletions(-) create mode 100644 core/Communication/CommunicationAnswer.php diff --git a/core/Chat.php b/core/Chat.php index 2c9ba82a..434ad7eb 100644 --- a/core/Chat.php +++ b/core/Chat.php @@ -5,6 +5,7 @@ namespace ManiaControl; use ManiaControl\Admin\AuthenticationManager; use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackManager; +use ManiaControl\Communication\CommunicationAnswer; use ManiaControl\Communication\CommunicationListener; use ManiaControl\Communication\CommunicationMethods; use ManiaControl\Players\Player; @@ -55,7 +56,7 @@ class Chat implements CallbackListener, CommunicationListener { //Socket Listenings $this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::SEND_CHAT_MESSAGE, $this, "communcationSendChat"); $this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::GET_SERVER_CHAT, $this, function ($data) { - return array("error" => false, "data" => $this->chatBuffer); + return new CommunicationAnswer($this->chatBuffer); }); } @@ -250,7 +251,7 @@ class Chat implements CallbackListener, CommunicationListener { */ public function communcationSendChat($data) { if (!is_object($data) || !property_exists($data, "message")) { - return array("error" => true, "data" => "You have to provide a valid message"); + return new CommunicationAnswer("You have to provide a valid message", true); } $prefix = true; @@ -306,7 +307,7 @@ class Chat implements CallbackListener, CommunicationListener { } } - return array("error" => false, "data" => ""); + return new CommunicationAnswer(); } diff --git a/core/Communication/CommunicationAnswer.php b/core/Communication/CommunicationAnswer.php new file mode 100644 index 00000000..e8f4e3fe --- /dev/null +++ b/core/Communication/CommunicationAnswer.php @@ -0,0 +1,22 @@ + + * @copyright 2014-2015 ManiaControl Team + * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 + */ +class CommunicationAnswer { + /** Properties are Public for serialization */ + public $error; + public $data; + + public function __construct($data = "", $error = false) { + $this->data = $data; + $this->error = $error; + } + +} \ No newline at end of file diff --git a/core/Communication/CommunicationManager.php b/core/Communication/CommunicationManager.php index 3d3e63b0..7931c143 100644 --- a/core/Communication/CommunicationManager.php +++ b/core/Communication/CommunicationManager.php @@ -262,11 +262,9 @@ class CommunicationManager implements CallbackListener { $answer = $this->triggerCommuncationCallback($data->method, $data->data); //Prepare Response if (!$answer) { - $data = array("error" => true, "data" => "No listener or response on the given Message"); - } else if (!array_key_exists("error", $answer) || !array_key_exists("data", $answer)) { - $data = array("error" => true, "data" => "Invalid Response on the Message"); + $data = new CommunicationAnswer("No listener or response on the given Message", true); } else { - $data = array("error" => $answer["error"], "data" => $answer["data"]); + $data = $answer; } } diff --git a/core/ManiaControl.php b/core/ManiaControl.php index 4c118413..37d43017 100644 --- a/core/ManiaControl.php +++ b/core/ManiaControl.php @@ -13,6 +13,7 @@ use ManiaControl\Callbacks\TimerListener; use ManiaControl\Callbacks\TimerManager; use ManiaControl\Commands\CommandListener; use ManiaControl\Commands\CommandManager; +use ManiaControl\Communication\CommunicationAnswer; use ManiaControl\Communication\CommunicationListener; use ManiaControl\Communication\CommunicationManager; use ManiaControl\Communication\CommunicationMethods; @@ -228,7 +229,7 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener, } $this->restart(); }, 5000); - return array("error" => false, "data" => ""); + return new CommunicationAnswer(); }); } diff --git a/core/Players/PlayerActions.php b/core/Players/PlayerActions.php index f728ec5e..1fbfd187 100644 --- a/core/Players/PlayerActions.php +++ b/core/Players/PlayerActions.php @@ -9,6 +9,7 @@ use FML\Controls\Quads\Quad_Icons64x64_1; use FML\ManiaLink; use ManiaControl\Admin\AuthenticationManager; use ManiaControl\Callbacks\EchoListener; +use ManiaControl\Communication\CommunicationAnswer; use ManiaControl\Communication\CommunicationListener; use ManiaControl\Communication\CommunicationMethods; use ManiaControl\Logger; @@ -84,31 +85,31 @@ class PlayerActions implements EchoListener, CommunicationListener { //Communication Manager Methods $this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::WARN_PLAYER, $this, function ($data) { if (!is_object($data) || !property_exists($data, "login")) { - return array("error" => true, "data" => "You have to provide a valid player Login"); + return new CommunicationAnswer("You have to provide a valid player Login", true); } $success = $this->warnPlayer(null, $data->login, false); - return array("error" => false, "data" => array("success" => $success)); + return new CommunicationAnswer(array("success" => $success)); }); $this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::MUTE_PLAYER, $this, function ($data) { if (!is_object($data) || !property_exists($data, "login")) { - return array("error" => true, "data" => "You have to provide a valid player Login"); + return new CommunicationAnswer("You have to provide a valid player Login", true); } $success = $this->mutePlayer(null, $data->login, false); - return array("error" => false, "data" => array("success" => $success)); + return new CommunicationAnswer(array("success" => $success)); }); $this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::UNMUTE_PLAYER, $this, function ($data) { if (!is_object($data) || !property_exists($data, "login")) { - return array("error" => true, "data" => "You have to provide a valid player Login"); + return new CommunicationAnswer("You have to provide a valid player Login", true); } $success = $this->unMutePlayer(null, $data->login, false); - return array("error" => false, "data" => array("success" => $success)); + return new CommunicationAnswer(array("success" => $success)); }); $this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::KICK_PLAYER, $this, function ($data) { if (!is_object($data) || !property_exists($data, "login")) { - return array("error" => true, "data" => "You have to provide a valid player Login"); + return new CommunicationAnswer("You have to provide a valid player Login", true); } $message = ""; @@ -117,7 +118,8 @@ class PlayerActions implements EchoListener, CommunicationListener { } $success = $this->kickPlayer(null, $data->login, $message, false); - return array("error" => false, "data" => array("success" => $success)); + + return new CommunicationAnswer(array("success" => $success)); }); } diff --git a/core/Players/PlayerManager.php b/core/Players/PlayerManager.php index 7295a100..d697177c 100644 --- a/core/Players/PlayerManager.php +++ b/core/Players/PlayerManager.php @@ -7,6 +7,7 @@ use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Callbacks\Callbacks; use ManiaControl\Callbacks\TimerListener; +use ManiaControl\Communication\CommunicationAnswer; use ManiaControl\Communication\CommunicationListener; use ManiaControl\Communication\CommunicationMethods; use ManiaControl\Logger; @@ -111,7 +112,7 @@ class PlayerManager implements CallbackListener, TimerListener, CommunicationLis // Communication Listenings $this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::GET_PLAYER_LIST, $this, function ($data) { - return array("error" => false, "data" => $this->players); + return new CommunicationAnswer($this->players); }); }