R-Type
Networked shoot'em up built on Aegis2D
Loading...
Searching...
No Matches
rtype::Scoreboard Class Reference

The max_entries best scores, best first, kept in a JSON file. More...

#include </__w/project-rtype/project-rtype/server/include/rtype/scoreboard.hpp>

Public Member Functions

 Scoreboard (std::filesystem::path file)
 Reads the board of a file.
 
bool record (std::string_view name, std::uint32_t score)
 Adds a score, if it is among the best.
 
const std::vector< ScoreEntry > & best () const noexcept
 The best scores.
 
const std::optional< std::string > & load_error () const noexcept
 Why the file could not be read at start.
 
const std::filesystem::path & file () const noexcept
 Where the board is kept.
 

Static Public Attributes

static constexpr std::size_t max_entries = 10
 Most scores kept.
 

Detailed Description

The max_entries best scores, best first, kept in a JSON file.

The file is {"scores": [{"name": "R9", "score": 1200}, ...]}. It is read once by the constructor: a missing file is an empty board; a file that cannot be read or is not a board (bad JSON, a field of the wrong type, a bad name) is reported by load_error() and logged, and the board starts empty, which never stops the server. Each record() that changes the board writes it at once, through a temporary file that then replaces the old one (core::Config::save()), so a crash never leaves half a file; a write that fails is logged and the board goes on in memory. Not thread-safe.

Constructor & Destructor Documentation

◆ Scoreboard()

rtype::Scoreboard::Scoreboard ( std::filesystem::path  file)
explicit

Reads the board of a file.

Parameters
fileWhere the board is kept; created at the first record.

Member Function Documentation

◆ best()

const std::vector< ScoreEntry > & rtype::Scoreboard::best ( ) const
noexcept

The best scores.

Returns
At most max_entries, best first.

◆ file()

const std::filesystem::path & rtype::Scoreboard::file ( ) const
noexcept

Where the board is kept.

Returns
The file given to the constructor.

◆ load_error()

const std::optional< std::string > & rtype::Scoreboard::load_error ( ) const
noexcept

Why the file could not be read at start.

Returns
The reason, or std::nullopt if it was read or missing.

◆ record()

bool rtype::Scoreboard::record ( std::string_view  name,
std::uint32_t  score 
)

Adds a score, if it is among the best.

An equal score goes after the ones already there.

Parameters
nameName of the player; an invalid name is not recorded.
scoreIts points.
Returns
true if the score is on the board, and the file was written.

Member Data Documentation

◆ max_entries

constexpr std::size_t rtype::Scoreboard::max_entries = 10
staticconstexpr

Most scores kept.


The documentation for this class was generated from the following file: