OSDN Git Service

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