import sys import socket import struct import pathlib import logging from objects import glob from handlers.mainHandler import STRUCTS sock_addr = (glob.config["socket"]["ip"] if glob.config["socket"]["ip"] != "0.0.0.0" else "127.0.0.1", glob.config["socket"]["port"]) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.connect(sock_addr) sock.setblocking(0) def send_packet(type, data = b""): global sock packet_id = list(STRUCTS.keys()).index(type) + 1 data = struct.pack(b"<3sB%ds" % len(data), b"FLY", packet_id, data) logging.info("<< %s" % data) sock.send(data) def sinput(p = "> "): s = input(p) if s in ["exit", "close", "stop"]: send_packet("server_close") exit() return s def srvr(): keys = list(STRUCTS.keys()) def choose_packetType(): print("Choose packetID to send") for i in range(len(keys)): print(" %d: %s" % (i + 1, keys[i])) id = 0 while id > len(keys) or id < 1: try: id = int( sinput() ) except SystemExit or KeyboardInterrupt: exit() except: pass return id def send_data(id): def _start(): ret = ( int( input(" FlightUUID (I) [DWORD] > ") ), float( input(" VehicleMaxFuel (f) [FLOAT] > ") ), int( input(" VehicleModelID (H) [WORD] > ") ), input(" PlayerName (%ds) [STRING] > ").encode() ) send_packet("start", struct.pack(b" ") ), int( input(" Flags 0-3 (B) [BYTE] > ") ), int( input(" Satisfaction (B) [BYTE] > ") ), int( input(" Speed (H) [WORD] > ") ), int( input(" Altitude (h) [WORD] > ") ), int( input(" Health (H) [WORD] > ") ), float( input(" FuelLevel (f) [FLOAT] > ") ), float( input(" X pos (f) [FLOAT] > ") ), float( input(" Y pos (f) [FLOAT] > ") ) ) send_packet("flight_data", struct.pack(b" ") ), ) send_packet("end", struct.pack(b"