mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-27 21:45:16 +01:00
feat(odometer): stamp each state with the day it was last updated, drop states not updated for a year (#435)
* feat(odometer): stamp the states with the day they were last updated, drop those not updated for a year The odometer store kept every vehicle ever run. Each state now carries the day it was last updated, written as a comment line "# updated <yyyy-mm-dd>" before the states of that day, which the builds without the stamps skip. A state with no stamp counts as updated the day it is read. A save drops the states not updated for more than 365 days, those the other simulations saved included. Refs #283. * test(odometer): cover the stamps of the states and their expiry * fix(odometer): a wrong clock doesn't drop the states, odometer.expiry sets the period Review of #435. A stamp later than today was taken as today on every read, so a session with the clock set back rewrote every state with that day, and the next session with the right clock dropped them all. The stamps are now kept as written and a recorded state keeps its later day, so a day never goes back. The expiry was measured from today, so a clock set forward, or the first save after a break of a year, dropped every state not run in that session. It is now measured from the latest day of the states read at the start of the session when that is earlier than today. The states this session records are stamped today, so the newest day of the merged store would always be today in a save that writes. The period is odometer.expiry <days> in eu07.ini, 365 unless set, 0 for never. A scenery's config block doesn't change it, it would drop the player's states. The states dropped are logged by the save that writes the file without them, not at every save while the write fails. as_written() reads with the day given to save().
This commit is contained in:
@@ -15,13 +15,17 @@ http://mozilla.org/MPL/2.0/.
|
||||
#include "utilities/utilities.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <charconv>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <format>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <string_view>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#ifdef _WIN32
|
||||
#include <process.h>
|
||||
#else
|
||||
@@ -62,8 +66,61 @@ std::optional<double> parse_number(std::string const &Text) {
|
||||
}
|
||||
}
|
||||
|
||||
// the comment line which gives the day the states after it were last updated
|
||||
constexpr std::string_view stamp_prefix{"# updated "};
|
||||
|
||||
// a whole number making up all of the text
|
||||
std::optional<int> parse_integer(std::string_view const Text) {
|
||||
if (Text.empty() || Text.front() == '-')
|
||||
return std::nullopt;
|
||||
int value{0};
|
||||
auto const *const end{Text.data() + Text.size()};
|
||||
if (auto const [used, error] {std::from_chars(Text.data(), end, value)}; error != std::errc{} || used != end)
|
||||
return std::nullopt;
|
||||
return value;
|
||||
}
|
||||
|
||||
// day written as yyyy-mm-dd
|
||||
std::optional<day> parse_day(std::string_view const Text) {
|
||||
if (Text.size() != 10 || Text[4] != '-' || Text[7] != '-')
|
||||
return std::nullopt;
|
||||
auto const year{parse_integer(Text.substr(0, 4))};
|
||||
auto const month{parse_integer(Text.substr(5, 2))};
|
||||
auto const dayofmonth{parse_integer(Text.substr(8, 2))};
|
||||
if (false == (year.has_value() && month.has_value() && dayofmonth.has_value()))
|
||||
return std::nullopt;
|
||||
std::chrono::year_month_day const date{std::chrono::year{*year}, std::chrono::month{static_cast<unsigned>(*month)},
|
||||
std::chrono::day{static_cast<unsigned>(*dayofmonth)}};
|
||||
if (false == date.ok())
|
||||
return std::nullopt;
|
||||
return day{date};
|
||||
}
|
||||
|
||||
std::string format_day(day const Day) {
|
||||
std::chrono::year_month_day const date{Day};
|
||||
return std::format("{:04}-{:02}-{:02}", static_cast<int>(date.year()), static_cast<unsigned>(date.month()),
|
||||
static_cast<unsigned>(date.day()));
|
||||
}
|
||||
|
||||
// the day of a stamp line, as written, a day later than Today included. Today for a malformed one: the states after it
|
||||
// are kept
|
||||
day stamp_day(std::string_view const Line, day const Today) {
|
||||
auto text{Line.substr(stamp_prefix.size())};
|
||||
while (false == text.empty() && (text.back() == ' ' || text.back() == '\t'))
|
||||
text.remove_suffix(1);
|
||||
return parse_day(text).value_or(Today);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
day today() {
|
||||
return std::chrono::floor<std::chrono::days>(std::chrono::system_clock::now());
|
||||
}
|
||||
|
||||
std::chrono::days expiry() {
|
||||
return std::chrono::days{std::max(0, Global.odometer_expiry)};
|
||||
}
|
||||
|
||||
void reading::begin(double const Km, origin const Source) {
|
||||
km = Km;
|
||||
start = Km;
|
||||
@@ -89,12 +146,16 @@ bool reading::moved() const {
|
||||
return km != start;
|
||||
}
|
||||
|
||||
std::size_t store::read(std::istream &Input) {
|
||||
std::size_t store::read(std::istream &Input, day const Today) {
|
||||
std::size_t count{0};
|
||||
// the states with no stamp before them, as the builds without the stamps wrote them, count as updated today
|
||||
auto stamp{Today};
|
||||
std::string line;
|
||||
while (std::getline(Input, line)) {
|
||||
if (false == line.empty() && line.back() == '\r')
|
||||
line.pop_back();
|
||||
if (line.starts_with(stamp_prefix))
|
||||
stamp = stamp_day(line, Today);
|
||||
if (line.empty() || line.front() == '#')
|
||||
continue;
|
||||
// the name goes last, it's the rest of the line after the separator
|
||||
@@ -109,7 +170,8 @@ std::size_t store::read(std::istream &Input) {
|
||||
auto const name{line.substr(separator + 1)};
|
||||
if (false == km.has_value() || name.empty())
|
||||
continue;
|
||||
m_states[name] = state{*km, guessed};
|
||||
m_states[name] = state{*km, guessed, stamp};
|
||||
m_read_latest = std::max(m_read_latest.value_or(stamp), stamp);
|
||||
++count;
|
||||
}
|
||||
return count;
|
||||
@@ -117,9 +179,23 @@ std::size_t store::read(std::istream &Input) {
|
||||
|
||||
void store::write(std::ostream &Output) const {
|
||||
Output << "# odometer states of the vehicles: <km> <vehicle name>, ~<km> for one counted from a made up state\n";
|
||||
Output << "# the states after a line \"" << stamp_prefix << "<yyyy-mm-dd>\" were last updated on that day (UTC)\n";
|
||||
Output << std::fixed << std::setprecision(4);
|
||||
for (auto const &[name, state] : m_states)
|
||||
Output << (state.guessed ? "~" : "") << state.km << ' ' << name << '\n';
|
||||
// by the day, then by the name as the map has them
|
||||
std::vector<std::map<std::string, state>::value_type const *> entries;
|
||||
entries.reserve(m_states.size());
|
||||
for (auto const &entry : m_states)
|
||||
entries.push_back(&entry);
|
||||
std::ranges::stable_sort(entries, {}, [](auto const *Entry) { return Entry->second.updated; });
|
||||
std::optional<day> group;
|
||||
for (auto const *entry : entries) {
|
||||
auto const &written{entry->second};
|
||||
if (group != written.updated) {
|
||||
group = written.updated;
|
||||
Output << stamp_prefix << format_day(written.updated) << '\n';
|
||||
}
|
||||
Output << (written.guessed ? "~" : "") << written.km << ' ' << entry->first << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<double> store::find(std::string const &Vehicle) const {
|
||||
@@ -129,6 +205,13 @@ std::optional<double> store::find(std::string const &Vehicle) const {
|
||||
return lookup->second.km;
|
||||
}
|
||||
|
||||
std::optional<day> store::updated(std::string const &Vehicle) const {
|
||||
auto const lookup{m_states.find(Vehicle)};
|
||||
if (lookup == m_states.end())
|
||||
return std::nullopt;
|
||||
return lookup->second.updated;
|
||||
}
|
||||
|
||||
bool store::guessed(std::string const &Vehicle) const {
|
||||
auto const lookup{m_states.find(Vehicle)};
|
||||
return lookup != m_states.end() && lookup->second.guessed;
|
||||
@@ -140,10 +223,10 @@ bool store::newer(state const &Left, state const &Right) {
|
||||
return Left.km > Right.km;
|
||||
}
|
||||
|
||||
void store::record(std::string const &Vehicle, reading const &Odometer) {
|
||||
void store::record(std::string const &Vehicle, reading const &Odometer, day const Today) {
|
||||
if (Vehicle.empty() || false == valid(Odometer.km))
|
||||
return;
|
||||
state const recorded{Odometer.km, Odometer.source == origin::guessed};
|
||||
state const recorded{Odometer.km, Odometer.source == origin::guessed, Today};
|
||||
auto const lookup{m_states.find(Vehicle)};
|
||||
if (lookup == m_states.end()) {
|
||||
if (false == Odometer.moved())
|
||||
@@ -154,7 +237,10 @@ void store::record(std::string const &Vehicle, reading const &Odometer) {
|
||||
}
|
||||
if (false == newer(recorded, lookup->second))
|
||||
return;
|
||||
// a clock set back doesn't move the day back
|
||||
auto const latest{std::max(lookup->second.updated, Today)};
|
||||
lookup->second = recorded;
|
||||
lookup->second.updated = latest;
|
||||
m_modified = true;
|
||||
}
|
||||
|
||||
@@ -171,11 +257,25 @@ void record(store &States, std::string const &Vehicle, bool const Drivercab, rea
|
||||
void store::merge(store const &Other) {
|
||||
for (auto const &[name, other] : Other.m_states) {
|
||||
auto const [lookup, added] {m_states.try_emplace(name, other)};
|
||||
if (false == added && newer(other, lookup->second))
|
||||
lookup->second = other;
|
||||
if (added)
|
||||
continue;
|
||||
auto &mine{lookup->second};
|
||||
auto const latest{std::max(mine.updated, other.updated)};
|
||||
if (newer(other, mine))
|
||||
mine = other;
|
||||
mine.updated = latest;
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t store::expire(day const Today, std::chrono::days const Period) {
|
||||
if (Period <= std::chrono::days::zero())
|
||||
return 0;
|
||||
// not from a later Today alone: a clock set forward, or the first save after a break, doesn't drop every state not
|
||||
// run in this session
|
||||
auto const since{std::min(Today, m_read_latest.value_or(Today))};
|
||||
return std::erase_if(m_states, [since, Period](auto const &Entry) { return since - Entry.second.updated > Period; });
|
||||
}
|
||||
|
||||
bool store::holds(store const &Other) const {
|
||||
return std::all_of(Other.m_states.begin(), Other.m_states.end(), [this](auto const &Entry) {
|
||||
auto const lookup{m_states.find(Entry.first)};
|
||||
@@ -287,20 +387,21 @@ store &states() {
|
||||
namespace {
|
||||
|
||||
// the states as the file holds them once written, the km rounded to the digits written
|
||||
store as_written(store const &States) {
|
||||
store as_written(store const &States, day const Today) {
|
||||
std::stringstream text;
|
||||
States.write(text);
|
||||
store result;
|
||||
result.read(text);
|
||||
result.read(text, Today);
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool save(store &States, std::filesystem::path const &File) {
|
||||
bool save(store &States, std::filesystem::path const &File, day const Today, std::chrono::days const Period) {
|
||||
if (false == States.modified() && false == States.unconfirmed())
|
||||
return true;
|
||||
std::error_code error;
|
||||
std::size_t expired{0};
|
||||
if (File.has_parent_path())
|
||||
std::filesystem::create_directories(File.parent_path(), error);
|
||||
// other simulations on the machine (multiplayer, parallel runs) keep their states in the same file: what they
|
||||
@@ -309,12 +410,16 @@ bool save(store &States, std::filesystem::path const &File) {
|
||||
store saved;
|
||||
std::ifstream input{File, std::ios::binary};
|
||||
if (input.is_open())
|
||||
saved.read(input);
|
||||
saved.read(input, Today);
|
||||
States.merge(saved);
|
||||
// the vehicles not run for long, of this simulation and of the others alike, so the file doesn't keep every
|
||||
// vehicle ever run. they're logged once the file is written without them: a save that doesn't write it leaves
|
||||
// them in it (the next save that writes it drops them again), and a write that fails is tried again
|
||||
expired = States.expire(Today, Period);
|
||||
// another simulation that read the file before the last save of this one and put its own in place after it
|
||||
// dropped the states this one wrote. when the file holds them all, the same or newer, there's nothing to write
|
||||
if (false == States.modified()) {
|
||||
if (saved.holds(as_written(States))) {
|
||||
if (saved.holds(as_written(States, Today))) {
|
||||
States.confirmed();
|
||||
return true;
|
||||
}
|
||||
@@ -353,6 +458,8 @@ bool save(store &States, std::filesystem::path const &File) {
|
||||
return false;
|
||||
}
|
||||
States.saved();
|
||||
if (expired > 0)
|
||||
WriteLog(std::format("Odometer states of {} vehicles not updated for {} days dropped", expired, Period.count()));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ http://mozilla.org/MPL/2.0/.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <filesystem>
|
||||
#include <iosfwd>
|
||||
#include <map>
|
||||
@@ -23,6 +24,16 @@ http://mozilla.org/MPL/2.0/.
|
||||
// their own files
|
||||
namespace odometer {
|
||||
|
||||
// a calendar day, in UTC
|
||||
using day = std::chrono::sys_days;
|
||||
day today();
|
||||
|
||||
// a state not updated for longer than this is dropped from the store when it's saved: the vehicle wasn't run for that
|
||||
// long. it's odometer.expiry <days> in eu07.ini (global_settings::odometer_expiry), a year unless set there, 0 for never.
|
||||
// a year so the vehicles of the scenarios played once a season or once a year, and of the players who take a break of a
|
||||
// few months, keep their states; the file of the vehicles run within a year stays small all the same
|
||||
std::chrono::days expiry();
|
||||
|
||||
// the saved odometer states of the vehicles, km by vehicle name. the name is the one the scenario gives the vehicle
|
||||
// (the name of its node, TDynamicObject::name(), e.g. EU07-424), the only identity a vehicle has between the sessions:
|
||||
// the vehicles of the same name in any scenarios share one state, whatever their type, and a vehicle the scenario
|
||||
@@ -31,21 +42,36 @@ namespace odometer {
|
||||
class store {
|
||||
public:
|
||||
// reads the states written by write(). lines "<km> <vehicle name>", "~<km> <vehicle name>" for a state counted
|
||||
// from a made up one, '#' starts a comment line, a malformed line is skipped. returns the number of states read
|
||||
std::size_t read(std::istream &Input);
|
||||
// from a made up one, '#' starts a comment line, a malformed line is skipped. returns the number of states read.
|
||||
// the day a state was last updated is in a comment line "# updated <yyyy-mm-dd>" before it, which the builds
|
||||
// without the stamps skip as any comment. a state with no stamp before it (a file of such a build) or with a
|
||||
// malformed one counts as updated Today. a stamp is kept as written, also one later than Today (a clock set back
|
||||
// doesn't move the days of the states back, expire() doesn't measure from them)
|
||||
std::size_t read(std::istream &Input, day Today = today());
|
||||
// the states grouped by the day they were last updated, the oldest first, each group after its stamp line
|
||||
void write(std::ostream &Output) const;
|
||||
|
||||
std::optional<double> find(std::string const &Vehicle) const;
|
||||
// the state of the vehicle is counted from a made up one, which the cab's counter can still replace
|
||||
bool guessed(std::string const &Vehicle) const;
|
||||
// the day the state of the vehicle was last updated
|
||||
std::optional<day> updated(std::string const &Vehicle) const;
|
||||
// records the state of a vehicle. one not in the store yet gets in only once it has run, so the vehicles
|
||||
// standing in the sceneries don't fill the file. a recorded state is replaced only by a newer one: a larger
|
||||
// one, or a known one in place of a guessed one
|
||||
void record(std::string const &Vehicle, reading const &Odometer);
|
||||
// one, or a known one in place of a guessed one. a state recorded is updated Today, or keeps its later day: the day
|
||||
// of a state never goes back
|
||||
void record(std::string const &Vehicle, reading const &Odometer, day Today = today());
|
||||
// takes the states of the other store (the file, as other simulations have saved it since) newer than these.
|
||||
// the odometers only count up, so of two states of a vehicle the larger is the newer one. they aren't added
|
||||
// up: two simulations on one machine (multiplayer) count the same runs of the same vehicles
|
||||
// up: two simulations on one machine (multiplayer) count the same runs of the same vehicles. a state is updated
|
||||
// on the later of the days the two stores have
|
||||
void merge(store const &Other);
|
||||
// drops the states not updated for longer than Period, 0 for none. the period is measured back from Today, or from
|
||||
// the latest day of the states read() took when that's earlier: the last day the file was updated before this
|
||||
// simulation read it. so a clock set forward doesn't drop the states at once (nor one set back, the days of the
|
||||
// states are then later than Today), and a player back after a break keeps them in the session he comes back in;
|
||||
// the states this session records, updated Today, don't count. returns the number dropped
|
||||
std::size_t expire(day Today, std::chrono::days Period);
|
||||
// this store holds every state of the other one, the same or a newer one
|
||||
bool holds(store const &Other) const;
|
||||
|
||||
@@ -65,11 +91,14 @@ private:
|
||||
struct state {
|
||||
double km{0.0};
|
||||
bool guessed{false};
|
||||
day updated{};
|
||||
};
|
||||
// the first state is newer than the second one
|
||||
static bool newer(state const &Left, state const &Right);
|
||||
|
||||
std::map<std::string, state> m_states;
|
||||
// the latest day of the states read() took
|
||||
std::optional<day> m_read_latest;
|
||||
bool m_modified{false};
|
||||
bool m_unconfirmed{false};
|
||||
};
|
||||
@@ -125,8 +154,10 @@ std::filesystem::path file();
|
||||
// writes the store to the file when anything was recorded since, merged with what other simulations saved in the
|
||||
// file meanwhile. it's written to a file of this process aside first, then put in place. with nothing recorded since,
|
||||
// the file is read once more after a write, and written again when it lacks any of the states (another simulation's
|
||||
// save put its file over this one's); otherwise left as it is. returns false when the write failed
|
||||
bool save(store &States, std::filesystem::path const &File);
|
||||
// save put its file over this one's); otherwise left as it is. the states, those of the other simulations included,
|
||||
// not updated for longer than Period (store::expire()) are dropped first; the number dropped is logged by the save that
|
||||
// writes the file without them. returns false when the write failed
|
||||
bool save(store &States, std::filesystem::path const &File, day Today = today(), std::chrono::days Period = expiry());
|
||||
// saves the store of the simulation to its file. a test run doesn't save it
|
||||
bool save();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user