implementation of sendChat communication

这个提交包含在:
kremsy
2015-06-23 16:57:11 +02:00
父节点 2fbb51dcd4
当前提交 2f25069845
共有 4 个文件被更改,包括 91 次插入6 次删除

查看文件

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

查看文件

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

查看文件

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

查看文件

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