前の記事:shogi-server にどんな機能があるか見てみようぜ(^~^)?
「 UECとか GPS将棋とか Ruby 使ってるけど、
コンピューター将棋開発者が持ってるスキルで多いの Python なんで、 shogi-server を Python に書きかえるぜ」
📖 FastAPI - 代替ツールから受けたインスピレーションと比較¶
「 ディープラーニング勢と相性がいいんだぜ、Python。
移行先のフレームワークは Flask か、 FastAPI か、雰囲気みて そのうち決めようぜ」
Start a thrift server
$ ./graphicserver --port 9090 -d ./public/images
Start a Webrick server
$ ./start.rb
Browser
http://localhost:7000/game/hoge+testJishogiTest-1500-0+senteJishogiTest+goteJishogiTest+20081125125336.csa
「 👆 まず thrift サーバーというのが何か分からないぜ」
「 thrift が何をしてくれるのか分からん。トランスパイラ?
graphicserver
というシェルも見当たらないし実行できないぜ」
「 start.rb
は ramaze というフレームワークだが、これも起動の仕方が分からん」
「 この Webフレームワーク、使ってるかどうかも分かんないんだけどな。
将棋サーバーに入ってるのだから 調査するしかないよな」
http://localhost:7000/game/hoge+testJishogiTest-1500-0+senteJishogiTest+goteJishogiTest+20081125125336.csa
~~<del>
</del>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
「 ページの名前とメソッド名が、サブ・ディレクトリ名と第一引数が対応すると思うんだぜ」
# the index action is called automatically when no other action is specified
def index
@title = "Welcome to Ramaze!"
end
# the string returned at the end of the function is used as the html body
# if there is no template for the action. if there is a template, the string
# is silently ignored
def notemplate
"there is no 'notemplate.xhtml' associated with this action"
end
$pos2img_out_dir = File.join(".", "public", "images")
# ~中略~
def images(str)
file = File.join($pos2img_out_dir, str)
send_file(file, mime_type = Ramaze::Tool::MIME.type_for(file))
end
「 👆 images は多分 showgame/public/images
フォルダーの中の静的な画像を1枚表示するだけだとおもうんだぜ、images
フォルダー無いけど」
「 ぱっと見た感じ、Webフレームワークを使い始めて2,3日目、ぐらいのスキルレベルのコードだと思うのよ」
def sfen(str)
transport = Thrift::BufferedTransport.new(Thrift::Socket.new('localhost', 9090))
client = ShogiGraphic::Client.new(Thrift::BinaryProtocol.new(transport))
transport.open
result = client.usi2png(str)
transport.close
Ramaze::Log.error("Failed to get an image of %s from the Thrift server" % [str]) if !result || result.empty?
@img = "/images/%s" % [result]
end
「 👆 これも一目、 URLにSFENを入れたら画像が生成されるとか、
生成されたあとのファイルパスが @img
という変数か何かに格納されるぐらいだよな」
def game(csa_file)
csa_file.gsub!(" ", "+")
dir = "/home/daigo/rubyprojects/shogi-server"
files = Dir.glob(File.join(dir, "**", csa_file))
if files.empty?
redirect Rs()
end
board = ShogiServer::Board.new
board.initial
@moves = Array.new
teban = true
usi = ShogiServer::Usi.new
kifu = File.open(files.first) {|f| f.read}
kifu.each_line do |line|
# Ramaze::Log.warn(line)
if /^([\+\-])(\d)(\d)(\d)(\d)([A-Z]{2})/ =~ line
board.handle_one_move(line)
sfen = usi.board2usi(board, teban)
sfen = ShogiServer::Usi.escape(sfen)
@moves << A(h(line), :href => Rs(:sfen, u(sfen)))
teban = teban ? false : true
end
end
end
「 👆 URLでは空白が +
になってるから に戻して、
/home/daigo/rubyprojects/shogi-server
ディレクトリーの中にある
CSAの対局ログファイル、これは URL でファイル名が指定されるんだと思うんだが見つけてきて、
盤オブジェクトと、指し手の配列を生成して、
とりあえず見つかったファイルの1つ目を開いて各行読んで、
このとき指し手は先手が +
で始まる行、後手が -
で始まる行なんでパースして引っ掛けて、
単に指し手を配列に入れて、手番をひっくり返しているだけだよな」
「 思うんだけど、このWebアプリケーション要らなくない?」
「 REST API
で十分だよな。
棋譜ファイル名を入れたら 棋譜ファイルが返ってくる」
「 それ ハイパー テキスト トランスファー プロトコル。
タンニ シーエスエー ファイル オイトケダゼ」
「 usi2png
という機能 そもそも要らなくないか。将棋対局サーバーに。
ビューは 分けるという観点から」
Crieitは誰でも投稿できるサービスです。 是非記事の投稿をお願いします。どんな軽い内容でも投稿できます。
また、「こんな記事が読みたいけど見つからない!」という方は是非記事投稿リクエストボードへ!
こじんまりと作業ログやメモ、進捗を書き残しておきたい方はボード機能をご利用ください。
ボードとは?
コメント