Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trans server の通信部分の概要 #101

Merged
merged 3 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/mjx_mjai_translater/player.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions lib/mjx_mjai_translater/random_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ 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)
s.run_till_terminated_or_interrupted([1, 'int', 'SIGQUIT'])
end

#main
if __FILE__ == $0
main
end
49 changes: 41 additions & 8 deletions lib/mjx_mjai_translater/trans_sever.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,62 @@ 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()
#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 >= @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と同じ実装
Expand Down