Added formatMessage to Chat, and used it in LocalRecordsPlugin first

This commit is contained in:
Alexander Nell
2020-05-26 17:49:29 +02:00
parent 1071c46b5b
commit cb84df401a
2 changed files with 115 additions and 36 deletions

View File

@ -10,7 +10,9 @@ use ManiaControl\Communication\CommunicationListener;
use ManiaControl\Communication\CommunicationMethods;
use ManiaControl\General\UsageInformationAble;
use ManiaControl\General\UsageInformationTrait;
use ManiaControl\Maps\Map;
use ManiaControl\Players\Player;
use ManiaControl\Utils\Formatter;
use Maniaplanet\DedicatedServer\Xmlrpc\UnknownPlayerException;
/**
@ -26,13 +28,16 @@ class Chat implements CallbackListener, CommunicationListener, UsageInformationA
/*
* Constants
*/
const SETTING_FORMAT_ERROR = 'Error Format';
const SETTING_FORMAT_INFORMATION = 'Information Format';
const SETTING_FORMAT_SUCCESS = 'Success Format';
const SETTING_FORMAT_USAGEINFO = 'UsageInfo Format';
const SETTING_PUBLIC_PREFIX = 'Public Messages Prefix';
const SETTING_PRIVATE_PREFIX = 'Privat Messages Prefix';
const CHAT_BUFFER_SIZE = 200;
const SETTING_FORMAT_ERROR = 'Error Format';
const SETTING_FORMAT_INFORMATION = 'Information Format';
const SETTING_FORMAT_SUCCESS = 'Success Format';
const SETTING_FORMAT_USAGEINFO = 'UsageInfo Format';
const SETTING_FORMAT_MESSAGE_INPUT_COLOR = 'Format Message Input Color';
const SETTING_FORMAT_MESSAGE_MAP_AUTHOR = 'Format Message Add Map Author';
const SETTING_FORMAT_MESSAGE_PLAYER_LOGIN = 'Format Message Add Player Login';
const SETTING_PUBLIC_PREFIX = 'Public Messages Prefix';
const SETTING_PRIVATE_PREFIX = 'Private Messages Prefix';
const CHAT_BUFFER_SIZE = 200;
/*
* Private properties
@ -54,6 +59,9 @@ class Chat implements CallbackListener, CommunicationListener, UsageInformationA
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_FORMAT_INFORMATION, '$fff');
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_FORMAT_SUCCESS, '$0f0');
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_FORMAT_USAGEINFO, '$f80');
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_FORMAT_MESSAGE_INPUT_COLOR, '$fff');
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_FORMAT_MESSAGE_MAP_AUTHOR, false);
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_FORMAT_MESSAGE_PLAYER_LOGIN, false);
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_PUBLIC_PREFIX, '» ');
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_PRIVATE_PREFIX, '»» ');
@ -156,6 +164,43 @@ class Chat implements CallbackListener, CommunicationListener, UsageInformationA
return new CommunicationAnswer();
}
/**
* Format the given message with the given inputs and colors the inputs.
* @param string $message
* @param mixed ...$inputs
* @return string
*/
public function formatMessage($message, ...$inputs) {
$addMapAuthor = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_FORMAT_MESSAGE_MAP_AUTHOR);
$addPlayerLogin = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_FORMAT_MESSAGE_PLAYER_LOGIN);
$formatInputColor = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_FORMAT_MESSAGE_INPUT_COLOR);
$formattedInputs = array($message);
foreach ($inputs as $input) {
$strInput = null;
if (is_bool($input)) {
$strInput = $input ? 'true' : 'false';
} elseif ($input instanceof Map) {
$strInput = $input->getEscapedName();
if ($addMapAuthor) {
$strInput .= " (by {$input->authorLogin})";
}
} elseif ($input instanceof Player) {
$strInput = $input->getEscapedNickname();
if ($addPlayerLogin) {
$strInput .= " ({$input->login})";
}
} else {
$strInput = strval($input);
}
array_push($formattedInputs, Formatter::escapeText($formatInputColor . $strInput));
}
return call_user_func_array('sprintf', $formattedInputs);
}
/**
* Stores the ChatMessage in the Buffer
*