OSDN Git Service

Display not-yet-rated players. Interium commit.
[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   file["players"].sort.each do |key, yaml| # sort groups in the order written in players.yaml
61   sorted_keys = yaml.keys.sort {|a,b| yaml[b]['rate'] <=> yaml[a]['rate']} # sort players in a group by one's rate
62   top_rate = nil  
63   table = ERB.new(<<ENDTABLE, nil, "%>")
64 % sorted_keys.each do |key|
65   <%
66     win  = yaml[key]['win']
67     loss = yaml[key]['loss']
68     win_rate = win.to_f / (win + loss)
69     last_modified = yaml[key]['last_modified']
70     
71     player_decoration = "default"
72
73     case (Time.now - last_modified)/(60*60*24)
74     when (0..1) then player_decoration = "today"
75     when (0..7) then player_decoration = "this_week"
76     end
77     
78     case key
79     when "yowai_gps+95908f6c18338f5340371f71523fc5e3" then player_decoration = "yowai_gps"
80     when "gps+11648e4e66e7ed6a86cb7f1d0cf604fe"       then player_decoration = "gps"
81     end
82
83     rate = yaml[key]['rate']
84     top_rate ||= rate
85     diff_rate = rate - top_rate
86     diff_possibility = 1.0/(1.0 + 10**(-diff_rate/400.0))
87   %>
88   <tr class="<%=player_decoration%>">
89     <td class="name">
90         <span class="popup"><%= key %></span>
91         <%= h yaml[key]['name'] %> </td>
92     <td class="rate">
93         <span class="popup"><%= "%5d" % [ diff_rate ] %> | <%= "%.3f" % [ diff_possibility ] %></span>
94         <%= rate != 0 ? "%5d"  % [ rate ] : "N/A" %> </td>
95     <td class="ngames">
96         <%= "%5d"  % [ win ] %>  </td>
97     <td class="ngames">
98         <%= "%5d"  % [ loss ] %> </td>
99     <td class="win_rate">
100         <%= "%.3f" % [win_rate] %> </td>
101     <td class="last_modified">
102         <%= show_date(last_modified) %> </td>
103   </tr>
104 % end
105 ENDTABLE
106         
107     tables << table.result(binding)
108   end
109
110   body = erb.result(binding)
111   puts body
112 end
113
114 if __FILE__ == $0
115   main
116 end
117
118 # vim: ts=2 sw=2 sts=0
119
120 __END__
121 <html>
122 <head>
123   <title>Shogi Server Rating</title>
124   <link rel="StyleSheet" type="text/css" href="http://wdoor.c.u-tokyo.ac.jp/shogi/shogi.css">
125   <style type="text/css"><!--
126     BODY {margin: 10px 60px;
127           text-align: center;}
128     TABLE {margin-left: auto;
129            margin-right: auto;
130            border-collapse: collapse;
131            border: solid 2px;
132            width: 500px;}
133     CAPTION { caption-side: left;}
134     TH    {border: solid 1px;}
135     TD    {padding-left: 10px;
136            padding-right: 10px;
137            border: solid 1px;}
138     .name {text-align: center;
139            width: 180px;}
140     .rate, .ngames, .win_rate {text-align: right;}
141     .last_modified {text-align: left;}
142     .gps, .yowai_gps {background-color: lightgreen;}
143     .today     {background-color: #FFD700;}
144     .this_week {background-color: #FFDAB9;}
145
146     DIV.footer {text-align: right;
147                 font-size: 80%;}
148     
149     TD:hover .popup {
150       position: relative;   
151       display: inline;
152       background-color: #ffffaa;
153     }
154
155     .popup { display: none;
156              position: absolute;
157              top: 1em;
158              left: 7em;
159              white-space: nowrap;
160              border: 1px solid black;
161              padding:5px;
162              color:black;
163              text-decoration:none;
164            }
165   --></style>
166 </head>
167 <body>
168
169 <h1>Shogi Server Rating</h1>
170
171 % tables.each_with_index do |t, index|
172 <table>
173 <caption>Group: <%=group_names[index]%></caption>
174 <colgroup>
175   <col class="name">
176   <col class="rate">
177   <col class="ngames">
178   <col class="ngames">
179   <col class="win_rate">
180   <col class="last_modified">
181 </colgroup>
182 <thead>
183 <tr>
184   <th>name</th>
185   <th>rate</th> 
186   <th>win</th> 
187   <th>loss</th> 
188   <th>&#37;</th> 
189   <th>last_modified</th>
190 </tr>
191 </thead>
192 <tbody>
193   <%= t %>
194 </tbody>
195 </table>
196
197 <hr style="width:50%; margin:1em auto;">
198
199 % end
200
201 <p>Groups are independently rated. You can not compare rates across them.
202 <p>The average of the rates in a group is always 1000. 
203 <p><a href="http://wdoor.c.u-tokyo.ac.jp/shogi/">BACK</a>
204
205 <hr/>
206
207 <div class="footer">
208   <p>Last modified at <%=Time.now%>
209   <p>$Revision$
210 </div>
211
212 </body>
213 </html>
214