die_roller 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rubocop.yml +18 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +83 -0
- data/Rakefile +20 -0
- data/Steepfile +37 -0
- data/lib/die_roller/parser.rb +27 -0
- data/lib/die_roller/result.rb +31 -0
- data/lib/die_roller/roll.rb +19 -0
- data/lib/die_roller/roller.rb +21 -0
- data/lib/die_roller/version.rb +5 -0
- data/lib/die_roller.rb +35 -0
- data/sig/die_roll.rbs +45 -0
- metadata +60 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d5579717cc87b41bbabb38aec9c014857cf7abb2902e3110cd598845f6aef53f
|
4
|
+
data.tar.gz: be29b17a1c8c292f52db055e419002ed677aabe288ab51fdb9eeeda622f2685f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ea71769eb678c7844edcccadc6b608557b24b6797fd28dcb5e94c256eea6fa85786bf7fe3b3c3f9b11a2be98ce072ffd5b9e85f455b76c5d7dff6fba657e211e
|
7
|
+
data.tar.gz: f287f78fd9d7dbccbc6838a07ec2e9853f7c7a756437d33724cf6039de65b6277ec46f04fce3fa0d36d88c049dc29a11b3aa2781db531b37e3fd66db51f4fb72
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-minitest
|
3
|
+
- rubocop-rake
|
4
|
+
|
5
|
+
AllCops:
|
6
|
+
TargetRubyVersion: 3.0
|
7
|
+
NewCops: enable
|
8
|
+
|
9
|
+
Style/StringLiterals:
|
10
|
+
Enabled: true
|
11
|
+
EnforcedStyle: double_quotes
|
12
|
+
|
13
|
+
Style/StringLiteralsInInterpolation:
|
14
|
+
Enabled: true
|
15
|
+
EnforcedStyle: double_quotes
|
16
|
+
|
17
|
+
Layout/LineLength:
|
18
|
+
Max: 120
|
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Tony Drake
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# DieRoller
|
2
|
+
|
3
|
+
`DieRoller` is a simple dice notation parser and roller written in pure Ruby.
|
4
|
+
|
5
|
+
The gem supports the basic `AdX` dice roll notation (ie "2d6" meaning "2 6-sided dice").
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
10
|
+
|
11
|
+
Install the gem and add to the application's Gemfile by executing:
|
12
|
+
|
13
|
+
$ bundle add die_roller
|
14
|
+
|
15
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
16
|
+
|
17
|
+
$ gem install die_roller
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
The main interface is through `DieRoller.roll` which takes a string containing only dice notation. Multiple dice may be defined deliminated by whitespace.
|
22
|
+
|
23
|
+
Examples:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
DieRoller.roll("2d8")
|
27
|
+
DieRoller.roll("2d8 4d6")
|
28
|
+
DieRoller.roll(" 2d4 3d8")
|
29
|
+
```
|
30
|
+
|
31
|
+
If no number of dice are given, 1 is assumed:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
DieRoller.roll("d6") # Same as entering "1d6"
|
35
|
+
```
|
36
|
+
|
37
|
+
If parsing the string fails, `DieRoller::ParseError` will be raised:
|
38
|
+
|
39
|
+
```sh
|
40
|
+
> DieRoller.roll("1d6 badinput")
|
41
|
+
=> Invalid input 'badinput' (DieRoller::ParseError)
|
42
|
+
```
|
43
|
+
|
44
|
+
At least 4 sides must be declared per die, else `DieRoller::DieSizeError` is raised:
|
45
|
+
|
46
|
+
```sh
|
47
|
+
> DieRoller.roll("3d3 1d6")
|
48
|
+
=> Die size too small for '3d3' (DieRoller::DieSizeError)
|
49
|
+
```
|
50
|
+
|
51
|
+
## Development
|
52
|
+
|
53
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
54
|
+
|
55
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
56
|
+
|
57
|
+
## Contributing
|
58
|
+
|
59
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/t27duck/die_roller.
|
60
|
+
|
61
|
+
Pull requests will only be accepted if they meet all the following criteria (ran by CI):
|
62
|
+
|
63
|
+
1. Tests pass. This can be verified with:
|
64
|
+
|
65
|
+
```
|
66
|
+
$ bundle exec rake test
|
67
|
+
```
|
68
|
+
|
69
|
+
2. Code must conform to the [RuboCop rules](https://github.com/rubocop/rubocop#readme). This can be verified with:
|
70
|
+
|
71
|
+
```
|
72
|
+
$ bundle exec rake rubocop
|
73
|
+
```
|
74
|
+
|
75
|
+
3. RBS type signatures (in `sig/die_roller.rbs`). This can be verified with:
|
76
|
+
|
77
|
+
```
|
78
|
+
$ bundle exec rake steep
|
79
|
+
```
|
80
|
+
|
81
|
+
## License
|
82
|
+
|
83
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "minitest/test_task"
|
5
|
+
|
6
|
+
Minitest::TestTask.create
|
7
|
+
|
8
|
+
require "rubocop/rake_task"
|
9
|
+
|
10
|
+
RuboCop::RakeTask.new
|
11
|
+
|
12
|
+
require "steep"
|
13
|
+
require "steep/cli"
|
14
|
+
|
15
|
+
desc "Type check with Steep"
|
16
|
+
task :steep do
|
17
|
+
Steep::CLI.new(argv: ["check"], stdout: $stdout, stderr: $stderr, stdin: $stdin).run
|
18
|
+
end
|
19
|
+
|
20
|
+
task default: %i[test rubocop steep]
|
data/Steepfile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# D = Steep::Diagnostic
|
4
|
+
#
|
5
|
+
# target :lib do
|
6
|
+
# signature "sig"
|
7
|
+
#
|
8
|
+
# check "lib" # Directory name
|
9
|
+
# check "Gemfile" # File name
|
10
|
+
# check "app/models/**/*.rb" # Glob
|
11
|
+
# # ignore "lib/templates/*.rb"
|
12
|
+
#
|
13
|
+
# # library "pathname" # Standard libraries
|
14
|
+
# # library "strong_json" # Gems
|
15
|
+
#
|
16
|
+
# # configure_code_diagnostics(D::Ruby.default) # `default` diagnostics setting (applies by default)
|
17
|
+
# # configure_code_diagnostics(D::Ruby.strict) # `strict` diagnostics setting
|
18
|
+
# # configure_code_diagnostics(D::Ruby.lenient) # `lenient` diagnostics setting
|
19
|
+
# # configure_code_diagnostics(D::Ruby.silent) # `silent` diagnostics setting
|
20
|
+
# # configure_code_diagnostics do |hash| # You can setup everything yourself
|
21
|
+
# # hash[D::Ruby::NoMethod] = :information
|
22
|
+
# # end
|
23
|
+
# end
|
24
|
+
|
25
|
+
# target :test do
|
26
|
+
# signature "sig", "sig-private"
|
27
|
+
#
|
28
|
+
# check "test"
|
29
|
+
#
|
30
|
+
# # library "pathname" # Standard libraries
|
31
|
+
# end
|
32
|
+
|
33
|
+
target :lib do
|
34
|
+
signature "sig"
|
35
|
+
check "lib"
|
36
|
+
configure_code_diagnostics(Steep::Diagnostic::Ruby.default) # strict or all_error
|
37
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DieRoller
|
4
|
+
# Accpets a string input, and parses it for die tokens.
|
5
|
+
class Parser
|
6
|
+
attr_reader :tokens
|
7
|
+
|
8
|
+
def initialize(input)
|
9
|
+
generate_tokens(input)
|
10
|
+
validate_tokens
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def generate_tokens(input)
|
16
|
+
@tokens = input.split.reject { |t| t == "" || t.nil? }.map(&:downcase)
|
17
|
+
end
|
18
|
+
|
19
|
+
def validate_tokens
|
20
|
+
@tokens.each do |token|
|
21
|
+
raise ParseError, "Invalid input '#{token}'" unless token.match?(DICE_SYNTAX_REGEX)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private_constant :Parser
|
27
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DieRoller
|
4
|
+
# Holds the final result of rolls.
|
5
|
+
class Result
|
6
|
+
attr_reader :tokens, :rolls
|
7
|
+
|
8
|
+
def initialize(rolls:, tokens:)
|
9
|
+
@rolls = rolls
|
10
|
+
@tokens = tokens
|
11
|
+
end
|
12
|
+
|
13
|
+
def pairs
|
14
|
+
@pairs ||= dice.zip(values).map(&:freeze).freeze
|
15
|
+
end
|
16
|
+
|
17
|
+
def values
|
18
|
+
@values ||= @rolls.map(&:value).freeze
|
19
|
+
end
|
20
|
+
|
21
|
+
def dice
|
22
|
+
@dice ||= @rolls.map(&:die).freeze
|
23
|
+
end
|
24
|
+
|
25
|
+
def total
|
26
|
+
@total ||= @rolls.sum(&:value)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private_constant :Result
|
31
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DieRoller
|
4
|
+
# The result of a single die roll.
|
5
|
+
class Roll
|
6
|
+
attr_reader :sides, :value
|
7
|
+
|
8
|
+
def initialize(sides:, value:)
|
9
|
+
@sides = sides
|
10
|
+
@value = value
|
11
|
+
end
|
12
|
+
|
13
|
+
def die
|
14
|
+
"d#{sides}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private_constant :Roll
|
19
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DieRoller
|
4
|
+
# Responsible for rolling dice given `tokens` from a `DieRoller::Parser`.
|
5
|
+
class Roller
|
6
|
+
def self.roll(parser)
|
7
|
+
parser.tokens.map do |token|
|
8
|
+
numbers = token.split("d")
|
9
|
+
sides = numbers[1].to_i
|
10
|
+
|
11
|
+
raise DieSizeError, "Die size too small for '#{token}'" if sides < MINIMUM_DIE_SIZE
|
12
|
+
|
13
|
+
die_count = numbers[0].to_i
|
14
|
+
die_count = 1 if die_count.zero?
|
15
|
+
Array.new(die_count) { Roll.new(sides: sides, value: rand(sides) + 1) }
|
16
|
+
end.flatten
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
private_constant :Roller
|
21
|
+
end
|
data/lib/die_roller.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "die_roller/version"
|
4
|
+
require_relative "die_roller/parser"
|
5
|
+
require_relative "die_roller/result"
|
6
|
+
require_relative "die_roller/roll"
|
7
|
+
require_relative "die_roller/roller"
|
8
|
+
|
9
|
+
# A simple parser to roll one or more dice.
|
10
|
+
# DieRoller.roll("input")
|
11
|
+
#
|
12
|
+
# Example valid inputs:
|
13
|
+
# "1d6"
|
14
|
+
# "d6" # Defaults to 1 die
|
15
|
+
# "2d8" # 2 8-sided dice
|
16
|
+
# "1d6 2d4" # 1 6-sided die and 2 4-sided dice
|
17
|
+
# "2D100"
|
18
|
+
#
|
19
|
+
# Multiple dice can be separated with whitespace:
|
20
|
+
# "1d6 1d8"
|
21
|
+
#
|
22
|
+
# `.roll` returns a `DieRoller::Result` object with info regarding the roll(s).
|
23
|
+
module DieRoller
|
24
|
+
DICE_SYNTAX_REGEX = /\A\d*d\d+\z/
|
25
|
+
MINIMUM_DIE_SIZE = 4
|
26
|
+
|
27
|
+
class ParseError < StandardError; end
|
28
|
+
class DieSizeError < StandardError; end
|
29
|
+
|
30
|
+
def self.roll(input)
|
31
|
+
parser = Parser.new(input)
|
32
|
+
rolls = Roller.roll(parser)
|
33
|
+
Result.new(rolls: rolls, tokens: parser.tokens)
|
34
|
+
end
|
35
|
+
end
|
data/sig/die_roll.rbs
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
module DieRoller
|
2
|
+
VERSION: String
|
3
|
+
MINIMUM_DIE_SIZE: Integer
|
4
|
+
DICE_SYNTAX_REGEX: Regexp
|
5
|
+
|
6
|
+
class ParseError < StandardError
|
7
|
+
end
|
8
|
+
|
9
|
+
class DieSizeError < StandardError
|
10
|
+
end
|
11
|
+
|
12
|
+
class Roll
|
13
|
+
attr_reader sides: Array[String]
|
14
|
+
attr_reader value: Integer
|
15
|
+
|
16
|
+
def initialize: (sides: Integer, value: Integer) -> void
|
17
|
+
def die: -> String
|
18
|
+
def total: -> Integer
|
19
|
+
end
|
20
|
+
|
21
|
+
class Parser
|
22
|
+
attr_reader tokens: Array[String]
|
23
|
+
|
24
|
+
def initialize: (String) -> void
|
25
|
+
|
26
|
+
private
|
27
|
+
def generate_tokens: (String) -> void
|
28
|
+
def validate_tokens: -> void
|
29
|
+
end
|
30
|
+
|
31
|
+
class Roller
|
32
|
+
def self.roll: (Parser parser) -> Array[Roll]
|
33
|
+
end
|
34
|
+
|
35
|
+
class Result
|
36
|
+
attr_reader tokens: Array[String]
|
37
|
+
attr_reader rolls: Array[Roll]
|
38
|
+
|
39
|
+
def initialize: (rolls: Array[Roll], tokens: Array[String]) -> void
|
40
|
+
def pairs: -> Array[[String, (Integer | nil)]]
|
41
|
+
def dice: -> Array[String]
|
42
|
+
def values: -> Array[Integer]
|
43
|
+
def total: -> Integer
|
44
|
+
end
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: die_roller
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tony Drake
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-01-25 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A simple dice notation parser and roller.
|
14
|
+
email:
|
15
|
+
- t27duck@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".rubocop.yml"
|
21
|
+
- CHANGELOG.md
|
22
|
+
- LICENSE.txt
|
23
|
+
- README.md
|
24
|
+
- Rakefile
|
25
|
+
- Steepfile
|
26
|
+
- lib/die_roller.rb
|
27
|
+
- lib/die_roller/parser.rb
|
28
|
+
- lib/die_roller/result.rb
|
29
|
+
- lib/die_roller/roll.rb
|
30
|
+
- lib/die_roller/roller.rb
|
31
|
+
- lib/die_roller/version.rb
|
32
|
+
- sig/die_roll.rbs
|
33
|
+
homepage: https://github.com/t27duck/die_roller
|
34
|
+
licenses:
|
35
|
+
- MIT
|
36
|
+
metadata:
|
37
|
+
rubygems_mfa_required: 'true'
|
38
|
+
homepage_uri: https://github.com/t27duck/die_roller
|
39
|
+
source_code_uri: https://github.com/t27duck/die_roller
|
40
|
+
changelog_uri: https://github.com/t27duck/die_roller/CHANGELOG.md
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '3.0'
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubygems_version: 3.5.3
|
57
|
+
signing_key:
|
58
|
+
specification_version: 4
|
59
|
+
summary: A simple dice notation parser and roller.
|
60
|
+
test_files: []
|