def try_move(self, coord): """tenta se mover envia a coord para qual vai ir se autorizado recebe 200 se nao 400. """ req = Message(cmd=Commands.MOVE_TO, data=coord) self.dealer_socket.send(req.serialize()) if coord in self.robo.cacas: msg = Message(cmd=Commands.GET_FLAG, data=coord) self.dealer_socket.send(msg.serialize()) self.robo.current_pos = coord self.robo.move(coord)
def _process_request(self, msg): print("msg.cmd: ", msg.cmd) # cadastra o S.S if msg.cmd == Commands.LOGIN: if self.jogo.registra_jogador(msg.address, msg.data): print(msg.data) info = {'status': 200, 'info': 'logado'} self._send_status(info, msg.address) print("Login de ", msg.data, " efetuado com sucesso") else: info = {'status': 400, 'info': 'nao foi possivel logar'} self._send_status(info, msg.address) # autoriza ou nao o movimento de um jogador elif msg.cmd == Commands.MOVE_TO: if self.jogo.move_jogador(msg.address, msg.data): # info = {'status': 200, 'info': 'OK'} # self._send_status(info, msg.address) self._update_map() else: info = {'status': 400, 'info': 'posicao invalida ou ja ocupada'} self._send_status(info, msg.address) # valida ou nao uma caca, caso seja validada, envia a todos a nova lista de cacas # atualiza placar tb elif msg.cmd == Commands.GET_FLAG: # self._process_flag(msg) self._process_flag_thread(msg) elif msg.cmd == Commands.GET_IP: ip = self.jogo.get_player_ip(msg.data) rep = Message(cmd=Commands.GET_IP, data=ip) self._router_socket.send_multipart([msg.address, rep.serialize()]) else: pass
def get_flag(self, coord): """ Envia mensagem que obteve uma bandeira """ req = Message(cmd=Commands.GET_FLAG, data=coord) self.dealer_socket.send(req.serialize()) if self.supervisor.jogo.manual: Thread(target=self._wait_flag_status, daemon=False).start() else: self._wait_flag_status()
def set_mode(self, mode): """Manual = TRUE Automatico = False """ self.jogo.manual = mode msg = Message(cmd=Commands.MODE, data=mode) self._publish_socket.send(msg.serialize()) if mode: print("modo manual") else: print("modo automatico")
def _update_flags(self): # atualiza as cacas msg = Message(cmd=Commands.UPDATE_FLAGS, data=self.jogo.lista_de_cacas) self._publish_socket.send(msg.serialize()) if self.jogo.lista_de_cacas: print("Bandeiras ", self.jogo.lista_de_cacas) else: print("\n\n\n FIM DE JOGO \n\n\n")
def direita(self): self._mover(dir=Robo.RIGHT) self.current_pos = int( self.current_pos[0]), int(self.current_pos[1]) + 1 print(self.current_pos) if not self.manual: msg = Message(cmd=Mover.DIREITA) self.connection.send(msg.serialize()) time.sleep(Automatico.SLEEP_TIME)
def esquerda(self): self._mover(dir=Robo.LEFT) self.current_pos = int( self.current_pos[0]), int(self.current_pos[1]) - 1 print(self.current_pos) if not self.manual: msg = Message(cmd=Mover.ESQUERDA) self.connection.send(msg.serialize()) time.sleep(Automatico.SLEEP_TIME)
def frente(self): self._mover(dir=Robo.FRONT) self.current_pos = int(self.current_pos[0]) + 1, int( self.current_pos[1]) print(self.current_pos) if not self.manual: msg = Message(cmd=Mover.FRENTE) self.connection.send(msg.serialize()) time.sleep(Automatico.SLEEP_TIME)
def tras(self): self._mover(dir=Robo.BACK) self.current_pos = int(self.current_pos[0]) - 1, int( self.current_pos[1]) print(self.current_pos) if not self.manual: msg = Message(cmd=Mover.TRAS) self.connection.send(msg.serialize()) time.sleep(Automatico.SLEEP_TIME)
def _connect_to_supervisor(self, ip): # conecta ao respectivo S.S self._dealer_socket = self.context.socket(zmq.DEALER) self._dealer_socket.connect("tcp://%s:%d" % (ip, Supervisor.SUPERVISOR_PORT)) # envia uma mensagem pro ss (so pra testar msm) msg = Message(cmd=Commands.CONNECT_TO_SS) self._dealer_socket.send(msg.serialize())
def stop_game(self): """Termina a partida, imprime o placar, informa aos supervisores o fim da mesma """ msg = Message(cmd=Commands.STOP) self._publish_socket.send(msg.serialize()) placar = self.jogo.stop() print("o jogo terminou") for player in placar: print("o jogador ", player, " obteve ", placar[player]) self.jogo_started = False
def move(self, coord): """Envia ao robo a ordem para se mover para coord""" info = { "status": 200, "data": coord } msg = Message(cmd=Commands.MOVE_TO, data=info) self.router_socket.send_multipart([self.robot_address, msg.serialize()]) self.current_pos = coord print("as coord sao", self.current_pos)
def login(self): '''Metodo para o Login no jogo, envia a ID do robo''' dados = { Commands.ID: self.id, Commands.IP: self.my_ip, Commands.INITIAL_POS: self.supervisor.current_pos } # Commands.ID = string 'id', Commands.IP = 'ip', Commands.INITIAL_POS = 'initial_pos' req = Message(cmd=Commands.LOGIN, data=dados) self.dealer_socket.send(req.serialize()) self._read_rep()
def _sorteia_cacas(self): # Sorteia e envia as bandeiras self.cacas = self.jogo.sorteia_cacas() print(self.cacas) ##msg = Message(cmd=Commands.MODE, data=mode) #self._publish_socket.send(msg.serialize()) #self.cacas = [(0,1),(1,2),(2,3),(3,4),(1,1)] msg = Message(cmd=Commands.START, data=self.cacas) print("Bandeiras: ", self.cacas) self._publish_socket.send(msg.serialize())
def _sorteia_cacas(self): # Sorteia e envia as bandeiras try: if debug: self.cacas = def_flags print("Modo de debug") except: self.cacas = self.jogo.sorteia_cacas() msg = Message(cmd=Commands.START, data=self.cacas) print("Bandeiras: ", self.cacas) self._publish_socket.send(msg.serialize())
def tras(self): try: self.motor.tras() except Exception as e: pass self.current_pos = int(self.current_pos[0]) - 1, int(self.current_pos[1]) print(self.current_pos) if not self.manual: msg = Message(cmd=Mover.TRAS) self.connection.send(msg.serialize()) time.sleep(Automatico.SLEEP_TIME)
def esquerda(self): try: self.motor.esquerda() except Exception as e: pass self.current_pos = int(self.current_pos[0]), int(self.current_pos[1]) - 1 print(self.current_pos) if not self.manual: msg = Message(cmd=Mover.ESQUERDA) self.connection.send(msg.serialize()) time.sleep(Automatico.SLEEP_TIME)
def direita(self): try: self.motor.direita() except Exception as e: pass self.current_pos = int(self.current_pos[0]), int(self.current_pos[1]) + 1 print(self.current_pos) if not self.manual: msg = Message(cmd=Mover.DIREITA) self.connection.send(msg.serialize()) time.sleep(Automatico.SLEEP_TIME)
def frente(self): try: self.motor.move(Mover.FRENTE) except Exception as e: pass self.current_pos = int(self.current_pos[0]) + 1, int(self.current_pos[1]) print(self.current_pos) if not self.manual: while not self.motor.moveu: pass msg = Message(cmd=Mover.FRENTE) self.connection.send(msg.serialize()) time.sleep(Automatico.SLEEP_TIME)
def connect(self, player_id): # cria um socket temporario, envia a solicitacao pro servidor socket = self.context.socket(zmq.DEALER) socket.connect("tcp://%s:%d" % (self.SA_ip, self.port + 1)) msg = Message(cmd=Commands.GET_IP, data=player_id) socket.send(msg.serialize()) # recebe a resposta do servidor rep = socket.recv() rep = Message(0, rep) # self.SS_ip = rep.data self.connect_to_supervisor(rep.data) print(rep.data)
def _process_cmd(self, msg): address = msg.address # recebe mensagem de obtem bandeira if msg.cmd == Commands.GET_FLAG: self.comunica_com_sa.get_flag(msg.data) while self.is_blocked(): pass # o robo n precisa saber o status, so o supervisor self.router_socket.send_multipart([self.robot_address, Message(cmd=Commands.STATUS_GET_FLAG).serialize()]) # robo informa ao supervisor que quer ir para X,Y posicao elif msg.cmd == Commands.MOVE_TO: self.current_pos = msg.data self.comunica_com_sa.try_move(msg.data) # robo estabelece conexao com o seu respectivo supervisor elif msg.cmd == Commands.CONNECT_TO_SS: print("Robo Conectado") self.robot_address = address msg = Message(cmd=Commands.INITIAL_POS, data=self.begin_pos) self._send_reply(address, msg.serialize()) else: pass
def start_robot(self): msg = Message(cmd=Commands.START) self.router_socket.send_multipart([self.robot_address, msg.serialize()])
def quit(self): msg = Message(cmd=Commands.QUIT) self._publish_socket.send(msg.serialize())
def _send_status(self, info, address): resp = Message(cmd=Commands.STATUS, data=info) self._router_socket.send_multipart([address, resp.serialize()])
def manda_frente(self): msg = Message(cmd=Commands.DIRECTION, data=Mover.FRENTE) self.router_socket.send_multipart([self.robot_address, msg.serialize()]) self.current_pos = int(self.current_pos[0]) + 1, int(self.current_pos[1]) print("frente") print("\n", self.current_pos)
def manda_tras(self): msg = Message(cmd=Commands.DIRECTION, data=Mover.TRAS) self.router_socket.send_multipart([self.robot_address, msg.serialize()]) self.current_pos = int(self.current_pos[0]) - 1, int(self.current_pos[1]) print("tras") print("\n", self.current_pos)
def _update_map(self): msg = Message(cmd=Commands.UPDATE_MAP, data=self.jogo.update_map()) self._publish_socket.send(msg.serialize())
def manda_direita(self): msg = Message(cmd=Commands.DIRECTION, data=Mover.DIREITA) self.router_socket.send_multipart([self.robot_address, msg.serialize()]) self.current_pos = int(self.current_pos[0]), int(self.current_pos[1]) + 1 print("direita") print("\n", self.current_pos)
def set_mode(self, mode): msg = Message(cmd=Commands.MODE, data=mode) self.router_socket.send_multipart([self.robot_address, msg.serialize()])
def manda_esquerda(self): msg = Message(cmd=Commands.DIRECTION, data=Mover.ESQUERDA) self.router_socket.send_multipart([self.robot_address, msg.serialize()]) self.current_pos = int(self.current_pos[0]), int(self.current_pos[1]) - 1 print("esquerda") print("\n", self.current_pos)