Class: Sc2::Cli::VersusBot
- Inherits:
-
Thor::Group
- Object
- Thor::Group
- Sc2::Cli::VersusBot
- Includes:
- Thor::Actions
- Defined in:
- lib/sc2ai/cli/versus_bot.rb
Overview
Command line action allowing your bot to play vs a ladder bot via docker
Class Method Summary collapse
Instance Method Summary collapse
- #bot_validation ⇒ Object
- #bye ⇒ Object
- #contains_ladderbot_json ⇒ Object
- #copy_opponent_zip ⇒ Object
- #docker_exists ⇒ Object
- #extract_opponent ⇒ Object
- #get_botname ⇒ Object
- #parse_enemy_config ⇒ Object
- #port_configuration ⇒ Object
- #pull_ladderbots_json ⇒ Object
- #run_match ⇒ Object
- #set_compose_file ⇒ Object
- #start_container ⇒ Object
- #stop_container ⇒ Object
- #versus_bot_validation ⇒ Object
Class Method Details
.source_paths ⇒ Object
56 57 58 |
# File 'lib/sc2ai/cli/versus_bot.rb', line 56 def self.source_paths [Pathname(".")..to_s] end |
.source_root ⇒ Object
60 61 62 |
# File 'lib/sc2ai/cli/versus_bot.rb', line 60 def self.source_root Paths.template_root.to_s end |
Instance Method Details
#bot_validation ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/sc2ai/cli/versus_bot.rb', line 37 def bot_validation if Pathname("./boot.rb").exist? say_status("detected boot.rb", "found", :green) else say_status("detected boot.rb", "not found", :red) raise Sc2::Error, "boot.rb not found. Bot started from wrong directory." end ENV.delete("AIARENA") require "./boot" if $bot.is_a? Sc2::Player say_status("instance $bot", $bot.class, :green) else say_status("instance $bot", $bot.class, :red) raise Sc2::Error, "$bot instance nil or not Sc2::Player. Review boot.rb to ensure it creates $bot." end end |
#bye ⇒ Object
298 299 300 |
# File 'lib/sc2ai/cli/versus_bot.rb', line 298 def bye say "Game over." end |
#contains_ladderbot_json ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/sc2ai/cli/versus_bot.rb', line 112 def contains_ladderbot_json # Max size of the portion where zip filenames are stored max_end_of_cd_size = (1 << 16) - 1 + 22 + 20 searching_for_file = "ladderbots.json" # what we want to find found = File.open(Pathname(filename).realpath, "rb") do |f| # Borrowed code, will search near the end of a zip if f.size > max_end_of_cd_size f.seek(-max_end_of_cd_size, IO::SEEK_END) end content = f.read content.include?(searching_for_file) end if found say_status("zip contains ladderbots.json", "found", :green) else say_status("zip contains ladderbots.json", "not found", :red) puts "#{filename} should contain ladderbots.json. Add the file to the zip and try again." raise SystemExit end end |
#copy_opponent_zip ⇒ Object
140 141 142 143 144 |
# File 'lib/sc2ai/cli/versus_bot.rb', line 140 def copy_opponent_zip say_status("docker", "copying opponent...", :cyan) cmd = "docker compose #{@compose_args} cp #{Pathname(filename).realpath} versus_bot:/bots/" Kernel.system(cmd) end |
#docker_exists ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/sc2ai/cli/versus_bot.rb', line 18 def docker_exists if Kernel.system("docker --version", out: File::NULL, err: File::NULL) say_status("docker", "found", :green) else say_status("docker", "not found", :red) say("Please install 'docker' and/or ensure it's in your PATH to continue.") raise SystemExit end if Kernel.system("docker info", out: File::NULL, err: File::NULL) say_status("docker engine", "found", :green) else say(" ") say_status("docker engine", "offline", :red) say("Please start docker engine. If you have Docker Desktop installed, open it before retrying.") raise SystemExit end end |
#extract_opponent ⇒ Object
146 147 148 149 150 |
# File 'lib/sc2ai/cli/versus_bot.rb', line 146 def extract_opponent say_status("docker", "extracting opponent...", :cyan) cmd = "docker compose #{@compose_args} exec --workdir /bots versus_bot unzip -o #{Pathname(filename).basename} -d #{@enemy_botname}" Kernel.system(cmd) end |
#get_botname ⇒ Object
14 15 16 |
# File 'lib/sc2ai/cli/versus_bot.rb', line 14 def get_botname @enemy_botname = File.basename(filename, ".zip") end |
#parse_enemy_config ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/sc2ai/cli/versus_bot.rb', line 169 def parse_enemy_config say_status("docker", "configuring enemy", :cyan) json = JSON.parse(File.read(@temp_path)) bot_json = json["Bots"][@enemy_botname] @enemy_runtime = bot_json["Type"].downcase # "BinaryCpp", @enemy_race = bot_json["Race"].upcase.to_sym @enemy_filename = bot_json["FileName"] @command = case @enemy_runtime when "cppwin32" "wine #{@enemy_filename}.exe" when "cpplinux", "binarycpp" "/bots/#{@enemy_filename}/#{@enemy_filename}" when "dotnetcore" "dotnet #{@enemy_filename}.dll" when "java" "java -jar #{@enemy_filename}.jar" when "nodejs" "node #{@enemy_filename}.js" when "python" "python run.py" else @enemy_filename end # Make executable if %w[cpplinux binarycpp].include?(@enemy_runtime) cmd = "docker compose #{@compose_args} exec --workdir /bots/#{@enemy_botname} versus_bot chmod +x #{@command}" Kernel.system(cmd) end # Make data dir say_status("docker", "ensure data directory exists", :cyan) cmd = "docker compose #{@compose_args} exec --workdir /bots/#{@enemy_botname} versus_bot sh -c \"mkdir data && chmod +w data\"" Kernel.system(cmd) end |
#port_configuration ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/sc2ai/cli/versus_bot.rb', line 64 def port_configuration # Start with basic port setup @port_config = Sc2::Ports.port_config_auto(num_players: 2) say_status("port config", "configured", :green) ENV["SERVER_BASE_PORT"] = @port_config.server_port_set.base_port.to_s ENV["SERVER_GAME_PORT"] = @port_config.server_port_set.game_port.to_s ENV["CLIENT_BASE_PORT"] = @port_config.client_port_sets.first.base_port.to_s ENV["CLIENT_GAME_PORT"] = @port_config.client_port_sets.first.game_port.to_s # @port_config_env = # "-e SERVER_BASE_PORT=#{@port_config.server_port_set.base_port} " + # "-e SERVER_GAME_PORT=#{@port_config.server_port_set.game_port} " + # "-e CLIENT_BASE_PORT=#{@port_config.client_port_sets.first.base_port} " + # "-e CLIENT_GAME_PORT=#{@port_config.client_port_sets.first.game_port} " # # Create a modified version of ports for docker bot # @client_port_config = @port_config.dup # # Adjust ports for docker # @client_port_config.client_port_sets[0].base_port = 8453 # @client_port_config.client_port_sets[0].game_port = 2359 # # # Get dynamic assigned ports for host # real_client_base_port = `docker port versus_bot_container 8453/udp`.strip.split(":").last # real_client_game_port = `docker port versus_bot_container 2359`.strip.split(":").last # # Fix our client ports to forward to the docker bot # @port_config.client_port_sets[0].base_port = real_client_base_port.to_i # @port_config.client_port_sets[0].game_port = real_client_game_port.to_i end |
#pull_ladderbots_json ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/sc2ai/cli/versus_bot.rb', line 152 def pull_ladderbots_json require "tempfile" say_status("docker", "pulling enemy 'ladderbots.json'", :cyan) @temp_path = Tempfile.new("enemy_ladderbots.json").path cmd = "docker compose #{@compose_args} cp versus_bot:/bots/#{@enemy_botname}/ladderbots.json #{@temp_path}" Kernel.system(cmd) if File.exist?(@temp_path) say_status("docker", "pulled 'ladderbots.json' ok", :green) else say_status("docker", "error pulling 'ladderbots.json'", :red) exit end end |
#run_match ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/sc2ai/cli/versus_bot.rb', line 205 def run_match Async do Sc2.logger.level = :info client = Sc2::ClientManager.obtain(0) enemy_client = Sc2::ClientManager.obtain(1) if Gem.win_platform? sleep(10) end # Ladder specific overrides $bot.realtime = false $bot.opponent_id = @enemy_botname say_status("host", "connecting...", :cyan) $bot.connect(host: client.host, port: client.port) say_status("host", "creating game...", :cyan) $bot.create_game(map: MapFile.new(map.to_s), players: [ $bot, Sc2::Player.new(race: @enemy_race, name: @enemy_botname, type: Api::PlayerType::PARTICIPANT) ], realtime: false) begin Async do say_status("host", "joining game...", :cyan) $bot.join_game( server_host: client.host, port_config: @port_config ) $bot.add_listener($bot, klass: Sc2::Connection::StatusListener) $bot.play begin safe_player_name = $bot.name.gsub(/\s*[^A-Za-z0-9.-]\s*/, "_").downcase response = $bot.api.save_replay path = Pathname(Paths.bot_data_replay_dir).join("autosave-#{safe_player_name}-vs-#{@enemy_botname}.SC2Replay") f = File.new(path, "wb:ASCII-8BIT") f.write_nonblock(response.data) f.close rescue # Replay save failed end $bot.disconnect end Async do say_status("opponent", "joining game...", :cyan) # 192... ip # host_ladder_ip = `docker compose #{@compose_args} exec versus_bot sh -c "getent ahostsv4 host.docker.internal | grep STREAM"`.split(" ").first.strip # Local ip # host_ladder_ip = `docker inspect -f '{{range.NetworkSettings.Networks}}{{.Gateway}}{{end}}' versus_bot_container`.strip enemy_command = [@command, # "--LadderServer #{client.host}", # "--RealTime 0" "--LadderServer host.docker.internal", "--GamePort #{enemy_client.port}", "--StartPort #{@port_config.start_port}", "--OpponentId 00000000-0000-0000-0000-000000000000"] .join(" ") # cmd = "docker compose #{@compose_args} exec --workdir /bots/#{@enemy_botname} versus_bot bash -c '#{enemy_command}'" cmd = "docker compose #{@compose_args} exec --workdir /bots/#{@enemy_botname} versus_bot bash -c '#{enemy_command}'" puts cmd = {} if Gem.win_platform? [:new_pgroup] = true else [:pgroup] = true end # ::Async::Process.spawn(cmd, **process_options) unless Kernel.system(cmd) say_status("opponent", "exited with bad status", :red) raise SystemExit end end rescue => e say_status("error", e., :red) raise SystemExit # Replay save failed end end.wait ensure ClientManager.stop_all end |
#set_compose_file ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/sc2ai/cli/versus_bot.rb', line 93 def set_compose_file say_status("docker", "build local play container", :cyan) @compose_file = Sc2::Paths.gem_root .join("docker_build", "docker-compose-versus-bot.yml") ..to_s @compose_args = "-f #{@compose_file} #{@port_config_env}" cmd = "docker compose #{@compose_args} build" Kernel.system(cmd) end |
#start_container ⇒ Object
134 135 136 137 138 |
# File 'lib/sc2ai/cli/versus_bot.rb', line 134 def start_container say_status("docker", "starting bot container", :cyan) cmd = "docker compose #{@compose_args} up versus_bot -d" ## { rand(20) == 0 ? "--force-recreate" : "" } Kernel.system(cmd) end |
#stop_container ⇒ Object
293 294 295 296 |
# File 'lib/sc2ai/cli/versus_bot.rb', line 293 def stop_container cmd = "docker stop versus_bot_container" Kernel.system(cmd) end |
#versus_bot_validation ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'lib/sc2ai/cli/versus_bot.rb', line 103 def versus_bot_validation if Pathname(filename).exist? say_status("checking zip #{filename}", "found", :green) else say_status("checking zip #{filename}", "not found", :red) raise Sc2::Error, "#{filename} not found. Try an absolute path if relative will not detect." end end |