Class: Dopairb::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/dopairb/session.rb

Overview

One interactive session: owns the game state and ties the evaluation cycle to the director.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Session

Returns a new instance of Session.



10
11
12
13
14
15
16
# File 'lib/dopairb/session.rb', line 10

def initialize(config)
  @config = config
  @game = Game.new
  @director = Director.new(config)
  @fx = InputFx.new(config, @game)
  @finished = false
end

Instance Attribute Details

#cold_start ⇒ Object

Returns the value of attribute cold_start.



8
9
10
# File 'lib/dopairb/session.rb', line 8

def cold_start
  @cold_start
end

#config ⇒ Object (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/dopairb/session.rb', line 7

def config
  @config
end

#director ⇒ Object (readonly)

Returns the value of attribute director.



7
8
9
# File 'lib/dopairb/session.rb', line 7

def director
  @director
end

#fx ⇒ Object (readonly)

Returns the value of attribute fx.



7
8
9
# File 'lib/dopairb/session.rb', line 7

def fx
  @fx
end

#game ⇒ Object (readonly)

Returns the value of attribute game.



7
8
9
# File 'lib/dopairb/session.rb', line 7

def game
  @game
end

Instance Method Details

#active? ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/dopairb/session.rb', line 18

def active?
  !@config.off? && Term.interactive?
end

#around_eval(code, last_value) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dopairb/session.rb', line 38

def around_eval(code, last_value)
  input = RelineAdapter.take_input(code)
  OutputTap.start
  charge = @config.motion ? ChargeLine.new(@config, charge: @game.charge).start : nil
  t0 = now
  completed = false
  failure = nil
  begin
    result = yield
    completed = true
    result
  rescue Exception => e # rubocop:disable Lint/RescueException
    failure = e
    raise
  ensure
    charge&.stop
    lines, = OutputTap.stop
    # a throw (e.g. irb_exit) leaves neither flag set: stay quiet
    if completed
      react(:success, code, last_value, input, lines, now - t0, charge)
    elsif failure && !(SystemExit === failure)
      react(failure, code, last_value, input, lines, now - t0, charge)
    end
  end
end

#finish ⇒ Object



32
33
34
35
36
# File 'lib/dopairb/session.rb', line 32

def finish
  return if @finished
  @finished = true
  @director.outro(@game.stats) if active? && @game.evals > 0
end

#intro ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/dopairb/session.rb', line 22

def intro
  return unless active?
  Sound.warm_up(@config.duration)
  cold = @cold_start || (Sound.available? && !Sound.ready?(:intro, @config.duration))
  if @config.intro && @config.motion && cold
    @director.loading { Sound.ready?(:intro, @config.duration) }
  end
  @director.intro
end