123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- #include "galaxy.h"
- #include <algorithm> // sort
- #include <boost/format.hpp>
- #include <exception>
- #include <ostream>
- #include <set>
- #include <string>
- #include <fstream>
- #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;
- for (int x = 0; x < MAX_WARPS; ++x) warps[x] = 0;
- }
- void sector_warps::add(sector_type new_sector) {
- for (int x = 0; x < MAX_WARPS; ++x) {
- if (warps[x] == new_sector) return;
- if (warps[x] == 0) {
- warps[x] = new_sector;
- return;
- }
- }
- std::string message = str(boost::format("More then MAX %1% sectors for %2%") %
- MAX_WARPS % (int)sector);
- throw std::out_of_range(message);
- }
- std::ostream &operator<<(std::ostream &os, const sector_warps &warps) {
- os << "Sector: " << warps.sector << " ";
- for (int x = 0; x < MAX_WARPS; ++x) {
- if (warps.warps[x] != 0) {
- if (x != 0) os << ",";
- os << warps.warps[x];
- }
- }
- return os;
- }
- bool sector_sort(sector_type st1, sector_type st2) {
-
- if (st1 == 0) return false;
- if (st2 == 0) return false;
- return (st1 < st2);
- }
- void sector_warps::sort(void) {
- std::sort(&warps[0], &warps[MAX_WARPS], sector_sort);
- }
- bool sector_warps::operator==(const sector_warps &rhs) const {
-
- std::set<sector_type> contains;
- if (sector == rhs.sector) {
- int x;
- for (x = 0; x < MAX_WARPS; ++x) {
- if (warps[x] == 0) break;
- contains.insert(warps[x]);
- }
- for (x = 0; x < MAX_WARPS; ++x) {
- if (warps[0] == 0) break;
- auto pos = contains.find(rhs.warps[x]);
- if (pos == contains.end()) return false;
- contains.erase(pos);
- }
- return contains.empty();
- }
-
- return false;
- }
- #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::add_warp(sector_warps sw) {
- auto pos = warps.find(sw.sector);
- if (pos == warps.end()) {
-
- sw.sort();
- warps[sw.sector] = sw;
- BUGZ_LOG(info) << "add_warp NEW " << sw.sector;
- } else {
-
- if (pos->second == sw) {
- 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_class) {}
- void Galaxy::add_port(port p) {}
- namespace YAML {
- template <>
- struct convert<Galaxy> {
- static Node encode(const Galaxy &rhs) {
- Node node;
- for (auto const &config_iter : rhs.config) {
- node["config"][config_iter.first] = config_iter.second;
- }
- for (auto const &warp : rhs.warps) {
- for (int x = 0; x < MAX_WARPS; ++x) {
- if (warp.second.warps[x] == 0) break;
- node["warps"][warp.first].push_back(warp.second.warps[x]);
- }
- }
-
- return node;
- }
- static bool decode(const Node &node, Galaxy &rhs) {
- if (!node.IsMap()) return false;
- if (node["config"]) {
- } else {
- BUGZ_LOG(fatal) << "YAML missing config section.";
- }
- if (node["ports"]) {
- } else {
- BUGZ_LOG(fatal) << "YAML missing ports section.";
- }
- if (node["warps"]) {
- const Node &warps = node["warps"];
- if (warps.IsMap()) {
- 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>());
- }
- BUGZ_LOG(fatal) << "YAML warp: " << sw;
- rhs.add_warp(sw);
- }
- }
- } else {
- BUGZ_LOG(fatal) << "YAML missing warps section.";
- }
-
- return true;
- }
- };
- }
- void Galaxy::load(void) {
- std::string filename =
- str(boost::format("galaxy-%1%-%2%.json") % game % username);
-
- config.clear();
- ports.clear();
- warps.clear();
- if (file_exists(filename)) {
- YAML::Node data = YAML::LoadFile(filename);
- Galaxy g = data["galaxy"].as<Galaxy>();
- BUGZ_LOG(fatal) << "YAML: config keys: " << g.config.size();
- BUGZ_LOG(fatal) << "YAML: warp keys: " << g.warps.size();
- BUGZ_LOG(fatal) << "YAML: port keys: " << g.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;
- data["galaxy"] = *this;
- std::ofstream fout(filename);
- fout << data << std::endl;
- BUGZ_LOG(fatal) << "YAML: " << filename;
- }
|