OSDN Git Service

9203613d6774c3c22ae57f46167642d8718e2c57
[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     TH    {border: solid 1px;}
100     TD    {padding-left: 10px;
101            padding-right: 10px;
102            border: solid 1px;}
103     .name {text-align: center;}
104     .rate, .ngames, .win_rate {text-align: right;}
105     .last_modified {text-align: left;}
106
107     P.footer {text-align: right;
108              font-size: 80%;}
109      
110   --></style>
111 </head>
112 <body>
113
114 <h1>Shogi Server Rating</h1>
115
116 <% tables.each do |t| %>
117 <table>
118 <colgroup>
119   <col class="name">
120   <col class="rate">
121   <col class="ngames">
122   <col class="ngames">
123   <col class="win_rate">
124   <col class="last_modified">
125 </colgroup>
126 <thead>
127 <tr>
128   <th>name</th> <th>rate</th> <th>win</th> <th>loss</th> <th>win_rate</th> <th>last_modified</th>
129 </tr>
130 </thead>
131 <tbody>
132 <%= t %>
133 </tbody>
134 </table>
135 <% end %>
136
137 <p>The average of the rates is always 1000. 
138
139 <hr/>
140 <p class="footer">Last modified at <%=Time.now%>
141
142 </body>
143 </html>
144