refactor: merge FH4+FH5 into 'forza_horizon', add separate FH6 game plugin
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import struct
|
||||
import time
|
||||
from server.telemetry.data import TelemetryData
|
||||
|
||||
|
||||
def get_parser():
|
||||
return ForzaHorizon6Parser()
|
||||
|
||||
|
||||
class ForzaHorizon6Parser:
|
||||
def game_id(self) -> str:
|
||||
return "forza_horizon_6"
|
||||
|
||||
def parse(self, data: bytes, addr: tuple) -> TelemetryData:
|
||||
td = TelemetryData(game_id="forza_horizon_6", timestamp=time.time())
|
||||
td.raw = {"raw_hex": data.hex(), "length": len(data), "addr": f"{addr[0]}:{addr[1]}"}
|
||||
|
||||
if len(data) < 312:
|
||||
return td
|
||||
|
||||
try:
|
||||
td.raw["is_race_on"] = struct.unpack_from("<i", data, 0)[0]
|
||||
td.max_rpm = struct.unpack_from("<f", data, 8)[0]
|
||||
td.rpm = struct.unpack_from("<f", data, 16)[0]
|
||||
td.acceleration_x = struct.unpack_from("<f", data, 20)[0]
|
||||
td.acceleration_y = struct.unpack_from("<f", data, 24)[0]
|
||||
td.acceleration_z = struct.unpack_from("<f", data, 28)[0]
|
||||
|
||||
td.raw["car_group"] = struct.unpack_from("<I", data, 232)[0]
|
||||
td.raw["smashable_vel_diff"] = struct.unpack_from("<f", data, 236)[0]
|
||||
td.raw["smashable_mass"] = struct.unpack_from("<f", data, 240)[0]
|
||||
|
||||
td.position_x = struct.unpack_from("<f", data, 244)[0]
|
||||
td.position_y = struct.unpack_from("<f", data, 248)[0]
|
||||
td.position_z = struct.unpack_from("<f", data, 252)[0]
|
||||
td.speed_kmh = struct.unpack_from("<f", data, 256)[0] * 3.6
|
||||
td.speed_mph = td.speed_kmh * 0.621371
|
||||
td.horsepower = struct.unpack_from("<f", data, 260)[0] / 745.7
|
||||
td.torque = struct.unpack_from("<f", data, 264)[0]
|
||||
td.boost = struct.unpack_from("<f", data, 284)[0]
|
||||
td.fuel = struct.unpack_from("<f", data, 288)[0]
|
||||
td.best_lap = struct.unpack_from("<f", data, 296)[0]
|
||||
td.last_lap = struct.unpack_from("<f", data, 300)[0]
|
||||
td.lap_time = struct.unpack_from("<f", data, 304)[0]
|
||||
td.lap_number = struct.unpack_from("<H", data, 312)[0]
|
||||
td.raw["race_position"] = struct.unpack_from("<B", data, 314)[0]
|
||||
td.throttle = struct.unpack_from("<B", data, 315)[0] / 255.0
|
||||
td.brake = struct.unpack_from("<B", data, 316)[0] / 255.0
|
||||
td.clutch = struct.unpack_from("<B", data, 317)[0] / 255.0
|
||||
td.handbrake = struct.unpack_from("<B", data, 318)[0] / 255.0
|
||||
td.gear = struct.unpack_from("<B", data, 319)[0]
|
||||
td.steering = max(-1.0, min(1.0, struct.unpack_from("<b", data, 320)[0] / 127.0))
|
||||
except struct.error:
|
||||
pass
|
||||
|
||||
return td
|
||||
Reference in New Issue
Block a user