flix

0.81.0

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

type alias Location = { dir = String, file = String }§ Source

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

def best(entries: List[Entry]): Int32§ Source

Returns the highest score value currently in the high score table, or 0 if empty.

def clean(input: String): String§ Source

Cleans a score-table name using the same rules as interactive initials entry.

def empty(): List[Entry]§ Source

An empty high score table with no entries.

def insert(entry: Entry, entries: List[Entry]): List[Entry]§ Source

Inserts a new high score entry into the table and returns the re-ranked list.

def load(at: Location): Result[IoError, List[Entry]] \ FileRead§ Source

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.

def location(): Option[Location] \ Env§ Source

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.

def nameLength(): Int32§ Source

How many characters an entry's name may have.

def padded(name: String): String§ Source

Pads a name out to the full width so the table lines up.

def parse(lines: List[String]): List[Entry]§ Source

Parses a list of lines from disk into a ranked high score table.

def parseLine(line: String): Option[Entry]§ Source

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.

def places(): Int32§ Source

How many places the table keeps.

def qualifies(score: Int32, entries: List[Entry]): Bool§ Source

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.

def ranked(entries: List[Entry]): List[Entry]§ Source

Sorted best first and capped at places.

def render(entries: List[Entry]): List[String]§ Source

Renders a list of high score entries into serializable line strings.

def sanitize(input: String): String§ Source

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.

def save(at: Location, entries: List[Entry]): Result[IoError, Unit] \ FileWrite§ Source

Writes the table, creating the directory if this is the first run.

mkDirs is content when the directory already exists, so there is no existence check here -- one would be redundant, and racy besides.