OSDN Git Service

Show players who have accessed in the last 30 minutes with
[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 # minutes
122     when (0..30)        then player_decoration = "current"
123     when (0..(1*60*24)) then player_decoration = "today"
124     when (0..(7*60*24)) then player_decoration = "this_week"
125     end
126     
127     case key
128     when "yowai_gps+95908f6c18338f5340371f71523fc5e3" then player_decoration = "yowai_gps"
129     when "gps+11648e4e66e7ed6a86cb7f1d0cf604fe"       then player_decoration = "gps"
130     end
131
132     rate = yaml[key]['rate']
133     top_rate ||= rate
134     diff_rate = rate - top_rate
135     diff_possibility = 1.0/(1.0 + 10**(-diff_rate/400.0))
136   %>
137   <tr class="<%=player_decoration%>">
138     <td class="name">
139         <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>
140         <script type="text/javascript">
141           var tooltip<%=popup_id%> = new YAHOO.widget.Tooltip("myTooltip", {
142             context:"popup<%=popup_id%>",
143             text:"<%= h key %>" } );
144         </script>
145     </td>
146     <td class="rate">
147         <span id="popup<%=popup_id+=1%>"><%= rate != 0 ? "%5d"  % [ rate ] : "N/A" %></span>
148         <script type="text/javascript">
149           var tooltip<%=popup_id%> = new YAHOO.widget.Tooltip("myTooltip", {
150             context:"popup<%=popup_id%>",
151             text:"Behind <%= "%5d" % [ diff_rate ] %> (<%= "%.3f" % [ diff_possibility ] %>)" } );
152         </script>
153     </td>
154     <td class="ngames">
155         <%= "%5d"  % [ win ] %>  </td>
156     <td class="ngames">
157         <%= "%5d"  % [ loss ] %> </td>
158     <td class="win_rate">
159         <%= "%.3f" % [win_rate] %> </td>
160     <td class="last_modified">
161         <%= show_date(last_modified) %> </td>
162 % if $wdoor
163     <td class="rate">
164         <%= rate != 0 && yss_rate > 0 ? ("%5d"  % [2300 - yss_rate + rate]) : "N/A" %> </td>
165 % end
166   </tr>
167 % end
168 ENDTABLE
169         
170     tables << table.result(binding)
171   end
172
173   body = erb.result(binding)
174   puts body
175 end
176
177 if __FILE__ == $0
178   main
179 end
180
181 # vim: ts=2 sw=2 sts=0
182
183 __END__
184 <html>
185 <head>
186   <title>Shogi Server Rating</title>
187   <link rel="StyleSheet" type="text/css" href="http://wdoor.c.u-tokyo.ac.jp/shogi/shogi.css">
188   <!-- CSS --> 
189   <!-- License: http://developer.yahoo.com/yui/license.html -->
190   <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.4.1/build/reset-fonts-grids/reset-fonts-grids.css"> 
191   <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.4.1/build/base/base-min.css"> 
192   <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.4.1/build/container/assets/container.css"> 
193   <!-- Dependencies --> 
194   <script type="text/javascript" src="http://yui.yahooapis.com/2.4.1/build/yahoo-dom-event/yahoo-dom-event.js"></script> 
195   <!-- OPTIONAL: Animation (only required if enabling Animation) --> 
196   <script type="text/javascript" src="http://yui.yahooapis.com/2.4.1/build/animation/animation-min.js"></script> 
197   <!-- Source file --> 
198   <script type="text/javascript" src="http://yui.yahooapis.com/2.4.1/build/container/container-min.js"></script>
199   <style type="text/css"><!--
200     TABLE {margin-left: auto;
201            margin-right: auto;}
202     CAPTION { caption-side: left;}
203     .name {text-align: center;
204            width: 180px;}
205     .rate, .ngames, .win_rate {text-align: right;}
206     .last_modified {text-align: center;}
207     .gps, .yowai_gps {background-color: lightgreen;}
208     .current   {background-color: #FFFF00;}
209     .today     {background-color: #FFFF77;}
210     .this_week {background-color: #FFFFAA;}
211
212     #bd {text-align: center;}
213     #ft {text-align: right;}
214   --></style>
215 </head>
216 <body><div id="doc">
217
218 <div id="hd"></div>
219
220 <div id="bd">
221 <h1>Shogi Server Rating</h1>
222
223 % tables.each_with_index do |t, index|
224 <table>
225 <caption>Group: <%=group_names[index]%></caption>
226 <colgroup>
227   <col class="name">
228   <col class="rate">
229   <col class="ngames">
230   <col class="ngames">
231   <col class="win_rate">
232   <col class="last_modified">
233 % if $wdoor
234   <col class="rate)">
235 % end
236 </colgroup>
237 <thead>
238 <tr>
239   <th>name</th>
240   <th>rate</th> 
241   <th>win</th> 
242   <th>loss</th> 
243   <th>&#37;</th> 
244   <th>last_modified</th>
245 % if $wdoor
246   <th>(rate24)</th> 
247 % end
248 </tr>
249 </thead>
250 <tbody>
251   <%= t %>
252 </tbody>
253 </table>
254
255 <hr style="width:50%; margin:1em auto;">
256
257 % end
258
259 <p>Groups are independently rated. You can not compare rates across them.
260 <p>The average of the rates in a group is always 1000. 
261 <p><a href="http://wdoor.c.u-tokyo.ac.jp/shogi/">BACK</a>
262
263 <hr/>
264 </div>
265
266
267 <div id="ft">
268   <p>Last modified at <%=Time.now%>
269   <p>$Revision$
270 </div>
271
272 </div></body>
273 </html>
274