18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 21:45:16 +01:00
Files
maszyna/vehicle/odometer.h
jerrrrycho a8e8313343 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().
2026-11-26 22:27:27 +01:00

165 lines
9.9 KiB
C++

/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <chrono>
#include <filesystem>
#include <iosfwd>
#include <map>
#include <optional>
#include <string>
#include "vehicle/odometerreading.h"
// the odometer of a vehicle: the total distance it has run, kept between the sessions (eu07/maszyna#72).
// the analog counters in the cab (distcounter:) and the python screens (state["odometer_total"]) show
// this one state, instead of the counters starting from their .mmd offset and the screens each keeping
// 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
// names otherwise starts over, from the legacy or a made up state. the sceneries name the real vehicles by their
// numbers, so it's the same vehicle as a rule
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.
// 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. 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. 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;
// there are states recorded since they were last saved
bool modified() const { return m_modified; }
// the states were written to the file. until a later save finds them there (confirmed()), they may have been lost to
// another simulation's save, which read the file before this one wrote it and put its own in place after
void saved() {
m_modified = false;
m_unconfirmed = true;
}
bool unconfirmed() const { return m_unconfirmed; }
void confirmed() { m_unconfirmed = false; }
std::size_t size() const { return m_states.size(); }
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};
};
// the vehicle is one the store keeps: one with a driver's cab, which can show its odometer on the cab's counter or a
// python screen. that's
// - a vehicle whose type has a cab (Drivercab, TMoverParameters::HasDriverCab(): it has the driver's brake valve). that
// includes the locomotives and units the AI runs before a player's cab loads them, their made up states stay
// replaceable by the cab's counter
// - a vehicle whose cab showed the odometer in this session (reading::shown): the cab was loaded for a player, or a
// cab's counter was bound to it. the type alone misses the vehicles driven without a driver's brake valve (the DL2
// draisine, with a counter in its cab) and the motor cars the counter of a unit's cab counts for. such a vehicle is
// recorded in the sessions a player's cab shows it; in the others its saved state stays as it is
// - a vehicle whose state a cab's counter seeded (origin::counter), which is the same thing, told by the state
// the wagons, which make most of the AI's trains, aren't
bool kept(bool Drivercab, reading const &Odometer);
// records the state of a vehicle in the store when it's one the store keeps (kept()). the others aren't recorded; the
// states the store already holds for any of them stay as they are
void record(store &States, std::string const &Vehicle, bool Drivercab, reading const &Odometer);
// state kept in the file of the python screens before, when it holds a number. the screens opened the file in the
// directory of their script (lookup_path), and two kinds of them kept one:
// - the renderers of the units, next to the .mmd (elf_v1: EN62, EN76, EN96, SKM; impuls_v1: EN63), in
// <vehicle directory>/kilometry/<name without its last character>_przebieg.txt (state['name'][:-1]). the cars of
// these units are named after the unit and the letter of the car (EN76-022a .. EN76-022d, ED78-019A .. ED78-019D),
// so the cab cars at both ends of a unit share one file, the unit's odometer
// - the shared screens in scripts/ (koliber_hasler_elektroniczny, koliber_unicontrols_tramex[_ic]), in
// <scripts directory>/kilometry/<name>_przebieg.txt, the whole name
std::optional<double> read_legacy(std::istream &Input);
std::optional<double> legacy_state(std::filesystem::path const &Vehicledirectory, std::string const &Vehicle,
std::filesystem::path const &Scriptsdirectory = "scripts");
// made up state of a vehicle nothing is known about: a number from the name, in [10 000, 500 000) km,
// the same each session until the vehicle runs and gets its state saved
double guessed_state(std::string const &Vehicle);
// state a vehicle starts the session with: the saved one, else the legacy one, else a guessed one. a saved state
// counted from a guessed one stays guessed, so the cab's counter can still replace the made up part of it
reading starting_state(store const &States, std::string const &Vehicle, std::filesystem::path const &Vehicledirectory,
std::filesystem::path const &Scriptsdirectory = "scripts");
// state the cab's counter with offset Offset and scale Scale shows, once its offset no longer applies
// (the counter shows the odometer). nothing for a counter without an offset
std::optional<double> counter_seed(float Offset, float Scale);
// the store of the simulation is the player's file: false in a test run (--test, Global.test.enabled), whose vehicles
// neither start from the player's states nor change them
bool persistent();
// the odometer store of the simulation, read from the user's settings directory on the first use. in a test run an
// empty one, not read from anywhere
store &states();
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. 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();
} // namespace odometer