OSDN Git Service

Refactoring. Pretty output.
[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
52   file["players"].each do |key, yaml|
53     sorted_keys = yaml.keys.sort {|a,b| yaml[b]['rate'] <=> yaml[a]['rate']}
54
55   table = ERB.new(<<ENDTABLE, nil, "%>")
56 % sorted_keys.each do |key|
57   <%
58     win  = yaml[key]['win']
59     loss = yaml[key]['loss']
60     win_rate = win.to_f / (win + loss)
61     last_modified = yaml[key]['last_modified']
62   %>
63   <tr>
64     <td class="name">         <%= h yaml[key]['name'] %>            </td>
65     <td class="rate">         <%= "%5d"  % [ yaml[key]['rate'] ] %> </td>
66     <td class="ngames">       <%= "%5d"  % [ win ] %>               </td>
67     <td class="ngames">       <%= "%5d"  % [ loss ] %>              </td>
68     <td class="win_rate">     <%= "%.3f" % [win_rate] %>            </td>
69     <td class="last_modified"><%= show_date(last_modified) %>       </td>
70   </tr>
71 % end
72 ENDTABLE
73         
74     tables << table.result(binding)
75   end
76
77   body = erb.result(binding)
78   puts body
79 end
80
81 if __FILE__ == $0
82   main
83 end
84
85 # vim: ts=2 sw=2 sts=0
86
87 __END__
88 <html>
89 <head>
90   <title>Shogi Server Rating</title>
91   <link rel="StyleSheet" type="text/css" href="http://wdoor.c.u-tokyo.ac.jp/shogi/shogi.css">
92   <style type="text/css"><!--
93     BODY {margin: 10px 60px;
94           text-align: center;}
95     TABLE {margin-left: auto;
96            margin-right: auto;
97            border-collapse: collapse;
98            border: solid 2px;}
99     CAPTION { caption-side: left;}
100     TH    {border: solid 1px;}
101     TD    {padding-left: 10px;
102            padding-right: 10px;
103            border: solid 1px;}
104     .name {text-align: center;}
105     .rate, .ngames, .win_rate {text-align: right;}
106     .last_modified {text-align: left;}
107
108     DIV.footer {text-align: right;
109                 font-size: 80%;}
110   --></style>
111 </head>
112 <body>
113
114 <h1>Shogi Server Rating</h1>
115
116 % tables.each_with_index do |t, index|
117 <table>
118 <caption>Group: <%=index%></caption>
119 <colgroup>
120   <col class="name">
121   <col class="rate">
122   <col class="ngames">
123   <col class="ngames">
124   <col class="win_rate">
125   <col class="last_modified">
126 </colgroup>
127 <thead>
128 <tr>
129   <th>name</th>
130   <th>rate</th> 
131   <th>win</th> 
132   <th>loss</th> 
133   <th>win_rate</th> 
134   <th>last_modified</th>
135 </tr>
136 </thead>
137 <tbody>
138   <%= t %>
139 </tbody>
140 </table>
141
142 <hr style="width:50%; margin:1em auto;">
143
144 % end
145
146 <p>Groups are independently rated. You can not compare rates across them.
147 <p>The average of the rates in a group is always 1000. 
148 <p><a href="http://wdoor.c.u-tokyo.ac.jp/shogi/">BACK</a>
149
150 <hr/>
151
152 <div class="footer">
153   <p>Last modified at <%=Time.now%>
154   <p>$Revision$
155 </div>
156
157 </body>
158 </html>
159