BCDice/TRPGツールからの呼び出し方/BCDice-API

提供: TRPGツール開発・運用Wiki
< BCDice‎ | TRPGツールからの呼び出し方
2019年6月28日 (金) 22:12時点におけるOchaochaocha3 (トーク | 投稿記録)による版 (読みを設定する)
ナビゲーションに移動 検索に移動
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

BCDice-APIからのBCDiceの呼び出し方。v0.6.2のソースコードを参考にしている。

server.rb L74-L78

https://github.com/ysakasin/bcdice-api/blob/0dfd095a4c7befed58ffbb7eb880a9d729badf2a/server.rb#L74-L78

パス `/v1/diceroll` にリクエストが来たときの処理。ダイスロールを行う。

get "/v1/diceroll" do
  result, secret, dices = diceroll(params[:system], params[:command])

  jsonp ok: true, result: result, secret: secret, dices: dices
end

server.rb L20-L42

https://github.com/ysakasin/bcdice-api/blob/0dfd095a4c7befed58ffbb7eb880a9d729badf2a/server.rb#L20-L42

ダイスロールを行うメソッド。

def diceroll(system, command)
  dicebot = BCDice::DICEBOTS[system]
  if dicebot.nil?
    raise UnsupportedDicebot
  end
  if command.nil? || command.empty?
    raise CommandError
  end

  bcdice = BCDiceMaker.new.newBcDice
  bcdice.setDiceBot(dicebot)
  bcdice.setMessage(command)
  bcdice.setCollectRandResult(true)

  result, secret = bcdice.dice_command
  dices = bcdice.getRandResults.map {|dice| {faces: dice[1], value: dice[0]}}

  if result.nil?
    raise CommandError
  end

  return result, secret, dices
end