Class: BCDice::CommonCommand::BarabaraDice::Node::Command

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

Instance Method Summary collapse

Constructor Details

#initialize(secret:, notations:, cmp_op:, target_number:) ⇒ Command



11
12
13
14
15
16
# File 'lib/bcdice/common_command/barabara_dice/node.rb', line 11

def initialize(secret:, notations:, cmp_op:, target_number:)
  @secret = secret
  @notations = notations
  @cmp_op = cmp_op
  @target_number = target_number
end

Instance Method Details

#eval(game_system, randomizer) ⇒ Result



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
# File 'lib/bcdice/common_command/barabara_dice/node.rb', line 21

def eval(game_system, randomizer)
  round_type = game_system.round_type
  notations = @notations.map { |n| n.to_dice(round_type) }
  cmp_op = @cmp_op || game_system.default_cmp_op
  target_number = @target_number&.eval(round_type) || game_system.default_target_number

  dice_list_list = notations.map { |d| d.roll(randomizer) }
  dice_list_list.map!(&:sort) if game_system.sort_barabara_dice?

  dice_list = dice_list_list.flatten

  count_of_1 = dice_list.count(1)
  success_num = cmp_op ? dice_list.count { |d| d.send(cmp_op, target_number) } : 0
  success_num_text = "成功数#{success_num}" if cmp_op

  sequence = [
    "(#{notations.join('+')}#{Format.comparison_operator(cmp_op)}#{target_number})",
    dice_list.join(","),
    success_num_text,
    game_system.grich_text(count_of_1, dice_list.size, success_num)
  ].compact

  Result.new.tap do |r|
    r.secret = @secret
    r.text = sequence.join(" > ")
    r.last_dice_list_list = dice_list_list
    r.last_dice_list = dice_list
    r.success_num = success_num
  end
end