Scores
The high-score table.
Everything in this module that decides what the table contains is pure and needs no
handler at all -- parsing, rendering, ordering and qualification are ordinary functions
with ordinary tests. Only load and save touch a filesystem, and they are called from
main alone.
Type Aliases
Where the table is kept: the file, and the directory that must exist to hold it.
Both, rather than just the file, because saving has to create the directory on a first run. Carrying the directory alongside is honest about what was built; recovering it by hunting for the last separator in the path would be guesswork about a string we assembled ourselves.
Definitions
Returns the highest score value currently in the high score table, or 0 if empty.
Cleans a score-table name using the same rules as interactive initials entry.
Inserts a new high score entry into the table and returns the re-ranked list.
Reads the table, or an empty one if there is not a file yet.
A missing file is the ordinary first run and yields Ok(Nil). Every other error is
returned rather than swallowed: a table you have no permission to read is not the same
as no scores, and quietly treating it as one would let the next save overwrite
somebody's table with nothing.
Where the table lives, by the XDG convention.
$XDG_CONFIG_HOME/flix-invaders when that is set, otherwise
~/.config/flix-invaders. None on a machine with neither -- there is nowhere
sensible to put it, and inventing somewhere is worse than going without.
Parses a list of lines from disk into a ranked high score table.
Reads one line of the stored format, NAME|SCORE|LEVEL.
A line that does not parse is dropped rather than fatal: a truncated or hand-edited file should cost you the table, not the game.
Whether score earns a place.
A zero never qualifies, however empty the table -- there is no dignity in immortalising a game you did not play.
Renders a list of high score entries into serializable line strings.
Cleans a typed string: forces uppercase, strips anything outside A-Z and 0-9, and
caps the length at nameLength().
Used when the player enters initials. Scores.load relies on this to ensure bad lines
in the file cannot break the cabinet view.