Class: Legion::Extensions::Agentic::Defense::Avalanche::Helpers::AvalancheEngine

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/defense/avalanche/helpers/avalanche_engine.rb

Constant Summary

Constants included from Constants

Constants::ACCUMULATION_RATE, Constants::CASCADE_TYPES, Constants::MAGNITUDE_LABELS, Constants::MAX_CASCADE_HISTORY, Constants::MAX_SNOWPACKS, Constants::MELT_RATE, Constants::SNOWPACK_TYPES, Constants::STABILITY_LABELS, Constants::TRIGGER_THRESHOLD

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Constants

label_for

Constructor Details

#initializeAvalancheEngine

Returns a new instance of AvalancheEngine.



14
15
16
17
# File 'lib/legion/extensions/agentic/defense/avalanche/helpers/avalanche_engine.rb', line 14

def initialize
  @snowpacks       = {}
  @cascade_history = []
end

Instance Attribute Details

#cascade_historyObject (readonly)

Returns the value of attribute cascade_history.



12
13
14
# File 'lib/legion/extensions/agentic/defense/avalanche/helpers/avalanche_engine.rb', line 12

def cascade_history
  @cascade_history
end

#snowpacksObject (readonly)

Returns the value of attribute snowpacks.



12
13
14
# File 'lib/legion/extensions/agentic/defense/avalanche/helpers/avalanche_engine.rb', line 12

def snowpacks
  @snowpacks
end

Instance Method Details

#accumulate_all!(rate: Constants::ACCUMULATION_RATE) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/legion/extensions/agentic/defense/avalanche/helpers/avalanche_engine.rb', line 58

def accumulate_all!(rate: Constants::ACCUMULATION_RATE, **)
  count = 0
  @snowpacks.each_value do |pack|
    pack.accumulate!(rate)
    count += 1
  end
  { success: true, packs_accumulated: count, rate: rate }
end

#active_cascadesObject



76
77
78
# File 'lib/legion/extensions/agentic/defense/avalanche/helpers/avalanche_engine.rb', line 76

def active_cascades
  @cascade_history.select(&:active?)
end

#create_snowpack(snowpack_type:, domain:, content:, depth: 0.0, stability: 1.0) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/legion/extensions/agentic/defense/avalanche/helpers/avalanche_engine.rb', line 19

def create_snowpack(snowpack_type:, domain:, content:, depth: 0.0, stability: 1.0, **)
  raise ArgumentError, 'snowpack limit reached' if @snowpacks.size >= Constants::MAX_SNOWPACKS

  pack = Snowpack.new(
    snowpack_type: snowpack_type,
    domain:        domain,
    content:       content,
    depth:         depth,
    stability:     stability
  )
  @snowpacks[pack.id] = pack
  { success: true, snowpack_id: pack.id, snowpack: pack.to_h }
rescue ArgumentError => e
  { success: false, error: e.message }
end

#melt_all!(rate: Constants::MELT_RATE) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/legion/extensions/agentic/defense/avalanche/helpers/avalanche_engine.rb', line 67

def melt_all!(rate: Constants::MELT_RATE, **)
  count = 0
  @snowpacks.each_value do |pack|
    pack.melt!(rate)
    count += 1
  end
  { success: true, packs_melted: count, rate: rate }
end

#most_unstableObject



80
81
82
# File 'lib/legion/extensions/agentic/defense/avalanche/helpers/avalanche_engine.rb', line 80

def most_unstable
  @snowpacks.values.min_by(&:stability)
end

#terrain_reportObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/legion/extensions/agentic/defense/avalanche/helpers/avalanche_engine.rb', line 84

def terrain_report
  packs           = @snowpacks.values
  active_cascades = self.active_cascades
  critical_packs  = packs.select(&:critical?)
  unstable_packs  = packs.select(&:unstable?)

  {
    total_snowpacks:  packs.size,
    critical_count:   critical_packs.size,
    unstable_count:   unstable_packs.size,
    stable_count:     packs.count(&:stable?),
    active_cascades:  active_cascades.size,
    cascade_history:  @cascade_history.size,
    avg_stability:    avg_stability(packs),
    avg_depth:        avg_depth(packs),
    most_unstable_id: most_unstable&.id,
    recent_cascades:  recent_cascades(5)
  }
end

#trigger(snowpack_id:, force:, cascade_type: :chaotic) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/legion/extensions/agentic/defense/avalanche/helpers/avalanche_engine.rb', line 35

def trigger(snowpack_id:, force:, cascade_type: :chaotic, **)
  pack = @snowpacks[snowpack_id]
  raise ArgumentError, "snowpack not found: #{snowpack_id}" unless pack

  pack.destabilize!(force)
  instability = pack.instability

  return { success: true, triggered: false, instability: instability, reason: :below_threshold } \
    if (force + instability) < Constants::TRIGGER_THRESHOLD

  cascade = Cascade.new(
    cascade_type:      cascade_type,
    trigger_source:    snowpack_id,
    magnitude:         ((force + instability) / 2.0).clamp(0.0, 1.0).round(10),
    propagation_speed: instability.round(10),
    debris:            [pack.content]
  )
  record_cascade(cascade)
  { success: true, triggered: true, cascade_id: cascade.id, cascade: cascade.to_h, instability: instability }
rescue ArgumentError => e
  { success: false, error: e.message }
end