In this article I'll show how to make a CLI application to manage an Elo ranking ladder using the CapyElo object. If you don't know about the Elo ranking system, have a look at here first.
(The complete code is available at the end of the article)
First, lets define some structures to hold all the needed data.
Calculating the new Elo rank given a result and two entity is as simple as creating the CapyElo instance:
and getting the value to add/substract to their current Elo rank:
So, almost all of the application consists only in loading the ladder and results, and saving them back.
It only needs two arguments: the file containing the ladder and the one containing the results to add. I'll add an optional argument for a "dry" mode (which shows the modification that would be done without actually doing them). The argument definition then looks as follow:
and the actual parsing is done with:
To process any problem cleanly, we'll use some purpose made exceptions:
The ExcToStr conversion function is set with:
The ladder consists of a CapyDict where keys are the entities' name, and values are a pair of value: the current rank and the rank update. It is loaded as follow:
The results consists of pair of entities' name separated by a character indicating a win or a tie. They are loaded as follow:
The results are processed line by line. The win/tie character is located and the two names identified, then the current Elo rank of each entity is gotten from the ladder, finally the Elo update is calculated and memorised. If an entity name doesn't exist in the ladder it will be automatically added thanks to the CapyDict internal behaviour.
And finally the updated ladder is saved (or displayed in dry mode) as follow:
The function used to keep the ladder sorted is as follow:
I skip intentionally releasing memory. This is done automatically at the end of the program, which is so small and simple that there is nothing to fear here. Everything put together, it becomes:
Compile with the following Makefile:
Example of use: