Skip to content

Instantly share code, notes, and snippets.

@Billiam
Last active November 1, 2018 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Billiam/42f228f5677c72228b5b67598a427113 to your computer and use it in GitHub Desktop.
Save Billiam/42f228f5677c72228b5b67598a427113 to your computer and use it in GitHub Desktop.
Halite-III Ruby Debugging proxy
require 'socket'
class Game
def initialize(name, proxy_socket=nil)
if proxy_socket
@socket = UNIXSocket.new(proxy_socket)
@input = @output = @socket
else
@input = $stdin
@output = $stdout
end
#...
end
#...
# replace $stdin/$stdout with IO variables
# from initialize
def read_from_input
@input.gets.strip
end
private
def write_to_output(data)
data = "#{data.strip}"
LOGGER.info("Sending: #{data.inspect}")
@output.puts(data)
end
end
require 'socket'
SOCKET_FILE = './.bot_socket'
$stdout.sync = true
File.delete(SOCKET_FILE) if File.exist?(SOCKET_FILE)
server = UNIXServer.open(SOCKET_FILE)
bot_connection = server.accept
OUTPUT_DESTINATIONS = {
bot: $stdout,
halite: bot_connection
}
queue = Queue.new
# Halite output thread
Thread.new do
while true
queue << [:halite, $stdin.gets]
end
end
# Bot output thread
Thread.new do
while true
queue << [:bot, bot_connection.gets]
end
end
while true
source, message = queue.pop
OUTPUT_DESTINATIONS[source].puts(message)
end
> ./halite --no-timeout "ruby ProxyBot.rb" "ruby some_other_bot.rb"
# Then, in another terminal
> ruby MyBot.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment