diff --git a/core/Chat.php b/core/Chat.php index f058d4b1..61493482 100644 --- a/core/Chat.php +++ b/core/Chat.php @@ -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 * diff --git a/plugins/MCTeam/LocalRecordsPlugin.php b/plugins/MCTeam/LocalRecordsPlugin.php index ab6808d7..5d8dafec 100644 --- a/plugins/MCTeam/LocalRecordsPlugin.php +++ b/plugins/MCTeam/LocalRecordsPlugin.php @@ -43,7 +43,7 @@ class LocalRecordsPlugin implements CallbackListener, CallQueueListener, Command * Constants */ const ID = 7; - const VERSION = 0.81; + const VERSION = 0.82; const NAME = 'Local Records Plugin'; const AUTHOR = 'MCTeam'; const MLID_RECORDS = 'ml_local_records'; @@ -516,7 +516,7 @@ class LocalRecordsPlugin implements CallbackListener, CallQueueListener, Command $checkpointsString = $this->getCheckpoints($player->login); $this->checkpoints[$player->login] = array(); - $message = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_NOTIFICATION_MESSAGE_PREFIX); + $messagePrefix = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_NOTIFICATION_MESSAGE_PREFIX); $notifyPrivatelyAt = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_NOTIFY_BEST_RECORDS_PRIVATE); $notifyPubliclyAt = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_NOTIFY_BEST_RECORDS_PUBLIC); @@ -528,13 +528,13 @@ class LocalRecordsPlugin implements CallbackListener, CallQueueListener, Command return; } else if ($oldRecord->time == $time) { // Same time - if ($notifyPubliclyAt < $oldRecord->rank && $oldRecord->rank <= $notifyPrivatelyAt) { - $message .= 'You'; - } else { - $message .= '$<$fff' . $player->nickname . '$>'; - } - $message .= ' equalized the $<$ff0' . $oldRecord->rank . '.$> Local Record:'; - $message .= ' $<$fff' . Formatter::formatTime($oldRecord->time) . '$>!'; + $isPM = ($notifyPubliclyAt < $oldRecord->rank && $oldRecord->rank <= $notifyPrivatelyAt); + $message = $this->maniaControl->getChat()->formatMessage( + $messagePrefix . '%s equalized the %s Local Record: %s!', + ($isPM ? 'You' : $player), + '$ff0' . $oldRecord->rank . '.', + Formatter::formatTime($oldRecord->time) + ); if ($oldRecord->rank <= $notifyPubliclyAt) { $this->maniaControl->getCallQueueManager()->registerListening( @@ -581,21 +581,20 @@ class LocalRecordsPlugin implements CallbackListener, CallQueueListener, Command $newRecord = $this->getLocalRecord($map, $player); $improvedRank = ($oldRecord && $newRecord->rank >= $oldRecord->rank); - if ($notifyPubliclyAt < $newRecord->rank && $newRecord->rank <= $notifyPrivatelyAt) { - $message .= 'You'; - } else { - $message .= '$<$fff' . $player->nickname . '$>'; - } - $message .= ' ' . ($improvedRank ? 'improved' : 'gained') . ' the'; - $message .= ' $<$ff0' . $newRecord->rank . '.$> Local Record:'; - $message .= ' $<$fff' . Formatter::formatTime($newRecord->time) . '$>!'; + $isPM = ($notifyPubliclyAt < $newRecord->rank && $newRecord->rank <= $notifyPrivatelyAt); + $message = $this->maniaControl->getChat()->formatMessage( + $messagePrefix . '%s ' . ($improvedRank ? 'improved' : 'gained') . ' the %s Local Record: %s!', + ($isPM ? 'You' : $player), + '$ff0' . $newRecord->rank . '.', + Formatter::formatTime($newRecord->time) + ); if ($oldRecord) { - $message .= ' ('; - if ($improvedRank) { - $message .= '$<$ff0' . $oldRecord->rank . '.$> '; - } $timeDiff = $oldRecord->time - $newRecord->time; - $message .= '$<$fff-' . Formatter::formatTime($timeDiff) . '$>)'; + $message .= $this->maniaControl->getChat()->formatMessage( + ' (%s%s)', + ($improvedRank ? '$ff0'.$oldRecord->rank.'. ' : ''), + '-'.Formatter::formatTime($timeDiff) + ); } if ($newRecord->rank <= $notifyPubliclyAt) { @@ -710,7 +709,11 @@ class LocalRecordsPlugin implements CallbackListener, CallQueueListener, Command $commandParts = explode(' ', $chat[1][2]); if (count($commandParts) < 2 || strlen($commandParts[1]) == 0) { - $this->maniaControl->getChat()->sendUsageInfo('Missing CSV-Filename ID! (Example: //exportrecs locals.csv)', $player); + $message = $this->maniaControl->getChat()->formatMessage( + 'Missing CSV-Filename! (Example %s)', + '//exportrecs locals.csv' + ); + $this->maniaControl->getChat()->sendUsageInfo($message, $player); return; } @@ -758,7 +761,12 @@ class LocalRecordsPlugin implements CallbackListener, CallQueueListener, Command $this->maniaControl->getChat()->sendException($e, $player); } - $this->maniaControl->getChat()->sendSuccess('Successfully exported Local Records of map ' . $map->getEscapedName() . ' to $<$fff' . $filename . '$>!'); + $message = $this->maniaControl->getChat()->formatMessage( + 'Exported Local Records of %s to %s!', + $map, + $filename + ); + $this->maniaControl->getChat()->sendSuccess($message, $player); } /** @@ -872,7 +880,11 @@ class LocalRecordsPlugin implements CallbackListener, CallQueueListener, Command $commandParts = explode(' ', $chat[1][2]); if (count($commandParts) < 2 || strlen($commandParts[1]) == 0) { - $this->maniaControl->getChat()->sendUsageInfo('Missing Record ID! (Example: //delrec 3)', $player); + $message = $this->maniaControl->getChat()->formatMessage( + 'Missing Record ID! (Example: %s)', + '//delrec 3' + ); + $this->maniaControl->getChat()->sendUsageInfo($message, $player); return; } @@ -880,13 +892,26 @@ class LocalRecordsPlugin implements CallbackListener, CallQueueListener, Command $currentMap = $this->maniaControl->getMapManager()->getCurrentMap(); $records = $this->getLocalRecords($currentMap); if ($recordRank <= 0 || count($records) < $recordRank) { - $this->maniaControl->getChat()->sendError('Cannot remove record $<$fff' . $recordRank . '$>!', $player); + $message = $this->maniaControl->getChat()->formatMessage( + 'Cannot remove record no. %s, does not exist!', + $recordRank + ); + $this->maniaControl->getChat()->sendError($message, $player); return; } assert($recordRank == $records[$recordRank-1]->rank); $playerIndex = $records[$recordRank-1]->playerIndex; - $playerNick = $records[$recordRank-1]->nickname; + $recordPlayer = $this->maniaControl->getPlayerManager()->getPlayerByIndex($playerIndex); + if (!$recordPlayer) { + // should never happen, but you never know + $message = $this->maniaControl->getChat()->formatMessage( + 'Cannot remove record no. %s, player does not exist!', + $recordRank + ); + $this->maniaControl->getChat()->sendError($message, $player); + return; + } $mysqli = $this->maniaControl->getDatabase()->getMysqli(); $query = "DELETE FROM `" . self::TABLE_RECORDS . "` @@ -899,7 +924,12 @@ class LocalRecordsPlugin implements CallbackListener, CallQueueListener, Command } $this->maniaControl->getCallbackManager()->triggerCallback(self::CB_LOCALRECORDS_CHANGED, null); - $this->maniaControl->getChat()->sendSuccess('Record no. $<$fff' . $recordRank . '$> by $<$fff' . $playerNick . '$> has been removed!'); + $message = $this->maniaControl->getChat()->formatMessage( + 'Record no. %s by %s has been removed!', + $recordRank, + $recordPlayer + ); + $this->maniaControl->getChat()->sendSuccess($message); } /** @@ -941,7 +971,11 @@ class LocalRecordsPlugin implements CallbackListener, CallQueueListener, Command } $this->maniaControl->getCallbackManager()->triggerCallback(self::CB_LOCALRECORDS_CHANGED, null); - $this->maniaControl->getChat()->sendSuccess('$<$fff'.$player->getEscapedNickname().'$> removed his personal record!'); + $message = $this->maniaControl->getChat()->formatMessage( + '%s removed his personal record!', + $player + ); + $this->maniaControl->getChat()->sendSuccess($message); } /**