class Pbw::Utils::Chance
Attributes
chance[RW]
dice[RW]
rolled[RW]
score[RW]
Public Class Methods
demongoize(object)
click to toggle source
# File lib/pbw/utils/chance.rb, line 36 def self.demongoize(object) read(object.to_s) end
evolve(object)
click to toggle source
# File lib/pbw/utils/chance.rb, line 44 def self.evolve(object) object.mongoize if object.is_a?(::Pbw::Utils::Chance) end
mongoize(object)
click to toggle source
# File lib/pbw/utils/chance.rb, line 40 def self.mongoize(object) object.mongoize if object.is_a?(::Pbw::Utils::Chance) end
new(chance = 0, score = 0, dice = Dice.new)
click to toggle source
# File lib/pbw/utils/chance.rb, line 25 def initialize(chance = 0, score = 0, dice = Dice.new) @chance = chance @score = score @dice = dice @rolled = 0 end
read(s)
click to toggle source
# 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
Public Instance Methods
+(n)
click to toggle source
# File lib/pbw/utils/chance.rb, line 52 def +(n) ::Pbw::Utils::Chance.new(chance, score, (self.dice + n)) end
-(n)
click to toggle source
# File lib/pbw/utils/chance.rb, line 56 def -(n) ::Pbw::Utils::Chance.new(chance, score, (self.dice - n)) end
<(c)
click to toggle source
# 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)
click to toggle source
# 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?()
click to toggle source
# File lib/pbw/utils/chance.rb, line 48 def blank? chance == 0 end
mongoize()
click to toggle source
# File lib/pbw/utils/chance.rb, line 32 def mongoize to_s end
percentage_success()
click to toggle source
# File lib/pbw/utils/chance.rb, line 93 def percentage_success (probability_of_success * 100).round(0).to_i end
probability_of_success()
click to toggle source
# 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?()
click to toggle source
# 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()
click to toggle source
# File lib/pbw/utils/chance.rb, line 116 def to_pretty "#{to_s} (#{percentage_success}% chance)" end
Also aliased as: pp
to_s()
click to toggle source
# 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