diff --git a/Rakefile b/Rakefile index 14050f9fa..12cb53f96 100644 --- a/Rakefile +++ b/Rakefile @@ -41,6 +41,7 @@ namespace :test do 'src/test/testDiceBotLoaders.rb', 'src/test/testDiceBotPrefixesCompatibility.rb', 'src/test/test_srs_help_messages.rb', + 'src/test/test_detailed_rand_results.rb', 'src/test/range_table_test.rb', ] end diff --git a/src/bcdiceCore.rb b/src/bcdiceCore.rb index 3aca1cd5f..3834feeca 100755 --- a/src/bcdiceCore.rb +++ b/src/bcdiceCore.rb @@ -89,6 +89,9 @@ class BCDice VERSION = "2.03.05".freeze attr_reader :cardTrader + attr_reader :rand_results, :detailed_rand_results + + alias getRandResults rand_results def initialize(parent, cardTrader, diceBot, counterInfos, tableFileData) @parent = parent @@ -105,7 +108,6 @@ def initialize(parent, cardTrader, diceBot, counterInfos, tableFileData) @isMessagePrinted = false @rands = nil @isKeepSecretDice = true - @randResults = nil @isIrcMode = true end @@ -926,8 +928,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 @@ -976,7 +981,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 @@ -986,23 +993,66 @@ def rand(max) value = randFromRands(max) end - unless @randResults.nil? - @randResults << [(value + 1), max] + unless @rand_results.nil? + @rand_results << [(value + 1), max] end 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(:normal, max, ret + 1) + 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 = [] + @rand_results = [] + @detailed_rand_results = [] else - @randResults = nil + @rand_results = nil + @detailed_rand_results = nil end end - def getRandResults - @randResults + # @params [Symbol] kind + # @params [Integer] sides + # @params [Integer] value + def push_to_detail(kind, sides, value) + unless @detailed_rand_results.nil? + detail = DetailedRandResult.new(kind, sides, value) + @detailed_rand_results.push(detail) + end end def randNomal(max) diff --git a/src/diceBot/Cthulhu7th.rb b/src/diceBot/Cthulhu7th.rb index 9eb77e8b5..872300845 100644 --- a/src/diceBot/Cthulhu7th.rb +++ b/src/diceBot/Cthulhu7th.rb @@ -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) diff --git a/src/test/test_detailed_rand_results.rb b/src/test/test_detailed_rand_results.rb new file mode 100644 index 000000000..2d0bc38f6 --- /dev/null +++ b/src/test/test_detailed_rand_results.rb @@ -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.detailed_rand_results.size) + assert_equal(:normal, @bcdice.detailed_rand_results[0].kind) + assert_equal(100, @bcdice.detailed_rand_results[0].sides) + assert_equal(49, @bcdice.detailed_rand_results[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.detailed_rand_results.size) + assert_equal(:tens_d10, @bcdice.detailed_rand_results[0].kind) + assert_equal(10, @bcdice.detailed_rand_results[0].sides) + assert_equal(30, @bcdice.detailed_rand_results[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.detailed_rand_results[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.detailed_rand_results.size) + assert_equal(:d9, @bcdice.detailed_rand_results[0].kind) + assert_equal(10, @bcdice.detailed_rand_results[0].sides) + assert_equal(2, @bcdice.detailed_rand_results[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.detailed_rand_results + assert_equal(3, details.size) + + assert_equal(:normal, 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[2].kind) + assert_equal(10, details[2].sides) + assert_equal(60, details[2].value) + end +end