From 642888b7df6c2115662c1a1b92f85eb7c27d2fc8 Mon Sep 17 00:00:00 2001 From: SAKATA Sinji Date: Thu, 12 Mar 2020 20:34:28 +0900 Subject: [PATCH 1/6] =?UTF-8?q?Tens=20D10=E3=81=AA=E3=81=A9=E3=81=AE?= =?UTF-8?q?=E8=A9=B3=E7=B4=B0=E3=81=AA=E3=83=80=E3=82=A4=E3=82=B9=E3=83=AD?= =?UTF-8?q?=E3=83=BC=E3=83=AB=E4=B8=80=E8=A6=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Rakefile | 3 +- src/bcdiceCore.rb | 63 ++++++++++++++++++++++-- src/diceBot/Cthulhu7th.rb | 4 +- src/test/test_detailed_rand_results.rb | 68 ++++++++++++++++++++++++++ 4 files changed, 132 insertions(+), 6 deletions(-) create mode 100644 src/test/test_detailed_rand_results.rb diff --git a/Rakefile b/Rakefile index 4a2452e56..772692c8e 100644 --- a/Rakefile +++ b/Rakefile @@ -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 diff --git a/src/bcdiceCore.rb b/src/bcdiceCore.rb index 3aca1cd5f..047223728 100755 --- a/src/bcdiceCore.rb +++ b/src/bcdiceCore.rb @@ -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 @@ -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 @@ -993,18 +998,70 @@ def rand(max) return value end + # @params [Integer] max + # @return [Integer] 0以上max未満の整数 + def rand(max) + ret = rand_inner(max) + + push_to_detail(max.to_s, 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", ret) + return ret + end + + # d10を0~9として扱うダイスロール + # @return [Integer] 0以上9以下の整数 + def roll_d9() + ret = rand_inner(10) + + push_to_detail("d9", ret) + return ret + end + def setCollectRandResult(b) if b @randResults = [] + @detailedRandResults = [] else @randResults = nil + @detailedRandResults = nil end end + def push_to_detail(sides, value) + unless @detailedRandResults.nil? + @detailedRandResults.push([value, sides]) + end + end + + # @return [Array>] def getRandResults @randResults end + # ダイスロールの詳細結果 + # ダイスのタイプ + # - "tens_d10": 10面ダイスで十の位を決めるダイス + # - "d9": 10面ダイスで0~9を決めるダイス + # - 整数.to_s : #{整数}面ダイス + # @return [Array>] + def getDetailedRandResults + @detailedRandResults + end + def randNomal(max) Kernel.rand(max) end 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..7827db497 --- /dev/null +++ b/src/test/test_detailed_rand_results.rb @@ -0,0 +1,68 @@ +# -*- 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("100", @bcdice.getDetailedRandResults[0][1]) + assert_equal(49, @bcdice.getDetailedRandResults[0][0]) + + 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][1]) + assert_equal(30, @bcdice.getDetailedRandResults[0][0]) + + 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][0]) + 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][1]) + assert_equal(2, @bcdice.getDetailedRandResults[0][0]) + + assert_equal(1, @bcdice.getRandResults.size) + assert_equal(10, @bcdice.getRandResults[0][1]) + assert_equal(3, @bcdice.getRandResults[0][0]) + end +end From a73f949bf23f100619ae1a19893b228571508543 Mon Sep 17 00:00:00 2001 From: SAKATA Sinji Date: Sun, 15 Mar 2020 03:06:46 +0900 Subject: [PATCH 2/6] =?UTF-8?q?=E8=A9=B3=E7=B4=B0=E7=B5=90=E6=9E=9C?= =?UTF-8?q?=E3=81=AE=E6=A7=8B=E9=80=A0=E4=BD=93=E3=82=92=E4=BD=9C=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bcdiceCore.rb | 16 +++++++++++----- src/test/test_detailed_rand_results.rb | 17 ++++++++++------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/bcdiceCore.rb b/src/bcdiceCore.rb index 047223728..23a405ddb 100755 --- a/src/bcdiceCore.rb +++ b/src/bcdiceCore.rb @@ -998,12 +998,14 @@ def rand_inner(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(max.to_s, ret + 1) + push_to_detail(:nomal, max, ret + 1) return ret end @@ -1018,7 +1020,7 @@ def roll_tens_d10() ret = r * 10 - push_to_detail("tens_d10", ret) + push_to_detail(:tens_d10, 10, ret) return ret end @@ -1027,7 +1029,7 @@ def roll_tens_d10() def roll_d9() ret = rand_inner(10) - push_to_detail("d9", ret) + push_to_detail(:d9, 10, ret) return ret end @@ -1041,9 +1043,13 @@ def setCollectRandResult(b) end end - def push_to_detail(sides, value) + # @params [Symbol] kind + # @params [Integer] sides + # @params [Integer] value + def push_to_detail(kind, sides, value) unless @detailedRandResults.nil? - @detailedRandResults.push([value, sides]) + detail = DetailedRandResult.new(kind, sides, value) + @detailedRandResults.push(detail) end end diff --git a/src/test/test_detailed_rand_results.rb b/src/test/test_detailed_rand_results.rb index 7827db497..30d9a6b5d 100644 --- a/src/test/test_detailed_rand_results.rb +++ b/src/test/test_detailed_rand_results.rb @@ -19,8 +19,9 @@ def test_rand assert_equal(49 - 1, value) assert_equal(1, @bcdice.getDetailedRandResults.size) - assert_equal("100", @bcdice.getDetailedRandResults[0][1]) - assert_equal(49, @bcdice.getDetailedRandResults[0][0]) + 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]) @@ -34,8 +35,9 @@ def test_tens_d10 assert_equal(30, value) assert_equal(1, @bcdice.getDetailedRandResults.size) - assert_equal("tens_d10", @bcdice.getDetailedRandResults[0][1]) - assert_equal(30, @bcdice.getDetailedRandResults[0][0]) + 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]) @@ -47,7 +49,7 @@ def test_tens_d10_zero value = @bcdice.roll_tens_d10() assert_equal(0, value) - assert_equal(0, @bcdice.getDetailedRandResults[0][0]) + assert_equal(0, @bcdice.getDetailedRandResults[0].value) assert_equal(10, @bcdice.getRandResults[0][0]) end @@ -58,8 +60,9 @@ def test_d9 assert_equal(2, value) assert_equal(1, @bcdice.getDetailedRandResults.size) - assert_equal("d9", @bcdice.getDetailedRandResults[0][1]) - assert_equal(2, @bcdice.getDetailedRandResults[0][0]) + 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]) From dfb9d41e422a64ec2dc82a81bfd31a31a8dbec3f Mon Sep 17 00:00:00 2001 From: SAKATA Sinji Date: Sun, 15 Mar 2020 03:33:11 +0900 Subject: [PATCH 3/6] =?UTF-8?q?=E6=96=B0=E3=82=AF=E3=83=88=E3=82=A5?= =?UTF-8?q?=E3=83=AB=E3=83=95=E3=82=92=E4=BD=BF=E3=81=A3=E3=81=9FdetaildRa?= =?UTF-8?q?ndResults=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/test_detailed_rand_results.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/test/test_detailed_rand_results.rb b/src/test/test_detailed_rand_results.rb index 30d9a6b5d..713176b4a 100644 --- a/src/test/test_detailed_rand_results.rb +++ b/src/test/test_detailed_rand_results.rb @@ -68,4 +68,28 @@ def test_d9 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) + assert_equal(10, details[2].sides) + assert_equal(60, details[2].value) + end end From ab5c404016ac25473fa7065a3b6f8f93f57dd13f Mon Sep 17 00:00:00 2001 From: SAKATA Sinji Date: Mon, 16 Mar 2020 08:52:26 +0900 Subject: [PATCH 4/6] Fix typo Co-Authored-By: ocha --- src/test/test_detailed_rand_results.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/test_detailed_rand_results.rb b/src/test/test_detailed_rand_results.rb index 713176b4a..392da2391 100644 --- a/src/test/test_detailed_rand_results.rb +++ b/src/test/test_detailed_rand_results.rb @@ -88,7 +88,7 @@ def test_coc7th assert_equal(10, details[1].sides) assert_equal(50, details[1].value) - assert_equal(:tens_d10, details[1].kind) + assert_equal(:tens_d10, details[2].kind) assert_equal(10, details[2].sides) assert_equal(60, details[2].value) end From 3aeed2557480b6ac5cf420bd0bf3ef0e59e20e5c Mon Sep 17 00:00:00 2001 From: SAKATA Sinji Date: Mon, 16 Mar 2020 09:05:24 +0900 Subject: [PATCH 5/6] Rubish detailed_rand_results --- src/bcdiceCore.rb | 35 ++++++++------------------ src/test/test_detailed_rand_results.rb | 28 ++++++++++----------- 2 files changed, 25 insertions(+), 38 deletions(-) diff --git a/src/bcdiceCore.rb b/src/bcdiceCore.rb index 23a405ddb..64273dfb5 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 @@ -991,8 +993,8 @@ def rand_inner(max) value = randFromRands(max) end - unless @randResults.nil? - @randResults << [(value + 1), max] + unless @rand_results.nil? + @rand_results << [(value + 1), max] end return value @@ -1035,11 +1037,11 @@ def roll_d9() def setCollectRandResult(b) if b - @randResults = [] - @detailedRandResults = [] + @rand_results = [] + @detailed_rand_results = [] else - @randResults = nil - @detailedRandResults = nil + @rand_results = nil + @detailed_rand_results = nil end end @@ -1047,27 +1049,12 @@ def setCollectRandResult(b) # @params [Integer] sides # @params [Integer] value def push_to_detail(kind, sides, value) - unless @detailedRandResults.nil? + unless @detailed_rand_results.nil? detail = DetailedRandResult.new(kind, sides, value) - @detailedRandResults.push(detail) + @detailed_rand_results.push(detail) end end - # @return [Array>] - def getRandResults - @randResults - end - - # ダイスロールの詳細結果 - # ダイスのタイプ - # - "tens_d10": 10面ダイスで十の位を決めるダイス - # - "d9": 10面ダイスで0~9を決めるダイス - # - 整数.to_s : #{整数}面ダイス - # @return [Array>] - def getDetailedRandResults - @detailedRandResults - end - def randNomal(max) Kernel.rand(max) end diff --git a/src/test/test_detailed_rand_results.rb b/src/test/test_detailed_rand_results.rb index 392da2391..6618ec410 100644 --- a/src/test/test_detailed_rand_results.rb +++ b/src/test/test_detailed_rand_results.rb @@ -18,10 +18,10 @@ def test_rand 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.detailed_rand_results.size) + assert_equal(:nomal, @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]) @@ -34,10 +34,10 @@ def test_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.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]) @@ -49,7 +49,7 @@ def test_tens_d10_zero value = @bcdice.roll_tens_d10() assert_equal(0, value) - assert_equal(0, @bcdice.getDetailedRandResults[0].value) + assert_equal(0, @bcdice.detailed_rand_results[0].value) assert_equal(10, @bcdice.getRandResults[0][0]) end @@ -59,10 +59,10 @@ def test_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.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]) @@ -77,7 +77,7 @@ def test_coc7th @bcdice.setMessage("CC(2)") @bcdice.dice_command - details = @bcdice.getDetailedRandResults + details = @bcdice.detailed_rand_results assert_equal(3, details.size) assert_equal(:nomal, details[0].kind) From 68cf7aae6195727dd471a579a98ea46175238b24 Mon Sep 17 00:00:00 2001 From: SAKATA Sinji Date: Mon, 16 Mar 2020 09:07:53 +0900 Subject: [PATCH 6/6] Fix spelling error --- src/bcdiceCore.rb | 2 +- src/test/test_detailed_rand_results.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/bcdiceCore.rb b/src/bcdiceCore.rb index 64273dfb5..3834feeca 100755 --- a/src/bcdiceCore.rb +++ b/src/bcdiceCore.rb @@ -1007,7 +1007,7 @@ def rand_inner(max) def rand(max) ret = rand_inner(max) - push_to_detail(:nomal, max, ret + 1) + push_to_detail(:normal, max, ret + 1) return ret end diff --git a/src/test/test_detailed_rand_results.rb b/src/test/test_detailed_rand_results.rb index 6618ec410..2d0bc38f6 100644 --- a/src/test/test_detailed_rand_results.rb +++ b/src/test/test_detailed_rand_results.rb @@ -19,7 +19,7 @@ def test_rand assert_equal(49 - 1, value) assert_equal(1, @bcdice.detailed_rand_results.size) - assert_equal(:nomal, @bcdice.detailed_rand_results[0].kind) + 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) @@ -80,7 +80,7 @@ def test_coc7th details = @bcdice.detailed_rand_results assert_equal(3, details.size) - assert_equal(:nomal, details[0].kind) + assert_equal(:normal, details[0].kind) assert_equal(10, details[0].sides) assert_equal(4, details[0].value)