Module: GoshrineBot::GtpProcess

Defined in:
lib/goshrine_bot/gtp_stdio_client.rb

Instance Method Summary collapse

Instance Method Details

#log(str) ⇒ Object



17
18
19
20
21
# File 'lib/goshrine_bot/gtp_stdio_client.rb', line 17

def log(str)
  File.open(@logfile, "a") do |f|
    f.write "#{Time.now} - #{str}\n"
  end
end

#logfile=(logfile) ⇒ Object



13
14
15
# File 'lib/goshrine_bot/gtp_stdio_client.rb', line 13

def logfile=(logfile)
  @logfile = logfile
end

#post_initObject



6
7
8
9
10
11
# File 'lib/goshrine_bot/gtp_stdio_client.rb', line 6

def post_init
  @command_id = 0
  @results = Queue.new
  @cmd_queue = Queue.new
  @data = ""
end

#receive_data(data) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/goshrine_bot/gtp_stdio_client.rb', line 37

def receive_data(data)
  log "I: #{data.inspect}"
  @data += data
  while (match_data = @data.match(/^([?=])(\d+)?\s(.*?)\n\n/m))      
    @data = @data[match_data[0].size..-1]

    res = @results.deq
    status = match_data[1]
    if status == '?'
      res.fail(match_data[3])
    else
      res.succeed(match_data[3])
    end
    if @cmd_queue.size > 0
      command = @cmd_queue.deq
      log "O: #{command.inspect}"
      send_data("#{command}\n")
    end
  end
end

#send(command) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/goshrine_bot/gtp_stdio_client.rb', line 23

def send(command)
  @command_id += 1
  # if we're waiting on results, don't send data just yet; queue it.
  if @results.size > 0
    @cmd_queue.enq command
  else
    log "O: #{command.inspect}"
    send_data("#{command}\n")
  end
  res = EM::DefaultDeferrable.new
  @results.enq res
  res
end

#unbindObject



58
59
60
61
# File 'lib/goshrine_bot/gtp_stdio_client.rb', line 58

def unbind
  puts "gtp process exited with status: #{get_status.exitstatus.inspect}"
  log "gtp process exited with status: #{get_status.exitstatus.inspect}"
end