Module: Lich::Gemstone::Combat::Parser

Defined in:
documented/gemstone/combat/parser.rb

Constant Summary collapse

/<a exist="(?<id>[^"]+)" noun="(?<noun>[^"]+)">(?<name>[^<]+)<\/a>/i.freeze
BOLD_WRAPPER_PATTERN =

Examples:

BOLD_WRAPPER_PATTERN.match("<pushBold/><a exist=\"123\" noun=\"creature\">Goblin</a><popBold/>")
# => #<MatchData "<pushBold/><a exist=\"123\" noun=\"creature\">Goblin</a><popBold/>" ...>

See Also:

  • #extract_creature_target
/<pushBold\/>([^<]*<a exist="[^"]+"[^>]+>[^<]+<\/a>)<popBold\/>/i.freeze

Class Method Summary collapse

Class Method Details

.extract_creature_target(line) ⇒ Hash?

Extracts a creature target from a line containing bolded links.

Examples:

extract_creature_target("<pushBold/><a exist=\"123\" noun=\"creature\">Goblin</a><popBold/>")
# => { id: 123, noun: "creature", name: "Goblin" }

Parameters:

  • line (String)

    the line containing the target information.

Returns:

  • (Hash, nil)

    the extracted target information or nil if not found.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'documented/gemstone/combat/parser.rb', line 117

def extract_creature_target(line)
  # Check if line contains a bolded link
  bold_match = BOLD_WRAPPER_PATTERN.match(line)
  return nil unless bold_match

  # Extract the link from within the bold tags
  link_text = bold_match[1]
  link_match = TARGET_LINK_PATTERN.match(link_text)
  return nil unless link_match

  id = link_match[:id].to_i
  return nil if id <= 0 # Skip invalid IDs

  {
    id: id,
    noun: link_match[:noun],
    name: link_match[:name]
  }
end

.extract_target_from_line(line) ⇒ Hash?

Extracts target information from a line, only accepting bolded creatures.

Examples:

extract_target_from_line("<pushBold/><a exist=\"123\" noun=\"creature\">Goblin</a><popBold/>")
# => { id: 123, noun: "creature", name: "Goblin" }

Parameters:

  • line (String)

    the line containing the target information.

Returns:

  • (Hash, nil)

    the extracted target information or nil if not found.



171
172
173
174
175
# File 'documented/gemstone/combat/parser.rb', line 171

def extract_target_from_line(line)
  # ONLY accept bolded creatures as targets
  # Non-bolded links are equipment, objects, or other non-combatants
  extract_creature_target(line)
end

.extract_target_from_match(match) ⇒ Hash?

Extracts target information from a regex match object.

Examples:

extract_target_from_match(match)
# => { id: 123, noun: "creature", name: "Goblin" }

Parameters:

  • match (MatchData)

    the regex match object containing target information.

Returns:

  • (Hash, nil)

    the extracted target information or nil if not found.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'documented/gemstone/combat/parser.rb', line 144

def extract_target_from_match(match)
  return nil unless match.names.include?('target')
  target_text = match[:target]
  return nil if target_text.nil? || target_text.strip.empty?

  # Look for creature in target text
  if (target_match = TARGET_LINK_PATTERN.match(target_text))
    id = target_match[:id].to_i
    return nil if id < 0

    return {
      id: id,
      noun: target_match[:noun],
      name: target_match[:name]
    }
  end

  nil
end

.parse_attack(line) ⇒ Hash?

Parses a combat attack line and extracts relevant information.

Examples:

parse_attack("You attack the Goblin!")
# => { name: "attack_name", target: { id: 123, noun: "creature", name: "Goblin" }, damaging: true }

Parameters:

  • line (String)

    the line containing the attack information.

Returns:

  • (Hash, nil)

    a hash with attack details or nil if no attack is detected.

See Also:

  • #attack_lookup
  • #attack_detector


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'documented/gemstone/combat/parser.rb', line 53

def parse_attack(line)
  return nil unless attack_detector.match?(line)

  attack_lookup.each do |pattern, name|
    if (match = pattern.match(line))
      target_info = extract_target_from_match(match) || extract_target_from_line(line)
      return {
        name: name,
        target: target_info || {},
        damaging: true
      }
    end
  end
  nil
end

.parse_damage(line) ⇒ Integer?

Parses a damage line and extracts the damage value.

Examples:

parse_damage("You deal 50 damage!")
# => 50

Parameters:

  • line (String)

    the line containing the damage information.

Returns:

  • (Integer, nil)

    the damage value or nil if not found.



76
77
78
79
# File 'documented/gemstone/combat/parser.rb', line 76

def parse_damage(line)
  result = Definitions::Damage.parse(line)
  result ? result[:damage] : nil
end

.parse_status(line) ⇒ Hash?

Note:

Only returns a result if tracking statuses is enabled.

Parses a status line and returns the status information.

Examples:

parse_status("You are now stunned!")
# => { status: "stunned", action: "status_action" }

Parameters:

  • line (String)

    the line containing the status information.

Returns:

  • (Hash, nil)

    the parsed status information or nil if not tracking statuses.



89
90
91
92
93
94
# File 'documented/gemstone/combat/parser.rb', line 89

def parse_status(line)
  return nil unless Tracker.settings[:track_statuses]

  # Return the full result including action field
  Definitions::Statuses.parse(line)
end

.parse_ucs(line) ⇒ Hash?

Note:

Only returns a result if tracking UCS is enabled.

Parses a UCS line and returns the UCS information.

Examples:

parse_ucs("You have gained a new skill!")
# => { skill: "new_skill" }

Parameters:

  • line (String)

    the line containing the UCS information.

Returns:

  • (Hash, nil)

    the parsed UCS information or nil if not tracking UCS.



104
105
106
107
108
# File 'documented/gemstone/combat/parser.rb', line 104

def parse_ucs(line)
  return nil unless Tracker.settings[:track_ucs]

  Definitions::UCS.parse(line)
end