Added max_fuel, model_id and fuel usage

This commit is contained in:
Emily 2019-08-01 00:12:38 +02:00
parent 59709ec34c
commit 7df8222cb4

View File

@ -10,9 +10,9 @@ flights = {}
clients = set()
STRUCTS = {
"start": b"IB24s",
"start": b"fHB24s",
"end": b"",
"flight_data": b"?BHhHff",
"flight_data": b"2BHhH3f",
"server_open": b"",
"server_close": b""
}
@ -145,25 +145,35 @@ async def handle(ws, path):
logging.info("<ws://%s:%d/%s> Disconnected." % (*ws.remote_address, path))
class Flight:
def __init__(self, data):
(self.uuid, self.playername_len, self.playername) = struct.unpack(b"<" + STRUCTS["start"], data[:29])
def __init__(self, uuid, data):
self.uuid = uuid
(
self.max_fuel,
self.model_id,
self.playername_len,
self.playername
) = struct.unpack(b"<" + STRUCTS["start"], data[:31])
self.active = True
self.last_timeline_values = [None] * 7
self.last_timeline_values = [None] * 8
self.timeline = b""
logging.info("[%d] Flight started" % self.uuid)
asyncio.ensure_future( broadcast_flight_change(self.uuid, self.active) )
def get_all(self):
return struct.pack(b"<" + STRUCTS["start"],
def get_head(self):
return struct.pack(b"<I" + STRUCTS["start"],
self.uuid,
self.max_fuel,
self.model_id,
self.playername_len,
self.playername,
) + self.timeline
self.playername)
def get_all(self):
return self.get_head() + self.timeline
def add(self, data):
data = [*struct.unpack(b"<" + STRUCTS["flight_data"], data[:16])]
data = [*struct.unpack(b"<" + STRUCTS["flight_data"], data[:20])]
for i in range(len(data)):
if data[i] == self.last_timeline_values[i]:
data[i] = None
@ -172,7 +182,7 @@ class Flight:
frame = self.format_flight_data(data)
if not frame:
logging.debug("[%d] Empty frame (skipping)" % self.uuid)
logging.debug("[%d] Empty frame (skipping)" % self.uuid) # Should not be possible?
return
logging.debug("[%d] New frame: %s" % (self.uuid, frame))
@ -181,13 +191,12 @@ class Flight:
self.timeline += frame
@staticmethod
def format_flight_data(data):
def format_flight_data(data): # Compresses data by setting a flag of what data has changed
ret = b""
flag = 0
for i in range(len(data)):
if data[i] != None:
flag |= 1<<i
if i != 0: # We dont include the paused bool (?) in the data as we keep it in the flag instead
ret += struct.pack(b"<" + bytes([STRUCTS["flight_data"][i]]), data[i])
if flag == 0: # Empty frame; Nothing changed so we dont even add a timestamp
@ -237,8 +246,8 @@ class DiscoveryProtocol(asyncio.DatagramProtocol):
@staticmethod
def handle_start(data):
flight = Flight(data)
flights[flight.uuid] = flight
(uuid, data) = (*struct.unpack(b"<I", data[:4]), data[4:])
flights[uuid] = flight = Flight(uuid, data)
logging.debug("Added %d into flights dict" % flight.uuid)
@staticmethod