Class: BCDice::CommonCommand::TallyDice::Node::Command
- Inherits:
-
Object
- Object
- BCDice::CommonCommand::TallyDice::Node::Command
- Defined in:
- lib/bcdice/common_command/tally_dice/node.rb
Overview
個数カウントダイス:コマンドのノード
Constant Summary collapse
- MAX_SIDES =
最大面数
20
Instance Method Summary collapse
- #eval(game_system, randomizer) ⇒ Result?
-
#initialize(secret:, notation:) ⇒ Command
constructor
A new instance of Command.
Constructor Details
#initialize(secret:, notation:) ⇒ Command
Returns a new instance of Command.
17 18 19 20 |
# File 'lib/bcdice/common_command/tally_dice/node.rb', line 17 def initialize(secret:, notation:) @secret = secret @notation = notation end |
Instance Method Details
#eval(game_system, randomizer) ⇒ Result?
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 |
# File 'lib/bcdice/common_command/tally_dice/node.rb', line 25 def eval(game_system, randomizer) dice = @notation.to_dice(game_system.round_type) unless dice.valid? return nil end if dice.sides > MAX_SIDES return Result.new("(#{dice}) > 面数は1以上、#{MAX_SIDES}以下としてください") end values = dice.roll(randomizer) values_str = (game_system. ? values.sort : values) .join(",") # TODO: Ruby 2.7以降のみサポートするようになった場合 # Enumerable#tally で書く values_count = values .group_by(&:itself) .transform_values(&:length) values_count_strs = (1..dice.sides).map do |v| count = values_count.fetch(v, 0) next nil if count == 0 && !dice.show_zeros? "[#{v}]×#{values_count.fetch(v, 0)}" end sequence = [ "(#{dice})", values_str, values_count_strs.compact.join(", "), ].compact Result.new.tap do |r| r.secret = @secret r.text = sequence.join(" > ") end end |