From 844ed937f5c128c3d6ecba27a500d0d397faecee Mon Sep 17 00:00:00 2001 From: SAKATA Sinji Date: Tue, 31 Jan 2023 09:33:27 +0900 Subject: [PATCH] =?UTF-8?q?tester-ui=E3=82=92master=E3=81=AB=E8=BF=BD?= =?UTF-8?q?=E5=BE=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/bcdice_api/app.rb | 2 + lib/bcdice_api/controller/tester.rb | 59 +++++++++++++++++++++++++++++ lib/bcdice_api/views/tester.erb | 9 ++++- 3 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 lib/bcdice_api/controller/tester.rb diff --git a/lib/bcdice_api/app.rb b/lib/bcdice_api/app.rb index e72b723..95b6b58 100644 --- a/lib/bcdice_api/app.rb +++ b/lib/bcdice_api/app.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require_relative 'controller/root' +require_relative 'controller/tester' require_relative 'controller/v1' require_relative 'controller/v2' @@ -8,6 +9,7 @@ module BCDiceAPI APP = Rack::URLMap.new( { '/' => Controller::Root, + '/tester' => Controller::Tester, '/v1' => Controller::V1, '/v2' => Controller::V2 } diff --git a/lib/bcdice_api/controller/tester.rb b/lib/bcdice_api/controller/tester.rb new file mode 100644 index 0000000..5b15490 --- /dev/null +++ b/lib/bcdice_api/controller/tester.rb @@ -0,0 +1,59 @@ +# frozen_string_literal: true + +require_relative '../base' + +module BCDiceAPI + module Controller + class Tester < Base + helpers do + def roll(id, command) + klass = BCDiceAPI::DICEBOTS[id] + raise UnsupportedDicebot if klass.nil? + raise CommandError if command.nil? || command.empty? + + game_system = klass.new(command) + + result = game_system.eval + raise CommandError if result.nil? + + { + text: result.text, + rands: result.detailed_rands.map(&:to_h).map{|item| item[:value]} + } + end + end + + TEST_GAME_TYPE = "DiceBot" + + get "/" do + @command = params[:command] + game_system_id = params[:game] + + dicebot = BCDiceAPI::DICEBOTS[game_system_id] || BCDiceAPI::DICEBOTS[TEST_GAME_TYPE] + @title = dicebot::NAME + @id = dicebot::ID + @help = dicebot::HELP_MESSAGE + + if @command + begin + @result = roll(game_system_id, @command) + rescue CommandError + @result = { + text: "対応していないコマンド:#{@command}", + rands: [], + } + end + end + erb :tester + end + + not_found do + json ok: false, reason: 'not found' + end + + error do + json ok: false + end + end + end +end diff --git a/lib/bcdice_api/views/tester.erb b/lib/bcdice_api/views/tester.erb index c314f0f..d9ddab8 100644 --- a/lib/bcdice_api/views/tester.erb +++ b/lib/bcdice_api/views/tester.erb @@ -16,6 +16,10 @@ } .pre { white-space: pre-wrap; + + padding: 1rem; + background-color: #f5f5f5; + border-radius: 4px; } @@ -38,6 +42,7 @@
+
@@ -48,11 +53,11 @@ <% if @result %>

出力 <%= @command %>

-<%= @result[:result] %>
+<%= @result[:text] %>
         

ダイス

-<%= @result[:dices] %>
+<%= @result[:rands] %>
         
<% end %>