Skip to content

Commit

Permalink
Merge pull request #171 from bcdice/add_dice-fix_d66
Browse files Browse the repository at this point in the history
AddDice: D66が振れない問題を修正する
  • Loading branch information
ysakasin authored Apr 30, 2020
2 parents ca0cfd3 + d60f38c commit 6d74ca1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
47 changes: 27 additions & 20 deletions src/dice/add_dice/randomizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,55 +12,62 @@ def initialize(bcdice, dicebot, cmp_op)
@dice_list = []
end

# ダイスを振る
# @param [Integer] times ダイス数
# @param [Integer] sides 面数
# @param [Integer] critical クリティカルヒットの閾値
# @return [(Integer, String)] 合計、および出目の文字列
def roll(times, sides, critical)
total = 0
# 振り足し分も含めた出目の総計
total_sum = 0
# 振り足し分も含めた出目グループの配列
results_list = []

loop_count = 0
queue = [times]
while !queue.empty?
times = queue.shift
val, dice_list = roll_once(times, sides)

total += val
dice_list = roll_once(times, sides)
@dice_list.concat(dice_list)

# 出目の小計
# TODO: Ruby 2.4以降では dice_list.sum とできる
sum = dice_list.reduce(0, &:+)

total_sum += sum
results_list.push(dice_list)

enqueue_reroll(dice_list, queue, times)
@dicebot.check2dCritical(critical, val, queue, loop_count)
@dicebot.check2dCritical(critical, sum, queue, loop_count)
loop_count += 1
end

text = total.to_s
text = total_sum.to_s
results_list.each do |list|
text += '[' + list.join(',') + ']'
end

[total, text]
[total_sum, text]
end

private

# ダイスを振る(振り足しなしの1回分)
# @param [Integer] times ダイス数
# @param [Integer] sides 面数
# @return [Array<Integer>] 出目の配列
def roll_once(times, sides)
@sides = sides if @sides < sides

if sides == 66
return rollD66(dice_wk)
return Array.new(times) { @bcdice.getD66Value() }
end

_, dice_list, = @bcdice.roll(times, sides, @dicebot.sortType & 1)
dice_list = dice_list.split(",").map(&:to_i)
@dice_list.concat(dice_list)

total = dice_list.inject(0, &:+)
return [total, dice_list]
end

def roll_d66(times)
dice_list = Array.new(times) { @bcdice.getD66Value() }
@dice_list.concat(dice_list)
_, dice_str, = @bcdice.roll(times, sides, @dicebot.sortType & 1)

total = dice_list.inject(0, &:+)
return [total, dice_list]
# 現在は出目が文字列で返ってきてしまうので、整数の配列に変換する
return dice_str.split(',').map(&:to_i)
end

def double_check?
Expand Down
6 changes: 6 additions & 0 deletions src/test/data/None.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ DiceBot : (2D4) > 3[1,2] > 3
rand:1/4,2/4
============================
input:
2D66
output:
DiceBot : (2D66) > 120[66,54] > 120
rand:6/6,6/6,5/6,4/6
============================
input:
1D6+1
output:
DiceBot : (1D6+1) > 4[4]+1 > 5
Expand Down

0 comments on commit 6d74ca1

Please sign in to comment.