class BCDice::CommonCommand::UpperDice::Node::Command
Public Class Methods
new(secret:, notations:, modifier:, cmp_op:, target_number:, reroll_threshold: nil)
click to toggle source
@param secret [Boolean] @param notations [Array<Notation>] @param modifier [Integer] @param cmp_op [Symbol, nil] @param target_number [Integer, nil] @param reroll_threshold [Integer]
# File lib/bcdice/common_command/upper_dice/node.rb, line 14 def initialize(secret:, notations:, modifier:, cmp_op:, target_number:, reroll_threshold: nil) @secret = secret @notations = notations @modifier = modifier @cmp_op = cmp_op @target_number = target_number @reroll_threshold = reroll_threshold end
Public Instance Methods
eval(game_system, randomizer)
click to toggle source
上方無限ロールを実行する
@param randomizer [Randomizer] @return [Result, nil]
# File lib/bcdice/common_command/upper_dice/node.rb, line 27 def eval(game_system, randomizer) round_type = game_system.round_type dice_list = @notations.map { |n| n.to_dice(round_type) } reroll_threshold = @reroll_threshold&.eval(round_type) || game_system.upper_dice_reroll_threshold || 0 modifier = @modifier&.eval(round_type) || 0 target_number = @target_number&.eval(round_type) expr = expr(dice_list, reroll_threshold, modifier, target_number) if reroll_threshold <= 1 return result_with_text("(#{expr}) > 無限ロールの条件がまちがっています") end roll_list = dice_list.map do |n| n.roll(randomizer, reroll_threshold, game_system.sort_barabara_dice?) end.reduce([], :concat) result = if @cmp_op result_success_count(roll_list, modifier, target_number) else result_max_sum(roll_list, modifier) end sequence = [ "(#{expr})", interlim_expr(roll_list, modifier), result ] result_with_text(sequence.join(" > ")) end
Private Instance Methods
expr(dice_list, reroll_threshold, modifier, target_number)
click to toggle source
パース済みのコマンドを文字列で表示する
@return [String]
# File lib/bcdice/common_command/upper_dice/node.rb, line 101 def expr(dice_list, reroll_threshold, modifier, target_number) formated_cmp_op = Format.comparison_operator(@cmp_op) formated_modifier = Format.modifier(modifier) "#{dice_list.join('+')}[#{reroll_threshold}]#{formated_modifier}#{formated_cmp_op}#{target_number}" end
interlim_expr(roll_list, modifier)
click to toggle source
ダイスロールの結果を文字列に変換する 振り足しがなければその数値、振り足しがあれば合計と各ダイスの出目を出力する
@param roll_list [Array<Hash>] @param modifier [Integer] @return [String]
# File lib/bcdice/common_command/upper_dice/node.rb, line 86 def interlim_expr(roll_list, modifier) dice = roll_list.map do |e| if e[:list].size == 1 e[:sum] else "#{e[:sum]}[#{e[:list].join(',')}]" end end.join(",") dice + Format.modifier(modifier) end
result_max_sum(roll_list, modifier)
click to toggle source
# File lib/bcdice/common_command/upper_dice/node.rb, line 72 def result_max_sum(roll_list, modifier) sum_list = roll_list.map { |e| e[:sum] } total = sum_list.sum() + modifier max = sum_list.map { |i| i + modifier }.max "#{max}/#{total}(最大/合計)" end
result_success_count(roll_list, modifier, target_number)
click to toggle source
# File lib/bcdice/common_command/upper_dice/node.rb, line 63 def result_success_count(roll_list, modifier, target_number) success_count = roll_list.count do |e| x = e[:sum] + modifier x.send(@cmp_op, target_number) end "成功数#{success_count}" end
result_with_text(text)
click to toggle source
# File lib/bcdice/common_command/upper_dice/node.rb, line 108 def result_with_text(text) Result.new.tap do |r| r.secret = @secret r.text = text end end