فهرست منبع

Display best trades. Update YAML output.

Steve Thielemann 3 سال پیش
والد
کامیت
d0ad9a1746
3فایلهای تغییر یافته به همراه109 افزوده شده و 7 حذف شده
  1. 21 1
      director.cpp
  2. 78 5
      galaxy.cpp
  3. 10 1
      galaxy.h

+ 21 - 1
director.cpp

@@ -315,7 +315,19 @@ void Director::menu_choice(void) {
         case 'C':  // configure
           break;
         case 'D':  // display report
-          break;
+        {
+          auto pptv = galaxy.find_best_trades();
+          std::string output;
+          galaxy.sort_port_pair_type(pptv);
+          int max_display = 25;
+          for (auto const &ppt : pptv) {
+            output = str(boost::format("%1$5d:%2$5d => %3$d\n\r") % ppt.s1 %
+                         ppt.s2 % ppt.type);
+            to_client(output);
+            max_display--;
+            if (max_display == 0) break;
+          }
+        } break;
         case 'E':  // Export Data/Save
           to_client("Saving...");
           galaxy.save();
@@ -483,6 +495,14 @@ void Director::SL_sectorline(const std::string &line) {
     sectorline: [           in Star Stomper (Sverdlov Merchant Cruiser)]
     sectorline: [Warps to Sector(s) :  70 - 441 - 575 - 600 - 629 - 711]
     sectorline: [Warps to Sector(s) :  70 - (475) - 569]
+
+    What can we get from Traders : line?  Can we get if they are hostile
+    to us?  We need to respond to "is powering up weapons" ... We can
+    react faster then a person can!
+    "phil is powering up weapons systems!"
+
+    Also the auto-attack ones Ferrengi -- we need to auto-respond Retreat.
+
     */
     if (in(line, "Sector  :")) {
       current_sector = stoi(line.substr(10));

+ 78 - 5
galaxy.cpp

@@ -84,8 +84,7 @@ int trade_type(port_type port1, port_type port2) {
   }
 
   if (matches == 1) {
-    if (inv2.foe[FUEL])
-      return 4;
+    if (inv2.foe[FUEL]) return 4;
     return 3;
   }
   return 0;
@@ -383,8 +382,7 @@ void Galaxy::save(void) {
   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);  // time_t
+  meta["save_time"] = std::chrono::system_clock::to_time_t(now);  // time_t
 
   data["meta"] = meta;
   BUGZ_LOG(fatal) << "YAML config: " << config.size();
@@ -399,6 +397,7 @@ void Galaxy::save(void) {
     for (auto const &sector : warp.second.warps) {
       data["warps"][warp.first].push_back(sector);
     }
+    data["warps"][warp.first].SetStyle(YAML::EmitterStyle::Flow);
     /*
     for (int x = 0; x < MAX_WARPS; ++x) {
       if (warp.second.warps[x] == 0) break;
@@ -419,9 +418,83 @@ void Galaxy::save(void) {
       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);
   }
 
   std::ofstream fout(filename);
   fout << data << std::endl;
   BUGZ_LOG(fatal) << "YAML: " << filename;
-}
+}
+
+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;
+
+  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
+    if (highest && (s < sector)) continue;
+
+    // verify we have a way back
+    {
+      auto warpback = warps.find(s);
+      // can we find that warp?
+      if (warpback == warps.end()) continue;
+      // does it link back to the port's sector?
+      if (warpback->second.warps.find(sector) == warpback->second.warps.end())
+        continue;
+    }
+
+    // Does this sector have a port?
+    auto possible_port = ports.find(s);
+    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?
+
+    pptv.push_back(port_pair_type{t, sector, s});
+    BUGZ_LOG(fatal) << "sector: " << sector << " and " << s << " tt:" << t;
+  }
+  return pptv;
+}
+
+bool compare_port_pair(const port_pair_type &ppt1, const port_pair_type &ppt2) {
+  if (ppt1.type == ppt2.type) {
+    return ppt1.s1 < ppt2.s1;
+  } else {
+    return (ppt1.type < ppt2.type);
+  }
+}
+
+void Galaxy::sort_port_pair_type(std::vector<port_pair_type> &pptv) {
+  sort(pptv.begin(), pptv.end(), compare_port_pair);
+}
+
+std::vector<port_pair_type> Galaxy::find_best_trades(void) {
+  std::vector<port_pair_type> pptv;
+
+  for (auto const &pi : ports) {
+    if (pi.second.type == 0) continue;
+    sector_type sector = pi.second.sector;
+    auto port_warps = warps.find(sector);
+    if (port_warps == warps.end()) continue;
+
+    std::vector<port_pair_type> ppt_sector = find_trades(sector, true);
+    if (ppt_sector.empty()) continue;
+    pptv.insert(pptv.end(), ppt_sector.begin(), ppt_sector.end());
+  }
+  return pptv;
+}

+ 10 - 1
galaxy.h

@@ -161,11 +161,16 @@ constexpr buysell invert_buysell(const buysell market) {
  * 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 
  */
 int trade_type(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]) {
@@ -196,7 +201,7 @@ constexpr uint8_t type_from_buysell(const buysell market) {
 }
 
 struct port {
-  uint16_t sector;
+  sector_type sector;
   uint8_t type;
   uint16_t amount[3];
   uint8_t percent[3];
@@ -230,6 +235,10 @@ class Galaxy {
   void save(void);
   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);
+  
   char game;
   std::string username;
 };