Browse Source

Initial move to json.

The tests are failing in the (bool) section.
Code still needs to be tested/run.
Steve Thielemann 3 years ago
parent
commit
1934c52c5b
12 changed files with 160 additions and 168 deletions
  1. 6 6
      CMakeLists.txt
  2. 4 2
      config.cpp
  3. 4 2
      config.h
  4. 16 18
      director.cpp
  5. 23 26
      dispatchers.cpp
  6. 71 82
      galaxy.cpp
  7. 5 4
      galaxy.h
  8. 1 1
      pch.hpp
  9. 3 3
      scripts.cpp
  10. 3 3
      session.cpp
  11. 11 11
      test-director.cpp
  12. 13 10
      twproxy.cpp

+ 6 - 6
CMakeLists.txt

@@ -36,13 +36,13 @@ ADD_DEFINITIONS(-DBOOST_LOG_DYN_LINK)
 FIND_PACKAGE( Boost 1.60 COMPONENTS program_options log_setup log REQUIRED )
 INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} )
 
-if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/yaml-cpp)
+if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/json)
   message("***")
-  message("*** ERROR/MISSING *** please run: git clone https://github.com/jbeder/yaml-cpp.git --depth 1")
+  message("*** ERROR/MISSING *** please run: git clone https://github.com/nlohmann/json.git --depth 1")
   message("***")
 endif()
 
-add_subdirectory(yaml-cpp)
+add_subdirectory(json)
 
 if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/googletest)
   message("***")
@@ -58,11 +58,11 @@ option(gtest_disable_pthreads "Disable uses of pthreads in gtest." OFF)
 ### TESTS
 add_executable(test-galaxy test-galaxy.cpp galaxy.cpp config.cpp utils.cpp buysell.cpp)
 add_dependencies(test-galaxy gtest)
-target_link_libraries(test-galaxy gtest_main ${Boost_LIBRARIES} yaml-cpp)
+target_link_libraries(test-galaxy gtest_main ${Boost_LIBRARIES})
 
 add_executable(test-director test-director.cpp galaxy.cpp utils.cpp buysell.cpp director.cpp dispatchers.cpp boxes.cpp scripts.cpp config.cpp ansicolor.cpp)
 add_dependencies(test-director gtest)
-target_link_libraries(test-director gtest_main ${Boost_LIBRARIES} yaml-cpp)
+target_link_libraries(test-director gtest_main ${Boost_LIBRARIES})
 
 enable_testing()
 add_test(NAME test-galaxy
@@ -72,7 +72,7 @@ add_test(NAME test-director
 
 # dispatchers.cpp 
 ADD_EXECUTABLE( twproxy twproxy.cpp utils.cpp session.cpp boxes.cpp director.cpp galaxy.cpp dispatchers.cpp scripts.cpp buysell.cpp config.cpp ansicolor.cpp)
-TARGET_LINK_LIBRARIES( twproxy ${Boost_LIBRARIES} pthread yaml-cpp)
+TARGET_LINK_LIBRARIES( twproxy ${Boost_LIBRARIES} pthread)
 
 target_precompile_headers(twproxy PRIVATE pch.hpp)
 

+ 4 - 2
config.cpp

@@ -1,4 +1,6 @@
-#include "yaml-cpp/yaml.h"
+#include "json.hpp"
 
-YAML::Node CONFIG;
+using json = nlohmann::json;
+
+json CONFIG;
 

+ 4 - 2
config.h

@@ -1,8 +1,10 @@
 #ifndef CONFIG_H
 #define CONFIG_H
 
-#include "yaml-cpp/yaml.h"
+#include "json.hpp"
 
-extern YAML::Node CONFIG;
+using json = nlohmann::json;
+
+extern json CONFIG;
 
 #endif

+ 16 - 18
director.cpp

@@ -344,7 +344,7 @@ void Director::proxy_activate(void) {
   to_client(box.bottom());
 
   if (galaxy.meta["port_CIM"]) {
-    time_t last_port_cim_report = galaxy.meta["port_CIM"].as<int>();
+    time_t last_port_cim_report = (int)galaxy.meta["port_CIM"];
     int seconds_ago = time_t_now() - last_port_cim_report;
     int minutes_ago = seconds_ago / 60;
     BUGZ_LOG(fatal) << "port_CIM was " << minutes_ago << " minutes ago.";
@@ -356,9 +356,6 @@ void Director::proxy_activate(void) {
     }
   } else {
     BUGZ_LOG(fatal) << "no meta port_CIM value seen.";
-    for( auto const &d : galaxy.meta) {
-      BUGZ_LOG(fatal) << d.first << " : " << d.second;
-    }
   }
 
   chain = main_menu;
@@ -397,7 +394,7 @@ void Director::menu_choice(void) {
 
           int max_display = 20;
           if (galaxy.config["display_lines"])
-            max_display = galaxy.config["display_lines"].as<int>();
+            max_display = galaxy.config["display_lines"];
           else
             galaxy.config["display_lines"] = max_display;
 
@@ -707,10 +704,11 @@ void Director::config_edit(void) {
   ANSIColor key(COLOR::GREEN, ATTR::BOLD);
   ANSIColor value(COLOR::BLUE, ATTR::BOLD);
 
-  for (auto const &cfg : galaxy.config) {
+  for (auto const &cfg : galaxy.config.items()) {
+  // for (auto const &cfg : galaxy.config) {
     std::string output =
         str(boost::format("%1%%2$2d %3%%4$20s: %5%%6$s%7%\n\r") % number() %
-            item % key() % cfg.first % value() % cfg.second % reset());
+            item % key() % cfg.key() % value() % cfg.value() % reset());
     to_client(output);
     ++item;
   }
@@ -752,14 +750,14 @@ void Director::config_have_input(void) {
         return;
       } else {
         int pos = 1;
-        const YAML::Node &config = galaxy.config;
-        for (auto const &c : config) {
+        
+        for (auto const &c : galaxy.config.items()) {
           if (pos == item) {
             // got it!
             ANSIColor key(COLOR::GREEN, ATTR::BOLD);
             ANSIColor value(COLOR::BLUE, ATTR::BOLD);
 
-            config_item = c.first.as<std::string>();
+            config_item = c.key();
             std::string output =
                 str(boost::format("%1%%2% : %3%%4%\n\r") % key() % config_item %
                     value() % galaxy.meta["help"][config_item]);
@@ -1113,7 +1111,7 @@ void Director::SL_infoline(const std::string &line) {
 
   if (line == "<Info>") {
     state = 0;
-    galaxy.meta["info"] = YAML::Node();
+    galaxy.meta["info"] = json();
   }
   if (line.empty()) {
     ++state;
@@ -1121,10 +1119,10 @@ void Director::SL_infoline(const std::string &line) {
       SL_parser = nullptr;
 
       // clear out the existing ship data
-      galaxy.meta["ship"] = YAML::Node();
+      galaxy.meta["ship"] = json();
       // process the parsed information in meta["info"]
       if (galaxy.meta["info"]["Total Holds"]) {
-        std::string work = galaxy.meta["info"]["Total Holds"].as<std::string>();
+        std::string work = galaxy.meta["info"]["Total Holds"];
         replace(work, "Fuel Ore", "Fuel");
         auto parts = split(work, " - ");
         int total_holds = stoi(parts[0]);
@@ -1150,14 +1148,14 @@ void Director::SL_infoline(const std::string &line) {
       }
 
       if (galaxy.meta["info"]["Turns to Warp"]) {
-        int warp_turns = galaxy.meta["info"]["Turns to Warp"].as<int>();
+        int warp_turns = galaxy.meta["info"]["Turns to Warp"];
         BUGZ_LOG(fatal) << "Turns to Warp: " << warp_turns;
         galaxy.meta["ship"]["warp_turns"] = warp_turns;
       }
 
       if (galaxy.meta["info"]["LongRange Scan"]) {
         std::string scanner_text =
-            galaxy.meta["info"]["LongRange Scan"].as<std::string>();
+            galaxy.meta["info"]["LongRange Scan"];
         char scanner = scanner_text[0];
         BUGZ_LOG(fatal) << "Scanner: " << scanner;
         galaxy.meta["ship"]["scanner"] = scanner;
@@ -1166,7 +1164,7 @@ void Director::SL_infoline(const std::string &line) {
       // turns isn't ship specific
       if (galaxy.meta["info"]["Turns left"]) {
         // OR this could be "Unlimited" !!!
-        std::string text = galaxy.meta["info"]["Turns left"].as<std::string>();
+        std::string text = galaxy.meta["info"]["Turns left"];
         if (text == "Unlimited") {
           galaxy.meta["turns"] = -1;
         } else {
@@ -1178,7 +1176,7 @@ void Director::SL_infoline(const std::string &line) {
       }
 
       if (galaxy.meta["info"]["Current Sector"]) {
-        int sector = galaxy.meta["info"]["Current Sector"].as<int>();
+        int sector = galaxy.meta["info"]["Current Sector"];
         BUGZ_LOG(fatal) << "Sector: " << sector;
         // it should already be sector ...
         current_sector = sector;
@@ -1186,7 +1184,7 @@ void Director::SL_infoline(const std::string &line) {
 
       if (galaxy.meta["info"]["Credits"]) {
         std::string credit_text =
-            galaxy.meta["info"]["Credits"].as<std::string>();
+            galaxy.meta["info"]["Credits"];
         replace(credit_text, ",", "");
         int credits = stoi(credit_text);
         galaxy.meta["credits"] = credits;

+ 23 - 26
dispatchers.cpp

@@ -415,9 +415,7 @@ MoveDispatch::MoveDispatch(Director &d) : Dispatch(d) {
   BUGZ_LOG(warning) << "MoveDispatch()";
 }
 
-MoveDispatch::~MoveDispatch() {
-  BUGZ_LOG(warning) << "~MoveDispatch()";
-}
+MoveDispatch::~MoveDispatch() { BUGZ_LOG(warning) << "~MoveDispatch()"; }
 
 // sector_type move_to;
 
@@ -581,7 +579,8 @@ void MoveDispatch::server_prompt(const std::string &prompt) {
         // Yes! we found the sector in the scan!
         if (!density_clear(d)) {
           BUGZ_LOG(fatal) << "Failed density check on single move.";
-          why_failed = str(boost::format("Density of %1% at %2%.") % move_to % d.density );  // density of move_to ...
+          why_failed = str(boost::format("Density of %1% at %2%.") % move_to %
+                           d.density);  // density of move_to ...
           success = false;
           deactivate();
           return;
@@ -619,7 +618,8 @@ void MoveDispatch::server_prompt(const std::string &prompt) {
         } else {
           to_server("N");
           BUGZ_LOG(fatal) << "density_clear(" << to_check << ") : false";
-          why_failed = str(boost::format("Density of %1% at %2%.") % move_to % d.density );  
+          why_failed = str(boost::format("Density of %1% at %2%.") % move_to %
+                           d.density);
           success = false;
           deactivate();
         }
@@ -645,7 +645,8 @@ void MoveDispatch::server_prompt(const std::string &prompt) {
       } else {
         to_server("Y");
         BUGZ_LOG(fatal) << "Stopped by density: " << to_check;
-        why_failed = str(boost::format("Density of %1% at %2%.") % to_check % d.density );  
+        why_failed =
+            str(boost::format("Density of %1% at %2%.") % to_check % d.density);
         success = 0;
         deactivate();
       }
@@ -715,15 +716,14 @@ void TraderDispatch::activate(void) {
   to_server("I");
 
   if (director.galaxy.config["stop_percent"]) {
-    stop_percent = director.galaxy.config["stop_percent"].as<int>();
+    stop_percent = director.galaxy.config["stop_percent"];
   } else {
     stop_percent = 25;
     director.galaxy.config["stop_percent"] = stop_percent;
   }
 
   if (director.galaxy.config["trade_end_empty"]) {
-    std::string tee =
-        director.galaxy.config["trade_end_empty"].as<std::string>();
+    std::string tee = director.galaxy.config["trade_end_empty"];
     if ((toupper(tee[0]) == 'Y') || (toupper(tee[0]) == 'T')) {
       trade_end_empty = true;
     } else {
@@ -750,8 +750,8 @@ void TraderDispatch::server_line(const std::string &line,
       temp = "\x1b[1A\x1b[1;33m** SLOW MOVE **";
     // don't output lines that move cursor up 3.
     // if (!in(temp, "\x1b[3A")) {
-      temp.append("\n\r");
-      to_client(temp);
+    temp.append("\n\r");
+    to_client(temp);
     // }
   }
 
@@ -800,7 +800,7 @@ void TraderDispatch::server_line(const std::string &line,
       // We've found the port!
       // product is the index
       port->second.amount[product] -=
-          director.galaxy.meta["ship"]["holds"]["total"].as<int>();
+          (int)director.galaxy.meta["ship"]["holds"]["total"];
       BUGZ_LOG(fatal) << "Port " << active_port << "," << product
                       << " amount is now " << port->second.amount[product];
     }
@@ -812,7 +812,7 @@ void TraderDispatch::server_line(const std::string &line,
     last_offer = 0;
     final_offer = 0;
     if (director.galaxy.meta["_trade"][active_port][product]) {
-      percent = director.galaxy.meta["_trade"][active_port][product].as<float>();
+      percent = director.galaxy.meta["_trade"][active_port][product];
       percent += 1.0;
       BUGZ_LOG(fatal) << "Percent for " << active_port << " now " << percent;
     } else {
@@ -868,7 +868,7 @@ void TraderDispatch::server_line(const std::string &line,
       // Ok, the offer was possibly accepted.
       int success;
       if (buying)
-        success = director.galaxy.meta["ship"]["holds"]["total"].as<int>();
+        success = director.galaxy.meta["ship"]["holds"]["total"];
       else
         success = 0;
 
@@ -901,10 +901,10 @@ void TraderDispatch::server_prompt(const std::string &prompt) {
       }
 
       if (director.galaxy.meta["ship"]["holds"]["total"]) {
-        int total = director.galaxy.meta["ship"]["holds"]["total"].as<int>();
+        int total = director.galaxy.meta["ship"]["holds"]["total"];
         int empty = 0;
         if (director.galaxy.meta["ship"]["holds"]["empty"]) {
-          empty = director.galaxy.meta["ship"]["holds"]["empty"].as<int>();
+          empty = director.galaxy.meta["ship"]["holds"]["empty"];
         }
         if (total != empty) {
           BUGZ_LOG(fatal) << "FAIL: " << total << " total holds, " << empty
@@ -931,9 +931,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"].as<int>();
+      int holds = director.galaxy.meta["ship"]["holds"]["total"];
       if (director.galaxy.meta["ship"]["holds"]["empty"]) {
-        if (holds == director.galaxy.meta["ship"]["holds"]["empty"].as<int>())
+        if (holds == director.galaxy.meta["ship"]["holds"]["empty"])
           all_holds_empty = true;
       }
 
@@ -1029,9 +1029,8 @@ 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"].as<int>());
+        std::string max = str(boost::format("[%1%]") %
+                              director.galaxy.meta["ship"]["holds"]["total"]);
         if (!in(prompt, max)) {
           buy_ok = false;
         }
@@ -1067,12 +1066,10 @@ void TraderDispatch::server_prompt(const std::string &prompt) {
                   if (director.galaxy.meta["ship"]["holds"]["total"]) {
                     BUGZ_LOG(fatal)
                         << pos->second.amount[x] << " : "
-                        << director.galaxy.meta["ship"]["holds"]["total"]
-                               .as<int>();
+                        << (int)director.galaxy.meta["ship"]["holds"]["total"];
 
                     if (pos->second.amount[x] <
-                        director.galaxy.meta["ship"]["holds"]["total"]
-                            .as<int>()) {
+                        (int)director.galaxy.meta["ship"]["holds"]["total"]) {
                       BUGZ_LOG(fatal) << "Other port " << other_port
                                       << " is burnt " << x << " burnt = true";
                       burnt = true;
@@ -1168,7 +1165,7 @@ void TraderDispatch::server_prompt(const std::string &prompt) {
               if (pos->second.percent[x] < stop_percent) burnt = true;
               if (director.galaxy.meta["ship"]["holds"]["total"])
                 if (pos->second.amount[x] <
-                    director.galaxy.meta["ship"]["holds"]["total"].as<int>())
+                    (int)director.galaxy.meta["ship"]["holds"]["total"])
                   burnt = true;
             }
           }

+ 71 - 82
galaxy.cpp

@@ -11,7 +11,8 @@
 
 #include "config.h"
 #include "logging.h"
-#include "yaml-cpp/yaml.h"
+#include "json.hpp"
+using json = nlohmann::json;
 
 // c++ default exceptions list
 // https://en.cppreference.com/w/cpp/error/exception
@@ -200,8 +201,8 @@ Galaxy::Galaxy() { burnt_percent = 40; }
 Galaxy::~Galaxy() { BUGZ_LOG(fatal) << "Galaxy::~Galaxy()"; }
 
 void Galaxy::reset(void) {
-  meta = YAML::Node();
-  config = YAML::Node();
+  meta = json();
+  config = json();
   ports.clear();
   warps.clear();
 }
@@ -284,20 +285,25 @@ void Galaxy::load(void) {
   std::string basename = "galaxy";
 
   if (CONFIG["basename"]) {
-    basename = CONFIG["basename"].as<std::string>();
+    basename = CONFIG["basename"];
   }
   std::string filename =
-      str(boost::format("%1%-%2%-%3%.yaml") % basename % game % username);
+      str(boost::format("%1%-%2%-%3%.json") % basename % game % username);
 
   // reset ?
-  meta = YAML::Node();
-  config = YAML::Node();
+  meta = json();
+  config = json();
   ports.clear();
   warps.clear();
   if (file_exists(filename)) {
     BUGZ_LOG(fatal) << "Galaxy::load( " << filename << " )";
 
-    YAML::Node data = YAML::LoadFile(filename);
+    json data;
+    {
+      std::ifstream fin(filename);
+      data = json::parse(fin); 
+    }
+    // YAML::Node data = YAML::LoadFile(filename);
 
     if (data["meta"]) {
       meta = data["meta"];
@@ -310,14 +316,14 @@ void Galaxy::load(void) {
     if (data["config"]) {
       config = data["config"];
     } else {
-      BUGZ_LOG(fatal) << "YAML Missing config section.";
+      BUGZ_LOG(fatal) << "Missing config section.";
     }
     if (data["ports"]) {
-      const YAML::Node ports = data["ports"];
-      for (auto const &port_iter : ports) {
+      // const json &ports = data["ports"];
+      for (auto const &port_iter : data["ports"].items() ) {
         port p;
-        p.sector = port_iter.first.as<int>();
-        p.type = port_iter.second["class"].as<int>();
+        p.sector = sstoi(port_iter.key()); // first.as<int>();
+        p.type = (int)port_iter.value()["class"];
         int x;
         if (p.type == 0) {
           // nothing to load for type = 0, set defaults.
@@ -327,36 +333,36 @@ void Galaxy::load(void) {
           }
         } else {
           x = 0;
-          for (auto const &amount : port_iter.second["amount"]) {
-            p.amount[x] = amount.as<int>();
+          for (auto const &amount : port_iter.value()["amount"].items()) {
+            p.amount[x] = sstoi(amount.value());
             ++x;
           }
           x = 0;
-          for (auto const &pct : port_iter.second["pct"]) {
-            p.percent[x] = pct.as<int>();
+          for (auto const &pct : port_iter.value()["pct"].items()) {
+            p.percent[x] = sstoi(pct.value());
             ++x;
           }
         }
         add_port(p);
       }
     } else {
-      BUGZ_LOG(fatal) << "YAML Missing ports section.";
+      BUGZ_LOG(fatal) << "Missing ports section.";
     }
     if (data["warps"]) {
-      const YAML::Node &warps = data["warps"];
+      // const json &warps = data["warps"];
       // if (warps.IsMap()) {
-      for (auto const warp_iter : warps) {
+      for (auto const warp_iter : data["warps"].items()) {
         sector_warps sw;
-        sw.sector = warp_iter.first.as<int>();
-        for (auto const sector_iter : warp_iter.second) {
-          sw.add(sector_iter.as<int>());
+        sw.sector = sstoi(warp_iter.key());
+        for (auto const sector_iter : warp_iter.value().items()) {
+          sw.add(sstoi(sector_iter.value()));
         }
         // BUGZ_LOG(trace) << "YAML warp: " << sw;
         add_warp(sw);
       }
       // }
     } else {
-      BUGZ_LOG(fatal) << "YAML Missing warps section.";
+      BUGZ_LOG(fatal) << "Missing warps section.";
     }
 
     BUGZ_LOG(trace) << "YAML:   meta keys: " << meta.size();
@@ -365,7 +371,7 @@ void Galaxy::load(void) {
     BUGZ_LOG(trace) << "YAML:   port keys: " << ports.size();
 
   } else {
-    BUGZ_LOG(fatal) << "Missing YAML: " << filename;
+    BUGZ_LOG(fatal) << "Missing: " << filename;
   }
 }
 
@@ -373,16 +379,14 @@ void Galaxy::save(void) {
   std::string basename = "galaxy";
 
   if (CONFIG["basename"]) {
-    basename = CONFIG["basename"].as<std::string>();
+    basename = CONFIG["basename"];
   }
   std::string filename =
-      str(boost::format("%1%-%2%-%3%.yaml") % basename % game % username);
+      str(boost::format("%1%-%2%-%3%.json") % basename % game % username);
 
   std::ofstream fout(filename);
-  // fout << "%YAML 1.2" << std::endl << "---" << std::endl;
-  fout << "---" << std::endl;
 
-  BUGZ_LOG(fatal) << "YAML: " << filename;
+  BUGZ_LOG(fatal) << "save: " << filename;
   int depth = 0;
   std::string depth_spacer;
 
@@ -395,90 +399,67 @@ void Galaxy::save(void) {
   meta["sequence"]["part"].push_back(3);
   meta["sequence"]["smeg"].push_back(0);
   */
+  json output;
+
+  output["meta"] = meta;
+  output["config"] = config;
 
-  // meta:
-  fout << "meta:" << std::endl;
-  ++depth;
-  depth_spacer.assign(depth * 2, ' ');
-
-  std::function<void(std::ofstream & of, int depth, const YAML::Node &n)>
-      yaml_out;
-
-  yaml_out = [&yaml_out](std::ofstream &of, int yaml_depth,
-                         const YAML::Node &n) {
-    std::string yaml_spacer;
-    yaml_spacer.assign(yaml_depth * 2, ' ');
-    for (auto const &data : n) {
-      std::string key = data.first.as<std::string>();
-      if (key[0] == '_') {
-        BUGZ_LOG(fatal) << "Skip key: " << key;
-        continue;
-      }
 
-      if (data.second.Type() == YAML::NodeType::Scalar) {
-        of << yaml_spacer << data.first << ": " << data.second << std::endl;
-      } else if (data.second.Type() == YAML::NodeType::Map) {
-        // nested
-        of << yaml_spacer << data.first << ":" << std::endl;
-        yaml_out(of, yaml_depth + 1, data.second);
-      } else if (data.second.Type() == YAML::NodeType::Sequence) {
-        // sequence
-        BUGZ_LOG(fatal) << "Outputting Sequence... " << data.first;
-        of << yaml_spacer << data.first << ": [";
-        bool first = true;
-        for (auto const &seq : data.second) {
-          if (!first)
-            of << ", ";
-          else
-            first = false;
-          of << seq;
-        }
-        of << "]" << std::endl;
-      } else {
-        BUGZ_LOG(fatal) << "Unsupported NodeType: " << data.second.Type();
-      }
-    }
-  };
   BUGZ_LOG(trace) << "YAML meta: " << meta.size();
-  yaml_out(fout, depth, meta);
+  // yaml_out(fout, depth, meta);
 
   BUGZ_LOG(trace) << "YAML config: " << config.size();
-  fout << "config:" << std::endl;
+
+  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);
+  // yaml_out(fout, depth, config);
 
-  BUGZ_LOG(trace) << "YAML warps: " << warps.size();
-  fout << "warps:" << std::endl;
 
   for (auto const &warp : warps) {
-    fout << depth_spacer << warp.first << ": [";
+    // fout << depth_spacer << warp.first << ": [";
     bool first = true;
+    output["warps"][warp.first] = json::array();
     for (auto const &sector : warp.second.warps) {
+      output["warps"][warp.first].push_back(sector);
+      /*
       if (!first) {
         fout << ", ";
       } else
         first = false;
       fout << sector;
+      */
     }
-    fout << "]" << std::endl;
+    // fout << "]" << std::endl;
   }
 
   BUGZ_LOG(trace) << "YAML ports: " << ports.size();
-  fout << "ports:" << std::endl;
+  // 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) {
+    output["ports"][port.first]["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) {
       // 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]};
+      for (int x = 0; x < 3; x++) {
+        output["ports"][port.first]["amount"][x] = port.second.amount[x];
+      }
+      /*
       fout << ", amount: [";
       for (int x = 0; x < 3; x++) {
         fout << port.second.amount[x];
@@ -490,6 +471,11 @@ void Galaxy::save(void) {
         if (x != 2) fout << ", ";
       }
       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];
+     }
     }
   }
 
@@ -520,6 +506,9 @@ void Galaxy::save(void) {
     }
   }
   */
+
+   fout << output;
+
 }
 
 std::vector<port_pair_type> Galaxy::find_trades(sector_type sector,
@@ -585,7 +574,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"].as<int>();
+  burnt_percent = config["burnt_percent"];
   if (burnt_percent > 90) {
     burnt_percent = 90;
     config["burnt_percent"] = 90;
@@ -872,7 +861,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"]["total"]) {
-    max_holds = meta["ship"]["holds"]["total"].as<int>();
+    max_holds = meta["ship"]["holds"]["total"];
   }
 
   // find which FOE are flipped.  Save index pos.

+ 5 - 4
galaxy.h

@@ -12,7 +12,8 @@
 #include <vector>
 
 #include "buysell.h"
-#include "yaml-cpp/yaml.h"
+#include "json.hpp"
+using json = nlohmann::json;
 
 // #define MAX_WARPS 6
 
@@ -121,8 +122,8 @@ class Galaxy {
   ~Galaxy();
 
   void reset(void);
-  YAML::Node config;
-  YAML::Node meta;
+  json config;
+  json meta;
 
   int burnt_percent;
   density_scan dscan;
@@ -154,4 +155,4 @@ class Galaxy {
   std::string username;
 };
 
-#endif
+#endif

+ 1 - 1
pch.hpp

@@ -13,5 +13,5 @@
 #include <iomanip>
 #include <string>
 #include <map>
-#include "yaml-cpp/yaml.h"
+#include "json.hpp"
 

+ 3 - 3
scripts.cpp

@@ -39,7 +39,7 @@ void ScriptTerror::activate(void) {
   // Save the trade_end_empty setting, and set to Y
   if (director.galaxy.config["trade_end_empty"]) {
     old_trade_end_empty =
-        director.galaxy.config["trade_end_empty"].as<std::string>();
+        director.galaxy.config["trade_end_empty"];
   } else {
     old_trade_end_empty = "Y";
   }
@@ -88,7 +88,7 @@ void ScriptTerror::input_notify(void) {
   // find nearest
   int stop_percent;
   if (director.galaxy.config["stop_percent"]) {
-    stop_percent = director.galaxy.config["stop_percent"].as<int>();
+    stop_percent = director.galaxy.config["stop_percent"];
   } else {
     stop_percent = 25;
     director.galaxy.config["stop_percent"] = stop_percent;
@@ -176,7 +176,7 @@ void ScriptTerror::trade_notify(void) {
     // find nearest
     int stop_percent;
     if (director.galaxy.config["stop_percent"]) {
-      stop_percent = director.galaxy.config["stop_percent"].as<int>();
+      stop_percent = director.galaxy.config["stop_percent"];
     } else {
       stop_percent = 25;
       director.galaxy.config["stop_percent"] = stop_percent;

+ 3 - 3
session.cpp

@@ -31,10 +31,10 @@ Session::Session(boost::asio::ip::tcp::socket socket,
   BUGZ_LOG(info) << "Session::Session()";
   // server_sent = 0;
   time_ms = 50;
-  if (CONFIG["prompt_timeout"]) time_ms = CONFIG["prompt_timeout"].as<int>();
+  if (CONFIG["prompt_timeout"]) time_ms = CONFIG["prompt_timeout"];
 
   keepalive_secs = 45;
-  if (CONFIG["keepalive"]) keepalive_secs = CONFIG["keepalive"].as<int>();
+  if (CONFIG["keepalive"]) keepalive_secs = CONFIG["keepalive"];
 
   // Initialize the director
   director.to_server = boost::bind(&Session::to_server, this, _1);
@@ -598,7 +598,7 @@ void Server::on_signal(const boost::system::error_code &ec, int signal) {
 }
 
 Server::~Server() {
-  CONFIG = YAML::Node();
+  CONFIG = json();
   BUGZ_LOG(info) << "Server::~Server()";
 }
 /**

+ 11 - 11
test-director.cpp

@@ -136,7 +136,7 @@ TEST(director, director_parsing_density) {
   // Check that each sector was correctly processed
   std::vector<int> sectors = {70, 441, 575, 600, 629, 711};
 
-  YAML::Node sector_data;
+  json sector_data;
   sector_data[70]["density"] = 0;
   sector_data[70]["warps"] = 6;
   sector_data[70]["navhaz"] = 0;
@@ -170,20 +170,20 @@ TEST(director, director_parsing_density) {
 
   for (auto sector : sectors) {
     // GTEST_COUT << "Testing Sector " << sector << std::endl;
-    EXPECT_EQ(dir.galaxy.meta["density"][sector]["density"].as<int>(),
-              sector_data[sector]["density"].as<int>())
+    EXPECT_EQ((int)dir.galaxy.meta["density"][sector]["density"],
+              (int)sector_data[sector]["density"])
         << "Sector " << sector << " density";
-    EXPECT_EQ(dir.galaxy.meta["density"][sector]["warps"].as<int>(),
-              sector_data[sector]["warps"].as<int>())
+    EXPECT_EQ((int)dir.galaxy.meta["density"][sector]["warps"],
+              (int)sector_data[sector]["warps"])
         << "Sector " << sector << " warps";
-    EXPECT_EQ(dir.galaxy.meta["density"][sector]["navhaz"].as<int>(),
-              sector_data[sector]["navhaz"].as<int>())
+    EXPECT_EQ((int)dir.galaxy.meta["density"][sector]["navhaz"],
+              (int)sector_data[sector]["navhaz"])
         << "Sector " << sector << " navhaz";
-    EXPECT_EQ(dir.galaxy.meta["density"][sector]["anom"].as<bool>(),
-              sector_data[sector]["anom"].as<bool>())
+    EXPECT_EQ((bool)dir.galaxy.meta["density"][sector]["anom"],
+              (bool)sector_data[sector]["anom"])
         << "Sector " << sector << " anom";
-    EXPECT_EQ(dir.galaxy.meta["density"][sector]["known"].as<bool>(),
-              sector_data[sector]["known"].as<bool>())
+    EXPECT_EQ((bool)dir.galaxy.meta["density"][sector]["known"],
+              (bool)sector_data[sector]["known"])
         << "Sector " << sector << " known";
   }
 }

+ 13 - 10
twproxy.cpp

@@ -46,26 +46,26 @@ void init_logging(void) {
 
   // "proxy-%Y-%m-%d.log"
   std::string log_filename = "proxy.log";
-  if (CONFIG["log_file"]) log_filename = CONFIG["log_file"].as<std::string>();
+  if (CONFIG["log_file"]) log_filename = CONFIG["log_file"];
 
   //  = from_config("log_file", "proxy.log");
   // "%I:%M:%S.%f %p"
   std::string log_timeformat = "%H:%M:%S.%f";
   if (CONFIG["log_timeformat"])
-    log_timeformat = CONFIG["log_timeformat"].as<std::string>();
+    log_timeformat = CONFIG["log_timeformat"];
   // from_config("log_timeformat", "%H:%M:%S.%f");
   bool log_autoflush = false;
   if (CONFIG["log_autoflush"]) {
-    log_autoflush = CONFIG["log_autoflush"].as<int>() == 1;
+    log_autoflush = (int)CONFIG["log_autoflush"] == 1;
   }
   int log_level = 2;
   if (CONFIG["log_level"]) {
-    log_level = CONFIG["log_level"].as<int>();
+    log_level = (int)CONFIG["log_level"];
   }
 
   bool console = false;
   if (CONFIG["log_console"]) {
-    console = CONFIG["log_console"].as<int>() == 1;
+    console = (int)CONFIG["log_console"] == 1;
   }
 
   std::cout << "Logging to: ";
@@ -107,7 +107,10 @@ int main(int argc, char *argv[]) {
     return EXIT_FAILURE;
   }
 
-  CONFIG = YAML::LoadFile(argv[1]);
+  {
+    std::ifstream fin(argv[1]);
+    CONFIG = json::parse(fin); // YAML::LoadFile(argv[1]);
+  }
 
   init_logging();
 
@@ -124,13 +127,13 @@ int main(int argc, char *argv[]) {
     // The leaks are reported here, because this is the first usage of the
     // BOOST_LOG_TRIVIAL()! Comment these out, and the leaks are reported at the
     // next logging usage instance.
-    std::string value = CONFIG[key].as<std::string>();
+    std::string value = CONFIG[key];
     BUGZ_LOG(info) << "Config: " << key << " : " << value;
   }
 
   if (!config_ok) return EXIT_FAILURE;
 
-  int port = CONFIG["server"].as<int>();
+  int port = CONFIG["server"];
   // int port = 9999; // 2002;
 
   try {
@@ -146,8 +149,8 @@ int main(int argc, char *argv[]) {
     boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(),
                                             port);  // std::atoi(argv[i]));
     // connect to the game server
-    std::string host = CONFIG["host"].as<std::string>();
-    std::string port = CONFIG["port"].as<std::string>();
+    std::string host = CONFIG["host"];
+    std::string port = CONFIG["port"];
     BUGZ_LOG(fatal) << "host: " << host << " port: " << port;
 
     Server serve(io_service, endpoint, host, port, telnet);