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