first socket method implementation :)

This commit is contained in:
kremsy 2015-06-21 22:24:10 +02:00
parent a025211dfd
commit 8a58035a57
4 changed files with 47 additions and 4 deletions

View File

@ -3,7 +3,11 @@
namespace ManiaControl;
use ManiaControl\Admin\AuthenticationManager;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Players\Player;
use ManiaControl\Sockets\SocketListener;
use ManiaControl\Sockets\SocketMethods;
use Maniaplanet\DedicatedServer\Xmlrpc\UnknownPlayerException;
/**
@ -13,7 +17,7 @@ use Maniaplanet\DedicatedServer\Xmlrpc\UnknownPlayerException;
* @copyright 2014-2015 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class Chat {
class Chat implements CallbackListener, SocketListener {
/*
* Constants
*/
@ -22,12 +26,13 @@ class Chat {
const SETTING_FORMAT_SUCCESS = 'Success Format';
const SETTING_FORMAT_ERROR = 'Error Format';
const SETTING_FORMAT_USAGEINFO = 'UsageInfo Format';
const CHAT_BUFFER_SIZE = 200;
/*
* Private properties
*/
/** @var ManiaControl $maniaControl */
private $maniaControl = null;
private $chatBuffer = array();
/**
* Construct chat utility
@ -43,8 +48,17 @@ class Chat {
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_FORMAT_SUCCESS, '$0f0');
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_FORMAT_ERROR, '$f30');
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_FORMAT_USAGEINFO, '$f80');
//Callbacks
$this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_PLAYERCHAT, $this, 'onPlayerChat');
//Socket Listenings
$this->maniaControl->getSocketManager()->registerSocketListener(SocketMethods::GET_SERVER_CHAT, $this, function ($data) {
return $this->chatBuffer;
});
}
/**
* Send an information message to the given login
*
@ -226,4 +240,24 @@ class Chat {
$format = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_FORMAT_USAGEINFO);
return $this->sendChat($format . $message, $login, $prefix);
}
/**
* Stores the ChatMessage in the Buffer
*
* @param $data
*/
public function onPlayerChat($data) {
$login = $data[1][1];
$player = $this->maniaControl->getPlayerManager()->getPlayer($login);
$nickname = "";
if ($player) {
$nickname = $player->nickname;
}
array_push($this->chatBuffer, array("user" => $login, "nickname" => $nickname, "message" => $data[1][2]));
if (count($this->chatBuffer) > self::CHAT_BUFFER_SIZE) {
array_shift($this->chatBuffer);
}
}
}

View File

@ -212,7 +212,7 @@ class SocketManager implements CallbackListener {
} else if (!property_exists($data, "method") || !property_exists($data, "data")) {
$data = array("error" => true, "data" => "Invalid Message");
} else {
$answer = $this->triggerSocketCallback($data->method, $data);
$answer = $this->triggerSocketCallback($data->method, $data->data);
//Prepare Response
if (!$answer) {
$data = array("error" => true, "data" => "No listener or response on the given Message");

View File

@ -0,0 +1,9 @@
<?php
namespace ManiaControl\Sockets;
interface SocketMethods {
/** Returns the last 200 lines of the chat (inclusive player logins and nicknames) */
const GET_SERVER_CHAT = "Chat.GetServerChat";
}

View File

@ -6,7 +6,6 @@ Sample Web Implementation
$errstr = null;
$socket = fsockopen("xx.xxx.xx.xx", xxxxx, $errno, $errstr, 2);
echo "ok?" . $errno . " - " . $errstr . "\n";
$buff = '';
$data = array("method" => "getServerChat", "data" => "");
@ -21,6 +20,7 @@ Sample Web Implementation
// Read Answer Data
$len = (int)fgets($socket);
echo $len;
$buff = '';
while (!feof($socket) && strlen($buff) < $len) {
$buff .= fgets($socket, $len - strlen($buff) + 1);
}