require "roda"
require_relative "rank_king"
module RankKing
class Web < Roda
plugin :named_templates
plugin :render
template(:layout) { <<~ERB }
Rank King
<%= yield %>
ERB
template(:pools) { <<~ERB }
Pools
ERB
template(:pool) { <<~ERB }
<%= @pool.name %>
Axes
Items
<% @pool.items.each do |item| %>
- <%= item.title %>
<% end %>
ERB
template(:axis) { <<~ERB }
<%= @axis.pool.name %> - <%= @axis.name %>
Items
<% @axis.ratings.sort_by { -_1.ordinal }.each.with_index do |rating, i| %>
<%= i+1 %> |
<%= rating.item.title %> |
<%= rating.ordinal.round(1) %> |
<%= rating.mu.round(1) %> |
<%= rating.sigma.round(1) %> |
<% end %>
<% @game.each do |item| %>
- <%= item.title %>
<% end %>
ERB
route do |r|
r.root do
r.redirect "/pools"
end
r.on "pools" do
r.is do
@pools = Pool.all
view :pools
end
r.on Integer do |id|
@pool = Pool[id]
r.is do
view :pool
end
r.on "axes" do
r.on Integer do |id|
@axis = Axis[id]
r.is "rank" do
items = r.params.values_at("item_0", "item_1").map { Item[_1] }
winner, loser = items.partition { _1.id == r.params.fetch("winner").to_i }.flatten
RankKing.rank(@axis, winner:, loser:)
r.redirect "/pools/#{@pool.id}/axes/#{@axis.id}"
end
r.is do
@game = RankKing.suggest_game(@axis).shuffle
view :axis
end
end
end
end
end
end
end
end