Commits

David Jones committed 66f0d5d

stores the game results now, if given an output option.
Also, prints out the running status, if given the verbose option

The game output is the same as the playgame.jar, except there
are extra INFO lines from the tcp server.

Changed host, ip, and user from optional to positional parameters.

Comments (0)

Files changed (1)

 import time, sys
 import threading
 from engine import BotThread
+from PlanetWars import PlanetWars
 
+import re
 
 class FailedToConnect(Exception): pass
 
-def tcp(host, port, user, bot_command, password=None):
+def tcp(host, port, user, bot_command, options):
     # Start up the bot.  This may fail.
     bot = BotThread(bot_command)
-
+    of = None
+    info_lines = []
     try:
         # Start up the network connection
         sock = socket(AF_INET, SOCK_STREAM)
         else:
             raise FailedToConnect
 
-        if password is not None:
-            sock.sendall("USER %s PASS %s\n" % (user, password))
+        if options.password is not None:
+            sock.sendall("USER %s PASS %s\n" % (user, options.password))
         else:
             sock.sendall("USER %s\n" % user)
 
 
         # play the game
         game_state = []
-
-        for turn in xrange(1, 100000):
+        need_newline = False
+        line_feed = False
+        turn = 0
+        while True:
             if not fp: break
             line = fp.readline()
             if not line:
                 break
             if line.startswith("INFO "):
+                if line_feed: print "\n"
                 print line[5:].strip()
+                line_feed = False                
+                if of is None:
+                    info_lines.append(line.strip())
+                else:
+                    if need_newline:
+                        of.write("\n")
+                    of.write(line + "\n")
+                    need_newline = False
+
+                key = "INFO This is game_id="
+                if options.output_prefix and of is None:
+                    m = re.search(key + "(\\d+)", line)
+                    if m:
+                        game_id = m.groups()[0]
+                        of = open(options.output_prefix + "." + game_id, "w")
+                        for line in info_lines:
+                            of.write(line + "\n")
+
             else:
                 # print "got line: <%s>" % (line.strip())
                 game_state.append(line)
                 if line.startswith("go"):
+                    turn += 1
                     timeoutms = 2000
+                    pw = PlanetWars(game_state)
+                    if of:
+                        of.write(pw.dump_state(turn==1))
+                        need_newline = True
+                    if options.verbose:
+                        counts, rates = pw.get_counts()
+                        
+                        s = ("turn %3d counts: [%4s, %4s] rates: [%3s, %3s] count diff: %5d"
+                             % (turn,
+                                counts[1], counts[2], rates[1], rates[2],
+                                counts[1] - counts[2]))
+                        line_feed = True
+                        sys.stderr.write("\r%-50s" % s)
                     move, success = bot.run_step("".join(game_state),
                                                  timeoutms)
                     game_state = []
         
 def main(argv):
     from optparse import OptionParser
-    usage = """usage: %prog [options] bot_command
-
-    run the bot from bot_command over the tcp connection.
-    bot_command, if it contains arguments, must be quoted."""
+    usage = """usage: %prog [options] IP PORT USERNAME BOT_COMMAND
+    
+    run the bot from BOT_COMMAND over the tcp connection.
+    If BOT_COMMAND contains arguments, it must be quoted."""
 
     # The quoting in the bot command could be fixed by changing the
     # BotThread class in engine.py.  Right now, it uses shell=True.
     
     
     parser = OptionParser(usage=usage)
-    parser.add_option("-a", "--address", dest="address",
-                      help="address of host")
-    parser.add_option("-p", "--port", dest="port", type="int",
-                      help="port to connect to")
+    parser.add_option("-o", "--output", dest="output_prefix",
+                      help="prefix of output file  OUTPUT.game_id")    
     parser.add_option("--pass", dest="password",
                       default=None)
-    parser.add_option("--user", dest="user")
+    parser.add_option("-v", "--verbose", dest="verbose",
+                      action="store_true")
+    
     (options, args) = parser.parse_args()
-    command = args[0]
-    tcp(options.address, options.port, options.user, command,
-        options.password)    
+    if len(args) < 4:
+        print "not enough arguments."
+        parser.print_usage()
+
+    ip = args[0]
+    
+    port = int(args[1])
+    user = args[2]
+    command = args[3]
+
+    tcp(ip, port, user, command, options)
                       
 
-def testf(host="127.0.0.1", port=9999,user="aaa"):
-    tcp(host,port,"/home/dave/a/ai/bots/bot/MyBot.py bot.log", user,"blahblah")
-
 
 if __name__ == "__main__":
     main(sys.argv[1:])