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 all 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
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 @@ -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.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