Last active
November 1, 2018 19:43
-
-
Save Billiam/42f228f5677c72228b5b67598a427113 to your computer and use it in GitHub Desktop.
Halite-III Ruby Debugging proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> ./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