Class: Lich::Gemstone::Messaging

Inherits:
Object
  • Object
show all
Defined in:
documented/gemstone/creature.rb

Overview

Represents the messaging associated with a creature. This class manages various messages related to the creature’s actions.

Constant Summary collapse

PLACEHOLDER_MAP =
{
  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

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Messaging

Returns a new instance of Messaging.



711
712
713
714
715
# File 'documented/gemstone/creature.rb', line 711

def initialize(data)
  data.each do |key, value|
    instance_variable_set("@#{key}", normalize(value))
  end
end

Instance Attribute Details

#arrivalObject

Returns the value of attribute arrival.



700
701
702
# File 'documented/gemstone/creature.rb', line 700

def arrival
  @arrival
end

#attackObject

Returns the value of attribute attack.



700
701
702
# File 'documented/gemstone/creature.rb', line 700

def attack
  @attack
end

#biteObject

Returns the value of attribute bite.



700
701
702
# File 'documented/gemstone/creature.rb', line 700

def bite
  @bite
end

#clawObject

Returns the value of attribute claw.



700
701
702
# File 'documented/gemstone/creature.rb', line 700

def claw
  @claw
end

#deathObject

Returns the value of attribute death.



700
701
702
# File 'documented/gemstone/creature.rb', line 700

def death
  @death
end

#descriptionObject

Returns the value of attribute description.



700
701
702
# File 'documented/gemstone/creature.rb', line 700

def description
  @description
end

#enrageObject

Returns the value of attribute enrage.



700
701
702
# File 'documented/gemstone/creature.rb', line 700

def enrage
  @enrage
end

#fleeObject

Returns the value of attribute flee.



700
701
702
# File 'documented/gemstone/creature.rb', line 700

def flee
  @flee
end

#frenzyObject

Returns the value of attribute frenzy.



700
701
702
# File 'documented/gemstone/creature.rb', line 700

def frenzy
  @frenzy
end

#mstrikeObject

Returns the value of attribute mstrike.



700
701
702
# File 'documented/gemstone/creature.rb', line 700

def mstrike
  @mstrike
end

#spell_prepObject

Returns the value of attribute spell_prep.



700
701
702
# File 'documented/gemstone/creature.rb', line 700

def spell_prep
  @spell_prep
end

#sympathyObject

Returns the value of attribute sympathy.



700
701
702
# File 'documented/gemstone/creature.rb', line 700

def sympathy
  @sympathy
end

Instance Method Details

#display(field, subs = {}) ⇒ String

Displays a message for a specified field, substituting placeholders.

Parameters:

  • field (Symbol)

    The field to display the message for.

  • subs (Hash) (defaults to: {})

    Substitutions for placeholders in the message.

Returns:

  • (String)

    The formatted message.



733
734
735
736
737
738
739
740
741
742
# File 'documented/gemstone/creature.rb', line 733

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 string against the template’s regex.

Parameters:

  • str (String)

    The string to match.

  • literals (Hash)

    Literal values for placeholders.

Returns:

  • (Hash, nil)

    The matched data if successful, otherwise nil.



748
749
750
751
752
753
754
755
# File 'documented/gemstone/creature.rb', line 748

def match(field, str)
  msg = send(field)
  if msg.is_a?(PlaceholderTemplate)
    msg.match(str)
  else
    msg == str ? {} : nil
  end
end

#normalize(value) ⇒ Object



717
718
719
720
721
722
723
724
725
726
727
# File 'documented/gemstone/creature.rb', line 717

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