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

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

Overview

Core parsing methods for extracting combat events, damage, status effects, and UCS events from game server output. Provides performance-optimized pattern matching with lazy loading and early-exit conditions.

Constant Summary collapse

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

Bold tag pattern - creatures are wrapped in bold tags Non-greedy match to avoid spanning multiple creatures Allow zero or more characters before tag (e.g., "a creature" or just "creature")

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

Class Method Summary collapse

Class Method Details

.extract_creature_target(line) ⇒ Object

Extract creature target (must be wrapped in bold tags)



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'documented/gemstone/combat/parser.rb', line 71

def extract_creature_target(line)
  # Cheap gate: the wrapper pattern can't match without the bold tag,
  # and the substring check avoids two regexes on most lines
  return nil unless line.include?('<pushBold/>')

  # 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 a creature target from a line of game output by looking for bold-wrapped links. Non-bolded links (equipment, objects, NPCs) are rejected.

Examples:

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

Parameters:

  • line (String)

    a line of game server output containing XML tags

Returns:

  • (Hash, nil)

    a hash with :id (Integer), :noun (String), and :name (String) keys if a bolded creature link is found; nil otherwise



124
125
126
127
128
# File 'documented/gemstone/combat/parser.rb', line 124

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) ⇒ Object

Try to extract target from regex match first, then from line



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'documented/gemstone/combat/parser.rb', line 96

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) ⇒ Object

Parse attack initiation



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'documented/gemstone/combat/parser.rb', line 33

def parse_attack(line)
  return nil if Definitions::Attacks.rejects?(line)

  Definitions::Attacks::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) ⇒ Object

Parse damage amounts using damage definitions



50
51
52
53
# File 'documented/gemstone/combat/parser.rb', line 50

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

.parse_status(line) ⇒ Object

Parse status effects (optional - performance setting)



56
57
58
59
60
61
# File 'documented/gemstone/combat/parser.rb', line 56

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) ⇒ Object

Parse UCS events (position, tierup, smite)



64
65
66
67
68
# File 'documented/gemstone/combat/parser.rb', line 64

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

  Definitions::UCS.parse(line)
end