dice-roller 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/dice.rb +63 -0
  3. metadata +114 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d16a3cb49a6548b9daa5035e542a38747f9f3d54
4
+ data.tar.gz: ec276691715c2e5a19efd889f4b881f805300017
5
+ SHA512:
6
+ metadata.gz: e0704aa1ad46f1cdf10378f5d96e4144488e218ecf3e7b9cfd771b80cdc9f254372b856f975c6f8aec3bbc9e525c822e6d2d82ffd69dd77271c534f9338e0fd0
7
+ data.tar.gz: 570091cf6530b7219f4fcbc402348775859e87323fe89dca17c79bacc165c0ab2467a3f0406bd4ab701c322e511b5c777dd9ad428951270845ad8a4c5adf107c
@@ -0,0 +1,63 @@
1
+ require 'active_support/all'
2
+ require 'expression_parser'
3
+
4
+ class Dice
5
+ InvalidModifier = Class.new(StandardError)
6
+
7
+ class << self
8
+ def roll(str)
9
+ new(str).result
10
+ end
11
+ end
12
+
13
+ attr_reader :raw_expression, :rolls, :result, :calculator
14
+
15
+ def initialize(str)
16
+ @raw_expression = str
17
+ @rolls = []
18
+
19
+ @result = Integer(ExpressionParser::Parser.new.parse(parse))
20
+ end
21
+
22
+ private
23
+
24
+ def dice_groups(str)
25
+ str.scan(/\d{,3}d\d{,3}[^(\ |\+)]+/)
26
+ end
27
+
28
+ def parse
29
+ str = raw_expression.dup
30
+
31
+ # Match groups of dices, calculate the groups and reinsert them
32
+ dice_groups(str).each do |group|
33
+ # Roll each dice
34
+ dices, sides, modifier, modifier_amount = group.scan(/(\d+)d(\d+)(\D+)(\d*)/).flatten
35
+ results = Integer(dices).times.collect { rand(Integer(sides)) + 1 }.sort
36
+
37
+ # If a modifier is present, handle that
38
+ if modifier.present?
39
+ modifier_amount = Integer(modifier_amount.presence || 1)
40
+
41
+ case modifier
42
+ when 'dl' # Drop lowest N
43
+ modifier_amount.times { results.shift }
44
+ when 'dh' # Drop highest N
45
+ modifier_amount.times { results.pop }
46
+ when 'l' # Pick lowest
47
+ results = [results.first]
48
+ when 'h' # Pick highest
49
+ results = [results.last]
50
+ else
51
+ raise InvalidModifier, "#{modifier} isn't a valid modifier for #{group}"
52
+ end
53
+ end
54
+
55
+ # Sum dice results and reinsert the results in the original string
56
+ result = results.inject(0) { |sum, x| sum + x }
57
+ @rolls << result
58
+ str.gsub!(group, result.to_s)
59
+ end
60
+
61
+ str
62
+ end
63
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dice-roller
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Emil Kampp
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: expression_parser
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.9'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.10'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.4'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.11'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.11'
83
+ description: A clean way to roll complex dice structures
84
+ email: emil@kampp.me
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - lib/dice.rb
90
+ homepage: http://emil.kampp.me
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.5.2
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: A ruby dice roller
114
+ test_files: []