#!/usr/bin/ruby # -*- coding: euc-jp -*- # $Id$ # # Author:: Team GPS # #-- # Copyright (C) 2006-2009 Team GPS # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #++ # # $:.unshift(File.expand_path(File.dirname(__FILE__))) require 'cgi' require 'erb' require 'stringio' require 'digest/sha1' require 'download_manager' require 'eval_graph' require 'kifu2csa' include ERB::Util FILE_NAME=File.basename(__FILE__) def first_attack(csa) require 'shogi-server' board = ShogiServer::Board.new board.initial csa.each_line do |line| board.handle_one_move(line.strip, nil) break unless board.sente_hands.empty? && board.gote_hands.empty? end board.move_count - 1 end def first_checkmate(csa) require 'shogi-server' board = ShogiServer::Board.new board.initial csa.each_line do |line| board.handle_one_move(line.strip, nil) break if board.checkmated?(true) || board.checkmated?(false) end board.move_count end def parse_move(move, csa=nil) return nil unless move case move.strip when /\d+/ ", #{$&}" when /^first$/ ", 1" when /^last$/ ", 'last'" when /^first[_\-]attack$/ i = first_attack(csa) i > 0 ? ", #{i}" : nil when /^first[_\-]checkmate$/ i = first_checkmate(csa) i > 0 ? ", #{i}" : nil else nil end end @cgi = CGI.new() @cgi.header("type" => "text/html") @title = "CSA Viewer" @my_uri = "http://"+ENV["HTTP_HOST"]+ENV["REQUEST_URI"] ENV['PATH'] = "/usr/bin" unless @cgi["csa"].empty? url = @cgi["csa"] dm = DownloadManager.new(url) kifu = "" begin kifu = dm.kifu_download if dm.is_kifu? flag = (FILE_NAME == "kifu.cgi") kifu = Kifu2Csa.new.kifu_to_csa(kifu, flag) end rescue DownloadManager::ForbiddenError cgi = CGI.new("html4") cgi.header("type" => "text/html", "status" => "FORBIDDEN") cgi.out({"content-type" => "text/html"}) do cgi.html do cgi.head {cgi.title{"403 FORBIDDEN"}} + cgi.body do cgi.h1{"403 FORBIDDEN"} + cgi.p{"You don't have permission to access the page you request on this server."} end end end exit end go_last = @cgi["go_last"].empty? ? nil : "last" move = @cgi["move_to"].empty? ? nil : @cgi["move_to"] move ||= @cgi["move"].empty? ? nil : @cgi["move"] move ||= go_last # Generate an eval graph if ! dm.is_kifu? file = "cache/" + Digest::SHA1.hexdigest(@cgi["csa"]) original_file_name = nil if %r!/(.*?\.csa$)! =~ @cgi["csa"] original_file_name= $1 end file_svg = "#{file}.svg" read(StringIO.new(kifu).readlines, file, original_file_name) str = reformat_svg( open(file_svg){|f| f.read}) open(file_svg,"w+") {|f| f << str} end # try to parse filename if %r!\+([A-Za-z0-9_@.-]+)\+([A-Za-z0-9_@.-]+)\+([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})\.csa$! =~ url @title = $1+" vs. "+$2+" ("+$3+"-"+$4+"-"+$5+" "+$6+":"+$7+")" end end erb = ERB.new( DATA.read, nil, "%<>" ) body = erb.result(binding) @cgi.out({"content-type" => "text/html; charset=euc-jp"}){body} __END__
<%= h kifu %>
この対局にコメントする:
Tweet
評価値グラフ
棋譜再生用 Javascript 将棋盤を利用して棋譜を表示します。