Skip to content

Commit

Permalink
Merge pull request #296 from bcdice/drop_prerolling
Browse files Browse the repository at this point in the history
事前処理によるダイスロールを廃止
  • Loading branch information
ysakasin authored Nov 8, 2020
2 parents 7bed223 + a41761e commit 886dbd5
Show file tree
Hide file tree
Showing 11 changed files with 458 additions and 1,282 deletions.
2 changes: 1 addition & 1 deletion lib/bcdice/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def enable_debug
# コマンドを評価する
# @return [Result, nil] コマンド実行結果。コマンドが実行できなかった場合はnilを返す
def eval
command = BCDice::Preprocessor.process(@raw_input, @randomizer, self)
command = BCDice::Preprocessor.process(@raw_input, self)
upcased_command = command.upcase

result = dice_command(command) || eval_common_command(upcased_command)
Expand Down
43 changes: 5 additions & 38 deletions lib/bcdice/preprocessor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,26 @@ module BCDice
#
# @example
# Preprocessor.process(
# "1d6+[2D6]+[40..50]+4D+(3*4) 切り取られる部分",
# randomizer,
# "1d6+4D+(3*4) 切り取られる部分",
# game_system
# ) #=> "1d6+3+46+4D6+7"
# ) #=> "1d6+4D6+7"
class Preprocessor
# @param (see #initialize)
# @return [String]
def self.process(text, randomizer, game_system)
Preprocessor.new(text, randomizer, game_system).process()
def self.process(text, game_system)
Preprocessor.new(text, game_system).process()
end

# @param text [String]
# @param randomizer [Randomizer]
# @param game_system [Base]
def initialize(text, randomizer, game_system)
def initialize(text, game_system)
@text = text
@randomizer = randomizer
@game_system = game_system
end

# @return [String]
def process
trim_after_whitespace()
replace_preroll()
replace_range()
replace_parentheses()

@text = @game_system.change_text(@text)
Expand All @@ -44,34 +39,6 @@ def trim_after_whitespace()
@text = @text.strip.split(/\s/, 2).first
end

# [1D6]のような部分を事前ダイスロールする
def replace_preroll()
@text = @text.gsub(/\[\d+D\d+\]/i) do |matched|
# Remove '[' and ']'
command = matched[1..-2].upcase
times, sides = command.split("D").map(&:to_i)

@randomizer.roll_sum(times, sides)
end
end

# [1...6]のような部分を事前ダイスロールする
def replace_range
@text = @text.gsub(/\[(\d+)\.\.\.(\d+)\]/) do |matched|
first = Regexp.last_match(1).to_i
last = Regexp.last_match(2).to_i

if first > last
matched
else
steps = last - first + 1
dice = @randomizer.roll_once(steps)

first + dice - 1
end
end
end

# カッコ書きの数式を事前計算する
def replace_parentheses
@text = @text.gsub(%r{\([\d/\+\*\-\(\)]+\)}) do |expr|
Expand Down
Loading

0 comments on commit 886dbd5

Please sign in to comment.