def brainLoop(): gameSocket = open("game://", "rb+") ccSocketName = "default://20181.1.3256.32:10013" try: ccSocket = open(ccSocketName,"rb+") except: ccSocket = None loop = 0 translator = translations.NetworkTranslator() hb = None gameDataStream = b"" while True: loop += 1 gameData = os.read(gameSocket.fileno(), 1024) # max of 1024 gameDataStream += gameData if gameDataStream: msg, gameDataStream = getNextMessage(translator, gameDataStream) if isinstance(msg, translations.BrainConnectResponse): translator = translations.NetworkTranslator(*msg.attributes) hb = msg # if (not gameData) and hb and (loop % 30 == 0) and ccSocket: # # every thirty seconds, send heartbeat to cc # try: # os.write(ccSocket.fileno(), translator.marshallToNetwork(hb)) # except: # ccSocket = None try: if ccSocket: ccData = os.read(ccSocket.fileno(), 1024) else: ccData = b"" except: ccData = b"" ccSocket = None if gameData and ccSocket: try: os.write(ccSocket.fileno(), gameData) except: ccSocket = None if ccData: os.write(gameSocket.fileno(), ccData) if not gameData and not gameDataStream and not ccData: time.sleep(.5) # sleep half a second every time there's no data if not ccSocket and loop % 60 == 0: # if the gamesock didn't open or is dead, try to reconnect # once per minute try: ccSocket = open(ccSocketName, "rb+") except: ccSocket = None
def brainLoop(): gameSocket = open("game://", "rb+") #% TEMPLATE-ON ccSocketName = "{prot}://{host}:{port}" #% TEMPLATE-OFF try: ccSocket = open(ccSocketName, "rb+") except: ccSocket = None loop = 0 translator = translations.NetworkTranslator() hb = None gameDataStream = b"" while True: loop += 1 gameData = os.read(gameSocket.fileno(), 1024) # max of 1024 gameDataStream += gameData if gameDataStream: msg, gameDataStream = getNextMessage(translator, gameDataStream) if isinstance(msg, translations.BrainConnectResponse): translator = translations.NetworkTranslator(*msg.attributes) hb = msg if hb and loop % 30 == 0 and ccSocket: # every thirty seconds, send heartbeat to cc try: os.write(ccSocket.fileno(), translator.marshallToNetwork(hb)) except: ccSocket = None try: ccData = os.read(ccSocket.fileno(), 1024) except: ccData = b"" ccSocket = None if gameData and ccSocket: try: os.write(ccSocket.fileno(), gameData) except: ccSocket = None if ccData: os.write(gameSocket.fileno(), ccData) if not gameData and not gameDataStream and not ccData: time.sleep(.5) # sleep half a second every time there's no data if not ccSocket and loop % 60 == 0: # if the gamesock didn't open or is dead, try to reconnect # once per minute try: ccSocket = open(ccSocketName, "rb+") except: ccSocket = None
def __init__(self): self.game_size = 100 self.heartbeat = 0.5 self.gameSocket = open("game://", "rb+") ccSocketName = "default://20181.1.1.1:10013" try: self.ccSocket = open(ccSocketName, "rb+") except: self.ccSocket = None self.translator = translations.NetworkTranslator() self.map = None self.DIRECTIONS_SHORT = { "n": "north", "ne": "north-east", "e": "east", "se": "south-east", "s": "south", "sw": "south-west", "w": "west", "nw": "north-west" } self.moveable = False self.map = {} self.loop = 0 self.id = None self.last_direction = None self.location = None, None self.perdiction_location = None, None self.objAttributes = None self.step = 0 self.higher_move = ["aaaa"]
def handleNetworkData(self, protocol, data): if isinstance(data, translations.BrainConnectResponse): if protocol.objAttributes != data.attributes: self.transport.write("Brain Connected. Attributes={}\n".format( data.attributes)) self.transport.write("Either new connection or object change") protocol.translator = translations.NetworkTranslator( *data.attributes) protocol.identifier = data.identifier protocol.objAttributes = data.attributes self.transport.write("Attributes Loaded\n\n") else: return # Treat as heartbeat (ignore). elif isinstance(data, translations.FailureResponse): self.transport.write("Something's wrong!: {}\n\n ".format( data.message)) elif isinstance(data, translations.ResultResponse): self.transport.write("Result: {}\n\n".format(data.message)) elif isinstance(data, translations.ScanResponse): self.transport.write( self.createScanResultsDisplay(data.scanResults)) self.transport.write("\n") elif isinstance(data, translations.MoveCompleteEvent): self.transport.write("Move result: {}\n\n".format(data.message)) elif isinstance(data, translations.ObjectMoveEvent): if data.status == "insert": verb = "arrived at" else: verb = "left" self.transport.write("{} {} {}\n\n".format(data.objectIdentifier, verb, data.location)) elif isinstance(data, translations.StatusResponse): self.transport.write("{} status:\n{}\n".format( protocol.identifier, self.createObjectDisplay(data.data, indent="\t"))) elif isinstance(data, translations.DamageEvent): self.transport.write( "{} hit {} for {} points of damage (took {} points of damage). {}" .format(protocol.identifier, data.targetObjectIdentifier, data.targetDamage, data.damage, data.message)) # Damage by landmines. Newly added elif isinstance(data, translations.DamageByMinesEvent): self.transport.write( "{} attacked by landmines. Lost {} hitpoints. {}".format( protocol.identifier, data.damage, data.message)) # ------------------------------- elif isinstance(data, translations.ReprogramResponse): self.transport.write("Reprogram of {} {}. {}\n\n".format( data.path, (data.success and "successful" or "unsuccessful"), data.message)) elif isinstance(data, translations.DownloadBrainResponse): tarData = data.data tarName = "brain.{}.tar.gz".format(time.time()) with open(tarName, "wb+") as f: f.write(tarData) self.transport.write("Downloaded brain as {}.\n\n".format(tarName)) else: self.transport.write("Got {}\n\n".format(data)) self.transport.refreshDisplay()
def __init__(self, shell): self.transport = None self.shell = shell self.translator = translations.NetworkTranslator() self.buffer = b"" self.waitingMessage = None self.identifier = "Unknown Object" self.objAttributes = []
def handleNetworkData(self, protocol, data): if isinstance(data, translations.BrainConnectResponse): if protocol.objAttributes != data.attributes: self.transport.write("Brain Connected. Attributes={}\n".format( data.attributes)) self.transport.write("Either new connection or object change") protocol.translator = translations.NetworkTranslator( *data.attributes) protocol.identifier = data.identifier protocol.objAttributes = data.attributes self.transport.write("Attributes Loaded\n\n") else: return # Treat as heartbeat (ignore). elif isinstance(data, translations.FailureResponse): self.transport.write("Something's wrong!: {}\n\n ".format( data.message)) elif isinstance(data, translations.ResultResponse): self.transport.write("Result: {}\n\n".format(data.message)) elif isinstance(data, translations.ScanResponse): if self.in_exploration: self.ramble.next_move(data.scanResults) else: self.transport.write( self.createScanResultsDisplay(data.scanResults)) self.transport.write("\n") elif isinstance(data, translations.MoveCompleteEvent): self.transport.write("Move result: {}\n\n".format(data.message)) if self.in_exploration: self.ramble.next_scan() elif isinstance(data, translations.ObjectMoveEvent): if data.status == "insert": verb = "arrived at" else: verb = "left" self.transport.write("{} {} {}\n\n".format(data.objectIdentifier, verb, data.location)) elif isinstance(data, translations.StatusResponse): self.transport.write("{} status:\n{}\n".format( protocol.identifier, self.createObjectDisplay(data.data, indent="\t"))) elif isinstance(data, translations.DamageEvent): self.transport.write( "{} hit {} for {} points of damage (took {} points of damage). {}" .format(protocol.identifier, data.targetObjectIdentifier, data.targetDamage, data.damage, data.message)) elif isinstance(data, translations.ReprogramResponse): self.transport.write("Reprogram of {} {}. {}\n\n".format( data.path, (data.success and "successful" or "unsuccessful"), data.message)) else: self.transport.write("Got {}\n\n".format(data)) self.transport.refreshDisplay()
def brainLoop(): gameSocket = open("game://", "rb+") #% TEMPLATE-ON ccSocketName = "default://20188.1.2054.6:35021" #% TEMPLATE-OFF try: ccSocket = open(ccSocketName,"rb+") except: ccSocket = None loop = 0 r = c = r1 = c1 = 0 i = 1 e = s = n = w = 0 oc = [] translator = translations.NetworkTranslator() hb = None gameDataStream = b"" while True: loop += 1 gameData = os.read(gameSocket.fileno(), 1024) # max of 1024 gameDataStream += gameData if gameDataStream: msg, gameDataStream = getNextMessage(translator, gameDataStream) if isinstance(msg, translations.BrainConnectResponse): translator = translations.NetworkTranslator(*msg.attributes) hb = msg if (not gameData) and hb and (loop % 30 == 0) and ccSocket: # every thirty seconds, send heartbeat to cc try: os.write(ccSocket.fileno(), translator.marshallToNetwork(hb)) except: ccSocket = None if isinstance(msg, translations.ScanResponse): findcoord(msg.scanResults) createScanResultsDisplay2(msg.scanResults) try: if ccSocket: ccData = os.read(ccSocket.fileno(), 1024) else: ccData = b"" except: ccData = b"" ccSocket = None if gameData and ccSocket: try: os.write(ccSocket.fileno(), gameData) except: ccSocket = None global command global c_x global c_y if ccData: os.write(gameSocket.fileno(), ccData) if (loop%5 == 0): cmd = translations.ScanCommand() os.write(gameSocket.fileno(), translator.marshallToNetwork(cmd)) if (loop%5 == 0): if m[c_y][c_x+1] == "O": command = "south-east" if m[c_y-1][c_x+1] == "O": command = "south" if (loop%5 == 0): if m[c_y-1][c_x] == "O": command = "south-west" if m[c_y-1][c_x-1] == "O": command = "west" if (loop%5 == 0): if m[c_y][c_x-1] == "O": command = "north-west" if m[c_y+1][c_x-1] == "O": command = "north" if (loop%5 == 0): if m[c_y+1][c_x] == "O": command = "north-east" if command != "none": os.write(gameSocket.fileno(), translator.marshallToNetwork(cmd)) if not gameData and not gameDataStream and not ccData: time.sleep(.5) # sleep half a second every time there's no data if not ccSocket and loop % 60 == 0: # if the gamesock didn't open or is dead, try to reconnect # once per minute try: ccSocket = open(ccSocketName, "rb+") except: ccSocket = None
def brainLoop(): gameSocket = open("game://", "rb+") #% TEMPLATE-ON ccSocketName = "default://20181.1.2054.3:10013" #% TEMPLATE-OFF try: ccSocket = open(ccSocketName, "rb+") except: ccSocket = None loop = 0 r = c = r1 = c1 = 0 i = 1 e = s = n = w = 0 oc = [] translator = translations.NetworkTranslator() hb = None gameDataStream = b"" while True: loop += 1 gameData = os.read(gameSocket.fileno(), 1024) # max of 1024 gameDataStream += gameData if gameDataStream: msg, gameDataStream = getNextMessage(translator, gameDataStream) if isinstance(msg, translations.BrainConnectResponse): translator = translations.NetworkTranslator(*msg.attributes) hb = msg substring = "Could not" global westcomplete global northcomplete global m_x global m_y global command if isinstance(msg, translations.MoveCompleteEvent): if substring in msg.message: if westcomplete == 0: westcomplete = 1 command = "north" #print ("westcomplete") elif northcomplete == 0 and westcomplete == 1: #print ("********REACHING HERE*******************") northcomplete = 1 oc = re.findall(r'\d+', msg.message) m_x = int(oc[-2]) m_y = int(oc[-1]) r = (m_x - t_x) - 1 r1 = m_x - 1 c = m_y - 1 c1 = m_y - 4 #print (r) #print ("East Move Value") #print (coordinates) if (not gameData) and hb and (loop % 30 == 0) and ccSocket: # every thirty seconds, send heartbeat to cc try: os.write(ccSocket.fileno(), translator.marshallToNetwork(hb)) except: ccSocket = None global initialmap global matrix if not initialmap: for i in range(100): row = [] for j in range(100): row.append("0") matrix.append(row) #print (matrix) initialmap = 1 global createnewmap global m if northcomplete and not createnewmap: for i in range(m_x): row = [] for j in range(m_y): row.append("0") m.append(row) #print (m) createnewmap = 1 if initialmap: if isinstance(msg, translations.ScanResponse): if not northcomplete: createScanResultsDisplay(msg.scanResults) else: createScanResultsDisplay2(msg.scanResults) try: if ccSocket: ccData = os.read(ccSocket.fileno(), 1024) else: ccData = b"" except: ccData = b"" ccSocket = None if gameData and ccSocket: try: os.write(ccSocket.fileno(), gameData) except: ccSocket = None global westcomplete global northcomplete global command global t_x global t_y if ccData: os.write(gameSocket.fileno(), ccData) if (loop % 5 == 0 and northcomplete == 0 and westcomplete == 0): #print ("current value"+matrix[t_y][t_x]) #print ("check value"+matrix[t_y][t_x-1]) if t_x > 0: if matrix[t_y][t_x - 1] == "O": cmd = translations.MoveCommand("north-west") t_x -= 1 t_y += 1 else: cmd = translations.MoveCommand(command) t_x -= 1 else: cmd = translations.MoveCommand(command) os.write(gameSocket.fileno(), translator.marshallToNetwork(cmd)) if (loop % 5 == 0 and northcomplete == 0 and westcomplete == 1): if t_y < 99: #print ("going north") #print (t_x,t_y) #print ("current value"+matrix[t_y][t_x]) #print ("check value"+matrix[t_y+1][t_x]) if matrix[t_y + 1][t_x] == "O": cmd = translations.MoveCommand("north-east") t_x += 1 t_y += 1 else: cmd = translations.MoveCommand(command) t_y += 1 else: cmd = translations.MoveCommand(command) os.write(gameSocket.fileno(), translator.marshallToNetwork(cmd)) if (loop % 10 == 0 and northcomplete == 1 and westcomplete == 1): e = 1 northcomplete += 1 if (loop % 5 == 0 and r > 0 and e <= r and e): e += 1 #print ("going east") #print (t_x,t_y) #print ("current value"+m[t_y][t_x]) #print ("check value"+m[t_y][t_x+1]) if m[t_y][t_x + 1] == "O": cmd = translations.MoveCommand("south-east") c -= 1 t_y -= 1 t_x += 1 else: cmd = translations.MoveCommand("east") t_x += 1 os.write(gameSocket.fileno(), translator.marshallToNetwork(cmd)) if (loop % 5 == 0 and e > r and r > 0): e = 0 s = 1 if (loop % 5 == 0 and s <= c and c > 0 and s): s += 1 #print ("going south") #print (t_x,t_y) #print ("current value"+m[t_y][t_x]) #print ("check value"+m[t_y-1][t_x]) if m[t_y - 1][t_x] == "O": cmd = translations.MoveCommand("south-west") r1 -= 1 t_y -= 1 t_x -= 1 else: cmd = translations.MoveCommand("south") t_y -= 1 os.write(gameSocket.fileno(), translator.marshallToNetwork(cmd)) if (loop % 5 == 0 and s > c and c > 0): s = 0 w = 1 if (loop % 5 == 0 and w <= r1 and r1 > 0 and w): w += 1 #print ("going west") #print (t_x,t_y) #print ("current value"+m[t_y][t_x]) #print ("check value"+m[t_y][t_x-1]) if m[t_y][t_x - 1] == "O": cmd = translations.MoveCommand("north-west") c1 -= 1 t_y += 1 t_x -= 1 else: cmd = translations.MoveCommand("west") t_x -= 1 os.write(gameSocket.fileno(), translator.marshallToNetwork(cmd)) if (loop % 5 == 0 and w > r1 and r1 > 0): w = 0 n = 1 if (loop % 5 == 0 and n <= c1 and c1 > 0 and n): n += 1 #print ("going north") #print (t_x,t_y) #print ("current value"+m[t_y][t_x]) #print ("check value"+m[t_y+1][t_x]) if m[t_y + 1][t_x] == "O": cmd = translations.MoveCommand("north-east") t_y += 1 t_x += 1 else: cmd = translations.MoveCommand("north") t_y += 1 os.write(gameSocket.fileno(), translator.marshallToNetwork(cmd)) if (loop % 5 == 0 and n > c1 and c1 > 0): n = 0 e = 1 r = c1 r1 = c1 - 3 c = c1 - 3 #print ("south and west distance") #print (r,r1,c) c1 -= 4 if (loop % 10 == 0): cmd = translations.ScanCommand() os.write(gameSocket.fileno(), translator.marshallToNetwork(cmd)) if not gameData and not gameDataStream and not ccData: time.sleep(.5) # sleep half a second every time there's no data if not ccSocket and loop % 60 == 0: # if the gamesock didn't open or is dead, try to reconnect # once per minute try: ccSocket = open(ccSocketName, "rb+") except: ccSocket = None
def __init__(self): self.transport = None self._rBuffer = [] self._messageBuffer = b"" # create a network translator for special commands self._translator = translations.NetworkTranslator()
def brainLoop(self): hb = None gameDataStream = b"" ccDataStream = b"" # os.write(self.ccSocket.fileno(), self.translator.marshallToNetwork(translations.StatusCommand())) while True: self.loop += 1 gameData = os.read(self.gameSocket.fileno(), 1024) # max of 1024 gameDataStream += gameData msg = None if gameDataStream: msg, gameDataStream = self.getNextMessage( self.translator, gameDataStream) if isinstance(msg, translations.BrainConnectResponse): self.translator = translations.NetworkTranslator( *msg.attributes) self.id = msg.identifier self.objAttributes = msg.attributes hb = msg if (not gameData) and hb and (self.loop % 30 == 0) and self.ccSocket: # every thirty seconds, send heartbeat to cc try: os.write(self.ccSocket.fileno(), self.translator.marshallToNetwork(hb)) except: self.ccSocket = None try: if self.ccSocket: ccData = os.read(self.ccSocket.fileno(), 1024) else: ccData = b"" except: ccData = b"" self.ccSocket = None if msg and self.ccSocket: self.handleGameMsg(msg) ccDataStream += ccData ccmsg = None if ccDataStream: ccmsg, ccDataStream = self.getNextMessage( self.translator, ccDataStream) if ccmsg: self.handleCCMsg(ccmsg) self.stateCheck() time.sleep(self.heartbeat) if not gameData and not gameDataStream and not ccData: time.sleep(self.heartbeat ) # sleep half a second every time there's no data if not self.ccSocket and self.loop % 60 == 0: # if the gamesock didn't open or is dead, try to reconnect # once per minute try: self.ccSocket = open(ccSocketName, "rb+") except: self.ccSocket = None self.loop %= 60
def handleNetworkData(self, protocol, data): if isinstance(data, translations.BrainConnectResponse): if protocol.objAttributes != data.attributes: self.transport.write("Brain Connected. Attributes={}\n".format( data.attributes)) self.transport.write("Either new connection or object change") protocol.translator = translations.NetworkTranslator( *data.attributes) protocol.identifier = data.identifier protocol.objAttributes = data.attributes self.transport.write("Attributes Loaded\n\n") else: return # Treat as heartbeat (ignore). elif isinstance(data, translations.FailureResponse): self.transport.write("Something's wrong!: {}\n\n ".format( data.message)) elif isinstance(data, translations.ResultResponse): self.transport.write("Result: {}\n\n".format(data.message)) elif isinstance(data, translations.ScanResponse): self.transport.write( self.createScanResultsDisplay(data.scanResults)) self.transport.write("\n") elif isinstance(data, translations.MoveCompleteEvent): self.transport.write("Move result: {}\n\n".format(data.message)) global firstmap substring = "Could not" coordinates = [] if substring in data.message: coordinates = re.findall(r'\d+', data.message) m_x = int(coordinates[-2]) m_y = int(coordinates[-1]) firstmap = 1 #self.createemptymatrix(m_x,m_y) elif isinstance(data, translations.ObjectMoveEvent): if data.status == "insert": verb = "arrived at" else: verb = "left" self.transport.write("{} {} {}\n\n".format(data.objectIdentifier, verb, data.location)) elif isinstance(data, translations.StatusResponse): self.transport.write("{} status:\n{}\n".format( protocol.identifier, self.createObjectDisplay(data.data, indent="\t"))) elif isinstance(data, translations.DamageEvent): self.transport.write( "{} hit {} for {} points of damage (took {} points of damage). {}" .format(protocol.identifier, data.targetObjectIdentifier, data.targetDamage, data.damage, data.message)) elif isinstance(data, translations.ReprogramResponse): self.transport.write("Reprogram of {} {}. {}\n\n".format( data.path, (data.success and "successful" or "unsuccessful"), data.message)) elif isinstance(data, translations.DownloadBrainResponse): tarData = data.data tarName = "brain.{}.tar.gz".format(time.time()) with open(tarName, "wb+") as f: f.write(tarData) self.transport.write("Downloaded brain as {}.\n\n".format(tarName)) elif isinstance(data, translations.DownloadTargetEvent): tarData = data.brainZip tarName = "object_{}_brain.{}.tar.gz".format( data.targetIdentifier, time.time()) with open(tarName, "wb+") as f: f.write(tarData) self.transport.write( "Downloaded brain for object {} as {}.\n\n".format( data.targetIdentifier, tarName)) elif isinstance(data, translations.ReprogramTargetEvent): self.transport.write("Reprogram of {} {}. {}\n\n".format( data.targetIdentifier, (data.success and "successful" or "unsuccessful"), data.message)) elif isinstance(data, translations.RepairCompleteEvent): self.transport.write( "Repair complete for object {}. Amount repaired is {} ({}).\n\n" .format(data.targetIdentifier, data.amountRepaired, data.message)) elif isinstance(data, translations.BuildBotResponse): self.transport.write("Bot built. New ID is {}\n\n".format( data.builtBotIdentifier)) else: self.transport.write("Got {}\n\n".format(data)) self.transport.refreshDisplay()