From c567696860aae9c6f0e0ee752d655198112c786a Mon Sep 17 00:00:00 2001 From: nissymori Date: Wed, 25 Aug 2021 19:17:57 +0900 Subject: [PATCH 1/3] fix class dependancy --- lib/mjx_mjai_translater/player.rb | 4 ++-- lib/mjx_mjai_translater/random_agent.rb | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/mjx_mjai_translater/player.rb b/lib/mjx_mjai_translater/player.rb index fbfd0e9..90bd03d 100644 --- a/lib/mjx_mjai_translater/player.rb +++ b/lib/mjx_mjai_translater/player.rb @@ -5,10 +5,10 @@ $LOAD_PATH.unshift(__dir__) unless $LOAD_PATH.include?(__dir__) require "mjx_to_mjai" -class Player < Mjai::TCPPlayer +class Player def initialize(socket, id) - #super(socket, name) + @socket = socket @legal_actions = [] # mjxとのやりとりで更新していく @hand = [] # mjxとのやりとりで更新していく。 @id = id # mjaiのid diff --git a/lib/mjx_mjai_translater/random_agent.rb b/lib/mjx_mjai_translater/random_agent.rb index be61447..5bc21ac 100755 --- a/lib/mjx_mjai_translater/random_agent.rb +++ b/lib/mjx_mjai_translater/random_agent.rb @@ -20,4 +20,6 @@ def main s.run_till_terminated_or_interrupted([1, 'int', 'SIGQUIT']) end -#main +if __FILE__ == $0 + main +end From c349e36db3eede571654e47aeeccf20f03a2418d Mon Sep 17 00:00:00 2001 From: nissymori Date: Wed, 25 Aug 2021 19:47:12 +0900 Subject: [PATCH 2/3] add frame of communication --- lib/mjx_mjai_translater/trans_sever.rb | 47 ++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/lib/mjx_mjai_translater/trans_sever.rb b/lib/mjx_mjai_translater/trans_sever.rb index 33838da..61c5aa8 100644 --- a/lib/mjx_mjai_translater/trans_sever.rb +++ b/lib/mjx_mjai_translater/trans_sever.rb @@ -25,24 +25,57 @@ def initialize() # params paramsはcommandからextractされる。 def run() #TCPserverにおけるrunの部分 - initial_communication() #mjaiのclientとの最初の通信 - initialize_players(socket) while true - observation = get_observation() - take_action(observation) + Thread.new(@server.accept()) do |socket| + message = initial_communication(socket) + if message["type"] != "join" || !message["name"] || !message["room"] + raise(LocalError, "Expected e.g. %s" % + JSON.dump({"type" => "join", "name" => "noname", "room" => @params[:room]})) + end + if message["room"] != @params[:room] + raise(LocalError, "No such room. Available room: %s" % @params[:room]) + end + initialize_players() + observation = get_observation() + take_action(observation) + end end - end - def initial_communication() # clientとの最初の通信 + def initial_communication(socket) # clientとの最初の通信 + socket.sync = true + socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) + send(socket, { + "type" => "hello", + "protocol" => "mjsonp", + "protocol_version" => 3, + }) + line = socket.gets() + if !line + raise(LocalError, "Connection closed") + end + puts("server <- player ?\t#{line}") + message = JSON.parse(line) + return message end def initialize_players(socket) + if @players.size >= self.num_tcp_players + raise(LocalError, "The room is busy. Retry after a while.") + end @num_player_size.times do |i| @players.push(Player.new(socket, i)) # ここの作る順番がidになる。 end - end + end + + def process_one_game() + # for 指定された局数 + # - while game + # - 局開始 + # - 局終わり + # - end game + end def do_action(action) # mjai_clientにactionを渡してresponseを得る。 #mjaiと同じ実装 From f04efbd0bdc0e9efebbe6ef88bb26ffc8e5c52b8 Mon Sep 17 00:00:00 2001 From: nissymori Date: Thu, 26 Aug 2021 16:50:44 +0900 Subject: [PATCH 3/3] fix --- lib/mjx_mjai_translater/random_agent.rb | 2 +- lib/mjx_mjai_translater/trans_sever.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/mjx_mjai_translater/random_agent.rb b/lib/mjx_mjai_translater/random_agent.rb index 5bc21ac..7bcb422 100755 --- a/lib/mjx_mjai_translater/random_agent.rb +++ b/lib/mjx_mjai_translater/random_agent.rb @@ -13,7 +13,7 @@ def take_action(observation, _unused_call) end end -def main +def main # agentを1対立てる s = GRPC::RpcServer.new s.add_http2_port('0.0.0.0:50051', :this_port_is_insecure) s.handle(RandomAgent) diff --git a/lib/mjx_mjai_translater/trans_sever.rb b/lib/mjx_mjai_translater/trans_sever.rb index 61c5aa8..000ba9f 100644 --- a/lib/mjx_mjai_translater/trans_sever.rb +++ b/lib/mjx_mjai_translater/trans_sever.rb @@ -20,7 +20,7 @@ def initialize() # params paramsはcommandからextractされる。 @_mjx_events = nil @new_mjai_acitons = [] @next_mjx_actions = [] - initialize_players(@server)# クラスができるときにplayerも必要な数作るようにする。 + #initialize_players(@server)# クラスができるときにplayerも必要な数作るようにする。 end def run() @@ -61,7 +61,7 @@ def initial_communication(socket) # clientとの最初の通信 end def initialize_players(socket) - if @players.size >= self.num_tcp_players + if @players.size >= @num_tcp_players raise(LocalError, "The room is busy. Retry after a while.") end @num_player_size.times do |i|