From 9bc766beab23c8c5f507ca3c7abce24049e6229e Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Mon, 4 Dec 2017 16:30:23 +0100 Subject: [PATCH] better record logging --- src/ui/ui.cpp | 19 +---------- src/world/Record.hpp | 2 ++ src/world/Simulation.hpp | 1 + src/world/sim.cpp | 73 ++++++++++++++++++++++++++++------------ 4 files changed, 56 insertions(+), 39 deletions(-) diff --git a/src/ui/ui.cpp b/src/ui/ui.cpp index c4d35e3..11b63d8 100644 --- a/src/ui/ui.cpp +++ b/src/ui/ui.cpp @@ -307,24 +307,7 @@ void RecordsPanel::Draw(graphics::Viewport &viewport) noexcept { int i = 0; for (const auto &r : sim.Records()) { if (!r) continue; - switch (r.type) { - default: - case world::Record::VALUE: - records[i]->Text(DecimalString(r.value, 2)); - break; - case world::Record::LENGTH: - records[i]->Text(LengthString(r.value)); - break; - case world::Record::MASS: - records[i]->Text(MassString(r.value)); - break; - case world::Record::PERCENTAGE: - records[i]->Text(PercentageString(r.value)); - break; - case world::Record::TIME: - records[i]->Text(TimeString(r.value)); - break; - } + records[i]->Text(r.ValueString()); std::string str(r.holder->Name()); bool first = true; for (auto p : r.holder->Parents()) { diff --git a/src/world/Record.hpp b/src/world/Record.hpp index 3436cbd..0f4ea7d 100644 --- a/src/world/Record.hpp +++ b/src/world/Record.hpp @@ -26,6 +26,8 @@ struct Record { operator bool() const noexcept { return holder; } + std::string ValueString() const; + }; } diff --git a/src/world/Simulation.hpp b/src/world/Simulation.hpp index 1902340..50f06b4 100644 --- a/src/world/Simulation.hpp +++ b/src/world/Simulation.hpp @@ -65,6 +65,7 @@ public: const std::vector &Records() const noexcept { return records; } void CheckRecords(creature::Creature &) noexcept; + void LogRecord(const Record &rold, const Record &rnew); std::ostream &Log(); diff --git a/src/world/sim.cpp b/src/world/sim.cpp index 1d9706e..b23790d 100644 --- a/src/world/sim.cpp +++ b/src/world/sim.cpp @@ -1,3 +1,4 @@ +#include "Record.hpp" #include "Simulation.hpp" #include "Body.hpp" @@ -13,6 +14,22 @@ namespace blobs { namespace world { +std::string Record::ValueString() const { + switch (type) { + default: + case VALUE: + return ui::DecimalString(value, 2); + case LENGTH: + return ui::LengthString(value); + case MASS: + return ui::MassString(value); + case PERCENTAGE: + return ui::PercentageString(value); + case TIME: + return ui::TimeString(value); + } +} + Simulation::Simulation(Body &r, app::Assets &assets) : root(r) , assets(assets) @@ -85,63 +102,77 @@ void Simulation::SetDead(creature::Creature *c) { void Simulation::CheckRecords(creature::Creature &c) noexcept { if (c.Age() > records[0].value) { - if (records[0].holder && records[0].holder != &c) { - Log() << "new age record by " << c.Name() << std::endl; - } + Record rold(records[0]); records[0].value = c.Age(); records[0].time = Time(); records[0].holder = &c; + if (rold.holder && rold.holder != &c) { + LogRecord(rold, records[0]); + } } if (c.Mass() > records[1].value) { - if (records[1].holder && records[1].holder != &c) { - Log() << "new mass record by " << c.Name() << std::endl; - } + Record rold(records[1]); records[1].value = c.Mass(); records[1].time = Time(); records[1].holder = &c; + if (rold.holder && rold.holder != &c) { + LogRecord(rold, records[1]); + } } if (c.Size() > records[2].value) { - if (records[2].holder && records[2].holder != &c) { - Log() << "new size record by " << c.Name() << std::endl; - } + Record rold(records[2]); records[2].value = c.Size(); records[2].time = Time(); records[2].holder = &c; + if (rold.holder && rold.holder != &c) { + LogRecord(rold, records[2]); + } } if (c.Strength() > records[3].value) { - if (records[3].holder && records[3].holder != &c) { - Log() << "new strength record by " << c.Name() << std::endl; - } + Record rold(records[3]); records[3].value = c.Strength(); records[3].time = Time(); records[3].holder = &c; + if (rold.holder && rold.holder != &c) { + LogRecord(rold, records[3]); + } } if (c.Stamina() > records[4].value) { - if (records[4].holder && records[4].holder != &c) { - Log() << "new stamina record by " << c.Name() << std::endl; - } + Record rold(records[4]); records[4].value = c.Stamina(); records[4].time = Time(); records[4].holder = &c; + if (rold.holder && rold.holder != &c) { + LogRecord(rold, records[4]); + } } if (c.Dexerty() > records[5].value) { - if (records[5].holder && records[5].holder != &c) { - Log() << "new dexerty record by " << c.Name() << std::endl; - } + Record rold(records[5]); records[5].value = c.Dexerty(); records[5].time = Time(); records[5].holder = &c; + if (rold.holder && rold.holder != &c) { + LogRecord(rold, records[5]); + } } if (c.Intelligence() > records[6].value) { - if (records[6].holder && records[6].holder != &c) { - Log() << "new intelligence record by " << c.Name() << std::endl; - } + Record rold(records[6]); records[6].value = c.Intelligence(); records[6].time = Time(); records[6].holder = &c; + if (rold.holder && rold.holder != &c) { + LogRecord(rold, records[6]); + } } } +void Simulation::LogRecord(const Record &rold, const Record &rnew) { + Log() << "at age " << ui::TimeString(rnew.holder->Age()) << " " + << rnew.holder->Name() << " broke the " << rnew.name << " record of " + << rold.ValueString() << " by " << rold.holder->Name() + << " (established " << ui::TimeString(rold.time) << ")" << std::endl; +} + std::ostream &Simulation::Log() { return std::cout << '[' << ui::TimeString(Time()) << "] "; } -- 2.39.2