def edit(): data = receive_message(sock)[0] print(data) data = receive_message(sock)[0] print(data) command = validate_input("Input merge to merge all similar pokemon for higher XP or quit: ", ["merge", "quit"]) send_message(command, ("localhost", edit_port), sock) if command == "merge": data = receive_message(sock)[0] print(data)
def battle_preparation(self, player_chosen_pokemons): for i in range(0, 2): send_message(message=str( len(self.players[i].player_info["pokemons_info"])), address=self.players_address[i], sock=self.sock) send_message(message=self.players[i].get_pokemon_info(), address=self.players_address[i], sock=self.sock) count = 0 while count < 2: data, address = receive_message(self.sock) index = [int(x) for x in data.split()] pokemons = [] if address == self.players_address[0]: for i in index: pokemon = Pokemon( self.players[0].player_info["pokemons_info"][i]) pokemon.prepare_battle() pokemons.append(pokemon) player_chosen_pokemons[0] = pokemons else: for i in index: pokemon = Pokemon( self.players[1].player_info["pokemons_info"][i]) pokemon.prepare_battle() pokemons.append(pokemon) player_chosen_pokemons[1] = pokemons count += 1 player_chosen_pokemons[0].sort(key=lambda k: k.info['current_speed'], reverse=True) player_chosen_pokemons[1].sort(key=lambda k: k.info['current_speed'], reverse=True)
def pokecat_client(): print("Welcome to pokecat. You can travel using keys: a w s d for left, up, down, right.\n" "You are the X in the mini-map, O are pokemons. Input quit to quit game.") mini_map = receive_message(sock)[0] print(mini_map) while True: command = validate_input("", ["quit", "a", "w", "s", "d"]) send_message(command, ("localhost", pokecat_port), sock) if command != "quit": result = receive_message(sock)[0] print(result) mini_map = receive_message(sock)[0] print(mini_map) else: result = receive_message(sock)[0] print(result) break
def listen(self): command, address = receive_message(self.sock) if command == "merge": self.address_player[address].merge_all_similar_pokemon( self.pokemon_count[address]) self.address_player[address].save_progress() send_message(self.address_player[address].get_pokemon_info(), address, self.sock) self.address_player.pop(address) self.pokemon_count.pop(address) else: self.address_player.pop(address) self.pokemon_count.pop(address)
def execute_game(self): thread1 = Thread(target=self.allocate_pokemon, args=()) thread1.start() thread2 = Thread(target=self.queue_handling, args=()) thread2.start() while True: data, address = receive_message(sock=self.sock) if data == "quit": self.ip_players[address].save_progress() send_message("Save success", address, self.sock) self.ip_players.pop(address, None) self.player_locations.pop(address, None) elif re.match('^[asdw]$', data): self.queue.put((3, data, address)) self.queue.put((4, address))
def handle_client_verification(private_socket, client_address): option = receive_message(private_socket)[0] if option == '1': while True: user_name = receive_message(private_socket)[0] password = receive_message(private_socket)[0] if Player.is_user_name_exist(user_name) or user_name == "": send_message( "Choose another user name. Already exists or blank.", client_address, private_socket) else: Player.add_user(user_name, password) send_message("Success", client_address, private_socket) print(user_name + " registered and login successfully") break elif option == '2': while True: user_name = receive_message(private_socket)[0] password = receive_message(private_socket)[0] if Player.is_login_correct(user_name, password): send_message("Success", client_address, private_socket) print(user_name + " login successfully") break else: send_message("Incorrect", client_address, private_socket) option = receive_message(private_socket)[0] if option == '1': pokecat_instance.add_player(client_address, user_name) elif option == '2': player = Player(user_name) if len(player.player_info["pokemons_info"]) >= 3: send_message("In player queue", client_address, private_socket) player_queue.put(Player(user_name)) player_address_queue.put(client_address) else: send_message("Not enough pokemon", client_address, private_socket) elif option == '3': edit_room.add_player(client_address, user_name) private_socket.close()
def play_turn(self, player_turn, player_chosen_pokemons): opponent = (player_turn + 1) % 2 send_message("Your turn", self.players_address[player_turn], self.sock) send_message("Your opponent's turn", self.players_address[opponent], self.sock) for i in range(0, 2): send_message( "Your current pokemon is: " + player_chosen_pokemons[i][0].info["name"], self.players_address[i], self.sock) send_message( "The opponent current pokemon is: " + player_chosen_pokemons[opponent][0].info["name"], self.players_address[player_turn], self.sock) if player_chosen_pokemons[player_turn][0].is_lost(): send_message( "You need to switch pokemon. Current one is dead. Or you can quit", self.players_address[player_turn], self.sock) command = receive_message(self.sock)[0] if command == "quit": send_message("Your opponent quit", self.players_address[opponent], self.sock) self.end_game(opponent, player_turn, player_chosen_pokemons) message = self.get_switchable_pokemons(player_chosen_pokemons, player_turn) send_message(message, self.players_address[player_turn], self.sock) swap_idx = int(receive_message(self.sock)[0]) if not player_chosen_pokemons[player_turn][swap_idx].is_lost(): player_chosen_pokemons[player_turn][0], player_chosen_pokemons[player_turn][swap_idx] = \ player_chosen_pokemons[player_turn][swap_idx], player_chosen_pokemons[player_turn][0] send_message("Successful switch", self.players_address[player_turn], self.sock) else: send_message("Unsuccessful switch", self.players_address[player_turn], self.sock) send_message("Your opponent choose switch", self.players_address[opponent], self.sock) else: send_message("You can attack or switch or quit current pokemon.", self.players_address[player_turn], self.sock) command = receive_message(self.sock)[0] if command == 'attack': dmg = player_chosen_pokemons[player_turn][ 0].attack_other_pokemon( player_chosen_pokemons[opponent][0]) send_message( "You caused " + str(dmg) + " to opponent current pokemon", self.players_address[player_turn], self.sock) send_message( "Your current pokemon received a damage of " + str(dmg), self.players_address[opponent], self.sock) elif command == 'switch': message = self.get_switchable_pokemons(player_chosen_pokemons, player_turn) send_message(message, self.players_address[player_turn], self.sock) if message == "You can't switch pokemon because there are none left. You lost a turn": pass else: swap_idx = int(receive_message(self.sock)[0]) if not player_chosen_pokemons[player_turn][ swap_idx].is_lost(): player_chosen_pokemons[player_turn][0], player_chosen_pokemons[player_turn][swap_idx] = \ player_chosen_pokemons[player_turn][swap_idx], player_chosen_pokemons[player_turn][0] send_message("Successful switch", self.players_address[player_turn], self.sock) else: send_message("Unsuccessful switch", self.players_address[player_turn], self.sock) send_message("Your opponent choose switch", self.players_address[opponent], self.sock) elif command == 'quit': send_message("Your opponent quit", self.players_address[opponent], self.sock) self.end_game(opponent, player_turn, player_chosen_pokemons)
def pokebat_client(): data = receive_message(sock)[0] print(data) if data == "Not enough pokemon": return print("Waiting for room...") number_of_poke, pokebat_server_address = receive_message(sock) number_of_poke = int(number_of_poke) poke_info = receive_message(sock)[0] print("Enter 3 distinct value from 0 to " + str(number_of_poke - 1) + "to choose pokemons") print(poke_info) indices = [] while len(indices) < 3: try: num = int(input("Input " + str(len(indices) + 1) + " number from 0 - " + str(number_of_poke) + ": ")) if num >= number_of_poke: print("Too big") pass elif num in indices: print("Exisited") else: indices.append(num) except ValueError: print("Incorrect format") pass result = "" for i in indices: result += str(i) + " " send_message(result, pokebat_server_address, sock) while True: turn = receive_message(sock)[0] print(turn) if turn == "Your turn": current_pokemon = receive_message(sock)[0] print(current_pokemon) current_opponent_pokemon = receive_message(sock)[0] print(current_opponent_pokemon) command = receive_message(sock)[0] print(command) if command == "You need to switch pokemon. Current one is dead. Or you can quit": command = validate_input("Enter quit or attack: ", ["quit", "switch"]) send_message(command, pokebat_server_address, sock) if command == "quit": continue switchable = receive_message(sock)[0] print(switchable) swap_idx = validate_input("Input the pokemon you want to swap: ", ["1", "2"]) send_message(swap_idx, pokebat_server_address, sock) result = receive_message(sock)[0] print(result) else: command = validate_input("Enter your command: attack or switch or quit: ", ["attack", "quit", "switch"]) send_message(command, pokebat_server_address, sock) if command == "switch": switchable = receive_message(sock)[0] print(switchable) swap_idx = swap_idx = validate_input("Input the pokemon you want to swap: ", ["1", "2"]) send_message(swap_idx, pokebat_server_address, sock) result = receive_message(sock)[0] print(result) elif command == "attack": result = receive_message(sock)[0] print(result) else: continue elif turn == "Your opponent's turn": current_pokemon = receive_message(sock)[0] print(current_pokemon) result = receive_message(sock)[0] print(result) else: break
send_message(command, ("localhost", edit_port), sock) if command == "merge": data = receive_message(sock)[0] print(data) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) port = randint(0, 65535) try: sock.bind(("localhost", port)) except socket.error: print("Error creating socket") sys.exit() print("Client started on port " + str(port)) send_message("Please provide socket", ("localhost", listening_port), sock) data = receive_message(sock)[0] verification_port = int(data) server_address = ("localhost", verification_port) option = validate_input("1 for register. 2 for login: "******"1", "2"]) send_message(option, ("localhost", verification_port), sock) while True: user_name = input("Input user name: ") password = input("Input password: "******"localhost", verification_port), sock) send_message(password, ("localhost", verification_port), sock) respond = receive_message(sock)[0] print(respond) if respond == "Success": break option = validate_input("1 for pokecat. 2 for pokebat. 3 for edit profile. quit for exit: ", ["1", "2", "3", "quit"]) send_message(option, ("localhost", verification_port), sock)
edit_profile_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) listening_sock.bind(("localhost", listening_port)) pokecat_sock.bind(("localhost", pokecat_port)) edit_profile_sock.bind(("localhost", edit_profile_port)) pokemons_dicts = Pokemon.get_all_base_pokemon_dicts() pokecat_instance = PokeCat(pokecat_sock, len(pokemons_dicts), pokemons_dicts) pokecat_thread = Thread(target=pokecat_instance.execute_game, args=()) pokecat_thread.start() pokebat_thread = Thread(target=handle_player_queue_for_pokebat, args=()) pokebat_thread.start() edit_room = EditPlayerProfile(edit_profile_sock) edit_thread = Thread(target=edit_room.listen, args=()) edit_thread.start() print("Server started on port 100") while True: data, address = receive_message(listening_sock) print(str(address) + ":" + data) new_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) new_port = 0 while True: new_port = randint(0, 65535) try: new_sock.bind(("localhost", new_port)) break except socket.error: pass send_message(str(new_port), address, listening_sock) new_thread = Thread(target=handle_client_verification, args=(new_sock, address)) new_thread.start()