4 # Author:: Daigo Moriwaki
5 # Homepage:: http://sourceforge.jp/projects/shogi-server/
8 # Copyright (C) 2006-2008 Daigo Moriwaki <daigo at debian dot org>
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 # mk_html generates an html page from a players.yaml.
31 # ./mk_html [OPTION] < players.yaml > rating.html
33 # [<tt>-h, --help</tt>]
36 # [<tt>-w,--wdoor</tt>]
39 # [<tt>--footer</tt> <i>filename</i>]
40 # insert contents in the filename at the bottom of a genrated page.
46 # $ sudo aptitude install ruby ruby1.8
50 # $ ./mk_html --footer ./wdoor_1000.html < players.yaml > players.html
52 # $ cat ./wdoor-1000.html
53 # <p>The average of the rates in a group is always 1000.
54 # <p><a href="http://wdoor.c.u-tokyo.ac.jp/shogi/">BACK</a>
64 if (Time.now - time < 30*60)
66 elsif (Time.now - time < 24*60*60)
67 return time.strftime("%Y-%m-%d")
68 # return "%d hours ago" % [(Time.now - time)/3600]
70 return time.strftime("%Y-%m-%d")
74 def remove_long_to_see_players(file)
75 return unless file["players"][999] # only for Not-Yet-Rated players
77 file["players"][999].delete_if do |key, value|
78 value['last_modified'] < Time.now - 24*3600*30 # 30 days
85 opts = OptionParser.new
86 opts.banner = "Usage: mk_html [OPTIONS]"
87 opts.on("--footer filename", String, "Insert contents of the filename at the bottom of a genrated page") do |filename|
88 unless File.exist?(filename)
89 $stderr.puts "File not found: %s" % [filename]
92 $footer_content = File.open(filename).read
94 opts.on("-w","--wdoor", "adopt a wdoor style") { $wdoor=true }
95 opts.on_tail("-h", "--help", "show this message") do
107 while l = $stdin.gets do
110 file = YAML::load(lines)
111 erb = ERB.new( DATA.read, nil, "%>" )
114 file["players"].keys.sort.each do |index|
116 group_names << "#{index}"
118 group_names << "Not-Yet-Rated Players"
121 remove_long_to_see_players(file)
127 file["players"].keys.each do |group_index|
128 file["players"][group_index].each do |player, yaml|
129 if player == "YSS+707d4f98d9d2620cdaab58f19d02a2e4"
130 yss_rate = yaml['rate']
136 file["players"].sort.each do |key, yaml| # sort groups in the order written in players.yaml
137 sorted_keys = yaml.keys.sort do |a,b|
138 # sort players in a group by one's rate
139 if yaml[b]['rate'] == 0 && yaml[a]['rate'] == 0
140 # mainly for not-rated-yet players
141 yaml[b]['last_modified'] <=> yaml[a]['last_modified']
143 yaml[b]['rate'] <=> yaml[a]['rate']
147 table = ERB.new(<<ENDTABLE, nil, "%>")
148 % sorted_keys.each do |key|
150 win = yaml[key]['win']
151 loss = yaml[key]['loss']
152 win_rate = win.to_f / (win + loss)
153 last_modified = yaml[key]['last_modified']
155 player_decoration = "default"
157 case (Time.now - last_modified)/60 # minutes
158 when (0..30) then player_decoration = "current"
159 when (0..(1*60*24)) then player_decoration = "today"
160 when (0..(7*60*24)) then player_decoration = "this_week"
164 when "yowai_gps+95908f6c18338f5340371f71523fc5e3" then player_decoration = "yowai_gps"
165 when "gps+11648e4e66e7ed6a86cb7f1d0cf604fe" then player_decoration = "gps"
168 rate = yaml[key]['rate']
170 diff_rate = rate - top_rate
171 diff_possibility = 1.0/(1.0 + 10**(-diff_rate/400.0))
173 <tr class="<%=player_decoration%>">
175 <a id="popup<%=popup_id+=1%>" href="http://wdoor.c.u-tokyo.ac.jp/shogi/tools/view/show-player.cgi?event=LATEST&filter=floodgate&show_self_play=1&user=<%=u key%>"><%= h yaml[key]['name'] %></a>
176 <script type="text/javascript">
177 var tooltip<%=popup_id%> = new YAHOO.widget.Tooltip("myTooltip", {
178 context:"popup<%=popup_id%>",
179 text:"<%= h key %>" } );
183 <span id="popup<%=popup_id+=1%>"><%= rate != 0 ? "%5d" % [ rate ] : "N/A" %></span>
184 <script type="text/javascript">
185 var tooltip<%=popup_id%> = new YAHOO.widget.Tooltip("myTooltip", {
186 context:"popup<%=popup_id%>",
187 text:"Behind <%= "%5d" % [ diff_rate ] %> (<%= "%.3f" % [ diff_possibility ] %>)" } );
191 <%= "%5d" % [ win ] %> </td>
193 <%= "%5d" % [ loss ] %> </td>
194 <td class="win_rate">
195 <%= "%.3f" % [win_rate] %> </td>
196 <td class="last_modified">
197 <%= show_date(last_modified) %> </td>
200 <%= rate != 0 && yss_rate > 0 ? ("%5d" % [2300 - yss_rate + rate]) : "N/A" %> </td>
206 tables << table.result(binding)
209 body = erb.result(binding)
217 # vim: ts=2 sw=2 sts=0
222 <title>Shogi Server Rating</title>
223 <link rel="StyleSheet" type="text/css" href="http://wdoor.c.u-tokyo.ac.jp/shogi/shogi.css">
225 <!-- License: http://developer.yahoo.com/yui/license.html -->
226 <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.4.1/build/reset-fonts-grids/reset-fonts-grids.css">
227 <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.4.1/build/base/base-min.css">
228 <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.4.1/build/container/assets/container.css">
229 <!-- Dependencies -->
230 <script type="text/javascript" src="http://yui.yahooapis.com/2.4.1/build/yahoo-dom-event/yahoo-dom-event.js"></script>
231 <!-- OPTIONAL: Animation (only required if enabling Animation) -->
232 <script type="text/javascript" src="http://yui.yahooapis.com/2.4.1/build/animation/animation-min.js"></script>
234 <script type="text/javascript" src="http://yui.yahooapis.com/2.4.1/build/container/container-min.js"></script>
235 <style type="text/css"><!--
236 TABLE {margin-left: auto;
238 CAPTION { caption-side: left;}
239 .name {text-align: center;
241 .rate, .ngames, .win_rate {text-align: right;}
242 .last_modified {text-align: center;}
243 .gps, .yowai_gps {background-color: lightgreen;}
244 .current {background-color: #FFD700;}
245 .today {background-color: #FFFF00;}
246 .this_week {background-color: #FFFFAA;}
248 #bd {text-align: center;}
249 #ft {text-align: right;}
257 <h1>Shogi Server Rating</h1>
259 % tables.each_with_index do |t, index|
261 <caption>Group: <%=group_names[index]%></caption>
267 <col class="win_rate">
268 <col class="last_modified">
280 <th>last_modified</th>
291 <hr style="width:50%; margin:1em auto;">
295 <p>Groups are independently rated. You can not compare rates across them.
297 <%= $footer_content %>
304 <p>Last modified at <%=Time.now%>