OSDN Git Service

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