Class: Tools::Throw
- Inherits:
-
Object
- Object
- Tools::Throw
- Defined in:
- app/models/tools/throw.rb
Instance Attribute Summary collapse
-
#dice ⇒ Object
Returns the value of attribute dice.
-
#offset ⇒ Object
Returns the value of attribute offset.
Instance Method Summary collapse
-
#add_offset(delta) ⇒ Object
Adds delta to the offset value.
-
#initialize(dice, offset = 0) ⇒ Throw
constructor
Creates a new
Throw
with anArray
ofDie
and aFixnum
asoffset
. -
#roll(rolls = 1, detailed = false) ⇒ Object
Rolls the Throw and returns the result in different ways according to the params.
-
#substract_offset(delta) ⇒ Object
Subtracts delta to the offset value.
-
#to_s ⇒ Object
Returns the Trhow as a string with the format aDx,bDy,cDz+n, where a,b,c are the number of dice, x,y,z are the number of sides and n the offset.
Constructor Details
#initialize(dice, offset = 0) ⇒ Throw
Creates a new Throw
with an Array
of Die
and a Fixnum
as offset
6 7 8 9 |
# File 'app/models/tools/throw.rb', line 6 def initialize dice, offset=0 @dice = dice.sort_by{|die| die.sides} @offset = offset end |
Instance Attribute Details
#dice ⇒ Object
Returns the value of attribute dice.
3 4 5 |
# File 'app/models/tools/throw.rb', line 3 def dice @dice end |
#offset ⇒ Object
Returns the value of attribute offset.
3 4 5 |
# File 'app/models/tools/throw.rb', line 3 def offset @offset end |
Instance Method Details
#add_offset(delta) ⇒ Object
Adds delta to the offset value
28 29 30 31 |
# File 'app/models/tools/throw.rb', line 28 def add_offset delta @offset+= delta self end |
#roll(rolls = 1, detailed = false) ⇒ Object
Rolls the Throw and returns the result in different ways according to the params. If detailed=false it will call simple_roll
and detailed_roll
in case of true
. Refer to these methods for more info of the format. Returns the result as simple_roll/detailed_roll
if rolls==1 or as Array
of simple_roll/detailed_roll
if rolls>1.
16 17 18 19 20 21 22 23 24 25 |
# File 'app/models/tools/throw.rb', line 16 def roll rolls=1, detailed=false return simple_roll if rolls==1 and !detailed return detailed_roll if rolls==1 and detailed results = Array.new rolls.times do results << simple_roll if !detailed results << detailed_roll if detailed end return results end |
#substract_offset(delta) ⇒ Object
Subtracts delta to the offset value
34 35 36 37 |
# File 'app/models/tools/throw.rb', line 34 def substract_offset delta @offset-= delta self end |
#to_s ⇒ Object
Returns the Trhow as a string with the format aDx,bDy,cDz+n, where a,b,c are the number of dice, x,y,z are the number of sides and n the offset. Example “D6,D20,D100+8”, etc.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'app/models/tools/throw.rb', line 41 def to_s throw_dice_number = Array.new throw_dice = Array.new temp_dice = dice.sort_by{|die| die.sides} last_die = nil temp_dice.each do |temp_die| if !temp_die.eql? last_die last_die = temp_die throw_dice << temp_die throw_dice_number << temp_dice.count{|die| die.eql? temp_die} end end throw_string = "" throw_dice.each_index do |i| throw_string << "," if i>0 throw_string << throw_dice_number[i].to_s + throw_dice[i].to_s end throw_string << "+" if @offset>0 throw_string << "#{@offset}" if @offset!=0 return throw_string end |