Skip to content
TheUnknownOne edited this page Jul 9, 2013 · 2 revisions

Scripting/Server/Colored Text

PO provides some functions to send messages using scripts. However it's either plain black colored text (possible with some modifiers for messages with ~~~ at the beginning) or using HTML directly. It could be useful to print messages with different colors for different types of "system" messages, however using HTML by hand every time isn't exactly the most pleasant way to do it.

HTML escaping

First we need to escape &, < and > in messages themselves as they have special meaning in HTML.

function html_escape(text) {
    var m = (text || "").toString();

    if (m.length > 0) {
        return m.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
    } else {
        return "";
    }
}

Message types

You probably don't want to remember which color means something like warning, error, or plain system message. It is better to have global variables that will hold this data. It makes it easier to change later, too.

This is a helper that will be used to construct a class of message type. color is a value suitable for HTML color (#3830A2, red, etc.). symbol is a special symbol (a string actually) that will be placed before message so different message types look different. Symbol is a part of HTML code, so you can use &amp;#10071; for example, or a symbol it represents directly (unless it's one of &, >, or <). color_all and symbol_all are used for messages sent to all players instead.

// if color_all and symbol_all are not specified color and symbol are used.
function POMessageType(color, symbol, color_all, symbol_all) {
    this.color = color;
    this.symbol = symbol;

    if (color_all) {
        this.color_all = color_all;
    } else {
        this.color_all = color;
    }

    if (symbol_all) {
        this.symbol_all = symbol_all;
    } else {
        this.symbol_all = symbol;
    }
}

You can define message types now like this:

var MESSAGE_INFORMATION = new POMessageType("#3830A2", "&#10070;", "#268885", "&#9432;");
var MESSAGE_WARNING = new POMessageType("#BF9030", "&#10071;", "#BC150F", "&#10007;");
var MESSAGE_IMPORTANT = new POMessageType("blue", "&#9775;");
var MESSAGE_ME = new POMessageType("#A144AF", "&#9835;");

Send message functions

You can use following functions to send colored messages. send_message sends to a specified player, while send_message_all sends to all players on server or channel.

  • type - should be one of MESSAGE_* variables defined above.
  • target - id of a player who will receive a message.
  • message - message string (will be HTML escaped automatically).
  • channel_id - optional. If specified will limit a message to specified channel.
function send_message(type, target, message, channel_id) {
    if (typeof(channel_id) == "undefined") {
        sys.sendHtmlMessage(
            target,
            "<span style='color: " + type.color + ";'> "
             + type.symbol + " " + html_escape(message) + "</span>"
        );
    } else {
        sys.sendHtmlMessage(
            target,
             "<span style='color: " + type.color + ";'> "
             + type.symbol + " " + html_escape(message) + "</span>",
            channel_id
        );
    }
}

function send_message_all(type, message, channel_id) {
    if (typeof(channel_id) == "undefined") {
        sys.sendHtmlAll(
            "<span style='color: " + type.color_all + ";'> "
             + type.symbol_all + " " + html_escape(message) + "</span>"
        );
    } else {
        sys.sendHtmlAll(
            "<span style='color: " + type.color_all + ";'> "
             + type.symbol_all + " " + html_escape(message) + "</span>",
             channel_id
        );
    }
}

Example

send_message_all(MESSAGE_ME, "test user: some test message");