123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381 |
- #include "galaxy.h"
- #include <algorithm> // sort
- #include <boost/format.hpp>
- #include <chrono>
- #include <exception>
- #include <fstream>
- #include <ostream>
- #include <set>
- #include <string>
- #include "logging.h"
- #include "yaml-cpp/yaml.h"
- bool buysell::operator==(const buysell &rhs) const {
- return ((foe[0] == rhs.foe[0]) && (foe[1] == rhs.foe[1]) &&
- (foe[2] == rhs.foe[2]));
- }
- std::ostream &operator<<(std::ostream &os, const buysell &bs) {
- os << bs.foe[0] << bs.foe[1] << bs.foe[2];
- return os;
- }
- bool buysell_text::operator==(const buysell_text &rhs) const {
- return ((txt[0] == rhs.txt[0]) && (txt[1] == rhs.txt[1]) &&
- (txt[2] == rhs.txt[2]));
- }
- std::ostream &operator<<(std::ostream &os, const buysell_text &bst) {
- os << '"' << bst.txt[0] << bst.txt[1] << bst.txt[2] << '"';
- return os;
- }
- std::ostream &operator<<(std::ostream &os, const port &p) {
- if (p.type == 0) {
- os << p.sector << ": " << (int)p.type;
- } else {
- os << p.sector << ": " << (int)p.type << " " << text_from_type(p.type)
- << " " << p.amount[0] << "," << p.amount[1] << "," << p.amount[2];
- }
- return os;
- }
- sector_warps::sector_warps() {
- sector = 0;
-
- }
- void sector_warps::add(sector_type new_sector) {
- warps.insert(new_sector);
-
- }
- std::ostream &operator<<(std::ostream &os, const sector_warps &warps) {
- os << "Sector: " << warps.sector << " ";
- bool comma = false;
- for (auto const &warp : warps.warps) {
- if (comma)
- os << ",";
- else
- comma = true;
- os << warp;
- }
-
- return os;
- }
- #define GTEST_COUT std::cerr << "[ ] [ INFO ]"
- struct port parse_portcim(const std::string line) {
- struct port p;
- p.sector = std::stoi(line);
-
- static std::regex portrx(
- "[ ]*([0-9]+) (.)[ ]+([0-9]+)[ ]+([0-9]+%) (.)[ "
- "]+([0-9]+)[ ]+([0-9]+%) (.)[ ]+([0-9]+)[ ]+([0-9]+%)[ ]*",
- std::regex_constants::ECMAScript);
-
-
-
-
-
-
-
-
-
-
-
- #ifdef GTEST_DEBUG
- GTEST_COUT << "Sector: " << p.sector << std::endl;
- GTEST_COUT << "Line: [" << line << "]" << std::endl;
- #endif
- buysell port_buysell;
- std::smatch matches;
- if (std::regex_match(line, matches, portrx)) {
- #ifdef GTEST_DEBUG
- for (size_t x = 1; x < matches.size(); ++x) {
- GTEST_COUT << x << " : " << matches[x] << std::endl;
- }
- #endif
- if (matches.size() != 11) {
- #ifdef GTEST_DEBUG
- GTEST_COUT << "Now you have 101 problems." << std::endl;
- #endif
- p.sector = 0;
- p.type = 0;
- return p;
- }
-
- p.sector = stoi(matches[1]);
-
-
-
-
- for (int x = 0; x < 3; ++x) {
- int pos = x * 3;
- port_buysell.foe[x] = matches[pos + 2] == "-";
- p.amount[x] = stoi(matches[pos + 3]);
- p.percent[x] = stoi(matches[pos + 4]);
- }
- p.type = type_from_buysell(port_buysell);
- #ifdef GTEST_DEBUG
- GTEST_COUT << "port is type " << (int)p.type << std::endl;
- #endif
- return p;
- } else {
- #ifdef GTEST_DEBUG
- GTEST_COUT << "regex_match failed." << std::endl;
- #endif
- p.type = 0;
- p.sector = 0;
- return p;
- }
- }
- Galaxy::Galaxy() {}
- Galaxy::~Galaxy() { BUGZ_LOG(fatal) << "Galaxy::~Galaxy()"; }
- void Galaxy::reset(void) {
- meta = YAML::Node();
- config = YAML::Node();
- ports.clear();
- warps.clear();
- }
- void Galaxy::add_warp(sector_warps sw) {
- auto pos = warps.find(sw.sector);
- if (pos == warps.end()) {
-
-
- warps[sw.sector] = sw;
- BUGZ_LOG(info) << "add_warp NEW " << sw.sector;
- } else {
-
- if (pos->second.warps == sw.warps) {
- BUGZ_LOG(trace) << "add_warp: Yup, I already know about " << sw.sector;
- } else {
- BUGZ_LOG(info) << "add_warp: Warps don't match! Updating...";
- BUGZ_LOG(warning) << "Have: " << pos->second;
- BUGZ_LOG(warning) << "Got : " << sw;
- warps[sw.sector] = sw;
- }
- }
- }
- void Galaxy::add_port(sector_type sector, int port_type) {
- auto pos = ports.find(sector);
- if (pos == ports.end()) {
-
- port p;
- p.sector = sector;
- p.type = port_type;
- for (int x = 0; x < 3; x++) {
- p.amount[x] = 0;
- p.percent[x] = 0;
- }
- BUGZ_LOG(trace) << "add_port: " << sector << ", " << port_type << " : "
- << p;
- ports[sector] = p;
- } else {
-
- if (pos->second.type == port_type) {
- BUGZ_LOG(trace) << "add_port: Yup, port " << sector << " is class "
- << port_type;
- } else {
- BUGZ_LOG(fatal) << "add_port: " << sector << " shows " << pos->second.type
- << " >> set to " << port_type;
- pos->second.type = port_type;
- }
- }
- }
- void Galaxy::add_port(port p) {
- auto pos = ports.find(p.sector);
- if (pos == ports.end()) {
- BUGZ_LOG(trace) << "add_port: NEW " << p;
- ports[p.sector] = p;
- } else {
- if (pos->second.type != p.type) {
- if ((pos->second.type == 9) && (p.type == 8)) {
- BUGZ_LOG(info) << "add_port: StarDock " << p.sector;
- p.type = 9;
- ports[p.sector] = p;
- } else {
- BUGZ_LOG(fatal) << "add_port: " << pos->second << " NEW : " << p;
- ports[p.sector] = p;
- }
- } else {
- if (pos->second.amount != p.amount) {
- BUGZ_LOG(info) << "add_port: UPDATE " << p.sector;
- pos->second = p;
- } else {
- BUGZ_LOG(info) << "add_port: Yup " << p.sector;
- }
- }
- }
- }
- void Galaxy::load(void) {
- std::string filename =
- str(boost::format("galaxy-%1%-%2%.json") % game % username);
-
- meta = YAML::Node();
- config = YAML::Node();
- ports.clear();
- warps.clear();
- if (file_exists(filename)) {
- YAML::Node data = YAML::LoadFile(filename);
- if (config["meta"]) meta = config["meta"];
- meta["load_from"] = filename;
- std::chrono::_V2::system_clock::time_point now =
- std::chrono::system_clock::now();
- meta["load_time"] = std::chrono::system_clock::to_time_t(now);
- if (data["config"]) {
- config = data["config"];
- } else {
- BUGZ_LOG(fatal) << "YAML Missing config section.";
- }
- if (data["ports"]) {
- const YAML::Node ports = data["ports"];
- for (auto const &port_iter : ports) {
- port p;
- p.sector = port_iter.first.as<int>();
- p.type = port_iter.second["class"].as<int>();
- int x = 0;
- for (auto const &amount : port_iter.second["amount"]) {
- p.amount[x] = amount.as<int>();
- ++x;
- }
- x = 0;
- for (auto const &pct : port_iter.second["pct"]) {
- p.percent[x] = pct.as<int>();
- ++x;
- }
- add_port(p);
- }
- } else {
- BUGZ_LOG(fatal) << "YAML Missing ports section.";
- }
- if (data["warps"]) {
- const YAML::Node &warps = data["warps"];
-
- for (auto const warp_iter : warps) {
- sector_warps sw;
- sw.sector = warp_iter.first.as<int>();
- for (auto const sector_iter : warp_iter.second) {
- sw.add(sector_iter.as<int>());
- }
-
- add_warp(sw);
- }
-
- } else {
- BUGZ_LOG(fatal) << "YAML Missing warps section.";
- }
- BUGZ_LOG(fatal) << "YAML: config keys: " << config.size();
- BUGZ_LOG(fatal) << "YAML: warp keys: " << warps.size();
- BUGZ_LOG(fatal) << "YAML: port keys: " << ports.size();
- } else {
- BUGZ_LOG(fatal) << "Missing YAML: " << filename;
- }
- }
- void Galaxy::save(void) {
- std::string filename =
- str(boost::format("galaxy-%1%-%2%.json") % game % username);
- YAML::Node data;
-
- meta["save_to"] = filename;
- std::chrono::_V2::system_clock::time_point now =
- std::chrono::system_clock::now();
- meta["save_time"] =
- std::chrono::system_clock::to_time_t(now);
- data["meta"] = meta;
- BUGZ_LOG(fatal) << "YAML config: " << config.size();
- data["config"] = config;
-
- BUGZ_LOG(fatal) << "YAML warps: " << warps.size();
- for (auto const &warp : warps) {
- for (auto const §or : warp.second.warps) {
- data["warps"][warp.first].push_back(sector);
- }
-
- }
- BUGZ_LOG(fatal) << "YAML ports: " << ports.size();
-
- for (auto const &port : ports) {
- data["ports"][port.second.sector]["class"] = (int)port.second.type;
- for (int x = 0; x < 3; x++) {
- data["ports"][port.second.sector]["amount"].push_back(
- (int)port.second.amount[x]);
- data["ports"][port.second.sector]["pct"].push_back(
- (int)port.second.percent[x]);
- }
- }
- std::ofstream fout(filename);
- fout << data << std::endl;
- BUGZ_LOG(fatal) << "YAML: " << filename;
- }
|