Class: BCDice::CommonCommand::RerollDice::Node::RerollCondition

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

Overview

振り足し条件を表すクラス。

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmp_op, threshold) ⇒ RerollCondition

Returns a new instance of RerollCondition.

Parameters:

  • cmp_op (Symbol)

    比較演算子

  • threshold (Integer)

    振り足しの閾値



132
133
134
135
# File 'lib/bcdice/common_command/reroll_dice/node.rb', line 132

def initialize(cmp_op, threshold)
  @cmp_op = cmp_op
  @threshold = threshold
end

Instance Attribute Details

#cmp_opSymbol (readonly)

Returns 比較演算子.

Returns:

  • (Symbol)

    比較演算子



126
127
128
# File 'lib/bcdice/common_command/reroll_dice/node.rb', line 126

def cmp_op
  @cmp_op
end

#thresholdInteger (readonly)

Returns 振り足しの閾値.

Returns:

  • (Integer)

    振り足しの閾値



128
129
130
# File 'lib/bcdice/common_command/reroll_dice/node.rb', line 128

def threshold
  @threshold
end

Instance Method Details

#reroll?(value) ⇒ Boolean

Returns 振り足しを行うべきか.

Parameters:

  • value (Integer)

    出目

Returns:

  • (Boolean)

    振り足しを行うべきか



160
161
162
# File 'lib/bcdice/common_command/reroll_dice/node.rb', line 160

def reroll?(value)
  value.send(@cmp_op, @threshold)
end

#valid?(sides) ⇒ Boolean

Returns 振り足し条件が妥当か.

Parameters:

  • sides (Integer)

    ダイスの面数

Returns:

  • (Boolean)

    振り足し条件が妥当か



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/bcdice/common_command/reroll_dice/node.rb', line 139

def valid?(sides)
  return false unless @threshold

  case @cmp_op
  when :<=
    @threshold < sides
  when :<
    @threshold <= sides
  when :>=
    @threshold > 1
  when :>
    @threshold >= 1
  when :'!='
    (1..sides).include?(@threshold)
  else # :==
    true
  end
end