Skip to content

Commit

Permalink
Refactor BCDice#check_suc
Browse files Browse the repository at this point in the history
- DiceBot#check_suc に移動
- パラメータを厳格化
- DiceBot#getDiceList, @diceText, @diffText を廃止
  • Loading branch information
ysakasin committed Mar 30, 2020
1 parent 2f4153f commit 6bdb593
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 129 deletions.
75 changes: 0 additions & 75 deletions src/bcdiceCore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1444,81 +1444,6 @@ def check_hit(dice_now, signOfInequality, diff) # 成功数判定用
return suc
end

#################### ゲーム別成功度判定 ########################
def check_suc(*check_param)
total_n, dice_n, signOfInequality, diff, dice_cnt, dice_max, n1, n_max = *check_param

debug('check params : total_n, dice_n, signOfInequality, diff, dice_cnt, dice_max, n1, n_max',
total_n, dice_n, signOfInequality, diff, dice_cnt, dice_max, n1, n_max)

return "" unless /((\+|\-)?[\d]+)[)]?$/ =~ total_n.to_s

total_n = Regexp.last_match(1).to_i
diff = diff.to_i

check_paramNew = [total_n, dice_n, signOfInequality, diff, dice_cnt, dice_max, n1, n_max]

text = getSuccessText(*check_paramNew)
text ||= ""

if text.empty?
if signOfInequality != ""
debug('どれでもないけど判定するとき')
return check_nDx(*check_param)
end
end

return text
end

def getSuccessText(*check_param)
debug('getSuccessText begin')

_total_n, _dice_n, _signOfInequality, _diff, dice_cnt, dice_max, = *check_param

debug("dice_max, dice_cnt", dice_max, dice_cnt)

if (dice_max == 100) && (dice_cnt == 1)
debug('1D100判定')
return @diceBot.check_1D100(*check_param)
end

if (dice_max == 20) && (dice_cnt == 1)
debug('1d20判定')
return @diceBot.check_1D20(*check_param)
end

if dice_max == 10
debug('d10ベース判定')
return @diceBot.check_nD10(*check_param)
end

if dice_max == 6
if dice_cnt == 2
debug('2d6判定')
result = @diceBot.check_2D6(*check_param)
return result unless result.empty?
end

debug('xD6判定')
return @diceBot.check_nD6(*check_param)
end

return ""
end

def check_nDx(total_n, _dice_n, signOfInequality, diff, _dice_cnt, _dice_max, _n1, _n_max) # ゲーム別成功度判定(ダイスごちゃ混ぜ系)
debug('check_nDx begin diff', diff)
success = check_hit(total_n, signOfInequality, diff)
debug('check_nDx success', success)

if success >= 1
return " > 成功"
end

return " > 失敗"
end

###########################################################################
# ** 出力関連
###########################################################################
Expand Down
18 changes: 13 additions & 5 deletions src/dice/AddDice.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# -*- coding: utf-8 -*-

require "utils/normalizer"

class AddDice
def initialize(bcdice, diceBot)
@bcdice = bcdice
@diceBot = diceBot
@nick_e = @bcdice.nick_e

@dice_list = []
end

#################### 加算ダイス ########################
Expand Down Expand Up @@ -69,9 +73,6 @@ def rollDice(string)
string += "#{signOfInequality}#{diffText}"
end

@diceBot.setDiceText(output)
@diceBot.setDiffText(diffText)

# ダイス目による補正処理(現状ナイトメアハンターディープ専用)
addText, revision = @diceBot.getDiceRevision(n_max, dice_max, total_n)
debug('addText, revision', addText, revision)
Expand All @@ -90,7 +91,9 @@ def rollDice(string)
total_n += revision

if signOfInequality != "" # 成功度判定処理
successText = @bcdice.check_suc(total_n, dice_n, signOfInequality, diffText, dice_cnt, dice_max, n1, n_max)
cmp_op = Normalizer.cmp_op(signOfInequality)
target = Normalizer.target_number(diffText)
successText = @diceBot.check_suc(total_n, dice_n, @dice_list, dice_max, cmp_op, target)
debug("check_suc successText", successText)
output += successText
end
Expand Down Expand Up @@ -295,7 +298,10 @@ def rollLocal(dice_wk, dice_max, sortType)
return rollD66(dice_wk)
end

return @bcdice.roll(dice_wk, dice_max, sortType)
ret = @bcdice.roll(dice_wk, dice_max, sortType)
@dice_list.concat(ret[1].split(",").map(&:to_i))

return ret
end

def rollD66(count)
Expand All @@ -310,6 +316,8 @@ def rollD66(count)
n1Count = d66List.count(1)
nMaxCount = d66List.count(66)

@dice_list.concat(d66List)

return [total, text, n1Count, nMaxCount, 0, 0, 0]
end

Expand Down
116 changes: 67 additions & 49 deletions src/diceBot/DiceBot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,6 @@ def rand(max)
@@bcdice.rand(max)
end

def check_suc(*params)
@@bcdice.check_suc(*params)
end

def roll(*args)
@@bcdice.roll(*args)
end
Expand Down Expand Up @@ -288,39 +284,84 @@ def rollDiceCommand(_command)
nil
end

def setDiceText(diceText)
debug("setDiceText diceText", diceText)
@diceText = diceText
end

def setDiffText(diffText)
@diffText = diffText
end

def dice_command_xRn(_string, _nick_e)
''
end

def check_2D6(_total_n, _dice_n, _signOfInequality, _diff, _dice_cnt, _dice_max, _n1, _n_max) # ゲーム別成功度判定(2D6)
''
end
# @param total [Integer] コマンド合計値
# @param dice_total [Integer] ダイス目の合計値
# @param dice_list [Array<Integer>] ダイスの一覧
# @param sides [Integer] 振ったダイスの面数
# @param cmp_op [Symbol] 比較演算子
# @param target [Integer, String] 目標値の整数か'?'
# @return [String]
def check_suc(total, dice_total, dice_list, sides, cmp_op, target)
ret =
case [dice_list.size, sides]
when [1, 100]
check_1D100(total, dice_total, cmp_op, target)
when [1, 20]
check_1D20(total, dice_total, cmp_op, target)
when [2, 6]
check_2D6(total, dice_total, dice_list, cmp_op, target)
end

def check_nD6(_total_n, _dice_n, _signOfInequality, _diff, _dice_cnt, _dice_max, _n1, _n_max) # ゲーム別成功度判定(nD6)
''
end
return ret unless ret.nil? || ret.empty?

def check_nD10(_total_n, _dice_n, _signOfInequality, _diff, _dice_cnt, _dice_max, _n1, _n_max) # ゲーム別成功度判定(nD10)
''
end
ret =
case sides
when 10
check_nD10(total, dice_total, dice_list, cmp_op, target)
when 6
check_nD6(total, dice_total, dice_list, cmp_op, target)
end

def check_1D100(_total_n, _dice_n, _signOfInequality, _diff, _dice_cnt, _dice_max, _n1, _n_max) # ゲーム別成功度判定(1d100)
''
return ret unless ret.nil? || ret.empty?

check_nDx(total, cmp_op, target)
end

def check_1D20(_total_n, _dice_n, _signOfInequality, _diff, _dice_cnt, _dice_max, _n1, _n_max) # ゲーム別成功度判定(1d20)
''
# 成功か失敗かを文字列で返す
#
# @param (see #check_suc)
# @return [String]
def check_nDx(total, cmp_op, target)
return " > 失敗" if target.is_a?(String)

# Due to Ruby 1.8
success = cmp_op == :"!=" ? total != target : total.send(cmp_op, target)
if success
" > 成功"
else
" > 失敗"
end
end

# @abstruct
# @param (see #check_suc)
# @return [nil]
def check_1D100(total, dice_total, cmp_op, target); end

# @abstruct
# @param (see #check_suc)
# @return [nil]
def check_1D20(total, dice_total, cmp_op, target); end

# @abstruct
# @param (see #check_suc)
# @return [nil]
def check_nD10(total, dice_total, dice_list, cmp_op, target); end

# @abstruct
# @param (see #check_suc)
# @return [nil]
def check_2D6(total, dice_total, dice_list, cmp_op, target); end

# @abstruct
# @param (see #check_suc)
# @return [nil]
def check_nD6(total, dice_total, dice_list, cmp_op, target); end

def get_table_by_2d6(table)
get_table_by_nD6(table, 2)
end
Expand Down Expand Up @@ -431,29 +472,6 @@ def should_reroll?(loop_count)
loop_count < @rerollLimitCount || @rerollLimitCount == 0
end

def getDiceList
getDiceListFromDiceText(@diceText)
end

def getDiceListFromDiceText(diceText)
debug("getDiceList diceText", diceText)

diceList = []

if /\[([\d,]+)\]/ =~ diceText
diceText = Regexp.last_match(1)
end

return diceList unless /([\d,]+)/ =~ diceText

diceString = Regexp.last_match(1)
diceList = diceString.split(/,/).collect { |i| i.to_i }

debug("diceList", diceList)

return diceList
end

# ** 汎用表サブルーチン
def get_table_by_number(index, table, default = '1')
table.each do |item|
Expand Down
36 changes: 36 additions & 0 deletions src/utils/normalizer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Normalizer
module_function

# 比較演算子をシンボルに正規化する
#
# @param op [String]
# @return [Symbol, nil]
def cmp_op(op)
case op
when /<=|=</
:<=
when />=|=>/
:>=
when /<>|!=|=!/
:'!='
when /</
:<
when />/
:>
when /\=/
:==
end
end

# 目標値を正規化する
#
# @param val [String]
# @return [Integer, String] 整数か'?'
def target_number(val)
if val == '?'
val
else
val.to_i
end
end
end

0 comments on commit 6bdb593

Please sign in to comment.