|
@@ -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 §or : 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.
|