2013-11-09 17:24:03 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace ManiaControl;
|
2014-03-02 11:25:47 +01:00
|
|
|
use Maniaplanet\DedicatedServer\Xmlrpc\Exception;
|
2013-11-09 17:24:03 +01:00
|
|
|
|
|
|
|
/**
|
2013-11-10 13:46:44 +01:00
|
|
|
* Chat utility class
|
2013-11-09 17:24:03 +01:00
|
|
|
*
|
2013-11-10 13:46:44 +01:00
|
|
|
* @author steeffeen & kremsy
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
|
|
|
class Chat {
|
2013-12-03 18:03:16 +01:00
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
*/
|
2014-01-16 18:08:32 +01:00
|
|
|
const SETTING_PREFIX = 'Messages Prefix';
|
2013-12-03 18:03:16 +01:00
|
|
|
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';
|
|
|
|
|
2013-11-09 17:24:03 +01:00
|
|
|
/**
|
|
|
|
* Private properties
|
|
|
|
*/
|
2013-11-10 13:46:44 +01:00
|
|
|
private $maniaControl = null;
|
2013-11-09 17:24:03 +01:00
|
|
|
|
2013-11-10 13:46:44 +01:00
|
|
|
/**
|
|
|
|
* Construct chat utility
|
|
|
|
*
|
2014-01-16 18:08:32 +01:00
|
|
|
* @param ManiaControl $maniaControl
|
2013-11-10 13:46:44 +01:00
|
|
|
*/
|
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
|
|
|
$this->maniaControl = $maniaControl;
|
2014-01-16 18:08:32 +01:00
|
|
|
|
2013-12-03 18:03:16 +01:00
|
|
|
// Init settings
|
2013-12-08 22:38:20 +01:00
|
|
|
$this->maniaControl->settingManager->initSetting($this, self::SETTING_PREFIX, '» ');
|
2013-12-03 18:03:16 +01:00
|
|
|
$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-10 13:46:44 +01:00
|
|
|
}
|
2013-11-09 17:24:03 +01:00
|
|
|
|
|
|
|
/**
|
2013-11-10 13:46:44 +01:00
|
|
|
* Get prefix
|
|
|
|
*
|
2014-01-16 18:08:32 +01:00
|
|
|
* @param string|bool $prefix
|
2013-11-10 13:46:44 +01:00
|
|
|
* @return string
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2013-11-10 13:46:44 +01:00
|
|
|
private function getPrefix($prefix) {
|
2014-01-31 17:08:20 +01:00
|
|
|
if (is_string($prefix)) {
|
2013-11-10 13:46:44 +01:00
|
|
|
return $prefix;
|
|
|
|
}
|
2014-01-31 17:08:20 +01:00
|
|
|
if ($prefix === true) {
|
2013-12-03 18:03:16 +01:00
|
|
|
return $this->maniaControl->settingManager->getSetting($this, self::SETTING_PREFIX);
|
2013-11-10 13:46:44 +01:00
|
|
|
}
|
|
|
|
return '';
|
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
|
2013-11-10 13:46:44 +01:00
|
|
|
* @return bool
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2013-12-03 18:03:16 +01:00
|
|
|
public function sendChat($message, $login = null, $prefix = true) {
|
2014-01-31 17:08:20 +01:00
|
|
|
if (!$this->maniaControl->client) {
|
2013-11-10 13:46:44 +01:00
|
|
|
return false;
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
2013-12-03 18:03:16 +01:00
|
|
|
$chatMessage = '$z$<' . $this->getPrefix($prefix) . $message . '$>$z';
|
2014-03-01 11:11:50 +01:00
|
|
|
if ($login === null) {
|
|
|
|
$this->maniaControl->client->chatSendServerMessage($chatMessage);
|
|
|
|
} else {
|
2014-03-02 11:25:47 +01:00
|
|
|
try{
|
2014-03-01 11:11:50 +01:00
|
|
|
$this->maniaControl->client->chatSendServerMessage($chatMessage, $login);
|
2014-03-02 11:25:47 +01:00
|
|
|
} catch(Exception $e){
|
|
|
|
if($e->getMessage() != "Login unknown."){
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send an information message to the given login
|
|
|
|
*
|
2014-01-16 18:08:32 +01:00
|
|
|
* @param string $message
|
|
|
|
* @param string $login
|
|
|
|
* @param string|bool $prefix
|
2013-11-10 13:46:44 +01:00
|
|
|
* @return bool
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2013-12-03 18:03:16 +01:00
|
|
|
public function sendInformation($message, $login = null, $prefix = true) {
|
|
|
|
$format = $this->maniaControl->settingManager->getSetting($this, self::SETTING_FORMAT_INFORMATION);
|
2013-11-09 17:24:03 +01:00
|
|
|
return $this->sendChat($format . $message, $login);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2013-11-10 13:46:44 +01:00
|
|
|
* @return bool
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2013-12-03 18:03:16 +01:00
|
|
|
public function sendSuccess($message, $login = null, $prefix = true) {
|
|
|
|
$format = $this->maniaControl->settingManager->getSetting($this, self::SETTING_FORMAT_SUCCESS);
|
2013-11-09 17:24:03 +01:00
|
|
|
return $this->sendChat($format . $message, $login);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send an error message to the given login
|
|
|
|
*
|
2014-01-16 18:08:32 +01:00
|
|
|
* @param string $message
|
|
|
|
* @param string $login
|
|
|
|
* @param string|bool $prefix
|
2013-11-10 13:46:44 +01:00
|
|
|
* @return bool
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2013-12-03 18:03:16 +01:00
|
|
|
public function sendError($message, $login = null, $prefix = true) {
|
|
|
|
$format = $this->maniaControl->settingManager->getSetting($this, self::SETTING_FORMAT_ERROR);
|
2013-11-09 17:24:03 +01:00
|
|
|
return $this->sendChat($format . $message, $login);
|
|
|
|
}
|
2013-11-13 01:43:12 +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
|
2013-11-13 01:43:12 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function sendUsageInfo($message, $login = null, $prefix = false) {
|
2013-12-03 18:03:16 +01:00
|
|
|
$format = $this->maniaControl->settingManager->getSetting($this, self::SETTING_FORMAT_USAGEINFO);
|
2013-11-13 01:43:12 +01:00
|
|
|
return $this->sendChat($format . $message, $login);
|
|
|
|
}
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|