def get_all_units(params): all_units = [] for _ in range(int(params[1])): cmd, p = parse(receive(params[0])) while not cmd: cmd, p = parse(receive(params[0])) all_units.append(Unit.Unit(p)) time.sleep(.001) Client.done(params) return all_units
def create_map(screen, ratios): res = receive(client) print(res, PLAYER_NUMBER) if PLAYER_NUMBER == 1: a = Map(screen, [''] * (24 * 24)) # a.generate_tiles_from_ratio(*ratios) a.generate_tiles_from_config() send(client, f"GMP~{str(a)}") time.sleep(0.5) return a else: time.sleep(0.5) tiles_arr = ptr.map_parse(receive(client), client) print(tiles_arr) return Map(screen, tiles_arr)
def handle_client(client, addr): while True: try: res = receive(client) print(res) if res.split('~')[0] == 'MAP': return handle_map(client, res.split('~')[1], addr) ptr.server_parse(res, client) x = ptr.server_parse(receive(client), client) if x: print(x) except Exception as e: print(e) quit()
def handle_map(client, room, addr): try: lock.acquire() try: rooms[room].append(addr) except KeyError: rooms[room] = [addr] lock.release() while True: lock.acquire() if len(rooms[room]) == 2: lock.release() send(client, 'DON') break lock.release() while True: res = receive(client) if res: lock.acquire() print(res) for ad in rooms[room]: if ad is not addr: send(clients[ad], res) lock.release() except OSError as e: rooms[room].remove(addr) del clients[addr] for ad in rooms[room]: if ad is not addr: send(clients[ad], f"FRT") raise OSError(e)
def map_send(soc, msg): try: send(soc, msg) res = receive(client) print(f"(MAP) MSG: {msg}, RES: {res}") return ptr.map_parse(res, client) except ConnectionRefusedError: print("Server seems to be shut down.") except ConnectionAbortedError: print("Server seems to be shut down.")
def client_send(soc, msg): try: send(soc, msg) res = receive(client) print(f"MSG: {msg}, RES: {res}") return ptr.client_parse(res, client) except ConnectionRefusedError: messagebox.showerror("Connection Error", "Server seems to be shut down.") print("Server seems to be shut down.") return "ERR~11" except ConnectionAbortedError: messagebox.showerror("Connection Error", "Server seems to be shut down.") print("Server seems to be shut down.") return "ERR~11" except socket.timeout: messagebox.showerror("Connection Error", "Server seems to be shut down.") print("Server seems to be shut down.") return "ERR~11"
def handle_msgs(c): while True: res = ptr.map_parse(receive(c), c) change_units(True, res, 0)
import socket from pyticario import protocol as ptr from pyticario.network.common import receive, send IP = socket.gethostbyname(socket.gethostname()) client = socket.socket() client.connect((IP, ptr.PORT)) while True: name = input(">>> ") send(client, name) if name == "DIS": break res = receive(client) x = ptr.client_parse(res, client) if x: print(x)
def get_units_array(params): arr = [] for i in range(int(params[1])): cmd, p = parse(receive(params[0])) arr.append(Unit(*p[0].split(','))) return arr
def get_active_rooms(params): active_rooms = [] for i in range(int(params[1])): active_rooms.append(receive(params[0])) Client.done(params) return active_rooms