From 2f25069845e5eef34acab187e002c03888b521fb Mon Sep 17 00:00:00 2001 From: kremsy Date: Tue, 23 Jun 2015 16:57:11 +0200 Subject: [PATCH] implementation of sendChat communication --- core/Chat.php | 74 ++++++++++++++++++++- core/Communication/CommunicationManager.php | 6 +- core/Communication/CommunicationMethods.php | 11 +++ core/Communication/usage_documentation.txt | 6 +- 4 files changed, 91 insertions(+), 6 deletions(-) diff --git a/core/Chat.php b/core/Chat.php index 601237ac..2c9ba82a 100644 --- a/core/Chat.php +++ b/core/Chat.php @@ -53,12 +53,12 @@ class Chat implements CallbackListener, CommunicationListener { $this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_PLAYERCHAT, $this, 'onPlayerChat'); //Socket Listenings - $this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::GET_SERVER_CHAT, $this, function ($error, $data) { - return $this->chatBuffer; + $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); }); } - /** * Send an information message to the given login * @@ -242,6 +242,74 @@ class Chat implements CallbackListener, CommunicationListener { } + /** + * Handles SendChat Communication Request + * + * @param $data + * @return array + */ + public function communcationSendChat($data) { + if (!is_object($data) || !property_exists($data, "message")) { + return array("error" => true, "data" => "You have to provide a valid message"); + } + + $prefix = true; + if (property_exists($data, "prefix")) { + $prefix = $data->prefix; + } + + $login = null; + if (property_exists($data, "login")) { + $login = $data->login; + } + + $adminLevel = 0; + if (property_exists($data, "adminLevel")) { + $adminLevel = $data->adminLevel; + } + + $type = "default"; + if (property_exists($data, "type")) { + $type = $data->type; + } + + switch ($type) { + case "information": + if ($adminLevel) { + $this->sendInformationToAdmins($data->message, $adminLevel, $prefix); + } else { + $this->sendInformation($data->message, $login, $prefix); + } + break; + case "success": + if ($adminLevel) { + $this->sendInformationToAdmins($data->message, $adminLevel, $prefix); + } else { + $this->sendSuccess($data->message, $login, $prefix); + } + break; + case "error": + if ($adminLevel) { + $this->sendErrorToAdmins($data->message, $adminLevel, $prefix); + } else { + $this->sendError($data->message, $login, $prefix); + } + break; + case "usage": + $this->sendUsageInfo($data->message, $login, $prefix); + break; + default: + if ($adminLevel) { + $this->sendMessageToAdmins($data->message, $adminLevel, $prefix); + } else { + $this->sendChat($data->message, $login, $prefix); + } + } + + return array("error" => false, "data" => ""); + } + + /** * Stores the ChatMessage in the Buffer * diff --git a/core/Communication/CommunicationManager.php b/core/Communication/CommunicationManager.php index 621c43bd..3f456bf2 100644 --- a/core/Communication/CommunicationManager.php +++ b/core/Communication/CommunicationManager.php @@ -259,12 +259,14 @@ class CommunicationManager implements CallbackListener { } else if (!property_exists($data, "method") || !property_exists($data, "data")) { $data = array("error" => true, "data" => "Invalid Message"); } else { - $answer = $this->triggerCommuncationCallback($data->method, $data->error, $data->data); + $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"); } else { - $data = array("error" => false, "data" => $answer); + $data = array("error" => $answer["error"], "data" => $answer["data"]); } } diff --git a/core/Communication/CommunicationMethods.php b/core/Communication/CommunicationMethods.php index b34256ef..9a50e948 100644 --- a/core/Communication/CommunicationMethods.php +++ b/core/Communication/CommunicationMethods.php @@ -12,4 +12,15 @@ namespace ManiaControl\Communication; interface CommunicationMethods { /** Returns the last 200 lines of the chat (inclusive player logins and nicknames) */ const GET_SERVER_CHAT = "Chat.GetServerChat"; + + /** Sends a ChatMessage to the Server + * Required Params: + * - message + * Optional Params + * - prefix (use custom prefix or false for no prefix) + * - login (login of a receiver if the message don't get sent to all) + * - adminLevel (minimum Admin Level if the Message should get sent to an Admin) + * - type (type of the message (information, error, success or usage) + */ + const SEND_CHAT_MESSAGE = "Chat.SendChatMessage"; } \ No newline at end of file diff --git a/core/Communication/usage_documentation.txt b/core/Communication/usage_documentation.txt index e4649fe5..fccbc9c5 100644 --- a/core/Communication/usage_documentation.txt +++ b/core/Communication/usage_documentation.txt @@ -5,11 +5,15 @@ There the following settings are existing: -- Passsword (Password which get used to encrypt and decrypt the messages for the openssl connection) -- Listening port for every server (this is the port the CommunicationManager listens at) + +For the description of the available implemented communcation Methods check the CommunicationMethods interface. + + Sample ManiaControl Implementation (for ManiaControl to ManiaControl connections) ##php code begin $communication = $this->maniaControl->getCommunicationManager()->createCommunication(IP/Domain, PORT, 'YOUR_PASSWORD'); - $communication->call(function($error, $data){ + $communication->call(function($data){ var_dump($data); }, CommunicationMethods::GET_SERVER_CHAT); ##php code end