You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
2.7 KiB

require "roda"
require_relative "rank_king"
module RankKing
class Web < Roda
plugin :named_templates
plugin :render
template(:layout) { <<~ERB }
<html>
<body>
<h1>Rank King</h1>
<%= yield %>
</body>
</html>
ERB
template(:pools) { <<~ERB }
<h2>Pools</h2>
<ul>
<% @pools.each do |pool| %>
<li><a href="/pools/<%= pool.id %>"><%= pool.name %></a></li>
<% end %>
</ul>
ERB
template(:pool) { <<~ERB }
<h2><%= @pool.name %></h2>
<h3>Axes</h3>
<ul>
<% @pool.axes.each do |axis| %>
<li><a href="/pools/<%= @pool.id %>/axes/<%= axis.id %>"><%= axis.name %></a></li>
<% end %>
</ul>
<h3>Items</h3>
<ol>
<% @pool.items.each do |item| %>
<li><%= item.title %></li>
<% end %>
</ol>
ERB
template(:axis) { <<~ERB }
<h2><%= @axis.pool.name %> - <%= @axis.name %></h2>
<h3>Items</h3>
<table>
<% @axis.ratings.sort_by { -_1.ordinal }.each.with_index do |rating, i| %>
<tr>
<td><%= i+1 %></td>
<td><%= rating.item.title %></td>
<td><%= rating.ordinal.round(1) %></td>
<td><%= rating.mu.round(1) %></td>
<td><%= rating.sigma.round(1) %></td>
</tr>
<% end %>
</table>
<ul>
<% @game.each do |item| %>
<li><%= item.title %></li>
<% end %>
</ul>
<form method="post" action="/pools/<%= @pool.id %>/axes/<%= @axis.id %>/rank">
<% @game.each.with_index do |item, i| %>
<label><input type="radio" name="winner" value="<%= item.id %>"><%= item.title %></label>
<input type="hidden" name="item_<%= i %>" value="<%= item.id %>">
<% end %>
<input type="submit">
</form>
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