Class: BCDice::CommonCommand::RerollDice::Node::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/bcdice/common_command/reroll_dice/node.rb

Instance Method Summary collapse

Constructor Details

#initialize(secret:, notations:, source:, cmp_op: nil, target_number: nil, reroll_cmp_op: nil, reroll_threshold: nil) ⇒ Command

Returns a new instance of Command.



8
9
10
11
12
13
14
15
16
# File 'lib/bcdice/common_command/reroll_dice/node.rb', line 8

def initialize(secret:, notations:, source:, cmp_op: nil, target_number: nil, reroll_cmp_op: nil, reroll_threshold: nil)
  @secret = secret
  @notations = notations
  @cmp_op = cmp_op
  @target_number_node = target_number
  @reroll_cmp_op = reroll_cmp_op
  @reroll_threshold_node = reroll_threshold
  @source = source
end

Instance Method Details

#eval(game_system, randomizer) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/bcdice/common_command/reroll_dice/node.rb', line 18

def eval(game_system, randomizer)
  round_type = game_system.round_type
  cmp_op = @cmp_op || game_system.default_cmp_op
  reroll_cmp_op = @reroll_cmp_op || cmp_op || :>=

  target_number =
    @target_number_node&.eval(round_type) ||
    game_system.default_target_number

  reroll_threshold =
    @reroll_threshold_node&.eval(round_type) ||
    game_system.reroll_dice_reroll_threshold ||
    target_number

  reroll_condition = RerollCondition.new(reroll_cmp_op, reroll_threshold)

  dice_queue = @notations.map { |node| node.to_dice(round_type) }
  unless dice_queue.all? { |d| reroll_condition.valid?(d.sides) }
    return result_with_text("#{@source} > 条件が間違っています。2R6>=5 あるいは 2R6[5] のように振り足し目標値を指定してください。")
  end

  dice_list_list = roll(
    dice_queue,
    randomizer,
    reroll_condition,
    game_system.sort_barabara_dice?
  )

  dice_list = dice_list_list.flatten

  # 振り足し分は出目1の個数をカウントしない
  one_count = dice_list_list
              .take(@notations.size)
              .flatten
              .count(1)

  success_count =
    if cmp_op
      dice_list.count { |val| val.send(cmp_op, target_number) }
    else
      0
    end

  sequence = [
    expr(round_type, reroll_condition, cmp_op, target_number),
    dice_list_list.map { |list| list.join(",") }.join(" + "),
    "成功数#{success_count}",
    game_system.grich_text(one_count, dice_list.size, success_count),
  ].compact

  result_with_text(sequence.join(""))
end