Class: FifthedSim::RollRepl

Inherits:
Object
  • Object
show all
Defined in:
lib/fifthed_sim/roll_repl.rb

Instance Method Summary collapse

Constructor Details

#initialize(inspect: true, errors: false, auto_info: false) ⇒ RollRepl

Returns a new instance of RollRepl.



3
4
5
6
7
8
9
# File 'lib/fifthed_sim/roll_repl.rb', line 3

def initialize(inspect: true,
               errors: false,
               auto_info: false)
  @inspect = inspect
  @errors = errors
  @auto_info = auto_info
end

Instance Method Details

#display_compile_error(err, cmd) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/fifthed_sim/roll_repl.rb', line 90

def display_compile_error(err, cmd)
  if @errors
    error_msg("Could not read line: #{err.tree_cause}")
  else
    error_msg("Could not read line: #{err.message}")
  end
end

#display_roll(r) ⇒ Object



80
81
82
83
84
# File 'lib/fifthed_sim/roll_repl.rb', line 80

def display_roll(r)
  puts "  =  " + r.value_equation(terminal: true) if @inspect  
  puts "  => " + Rainbow(r.value.to_s).underline.bright.to_s
  print_last_info if @auto_info
end

#error_msg(msg) ⇒ Object



86
87
88
# File 'lib/fifthed_sim/roll_repl.rb', line 86

def error_msg(msg)
  puts Rainbow(msg).color(:red).bright
end

#exitObject



106
107
108
109
# File 'lib/fifthed_sim/roll_repl.rb', line 106

def exit
  puts "Goodbye."
  exit!
end

#helpObject



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/fifthed_sim/roll_repl.rb', line 111

def help
  cmd = ->(a){ Rainbow(a.to_s).bright.underline + ":" }
  puts %Q{COMMANDS:
  #{cmd[:help]} display this message
  #{cmd[:inspect]} toggle equation inspection
  #{cmd[:quit]} exit the roller
  #{cmd[:errors]} toggle in-depth compile errors for dice expressions
  #{cmd["arrow keys"]} navigate like GNU readline
  #{cmd[:info]} get info about the previous roll, or a roll on this line.
  #{cmd["rr, reroll"]} reroll the previous dice
  }
end

#info_command(cmd) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/fifthed_sim/roll_repl.rb', line 24

def info_command(cmd)
  s = cmd.gsub(/info/, "").chomp
  if s.length > 0
    roll(s)
  end
  print_last_info
end


32
33
34
35
36
37
38
39
# File 'lib/fifthed_sim/roll_repl.rb', line 32

def print_last_info
  return error_msg("Have nothing to get info of") unless @last_roll
  lb = ->(x){Rainbow(x).color(:yellow).bright.to_s + ": "}
  a = %i(max min percentile).map do |p|
    lb[p] + @last_roll.public_send(p).to_s
  end.inject{|m, x| m + ", " + x}
  puts a
end

#rerollObject



41
42
43
44
45
# File 'lib/fifthed_sim/roll_repl.rb', line 41

def reroll
  return error_msg("Nothing to reroll") unless @last_roll
  @last_roll = @last_roll.reroll
  display_roll(@last_roll)
end

#roll(cmd) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fifthed_sim/roll_repl.rb', line 68

def roll(cmd)
  r = DiceExpression(cmd)
  @last_roll = r
  display_roll(r)
rescue FifthedSim::Compiler::CompileError => e
  if e.char
    display_compile_error(e, cmd)
  else
    error_msg("Could not parse expression!")
  end
end

#run(kill_on_interrupt = false) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/fifthed_sim/roll_repl.rb', line 11

def run(kill_on_interrupt = false)
  begin
    while buf = Readline.readline("> ", true)
      run_cmd(buf)
      kill_on_interrupt = false
    end
  rescue Interrupt
    self.exit if kill_on_interrupt
    puts "Control-C again or 'Exit' to quit"
    run(true)
  end
end

#run_cmd(cmd) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fifthed_sim/roll_repl.rb', line 47

def run_cmd(cmd)
  case cmd
  when /(quit|exit|stop)/
    self.exit
  when "rr", "reroll"
    self.reroll
  when "info"
    info_command(cmd)
  when "help"
    self.help
  when "inspect"
    toggle_inspect
  when "errors"
    toggle_errors
  when "autoinfo"
    toggle_auto_info
  else
    self.roll(cmd)
  end
end