diff --git a/core/Chat.php b/core/Chat.php index f102617d..f6e5ea19 100644 --- a/core/Chat.php +++ b/core/Chat.php @@ -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); + } + } } diff --git a/core/Sockets/SocketManager.php b/core/Sockets/SocketManager.php index 020621ab..eef8f40d 100644 --- a/core/Sockets/SocketManager.php +++ b/core/Sockets/SocketManager.php @@ -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"); diff --git a/core/Sockets/SocketMethods.php b/core/Sockets/SocketMethods.php new file mode 100644 index 00000000..83b09ba0 --- /dev/null +++ b/core/Sockets/SocketMethods.php @@ -0,0 +1,9 @@ + "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); }