implementation of sendChat communication

This commit is contained in:
kremsy 2015-06-23 16:57:11 +02:00
parent 2fbb51dcd4
commit 2f25069845
4 changed files with 91 additions and 6 deletions

View File

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

View File

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

View File

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

View File

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