Side project: Improving my lap times with PHP



This article is part one of what I hope to be a series. Come back soon for future updates!

When I'm not working, I can normally be found sim racing on F1 2020. There's no better way to let off steam than to jump into a virtual race and pull off some mad overtakes on virtual Max Verstappen.

In the real world of F1, each and every driver has a team of data analysts, helping them to get the most out of the car. It's their job to interpret live data feeds about the status of the race and tell the driver what they need to do to go faster. Wouldn't it be cool if virtual racers could access to the same kind of live data as they're driving? Well, it turns out they can!

Many popular racing sims (like iRacing, Project Cars, and the F1 franchise) can send information about the current state of the game over the network as a UDP stream. This includes car telemetry (like the position of the steering wheel, accelerator and brakes), weather data, lap timings, and a whole lot more.

While there are existing apps that take advantage of this data, most lack important features (like the ability to visualise multiple laps of data) or are just too expensive for a casual gamer. So I decided to write my own - how hard can it be?

Step 1: understanding the data

The first part of my challenge (and the only part completed so far!) is parsing the UDP packets from the F1 game.

Now I doubt many developers would opt for PHP to parse a binary stream, but it's the language I'm most familiar with. Simultaneously working on an unfamiliar problem while also trying to learn a different language would only be making things even harder for myself.

The data comes through as a raw binary stream - a long list of ones and zeros with no delineation between the different values. In PHP, the inbuilt unpack() function can turn this into "ordinary" integers, floats and strings for us to work with. However, in order to interpret each value correctly, the unpack() function needs a specification of what the stream contains. Thankfully, Codemasters publish a specification for the F1 2020 UDP packets on their forum.

The geeky bit

To represent the game data in PHP, I wrote a series of models. Each model represents one of the 9 different packets the game might send. Each model takes advantage of PHP 7.4 typed properties to denote the data type of each value. Since PHP data types do not have a specific size, custom annotation to denotes the specific type, such as an 8-bit unsigned integer.

On receipt of a data stream, it works like this:

If you're a PHP developer and would like to give it a go, check out my sim telemetry parsing library on GitHub.

What's next?

Now the parser library is ready, my next step is to build a server application that uses it to parse the messages and store them. I'll be publishing an update here as soon as I have one!

Recent Posts

Side project: Improving my lap times with PHP

Why this site scores 100% for performance in Lighthouse

← Back to all posts