Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tens D10などの詳細なダイスロール一覧 #134

Merged
merged 7 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ namespace :test do
'src/test/setup',
'src/test/testDiceBotLoaders.rb',
'src/test/testDiceBotPrefixesCompatibility.rb',
'src/test/test_srs_help_messages.rb'
'src/test/test_srs_help_messages.rb',
'src/test/test_detailed_rand_results.rb',
]
end
end
Expand Down
69 changes: 66 additions & 3 deletions src/bcdiceCore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -926,8 +926,11 @@ def roll(dice_cnt, dice_max, dice_sort = 0, dice_add = 0, dice_ul = '', dice_dif
round = 0

loop do
dice_n = rand(dice_max).to_i + 1
dice_n -= 1 if d9_on
if d9_on
dice_n = roll_d9()
else
dice_n = rand(dice_max).to_i + 1
end

dice_now += dice_n

Expand Down Expand Up @@ -976,7 +979,9 @@ def setRandomValues(rands)
@rands = rands
end

def rand(max)
# @params [Integer] max
# @return [Integer] 0以上max未満の整数
def rand_inner(max)
debug('rand called @rands', @rands)

value = 0
Expand All @@ -993,18 +998,76 @@ def rand(max)
return value
end

DetailedRandResult = Struct.new(:kind, :sides, :value)

# @params [Integer] max
# @return [Integer] 0以上max未満の整数
def rand(max)
ret = rand_inner(max)

push_to_detail(:nomal, max, ret + 1)
ysakasin marked this conversation as resolved.
Show resolved Hide resolved
return ret
end

# 十の位をd10を使って決定するためのダイスロール
# @return [Integer] 0以上90以下で10の倍数となる整数
def roll_tens_d10()
# rand_innerの戻り値を10倍すればすむ話なのだが、既存のテストとの互換性の為に処理をする
r = rand_inner(10) + 1
if r == 10
r = 0
end

ret = r * 10

push_to_detail(:tens_d10, 10, ret)
return ret
end

# d10を0~9として扱うダイスロール
# @return [Integer] 0以上9以下の整数
def roll_d9()
ret = rand_inner(10)

push_to_detail(:d9, 10, ret)
return ret
end

def setCollectRandResult(b)
if b
@randResults = []
@detailedRandResults = []
else
@randResults = nil
@detailedRandResults = nil
end
end

# @params [Symbol] kind
# @params [Integer] sides
# @params [Integer] value
def push_to_detail(kind, sides, value)
unless @detailedRandResults.nil?
detail = DetailedRandResult.new(kind, sides, value)
@detailedRandResults.push(detail)
end
end

# @return [Array<Array<(Integer, Integer)>>]
ysakasin marked this conversation as resolved.
Show resolved Hide resolved
def getRandResults
@randResults
end

# ダイスロールの詳細結果
# ダイスのタイプ
# - "tens_d10": 10面ダイスで十の位を決めるダイス
# - "d9": 10面ダイスで0~9を決めるダイス
# - 整数.to_s : #{整数}面ダイス
# @return [Array<Array<(Integer, String)>>]
def getDetailedRandResults
ysakasin marked this conversation as resolved.
Show resolved Hide resolved
@detailedRandResults
end

def randNomal(max)
Kernel.rand(max)
end
Expand Down
4 changes: 2 additions & 2 deletions src/diceBot/Cthulhu7th.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ def getTotalLists(bonus_dice_count, units_digit)

tens_digit_count = 1 + bonus_dice_count.abs
tens_digit_count.times do
bonus = rollPercentD10
total = (bonus * 10) + units_digit
bonus = bcdice.roll_tens_d10()
total = bonus + units_digit
total = 100 if total == 0

total_list.push(total)
Expand Down
95 changes: 95 additions & 0 deletions src/test/test_detailed_rand_results.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# -*- coding: utf-8 -*-

require 'test/unit'
require 'bcdiceCore'

# ダイスロール結果詳細のテストケース
# 10の位用にダイスロールした場合などの確認
class TestDetailedRandResults < Test::Unit::TestCase
def setup
@bcdice = BCDiceMaker.new.newBcDice
@bcdice.setCollectRandResult(true)
end

def test_rand
@bcdice.setRandomValues([[49, 100]])

value = @bcdice.rand(100)

assert_equal(49 - 1, value)

assert_equal(1, @bcdice.getDetailedRandResults.size)
assert_equal(:nomal, @bcdice.getDetailedRandResults[0].kind)
assert_equal(100, @bcdice.getDetailedRandResults[0].sides)
assert_equal(49, @bcdice.getDetailedRandResults[0].value)

assert_equal(1, @bcdice.getRandResults.size)
assert_equal(100, @bcdice.getRandResults[0][1])
assert_equal(49, @bcdice.getRandResults[0][0])
end

def test_tens_d10
@bcdice.setRandomValues([[3, 10]])
value = @bcdice.roll_tens_d10()

assert_equal(30, value)

assert_equal(1, @bcdice.getDetailedRandResults.size)
assert_equal(:tens_d10, @bcdice.getDetailedRandResults[0].kind)
assert_equal(10, @bcdice.getDetailedRandResults[0].sides)
assert_equal(30, @bcdice.getDetailedRandResults[0].value)

assert_equal(1, @bcdice.getRandResults.size)
assert_equal(10, @bcdice.getRandResults[0][1])
assert_equal(3, @bcdice.getRandResults[0][0])
end

def test_tens_d10_zero
@bcdice.setRandomValues([[10, 10]])
value = @bcdice.roll_tens_d10()

assert_equal(0, value)
assert_equal(0, @bcdice.getDetailedRandResults[0].value)
assert_equal(10, @bcdice.getRandResults[0][0])
end

def test_d9
@bcdice.setRandomValues([[3, 10]])
value = @bcdice.roll_d9()

assert_equal(2, value)

assert_equal(1, @bcdice.getDetailedRandResults.size)
assert_equal(:d9, @bcdice.getDetailedRandResults[0].kind)
assert_equal(10, @bcdice.getDetailedRandResults[0].sides)
assert_equal(2, @bcdice.getDetailedRandResults[0].value)

assert_equal(1, @bcdice.getRandResults.size)
assert_equal(10, @bcdice.getRandResults[0][1])
assert_equal(3, @bcdice.getRandResults[0][0])
end

def test_coc7th
dicebot = DiceBotLoader.loadUnknownGame("Cthulhu7th")
@bcdice.setDiceBot(dicebot)
@bcdice.setRandomValues([[4, 10], [5, 10], [6, 10]])

@bcdice.setMessage("CC(2)")
@bcdice.dice_command

details = @bcdice.getDetailedRandResults
assert_equal(3, details.size)

assert_equal(:nomal, details[0].kind)
assert_equal(10, details[0].sides)
assert_equal(4, details[0].value)

assert_equal(:tens_d10, details[1].kind)
assert_equal(10, details[1].sides)
assert_equal(50, details[1].value)

assert_equal(:tens_d10, details[1].kind)
ysakasin marked this conversation as resolved.
Show resolved Hide resolved
assert_equal(10, details[2].sides)
assert_equal(60, details[2].value)
end
end