Compare commits

..

No commits in common. "1556ebe9c7d4308ed42c0ab3a0eb0c5fd320b7f0" and "3a41506fb56691c6866c4c74c6e5e5b33cac9295" have entirely different histories.

7 changed files with 31 additions and 72 deletions

View File

@ -22,8 +22,6 @@ gem "mission_control-jobs"
gem "active_storage_validations"
gem "dice-roller", git: "https://github.com/Tablewarez/always_rolling"
group :development, :test do
gem "debug", platforms: %i[ mri windows ]
gem "dotenv"

View File

@ -1,14 +1,3 @@
GIT
remote: https://github.com/Tablewarez/always_rolling
revision: 44ed421d854dc41e6021da3ab9427455d9c807ba
specs:
dice-roller (0.0.5)
activesupport (>= 4.2)
expression_parser (~> 0.9)
pry (~> 0.10)
rspec (~> 3.4)
simplecov (~> 0.11)
GEM
remote: https://rubygems.org/
specs:
@ -111,7 +100,6 @@ GEM
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
childprocess (5.0.0)
coderay (1.1.3)
concurrent-ruby (1.2.3)
connection_pool (2.4.1)
crass (1.0.6)
@ -119,14 +107,11 @@ GEM
debug (1.9.2)
irb (~> 1.10)
reline (>= 0.3.8)
diff-lcs (1.5.1)
docile (1.4.0)
dotenv (3.1.2)
drb (2.2.1)
erubi (1.12.0)
et-orbi (1.2.11)
tzinfo
expression_parser (0.9.0)
ffi (1.16.3)
fugit (1.10.1)
et-orbi (~> 1, >= 1.2.7)
@ -166,7 +151,6 @@ GEM
net-smtp
marcel (1.0.4)
matrix (0.4.2)
method_source (1.1.0)
mini_magick (4.12.0)
mini_mime (1.1.5)
minitest (5.23.1)
@ -209,9 +193,6 @@ GEM
activesupport (>= 7.0.0)
rack
railties (>= 7.0.0)
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
psych (5.1.2)
stringio
public_suffix (5.0.5)
@ -265,19 +246,6 @@ GEM
io-console (~> 0.5)
rexml (3.2.8)
strscan (>= 3.0.9)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.0)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.1)
rubocop (1.64.0)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
@ -316,12 +284,6 @@ GEM
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
simplecov (0.22.0)
docile (~> 1.1)
simplecov-html (~> 0.11)
simplecov_json_formatter (~> 0.1)
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.4)
solid_queue (0.3.1)
activejob (>= 7.1)
activerecord (>= 7.1)
@ -369,7 +331,6 @@ DEPENDENCIES
bootsnap
capybara
debug
dice-roller!
dotenv
image_processing (~> 1.2)
importmap-rails

View File

@ -1,7 +1,5 @@
# frozen_string_literal: true
require "dice"
class DiceRoller
attr_reader :dice
attr_reader :result
@ -13,8 +11,21 @@ class DiceRoller
end
def roll
roll_command = parse_roll_command_references(roll_command)
Dice.roll(roll_command)
result = 0
operator = nil
roll_command_parts.each do |part|
case part
when /\A\d*d\d+\z/
operator.nil? ? (result += roll_dice(part)) : (result = result.send(operator, roll_dice(part)))
when /\A\d+\z/
operator.nil? ? (result += part.to_i) : (result = result.send(operator, part.to_i))
when "self"
operator.nil? ? (result += @stat) : (result = result.send(operator, @stat))
when /\A[+\-*\/]\z/
operator = part
end
end
result
end
def valid?
@ -39,8 +50,10 @@ class DiceRoller
private
def roll_delimiters
[ "+", "-", "*", "/" ]
def roll_command_parts
@roll_command.scan(/([+\-*\/])|(\d*d\d+)|(\d+)|(self)/xi)
.flatten
.compact_blank
end
def roll_dice(command)
@ -56,18 +69,4 @@ class DiceRoller
end
result
end
def parse_roll_command_references(roll_command)
roll_command = @roll_command.include?("self") ? @roll_command.gsub("self", @stat.to_s) : @roll_command
roll_command.split(Regexp.union(roll_delimiters)).each do |part|
next if part.match?(/\A\d*\z/)
next if part.match?(/\A\d*d\d*\z/)
value = CharacterSheetFeature.find_by(slug: part).featurable.value
raise "#{part} not found" if value.blank?
roll_command.gsub!(part, value.to_s)
end
roll_command
end
end

View File

@ -1,14 +1,14 @@
strength:
name: Strength
value: 10
roll_command: 1d20+self
roll_command: d20+self
dexterity:
name: Dexterity
value: 10
roll_command: 1d20+self
roll_command: d20+self
constitution:
name: Constitution
value: 10
roll_command: 1d20+self
roll_command: d20+self

View File

@ -27,7 +27,7 @@ class StatTest < ActiveSupport::TestCase
test "rolls with roll_command" do
stat = stats(:strength)
stat.update(roll_command: "1d6")
stat.roll_command = "1d6"
100.times { assert (1..6).cover?(stat.roll(tables(:dnd_table))) }
end
end

View File

@ -8,15 +8,15 @@ class DiceRollerTest < ActiveSupport::TestCase
"2d6",
"12",
"self",
"1d12",
"1d20+self",
"8+1D8-self",
"d12",
"d20+self",
"8+D8-self",
]
invalid_strings = [
"+",
"sel+10",
"1d8++13",
"d8++13",
]
valid_strings.each do |roll_command|
@ -30,9 +30,9 @@ class DiceRollerTest < ActiveSupport::TestCase
test "rolls appropriate results" do
100.times do
assert (1..6).include? DiceRoller.new("1d6").roll
assert (1..6).include? DiceRoller.new("d6").roll
assert (2..12).include? DiceRoller.new("2d6").roll
assert (11..30).include? DiceRoller.new("1d20+self", stat: stats(:strength)).roll
assert (11..30).include? DiceRoller.new("d20+self", stat: stats(:strength)).roll
end
end
end

View File

@ -1,9 +1,10 @@
Character sheets
= Must haves
- Add tests for feature references
- Refer to stat in roll (Skill in Troika)
- Lists
- Reorganise sections
- Edit additional roll types
- improve dice roll parsing to allow e.g. choose highest
= Nice to haves
- Calculate from result - e.g. troika damage roll should calc. damage amount from die roll:w
- Add min/max when creating stats