2021-05-21に更新

shogi-serverのwebアプリケーション部分をPythonに書き直そうぜ(^~^)

前の記事:shogi-server にどんな機能があるか見てみようぜ(^~^)?

shogi-serverのwebアプリケーション部分をPythonに書き直そうぜ(^~^)

20210124shogi2a2b1.png
「 UECとか GPS将棋とか Ruby 使ってるけど、
コンピューター将棋開発者が持ってるスキルで多いの Python なんで、 shogi-server を Python に書きかえるぜ」

kifuwarabe-futsu.png
「 そうか」

ohkina-hiyoko-futsu.png
「 Goじゃなくていいの?」

📖 FastAPI - 代替ツールから受けたインスピレーションと比較¶

ramen-tabero-futsu2.png
「 ディープラーニング勢と相性がいいんだぜ、Python。
移行先のフレームワークは Flask か、 FastAPI か、雰囲気みて そのうち決めようぜ」

README

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

ramen-tabero-futsu2.png
「 👆 まず thrift サーバーというのが何か分からないぜ」

📖 Apache Thrift

kifuwarabe-futsu.png
「 👆 Apacheのプロジェクトなんじゃないか?」

ramen-tabero-futsu2.png
「 thrift が何をしてくれるのか分からん。トランスパイラ?
graphicserver というシェルも見当たらないし実行できないぜ」

ramen-tabero-futsu2.png
「 start.rb は ramaze というフレームワークだが、これも起動の仕方が分からん」

ohkina-hiyoko-futsu.png
「 ソースを見て行きましょう」

showgame

ramen-tabero-futsu2.png
「 この Webフレームワーク、使ってるかどうかも分かんないんだけどな。
将棋サーバーに入ってるのだから 調査するしかないよな」

controller

main.rb

ramen-tabero-futsu2.png
「 このファイルを見ると、Webページが5つあるぜ」

  • index
  • game(csa_file)
  • sfen(str)
  • images(str)
  • notemplate
  http://localhost:7000/game/hoge+testJishogiTest-1500-0+senteJishogiTest+goteJishogiTest+20081125125336.csa
                        ~~<del>
                             </del>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ramen-tabero-futsu2.png
「 ページの名前とメソッド名が、サブ・ディレクトリ名と第一引数が対応すると思うんだぜ」

  # the index action is called automatically when no other action is specified
  def index
    @title = "Welcome to Ramaze!"
  end

ramen-tabero-futsu2.png
「 👆 index はどうでもいいだろ」

  # 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

ramen-tabero-futsu2.png
「 👆 notemplate もどうでもいいだろ」

$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

ramen-tabero-futsu2.png
「 👆 images は多分 showgame/public/images フォルダーの中の静的な画像を1枚表示するだけだとおもうんだぜ、images フォルダー無いけど」

ohkina-hiyoko-futsu.png
「 ぱっと見た感じ、Webフレームワークを使い始めて2,3日目、ぐらいのスキルレベルのコードだと思うのよ」

kifuwarabe-futsu.png
「 たいした機能無いんじゃないか?」

  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

ramen-tabero-futsu2.png
「 👆 これも一目、 URLにSFENを入れたら画像が生成されるとか、
生成されたあとのファイルパスが @img という変数か何かに格納されるぐらいだよな」

ohkina-hiyoko-futsu.png
「 やりたいことは分かる」

  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

ramen-tabero-futsu2.png
「 👆 URLでは空白が + になってるから に戻して、 /home/daigo/rubyprojects/shogi-server ディレクトリーの中にある
CSAの対局ログファイル、これは URL でファイル名が指定されるんだと思うんだが見つけてきて、
盤オブジェクトと、指し手の配列を生成して、
とりあえず見つかったファイルの1つ目を開いて各行読んで、
このとき指し手は先手が + で始まる行、後手が - で始まる行なんでパースして引っ掛けて、
単に指し手を配列に入れて、手番をひっくり返しているだけだよな」

kifuwarabe-futsu.png
「 ゆっくりしゃべってくれだぜ」

ohkina-hiyoko-futsu.png
「 思うんだけど、このWebアプリケーション要らなくない?」

ramen-tabero-futsu2.png
「 REST API で十分だよな。
棋譜ファイル名を入れたら 棋譜ファイルが返ってくる」

kifuwarabe-futsu.png
「 それ ハイパー テキスト トランスファー プロトコル。
タンニ シーエスエー ファイル オイトケダゼ」

ramen-tabero-futsu2.png
「 usi2png という機能 そもそも要らなくないか。将棋対局サーバーに。
ビューは 分けるという観点から」

ohkina-hiyoko-futsu.png
「 サーバーサイドで処理した方が速いということなんじゃないの?」

ramen-tabero-futsu2.png
「 どうだか」

ツイッターでシェア
みんなに共有、忘れないようにメモ

むずでょ

光速のアカウント凍結されちゃったんで……。ゲームプログラムを独習中なんだぜ☆電王戦IIに出た棋士もコンピューターもみんな好きだぜ☆▲(パソコン将棋)WCSC29一次予選36位、SDT5予選42位▲(パソコン囲碁)AI竜星戦予選16位

Crieitは誰でも投稿できるサービスです。 是非記事の投稿をお願いします。どんな軽い内容でも投稿できます。

また、「こんな記事が読みたいけど見つからない!」という方は是非記事投稿リクエストボードへ!

有料記事を販売できるようになりました!

こじんまりと作業ログやメモ、進捗を書き残しておきたい方はボード機能をご利用ください。
ボードとは?

コメント