OSDN Git Service

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