Class: Natural20::DieRoll

Inherits:
Object
  • Object
show all
Defined in:
lib/natural_20/die_roll.rb

Defined Under Namespace

Classes: DieRolls

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rolls, modifier, die_sides = 20, advantage: false, disadvantage: false, description: nil, roller: nil) ⇒ DieRoll

This represents a dice roll



126
127
128
129
130
131
132
133
134
# File 'lib/natural_20/die_roll.rb', line 126

def initialize(rolls, modifier, die_sides = 20, advantage: false, disadvantage: false, description: nil, roller: nil)
  @rolls = rolls
  @modifier = modifier
  @die_sides = die_sides
  @advantage = advantage
  @disadvantage = disadvantage
  @description = description
  @roller = roller
end

Instance Attribute Details

#die_sidesObject (readonly)

Returns the value of attribute die_sides.



121
122
123
# File 'lib/natural_20/die_roll.rb', line 121

def die_sides
  @die_sides
end

#modifierObject (readonly)

Returns the value of attribute modifier.



121
122
123
# File 'lib/natural_20/die_roll.rb', line 121

def modifier
  @modifier
end

#rollerObject (readonly)

Returns the value of attribute roller.



121
122
123
# File 'lib/natural_20/die_roll.rb', line 121

def roller
  @roller
end

#rollsObject (readonly)

Returns the value of attribute rolls.



121
122
123
# File 'lib/natural_20/die_roll.rb', line 121

def rolls
  @rolls
end

Class Method Details

.numeric?(c) ⇒ Boolean



212
213
214
215
216
217
218
219
220
# File 'lib/natural_20/die_roll.rb', line 212

def self.numeric?(c)
  return true if c =~ /\A\d+\Z/

  begin
    true if Float(c)
  rescue StandardError
    false
  end
end

.parse(roll_str) ⇒ Natural20::DieRollDetail



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/natural_20/die_roll.rb', line 243

def self.parse(roll_str)
  die_count_str = ''
  die_type_str = ''
  modifier_str = ''
  modifier_op = ''
  state = :initial

  roll_str.strip.each_char do |c|
    case state
    when :initial
      if numeric?(c)
        die_count_str << c
      elsif c == 'd'
        state = :die_type
      elsif c == '+'
        state = :modifier
      end
    when :die_type
      next if c == ' '

      if numeric?(c)
        die_type_str << c
      elsif c == '+'
        state = :modifier
      elsif c == '-'
        modifier_op = '-'
        state = :modifier
      end
    when :modifier
      next if c == ' '

      modifier_str << c if numeric?(c)
    end
  end

  if state == :initial
    modifier_str = die_count_str
    die_count_str = '0'
  end

  number_of_die = die_count_str.blank? ? 1 : die_count_str.to_i

  detail = Natural20::DieRollDetail.new
  detail.die_count = number_of_die
  detail.die_type = die_type_str
  detail.modifier = modifier_str
  detail.modifier_op = modifier_op
  detail
end

.roll(roll_str, crit: false, disadvantage: false, advantage: false, description: nil, entity: nil, battle: nil) ⇒ Natural20::DieRoll

Rolls the dice, details on dice rolls and its values are preserved



301
302
303
304
305
# File 'lib/natural_20/die_roll.rb', line 301

def self.roll(roll_str, crit: false, disadvantage: false, advantage: false, description: nil, entity: nil, battle: nil)
  roller = Roller.new(roll_str, crit: crit, disadvantage: disadvantage, advantage: advantage,
                                description: description, entity: entity, battle: battle)
  roller.roll
end

.roll_with_lucky(entity, roll_str, crit: false, disadvantage: false, advantage: false, description: nil, battle: nil) ⇒ Natural20::DieRoll

Rolls the dice checking lucky feat, details on dice rolls and its values are preserved



316
317
318
319
320
321
322
323
324
325
# File 'lib/natural_20/die_roll.rb', line 316

def self.roll_with_lucky(entity, roll_str, crit: false, disadvantage: false, advantage: false, description: nil, battle: nil)
  roller = Roller.new(roll_str, crit: crit, disadvantage: disadvantage, advantage: advantage,
                                description: description, entity: entity, battle: battle)
  result = roller.roll
  if result.nat_1? && entity.class_feature?('lucky')
    roller.roll(lucky: true)
  else
    result
  end
end

.t(key, options = {}) ⇒ Object



327
328
329
# File 'lib/natural_20/die_roll.rb', line 327

def self.t(key, options = {})
  I18n.t(key, **options)
end

Instance Method Details

#+(other) ⇒ Object



232
233
234
235
236
237
238
239
# File 'lib/natural_20/die_roll.rb', line 232

def +(other)
  if other.is_a?(DieRolls)
    other.add_to_front(self)
    other
  else
    DieRolls.new([self, other])
  end
end

#<=>(other) ⇒ Object



228
229
230
# File 'lib/natural_20/die_roll.rb', line 228

def <=>(other)
  result <=> other.result
end

#==(other) ⇒ Object



222
223
224
225
226
# File 'lib/natural_20/die_roll.rb', line 222

def ==(other)
  return true if other.rolls == @rolls && other.modifier == @modifier && other.die_sides == @die_sides

  false
end

#color_roll(roll) ⇒ String

adds color flair to the roll depending on value



179
180
181
182
183
184
185
186
187
188
# File 'lib/natural_20/die_roll.rb', line 179

def color_roll(roll)
  case roll
  when 1
    roll.to_s.colorize(:red)
  when @die_sides
    roll.to_s.colorize(:green)
  else
    roll.to_s
  end
end

#nat_1?Boolean



148
149
150
151
152
153
154
155
156
# File 'lib/natural_20/die_roll.rb', line 148

def nat_1?
  if @advantage
    @rolls.map(&:max).detect { |r| r == 1 }
  elsif @disadvantage
    @rolls.map(&:min).detect { |r| r == 1 }
  else
    @rolls.include?(1)
  end
end

#nat_20?Boolean

This is a natural 20 or critical roll



138
139
140
141
142
143
144
145
146
# File 'lib/natural_20/die_roll.rb', line 138

def nat_20?
  if @advantage
    @rolls.map(&:max).detect { |r| r == 20 }
  elsif @disadvantage
    @rolls.map(&:min).detect { |r| r == 20 }
  else
    @rolls.include?(20)
  end
end

#reroll(lucky: false) ⇒ Object



158
159
160
# File 'lib/natural_20/die_roll.rb', line 158

def reroll(lucky: false)
  @roller.roll(lucky: lucky)
end

#resultInteger

computes the integer result of the dice roll



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/natural_20/die_roll.rb', line 164

def result
  sum = if @advantage
          @rolls.map(&:max).sum
        elsif @disadvantage
          @rolls.map(&:min).sum
        else
          @rolls.sum
        end

  sum + @modifier
end

#to_sObject



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/natural_20/die_roll.rb', line 190

def to_s
  rolls = @rolls.map do |r|
    if @advantage
      r.map do |i|
        i == r.max ? color_roll(i).bold : i.to_s.colorize(:gray)
      end.join(' | ')
    elsif @disadvantage
      r.map do |i|
        i == r.min ? color_roll(i).bold : i.to_s.colorize(:gray)
      end.join(' | ')
    else
      color_roll(r)
    end
  end

  if @modifier != 0
    "(#{rolls.join(' + ')}) + #{@modifier}"
  else
    "(#{rolls.join(' + ')})"
  end
end