Class: Legion::Extensions::Tick::Helpers::State

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/tick/helpers/state.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode: :dormant) ⇒ State

Returns a new instance of State.



11
12
13
14
15
16
17
18
19
# File 'lib/legion/extensions/tick/helpers/state.rb', line 11

def initialize(mode: :dormant)
  @mode = mode
  @tick_count = 0
  @last_signal_at = nil
  @last_high_salience_at = nil
  @phase_results = {}
  @current_phase = nil
  @mode_history = [{ mode: mode, at: Time.now.utc }]
end

Instance Attribute Details

#current_phaseObject (readonly)

Returns the value of attribute current_phase.



8
9
10
# File 'lib/legion/extensions/tick/helpers/state.rb', line 8

def current_phase
  @current_phase
end

#last_high_salience_atObject (readonly)

Returns the value of attribute last_high_salience_at.



8
9
10
# File 'lib/legion/extensions/tick/helpers/state.rb', line 8

def last_high_salience_at
  @last_high_salience_at
end

#last_signal_atObject (readonly)

Returns the value of attribute last_signal_at.



8
9
10
# File 'lib/legion/extensions/tick/helpers/state.rb', line 8

def last_signal_at
  @last_signal_at
end

#modeObject (readonly)

Returns the value of attribute mode.



8
9
10
# File 'lib/legion/extensions/tick/helpers/state.rb', line 8

def mode
  @mode
end

#mode_historyObject (readonly)

Returns the value of attribute mode_history.



8
9
10
# File 'lib/legion/extensions/tick/helpers/state.rb', line 8

def mode_history
  @mode_history
end

#phase_resultsObject (readonly)

Returns the value of attribute phase_results.



8
9
10
# File 'lib/legion/extensions/tick/helpers/state.rb', line 8

def phase_results
  @phase_results
end

#tick_countObject (readonly)

Returns the value of attribute tick_count.



8
9
10
# File 'lib/legion/extensions/tick/helpers/state.rb', line 8

def tick_count
  @tick_count
end

Instance Method Details

#increment_tickObject



33
34
35
36
37
# File 'lib/legion/extensions/tick/helpers/state.rb', line 33

def increment_tick
  @tick_count += 1
  @phase_results = {}
  @current_phase = nil
end

#record_phase(phase, result) ⇒ Object



28
29
30
31
# File 'lib/legion/extensions/tick/helpers/state.rb', line 28

def record_phase(phase, result)
  @current_phase = phase
  @phase_results[phase] = result
end

#record_signal(salience: 0.0, source_type: nil) ⇒ Object



21
22
23
24
25
26
# File 'lib/legion/extensions/tick/helpers/state.rb', line 21

def record_signal(salience: 0.0, source_type: nil)
  @last_signal_at = Time.now.utc
  return unless salience >= Constants::HIGH_SALIENCE_THRESHOLD || source_type == :human_direct

  @last_high_salience_at = Time.now.utc
end

#seconds_since_high_salienceObject



53
54
55
56
57
# File 'lib/legion/extensions/tick/helpers/state.rb', line 53

def seconds_since_high_salience
  return 0.0 unless @last_high_salience_at

  Time.now.utc - @last_high_salience_at
end

#seconds_since_signalObject



47
48
49
50
51
# File 'lib/legion/extensions/tick/helpers/state.rb', line 47

def seconds_since_signal
  return 0.0 unless @last_signal_at

  Time.now.utc - @last_signal_at
end

#to_hObject



59
60
61
62
63
64
65
66
67
68
# File 'lib/legion/extensions/tick/helpers/state.rb', line 59

def to_h
  {
    mode:                  @mode,
    tick_count:            @tick_count,
    current_phase:         @current_phase,
    last_signal_at:        @last_signal_at,
    last_high_salience_at: @last_high_salience_at,
    phases_completed:      @phase_results.keys
  }
end

#transition_to(new_mode) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/legion/extensions/tick/helpers/state.rb', line 39

def transition_to(new_mode)
  return if new_mode == @mode

  @mode = new_mode
  @mode_history << { mode: new_mode, at: Time.now.utc }
  @mode_history.shift while @mode_history.size > 50
end