Attempting to use any addition modifier crashes library
jasedit opened this issue · 9 comments
Using the example provided in the README.md produces the following crash:
irb(main):001:0> require 'dicebag'
=> true
irb(main):002:0> dstr = "(Damage) 2d8 + 5 + 1d6"
=> "(Damage) 2d8 + 5 + 1d6"
irb(main):003:0> dice = DiceBag::Roll.new(dstr)
NoMethodError: undefined method `[]' for "5"@15:Parslet::Slice
from /Library/Ruby/Gems/2.0.0/gems/dicebag-3.1.0/lib/dicebag/transform.rb:39:in `block in <class:Transform>'
from /Library/Ruby/Gems/2.0.0/gems/parslet-1.7.1/lib/parslet/transform.rb:216:in `instance_eval'
from /Library/Ruby/Gems/2.0.0/gems/parslet-1.7.1/lib/parslet/transform.rb:216:in `call_on_match'
from /Library/Ruby/Gems/2.0.0/gems/parslet-1.7.1/lib/parslet/transform.rb:234:in `block in transform_elt'
from /Library/Ruby/Gems/2.0.0/gems/parslet-1.7.1/lib/parslet/transform.rb:231:in `each'
from /Library/Ruby/Gems/2.0.0/gems/parslet-1.7.1/lib/parslet/transform.rb:231:in `transform_elt'
from /Library/Ruby/Gems/2.0.0/gems/parslet-1.7.1/lib/parslet/transform.rb:184:in `apply'
from /Library/Ruby/Gems/2.0.0/gems/parslet-1.7.1/lib/parslet/transform.rb:253:in `block in recurse_array'
from /Library/Ruby/Gems/2.0.0/gems/parslet-1.7.1/lib/parslet/transform.rb:253:in `map'
from /Library/Ruby/Gems/2.0.0/gems/parslet-1.7.1/lib/parslet/transform.rb:253:in `recurse_array'
from /Library/Ruby/Gems/2.0.0/gems/parslet-1.7.1/lib/parslet/transform.rb:189:in `apply'
from /Library/Ruby/Gems/2.0.0/gems/dicebag-3.1.0/lib/dicebag.rb:160:in `parse'
from /Library/Ruby/Gems/2.0.0/gems/dicebag-3.1.0/lib/dicebag/roll.rb:14:in `initialize'
from (irb):3:in `new'
from (irb):3
from /usr/bin/irb:12:in `<main>'
I'm getting the same error.
Huh. I'll check this out when I get time. Sadly, for personal coding stuff, not a lot of that to go around lately...
It seems to show up when you do 1d100 + 5
instead of 1d100 + 5d6
. It seems that adding a static modifier breaks it.
I wanted to make a pull request, but there where no specs that I would run to prove my point before making code changes. 😞
Yeeeeah, one of those things I keep meaning to get around to. :( DiceBag is more or less mostly a toy meant for my irc-bot, so never thought anyone would actually use it. Since it's now getting interest, I'll try to get it fixed up to be community friendly.
I can get irb to puts
a result out when changing the code like @ekampp said - use "(Damage) 1d6 + 1d8"
instead of "(Damage)1d6 + 8"
. In other words - don't use plain, static values like +5
. Instead use all #d#
values.
2.2.3 :001 > require 'dicebag'
=> true
2.2.3 :002 > dstr = "(Damage) 1d6 + 1d8"
=> "(Damage) 1d6 + 1d8"
2.2.3 :003 > dice = DiceBag::Roll.new(dstr)
=> #<DiceBag::Roll:0x007f94e2239668 @dstr="(Damage) 1d6 + 1d8", @tree=[[:label, #<DiceBag::LabelPart:0x007f94e226b2d0 @value="Damage">], [:start, #<DiceBag::RollPart:0x007f94e226b258 @total=nil, @tally=[], @value={:count=>1, :sides=>6}, @count=1, @sides=6, @notes=[], @options={:explode=>0, :drop=>0, :keep=>0, :reroll=>0, :target=>0}>], [:add, #<DiceBag::RollPart:0x007f94e226b168 @total=nil, @tally=[], @value={:count=>1, :sides=>8}, @count=1, @sides=8, @notes=[], @options={:explode=>0, :drop=>0, :keep=>0, :reroll=>0, :target=>0}>]], @result=nil>
2.2.3 :004 > result = dice.result()
=> #<DiceBag::Result:0x007f94e18df860 @label="Damage", @total=7, @sections=[#<DiceBag::RollPart:0x007f94e226b258 @total=5, @tally=[5], @value={:count=>1, :sides=>6}, @count=1, @sides=6, @notes=[], @options={:explode=>0, :drop=>0, :keep=>0, :reroll=>0, :target=>0}>, #<DiceBag::RollPart:0x007f94e226b168 @total=2, @tally=[2], @value={:count=>1, :sides=>8}, @count=1, @sides=8, @notes=[], @options={:explode=>0, :drop=>0, :keep=>0, :reroll=>0, :target=>0}>]>
2.2.3 :005 > puts result
Damage: 7
=> nil
However...
...I still get the exact same error when trying to run the file via ruby my_file_name.rb
intead of typing it line-by-line in pry or irb.
[23:10]~/coding/rails/fba $ ruby dice.rb
/Users/Chris/.rvm/gems/ruby-2.2.3/gems/dicebag-3.1.0/lib/dicebag/transform.rb:39:in `block in <class:Transform>': undefined method `[]' for "5"@15:Parslet::Slice (NoMethodError)
from /Users/Chris/.rvm/gems/ruby-2.2.3/gems/parslet-1.7.1/lib/parslet/transform.rb:216:in `instance_eval'
from /Users/Chris/.rvm/gems/ruby-2.2.3/gems/parslet-1.7.1/lib/parslet/transform.rb:216:in `call_on_match'
from /Users/Chris/.rvm/gems/ruby-2.2.3/gems/parslet-1.7.1/lib/parslet/transform.rb:234:in `block in transform_elt'
from /Users/Chris/.rvm/gems/ruby-2.2.3/gems/parslet-1.7.1/lib/parslet/transform.rb:231:in `each'
from /Users/Chris/.rvm/gems/ruby-2.2.3/gems/parslet-1.7.1/lib/parslet/transform.rb:231:in `transform_elt'
from /Users/Chris/.rvm/gems/ruby-2.2.3/gems/parslet-1.7.1/lib/parslet/transform.rb:184:in `apply'
from /Users/Chris/.rvm/gems/ruby-2.2.3/gems/parslet-1.7.1/lib/parslet/transform.rb:253:in `block in recurse_array'
from /Users/Chris/.rvm/gems/ruby-2.2.3/gems/parslet-1.7.1/lib/parslet/transform.rb:253:in `map'
from /Users/Chris/.rvm/gems/ruby-2.2.3/gems/parslet-1.7.1/lib/parslet/transform.rb:253:in `recurse_array'
from /Users/Chris/.rvm/gems/ruby-2.2.3/gems/parslet-1.7.1/lib/parslet/transform.rb:189:in `apply'
from /Users/Chris/.rvm/gems/ruby-2.2.3/gems/dicebag-3.1.0/lib/dicebag.rb:160:in `parse'
from /Users/Chris/.rvm/gems/ruby-2.2.3/gems/dicebag-3.1.0/lib/dicebag/roll.rb:14:in `initialize'
from dice.rb:4:in `new'
from dice.rb:4:in `<main>'
I have no idea why irb works but running via an .rb file does not.
I went and made my own lib, super fast and dirty: https://github.com/DarkHeresyOnline/dice
If any of it is of interest I would be happy to backport in another PR.
Okay. I have found the issue here after writing some specs for this lib finally. I'll be pushing an update here soon.
The issue was in the Parslet::Transform sub-class, where it was not matching simple op=>values, and when it did match the xdx sub-trees, a bug was wiping out the op-string.
Kinda, sorta, super really embarrassing, really. :)
And 3.2.0 pushed. Let me know if you still have any issues. If not, I'll close this issue soon.
Added a few more updates. Sorry for the bugs earlier! They were quite stupid, honestly, but now I think the lib is in a much better position for working on it when I get more time. I've noted a few things I wanna do in the parser and transform classes to streamline things a bit more. Gonna close this issue now.