18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-20 08:04:57 +01:00

fix: the odometer store merges the file other simulations saved, keeps a made up state replaceable by the cab's counter

- each process writes its own <file>.<pid>.tmp and renames it in place; before writing it re-reads the file and
  merges it per vehicle (a known state beats a made up one, else the larger one, never the sum)
- a state counted from a made up one is saved as ~<km> and the .mmd counter offset still replaces it later
- a vehicle removed from the scenery has its odometer recorded right away
This commit is contained in:
jerrrrycho
2026-11-08 18:18:35 +01:00
parent 1083f3cb5d
commit a3ad30b72b
4 changed files with 292 additions and 34 deletions

View File

@@ -200,3 +200,163 @@ TEST_CASE("odometer: the cab's counter offset replaces only a guessed state, the
CHECK(reading.source == source);
}
}
TEST_CASE("odometer: a state counted from a made up one is saved as such, the cab's counter still replaces it") {
scratch_directory directory{"odometer_guessed"};
auto const guess{odometer::guessed_state("EU07-424")};
// the vehicle runs 30 km with no cab loaded (the AI's, loose stock)
odometer::store first;
auto ai{odometer::starting_state(first, "EU07-424", directory.path)};
REQUIRE(ai.source == odometer::origin::guessed);
ai.run(30000.0);
first.record("EU07-424", ai);
std::stringstream file;
first.write(file);
CHECK(file.str().find("\n~") != std::string::npos);
odometer::store second;
REQUIRE(second.read(file) == 1);
CHECK(second.guessed("EU07-424"));
CHECK(second.find("EU07-424").value() == doctest::Approx(guess + 30.0));
auto player{odometer::starting_state(second, "EU07-424", directory.path)};
CHECK(player.source == odometer::origin::guessed);
CHECK(player.km == doctest::Approx(guess + 30.0));
SUBCASE("a cab whose counter has an offset loads it: the offset replaces the made up state, the 30 km kept") {
player.run(2000.0);
player.seed(101532.0);
CHECK(player.source == odometer::origin::counter);
CHECK(player.start == doctest::Approx(101562.0));
CHECK(player.km == doctest::Approx(101564.0));
// recorded as known, also when the counter shows less than the made up state did
second.record("EU07-424", player);
CHECK_FALSE(second.guessed("EU07-424"));
CHECK(second.find("EU07-424").value() == doctest::Approx(101564.0));
odometer::reading low;
low.begin(10.0, odometer::origin::counter);
odometer::store replaced;
std::stringstream guessedfile{"~400000 SM42-001\n"};
replaced.read(guessedfile);
replaced.record("SM42-001", low);
CHECK(replaced.find("SM42-001").value() == 10.0);
CHECK_FALSE(replaced.guessed("SM42-001"));
CHECK(replaced.modified());
}
SUBCASE("it runs with no cab again: it stays guessed") {
player.run(1000.0);
second.record("EU07-424", player);
CHECK(second.guessed("EU07-424"));
CHECK(second.find("EU07-424").value() == doctest::Approx(guess + 31.0));
}
}
TEST_CASE("odometer: the store reads the marker of a made up state, and skips it malformed") {
std::stringstream file{"~300 guessed one\n"
"~ nothing\n"
"~abc name\n"
"~-5 negative\n"
"~~5 twice\n"
"400 known\n"};
odometer::store store;
CHECK(store.read(file) == 2);
CHECK(store.find("guessed one").value() == 300.0);
CHECK(store.guessed("guessed one"));
CHECK_FALSE(store.guessed("known"));
CHECK_FALSE(store.guessed("missing"));
}
TEST_CASE("odometer: a recorded state is replaced only by a newer one") {
std::stringstream file{"500 known\n~600 guessed\n"};
odometer::store store;
store.read(file);
// a vehicle of this simulation counting from less than another simulation saved meanwhile
store.record("known", run(400.0, 50000.0));
CHECK(store.find("known").value() == 500.0);
CHECK_FALSE(store.modified());
// a larger one does, also when the vehicle hasn't run in this session
store.record("known", run(900.0, 0.0));
CHECK(store.find("known").value() == 900.0);
CHECK(store.modified());
// a guessed state doesn't replace a known one, even a larger one
odometer::reading guessed;
guessed.begin(5000.0, odometer::origin::guessed);
guessed.run(1000.0);
store.record("known", guessed);
CHECK(store.find("known").value() == 900.0);
CHECK_FALSE(store.guessed("known"));
// a larger guessed one replaces a guessed one
store.record("guessed", guessed);
CHECK(store.find("guessed").value() == doctest::Approx(5001.0));
CHECK(store.guessed("guessed"));
}
TEST_CASE("odometer: two simulations saving one file keep each other's states") {
scratch_directory directory{"odometer_merge"};
auto const path{directory.path / "eu07_odometers.txt"};
write_file(path, "1000 shared\n~20000 both guessed\n50 untouched\n");
auto const load = [&path]() {
odometer::store store;
std::ifstream input{path, std::ios::binary};
store.read(input);
return store;
};
auto first{load()};
auto second{load()};
auto const guessed = [](double const Km, double const Metres) {
odometer::reading reading;
reading.begin(Km, odometer::origin::guessed);
reading.run(Metres);
return reading;
};
// both run the shared vehicle, as two multiplayer simulations of the same train do, the first one further
first.record("shared", run(1000.0, 5000.0));
first.record("first only", run(70.0, 1000.0));
first.record("both guessed", guessed(20000.0, 3000.0));
second.record("shared", run(1000.0, 2000.0));
second.record("second only", run(80.0, 1000.0));
second.record("both guessed", guessed(20000.0, 1000.0));
REQUIRE(odometer::save(first, path));
CHECK_FALSE(first.modified());
REQUIRE(odometer::save(second, path));
auto const saved{load()};
CHECK(saved.size() == 5);
// the larger state of the shared vehicle, not the sum of the two: they counted the same run
CHECK(saved.find("shared").value() == doctest::Approx(1005.0));
CHECK(saved.find("first only").value() == doctest::Approx(71.0));
CHECK(saved.find("second only").value() == doctest::Approx(81.0));
CHECK(saved.find("both guessed").value() == doctest::Approx(20003.0));
CHECK(saved.guessed("both guessed"));
CHECK(saved.find("untouched").value() == 50.0);
// the second one now knows the states the first one saved, for the vehicles it loads later
CHECK(second.find("first only").value() == doctest::Approx(71.0));
SUBCASE("a known state saved by one replaces a guessed one of the other") {
odometer::reading seeded;
seeded.begin(100.0, odometer::origin::counter);
first.record("both guessed", seeded);
REQUIRE(odometer::save(first, path));
second.record("both guessed", guessed(20003.0, 5000.0));
REQUIRE(odometer::save(second, path));
auto const after{load()};
CHECK(after.find("both guessed").value() == 100.0);
CHECK_FALSE(after.guessed("both guessed"));
CHECK(after.find("second only").value() == doctest::Approx(81.0));
}
SUBCASE("nothing but the file is left in the directory") {
std::size_t files{0};
for (auto const &entry : std::filesystem::directory_iterator{directory.path}) {
CAPTURE(entry.path().string());
CHECK(entry.path().filename() == path.filename());
++files;
}
CHECK(files == 1);
}
SUBCASE("a store with nothing recorded since doesn't write") {
write_file(path, "1 replaced by hand\n");
REQUIRE(odometer::save(second, path));
CHECK(load().size() == 1);
}
}

View File

@@ -8890,6 +8890,11 @@ vehicle_table::erase_disabled() {
++vehicleiter;
}
else {
// the distance the vehicle ran goes in the odometer store now, it doesn't depend on the vehicle being
// kept on the list until the next save (eu07/maszyna#72)
if( vehicle->MoverParameters != nullptr ) {
odometer::states().record( vehicle->name(), vehicle->MoverParameters->Odometer );
}
if( vehicle->MyTrack != nullptr
&& true == vehicle->MyTrack->RemoveDynamicObject(vehicle) ) {
vehicle->MyTrack = nullptr;

View File

@@ -13,11 +13,19 @@ http://mozilla.org/MPL/2.0/.
#include "utilities/Logs.h"
#include "utilities/utilities.h"
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <thread>
#ifdef _WIN32
#include <process.h>
#else
#include <unistd.h>
#endif
namespace odometer {
@@ -27,6 +35,14 @@ bool valid(double const Km) {
return std::isfinite(Km) && Km >= 0.0;
}
int process_id() {
#ifdef _WIN32
return _getpid();
#else
return static_cast<int>(getpid());
#endif
}
// number making up all of the text, surrounding whitespace aside
std::optional<double> parse_number(std::string const &Text) {
auto const first{Text.find_first_not_of(" \t\r\n")};
@@ -51,13 +67,16 @@ void reading::begin(double const Km, origin const Source) {
km = Km;
start = Km;
source = Source;
guess = Km;
}
void reading::seed(double const Km) {
if (source != origin::guessed || false == valid(Km))
return;
km += Km - start;
start = Km;
auto const shift{Km - guess};
km += shift;
start += shift;
guess = Km;
source = origin::counter;
}
@@ -81,47 +100,71 @@ std::size_t store::read(std::istream &Input) {
auto const separator{line.find_first_of(" \t")};
if (separator == std::string::npos)
continue;
auto const km{parse_number(line.substr(0, separator))};
auto number{line.substr(0, separator)};
auto const guessed{false == number.empty() && number.front() == '~'};
if (guessed)
number.erase(0, 1);
auto const km{parse_number(number)};
auto const name{line.substr(separator + 1)};
if (false == km.has_value() || name.empty())
continue;
m_states[name] = *km;
m_states[name] = state{*km, guessed};
++count;
}
return count;
}
void store::write(std::ostream &Output) const {
Output << "# odometer states of the vehicles: <km> <vehicle name>\n";
Output << "# odometer states of the vehicles: <km> <vehicle name>, ~<km> for one counted from a made up state\n";
Output << std::fixed << std::setprecision(4);
for (auto const &[name, km] : m_states)
Output << km << ' ' << name << '\n';
for (auto const &[name, state] : m_states)
Output << (state.guessed ? "~" : "") << state.km << ' ' << name << '\n';
}
std::optional<double> store::find(std::string const &Vehicle) const {
auto const lookup{m_states.find(Vehicle)};
if (lookup == m_states.end())
return std::nullopt;
return lookup->second;
return lookup->second.km;
}
bool store::guessed(std::string const &Vehicle) const {
auto const lookup{m_states.find(Vehicle)};
return lookup != m_states.end() && lookup->second.guessed;
}
bool store::newer(state const &Left, state const &Right) {
if (Left.guessed != Right.guessed)
return Right.guessed;
return Left.km > Right.km;
}
void store::record(std::string const &Vehicle, reading const &Odometer) {
if (Vehicle.empty() || false == valid(Odometer.km))
return;
state const recorded{Odometer.km, Odometer.source == origin::guessed};
auto const lookup{m_states.find(Vehicle)};
if (lookup == m_states.end()) {
if (false == Odometer.moved())
return;
m_states.emplace(Vehicle, Odometer.km);
m_states.emplace(Vehicle, recorded);
m_modified = true;
return;
}
if (lookup->second == Odometer.km)
if (false == newer(recorded, lookup->second))
return;
lookup->second = Odometer.km;
lookup->second = recorded;
m_modified = true;
}
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;
}
}
std::optional<double> read_legacy(std::istream &Input) {
std::stringstream content;
content << Input.rdbuf();
@@ -154,7 +197,14 @@ double guessed_state(std::string const &Vehicle) {
reading starting_state(store const &States, std::string const &Vehicle, std::filesystem::path const &Vehicledirectory) {
reading result;
if (auto const saved{States.find(Vehicle)}; saved.has_value()) {
result.begin(*saved, origin::saved);
if (States.guessed(Vehicle)) {
// the vehicle ran with a made up state before any cab with a known one loaded it (as the AI's or loose
// stock); the made up part of it stays replaceable
result.begin(*saved, origin::guessed);
result.guess = std::min(*saved, guessed_state(Vehicle));
} else {
result.begin(*saved, origin::saved);
}
} else if (auto const legacy{legacy_state(Vehicledirectory, Vehicle)}; legacy.has_value()) {
result.begin(*legacy, origin::legacy);
} else {
@@ -190,36 +240,59 @@ store &states() {
return odometers;
}
bool save() {
auto &odometers{states()};
if (false == odometers.modified())
bool save(store &States, std::filesystem::path const &File) {
if (false == States.modified())
return true;
auto const path{file()};
std::error_code error;
if (path.has_parent_path())
std::filesystem::create_directories(path.parent_path(), error);
// written aside first, a crash in the middle of a write doesn't lose the states
auto temporary{path};
temporary += ".tmp";
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
// saved since this one read it is merged in, not overwritten
{
std::ifstream input{File, std::ios::binary};
if (input.is_open()) {
store saved;
saved.read(input);
States.merge(saved);
}
}
// written aside first, a crash in the middle of a write doesn't lose the states. the file aside is this process'
// own, the other simulations write theirs
auto temporary{File};
temporary += "." + std::to_string(process_id()) + ".tmp";
{
std::ofstream output{temporary, std::ios::binary | std::ios::trunc};
if (false == output.is_open()) {
ErrorLog("Failed to save the odometer states to \"" + temporary.string() + "\"");
return false;
}
odometers.write(output);
States.write(output);
if (false == output.good()) {
output.close();
std::filesystem::remove(temporary, error);
ErrorLog("Failed to save the odometer states to \"" + temporary.string() + "\"");
return false;
}
}
std::filesystem::rename(temporary, path, error);
// the file can't be replaced while another simulation is reading it, that takes a moment
for (auto attempt{0};; ++attempt) {
std::filesystem::rename(temporary, File, error);
if (false == static_cast<bool>(error) || attempt == 4)
break;
std::this_thread::sleep_for(std::chrono::milliseconds(20));
}
if (error) {
ErrorLog("Failed to save the odometer states to \"" + path.string() + "\": " + error.message());
ErrorLog("Failed to save the odometer states to \"" + File.string() + "\": " + error.message());
std::error_code ignored;
std::filesystem::remove(temporary, ignored);
return false;
}
odometers.saved();
States.saved();
return true;
}
bool save() {
return save(states(), file());
}
} // namespace odometer

View File

@@ -26,7 +26,7 @@ enum class origin {
saved, // the odometer store of the earlier sessions
legacy, // the file a python screen kept it in before
counter, // the offset of the cab's counter, which used to show it on top of the distance run in the session
guessed // nothing known, a made up state which the cab's counter can still replace
guessed // counted from a made up state, which the cab's counter can still replace, also once it was saved
};
// odometer of one vehicle
@@ -34,11 +34,12 @@ struct reading {
double km{0.0}; // the state, km
double start{0.0}; // the state at the start of the session, km
origin source{origin::guessed};
double guess{0.0}; // the made up state a guessed one counts from, km
// sets the state the vehicle starts the session with
void begin(double Km, origin Source);
// replaces a guessed state with the one the cab's counter showed at the start, the distance run kept.
// a known state stays
// replaces the made up state a guessed one counts from with the one the cab's counter showed at the start,
// the distance run on top of it kept, in this session and the earlier ones. a known state stays
void seed(double Km);
// counts the distance run, in metres either way
void run(double Metres);
@@ -49,15 +50,22 @@ struct reading {
// the saved odometer states of the vehicles, km by vehicle name
class store {
public:
// reads the states written by write(). lines "<km> <vehicle name>", '#' starts a comment line, a malformed
// line is skipped. returns the number of states read
// 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);
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;
// 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
// 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);
// 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
void merge(store const &Other);
// there are states recorded since they were last saved
bool modified() const { return m_modified; }
@@ -65,7 +73,14 @@ public:
std::size_t size() const { return m_states.size(); }
private:
std::map<std::string, double> m_states;
struct state {
double km{0.0};
bool guessed{false};
};
// 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;
bool m_modified{false};
};
@@ -78,7 +93,8 @@ std::optional<double> legacy_state(std::filesystem::path const &Vehicledirectory
// 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
// 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);
// state the cab's counter with offset Offset and scale Scale shows, once its offset no longer applies
@@ -88,7 +104,11 @@ std::optional<double> counter_seed(float Offset, float Scale);
// the odometer store of the simulation, read from the user's settings directory on the first use
store &states();
std::filesystem::path file();
// writes the store when anything was recorded since. returns false when the write failed
// 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. returns false when the
// write failed
bool save(store &States, std::filesystem::path const &File);
// saves the store of the simulation to its file
bool save();
} // namespace odometer