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

BCDice ライブラリを v2 から v3 に更新 #193

Merged
merged 6 commits into from
Jan 9, 2021
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
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
[submodule "vendor/BCDice"]
path = vendor/BCDice
url = https://github.com/bcdice/BCDice.git
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ gem 'nokogiri', '~> 1.6'
gem 'd1lcs'
gem 'json'
gem 'mail', '~> 2.7'
gem 'bcdice'

group :irc do
gem 'mcinch'
Expand Down
8 changes: 6 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ GEM
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
ast (2.4.1)
bcdice (3.0.0.pre.rc.1)
i18n (~> 1.8.5)
buftok (0.2.0)
charlock_holmes (0.7.7)
coderay (1.1.3)
Expand Down Expand Up @@ -57,7 +59,7 @@ GEM
http-parser (1.2.1)
ffi-compiler (>= 1.0, < 2.0)
http_parser.rb (0.6.0)
i18n (1.8.5)
i18n (1.8.7)
concurrent-ruby (~> 1.0)
json (2.3.1)
lumberjack (1.2.7)
Expand Down Expand Up @@ -168,9 +170,11 @@ GEM

PLATFORMS
ruby
x86_64-linux

DEPENDENCIES
activesupport (~> 6.0)
bcdice
charlock_holmes (~> 0.7)
coveralls
d1lcs
Expand All @@ -193,4 +197,4 @@ DEPENDENCIES
yard (~> 0.8)

BUNDLED WITH
2.1.4
2.2.2
8 changes: 2 additions & 6 deletions lib/rgrb/plugin/bcdice/discord_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
require 'rgrb/plugin/bcdice/errors'
require 'rgrb/plugin/bcdice/generator'

require 'BCDice/src/cgiDiceBot'
require 'BCDice/src/diceBot/DiceBotLoader'
require 'BCDice/src/diceBot/DiceBotLoaderList'

module RGRB
module Plugin
module Bcdice
Expand Down Expand Up @@ -42,15 +38,15 @@ def bcdice(m, command, specified_game_title)
result =
begin
@generator.bcdice(command, specified_game_title)
rescue => e
rescue DiceBotNotFound, InvalidCommandError => e
notice_bcdice_error(m.channel, header_common, e)
return
end

# ゲームシステム名を含むヘッダ
header = "#{header_common}<#{result.game_name}>: "

send_channel(m.channel, result.message_lines, header)
send_channel(m.channel, result.message, header)
end

# git submodule で組み込んでいる BCDice のバージョンを出力する
Expand Down
5 changes: 1 addition & 4 deletions lib/rgrb/plugin/bcdice/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ def initialize(game_type)
class InvalidCommandError < StandardError
# @return [String] 指定されたコマンド
attr_reader :command
# @return [String] ゲームタイプ
attr_reader :game_type
# @return [String] ゲームシステム名
attr_reader :game_name

Expand All @@ -33,8 +31,7 @@ def initialize(command, dice_bot)
super("コマンド「#{command}」は無効です")

@command = command
@game_type = dice_bot.gameType
@game_name = dice_bot.gameName
@game_name = dice_bot::NAME
end
end
end
Expand Down
67 changes: 14 additions & 53 deletions lib/rgrb/plugin/bcdice/generator.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
# vim: fileencoding=utf-8

require 'bcdice'
require 'bcdice/game_system'

require 'rgrb/plugin_base/generator'
require 'rgrb/plugin/bcdice/constants'
require 'rgrb/plugin/bcdice/errors'

require 'BCDice/src/cgiDiceBot'
require 'BCDice/src/diceBot/DiceBotLoader'
require 'BCDice/src/diceBot/DiceBotLoaderList'

module RGRB
module Plugin
# BCDice のラッパープラグイン
module Bcdice
# BCDice の呼び出し結果
BcdiceResult = Struct.new(:error, :message_lines, :game_type, :game_name)
BcdiceResult = Struct.new(:message, :game_name)

# Bcdice の出力テキスト生成器
class Generator
include PluginBase::Generator

# ジェネレータを初期化する
def initialize
@bcdice = CgiDiceBot.new
@version_and_commit_id = get_version_and_commit_id
end

# プラグインがアダプタによって読み込まれた際の設定
#
# アダプタによってジェネレータが用意されたとき
Expand All @@ -44,61 +37,29 @@ def configure(*)
# @raise [DiceBotNotFound] ダイスボットが見つからなかった場合
# @raise [InvalidCommandError] 無効なコマンドが指定された場合
def bcdice(command, specified_game_type = nil)
# ゲームタイプが指定されていなかったら DiceBot にする
# アダプターは通常、常に引数を2つ与えてこのメソッドを呼ぶ
# そのため、デフォルト引数値では game_type を設定できない
game_type = specified_game_type || 'DiceBot'
# ダイスボットを探す
dice_bot = find_dice_bot(game_type)
dice_bot = BCDice.game_system_class(game_type)
# ダイスボットが見つからなかった場合は中断する
raise DiceBotNotFound, game_type unless dice_bot

result, _ = @bcdice.roll(command, game_type)
result = dice_bot.eval(command)
# 結果が返ってこなかった場合は中断する
raise InvalidCommandError.new(command, dice_bot) if result.empty?

# 結果の行の配列
message_lines = result.lstrip.split(' : ', 2)[1].lines
raise InvalidCommandError.new(command, dice_bot) unless result

# 結果を返す
BcdiceResult.new(command,
message_lines,
dice_bot.gameType,
dice_bot.gameName)
BcdiceResult.new(
result.text,
dice_bot::NAME
)
end

# git submodule で組み込んでいる BCDice のバージョンを返す
# @return [String]
def bcdice_version
"BCDice Version: #{@version_and_commit_id}"
end

private

# 起動時点での BCDice のコミット ID を取得・保存する
# @return [String]
def get_version_and_commit_id
bcdice_path = File.expand_path('../../../../vendor/BCDice', __dir__)
@commit_id =
begin
Dir.chdir(bcdice_path) do
`git show -s --format=%H`.strip
end
rescue
''
end

@commit_id.empty? ? $bcDiceVersion : "#{$bcDiceVersion} (#{@commit_id})"
end

# ダイスボットを探す
# @param [String] game_type ゲームタイプ
# @return [DiceBot]
def find_dice_bot(game_type)
if game_type == 'DiceBot'
DiceBot.new
else
DiceBotLoaderList.find(game_type)&.loadDiceBot ||
DiceBotLoader.loadUnknownGame(game_type)
end
"BCDice Version: #{BCDice::VERSION}"
end
end
end
Expand Down
8 changes: 2 additions & 6 deletions lib/rgrb/plugin/bcdice/irc_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
require 'rgrb/plugin/bcdice/errors'
require 'rgrb/plugin/bcdice/generator'

require 'BCDice/src/cgiDiceBot'
require 'BCDice/src/diceBot/DiceBotLoader'
require 'BCDice/src/diceBot/DiceBotLoaderList'

module RGRB
module Plugin
module Bcdice
Expand Down Expand Up @@ -42,15 +38,15 @@ def bcdice(m, command, specified_game_title)
result =
begin
@generator.bcdice(command, specified_game_title)
rescue => e
rescue DiceBotNotFound, InvalidCommandError => e
notice_bcdice_error(m.target, header_common, e)
return
end

# ゲームシステム名を含むヘッダ
header = "#{header_common}<#{result.game_name}>: "

send_notice(m.target, result.message_lines, header)
send_notice(m.target, result.message, header)
end

# git submodule で組み込んでいる BCDice のバージョンを出力する
Expand Down
10 changes: 4 additions & 6 deletions spec/rgrb/plugin/bcdice/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
describe '#bcdice_version' do
it 'BCDice のバージョンを出力する' do
expect(generator.bcdice_version).to(
match(/\ABCDice Version: [0-9]\.[0-9]{2}\.[0-9]{2} \([0-9a-f]{40}\)\z/)
match(/\ABCDice Version: \d+\.\d+\.\d+/)
)
end
end
Expand All @@ -31,12 +31,11 @@
subject { generator.bcdice('2d6') }

it 'ゲームシステムとして DiceBot が選ばれる' do
expect(subject.game_type).to eq('DiceBot')
expect(subject.game_name).to eq('DiceBot')
end

it '2d6 の結果が返る' do
expect(subject.message_lines[0].start_with?('(2D6) > ')).to be(true)
expect(subject.message.start_with?('(2D6) > ')).to be(true)
end
end
end
Expand All @@ -52,15 +51,14 @@

context('ソード・ワールド2.0') do
context('k20') do
subject { generator.bcdice('k20', 'SwordWorld2_0') }
subject { generator.bcdice('k20', 'SwordWorld2.0') }

it 'ゲームシステムとして「ソード・ワールド2.0」が選ばれる' do
expect(subject.game_type).to eq('SwordWorld2.0')
expect(subject.game_name).to eq('ソードワールド2.0')
end

it 'k20 の結果が返る' do
expect(subject.message_lines[0].start_with?('KeyNo.20c')).to be(true)
expect(subject.message.start_with?('KeyNo.20c')).to be(true)
end
end
end
Expand Down
1 change: 0 additions & 1 deletion vendor/BCDice
Submodule BCDice deleted from ff7639