diff --git a/app/services/dice_roller.rb b/app/services/dice_roller.rb index 3056a1a..dba5d02 100644 --- a/app/services/dice_roller.rb +++ b/app/services/dice_roller.rb @@ -13,8 +13,8 @@ class DiceRoller end def roll - @roll_command = @roll_command.sub("self", @stat.to_s) if @roll_command.include?("self") - Dice.roll(@roll_command) + roll_command = parse_roll_command_references(roll_command) + Dice.roll(roll_command) end def valid? @@ -39,6 +39,10 @@ class DiceRoller private + def roll_delimiters + [ "+", "-", "*", "/" ] + end + def roll_dice(command) parts = command.downcase.split("d").compact_blank die_type = parts.last @@ -52,4 +56,18 @@ 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 diff --git a/todo.md b/todo.md index 77efa7a..c71af93 100644 --- a/todo.md +++ b/todo.md @@ -1,10 +1,9 @@ Character sheets = Must haves -- Refer to stat in roll (Skill in Troika) +- Add tests for feature references - 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