ファイル情報

Rev. 2d5cda5e9dde331e37d58133b688a8b2c6295345
サイズ 8,534 バイト
日時 2017-09-03 14:40:39
作者 Daigo Moriwaki
ログメッセージ

Merge remote-tracking branch 'origin/master' into wdoor-stable

内容

  1. #!/usr/bin/ruby
  2. # $Id$
  3. #
  4. # Author:: Daigo Moriwaki
  5. # Homepage:: http://sourceforge.jp/projects/shogi-server/
  6. #
  7. #--
  8. # Copyright (C) 2006-2012 Daigo Moriwaki <daigo at debian dot org>
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; either version 2 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License
  21. # along with this program; if not, write to the Free Software
  22. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. #++
  24. #
  25. # == Synopsis
  26. #
  27. # mk_html generates an html page from a players.yaml.
  28. #
  29. # == Usage
  30. #
  31. # ./mk_html [OPTION] < players.yaml > rating.html
  32. #
  33. # [<tt>-h, --help</tt>]
  34. # show help
  35. #
  36. # [<tt>-w,--wdoor</tt>]
  37. # adpot wdoor style
  38. #
  39. # [<tt>--footer</tt> <i>filename</i>]
  40. # insert contents in the filename at the bottom of a genrated page.
  41. #
  42. # == Prerequire
  43. #
  44. # * Ruby 2.0.0 or later
  45. #
  46. # $ sudo aptitude install ruby ruby
  47. #
  48. # == Example
  49. #
  50. # $ ./mk_html --footer ./wdoor_1000.html < players.yaml > players.html
  51. #
  52. # $ cat ./wdoor-1000.html
  53. # <p>The average of the rates in a group is always 1000.
  54. # <p><a href="http://wdoor.c.u-tokyo.ac.jp/shogi/">BACK</a>
  55. #
  56. require 'optparse'
  57. require 'yaml'
  58. require 'erb'
  59. include ERB::Util
  60. def show_date(time)
  61. if (Time.now - time < 30*60)
  62. return "on line"
  63. elsif (Time.now - time < 24*60*60)
  64. return time.strftime("%Y-%m-%d")
  65. # return "%d hours ago" % [(Time.now - time)/3600]
  66. else
  67. return time.strftime("%Y-%m-%d")
  68. end
  69. end
  70. def remove_long_to_see_players(file)
  71. return unless file["players"][999] # only for Not-Yet-Rated players
  72. file["players"][999].delete_if do |key, value|
  73. value['last_modified'] < Time.now - 24*3600*30 # 30 days
  74. end
  75. end
  76. def main
  77. $wdoor = false
  78. $footer_content = nil
  79. opts = OptionParser.new
  80. opts.banner = "Usage: mk_html [OPTIONS]"
  81. opts.on("--footer filename", String, "Insert contents of the filename at the bottom of a genrated page") do |filename|
  82. unless File.exist?(filename)
  83. $stderr.puts "File not found: %s" % [filename]
  84. raise
  85. end
  86. $footer_content = File.open(filename).read
  87. end
  88. opts.on("-w","--wdoor", "adopt a wdoor style") { $wdoor=true }
  89. opts.on_tail("-h", "--help", "show this message") do
  90. puts opts
  91. exit
  92. end
  93. begin
  94. opts.parse(ARGV)
  95. rescue
  96. puts opts
  97. exit -1
  98. end
  99. lines = ""
  100. while l = $stdin.gets do
  101. lines << l
  102. end
  103. file = YAML::load(lines)
  104. erb = ERB.new( DATA.read, nil, "%>" )
  105. tables = []
  106. group_names = []
  107. file["players"].keys.sort.each do |index|
  108. if index < 999
  109. group_names << "#{index}"
  110. else
  111. group_names << "Not-Yet-Rated Players"
  112. end
  113. end
  114. remove_long_to_see_players(file)
  115. popup_id = 0
  116. if $wdoor
  117. yss_rate = 0
  118. file["players"].keys.each do |group_index|
  119. file["players"][group_index].each do |player, yaml|
  120. if player == "YSS+707d4f98d9d2620cdaab58f19d02a2e4"
  121. yss_rate = yaml['rate']
  122. end
  123. end
  124. end
  125. end
  126. file["players"].sort.each do |key, yaml| # sort groups in the order written in players.yaml
  127. sorted_keys = yaml.keys.sort do |a,b|
  128. # sort players in a group by one's rate
  129. if yaml[b]['rate'] == 0 && yaml[a]['rate'] == 0
  130. # mainly for not-rated-yet players
  131. yaml[b]['last_modified'] <=> yaml[a]['last_modified']
  132. else
  133. yaml[b]['rate'] <=> yaml[a]['rate']
  134. end
  135. end
  136. top_rate = nil
  137. table = ERB.new(<<ENDTABLE, nil, "%>")
  138. % sorted_keys.each do |key|
  139. <%
  140. win = yaml[key]['win']
  141. loss = yaml[key]['loss']
  142. win_rate = win.to_f / (win + loss)
  143. last_modified = yaml[key]['last_modified']
  144. player_decoration = "default"
  145. case (Time.now - last_modified)/60 # minutes
  146. when (0..30) then player_decoration = "current"
  147. when (0..(1*60*24)) then player_decoration = "today"
  148. when (0..(7*60*24)) then player_decoration = "this_week"
  149. end
  150. case key
  151. when "yowai_gps+95908f6c18338f5340371f71523fc5e3" then player_decoration = "yowai_gps"
  152. when "gps+11648e4e66e7ed6a86cb7f1d0cf604fe" then player_decoration = "gps"
  153. end
  154. rate = yaml[key]['rate']
  155. top_rate ||= rate
  156. diff_rate = rate - top_rate
  157. diff_possibility = 1.0/(1.0 + 10**(-diff_rate/400.0))
  158. %>
  159. <tr class="<%=player_decoration%>">
  160. <td class="name">
  161. <a id="popup<%=popup_id+=1%>" href="/shogi/view/show-player.cgi?event=LATEST&amp;filter=floodgate&amp;show_self_play=1&amp;user=<%=u key%>"><%= h yaml[key]['name'] %></a>
  162. <script type="text/javascript">
  163. var tooltip<%=popup_id%> = new YAHOO.widget.Tooltip("myTooltip", {
  164. context:"popup<%=popup_id%>",
  165. text:"<%= h key %>" } );
  166. </script>
  167. </td>
  168. <td class="rate">
  169. <span id="popup<%=popup_id+=1%>"><%= rate != 0 ? "%5d" % [ rate ] : "N/A" %></span>
  170. <script type="text/javascript">
  171. var tooltip<%=popup_id%> = new YAHOO.widget.Tooltip("myTooltip", {
  172. context:"popup<%=popup_id%>",
  173. text:"Behind <%= "%5d" % [ diff_rate ] %> (<%= "%.3f" % [ diff_possibility ] %>)" } );
  174. </script>
  175. </td>
  176. <td class="ngames">
  177. <%= "%5d" % [ win ] %> </td>
  178. <td class="ngames">
  179. <%= "%5d" % [ loss ] %> </td>
  180. <td class="win_rate">
  181. <%= "%.3f" % [win_rate] %> </td>
  182. <td class="last_modified">
  183. <%= show_date(last_modified) %> </td>
  184. % if $wdoor
  185. <td class="rate">
  186. <%= rate != 0 && yss_rate > 0 ? ("%5d" % [2300 - yss_rate + rate]) : "N/A" %> </td>
  187. % end
  188. </tr>
  189. % end
  190. ENDTABLE
  191. tables << table.result(binding)
  192. end
  193. body = erb.result(binding)
  194. puts body
  195. end
  196. if __FILE__ == $0
  197. main
  198. end
  199. # vim: ts=2 sw=2 sts=0
  200. __END__
  201. <html>
  202. <head>
  203. <title>Shogi Server Rating</title>
  204. <link rel="StyleSheet" type="text/css" href="http://wdoor.c.u-tokyo.ac.jp/shogi/shogi.css">
  205. <!-- CSS -->
  206. <!-- License: http://developer.yahoo.com/yui/license.html -->
  207. <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.4.1/build/reset-fonts-grids/reset-fonts-grids.css">
  208. <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.4.1/build/base/base-min.css">
  209. <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.4.1/build/container/assets/container.css">
  210. <!-- Dependencies -->
  211. <script type="text/javascript" src="http://yui.yahooapis.com/2.4.1/build/yahoo-dom-event/yahoo-dom-event.js"></script>
  212. <!-- OPTIONAL: Animation (only required if enabling Animation) -->
  213. <script type="text/javascript" src="http://yui.yahooapis.com/2.4.1/build/animation/animation-min.js"></script>
  214. <!-- Source file -->
  215. <script type="text/javascript" src="http://yui.yahooapis.com/2.4.1/build/container/container-min.js"></script>
  216. <style type="text/css"><!--
  217. TABLE {margin-left: auto;
  218. margin-right: auto;}
  219. CAPTION { caption-side: left;}
  220. .name {text-align: center;
  221. width: 180px;}
  222. .rate, .ngames, .win_rate {text-align: right;}
  223. .last_modified {text-align: center;}
  224. .gps, .yowai_gps {background-color: lightgreen;}
  225. .current {background-color: #FFD700;}
  226. .today {background-color: #FFFF00;}
  227. .this_week {background-color: #FFFFAA;}
  228. #bd {text-align: center;}
  229. #ft {text-align: right;}
  230. --></style>
  231. </head>
  232. <body><div id="doc">
  233. <div id="hd"></div>
  234. <div id="bd">
  235. <h1>Shogi Server Rating</h1>
  236. % tables.each_with_index do |t, index|
  237. <table>
  238. <caption>Group: <%=group_names[index]%></caption>
  239. <colgroup>
  240. <col class="name">
  241. <col class="rate">
  242. <col class="ngames">
  243. <col class="ngames">
  244. <col class="win_rate">
  245. <col class="last_modified">
  246. % if $wdoor
  247. <col class="rate)">
  248. % end
  249. </colgroup>
  250. <thead>
  251. <tr>
  252. <th>name</th>
  253. <th>rate</th>
  254. <th>win</th>
  255. <th>loss</th>
  256. <th>&#37;</th>
  257. <th>last_modified</th>
  258. % if $wdoor
  259. <th>(rate24)</th>
  260. % end
  261. </tr>
  262. </thead>
  263. <tbody>
  264. <%= t %>
  265. </tbody>
  266. </table>
  267. <hr style="width:50%; margin:1em auto;">
  268. % end
  269. <p>Groups are independently rated. You can not compare rates across them.
  270. <%= $footer_content %>
  271. <hr/>
  272. </div>
  273. <div id="ft">
  274. <p>Last modified at <%=Time.now%>
  275. <p>$Revision$
  276. </div>
  277. </div></body>
  278. </html>
旧リポジトリブラウザで表示