OSDN Git Service

* Changed the stley of the comment lines to RDoc's one.
[shogi-server/shogi-server.git] / mk_html
1 #!/usr/bin/ruby
2 # $Id$
3 #
4 # Author:: Daigo Moriwaki
5 # Homepage:: http://sourceforge.jp/projects/shogi-server/
6 #
7 #--
8 # Copyright (C) 2006-2008 Daigo Moriwaki <daigo at debian dot org>
9 #
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.
14 #
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.
19 #
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
23 #++
24 #
25 # == Synopsis
26 #
27 # mk_html generates an html page from a players.yaml.
28 #
29 # == Usage
30 #
31 # ./mk_html [OPTION] < players.yaml > rating.html
32 #
33 # -h, --help::
34 #    show help
35 #
36 # -w,--wdoor::
37 #    adpot wdoor style
38 #
39 # == PREREQUIRE
40 #
41 # * Ruby 1.8.7 and RDoc
42 #
43 #   $ sudo aptitude install ruby1.8 rdoc1.8
44 #
45
46 require 'optparse'
47 require 'rdoc/usage'
48 require 'yaml'
49 require 'erb'
50
51 include ERB::Util
52
53 def show_date(time)
54   if (Time.now - time < 30*60)
55     return "on line"
56   elsif (Time.now - time < 24*60*60)
57     return time.strftime("%Y-%m-%d")
58     # return "%d hours ago" % [(Time.now - time)/3600]
59   else
60     return time.strftime("%Y-%m-%d")
61   end
62 end
63
64 def usage
65   $stderr.puts <<-EOF
66 USAGE: #{$0} 
67   EOF
68   exit 1
69 end
70
71 def remove_long_to_see_players(file)
72   return unless file["players"][999] # only for Not-Yet-Rated players
73
74   file["players"][999].delete_if do |key, value|
75     value['last_modified'] < Time.now - 24*3600*30 # 30 days
76   end
77 end
78
79 def main
80   $wdoor = false
81   opts = OptionParser.new
82   opts.on("-h", "--help"){ RDoc::usage(0, 'Synopsis', 'Usage') }
83   opts.on("-w","--wdoor") { $wdoor=true }
84   opts.parse(ARGV) rescue RDoc::usage(1, 'Synopsis', 'Usage')
85
86   lines = ""
87   while l = $stdin.gets do
88     lines << l
89   end
90   file = YAML::load(lines)
91   erb = ERB.new( DATA.read, nil, "%>" )
92   tables = []
93   group_names = []
94   file["players"].keys.sort.each do |index|
95     if index < 999
96       group_names << "#{index}"
97     else
98       group_names << "Not-Yet-Rated Players"
99     end
100   end
101   remove_long_to_see_players(file)
102
103   popup_id = 0
104
105   if $wdoor
106     yss_rate = 0
107     file["players"].keys.each do |group_index|
108       file["players"][group_index].each do |player, yaml|
109         if player == "YSS+707d4f98d9d2620cdaab58f19d02a2e4"
110           yss_rate = yaml['rate']
111         end
112       end
113     end
114   end
115         
116   file["players"].sort.each do |key, yaml| # sort groups in the order written in players.yaml
117   sorted_keys = yaml.keys.sort do |a,b| 
118     # sort players in a group by one's rate
119     if yaml[b]['rate'] == 0 && yaml[a]['rate'] == 0
120       # mainly for not-rated-yet players
121       yaml[b]['last_modified'] <=> yaml[a]['last_modified']
122     else
123       yaml[b]['rate'] <=> yaml[a]['rate']
124     end
125   end 
126   top_rate = nil  
127   table = ERB.new(<<ENDTABLE, nil, "%>")
128 % sorted_keys.each do |key|
129   <%
130     win  = yaml[key]['win']
131     loss = yaml[key]['loss']
132     win_rate = win.to_f / (win + loss)
133     last_modified = yaml[key]['last_modified']
134     
135     player_decoration = "default"
136
137     case (Time.now - last_modified)/60 # minutes
138     when (0..30)        then player_decoration = "current"
139     when (0..(1*60*24)) then player_decoration = "today"
140     when (0..(7*60*24)) then player_decoration = "this_week"
141     end
142     
143     case key
144     when "yowai_gps+95908f6c18338f5340371f71523fc5e3" then player_decoration = "yowai_gps"
145     when "gps+11648e4e66e7ed6a86cb7f1d0cf604fe"       then player_decoration = "gps"
146     end
147
148     rate = yaml[key]['rate']
149     top_rate ||= rate
150     diff_rate = rate - top_rate
151     diff_possibility = 1.0/(1.0 + 10**(-diff_rate/400.0))
152   %>
153   <tr class="<%=player_decoration%>">
154     <td class="name">
155         <a id="popup<%=popup_id+=1%>" href="http://wdoor.c.u-tokyo.ac.jp/shogi/tools/view/show-player.cgi?event=LATEST&amp;filter=floodgate&amp;show_self_play=1&amp;user=<%=u key%>"><%= h yaml[key]['name'] %></a>
156         <script type="text/javascript">
157           var tooltip<%=popup_id%> = new YAHOO.widget.Tooltip("myTooltip", {
158             context:"popup<%=popup_id%>",
159             text:"<%= h key %>" } );
160         </script>
161     </td>
162     <td class="rate">
163         <span id="popup<%=popup_id+=1%>"><%= rate != 0 ? "%5d"  % [ rate ] : "N/A" %></span>
164         <script type="text/javascript">
165           var tooltip<%=popup_id%> = new YAHOO.widget.Tooltip("myTooltip", {
166             context:"popup<%=popup_id%>",
167             text:"Behind <%= "%5d" % [ diff_rate ] %> (<%= "%.3f" % [ diff_possibility ] %>)" } );
168         </script>
169     </td>
170     <td class="ngames">
171         <%= "%5d"  % [ win ] %>  </td>
172     <td class="ngames">
173         <%= "%5d"  % [ loss ] %> </td>
174     <td class="win_rate">
175         <%= "%.3f" % [win_rate] %> </td>
176     <td class="last_modified">
177         <%= show_date(last_modified) %> </td>
178 % if $wdoor
179     <td class="rate">
180         <%= rate != 0 && yss_rate > 0 ? ("%5d"  % [2300 - yss_rate + rate]) : "N/A" %> </td>
181 % end
182   </tr>
183 % end
184 ENDTABLE
185         
186     tables << table.result(binding)
187   end
188
189   body = erb.result(binding)
190   puts body
191 end
192
193 if __FILE__ == $0
194   main
195 end
196
197 # vim: ts=2 sw=2 sts=0
198
199 __END__
200 <html>
201 <head>
202   <title>Shogi Server Rating</title>
203   <link rel="StyleSheet" type="text/css" href="http://wdoor.c.u-tokyo.ac.jp/shogi/shogi.css">
204   <!-- CSS --> 
205   <!-- License: http://developer.yahoo.com/yui/license.html -->
206   <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.4.1/build/reset-fonts-grids/reset-fonts-grids.css"> 
207   <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.4.1/build/base/base-min.css"> 
208   <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.4.1/build/container/assets/container.css"> 
209   <!-- Dependencies --> 
210   <script type="text/javascript" src="http://yui.yahooapis.com/2.4.1/build/yahoo-dom-event/yahoo-dom-event.js"></script> 
211   <!-- OPTIONAL: Animation (only required if enabling Animation) --> 
212   <script type="text/javascript" src="http://yui.yahooapis.com/2.4.1/build/animation/animation-min.js"></script> 
213   <!-- Source file --> 
214   <script type="text/javascript" src="http://yui.yahooapis.com/2.4.1/build/container/container-min.js"></script>
215   <style type="text/css"><!--
216     TABLE {margin-left: auto;
217            margin-right: auto;}
218     CAPTION { caption-side: left;}
219     .name {text-align: center;
220            width: 180px;}
221     .rate, .ngames, .win_rate {text-align: right;}
222     .last_modified {text-align: center;}
223     .gps, .yowai_gps {background-color: lightgreen;}
224     .current   {background-color: #FFD700;}
225     .today     {background-color: #FFFF00;}
226     .this_week {background-color: #FFFFAA;}
227
228     #bd {text-align: center;}
229     #ft {text-align: right;}
230   --></style>
231 </head>
232 <body><div id="doc">
233
234 <div id="hd"></div>
235
236 <div id="bd">
237 <h1>Shogi Server Rating</h1>
238
239 % tables.each_with_index do |t, index|
240 <table>
241 <caption>Group: <%=group_names[index]%></caption>
242 <colgroup>
243   <col class="name">
244   <col class="rate">
245   <col class="ngames">
246   <col class="ngames">
247   <col class="win_rate">
248   <col class="last_modified">
249 % if $wdoor
250   <col class="rate)">
251 % end
252 </colgroup>
253 <thead>
254 <tr>
255   <th>name</th>
256   <th>rate</th> 
257   <th>win</th> 
258   <th>loss</th> 
259   <th>&#37;</th> 
260   <th>last_modified</th>
261 % if $wdoor
262   <th>(rate24)</th> 
263 % end
264 </tr>
265 </thead>
266 <tbody>
267   <%= t %>
268 </tbody>
269 </table>
270
271 <hr style="width:50%; margin:1em auto;">
272
273 % end
274
275 <p>Groups are independently rated. You can not compare rates across them.
276 <p>The average of the rates in a group is always 1000. 
277 <p><a href="http://wdoor.c.u-tokyo.ac.jp/shogi/">BACK</a>
278
279 <hr/>
280 </div>
281
282
283 <div id="ft">
284   <p>Last modified at <%=Time.now%>
285   <p>$Revision$
286 </div>
287
288 </div></body>
289 </html>
290