mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-19 22:44:59 +01:00
fix: read the odometer files of the koliber screens too, say why the unit renderers' drop a letter
the renderers of the units (elf_v1, impuls_v1) keep the unit's state, their cars are named after the unit and the letter of the car (EN76-022a .. EN76-022d), so they drop the last character. the koliber screens of scripts/ keep theirs in scripts/kilometry by the whole name, which wasn't read
This commit is contained in:
@@ -434,3 +434,22 @@ TEST_CASE("odometer: only the vehicles with a driver's cab are recorded") {
|
||||
CHECK(read.find("old wagon").value() == 300.0);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("odometer: the files of both kinds of python screens are read, by the name each used") {
|
||||
scratch_directory directory{"odometer_legacy_screens"};
|
||||
auto const vehicles{directory.path / "dynamic" / "elf_v1"};
|
||||
auto const scripts{directory.path / "scripts"};
|
||||
// the renderers of the units, next to the .mmd: state['name'][:-1], the cab cars of a unit share the file
|
||||
write_file(vehicles / "kilometry" / "EN76-022_przebieg.txt", "250000.5");
|
||||
CHECK(odometer::legacy_state(vehicles, "EN76-022a", scripts).value() == 250000.5);
|
||||
CHECK(odometer::legacy_state(vehicles, "EN76-022d", scripts).value() == 250000.5);
|
||||
// the shared screens of scripts/ (koliber_*): the whole name
|
||||
write_file(scripts / "kilometry" / "EU07-424_przebieg.txt", "123456.0");
|
||||
CHECK(odometer::legacy_state(vehicles, "EU07-424", scripts).value() == 123456.0);
|
||||
auto const started{odometer::starting_state(odometer::store{}, "EU07-424", vehicles, scripts)};
|
||||
CHECK(started.source == odometer::origin::legacy);
|
||||
CHECK(started.km == 123456.0);
|
||||
// neither
|
||||
CHECK_FALSE(odometer::legacy_state(vehicles, "EU07-425", scripts).has_value());
|
||||
CHECK_FALSE(odometer::legacy_state(vehicles, "", scripts).has_value());
|
||||
}
|
||||
|
||||
@@ -178,19 +178,34 @@ std::optional<double> read_legacy(std::istream &Input) {
|
||||
return parse_number(content.str());
|
||||
}
|
||||
|
||||
std::optional<double> legacy_state(std::filesystem::path const &Vehicledirectory, std::string const &Vehicle) {
|
||||
if (Vehicle.size() < 2)
|
||||
return std::nullopt;
|
||||
auto const path{Vehicledirectory / "kilometry" / (Vehicle.substr(0, Vehicle.size() - 1) + "_przebieg.txt")};
|
||||
namespace {
|
||||
|
||||
std::optional<double> read_legacy_file(std::filesystem::path const &Path) {
|
||||
std::error_code error;
|
||||
if (false == std::filesystem::is_regular_file(path, error))
|
||||
if (false == std::filesystem::is_regular_file(Path, error))
|
||||
return std::nullopt;
|
||||
std::ifstream input{path, std::ios::binary};
|
||||
std::ifstream input{Path, std::ios::binary};
|
||||
if (false == input.is_open())
|
||||
return std::nullopt;
|
||||
return read_legacy(input);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::optional<double> legacy_state(std::filesystem::path const &Vehicledirectory, std::string const &Vehicle,
|
||||
std::filesystem::path const &Scriptsdirectory) {
|
||||
if (Vehicle.empty())
|
||||
return std::nullopt;
|
||||
// the renderers of the units drop the letter of the car, the file is the unit's (see the header)
|
||||
if (Vehicle.size() > 1) {
|
||||
auto const unit{read_legacy_file(Vehicledirectory / "kilometry" / (Vehicle.substr(0, Vehicle.size() - 1) + "_przebieg.txt"))};
|
||||
if (unit.has_value())
|
||||
return unit;
|
||||
}
|
||||
// the shared screens of scripts/ keep the whole name
|
||||
return read_legacy_file(Scriptsdirectory / "kilometry" / (Vehicle + "_przebieg.txt"));
|
||||
}
|
||||
|
||||
double guessed_state(std::string const &Vehicle) {
|
||||
// FNV-1a, stable between the builds unlike std::hash
|
||||
std::uint64_t hash{14695981039346656037ull};
|
||||
@@ -201,7 +216,8 @@ double guessed_state(std::string const &Vehicle) {
|
||||
return 10000.0 + static_cast<double>(hash % 490000ull);
|
||||
}
|
||||
|
||||
reading starting_state(store const &States, std::string const &Vehicle, std::filesystem::path const &Vehicledirectory) {
|
||||
reading starting_state(store const &States, std::string const &Vehicle, std::filesystem::path const &Vehicledirectory,
|
||||
std::filesystem::path const &Scriptsdirectory) {
|
||||
reading result;
|
||||
if (auto const saved{States.find(Vehicle)}; saved.has_value()) {
|
||||
if (States.guessed(Vehicle)) {
|
||||
@@ -212,7 +228,7 @@ reading starting_state(store const &States, std::string const &Vehicle, std::fil
|
||||
} else {
|
||||
result.begin(*saved, origin::saved);
|
||||
}
|
||||
} else if (auto const legacy{legacy_state(Vehicledirectory, Vehicle)}; legacy.has_value()) {
|
||||
} else if (auto const legacy{legacy_state(Vehicledirectory, Vehicle, Scriptsdirectory)}; legacy.has_value()) {
|
||||
result.begin(*legacy, origin::legacy);
|
||||
} else {
|
||||
result.begin(guessed_state(Vehicle), origin::guessed);
|
||||
|
||||
@@ -95,10 +95,17 @@ private:
|
||||
// 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 (<vehicle directory>/kilometry/<name>_przebieg.txt,
|
||||
// where the screens dropped the last character of the vehicle name), when it holds a number
|
||||
// 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::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
|
||||
@@ -106,7 +113,8 @@ 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);
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user