def _get_client_socket(self): """ return client socket connected to the server. fail if error with msg """ self.statusbar.push(0, "Connecting to the server...") try: s = ttc.get_client_socket(exception=True) self.statusbar.push(0, "Connected") greeting = ttc.get_msg_from_socket(s) ttc.d(greeting) except Exception as exp: ttc.d("1 {}".format(exp)) self.show_error_dialog(str(exp)) sys.exit(1) return s
def main(): s = ttc.get_client_socket() try: # Recebe mensagem de olá hello_msg = ttc.get_msg_from_socket(s) print("\n{0}\n".format(hello_msg)) print(''' Você é a cruz/o xis (X). Entre com as coordenadas, onde você deseja colocar o X. Exemplo, canto do topo a esquerda será (0, 0). No seguinte formato: <numero> <numero> <aperte Enter> ''') gf = copy.deepcopy(ttc.GAME_FIELD) ttc.print_game_field(gf) ### Loop do jogo até ter um ganhador ou CTRL+C while True: #Pega a jogada do usuário turn_json = ttc.get_turn_from_user(gf) #Manda a jogada ao servidor s.sendall(turn_json.encode(encoding='utf-8')) #Pega a resposta do servidor sobre a jogada do usuario res = ttc.get_msg_from_socket(s, exception=False, ex=True) # se for um erro, pergunta a jogada normalmente if is_error_in_answer(res): print( "Servidor não está gostando da resposta, tente de novo.\n") continue else: ttc.apply_turn(turn_json, gf, ttc.USER_RAW_STEP) ttc.print_game_field(gf) # Procura por ganhadores, se tiver um o jogo termina handle_winner_variable(res) #Pega a jogada do servidor print("Esperando a resposta do servidor...") server_step = ttc.get_msg_from_socket(s) ttc.d("server step: {0}\n".format(server_step)) ttc.apply_turn(server_step, gf, ttc.SERVER_RAW_STEP) handle_winner_variable(server_step) ttc.print_game_field(gf) except KeyboardInterrupt as k: print("\nDesligando... {0}".format(k)) except Exception as exp: print(": {0}".format(exp)) ttc.print_game_field(gf) except: print("Erro inesperado:", sys.exc_info()[0]) s.close() sys.exit(0)
def main(): s = ttc.get_client_socket() try: # get hello hello_msg = ttc.get_msg_from_socket(s) print("\n{0}\n".format(hello_msg)) print(''' You are a cross (X). Enter coordinats, where to put next cross. Suppose, left top corner is (0, 0). Input in format: <int> <int> <hit Return> ''') gf = copy.deepcopy(ttc.GAME_FIELD) ttc.print_game_field(gf) ### loop for a game, untill winner or ^C while True: #B get a step from user turn_json = ttc.get_turn_from_user(gf) #B send step to the server s.sendall(turn_json.encode('utf-8')) #B get server answer about user step res = ttc.get_msg_from_socket(s, exception=False, ex=True) # if error - ask step again if is_error_in_answer(res): print( "Ou, server not pleasent about your answer, try again.\n") continue else: ttc.apply_turn(turn_json, gf, ttc.USER_RAW_STEP) ttc.print_game_field(gf) # check for winners in the answer, if exist any - game ends. handle_winner_variable(res) #B get server step print("Wait for server response...") server_step = ttc.get_msg_from_socket(s) ttc.d("server step: {0}\n".format(server_step)) ttc.apply_turn(server_step, gf, ttc.SERVER_RAW_STEP) handle_winner_variable(server_step) ttc.print_game_field(gf) except KeyboardInterrupt as k: print("\nShutting down... {0}".format(k)) except Exception as exp: print(": {0}".format(exp)) ttc.print_game_field(gf) except: print("Unexpected error:", sys.exc_info()[0]) s.close() sys.exit(0)
def main(): s = ttc.get_client_socket() try: # get hello hello_msg = ttc.get_msg_from_socket(s) print("\n{0}\n".format(hello_msg)) print(''' You are a cross (X). Enter coordinats, where to put next cross. Suppose, left top corner is (0, 0). Input in format: <int> <int> <hit Return> ''') gf = copy.deepcopy(ttc.GAME_FIELD) ttc.print_game_field(gf) ### loop for a game, untill winner or ^C while True: #B get a step from user turn_json = ttc.get_turn_from_user(gf) #B send step to the server s.sendall(turn_json) #B get server answer about user step res = ttc.get_msg_from_socket(s, exception=False, ex=True) # if error - ask step again if is_error_in_answer(res): print("Ou, server not pleasent about your answer, try again.\n") continue; else: ttc.apply_turn(turn_json, gf, ttc.USER_RAW_STEP) ttc.print_game_field(gf) # check for winners in the answer, if exist any - game ends. handle_winner_variable(res) #B get server step print("Wait for server response...") server_step = ttc.get_msg_from_socket(s) ttc.d("server step: {0}\n".format(server_step)) ttc.apply_turn(server_step, gf, ttc.SERVER_RAW_STEP) handle_winner_variable(server_step) ttc.print_game_field(gf) except KeyboardInterrupt as k: print ("\nShutting down... {0}".format(k)) except Exception as exp: print(": {0}".format(exp)) ttc.print_game_field(gf) except: print("Unexpected error:", sys.exc_info()[0]) s.close() sys.exit(0)