dice_stats 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 +4 -4
- data/lib/Dice.rb +1 -10
- data/lib/Dice_Set.rb +2 -11
- data/lib/Internal_Utilities/Math_Utilities.rb +0 -52
- data/lib/dice_stats.rb +1 -7
- metadata +1 -2
- data/lib/Internal_Utilities/probability_cache_db.rb +0 -188
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b91a68b4ea5dfe5d5f1cd908ec80f27860198e25
|
|
4
|
+
data.tar.gz: 0a9edddebaec43166a0681b2635ea3cac151eee8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2977a7f1c1f4070932cfb529bb753f3437e4e398040262ce8faa5021087706cc2b33da85715bc1e8c2dc610edcba9b4db423a2fb781b2b1c9eba28992cf7c920
|
|
7
|
+
data.tar.gz: 3773d2cab87fbe70f29bdd1086b3c2a2d4027c6dd4ca5c7f1305956d21801b8410aa155a8e069e812aaebf4f0a296909a9cbe3eee5a1276a73655ac490e63ad8
|
data/lib/Dice.rb
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
require 'bigdecimal'
|
|
2
2
|
require 'Internal_Utilities/Math_Utilities'
|
|
3
|
-
require 'Internal_Utilities/probability_cache_db'
|
|
4
3
|
|
|
5
4
|
module Dice_Stats
|
|
6
5
|
|
|
@@ -28,15 +27,7 @@ module Dice_Stats
|
|
|
28
27
|
if (@count < 0 || @sides < 0)
|
|
29
28
|
#error
|
|
30
29
|
else
|
|
31
|
-
|
|
32
|
-
if Cache.checkDice(@count.to_s + "d" + @sides.to_s)
|
|
33
|
-
@probability_distribution = Cache.getDice(@count.to_s + "d" + @sides.to_s)
|
|
34
|
-
else
|
|
35
|
-
@probability_distribution = calculate_probability_distribution
|
|
36
|
-
Cache.addDice(@count.to_s + "d" + @sides.to_s, @probability_distribution, (Time.now - t1).round(5))
|
|
37
|
-
end
|
|
38
|
-
#t2 = Time.now
|
|
39
|
-
#puts "Probabilities determined in #{(t2-t1).round(5)}"
|
|
30
|
+
@probability_distribution = calculate_probability_distribution
|
|
40
31
|
end
|
|
41
32
|
end
|
|
42
33
|
|
data/lib/Dice_Set.rb
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
require 'Dice'
|
|
2
2
|
require 'Internal_Utilities/Math_Utilities'
|
|
3
3
|
require 'Internal_Utilities/Filtered_distribution'
|
|
4
|
-
require 'Internal_Utilities/probability_cache_db'
|
|
5
4
|
|
|
6
5
|
module Dice_Stats
|
|
7
6
|
|
|
@@ -52,16 +51,8 @@ module Dice_Stats
|
|
|
52
51
|
else
|
|
53
52
|
@dice.sort! { |d1,d2| d2.sides <=> d1.sides }
|
|
54
53
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
@probability_distribution = Cache.getDice(self.clean_string(false))
|
|
58
|
-
else
|
|
59
|
-
@probability_distribution = combine_probability_distributions
|
|
60
|
-
Cache.addDice(self.clean_string(false), @probability_distribution, (Time.now - t1).round(5))
|
|
61
|
-
end
|
|
62
|
-
#t2 = Time.now
|
|
63
|
-
#puts "Probabilities determined in #{(t2-t1).round(5)}"
|
|
64
|
-
|
|
54
|
+
@probability_distribution = combine_probability_distributions
|
|
55
|
+
|
|
65
56
|
if (@probability_distribution.inject(0) { |memo,(k,v)| memo + v }.round(3).to_f != 1.0)
|
|
66
57
|
#puts "Error in probability distrubtion."
|
|
67
58
|
end
|
|
@@ -111,57 +111,5 @@ module Dice_Stats::Internal_Utilities
|
|
|
111
111
|
result
|
|
112
112
|
end
|
|
113
113
|
|
|
114
|
-
##
|
|
115
|
-
# Deprecated / incomplete.
|
|
116
|
-
# This method runs some quick tests on the basic math helper functions
|
|
117
|
-
# to make sure that the Factorial and Choose methods are working as expected.
|
|
118
|
-
def self.Test_Suite
|
|
119
|
-
#Choose
|
|
120
|
-
puts "Testing Choose edge cases..."
|
|
121
|
-
Test_Choose(6, 10, 1)
|
|
122
|
-
Test_Choose(6, 6, 1)
|
|
123
|
-
Test_Choose(5, 0, 1)
|
|
124
|
-
Test_Choose(-1, 5, 1)
|
|
125
|
-
Test_Choose(5, -1, 1)
|
|
126
|
-
puts
|
|
127
|
-
puts "Testing Choose basic math..."
|
|
128
|
-
Test_Choose(5, 3, 10)
|
|
129
|
-
Test_Choose(6, 2, 15)
|
|
130
|
-
Test_Choose(14, 7, 3432)
|
|
131
|
-
Test_Choose(7, 2, 21)
|
|
132
|
-
Test_Choose(7, 5, 21)
|
|
133
|
-
Test_Choose(7, 5, 21)
|
|
134
|
-
Test_Choose(6, 3, 20)
|
|
135
|
-
|
|
136
|
-
#Factorial
|
|
137
|
-
puts
|
|
138
|
-
puts "Testing Factorial edge cases..."
|
|
139
|
-
Test_Factorial(0, 0)
|
|
140
|
-
Test_Factorial(-2, 0)
|
|
141
|
-
puts
|
|
142
|
-
puts "Testing Factorial basic math..."
|
|
143
|
-
Test_Factorial(6, 720)
|
|
144
|
-
Test_Factorial(5, 120)
|
|
145
|
-
Test_Factorial(4, 24)
|
|
146
|
-
Test_Factorial(3, 6)
|
|
147
|
-
Test_Factorial(2, 2)
|
|
148
|
-
end
|
|
149
|
-
|
|
150
|
-
##
|
|
151
|
-
# Helper function for testing the Choose method.
|
|
152
|
-
def self.Test_Choose(a, b, expected)
|
|
153
|
-
puts "(#{a} #{b}) => #{expected} (Actual: " + self.Choose(a, b).to_s + ")"
|
|
154
|
-
if (self.Choose(a, b) != expected)
|
|
155
|
-
puts "`--> ERROR. FAILING CASE."
|
|
156
|
-
end
|
|
157
|
-
end
|
|
158
|
-
|
|
159
|
-
## Helper function for testing the Factorial method.
|
|
160
|
-
def self.Test_Factorial(a, expected)
|
|
161
|
-
puts "#{a}! => #{expected} (Actual: " + self.Factorial(a).to_s + ")"
|
|
162
|
-
if (self.Factorial(a) != expected)
|
|
163
|
-
puts "`--> ERROR. FAILING CASE."
|
|
164
|
-
end
|
|
165
|
-
end
|
|
166
114
|
end
|
|
167
115
|
end
|
data/lib/dice_stats.rb
CHANGED
|
@@ -13,10 +13,4 @@ end
|
|
|
13
13
|
module Dice_Stats::Internal_Utilities
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
-
require 'Dice_Set'
|
|
17
|
-
|
|
18
|
-
##
|
|
19
|
-
# Instantiates the cache.
|
|
20
|
-
# The cache is an instance of a DB_cache_connection in the Internal_Utilities module.
|
|
21
|
-
# It is used to cache past results.
|
|
22
|
-
Dice_Stats::Cache = Dice_Stats::Internal_Utilities::DB_cache_connection.new
|
|
16
|
+
require 'Dice_Set'
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: dice_stats
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Matthew Foy
|
|
@@ -41,7 +41,6 @@ files:
|
|
|
41
41
|
- lib/Internal_Utilities/Arbitrary_base_counter.rb
|
|
42
42
|
- lib/Internal_Utilities/Filtered_distribution.rb
|
|
43
43
|
- lib/Internal_Utilities/Math_Utilities.rb
|
|
44
|
-
- lib/Internal_Utilities/probability_cache_db.rb
|
|
45
44
|
- lib/dice_stats.rb
|
|
46
45
|
homepage: http://matthewfoy.ca
|
|
47
46
|
licenses:
|
|
@@ -1,188 +0,0 @@
|
|
|
1
|
-
require 'sqlite3'
|
|
2
|
-
require 'bigdecimal'
|
|
3
|
-
|
|
4
|
-
module Dice_Stats
|
|
5
|
-
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
module Dice_Stats::Internal_Utilities
|
|
9
|
-
|
|
10
|
-
##
|
|
11
|
-
# This class represents the cache. The cache is used for storing previously calculated results.
|
|
12
|
-
# Some dice sets can take quite a while to calculate.
|
|
13
|
-
# Storing them drastically increases performance when a cache hit is found.
|
|
14
|
-
class DB_cache_connection
|
|
15
|
-
##
|
|
16
|
-
# The version of the gem. If this is updated, the DB_cache_connection#initialize method will drop and recreate the tables
|
|
17
|
-
@@Version = [0, 1, 0]
|
|
18
|
-
|
|
19
|
-
##
|
|
20
|
-
# The path of the sqlite3 db file.
|
|
21
|
-
@@Path = '/srv/Dice_Stats/'
|
|
22
|
-
|
|
23
|
-
##
|
|
24
|
-
# The name of the sqlite3 db file.
|
|
25
|
-
@@DB_name = 'probability_cache.db'
|
|
26
|
-
|
|
27
|
-
##
|
|
28
|
-
# For internal use only. Checks the database version and schema on startup.
|
|
29
|
-
def initialize
|
|
30
|
-
checkSchema
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
##
|
|
34
|
-
# Checks the database version and schema and creates the table structures.
|
|
35
|
-
def checkSchema
|
|
36
|
-
begin
|
|
37
|
-
db = SQLite3::Database.open(@@Path + @@DB_name)
|
|
38
|
-
db.execute "CREATE TABLE IF NOT EXISTS DiceConfig(Key TEXT PRIMARY KEY, Val TEXT);"
|
|
39
|
-
statement = db.prepare "SELECT Val FROM DiceConfig WHERE Key = 'Version';"
|
|
40
|
-
row = statement.execute.next
|
|
41
|
-
|
|
42
|
-
if !row
|
|
43
|
-
db.execute "INSERT INTO DiceConfig (Key, Val) VALUES ('Version', '#{@@Version.join('.')}');"
|
|
44
|
-
createSchema(db, true)
|
|
45
|
-
else
|
|
46
|
-
version = row[0]
|
|
47
|
-
version = version.split('.')
|
|
48
|
-
version.map! { |i| i.to_i }
|
|
49
|
-
if (@@Version[0] != version[0] || @@Version[1] != version[1] || @@Version[2] != version[2])
|
|
50
|
-
# Version of database is different, drop and recreate!
|
|
51
|
-
createSchema(db, true)
|
|
52
|
-
db.execute "UPDATE DiceConfig SET Version = '#{@@Version.join('.')}' WHERE Key = 'Version'"
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
end
|
|
56
|
-
rescue SQLite3::Exception => e
|
|
57
|
-
puts e
|
|
58
|
-
ensure
|
|
59
|
-
statement.close if statement
|
|
60
|
-
db.close if db
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
##
|
|
66
|
-
# Creates the tables if they don't already exist.
|
|
67
|
-
# If +drop+ is set to true, the tables will be dropped first.
|
|
68
|
-
def createSchema(db, drop=false)
|
|
69
|
-
db.execute "DROP TABLE IF EXISTS DiceSet;" if drop
|
|
70
|
-
db.execute "CREATE TABLE IF NOT EXISTS DiceSet (Id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT UNIQUE, TimeElapsed DECIMAL)"
|
|
71
|
-
|
|
72
|
-
db.execute "DROP TABLE IF EXISTS RollProbability;" if drop
|
|
73
|
-
db.execute "CREATE TABLE IF NOT EXISTS RollProbability (Id INTEGER PRIMARY KEY AUTOINCREMENT, DiceSetId INT, Value INTEGER, Probability DECIMAL)"
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
##
|
|
77
|
-
# Drops and recreates the schema for the purpose of clearing the cache.
|
|
78
|
-
def purge
|
|
79
|
-
begin
|
|
80
|
-
db = SQLite3::Database.open(@@Path + @@DB_name)
|
|
81
|
-
puts "Purging cache..."
|
|
82
|
-
createSchema(db, true)
|
|
83
|
-
rescue SQLite3::Exception => e
|
|
84
|
-
puts e
|
|
85
|
-
ensure
|
|
86
|
-
db.close if db
|
|
87
|
-
end
|
|
88
|
-
end
|
|
89
|
-
|
|
90
|
-
##
|
|
91
|
-
# Checks the cache to see if the pattern has been previously calculated.
|
|
92
|
-
def checkDice(dice_pattern)
|
|
93
|
-
begin
|
|
94
|
-
db = SQLite3::Database.open(@@Path + @@DB_name)
|
|
95
|
-
|
|
96
|
-
statement = db.prepare "SELECT Id FROM DiceSet WHERE Name = '#{dice_pattern}'"
|
|
97
|
-
|
|
98
|
-
return statement.execute.count >= 1
|
|
99
|
-
|
|
100
|
-
rescue SQLite3::Exception => e
|
|
101
|
-
puts e
|
|
102
|
-
ensure
|
|
103
|
-
statement.close if statement
|
|
104
|
-
db.close if db
|
|
105
|
-
end
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
##
|
|
109
|
-
# Caches a dice pattern for future retrieval.
|
|
110
|
-
def addDice(dice_pattern, probability_distribution, timeElapsed=0.0)
|
|
111
|
-
begin
|
|
112
|
-
db = SQLite3::Database.open(@@Path + @@DB_name)
|
|
113
|
-
db.execute "INSERT INTO DiceSet (Name) VALUES ('#{dice_pattern}')"
|
|
114
|
-
diceset_id = db.last_insert_row_id
|
|
115
|
-
|
|
116
|
-
values = []
|
|
117
|
-
probability_distribution.each { |k,v|
|
|
118
|
-
values << "(#{diceset_id}, #{k}, #{v})"
|
|
119
|
-
}
|
|
120
|
-
#puts "Values:"
|
|
121
|
-
#puts values
|
|
122
|
-
|
|
123
|
-
insert = "INSERT INTO RollProbability (DiceSetId, Value, Probability) VALUES " + values.join(", ")
|
|
124
|
-
|
|
125
|
-
db.execute insert
|
|
126
|
-
rescue SQLite3::Exception => e
|
|
127
|
-
puts e
|
|
128
|
-
ensure
|
|
129
|
-
db.close if db
|
|
130
|
-
end
|
|
131
|
-
end
|
|
132
|
-
|
|
133
|
-
##
|
|
134
|
-
# Retrieves the probability distribution for a dice pattern from the cache.
|
|
135
|
-
def getDice(dice_pattern)
|
|
136
|
-
begin
|
|
137
|
-
db = SQLite3::Database.open(@@Path + @@DB_name)
|
|
138
|
-
|
|
139
|
-
statement1 = db.prepare "SELECT Id FROM DiceSet WHERE Name = '#{dice_pattern}'"
|
|
140
|
-
diceset_id = statement1.execute.first[0]
|
|
141
|
-
|
|
142
|
-
statement2 = db.prepare "SELECT Value, Probability FROM RollProbability WHERE DiceSetId = #{diceset_id}"
|
|
143
|
-
|
|
144
|
-
rs = statement2.execute
|
|
145
|
-
result = {}
|
|
146
|
-
rs.each { |row|
|
|
147
|
-
result[row[0]] = BigDecimal.new(row[1], 15)
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
return result
|
|
151
|
-
|
|
152
|
-
rescue SQLite3::Exception => e
|
|
153
|
-
puts e
|
|
154
|
-
ensure
|
|
155
|
-
statement1.close if statement1
|
|
156
|
-
statement2.close if statement2
|
|
157
|
-
db.close if db
|
|
158
|
-
end
|
|
159
|
-
end
|
|
160
|
-
|
|
161
|
-
##
|
|
162
|
-
# Retrieves how long a cached result originally took to calculate.
|
|
163
|
-
def getElapsed(dice_pattern)
|
|
164
|
-
begin
|
|
165
|
-
db = SQLite3::Database.open(@@Path + @@DB_name)
|
|
166
|
-
statement = db.prepare "SELECT TimeElapsed FROM DiceSet WHERE Name = '#{dice_pattern}'"
|
|
167
|
-
|
|
168
|
-
rs = statement.execute
|
|
169
|
-
result = nil
|
|
170
|
-
rs.each { |row|
|
|
171
|
-
result = row[0]
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
if (result == nil)
|
|
175
|
-
return 0.0
|
|
176
|
-
else
|
|
177
|
-
return result
|
|
178
|
-
end
|
|
179
|
-
|
|
180
|
-
rescue SQLite3::Exception => e
|
|
181
|
-
puts e
|
|
182
|
-
ensure
|
|
183
|
-
statement.close if statement
|
|
184
|
-
db.close if db
|
|
185
|
-
end
|
|
186
|
-
end
|
|
187
|
-
end
|
|
188
|
-
end
|