Skip to content

Commit

Permalink
Merge pull request #320 from bcdice/common_command_without_preprocess
Browse files Browse the repository at this point in the history
CommonCommandは自身で前処理をする
ysakasin authored Dec 12, 2020
2 parents e5a5b3e + 343c6c3 commit ea276c1
Showing 6 changed files with 19 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lib/bcdice/base.rb
Original file line number Diff line number Diff line change
@@ -165,9 +165,8 @@ def enable_debug
# @return [Result, nil] コマンド実行結果。コマンドが実行できなかった場合はnilを返す
def eval
command = BCDice::Preprocessor.process(@raw_input, self)
upcased_command = command.upcase

result = dice_command(command) || eval_common_command(upcased_command)
result = dice_command(command) || eval_common_command(@raw_input)
return nil unless result

result.rands = @randomizer.rand_results
@@ -229,6 +228,7 @@ def grich_text(count_one, dice_total_count, count_success); end
private

def eval_common_command(command)
command = change_text(command)
CommonCommand::COMMANDS.each do |klass|
result = klass.eval(command, self, @randomizer)
return result if result
4 changes: 2 additions & 2 deletions lib/bcdice/common_command/add_dice/node.rb
Original file line number Diff line number Diff line change
@@ -337,8 +337,8 @@ class DiceRoll
# @param [Number] times ダイスを振る回数のノード
# @param [Number] sides ダイスの面数のノード
def initialize(times, sides)
@times = times.literal
@sides = sides.literal
@times = times.eval(nil)
@sides = sides.eval(nil)

# ダイスを振った結果の出力
@text = nil
7 changes: 6 additions & 1 deletion lib/bcdice/common_command/calc/node.rb
Original file line number Diff line number Diff line change
@@ -11,7 +11,12 @@ def initialize(secret:, expr:)
end

def eval(round_type)
value = @expr.eval(round_type)
value =
begin
@expr.eval(round_type)
rescue ZeroDivisionError
"ゼロ除算が発生したため計算できませんでした"
end

Result.new.tap do |r|
r.secret = @secret
2 changes: 2 additions & 0 deletions lib/bcdice/common_command/d66_dice.rb
Original file line number Diff line number Diff line change
@@ -18,6 +18,8 @@ def eval(command, game_system, randomizer)
def parse(command, game_system)
return nil unless game_system.enabled_d66?

command = command.split(" ", 2).first

m = /^(S)?D66([ANS])?$/i.match(command)
return nil unless m

3 changes: 2 additions & 1 deletion lib/bcdice/common_command/version.rb
Original file line number Diff line number Diff line change
@@ -5,7 +5,8 @@ module Version

class << self
def eval(command, _game_system, _randomizer)
if command == "BCDICEVERSION"
command = command.split(" ", 2).first
if command.match?(/^BCDiceVersion$/i)
Result.new.tap do |r|
r.text = "BCDice Ver#{BCDice::VERSION}"
end
10 changes: 5 additions & 5 deletions test/data/None.toml
Original file line number Diff line number Diff line change
@@ -560,7 +560,7 @@ rands = [
[[ test ]]
game_system = "DiceBot"
input = "1d6-(1-2)>=3+2"
output = "(1D6+1>=5) > 3[3]+1 > 4 > 失敗"
output = "(1D6-(1-2)>=5) > 3[3]-(1-2) > 4 > 失敗"
rands = [
{ sides = 6, value = 3 },
]
@@ -1333,15 +1333,15 @@ rands = [
[[ test ]]
game_system = "DiceBot"
input = "choice[abc,def]"
output = "(choice[ABC,DEF]) > ABC"
output = "(choice[abc,def]) > abc"
rands = [
{ sides = 2, value = 1 },
]

[[ test ]]
game_system = "DiceBot"
input = "Schoice[abc2,def3]"
output = "(choice[ABC2,DEF3]) > DEF3"
output = "(choice[abc2,def3]) > def3"
secret = true
rands = [
{ sides = 2, value = 2 },
@@ -1452,13 +1452,13 @@ rands = []
[[ test ]]
game_system = "DiceBot"
input = "C(1/0) 0で割る"
output = "計算結果 > 0"
output = "計算結果 > ゼロ除算が発生したため計算できませんでした"
rands = []

[[ test ]]
game_system = "DiceBot"
input = "C(1+(2-3*4/(5/6))-7) 遠回しに0で割る"
output = "計算結果 > 0"
output = "計算結果 > ゼロ除算が発生したため計算できませんでした"
rands = []

[[ test ]]

0 comments on commit ea276c1

Please sign in to comment.