Class: Lich::Gemstone::Messaging
- Inherits:
-
Object
- Object
- Lich::Gemstone::Messaging
- Defined in:
- documented/gemstone/creature.rb
Overview
Creature messaging templates for contextual creature descriptions.
Stores and renders dynamic messaging for creatures: arrival/death text, spell casting, combat actions, and creature sounds. Supports placeholder substitution (pronouns, directions, weapon types) and random variation via arrays of alternatives.
Constant Summary collapse
- PLACEHOLDER_MAP =
Placeholder substitution options for message templates.
Maps placeholder names (used in curly braces within messaging templates) to their allowed values. Capitalized variants (e.g. Pronoun) use title case; lowercase variants use lowercase. RAW: values are regex patterns matched without escaping.
{ Pronoun: %w[He Her His It She], pronoun: %w[he her his it she], direction: %w[north south east west up down northeast northwest southeast southwest], weapon: %w[RAW:.+?] }
Instance Attribute Summary collapse
-
#arrival ⇒ Object
Returns the value of attribute arrival.
-
#attack ⇒ Object
Returns the value of attribute attack.
-
#bite ⇒ Object
Returns the value of attribute bite.
-
#claw ⇒ Object
Returns the value of attribute claw.
-
#death ⇒ Object
Returns the value of attribute death.
-
#description ⇒ Object
Returns the value of attribute description.
-
#enrage ⇒ Object
Returns the value of attribute enrage.
-
#flee ⇒ Object
Returns the value of attribute flee.
-
#frenzy ⇒ Object
Returns the value of attribute frenzy.
-
#mstrike ⇒ Object
Returns the value of attribute mstrike.
-
#spell_prep ⇒ Object
Returns the value of attribute spell_prep.
-
#sympathy ⇒ Object
Returns the value of attribute sympathy.
Instance Method Summary collapse
-
#display(field, subs = {}) ⇒ String, Object
Renders a messaging field with optional placeholder substitutions.
-
#initialize(data) ⇒ Messaging
constructor
A new instance of Messaging.
-
#match(field, str) ⇒ Hash?
Matches a messaging field against a string and returns captured groups.
-
#normalize(value) ⇒ PlaceholderTemplate, ...
Recursively normalizes a messaging value into a renderable form.
Constructor Details
#initialize(data) ⇒ Messaging
Returns a new instance of Messaging.
701 702 703 704 705 |
# File 'documented/gemstone/creature.rb', line 701 def initialize(data) data.each do |key, value| instance_variable_set("@#{key}", normalize(value)) end end |
Instance Attribute Details
#arrival ⇒ Object
Returns the value of attribute arrival.
677 678 679 |
# File 'documented/gemstone/creature.rb', line 677 def arrival @arrival end |
#attack ⇒ Object
Returns the value of attribute attack.
677 678 679 |
# File 'documented/gemstone/creature.rb', line 677 def attack @attack end |
#bite ⇒ Object
Returns the value of attribute bite.
677 678 679 |
# File 'documented/gemstone/creature.rb', line 677 def bite @bite end |
#claw ⇒ Object
Returns the value of attribute claw.
677 678 679 |
# File 'documented/gemstone/creature.rb', line 677 def claw @claw end |
#death ⇒ Object
Returns the value of attribute death.
677 678 679 |
# File 'documented/gemstone/creature.rb', line 677 def death @death end |
#description ⇒ Object
Returns the value of attribute description.
677 678 679 |
# File 'documented/gemstone/creature.rb', line 677 def description @description end |
#enrage ⇒ Object
Returns the value of attribute enrage.
677 678 679 |
# File 'documented/gemstone/creature.rb', line 677 def enrage @enrage end |
#flee ⇒ Object
Returns the value of attribute flee.
677 678 679 |
# File 'documented/gemstone/creature.rb', line 677 def flee @flee end |
#frenzy ⇒ Object
Returns the value of attribute frenzy.
677 678 679 |
# File 'documented/gemstone/creature.rb', line 677 def frenzy @frenzy end |
#mstrike ⇒ Object
Returns the value of attribute mstrike.
677 678 679 |
# File 'documented/gemstone/creature.rb', line 677 def mstrike @mstrike end |
#spell_prep ⇒ Object
Returns the value of attribute spell_prep.
677 678 679 |
# File 'documented/gemstone/creature.rb', line 677 def spell_prep @spell_prep end |
#sympathy ⇒ Object
Returns the value of attribute sympathy.
677 678 679 |
# File 'documented/gemstone/creature.rb', line 677 def sympathy @sympathy end |
Instance Method Details
#display(field, subs = {}) ⇒ String, Object
Renders a messaging field with optional placeholder substitutions.
Retrieves the named field (e.g. :description, :arrival) and renders it: if it is a PlaceholderTemplate, substitutes placeholders (falling back to random options); if it is an array, joins each rendered element with newlines; otherwise returns the plain value.
742 743 744 745 746 747 748 749 750 751 |
# File 'documented/gemstone/creature.rb', line 742 def display(field, subs = {}) msg = send(field) if msg.is_a?(Array) msg.map { |m| m.is_a?(PlaceholderTemplate) ? m.to_display(subs) : m }.join("\n") elsif msg.is_a?(PlaceholderTemplate) msg.to_display(subs) else msg end end |
#match(field, str) ⇒ Hash?
Matches a messaging field against a string and returns captured groups.
If the field is a PlaceholderTemplate, converts it to a regex and matches; returns captured placeholders as a hash, or nil if no match. For non-template fields, returns an empty hash if the field equals the string, nil otherwise.
767 768 769 770 771 772 773 774 |
# File 'documented/gemstone/creature.rb', line 767 def match(field, str) msg = send(field) if msg.is_a?(PlaceholderTemplate) msg.match(str) else msg == str ? {} : nil end end |
#normalize(value) ⇒ PlaceholderTemplate, ...
Recursively normalizes a messaging value into a renderable form.
Detects and converts placeholder templates (strings containing name patterns) into PlaceholderTemplate objects. Arrays are recursively normalized. Plain strings and other values pass through unchanged.
716 717 718 719 720 721 722 723 724 725 726 |
# File 'documented/gemstone/creature.rb', line 716 def normalize(value) if value.is_a?(Array) value.map { |v| normalize(v) } elsif value.is_a?(String) && value.match?(/\{[a-zA-Z_]+\}/) phs = value.scan(/\{([a-zA-Z_]+)\}/).flatten.map(&:to_sym) placeholders = phs.map { |ph| [ph, PLACEHOLDER_MAP[ph] || []] }.to_h PlaceholderTemplate.new(value, placeholders) else value end end |