Skip to content

Commit

Permalink
Merge pull request #134 from bcdice/detailed_rand_results
Browse files Browse the repository at this point in the history
Tens D10などの詳細なダイスロール一覧
  • Loading branch information
ysakasin authored Mar 19, 2020
2 parents 5ad5ef3 + 00ffb91 commit 164e0a7
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 12 deletions.
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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
Expand Down
70 changes: 60 additions & 10 deletions src/bcdiceCore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -105,7 +108,6 @@ def initialize(parent, cardTrader, diceBot, counterInfos, tableFileData)
@isMessagePrinted = false
@rands = nil
@isKeepSecretDice = true
@randResults = nil
@isIrcMode = true
end

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/diceBot/Cthulhu7th.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,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.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

0 comments on commit 164e0a7

Please sign in to comment.