-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
214 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# vim: fileencoding=utf-8 | ||
|
||
module RGRB | ||
module Plugin | ||
module Bcdice | ||
# ダイスボットが見つからないことを示すエラー | ||
class DiceBotNotFound < StandardError | ||
# @return [String] ゲームタイプ | ||
attr_reader :game_type | ||
|
||
# エラーを初期化する | ||
# @param [String] game_type ゲームタイプ | ||
def initialize(game_type) | ||
super("ゲームシステム「#{game_type}」は見つかりませんでした") | ||
|
||
@game_type = game_type | ||
end | ||
end | ||
|
||
# 無効なコマンドエラー | ||
class InvalidCommandError < StandardError | ||
# @return [String] 指定されたコマンド | ||
attr_reader :command | ||
# @return [String] ゲームタイプ | ||
attr_reader :game_type | ||
# @return [String] ゲームシステム名 | ||
attr_reader :game_name | ||
|
||
# エラーを初期化する | ||
# @param [String] command 指定されたコマンド | ||
# @param [DiceBot] dice_bot ダイスボット | ||
def initialize(command, dice_bot) | ||
super("コマンド「#{command}」は無効です") | ||
|
||
@command = command | ||
@game_type = dice_bot.gameType | ||
@game_name = dice_bot.gameName | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# vim: fileencoding=utf-8 | ||
|
||
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) | ||
|
||
# Bcdice の出力テキスト生成器 | ||
class Generator | ||
# 生成器を初期化する | ||
def initialize | ||
@bcdice = CgiDiceBot.new | ||
end | ||
|
||
# BCDice でダイスを振った結果を返す | ||
# @param [String] command ダイスコマンド | ||
# @param [String] specified_game_type 指定されたゲームタイプ | ||
# @return [BcdiceResult] | ||
# @raise [DiceBotNotFound] ダイスボットが見つからなかった場合 | ||
# @raise [InvalidCommandError] 無効なコマンドが指定された場合 | ||
def bcdice(command, specified_game_type = nil) | ||
# ゲームタイプが指定されていなかったら DiceBot にする | ||
game_type = specified_game_type || 'DiceBot' | ||
# ダイスボットを探す | ||
dice_bot = find_dice_bot(game_type) | ||
# ダイスボットが見つからなかった場合は中断する | ||
raise DiceBotNotFound, game_type unless dice_bot | ||
|
||
result, _ = @bcdice.roll(command, game_type) | ||
# 結果が返ってこなかった場合は中断する | ||
raise InvalidCommandError.new(command, dice_bot) if result.empty? | ||
|
||
# 結果の行の配列 | ||
message_lines = result.lstrip.split(' : ', 2)[1].lines | ||
|
||
# 結果を返す | ||
BcdiceResult.new(command, | ||
message_lines, | ||
dice_bot.gameType, | ||
dice_bot.gameName) | ||
end | ||
|
||
# git submodule で組み込んでいる BCDice のバージョンを返す | ||
# @return [String] | ||
def bcdice_version | ||
bcdice_path = File.expand_path('../../../../vendor/BCDice', __dir__) | ||
commit_id = Dir.chdir(bcdice_path) do | ||
`git show -s --format=%H`.strip | ||
end | ||
|
||
"BCDice Commit ID: #{commit_id}" | ||
end | ||
|
||
private | ||
|
||
# ダイスボットを探す | ||
# @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 | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# vim: fileencoding=utf-8 | ||
|
||
require_relative '../../../spec_helper' | ||
|
||
require 'rgrb/plugin/bcdice/generator' | ||
require 'rgrb/plugin/bcdice/errors' | ||
|
||
describe RGRB::Plugin::Bcdice::Generator do | ||
let(:generator) { described_class.new } | ||
|
||
describe '#bcdice_version' do | ||
it 'BCDice のバージョンを出力する' do | ||
expect(generator.bcdice_version).to( | ||
match(/\ABCDice Commit ID: [0-9a-f]{40}\z/) | ||
) | ||
end | ||
end | ||
|
||
describe '#bcdice' do | ||
context('ゲームシステムなし') do | ||
context('無効なコマンド') do | ||
it '無効なコマンドのエラーが発生する' do | ||
expect { generator.bcdice('not_found') }.to raise_error( | ||
RGRB::Plugin::Bcdice::InvalidCommandError, | ||
'コマンド「not_found」は無効です' | ||
) | ||
end | ||
end | ||
|
||
context('2d6') do | ||
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) | ||
end | ||
end | ||
end | ||
|
||
context('ソード・ワールド2.0、k20') do | ||
context('k20') do | ||
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) | ||
end | ||
end | ||
end | ||
end | ||
end |