Преглед изворни кода

Galaxy find_trades uses burnt_percent. YAML cleaner.

If port.type == 0, don't save/load extra stuff.
Steve Thielemann пре 3 година
родитељ
комит
963ef7b1b7
2 измењених фајлова са 126 додато и 58 уклоњено
  1. 105 48
      galaxy.cpp
  2. 21 10
      galaxy.h

+ 105 - 48
galaxy.cpp

@@ -45,7 +45,7 @@ std::ostream &operator<<(std::ostream &os, const port &p) {
   return os;
 }
 
-int trade_type(port_type port1, port_type port2) {
+trade_type_result trade_type_info(port_type port1, port_type port2) {
   // NONE = 0
   // GOOD = 1 = OE PAIR
   // OK   = 2 = ?? Pair
@@ -54,12 +54,6 @@ int trade_type(port_type port1, port_type port2) {
   buysell p1 = get_buysell(port1);
   buysell p2 = get_buysell(port2);
 
-  // O != E for both ports, and O != O
-  if ((p1.foe[ORG] != p1.foe[EQU]) && (p2.foe[ORG] != p2.foe[EQU]) &&
-      (p1.foe[ORG] != p2.foe[ORG])) {
-    return 1;
-  }
-
   buysell inv2 = invert_buysell(p2);
   int matches = 0;  // or pos.size();
   std::vector<int> pos;
@@ -74,20 +68,31 @@ int trade_type(port_type port1, port_type port2) {
   }
 
   if (matches > 1) {
+    // O != E for both ports, and O != O
+    if ((p1.foe[ORG] != p1.foe[EQU]) && (p2.foe[ORG] != p2.foe[EQU]) &&
+        (p1.foe[ORG] != p2.foe[ORG])) {
+      return trade_type_result{1, inv2};
+    }
+
     // at least 2 matches.  but are they trade pairs?
     // I can tell by comparing the last two positions in the same port.
     if (p1.foe[pos[matches - 1]] == p1.foe[pos[matches - 2]]) {
       // they are NOT.
-      return 3;
+      return trade_type_result{3, inv2};
     }
-    return 2;
+    return trade_type_result{2, inv2};
   }
 
   if (matches == 1) {
-    if (inv2.foe[FUEL]) return 4;
-    return 3;
+    if (inv2.foe[FUEL]) return trade_type_result{4, inv2};
+    return trade_type_result{3, inv2};
   }
-  return 0;
+  return trade_type_result{0, inv2};
+}
+
+int trade_type(port_type port1, port_type port2) {
+  trade_type_result r = trade_type_info(port1, port2);
+  return r.type;
 }
 
 /*
@@ -222,7 +227,8 @@ struct port parse_portcim(const std::string line) {
   }
 }
 
-Galaxy::Galaxy() {}
+Galaxy::Galaxy() { burnt_percent = 40; }
+
 Galaxy::~Galaxy() { BUGZ_LOG(fatal) << "Galaxy::~Galaxy()"; }
 
 void Galaxy::reset(void) {
@@ -239,11 +245,11 @@ void Galaxy::add_warp(sector_warps sw) {
     // not found
     // sw.sort();
     warps[sw.sector] = sw;
-    BUGZ_LOG(info) << "add_warp NEW " << sw.sector;
+    // BUGZ_LOG(info) << "add_warp NEW " << sw.sector;
   } else {
     // found!
     if (pos->second.warps == sw.warps) {
-      BUGZ_LOG(trace) << "add_warp: Yup, I already know about " << sw.sector;
+      // 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;
@@ -264,14 +270,14 @@ void Galaxy::add_port(sector_type sector, int port_type) {
       p.amount[x] = 0;
       p.percent[x] = 0;
     }
-    BUGZ_LOG(trace) << "add_port: " << sector << ", " << port_type << " : "
-                    << p;
+    // BUGZ_LOG(trace) << "add_port: " << sector << ", " << port_type << " : "
+    // << p;
     ports[sector] = p;
   } else {
     // port was found, so:
     if (pos->second.type == port_type) {
-      BUGZ_LOG(trace) << "add_port: Yup, port " << sector << " is class "
-                      << 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;
@@ -283,12 +289,12 @@ void Galaxy::add_port(sector_type sector, int 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;
+    // 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;
+        BUGZ_LOG(trace) << "add_port: StarDock " << p.sector;
         p.type = 9;
         ports[p.sector] = p;
       } else {
@@ -297,10 +303,10 @@ void Galaxy::add_port(port p) {
       }
     } else {
       if (pos->second.amount != p.amount) {
-        BUGZ_LOG(info) << "add_port: UPDATE " << p.sector;
+        // BUGZ_LOG(info) << "add_port: UPDATE " << p.sector;
         pos->second = p;
       } else {
-        BUGZ_LOG(info) << "add_port: Yup " << p.sector;
+        // BUGZ_LOG(info) << "add_port: Yup " << p.sector;
       }
     }
   }
@@ -333,15 +339,24 @@ void Galaxy::load(void) {
         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;
+        int x;
+        if (p.type == 0) {
+          // nothing to load for type = 0, set defaults.
+          for (x = 0; x < 3; ++x) {
+            p.amount[x] = 0;
+            p.percent[x] = 0;
+          }
+        } else {
+          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);
       }
@@ -385,8 +400,11 @@ void Galaxy::save(void) {
   meta["save_time"] = std::chrono::system_clock::to_time_t(now);  // time_t
 
   data["meta"] = meta;
+  // data["meta"].SetStyle(YAML::EmitterStyle::Flow);
   BUGZ_LOG(fatal) << "YAML config: " << config.size();
   data["config"] = config;
+  data["config"].SetStyle(YAML::EmitterStyle::Flow);
+
   /*
   for (auto const &config_iter : config) {
     data["config"][config_iter.first] = config_iter.second;
@@ -412,15 +430,22 @@ void Galaxy::save(void) {
    */
   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]);
+    if (port.second.type == 0) {
+      data["ports"][port.second.sector].SetStyle(YAML::EmitterStyle::Flow);
+    } else {
+      // nothing to save for type = 0
+      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]);
+      }
+      data["ports"][port.second.sector]["amount"].SetStyle(
+          YAML::EmitterStyle::Flow);
+      data["ports"][port.second.sector]["pct"].SetStyle(
+          YAML::EmitterStyle::Flow);
+      data["ports"][port.second.sector].SetStyle(YAML::EmitterStyle::Flow);
     }
-    data["ports"][port.second.sector]["amount"].SetStyle(YAML::EmitterStyle::Flow);
-    data["ports"][port.second.sector]["pct"].SetStyle(YAML::EmitterStyle::Flow);
-    data["ports"][port.second.sector].SetStyle(YAML::EmitterStyle::Flow);
   }
 
   std::ofstream fout(filename);
@@ -431,7 +456,7 @@ void Galaxy::save(void) {
 std::vector<port_pair_type> Galaxy::find_trades(sector_type sector,
                                                 bool highest) {
   std::vector<port_pair_type> pptv;
-  
+
   // Does this sector have a port?
   auto port = ports.find(sector);
   if (port == ports.end()) return pptv;
@@ -439,7 +464,6 @@ std::vector<port_pair_type> Galaxy::find_trades(sector_type sector,
   auto port_warps = warps.find(sector);
   // warps not found for port sector?
   if (port_warps == warps.end()) return pptv;
-  // TODO: Check if ports are burnt out
 
   for (auto const &s : port_warps->second.warps) {
     // only count the ports > our sector number -- so we don't have dups
@@ -460,13 +484,36 @@ std::vector<port_pair_type> Galaxy::find_trades(sector_type sector,
     if (possible_port == ports.end()) continue;
 
     // calculate trade type
-    int t = trade_type(port->second.type, possible_port->second.type);
-    if ((t == 0) || (t == 4)) continue;
-
-    // Are any of the trade pairs burnt out?
+    // int t = trade_type(port->second.type, possible_port->second.type);
+    trade_type_result ttr =
+        trade_type_info(port->second.type, possible_port->second.type);
+    if ((ttr.type == 0) || (ttr.type == 4)) continue;
+
+    // Are any of the trade pairs burnt out?  FUTURE: Are the lines we're
+    // interested in burnt? TODO:  Are the ports unknown?  (all percent=0?)
+    bool burnt = false;
+    for (int x = 0; x < 3; ++x) {
+      if (ttr.trades.foe[x]) {
+        if ((possible_port->second.percent[0] != 0) ||
+            (possible_port->second.percent[1] != 0) ||
+            (possible_port->second.percent[2] != 0))
+          if (possible_port->second.percent[x] < burnt_percent) burnt = true;
+        if ((port->second.percent[0] != 0) || (port->second.percent[1] != 0) ||
+            (port->second.percent[2] != 0))
+          if (port->second.percent[x] < burnt_percent) burnt = true;
+      }
+    }
+    if (burnt) continue;
+    /*
+    if ((possible_port->second.percent[0] < burnt_percent) ||
+        (possible_port->second.percent[1] < burnt_percent) ||
+        (possible_port->second.percent[2] < burnt_percent))
+      continue;
+    */
 
-    pptv.push_back(port_pair_type{t, sector, s});
-    BUGZ_LOG(fatal) << "sector: " << sector << " and " << s << " tt:" << t;
+    pptv.push_back(port_pair_type{ttr.type, sector, s});
+    BUGZ_LOG(trace) << "sector: " << sector << " and " << s
+                    << " tt:" << ttr.type;
   }
   return pptv;
 }
@@ -486,6 +533,16 @@ 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>();
+  if (burnt_percent > 90) {
+    burnt_percent = 90;
+    config["burnt_percent"] = 90;
+  }
+  if (burnt_percent < 10) {
+    burnt_percent = 10;
+    config["burnt_percent"] = 10;
+  }
+
   for (auto const &pi : ports) {
     if (pi.second.type == 0) continue;
     sector_type sector = pi.second.sector;

+ 21 - 10
galaxy.h

@@ -155,22 +155,30 @@ constexpr buysell invert_buysell(const buysell market) {
 
 /**
  * Find possible trades with these two port types
- * 
+ *
  * 0 = NONE
  * 1 = GOOD OE trade pair
  * 2 = OK trade pair
  * 3 = FAIR (one buys, one sells)
- * 
+ *
  * This will probably be expanded in the future to return:
  *    What port(s) to trade with.  (pos, return top 2)
- * 
- * @param port1 
- * @param port2 
- * @return int 
+ *
+ * @param port1
+ * @param port2
+ * @return int
  */
 int trade_type(port_type port1, port_type port2);
-struct port_pair_type { int type; sector_type s1, s2; };
+struct trade_type_result {
+  int type;
+  buysell trades;
+};
+trade_type_result trade_type_info(port_type port1, port_type port2);
 
+struct port_pair_type {
+  int type;
+  sector_type s1, s2;
+};
 
 constexpr uint8_t type_from_buysell(const buysell market) {
   if (market.foe[0]) {
@@ -223,6 +231,8 @@ class Galaxy {
   YAML::Node config;
   YAML::Node meta;
 
+  int burnt_percent;
+
   // warps;
   // ports;
   std::map<sector_type, port> ports;
@@ -236,9 +246,10 @@ class Galaxy {
   void load(void);
 
   std::vector<port_pair_type> find_best_trades(void);
-  std::vector<port_pair_type> find_trades(sector_type sector, bool highest=true);
-  void sort_port_pair_type(std::vector<port_pair_type> &pptv);
-  
+  std::vector<port_pair_type> find_trades(sector_type sector,
+                                          bool highest = true);
+  void sort_port_pair_type(std::vector<port_pair_type>& pptv);
+
   char game;
   std::string username;
 };