def main(uris): fail_fast() client = CCNxClient() for uri in uris: data = client.get(uri, "") print data
class GameClient(object): def __init__(self, username): self.ccnxClient = CCNxClient() self.username = username #@Remove def init(self): self.connectUri = "lci:/tutorial/game/connect" self.personalPrefix = "lci:/tutorial/" + self.username self.actionPrefix = "lci:/tutorial/" + self.username + "/action" self.statePrefix = "lci:/tutorial/game/players/state/" + self.username #@Remove def connect(self): payload = { "ip" : "localhost", "port" : 9695, "username" : self.username, "teamplayer" : 0, "number-of-ships" : 1 } self.ccnxClient.get(self.connectUri, json.dumps(payload)) # consume the result def run(self): listening = self.ccnxClient.listen(self.personalPrefix) while listening: name, data = self.ccnxClient.receive() if name == None: continue if "action" in name: self.display_state() self.take_action(name) listening = self.display_state() #@Remove def get_state(self, ship = 0): payload = { "username" : self.username, "ship-number" : ship, } data = self.ccnxClient.get(self.statePrefix, json.dumps(payload)) while data == None: data = self.ccnxClient.get(self.statePrefix, json.dumps(payload)) state = json.loads(data) return state def display_state(self, ship = 0): state = self.get_state(ship) if state["gameover"] == 1: print "Game over!" return False else: table = state_to_string(state) print table return True def get_input(self): action = str(raw_input('ACTION: ')) x = int(input('ROW: ')) y = int(input('COL: ')) # ship = int(input('ship: ')) return (action, x, y, 0) #@Remove def take_action(self, name): (action, x, y, ship) = self.get_input() params = {"x": x, "y": y, "ship-number": ship} response = { "username" : self.username, "action" : action, "params" : params } payload = json.dumps(response) self.ccnxClient.reply(name, payload)
class GameClient(object): def __init__(self, username): self.ccnxClient = CCNxClient() self.username = username #@Remove def init(self): self.connectUri = "lci:/tutorial/game/connect" self.personalPrefix = "lci:/tutorial/" + self.username self.actionPrefix = "lci:/tutorial/" + self.username + "/action" self.statePrefix = "lci:/tutorial/game/players/state/" + self.username #@Remove def connect(self): payload = { "ip": "localhost", "port": 9695, "username": self.username, "teamplayer": 0, "number-of-ships": 1 } self.ccnxClient.get(self.connectUri, json.dumps(payload)) # consume the result def run(self): listening = self.ccnxClient.listen(self.personalPrefix) while listening: name, data = self.ccnxClient.receive() if name == None: continue if "action" in name: self.display_state() self.take_action(name) listening = self.display_state() #@Remove def get_state(self, ship=0): payload = { "username": self.username, "ship-number": ship, } data = self.ccnxClient.get(self.statePrefix, json.dumps(payload)) while data == None: data = self.ccnxClient.get(self.statePrefix, json.dumps(payload)) state = json.loads(data) return state def display_state(self, ship=0): state = self.get_state(ship) if state["gameover"] == 1: print "Game over!" return False else: table = state_to_string(state) print table return True def get_input(self): action = str(raw_input('ACTION: ')) x = int(input('ROW: ')) y = int(input('COL: ')) # ship = int(input('ship: ')) return (action, x, y, 0) #@Remove def take_action(self, name): (action, x, y, ship) = self.get_input() params = {"x": x, "y": y, "ship-number": ship} response = { "username": self.username, "action": action, "params": params } payload = json.dumps(response) self.ccnxClient.reply(name, payload)
def main(uris): fail_fast() client = CCNxClient() for uri in uris: data = client.get(uri, "") print data