]> git.localhorst.tv Git - blobs.git/commitdiff
better record logging
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 4 Dec 2017 15:30:23 +0000 (16:30 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 4 Dec 2017 15:30:23 +0000 (16:30 +0100)
src/ui/ui.cpp
src/world/Record.hpp
src/world/Simulation.hpp
src/world/sim.cpp

index c4d35e3177e9f3b502ad657e556079963bacadd6..11b63d87f6798d97d01be0d491d253e0a2ccaf63 100644 (file)
@@ -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()) {
index 3436cbd6ffa82da7c1b96db7305f92d02f63be8f..0f4ea7dc8abd86b9934ad781941fd3aa750ab54b 100644 (file)
@@ -26,6 +26,8 @@ struct Record {
 
        operator bool() const noexcept { return holder; }
 
+       std::string ValueString() const;
+
 };
 
 }
index 19023406ace73a2466886fb2c71c960cea208bb7..50f06b4ca693456505662659d1aae938e85e932e 100644 (file)
@@ -65,6 +65,7 @@ public:
 
        const std::vector<Record> &Records() const noexcept { return records; }
        void CheckRecords(creature::Creature &) noexcept;
+       void LogRecord(const Record &rold, const Record &rnew);
 
        std::ostream &Log();
 
index 1d9706e80bc3af77717bb44dcd99837f1c8331bf..b23790dbef9a47d01eace54cb399d09f2d74c772 100644 (file)
@@ -1,3 +1,4 @@
+#include "Record.hpp"
 #include "Simulation.hpp"
 
 #include "Body.hpp"
 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()) << "] ";
 }