- self.process.sendline( "ATTACK %s" % foe )
- self.process.expect( "ATTACKING (.+) (.+)\n" )
- attack, bluff = self.process.match.groups()
- attack, bluff = attack.strip(), bluff.strip()
- return self.stringToItem(attack), self.stringToItem(bluff)
-
- def Defend( self, foe, bluff ):
- #print "DEFEND %s %s" % (foe, self.itemToString(bluff))
- self.process.sendline( "DEFEND %s %s" % (foe, self.itemToString(bluff) ) )
- self.process.expect( "DEFENDING (.+)" )
- #print '------------------ ', self.process.match.groups()[0].strip()
- defence = self.process.match.groups()[0].strip()
- return self.stringToItem(defence)
-
\ No newline at end of file
+ self.process.stdin.write ( ' '.join( ["ATTACK", repr(foe), "\r\n"] ) )
+ #print >>sys.stderr, self.process.stderr.readlines()
+ result = self.process.stdout.readline().split()
+ try:
+ attack, bluff = self.stringToItem( result[1] ), self.stringToItem( result[2] )
+ return attack, bluff
+ except:
+ #agent is insane
+ print "Agent is insane:", self, self.GetID()
+ pass
+
+ def Defend (self, foe, bluff ):
+ self.process.stdin.write ( ' '.join( ["DEFEND", repr(foe), self.itemToString( bluff ), "\r\n"] ) )
+ #print >>sys.stderr, self.process.stderr.readlines()
+ result = self.process.stdout.readline().split()
+ try:
+ defence = self.stringToItem( result[1] )
+ return defence
+ except:
+ #agent is insane
+ print "Agent is insane:", self, self.GetID()
+ pass
+
+ def Results (self, foe, isInstigatedByYou, winner, attItem, defItem, bluffItem, pointDelta):
+
+ BaseAgent.Results (self, foe, isInstigatedByYou, winner, attItem,
+ defItem, bluffItem, pointDelta)
+
+ string = ' '.join( [ "RESULTS", repr(foe), repr(isInstigatedByYou),
+ self.resultToString(winner),
+ self.itemToString( attItem ),
+ self.itemToString( defItem ),
+ self.itemToString( bluffItem ), repr(pointDelta),
+ "\r\n" ] )
+
+ #string = "RESULTS %s %s %s %s %s %s %d\r\n" % (foe, isInstigatedByYou,
+ # self.resultToString(winner),
+ # self.itemToString( attItem ),
+ # self.itemToString( defItem ),
+ # self.itemToString( bluffItem ), pointDelta)
+ #print string
+
+ self.process.stdin.write ( string )
+ self.process.stdout.readline() # read and discard (should be "OK")
+
+ # we kill off the process here because otherwise the class doesn't get
+ # destroyed until the end of the iteration. This causes us to hold more
+ # than MAX_TOTAL_AGENTS open for a period of time, which is a bad thing.
+ if self.IsDead():
+ try:
+ self.process.communicate( "BYE\r\n" )
+ except Exception, e:
+ print "Error in BYE:", self, ":", e
+
+ try:
+ self.process.kill()
+ except:
+ None
+
+
+ def __del__(self):
+ #unless we're being deleted unexpectedly, this is a no-op.
+ if self.process.poll() == None:
+ try:
+ self.process.communicate( "BYE\r\n" )
+ except Exception, e:
+ print "Error in BYE:", self, ":", e
+
+ try:
+ self.process.kill()
+ except:
+ None