TrackManiaControl/application/core/Chat.php

184 lines
5.5 KiB
PHP
Raw Normal View History

2013-11-09 17:24:03 +01:00
<?php
namespace ManiaControl;
use ManiaControl\Admin\AuthenticationManager;
use ManiaControl\Players\Player;
use Maniaplanet\DedicatedServer\Xmlrpc\UnknownPlayerException;
2013-11-09 17:24:03 +01:00
/**
* Chat Utility Class
2013-11-09 17:24:03 +01:00
*
2014-05-02 17:40:47 +02:00
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
2013-11-09 17:24:03 +01:00
*/
class Chat {
/*
* Constants
*/
2014-01-16 18:08:32 +01:00
const SETTING_PREFIX = 'Messages Prefix';
const SETTING_FORMAT_INFORMATION = 'Information Format';
2014-01-16 18:08:32 +01:00
const SETTING_FORMAT_SUCCESS = 'Success Format';
const SETTING_FORMAT_ERROR = 'Error Format';
const SETTING_FORMAT_USAGEINFO = 'UsageInfo Format';
/*
* Private Properties
2013-11-09 17:24:03 +01:00
*/
private $maniaControl = null;
2013-11-09 17:24:03 +01:00
/**
* Construct chat utility
*
2014-01-16 18:08:32 +01:00
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
2014-01-16 18:08:32 +01:00
// Init settings
2013-12-08 22:38:20 +01:00
$this->maniaControl->settingManager->initSetting($this, self::SETTING_PREFIX, '» ');
$this->maniaControl->settingManager->initSetting($this, self::SETTING_FORMAT_INFORMATION, '$fff');
$this->maniaControl->settingManager->initSetting($this, self::SETTING_FORMAT_SUCCESS, '$0f0');
$this->maniaControl->settingManager->initSetting($this, self::SETTING_FORMAT_ERROR, '$f00');
$this->maniaControl->settingManager->initSetting($this, self::SETTING_FORMAT_USAGEINFO, '$f80');
}
2013-11-09 17:24:03 +01:00
/**
2014-05-02 17:40:47 +02:00
* Send an information message to the given login
*
2014-05-02 17:40:47 +02:00
* @param string $message
* @param string $login
2014-01-16 18:08:32 +01:00
* @param string|bool $prefix
2014-05-02 17:40:47 +02:00
* @return bool
2013-11-09 17:24:03 +01:00
*/
2014-05-02 17:40:47 +02:00
public function sendInformation($message, $login = null, $prefix = true) {
2014-05-13 16:03:26 +02:00
$format = $this->maniaControl->settingManager->getSettingValue($this, self::SETTING_FORMAT_INFORMATION);
2014-05-02 18:21:38 +02:00
return $this->sendChat($format . $message, $login, $prefix);
2013-11-09 17:24:03 +01:00
}
2014-05-02 17:40:47 +02:00
2013-11-09 17:24:03 +01:00
/**
* Send a chat message to the given login
*
2014-01-16 18:08:32 +01:00
* @param string $message
* @param string $login
* @param string|bool $prefix
* @return bool
2013-11-09 17:24:03 +01:00
*/
public function sendChat($message, $login = null, $prefix = true) {
2014-01-31 17:08:20 +01:00
if (!$this->maniaControl->client) {
return false;
2013-11-09 17:24:03 +01:00
}
2014-04-14 13:17:00 +02:00
if (!$login) {
2014-05-02 17:40:47 +02:00
$prefix = $this->getPrefix($prefix);
2014-04-30 16:57:49 +02:00
$chatMessage = '$<$z$ff0' . str_replace(' ', '', $prefix) . $prefix . $message . '$>';
2014-05-16 20:07:41 +02:00
$this->maniaControl->client->chatSendServerMessage($chatMessage);
2014-03-01 11:11:50 +01:00
} else {
2014-04-30 16:57:49 +02:00
$chatMessage = '$<$z$ff0' . $this->getPrefix($prefix) . $message . '$>';
2014-05-19 00:03:36 +02:00
$login = Player::parseLogin($login);
2014-05-02 17:40:47 +02:00
try {
2014-05-16 20:07:41 +02:00
$this->maniaControl->client->chatSendServerMessage($chatMessage, $login);
} catch (UnknownPlayerException $e) {
2014-03-02 11:25:47 +01:00
}
2013-11-09 17:24:03 +01:00
}
2014-01-31 17:08:20 +01:00
return true;
2013-11-09 17:24:03 +01:00
}
/**
2014-05-02 17:40:47 +02:00
* Get prefix
2013-11-09 17:24:03 +01:00
*
2014-01-16 18:08:32 +01:00
* @param string|bool $prefix
2014-05-02 17:40:47 +02:00
* @return string
2013-11-09 17:24:03 +01:00
*/
2014-05-02 17:40:47 +02:00
private function getPrefix($prefix) {
if (is_string($prefix)) {
return $prefix;
}
if ($prefix === true) {
2014-05-13 16:03:26 +02:00
return $this->maniaControl->settingManager->getSettingValue($this, self::SETTING_PREFIX);
2014-05-02 17:40:47 +02:00
}
return '';
2013-11-09 17:24:03 +01:00
}
/**
* Sends a Error Message to all Connected Admins
*
* @param $message
* @param int $minLevel (Constant from AuthenticationManager)
* @param bool $prefix
*/
public function sendErrorToAdmins($message, $minLevel = AuthenticationManager::AUTH_LEVEL_MODERATOR, $prefix = true) {
$format = $this->maniaControl->settingManager->getSettingValue($this, self::SETTING_FORMAT_ERROR);
$this->sendMessageToAdmins($format . $message, $prefix);
}
/**
* Sends a Message to all Connected Admins
*
* @param $message
* @param int $minLevel (Constant from AuthenticationManager)
* @param bool $prefix
*/
public function sendMessageToAdmins($message, $minLevel = AuthenticationManager::AUTH_LEVEL_MODERATOR, $prefix = true) {
//TODO specifiy in player or adminmanager a getAdmins() with minlevel function
foreach ($this->maniaControl->playerManager->getPlayers() as $player) {
/** @var Player $player */
if ($this->maniaControl->authenticationManager->checkRight($player, $minLevel)) {
$this->sendChat($message, $player->login, $prefix);
}
}
}
2013-11-09 17:24:03 +01:00
/**
* Send a success message to the given login
*
2014-01-16 18:08:32 +01:00
* @param string $message
* @param string $login
* @param string|bool $prefix
* @return bool
2013-11-09 17:24:03 +01:00
*/
public function sendSuccess($message, $login = null, $prefix = true) {
2014-05-13 16:03:26 +02:00
$format = $this->maniaControl->settingManager->getSettingValue($this, self::SETTING_FORMAT_SUCCESS);
2014-05-02 18:21:38 +02:00
return $this->sendChat($format . $message, $login, $prefix);
2013-11-09 17:24:03 +01:00
}
2014-05-02 17:40:47 +02:00
/**
* Send the Exception Information to the Chat
*
* @param \Exception $exception
* @param string $login
* @return bool
*/
public function sendException(\Exception $exception, $login = null) {
$message = "Exception occurred: '{$exception->getMessage()}' ({$exception->getCode()})";
return $this->sendError($message, $login);
}
2013-11-09 17:24:03 +01:00
/**
2014-04-14 13:17:00 +02:00
* Send an Error Message to the Chat
2013-11-09 17:24:03 +01:00
*
2014-01-16 18:08:32 +01:00
* @param string $message
* @param string $login
* @param string|bool $prefix
* @return bool
2013-11-09 17:24:03 +01:00
*/
public function sendError($message, $login = null, $prefix = true) {
2014-05-13 16:03:26 +02:00
$format = $this->maniaControl->settingManager->getSettingValue($this, self::SETTING_FORMAT_ERROR);
2014-05-02 18:21:38 +02:00
return $this->sendChat($format . $message, $login, $prefix);
2013-11-09 17:24:03 +01:00
}
/**
* Send an usage info message to the given login
*
2014-01-16 18:08:32 +01:00
* @param string $message
* @param string $login
* @param string|bool $prefix
* @return bool
*/
public function sendUsageInfo($message, $login = null, $prefix = false) {
2014-05-13 16:03:26 +02:00
$format = $this->maniaControl->settingManager->getSettingValue($this, self::SETTING_FORMAT_USAGEINFO);
2014-05-02 18:21:38 +02:00
return $this->sendChat($format . $message, $login, $prefix);
}
2013-11-09 17:24:03 +01:00
}