Skip to content

Commit

Permalink
tester-uiをmasterに追従
Browse files Browse the repository at this point in the history
  • Loading branch information
ysakasin committed Jan 31, 2023
1 parent 1c748df commit 844ed93
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/bcdice_api/app.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# frozen_string_literal: true

require_relative 'controller/root'
require_relative 'controller/tester'
require_relative 'controller/v1'
require_relative 'controller/v2'

module BCDiceAPI
APP = Rack::URLMap.new(
{
'/' => Controller::Root,
'/tester' => Controller::Tester,
'/v1' => Controller::V1,
'/v2' => Controller::V2
}
Expand Down
59 changes: 59 additions & 0 deletions lib/bcdice_api/controller/tester.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 7 additions & 2 deletions lib/bcdice_api/views/tester.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
}
.pre {
white-space: pre-wrap;

padding: 1rem;
background-color: #f5f5f5;
border-radius: 4px;
}
</style>
</head>
Expand All @@ -38,6 +42,7 @@
<form>
<div class="form-group">
<div class="input-group mb-3">
<input type="text" id="game" name="game" hidden value="<%= @id %>">
<input type="text" id="command" name="command" class="form-control form-control-lg" placeholder="1D6" value="<%= @command %>">
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="send">Send (Enter)</button>
Expand All @@ -48,11 +53,11 @@
<% if @result %>
<h3>出力 <small><%= @command %></small></h3>
<pre class="pre">
<%= @result[:result] %>
<%= @result[:text] %>
</pre>
<h4>ダイス</h4>
<pre class="pre">
<%= @result[:dices] %>
<%= @result[:rands] %>
</pre>
<% end %>
</div>
Expand Down

0 comments on commit 844ed93

Please sign in to comment.