24 November, 2020

Counter-Strike parental control app for geeks

Creating a small mobile-friendly MQTT-based solution to monitor and manage your kid’s gaming habits.

This spring I noticed that my 12-year-old son started to look tired and on weekends I had trouble waking him up in the morning. Initially, I thought that this is due to the lack of vitamins and began to send him to sleep earlier. But this didn’t help. He continued to behave abnormally.

kid sleep on book

Once in the morning I decided to check the statistics of my Wi-Fi router and detected high traffic consumption at night. I showed these stats to my son and we decided that probably we had gnomes in the house that awaken at night. One of our suspicions was the Huawei phone that we thought could have initiated an intensive data exchange with the Chinese servers late at night. To eliminate this possibility, I started to completely turn off the Wi-Fi at home for the whole night.

wi-fi off

Well, it worked but was quite annoying. Every time you go to sleep you turn off the Wi-Fi. Every morning you wake up and turn it on. It is a repeated operation that is the subject for automation and also I wanted the router to disconnect only some devices at night. Or better leave an active Internet connection for my phone and laptop only. I even bought a special micro PC box with Debian Linux onboard which can handle all home traffic and provide a backup Internet connection via GSM network. I could easily reprogram this small box to serve as a cool home server doing all kinds of automation around the house.

But later I discovered that the reason for the huge night traffic is not a Chinese phone but an ordinary Counter-Strike game, which playing hours moved from night to daytime after my new Wi-Fi disabling policy. 

counter strike poster

So the problem became even more convoluted — I couldn’t turn the Internet connection on and off remotely and I couldn’t completely disable the Internet access for the whole day for my son. In a search for the ideal solution, I looked towards flespi and a bright idea crossed my head — how about creating a small home automation system that could control Counter-Strike launch possibility and log played hours which are accumulated for the whole year? And all these for the backend developer — i.e. I didn’t want to create any GUI.

Yes, it sounds quite complex but in reality, it took me one Sunday to accomplish this task using flespi, including one hour to tune MQTT Tiles to create an easy-to-use mobile-friendly, and secure GUI.

The scheme is straightforward. I implemented a script daemon that auto-started on my son’s Linux computer. This script daemon named steamctl was implemented using Lua programming language as the easiest language with the embedded libraries to access flespi REST and MQTT services.

counter strike play time control app

Steamctl connects to the flespi MQTT Broker and subscribes to the appropriate topic “steamctl/control/${hostname}” where it receives the current status of Counter-Strike permission for the given ${hostname}. It expects a message with payload “true” to enable Counter-Strike; otherwise, it will be disabled. If the game is disabled, once the steamctl detects it is running the process is immediately killed.

The topic “steamctl/control/${hostname}” is controlled directly by MQTT Tiles dashboard — you can use a toggle button to read the current state and change it with the mouse/finger click.

Another part of the steamctl daemon each minute checks if the Counter-Strike steam application is running at the moment and reports actual status to the flespi MQTT Broker to the topic “steamctl/status/${hostname}”. To avoid losing the information even when no Internet is available at the given moment, I used MQTT Broker REST API for publishing status messages directly to the broker with a local durable persistent cache of ongoing REST calls.

From the flespi side, the status messages are gathered by the flespi MQTT channel which then translates the message JSON and saves it to the appropriate flespi device. On top of it, I created a flespi calculator that calculates the total time played in the current day and assigned the device to it. 

In that setup I have everything in MQTT now: control button, current game status available as device telemetry, and calculated game duration for the current day. 

To control and visualize it I created a special dashboard in the MQTT Tiles application — simple yet powerful and mobile-friendly:

mqtt tiles parental control dashboard

After implementing the above scenario, I can enable and disable Counter-Strike at any time and from anywhere in the world. I could have added special processing to automatically disable Counter-Strike once a specified amount of hours has been played, but I went a different way and provided my son read-only access to the same dashboard via ACL token so that during the game he can open his phone and check how many minutes he played and how much is left.

For those who are interested in the technical part of this mini-project, I attach the lua scripts making up the steamctl daemon. Be aware that it uses some flespi proprietary libraries and can not be used directly. Just to get an idea of the amount of code you need:

  • steamctl.lua — primary process, controlling the game

  • status.lua — secondary module reporting Counter-Strike status to the MQTT Broker

***

As a result, I have an extremely motivated child who now always finishes his duties first and plays Counter-Strike only afterward. I can behave like a dictator and control him from any location in the world from a smartphone. I remember one day I enabled the game in the air, flying from Lithuania to Estonia on the weekend. Worked like a charm!