Class: Boneroller::Dice

Inherits:
Object
  • Object
show all
Defined in:
lib/cinch/plugins/dice/boneroller.rb

Overview

Represents a single turn.

Constant Summary collapse

HIGH_ROLL_THRESHOLD =
100
LOW_ROLL_THRESHOLD =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ Dice

Returns a new instance of Dice


25
26
27
28
29
30
31
32
# File 'lib/cinch/plugins/dice/boneroller.rb', line 25

def initialize(string)
  raise Errors::IncorrectFormatError, 'Your string does not appear to be a valid roll. Valid example: 1#2d3+4' if !string.match(ATTACK_REGEXP)
  @sum = 0
  @rolls = []
  @line, @comment = *string.split(" ",2)
  @comment = @line if @comment.to_s.strip.empty?
  @highest_roll_dropped, @lowest_roll_dropped = false, false
end

Instance Attribute Details

#commentObject (readonly)

Returns the value of attribute comment


20
21
22
# File 'lib/cinch/plugins/dice/boneroller.rb', line 20

def comment
  @comment
end

#lineObject (readonly)

Returns the value of attribute line


20
21
22
# File 'lib/cinch/plugins/dice/boneroller.rb', line 20

def line
  @line
end

#rollsObject (readonly)

Returns the value of attribute rolls


20
21
22
# File 'lib/cinch/plugins/dice/boneroller.rb', line 20

def rolls
  @rolls
end

#sumObject (readonly)

Returns the value of attribute sum


20
21
22
# File 'lib/cinch/plugins/dice/boneroller.rb', line 20

def sum
  @sum
end

Instance Method Details

#inspectObject


62
63
64
# File 'lib/cinch/plugins/dice/boneroller.rb', line 62

def inspect
  "#<Boneroller::Dice @comment=%s, @line=%s, @rolls=%s, @sum=%s>" % [@comment.inspect, @line.inspect, @rolls.inspect, @sum.inspect]
end

#rollObject Also known as: roll!

DO A BARREL ROLL


35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cinch/plugins/dice/boneroller.rb', line 35

def roll
  repeat_by, dice, faces, elements = *@line.scan(ATTACK_REGEXP).flatten
  faces = 100 if faces.eql?('%') # Percentile dice
  repeat_by ||= 1 # Repeat our rolls
  dice ||= 1 # Dice count
  # Error out if we are rolling too much, or shooting the wind
  raise Errors::RollTooHighError if dice.to_i > HIGH_ROLL_THRESHOLD || repeat_by.to_i > HIGH_ROLL_THRESHOLD
  raise Errors::RollTooLowError if faces.to_i < LOW_ROLL_THRESHOLD || dice.to_i < LOW_ROLL_THRESHOLD || repeat_by.to_i < LOW_ROLL_THRESHOLD
  # Roll it!
  repeat_by.to_i.times {
    roll = attack_roll(dice, faces)
    @sum += attack_elements(roll, elements)
  }
end

#rolled?Boolean

Returns:

  • (Boolean)

51
52
53
# File 'lib/cinch/plugins/dice/boneroller.rb', line 51

def rolled?
  !@rolls.empty?
end

#to_s(include_rolls = false) ⇒ Object


55
56
57
58
59
60
# File 'lib/cinch/plugins/dice/boneroller.rb', line 55

def to_s(include_rolls=false)
  rolls = @rolls.each_with_object([]) {|(notation,results),memo|
    memo << "[%s(%d)=%s]" % [notation, sum_ofArray(results), results * ","]
  }.join("; ")
  "#{@comment + ': ' if @comment}#@sum#{' (' + rolls + ')' if include_rolls}"
end