fly-socket/serv_emu.py

104 lines
3.3 KiB
Python
Raw Normal View History

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"<I" + STRUCTS["start"] % 24,
ret[0], # flightUUID
ret[1], # vehicleMaxFuel
ret[2], # vehicleModelID
len( ret[3] ), # playerNameLength
ret[3] # playerName
)
)
def _flight_data():
ret = (
int( input(" FlightUUID (I) [DWORD] > ") ),
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"<I" + STRUCTS["flight_data"], *ret) )
def _end():
ret = (
int( input(" FlightUUID (I) [DWORD] > ") ),
)
send_packet("end", struct.pack(b"<I" + STRUCTS["end"], *ret))
def _server_open():
send_packet("server_open")
def _server_close():
send_packet("server_close")
switch = {
1: _start,
2: _flight_data,
3: _end,
4: _server_open,
5: _server_close
}
switch.get(id)()
send_packet("server_open")
while True:
id = choose_packetType()
send_data(id)
srvr()