pbw 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NDBhYjIwMmM3NWQ0ODM0ZTY2OGFhZGU5YmQ4M2U3N2FmZjY3YmZkZQ==
4
+ Y2EzMDllYWJmZWE3NjEwZDBiN2JiNDMwMzdjYTFjNTEyZDk1Yjc2MQ==
5
5
  data.tar.gz: !binary |-
6
- OTk0NzNmYTE2NTY4MTE5YWVmNTUwMjcxY2I1ODJhNWE1ZWQ5NTM1OA==
6
+ NmMzYzEyNDlkNWQzNGE5YjhlN2ExZDhiOGQ3YmQ4NmMwYzQwMTcxOA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- NmZlMDAxMTdhMmY2YjhjZmYyYjgxMWMzNDY5NzkzMzZiOWI3NGU5MTY0NTE0
10
- Y2RiOWFhZWIyNjA1MThhN2EwMGE1N2ZiZDkyYjZkZDU2ZDI5ZTU2Y2QwMDRl
11
- NmI1MzliZDA4YWMxNWFhMDc1NDU5MjExNTY2NDE2ZTY0ZmM0ODI=
9
+ Y2Q0YjkwOGYwNDI4MzkwOGFhY2IyOGYxYzM0MWM4NGRlMDgyODdhMjNhZGY3
10
+ OGE4MDM5NGVlNGRlNjZmMjM4N2QwYTM5YmUwZDIxMWQyMTAwMDIzNWQ4MWM5
11
+ MGRjNmM5NmI2MmFiN2YyZjMzOTQ5YzhlYTEwOGJjNzI5NGI5Y2M=
12
12
  data.tar.gz: !binary |-
13
- Y2IxMmU1YmI4ZWQxOTY3YjBkNGI2NGE5NjA4MmZhZTg4NjIyNTM2NzJkN2I4
14
- ODhjNGEzOWVlMmZjOGFhNDZkZWM2NTc2MmIwMWU2MWFlZDczMjBhYjlkMjA1
15
- NzEyMDg1OTYwY2JjMDIxMjYxOTQ4MGQzZTM3ZTJlMWNjMGY0Zjc=
13
+ MmNhZDkwOTAxODM4MTI0YTIwNmM3YTBlMzI1MmM1MjVmN2M0N2Y1ZTRkZDc4
14
+ MjcwOGQxNjkyOTZiMzdlMWQ0YmY1YWVjM2ZkZTJmMDdmNDQ5NWRlYWY5MjYw
15
+ MGIxYjExM2QxNmNhZTkwYjE1MWYwNmRlMmQ1MjNlOTlkNGQwZTI=
@@ -3,8 +3,8 @@ module Pbw
3
3
  include ::Mongoid::Document
4
4
  include ::Mongoid::Timestamps
5
5
  field :name, type: String
6
- validates_presence_of :name
7
- validates_uniqueness_of :name
6
+
7
+ validates :name, presence: true
8
8
 
9
9
  has_many :tokens, :class_name => 'Pbw::Token'
10
10
  has_many :item_containers, :class_name => 'Pbw::ItemContainer'
@@ -3,8 +3,8 @@ module Pbw
3
3
  include ::Mongoid::Document
4
4
  include ::Mongoid::Timestamps
5
5
  field :name, type: String
6
- validates_presence_of :name
7
- validates_uniqueness_of :name
6
+
7
+ validates :name, presence: true
8
8
 
9
9
  has_many :item_containers, :class_name => 'Pbw::ItemContainer'
10
10
  has_many :item_conversions, :class_name => 'Pbw::ItemConversion'
@@ -3,7 +3,7 @@ module Pbw
3
3
  include ::Mongoid::Document
4
4
  include ::Mongoid::Timestamps
5
5
  field :name, type: String
6
- validates :name, presence: true, uniqueness: true
6
+ validates :name, presence: true
7
7
 
8
8
  attr_accessible :name
9
9
 
@@ -3,7 +3,7 @@ module Pbw
3
3
  include ::Mongoid::Document
4
4
  include ::Mongoid::Timestamps
5
5
  field :name, type: String
6
- validates_presence_of :name
6
+ validates :name, presence: true
7
7
 
8
8
  belongs_to :area, :class_name => 'Pbw::Area'
9
9
  belongs_to :user, :class_name => 'Pbw::User'
@@ -0,0 +1,35 @@
1
+ require 'pbw/utils/polynomial'
2
+ require 'pbw/utils/dice'
3
+ require 'pbw/utils/chance'
4
+
5
+ module StringExtensions
6
+ def dice
7
+ ::Pbw::Utils::Dice.read(self)
8
+ end
9
+
10
+ def roll
11
+ dice.roll
12
+ end
13
+
14
+ def chance
15
+ ::Pbw::Utils::Chance.read(self)
16
+ end
17
+
18
+ def success?
19
+ chance.success?
20
+ end
21
+
22
+ def is_number?
23
+ true if Float(self) rescue false
24
+ end
25
+
26
+ def pluralize_if(quantity)
27
+ return pluralize if quantity == 0 || quantity > 1
28
+ self
29
+ end
30
+
31
+ end
32
+
33
+ class String
34
+ include StringExtensions
35
+ end
@@ -2,6 +2,7 @@ require 'pbw/version'
2
2
  require 'mongoid'
3
3
  require 'devise'
4
4
  require 'cancan'
5
+ require 'pbw/utils/utils'
5
6
 
6
7
  module Pbw
7
8
  class Engine < ::Rails::Engine
@@ -11,7 +12,7 @@ module Pbw
11
12
  config.mount_at = '/pbw'
12
13
 
13
14
  rake_tasks do
14
- load File.join(File.dirname(__FILE__), 'tasks/pbw_tasks.rake')
15
+ load File.join(File.dirname(__FILE__), '../tasks/pbw_tasks.rake')
15
16
  end
16
17
 
17
18
  config.generators do |g|
@@ -0,0 +1,123 @@
1
+ module Pbw
2
+ module Utils
3
+ class Chance
4
+ attr_accessor :chance, :score, :dice, :rolled
5
+
6
+ def self.read(s)
7
+ return nil if s.blank?
8
+ begin
9
+ if s.include?("in")
10
+ a = s.split(" in ")
11
+ chance = a[0].to_i
12
+ dice = Dice.read(a[1])
13
+ ::Pbw::Utils::Chance.new(chance,0,dice)
14
+ else
15
+ a = s.split(" on ")
16
+ score = a[0].to_i
17
+ dice = Dice.read(a[1])
18
+ ::Pbw::Utils::Chance.new(0,score,dice)
19
+ end
20
+ rescue
21
+ raise "Invalid chance format"
22
+ end
23
+ end
24
+
25
+ def initialize(chance = 0, score = 0, dice = Dice.new)
26
+ @chance = chance
27
+ @score = score
28
+ @dice = dice
29
+ @rolled = 0
30
+ end
31
+
32
+ def mongoize
33
+ to_s
34
+ end
35
+
36
+ def self.demongoize(object)
37
+ read(object.to_s)
38
+ end
39
+
40
+ def self.mongoize(object)
41
+ object.mongoize if object.is_a?(::Pbw::Utils::Chance)
42
+ end
43
+
44
+ def self.evolve(object)
45
+ object.mongoize if object.is_a?(::Pbw::Utils::Chance)
46
+ end
47
+
48
+ def blank?
49
+ chance == 0
50
+ end
51
+
52
+ def +(n)
53
+ ::Pbw::Utils::Chance.new(chance, score, (self.dice + n))
54
+ end
55
+
56
+ def -(n)
57
+ ::Pbw::Utils::Chance.new(chance, score, (self.dice - n))
58
+ end
59
+
60
+ def <(c)
61
+ return false unless c
62
+ if c.is_a?(Chance)
63
+ (self.probability_of_success < c.probability_of_success)
64
+ else
65
+ (self.probability_of_success < c.to_f)
66
+ end
67
+ end
68
+
69
+ def >(c)
70
+ return true unless c
71
+ if c.is_a?(Chance)
72
+ (self.probability_of_success > c.probability_of_success)
73
+ else
74
+ (self.probability_of_success > c.to_f)
75
+ end
76
+ end
77
+
78
+ def probability_of_success
79
+ return @probability_of_success if @probability_of_success
80
+ return 0 unless dice
81
+ prob = dice.probabilities
82
+ @probability_of_success = 0
83
+ prob.keys.each do |hit|
84
+ if chance && chance > 0
85
+ @probability_of_success += prob[hit] if hit <= chance && hit != dice.min
86
+ elsif score && score > 0
87
+ @probability_of_success += prob[hit] if hit >= score && hit != dice.min
88
+ end
89
+ end
90
+ @probability_of_success
91
+ end
92
+
93
+ def percentage_success
94
+ (probability_of_success * 100).round(0).to_i
95
+ end
96
+
97
+ def success?
98
+ return false unless dice
99
+ self.rolled = dice.roll
100
+ return false if self.rolled == dice.min
101
+ if score > 0
102
+ self.rolled >= score
103
+ else
104
+ self.rolled <= chance
105
+ end
106
+ end
107
+
108
+ def to_s
109
+ if score > 0
110
+ "#{score} on #{dice}"
111
+ elsif chance > 0
112
+ "#{chance} in #{dice}"
113
+ end
114
+ end
115
+
116
+ def to_pretty
117
+ "#{to_s} (#{percentage_success}% chance)"
118
+ end
119
+
120
+ alias_method :pp, :to_pretty
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,160 @@
1
+ module Pbw
2
+ module Utils
3
+ class Dice
4
+ include Comparable
5
+ attr_accessor :n, :d, :m
6
+
7
+ def self.read(s)
8
+ return nil if s.blank?
9
+ begin
10
+ a = s.include?("+") ? s.split("+") : (s.include?("-") ? s.split("-") : [s])
11
+ b = a[0].split("d")
12
+ n = b[0].to_i
13
+ d = b[1].to_i
14
+ if a.length > 1
15
+ m = s.include?("+") ? m = a[1].to_i : m = 0 - a[1].to_i
16
+ else
17
+ m = 0
18
+ end
19
+ ::Pbw::Utils::Dice.new(n,d,m)
20
+ rescue
21
+ raise "Invalid dice notation"
22
+ end
23
+ end
24
+
25
+ def initialize(n = 0, d = 0, m = 0)
26
+ @n = n
27
+ @d = d
28
+ @m = m
29
+ end
30
+
31
+ def mongoize
32
+ to_s
33
+ end
34
+
35
+ def self.demongoize(object)
36
+ read(object.to_s)
37
+ end
38
+
39
+ def self.mongoize(object)
40
+ object.mongoize if object.is_a?(::Pbw::Utils::Dice)
41
+ end
42
+
43
+ def self.evolve(object)
44
+ object.mongoize if object.is_a?(::Pbw::Utils::Dice)
45
+ end
46
+
47
+ def blank?
48
+ n == 0 || d == 0
49
+ end
50
+
51
+ def <(d)
52
+ if d.is_a?(Dice)
53
+ (self.average < d.average)
54
+ else
55
+ (self.average < d.to_f)
56
+ end
57
+ end
58
+
59
+ def >(d)
60
+ if d.is_a?(Dice)
61
+ (self.average > d.average)
62
+ else
63
+ (self.average > d.to_f)
64
+ end
65
+ end
66
+
67
+ def ==(d)
68
+ (self.d == d.d) && (self.n == d.n) && (self.m == d.m)
69
+ end
70
+
71
+ def <=>(d)
72
+ self.average <=> d.average
73
+ end
74
+
75
+ def +(d)
76
+ if d.respond_to?(:roll)
77
+ if d.d == self.d
78
+ ::Pbw::Utils::Dice.new((self.n + d.n), self.d, (self.m + d.m))
79
+ else
80
+ ::Pbw::Utils::Dice.new((self.n), self.d, (self.m + d.average))
81
+ end
82
+ else
83
+ ::Pbw::Utils::Dice.new((self.n), self.d, (self.m + d))
84
+ end
85
+ end
86
+
87
+ def -(d)
88
+ if d.respond_to?(:roll)
89
+ if d.d == self.d
90
+ ::Pbw::Utils::Dice.new((self.n - d.n), self.d, (self.m - d.m))
91
+ else
92
+ ::Pbw::Utils::Dice.new((self.n), self.d, (self.m - d.average))
93
+ end
94
+ else
95
+ ::Pbw::Utils::Dice.new((self.n), self.d, (self.m - d))
96
+ end
97
+ end
98
+
99
+ def average
100
+ apd = sum_possibilities / d.to_f
101
+ ((apd * n) + m)
102
+ end
103
+
104
+ def max
105
+ (n * d) + m
106
+ end
107
+
108
+ def min
109
+ n + m
110
+ end
111
+
112
+ def roll
113
+ t = 0
114
+ n.times{ t += (rand(d) + 1)}
115
+ t + m
116
+ end
117
+
118
+ def to_s
119
+ m == 0 ? "#{self.n}d#{self.d}" : (m > 0 ? "#{self.n}d#{self.d}+#{self.m}" : "#{self.n}d#{self.d}-#{self.m.abs}")
120
+ end
121
+
122
+ def probabilities
123
+ return @probabilities if @probabilities
124
+ @probabilities = {}
125
+ min = n
126
+ max = n * d
127
+ combinations = 1 * d**n
128
+
129
+ polys = []
130
+
131
+ (1..n).each do |r|
132
+ polys.push(::Pbw::Utils::Polynomial.mkroll(d))
133
+ end
134
+
135
+ biggun = ::Pbw::Utils::Polynomial.new([1]) #identity
136
+
137
+ polys.each do |p|
138
+ biggun *= p
139
+ end
140
+
141
+ (min..max).each do |deg|
142
+ coeff = biggun.coefficients[deg]
143
+ if !(coeff.nil? or coeff == 0)
144
+ px = coeff.to_f / combinations.to_f
145
+ @probabilities[(deg + m)] = px
146
+ end
147
+ end
148
+ @probabilities
149
+ end
150
+
151
+ private
152
+ def sum_possibilities
153
+ t = 0
154
+ d.times{|x| t += (x+1)}
155
+ t
156
+ end
157
+ end
158
+
159
+ end
160
+ end
@@ -0,0 +1,19 @@
1
+ module Pbw
2
+ module Utils
3
+ class Names
4
+ def initialize(data_file)
5
+ @list = []
6
+ File.open(data_file, 'r') do |file|
7
+ while line = file.gets do
8
+ @list << line.strip.titleize
9
+ end
10
+ end
11
+ @list
12
+ end
13
+
14
+ def random_name
15
+ @list.sample
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,39 @@
1
+ module Pbw
2
+ module Utils
3
+ class Polynomial
4
+ def initialize (yarr)
5
+ @coefficients = yarr
6
+ end
7
+
8
+ attr_reader :coefficients
9
+
10
+ def self.mkroll (n)
11
+ x = [0]
12
+
13
+ (1..n).each do |i|
14
+ x[i] = 1
15
+ end
16
+
17
+ ::Pbw::Utils::Polynomial.new(x)
18
+ end
19
+
20
+ def *(other)
21
+ noob = []
22
+
23
+ @coefficients.each_with_index do |xi, i|
24
+ other.coefficients.each_with_index do |yj, j|
25
+ cell = i + j
26
+
27
+ if noob[cell].nil?
28
+ noob[cell] = 0
29
+ end
30
+
31
+ noob[cell] += xi * yj
32
+ end
33
+ end
34
+
35
+ ::Pbw::Utils::Polynomial.new(noob)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,8 @@
1
+ module Pbw
2
+ module Utils
3
+ require 'pbw/utils/polynomial'
4
+ require 'pbw/utils/dice'
5
+ require 'pbw/utils/chance'
6
+ require 'pbw/utils/names'
7
+ end
8
+ end
@@ -1,3 +1,3 @@
1
1
  module Pbw
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pbw
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seyed P. Razavi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-30 00:00:00.000000000 Z
11
+ date: 2013-07-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -229,6 +229,7 @@ files:
229
229
  - app/views/layouts/pbw/application.html.erb
230
230
  - config/initializers/devise.rb
231
231
  - config/initializers/mongoid_accessible_attributes.rb
232
+ - config/initializers/string_extensions.rb
232
233
  - config/locales/devise.en.yml
233
234
  - config/locales/pbw.en.yml
234
235
  - config/routes.rb
@@ -285,6 +286,11 @@ files:
285
286
  - lib/pbw/engine.rb
286
287
  - lib/pbw/tick.rb
287
288
  - lib/pbw/update.rb
289
+ - lib/pbw/utils/chance.rb
290
+ - lib/pbw/utils/dice.rb
291
+ - lib/pbw/utils/names.rb
292
+ - lib/pbw/utils/polynomial.rb
293
+ - lib/pbw/utils/utils.rb
288
294
  - lib/pbw/version.rb
289
295
  - lib/pbw.rb
290
296
  - lib/tasks/pbw_tasks.rake