Browse Source

More cleanup for proper json structures.

Mostly:  Keep the keys as strings.
Use json_str, json_int, json_bool helpers.
Steve Thielemann 3 years ago
parent
commit
fcbc7cbfbe
3 changed files with 65 additions and 102 deletions
  1. 20 12
      dispatchers.cpp
  2. 42 85
      galaxy.cpp
  3. 3 5
      twproxy.cpp

+ 20 - 12
dispatchers.cpp

@@ -792,7 +792,10 @@ void TraderDispatch::server_line(const std::string &line,
     BUGZ_LOG(fatal) << "% " << (float)initial_offer / (float)last_offer * 100.0;
     BUGZ_LOG(fatal) << "meta trade setting: " << percent << " for "
                     << active_port << " " << product;
-    director.galaxy.meta["_trade"][active_port][product] = percent;
+
+    director.galaxy
+        .meta["_trade"][std::to_string(active_port)][std::to_string(product)] =
+        percent;
 
     // subtract total holds value from this port's amount
     auto port = director.galaxy.ports.find(active_port);
@@ -814,7 +817,8 @@ void TraderDispatch::server_line(const std::string &line,
     std::string active_port_text = std::to_string(active_port);
     std::string product_text;
     product_text.assign(product, 1);
-    if (director.galaxy.meta["_trade"][active_port_text].contains(product_text)) {
+    if (director.galaxy.meta["_trade"][active_port_text].contains(
+            product_text)) {
       percent = director.galaxy.meta["_trade"][active_port_text][product_text];
       percent += 1.0;
       BUGZ_LOG(fatal) << "Percent for " << active_port << " now " << percent;
@@ -856,7 +860,8 @@ void TraderDispatch::server_line(const std::string &line,
     BUGZ_LOG(fatal) << "We're not interested => meta trade setting: " << percent
                     << " for " << active_port << " " << product;
     std::string active_port_text = std::to_string(active_port);
-    director.galaxy.meta["_trade"][active_port_text][product] = percent;
+    director.galaxy.meta["_trade"][active_port_text][std::to_string(product)] =
+        percent;
     try_again = true;
   }
 
@@ -905,10 +910,10 @@ void TraderDispatch::server_prompt(const std::string &prompt) {
       }
 
       if (director.galaxy.meta["ship"]["holds"].contains("total")) {
-        int total = director.galaxy.meta["ship"]["holds"]["total"];
+        int total = json_int(director.galaxy.meta["ship"]["holds"]["total"]);
         int empty = 0;
         if (director.galaxy.meta["ship"]["holds"].contains("empty")) {
-          empty = director.galaxy.meta["ship"]["holds"]["empty"];
+          empty = json_int(director.galaxy.meta["ship"]["holds"]["empty"]);
         }
         if (total != empty) {
           BUGZ_LOG(fatal) << "FAIL: " << total << " total holds, " << empty
@@ -935,9 +940,9 @@ void TraderDispatch::server_prompt(const std::string &prompt) {
       bool all_holds_empty = false;
       active_port = 0;
       // check the ship and holds here.  (MAYBE)
-      int holds = director.galaxy.meta["ship"]["holds"]["total"].get<int>();
+      int holds = json_int(director.galaxy.meta["ship"]["holds"]["total"]);
       if (director.galaxy.meta["ship"]["holds"].contains("empty")) {
-        if (holds == director.galaxy.meta["ship"]["holds"]["empty"].get<int>())
+        if (holds == json_int(director.galaxy.meta["ship"]["holds"]["empty"]))
           all_holds_empty = true;
       }
 
@@ -1035,8 +1040,9 @@ void TraderDispatch::server_prompt(const std::string &prompt) {
       if (in(prompt, " to buy ")) {
         bool buy_ok = true;
 
-        std::string max = str(boost::format("[%1%]") %
-                              director.galaxy.meta["ship"]["holds"]["total"].get<int>());
+        std::string max =
+            str(boost::format("[%1%]") %
+                json_int(director.galaxy.meta["ship"]["holds"]["total"]));
         if (!in(prompt, max)) {
           buy_ok = false;
         }
@@ -1072,10 +1078,12 @@ void TraderDispatch::server_prompt(const std::string &prompt) {
                   if (director.galaxy.meta["ship"]["holds"].contains("total")) {
                     BUGZ_LOG(fatal)
                         << pos->second.amount[x] << " : "
-                        << director.galaxy.meta["ship"]["holds"]["total"].get<int>();
+                        << json_int(
+                               director.galaxy.meta["ship"]["holds"]["total"]);
 
                     if (pos->second.amount[x] <
-                        director.galaxy.meta["ship"]["holds"]["total"].get<int>()) {
+                        json_int(
+                            director.galaxy.meta["ship"]["holds"]["total"])) {
                       BUGZ_LOG(fatal) << "Other port " << other_port
                                       << " is burnt " << x << " burnt = true";
                       burnt = true;
@@ -1171,7 +1179,7 @@ void TraderDispatch::server_prompt(const std::string &prompt) {
               if (pos->second.percent[x] < stop_percent) burnt = true;
               if (director.galaxy.meta["ship"]["holds"].contains("total"))
                 if (pos->second.amount[x] <
-                    director.galaxy.meta["ship"]["holds"]["total"].get<int>())
+                    json_int(director.galaxy.meta["ship"]["holds"]["total"]))
                   burnt = true;
             }
           }

+ 42 - 85
galaxy.cpp

@@ -10,8 +10,8 @@
 #include <string>
 
 #include "config.h"
-#include "logging.h"
 #include "json.hpp"
+#include "logging.h"
 using json = nlohmann::json;
 
 // c++ default exceptions list
@@ -301,7 +301,7 @@ void Galaxy::load(void) {
     json data;
     {
       std::ifstream fin(filename);
-      data = json::parse(fin); 
+      data = json::parse(fin);
     }
     // YAML::Node data = YAML::LoadFile(filename);
 
@@ -320,9 +320,9 @@ void Galaxy::load(void) {
     }
     if (data.contains("ports")) {
       // const json &ports = data["ports"];
-      for (auto const &port_iter : data["ports"].items() ) {
+      for (auto const &port_iter : data["ports"].items()) {
         port p;
-        p.sector = sstoi(port_iter.key()); // first.as<int>();
+        p.sector = sstoi(port_iter.key());  // first.as<int>();
         p.type = port_iter.value()["class"].get<int>();
         int x;
         if (p.type == 0) {
@@ -387,12 +387,35 @@ void Galaxy::save(void) {
   std::ofstream fout(filename);
 
   BUGZ_LOG(fatal) << "save: " << filename;
-  int depth = 0;
-  std::string depth_spacer;
+  // int depth = 0;
+  // std::string depth_spacer;
 
   meta["save_to"] = filename;
   meta["save_time"] = time_t_now();  // time_t
 
+  // For now, we'll delete this manually.
+  // FUTURE:  delete all keys starting with _.
+
+  if (meta.contains("density")) {
+    meta.erase("density");
+  }
+
+  // clean meta data
+  {
+    // build vector of things to delete, don't invalidate your iterator. (UB)
+    std::vector<std::string> to_delete;
+    for (auto m : meta.items()) {
+      if (m.key()[0] == '_') {
+        to_delete.push_back(m.key());
+      }
+    }
+
+    for (auto key : to_delete) {
+      BUGZ_LOG(fatal) << "erasing meta key: [" << m.key() << "]";
+      meta.erase(m.key());
+    }
+  }
+
   /*   // testing sequence code
   meta["sequence"]["part"].push_back(1);
   meta["sequence"]["part"].push_back(2);
@@ -404,7 +427,6 @@ void Galaxy::save(void) {
   output["meta"] = meta;
   output["config"] = config;
 
-
   BUGZ_LOG(trace) << "YAML meta: " << meta.size();
   // yaml_out(fout, depth, meta);
 
@@ -412,13 +434,10 @@ void Galaxy::save(void) {
 
   BUGZ_LOG(trace) << "YAML warps: " << warps.size();
 
-
-
   // in config, I usually switch to doing flow instead.  I'll keep this like
   // this for now.
   // yaml_out(fout, depth, config);
 
-
   for (auto const &warp : warps) {
     // fout << depth_spacer << warp.first << ": [";
     // bool first = true;
@@ -440,79 +459,29 @@ void Galaxy::save(void) {
   BUGZ_LOG(trace) << "YAML ports: " << ports.size();
   // fout << "ports:" << std::endl;
 
-  /*
-  When saving to yaml, my sector_type is like char.  So, it wants
-  to save the values as a character.  Cast to int.
-   */
   for (auto const &port : ports) {
     std::string port_text = std::to_string(port.first);
     output["ports"][port_text]["class"] = (int)port.second.type;
-    /*
-    fout << depth_spacer << port.second.sector
-         << ": {class: " << (int)port.second.type;
-    
-    if (port.second.type == 0) {
-      fout << "}" << std::endl;
-    } else {
-    */
-   if (port.second.type != 0) {
+    if (port.second.type != 0) {
       // write out the rest of the information
-      // output["ports"][port.first]["amount"] = json::array{port.second.amount[0], port.second.amount[1], port.second.aount[2]};
+      // output["ports"][port.first]["amount"] =
+      // json::array{port.second.amount[0], port.second.amount[1],
+      // port.second.aount[2]};
       for (int x = 0; x < 3; x++) {
         // output["ports"][port.first]["amount"][x] = port.second.amount[x];
-        output["ports"][port_text]["amount"].push_back(port.second.amount[x] );
-      }
-      /*
-      fout << ", amount: [";
-      for (int x = 0; x < 3; x++) {
-        fout << port.second.amount[x];
-        if (x != 2) fout << ", ";
+        output["ports"][port_text]["amount"].push_back(port.second.amount[x]);
       }
-      fout << "], pct: [";
+      // output["ports"][port.first]["pct"] =
+      // json::array{port.second.percent[0], port.second.percent[1],
+      // port.second.percent[2]};
       for (int x = 0; x < 3; x++) {
-        fout << (int)port.second.percent[x];
-        if (x != 2) fout << ", ";
+        // output["ports"][port.first]["pct"][x] = port.second.percent[x];
+        output["ports"][port_text]["pct"].push_back(port.second.percent[x]);
       }
-      fout << "]}" << std::endl;
-      */
-     // output["ports"][port.first]["pct"] = json::array{port.second.percent[0], port.second.percent[1], port.second.percent[2]};
-     for (int x = 0; x < 3; x++) {
-       // output["ports"][port.first]["pct"][x] = port.second.percent[x];
-       output["ports"][port_text]["pct"].push_back( port.second.percent[x] );
-     }
-    }
-  }
-
-  // the big data structures later on are the ones I really need to optimize
-  // here. Writing out 20 lines isn't what is killing us!
-
-  /*
-  for (auto const & meta_data : meta ) {
-    std::cerr << "key: " << meta_data.first << std::endl;
-
-    if (meta_data.second.Type() == YAML::NodeType::Scalar) {
-      fout << depth_spacer << meta_data.first << ": " << meta_data.second <<
-  std::endl; } else {
-      // nested structure
-      std::cerr << "Nested: " << meta_data.first << std::endl;
-      fout << depth_spacer << meta_data.first << ":" << std::endl;
-      ++depth;
-      depth_spacer.assign(depth *2, ' ');
-      for (auto const &nested : meta_data.second) {
-         if (nested.second.Type() == YAML::NodeType::Scalar) {
-           fout << depth_spacer << nested.first << ": " << nested.second <<
-  std::endl; } else { std::cerr << "double-Nested meta structure under key: " <<
-  nested.first << std::endl;
-        }
-      }
-      --depth;
-      depth_spacer.assign(depth * 2, ' ');
     }
   }
-  */
-
-   fout << output;
 
+  fout << output;
 }
 
 std::vector<port_pair_type> Galaxy::find_trades(sector_type sector,
@@ -578,7 +547,7 @@ void Galaxy::sort_port_pair_type(std::vector<port_pair_type> &pptv) {
 std::vector<port_pair_type> Galaxy::find_best_trades(void) {
   std::vector<port_pair_type> pptv;
 
-  burnt_percent = config["burnt_percent"];
+  burnt_percent = json_int(config["burnt_percent"]);
   if (burnt_percent > 90) {
     burnt_percent = 90;
     config["burnt_percent"] = 90;
@@ -695,18 +664,6 @@ port_pair_type Galaxy::find_closest_trade(int sector, int lowest_trade_type,
   int depth = 0;
 
   while (!found) {
-    /*
-    for (auto const &t : trades) {
-      if (t.type > 2) continue;
-      if (current.find(t.s1) != current.end()) {
-        // found one!
-        return t;
-      }
-      if (current.find(t.s2) != current.end()) {
-        return t;
-      }
-    }
-    */
     ++depth;
     BUGZ_LOG(info) << "depth: " << depth << " current:" << current.size()
                    << " seen: " << seen.size();
@@ -865,7 +822,7 @@ trade_type_result Galaxy::trade_type_info(sector_type port1, sector_type port2,
   // If we don't know how many holds the ship has, default to 300.
   int max_holds = 300;
   if (meta["ship"]["holds"].contains("total")) {
-    max_holds = meta["ship"]["holds"]["total"].get<int>();
+    max_holds = json_int(meta["ship"]["holds"]["total"]);
   }
 
   // find which FOE are flipped.  Save index pos.

+ 3 - 5
twproxy.cpp

@@ -48,25 +48,23 @@ void init_logging(void) {
   std::string log_filename = "proxy.log";
   if (CONFIG.contains("log_file")) log_filename = json_str(CONFIG["log_file"]);
 
-  //  = from_config("log_file", "proxy.log");
   // "%I:%M:%S.%f %p"
   std::string log_timeformat = "%H:%M:%S.%f";
   if (CONFIG.contains("log_timeformat"))
     log_timeformat = json_str(CONFIG["log_timeformat"]);
   // from_config("log_timeformat", "%H:%M:%S.%f");
-  bool log_autoflush = false;
 
+  bool log_autoflush = false;
   if (CONFIG.contains("log_autoflush")) {
     log_autoflush = json_bool(CONFIG["log_autoflush"]);
   }
 
   int log_level = 2;
   if (CONFIG.contains("log_level")) {
-    log_level = CONFIG["log_level"].get<int>();
+    log_level = json_int(CONFIG["log_level"]);
   }
 
   bool console = false;
-
   if (CONFIG.contains("log_console")) {
     console = json_bool(CONFIG["log_console"]);
   }
@@ -137,7 +135,7 @@ int main(int argc, char *argv[]) {
 
   if (!config_ok) return EXIT_FAILURE;
 
-  int listen_port = CONFIG["server"].get<int>();
+  int listen_port = json_int(CONFIG["server"]);
 
 #define BURN