Class: Pbw::Utils::Chance
- Inherits:
-
Object
- Object
- Pbw::Utils::Chance
- Defined in:
- lib/pbw/utils/chance.rb
Instance Attribute Summary collapse
-
#chance ⇒ Object
Returns the value of attribute chance.
-
#dice ⇒ Object
Returns the value of attribute dice.
-
#rolled ⇒ Object
Returns the value of attribute rolled.
-
#score ⇒ Object
Returns the value of attribute score.
Class Method Summary collapse
Instance Method Summary collapse
- #+(n) ⇒ Object
- #-(n) ⇒ Object
- #<(c) ⇒ Object
- #>(c) ⇒ Object
- #blank? ⇒ Boolean
-
#initialize(chance = 0, score = 0, dice = Dice.new) ⇒ Chance
constructor
A new instance of Chance.
- #mongoize ⇒ Object
- #percentage_success ⇒ Object
- #probability_of_success ⇒ Object
- #success? ⇒ Boolean
- #to_pretty ⇒ Object (also: #pp)
- #to_s ⇒ Object
Constructor Details
Instance Attribute Details
#chance ⇒ Object
Returns the value of attribute chance.
4 5 6 |
# File 'lib/pbw/utils/chance.rb', line 4 def chance @chance end |
#dice ⇒ Object
Returns the value of attribute dice.
4 5 6 |
# File 'lib/pbw/utils/chance.rb', line 4 def dice @dice end |
#rolled ⇒ Object
Returns the value of attribute rolled.
4 5 6 |
# File 'lib/pbw/utils/chance.rb', line 4 def rolled @rolled end |
#score ⇒ Object
Returns the value of attribute score.
4 5 6 |
# File 'lib/pbw/utils/chance.rb', line 4 def score @score end |
Class Method Details
.demongoize(object) ⇒ Object
36 37 38 |
# File 'lib/pbw/utils/chance.rb', line 36 def self.demongoize(object) read(object.to_s) end |
.evolve(object) ⇒ Object
44 45 46 |
# File 'lib/pbw/utils/chance.rb', line 44 def self.evolve(object) object.mongoize if object.is_a?(::Pbw::Utils::Chance) end |
.mongoize(object) ⇒ Object
40 41 42 |
# File 'lib/pbw/utils/chance.rb', line 40 def self.mongoize(object) object.mongoize if object.is_a?(::Pbw::Utils::Chance) end |
.read(s) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/pbw/utils/chance.rb', line 6 def self.read(s) return nil if s.blank? begin if s.include?("in") a = s.split(" in ") chance = a[0].to_i dice = Dice.read(a[1]) ::Pbw::Utils::Chance.new(chance,0,dice) else a = s.split(" on ") score = a[0].to_i dice = Dice.read(a[1]) ::Pbw::Utils::Chance.new(0,score,dice) end rescue raise "Invalid chance format" end end |
Instance Method Details
#+(n) ⇒ Object
52 53 54 |
# File 'lib/pbw/utils/chance.rb', line 52 def +(n) ::Pbw::Utils::Chance.new(chance, score, (self.dice + n)) end |
#-(n) ⇒ Object
56 57 58 |
# File 'lib/pbw/utils/chance.rb', line 56 def -(n) ::Pbw::Utils::Chance.new(chance, score, (self.dice - n)) end |
#<(c) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/pbw/utils/chance.rb', line 60 def <(c) return false unless c if c.is_a?(Chance) (self.probability_of_success < c.probability_of_success) else (self.probability_of_success < c.to_f) end end |
#>(c) ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'lib/pbw/utils/chance.rb', line 69 def >(c) return true unless c if c.is_a?(Chance) (self.probability_of_success > c.probability_of_success) else (self.probability_of_success > c.to_f) end end |
#blank? ⇒ Boolean
48 49 50 |
# File 'lib/pbw/utils/chance.rb', line 48 def blank? chance == 0 end |
#mongoize ⇒ Object
32 33 34 |
# File 'lib/pbw/utils/chance.rb', line 32 def mongoize to_s end |
#percentage_success ⇒ Object
93 94 95 |
# File 'lib/pbw/utils/chance.rb', line 93 def percentage_success (probability_of_success * 100).round(0).to_i end |
#probability_of_success ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/pbw/utils/chance.rb', line 78 def probability_of_success return @probability_of_success if @probability_of_success return 0 unless dice prob = dice.probabilities @probability_of_success = 0 prob.keys.each do |hit| if chance && chance > 0 @probability_of_success += prob[hit] if hit <= chance && hit != dice.min elsif score && score > 0 @probability_of_success += prob[hit] if hit >= score && hit != dice.min end end @probability_of_success end |
#success? ⇒ Boolean
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/pbw/utils/chance.rb', line 97 def success? return false unless dice self.rolled = dice.roll return false if self.rolled == dice.min if score > 0 self.rolled >= score else self.rolled <= chance end end |
#to_pretty ⇒ Object Also known as: pp
116 117 118 |
# File 'lib/pbw/utils/chance.rb', line 116 def to_pretty "#{to_s} (#{percentage_success}% chance)" end |
#to_s ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/pbw/utils/chance.rb', line 108 def to_s if score > 0 "#{score} on #{dice}" elsif chance > 0 "#{chance} in #{dice}" end end |