Kartones Blog

Be the change you want to see in this world

Zombie Infection Simulator mini-game

Over the last few days I've picked an hour here and there to build another entertaining small project. A zombie infection simular game.

Zombie Infection Simulator game screenshot

It is a port of a Java + Processing framework game with the same name by Kevan Davis to Javascript + HTML 5 <canvas> component for rendering.

I wanted to start practising some Javascript and play with canvas scaling so adapted the code and then made some small tweaks. There are a few changes and improvements that I would like to do, like more clear separation of entities and layers, or the fact the game doesn't runs at a fixed framerate (just enqueues another game loop iteration when current one finishes + optional delay), and probably many optimizations can be applied, but at least now I have a fully working version to use as the base. I'd like for example to try creating a version with graphics (also as an excuse to try Phaser.io), even if the amount of humans/zombies needs to shrink. But as I said, I now have the foundations and now can improve both the Javascript code itself and the game features whenever I can and want.

It is fun and relaxing to watch the little pixel humans panic from seeing zombies, and how the undead slowly but inexorably infect all the population (most times, the random city layout a few times creates isolated areas).

You can find the source code at my Github, or play it online from https://kartones.net/demos/026/.


Book Review: Buffettology

As part of my recent quest for reading and learning a bit about economics of our world, and by recommendation of a friend who knows at least more than me about the topic, I went for a totally different read this time.

Review

Buffettology book cover

Title: Buffettology: The Previously Unexplained Techniques That Have Made Warren Buffett The World's Most Famous Investor

Author(s): Mary Buffet, David Clark

What to buy? At what price? Seek the highest predictable annual compounding rate of return possible with the least amount of risk; benefit from the compounding effect of retained earnings, taking care about inflation and taxes; The price you pay determines your rate of return.

Previous paragraph was a small yet very relevant fragment of advices given in the book if you want to invest in a company. There are more, but these highlights provide a good summary. Written by a Buffet family member in the late nineties, explains in plain terms the basics of Warren Buffet's thinking, some of his investing techniques and a small framework to categorize, evaluate and judge if a company is worth an investment or not.

As another example, it suggests avoiding commodity business, trying to find consumer monopolies or "toll bridges", but more than 20 years later almost everyone will agree that this is not always clear to find. For example, when Google appeared wasn't a search and advertisement monopoly (and as of 2021 is losing ground on the advertisement business to Facebook and Amazon), and even companies that become part of a duopoly or oligopoly do so via insane amounts of invested money and are not yet profitable, so at least in the technological area Warren's concepts of searching for a company "with clean financials, good earnings and that is a monopoly" feels more of a holy grail (tell that to the venture capitals all over the world!).

Sorry, diverging outside of the book itself. In general, it is an interesting read, but parts of it are clearly obsolete. The listing of 54 interesting companies is a good example, despite still being interesting to read through. The authors tend to remark once and again and again a few topics, which isn't bad but becomes a bit repetitive, but even with that there are good general concepts to learn.

In my case, I'd choose the sentence "invest in companies that make products you understand" as something you can extrapolate to other areas.


Toggling Firefox dark mode via bash script

It might be just me, but all this dark theme trend, while really nice and an advancement over past attempts (I used Windows with a black theme long ago and was hard for anything other than a gaming-only computer), feels still not perfect: My Ubuntu 20.04's dark theme has an almost white notifications & calendar widget, some applications don't support it, but where I find it still inconsistent is regarding browser support; MS Edge will use the same operating system setting, and Firefox allows fine-tuning for the theme (window borders, etc.) but for the actual browsing it's a config setting of either on or off.

The property I'm talking about, as of Firefox v89 is "ui.systemUsesDarkTheme", with integer values 0/1 for disabled/enabled. And you can only change it from the advanced settings, from about:config.

That this is what I desire:

  • Operating System: Always dark theme ✓
  • Browser theme: Always dark theme ✓
  • Browser tabs dark theme detection/preference: Switchable on demand ✗

If I could have one of the following regarding the last point, I'd be happy:

  • A button to toggle ui.systemUsesDarkTheme values (enabling/disabling it whenever I want)
  • A way to create a javascript bookmarklet to toggle the value (AFAIK for security reasons you cannot change about:config settings via JS scripts)
  • A way to access the property from an extension, to build a "toggle pages dark theme" one (browserSettings looks very limited)

As none of those are available options right now, I did an alternative approach: creating a bash script that will run upon login (and on demand whenever I want), to automatically set the property at my Firefox profile preferences file (prefs.js), depending on the time of the day. It is not perfect, as you have to have Firefox closed or not only won't detect the change, but will also override when closing the value you set with whatever value it had while running, but at least works for me if I set it to run when I login.

Anyway, this is the script:

PROFILE="<your-profile-folder-name-here>"
HOUR=$(date +%H)

DARK_MODE_SETTING=0
if [ $HOUR -gt 20 ] || [ $HOUR -lt 8 ]
then
    DARK_MODE_SETTING=1
fi

sed -i "s/user_pref(\"ui.systemUsesDarkTheme\",.*);/user_pref(\"ui.systemUsesDarkTheme\",$DARK_MODE_SETTING);/" ~/.mozilla/firefox/$PROFILE/prefs.js

Company Employee Turnover Metrics

Motives of course can vary, but usually when a company has significant employee turnover it is due to one of two reasons:

  • Organizational changes (restructuring, saving costs by reducing workforce, ...)
  • Voluntary turnover (people leaving the company)

In both cases, I think it is a good metric for anyone interested in a company, whenever it is to join it, to invest on it, or simply to collaborate with it.

If there were some important restructuring you want to know about it. Did the company had poor management and it changed? Was it because it had to cut costs? or maybe because somebody decided to close a line of products without re-converting employees?

We live in a world where people are often seen as numbers. Even the department in charge of people is called "Human Resources", as if categorized similarly as hardware, transport and other types of resources available to the company. So it often happens that when you need to save money, usually you save some of it by reducing the amount of salaries to pay. I'm not going to dig into if that's always unfair or sometimes justified, just state a fact.

It is also really revealing if you can gather info of employee turnover, specially on the main taskforce. I'll exemplify with a tech company: do engineers, product managers and UX people leave almost every month, or is it instead considered an infrequent event the action of somebody leaving the company? do employees on average stay for many years, or is the rotation high? and if the rotation is high, is it only employees that had been for some time, or also there's a number of new hires that don't last much?

And even more revealing (IMHO at least) could be to weight the seniority of the people leaving vs the senior roles that have been at the company for long. If there's a sudden stampede of senior roles that look relevant for the company, either they are going to build something together, or something smells off. If a place has mostly "fresh" senior roles, what made all that old guard go away?

Again, this might not be bad on itself. Maybe the company was in desperate need of fresh air and change, and the new blood will bring radical new ideas and fix things up. But I think it would always be relevant to know what happened, what triggered that disruption.

All this rant comes because I was thinking the other day about how cool would be to be able to analyze employee turnover, because we have a great source of data... the problem is that it lives inside a walled garden: LinkedIn. Or maybe I should say electrified-fenced, as it is against their terms of use to do any web scrapping (and almost almost all data is not publicly available). It would be great to have an API to fetch, given a company, a few data of its employees, like the role and company tenure. Add to that searching for ex-employees, checking when they left, the role they had and how long did they stayed at the company. Everything else could be anonymized or not even returned, so really is not much of a privacy problem. But LinkedIn APIs are almost useless, and then even the company pages require being logged-in to check, so we have all such valuable information, but we can't explore it.

It's sad how the internet (exceptions like Wikipedia or Archive.org aside) has evolved into a group of huge, closed "information warehouses". Sure most of them are "free" (we pay with our data), but we can only use it in certain, limited ways imposed to us.


ctypes, OpenCV and Pillow

Note: This is a work in progress, consider this post more like some development notes of sources and links and references that I've found interesting. I might throw away half of it, or never finish the experiment.

What do ctypes, OpenCV-python and Pillow have in common, apart from being Python libraries? In my case they form a nice pack to begin tinkering with automation in less usual scenarios... like videogames.

After finding an amazing tutorial on how to teach an AI to drive cars in GTA V with Python, I thought I could start maybe with something simpler. Like an auto-clicker for those idle & clicker videogames.

I am using Pillow because its ImageGrab module is perfect to capture screenshots (with bounding box directly, so no need to crop anything), and it's a single line of code to transform the image into an optimized NumPy array... which is also the source for many OpenCV operations.

Regarding OpenCV, I'm just learning it as I go, half by reading its tutorials and half by directly playing with things I've seen in the GTA V AI article, like changing colorspaces and canny edge detection. I am still toying with which approach is better (grayscale or HSV), but I already have a working prototype that draws edges of coins as a black & white mask, and then by reading pixel data I'm able to pinpoint them in screen.

Handling input is becoming actually the hardest part, as I want to build it for Linux (there are lots of articles on ctypes and Windows DirectInput) and I'm not familiar with the X Window System. Thankfully the X11 library documentation contains all calls and I already have implemented querying the mouse position (XQueryPointer) and moving it to wherever I want (XWarpPointer), so I see where it detects entities better than just raw X,Y coordinates.

I am missing how to send mouse clicks, and I'd like to play with OpenCV a bit more to try to really detect items within it (right now is raw array comparisons).

I don't know if to publish the code, partly to not ease cheating on those games (which in the end you play for free) and party because excepting the input handling code, everything else are really basic operations with each library that you can find on their respective official documentation or tutorials.

In any case, here's one screenshot of a debug frame mask with edge detection. The game was running and already past the equivalent frame, but the goal is to automatically click to pick up all gold coins enemies leave upon dying:

A clicker game and OpenCV edge mask frame


Previous entries