OSDN Git Service

Implemented half-life effect. Numbers of win/lose decrease by this effect.
[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"].sort.each do |key, yaml| # sort groups in the order written in players.yaml
53     sorted_keys = yaml.keys.sort {|a,b| yaml[b]['rate'] <=> yaml[a]['rate']} # sort players in a group by one's 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     player_decoration = "default"
64
65     case (Time.now - last_modified)/(60*60*24)
66     when (0..1) then player_decoration = "today"
67     when (0..7) then player_decoration = "this_week"
68     end
69     
70     case key
71     when "yowai_gps+95908f6c18338f5340371f71523fc5e3" then player_decoration = "yowai_gps"
72     when "gps+11648e4e66e7ed6a86cb7f1d0cf604fe"       then player_decoration = "gps"
73     end
74   %>
75   <tr class="<%=player_decoration%>">
76     <td class="name">         <%= h yaml[key]['name'] %>            </td>
77     <td class="rate">         <%= "%5d"  % [ yaml[key]['rate'] ] %> </td>
78     <td class="ngames">       <%= "%5d"  % [ win ] %>               </td>
79     <td class="ngames">       <%= "%5d"  % [ loss ] %>              </td>
80     <td class="win_rate">     <%= "%.3f" % [win_rate] %>            </td>
81     <td class="last_modified"><%= show_date(last_modified) %>       </td>
82   </tr>
83 % end
84 ENDTABLE
85         
86     tables << table.result(binding)
87   end
88
89   body = erb.result(binding)
90   puts body
91 end
92
93 if __FILE__ == $0
94   main
95 end
96
97 # vim: ts=2 sw=2 sts=0
98
99 __END__
100 <html>
101 <head>
102   <title>Shogi Server Rating</title>
103   <link rel="StyleSheet" type="text/css" href="http://wdoor.c.u-tokyo.ac.jp/shogi/shogi.css">
104   <style type="text/css"><!--
105     BODY {margin: 10px 60px;
106           text-align: center;}
107     TABLE {margin-left: auto;
108            margin-right: auto;
109            border-collapse: collapse;
110            border: solid 2px;
111            width: 500px;}
112     CAPTION { caption-side: left;}
113     TH    {border: solid 1px;}
114     TD    {padding-left: 10px;
115            padding-right: 10px;
116            border: solid 1px;}
117     .name {text-align: center;
118            width: 180px;}
119     .rate, .ngames, .win_rate {text-align: right;}
120     .last_modified {text-align: left;}
121     .gps, .yowai_gps {background-color: lightgreen;}
122     .today     {background-color: #FFD700;}
123     .this_week {background-color: #FFDAB9;}
124
125     DIV.footer {text-align: right;
126                 font-size: 80%;}
127   --></style>
128 </head>
129 <body>
130
131 <h1>Shogi Server Rating</h1>
132
133 % tables.each_with_index do |t, index|
134 <table>
135 <caption>Group: <%=index%></caption>
136 <colgroup>
137   <col class="name">
138   <col class="rate">
139   <col class="ngames">
140   <col class="ngames">
141   <col class="win_rate">
142   <col class="last_modified">
143 </colgroup>
144 <thead>
145 <tr>
146   <th>name</th>
147   <th>rate</th> 
148   <th>win</th> 
149   <th>loss</th> 
150   <th>win_rate</th> 
151   <th>last_modified</th>
152 </tr>
153 </thead>
154 <tbody>
155   <%= t %>
156 </tbody>
157 </table>
158
159 <hr style="width:50%; margin:1em auto;">
160
161 % end
162
163 <p>Groups are independently rated. You can not compare rates across them.
164 <p>The average of the rates in a group is always 1000. 
165 <p><a href="http://wdoor.c.u-tokyo.ac.jp/shogi/">BACK</a>
166
167 <hr/>
168
169 <div class="footer">
170   <p>Last modified at <%=Time.now%>
171   <p>$Revision$
172 </div>
173
174 </body>
175 </html>
176