OSDN Git Service

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