Module: Lich::Messaging

Defined in:
documented/messaging.rb

Class Method Summary collapse

Class Method Details

Wraps link text in a clickable command link for frontends that support it.

Returns the link formatted as "cmd" type via .msg_format; on basic frontends without link support, returns the link_text only.

Examples:

Lich::Messaging.make_cmd_link("cast", "cast 1") #=> "<d cmd='cast 1'>cast</d>"

Parameters:

  • link_text (String) —

    the displayed text of the link

  • link_action (String) —

    the command to execute when the link is clicked

  • encode (Boolean) (defaults to: true) —

    whether to XML-encode the link text; defaults to true

Returns:

  • (String) —

    the formatted command link



188
189
190
# File 'documented/messaging.rb', line 188

def self.make_cmd_link(link_text, link_action, encode: true)
  return msg_format("cmd", link_text, cmd_link: link_action, encode: encode)
end

.mono(msg, encode: false) ⇒ nil

defaulting encoding here to false instead of true like other methods due to backwards compatibility that it never encoded before whereas all the other methods were already encoding and therefor should default to allow for them.

Returns:

  • (nil)

Raises:

  • (StandardError) —

    when msg is not a String



197
198
199
200
201
202
203
204
205
# File 'documented/messaging.rb', line 197

def self.mono(msg, encode: false)
  return raise StandardError.new 'Lich::Messaging.mono only works with String parameters!' unless msg.is_a?(String)
  msg = xml_encode(msg) if encode
  if Frontend.supports_mono?
    _respond "<output class=\"mono\"/>\n" + msg + "\n<output class=\"\"/>"
  else
    _respond msg.split("\n")
  end
end

.monsterbold(msg, encode: true) ⇒ String

Formats a message in monsterbold style (bold/error color).

Applies the appropriate preset color markup based on frontend capabilities.

Examples:

Lich::Messaging.monsterbold("CRITICAL") #=> "<pushBold/>CRITICAL<popBold/>"

Parameters:

  • msg (String) —

    the message to format

  • encode (Boolean) (defaults to: true) —

    whether to XML-encode the message; defaults to true

Returns:

  • (String) —

    the formatted message with bold/error color markup



35
36
37
38
# File 'documented/messaging.rb', line 35

def self.monsterbold(msg, encode: true)
  # return monsterbold_start + self.xml_encode(msg) + monsterbold_end
  return msg_format("monster", msg, encode: encode)
end

.msg(type = "info", msg = "", encode: true) ⇒ nil

Sends msg to the frontends in the preset style for type. A debug message is sent only while debug messaging is on.

Returns:

  • (nil) —

    always, so a method whose last expression is Lich::Messaging.msg returns nil: test for success before it, as in return data if found; Lich::Messaging.msg("bold", "not found")



172
173
174
175
# File 'documented/messaging.rb', line 172

def self.msg(type = "info", msg = "", encode: true)
  return if type == "debug" && (Lich.debug_messaging.nil? || Lich.debug_messaging == "false" || Lich.debug_messaging == false)
  _respond msg_format(type, msg, encode: encode)
end

.msg_format(type = "info", msg = "", cmd_link: nil, encode: true) ⇒ String

Wraps a message with preset color markup appropriate to its type and frontend.

Supports preset types (error, info, warn, green, etc.) for XML and GSL frontends, with fallback text prefixes for basic frontends. Handles clickable command links via the "cmd" type when cmd_link is provided.

Examples:

Format an error message

Lich::Messaging.msg_format("error", "Not found") #=> "<pushBold/>Not found<popBold/>"

Create a clickable command link (XML frontend)

Lich::Messaging.msg_format("cmd", "cast", cmd_link: "cast 1") #=> "<d cmd='cast 1'>cast</d>"

Parameters:

  • type (String) (defaults to: "info") —

    the message type: "error", "warn", "info", "green", "cmd", "link", etc.; defaults to "info"

  • msg (String) (defaults to: "") —

    the message text; defaults to empty string

  • cmd_link (String, nil) (defaults to: nil) —

    for type "cmd", the action string for the clickable link

  • encode (Boolean) (defaults to: true) —

    whether to XML-encode the message; defaults to true

Returns:

  • (String) —

    the message wrapped with preset color markup or prefixes



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'documented/messaging.rb', line 89

def self.msg_format(type = "info", msg = "", cmd_link: nil, encode: true)
  msg = xml_encode(msg) if encode
  preset_color_before = ""
  preset_color_after = ""

  wizard_color = { "white" => 128, "black" => 129, "dark blue" => 130, "dark green" => 131, "dark teal" => 132,
    "dark red" => 133, "purple" => 134, "gold" => 135, "light grey" => 136, "blue" => 137,
    "bright green" => 138, "teal" => 139, "red" => 140, "pink" => 141, "yellow" => 142 }

  if Frontend.supports_xml?
    case type
    when "error", "yellow", "bold", "monster", "creature"
      preset_color_before = monsterbold_start
      preset_color_after = monsterbold_end
    when "warn", "orange", "gold", "thought"
      preset_color_before = "<preset id='thought'>"
      preset_color_after = "</preset>"
    when "info", "teal", "whisper"
      preset_color_before = "<preset id='whisper'>"
      preset_color_after = "</preset>"
    when "green", "speech", "debug", "light green"
      preset_color_before = "<preset id='speech'>"
      preset_color_after = "</preset>"
    when "link", "command", "selectedLink", "watching", "roomName"
      preset_color_before = ""
      preset_color_after = ""
    when "cmd"
      preset_color_before = "<d cmd='#{xml_encode(cmd_link)}'>"
      preset_color_after = "</d>"
    end
  elsif Frontend.supports_gsl?
    case type
    when "error", "yellow", "bold", "monster", "creature"
      preset_color_before = monsterbold_start
      preset_color_after = (monsterbold_end + " ")
    when "warn", "orange", "gold", "thought"
      preset_color_before = wizard_color["gold"].chr.force_encoding(Encoding::ASCII_8BIT)
      preset_color_after = "\240".force_encoding(Encoding::ASCII_8BIT)
    when "info", "teal", "whisper"
      preset_color_before = wizard_color["teal"].chr.force_encoding(Encoding::ASCII_8BIT)
      preset_color_after = "\240".force_encoding(Encoding::ASCII_8BIT)
    when "green", "speech", "debug", "light green"
      preset_color_before = wizard_color["bright green"].chr.force_encoding(Encoding::ASCII_8BIT)
      preset_color_after = "\240".force_encoding(Encoding::ASCII_8BIT)
    when "link", "command", "selectedLink", "watching", "roomName"
      preset_color_before = ""
      preset_color_after = ""
    when "cmd" # these browsers can't handle links
      preset_color_before = ""
      preset_color_after = ""
    end
  else
    case type
    when "error", "yellow", "bold", "monster", "creature"
      preset_color_before = monsterbold_start
      preset_color_after = monsterbold_end
    when "warn", "orange", "gold", "thought"
      preset_color_before = "!! "
      preset_color_after = ""
    when "info", "teal", "whisper"
      preset_color_before = "-- "
      preset_color_after = ""
    when "green", "speech", "debug", "light green"
      preset_color_before = ">> "
      preset_color_after = ""
    when "link", "command", "selectedLink", "watching", "roomName"
      preset_color_before = ""
      preset_color_after = ""
    when "cmd" # these browsers can't handle links
      preset_color_before = ""
      preset_color_after = ""
    end
  end

  return (preset_color_before + msg + preset_color_after)
end

.stream_window(msg, window = "familiar", encode: true) ⇒ nil

Sends msg to a stream window, or to the main window where the frontend can't show that window. There, familiar is wrapped in the GSL control pair (\034GSe / \034GSf), and thoughts and voln in game-style text.

Returns:

  • (nil)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'documented/messaging.rb', line 45

def self.stream_window(msg, window = "familiar", encode: true)
  msg = xml_encode(msg) if encode
  if XMLData.game =~ /^GS/
    allowed_streams = ["familiar", "speech", "thoughts", "loot", "voln"]
  elsif XMLData.game =~ /^DR/
    allowed_streams = ["familiar", "speech", "thoughts", "combat"]
  end

  stream_window_before_txt = ""
  stream_window_after_txt = ""
  if Frontend.supports_streams? && allowed_streams.include?(window)
    stream_window_before_txt = "<pushStream id=\"#{window}\" ifClosedStyle=\"watching\"/>"
    stream_window_after_txt = "\r\n<popStream/>\r\n"
  else
    if window =~ /familiar/i
      stream_window_before_txt = "\034GSe\r\n"
      stream_window_after_txt = "\r\n\034GSf\r\n"
    elsif window =~ /thoughts/i
      stream_window_before_txt = "You hear the faint thoughts of LICH-MESSAGE echo in your mind:\r\n"
      stream_window_after_txt = ""
    elsif window =~ /voln/i
      stream_window_before_txt = %{The Symbol of Thought begins to burn in your mind and you hear LICH-MESSAGE thinking, "}
      stream_window_after_txt = %{"\r\n}
    end
  end

  _respond stream_window_before_txt + msg + stream_window_after_txt
end

.xml_encode(msg) ⇒ String

Encodes a message string for XML display in frontends.

When the frontend supports GSL, applies XML encoding and passes the result through sf_to_wiz for GSL control pair handling; bypasses multiline and ensures a string is always returned (never nil). Otherwise, applies standard XML encoding.

Examples:

Lich::Messaging.xml_encode("You see a <troll>.") #=> "You see a &lt;troll&gt;."

Parameters:

  • msg (String) —

    the message to encode

Returns:

  • (String) —

    the XML-encoded message, or empty string if nil from sf_to_wiz



18
19
20
21
22
23
24
# File 'documented/messaging.rb', line 18

def self.xml_encode(msg)
  if Frontend.supports_gsl?
    sf_to_wiz(msg.encode(:xml => :text), bypass_multiline: true) || "" # sf_to_wiz returns nil when blank/new line only, which causes issue for messaging, always return string if nil
  else
    msg.encode(:xml => :text)
  end
end