Browse Source

Added at_command_prompt to utils. DRY.

Steve Thielemann 3 years ago
parent
commit
270a65795d
4 changed files with 81 additions and 27 deletions
  1. 64 15
      director.cpp
  2. 6 5
      test-galaxy.cpp
  3. 10 7
      utils.cpp
  4. 1 0
      utils.h

+ 64 - 15
director.cpp

@@ -84,20 +84,27 @@ void Director::client_input(const std::string &input) {
       to_client(current_raw_prompt);
       return;
     }
+
     //
     // The command prompt that we're looking for:
     //
     // "Command [TL=00:00:00]:[242] (?=Help)? : "
     // the time, and the sector number vary...
-
-    if (startswith(prompt, "Command [")) {
-      // if (prompt.substr(0, 9) == "Command [") {
-      //  int len = prompt.length();
-      if (endswith(prompt, "] (?=Help)? : ")) {
-        // if (prompt.substr(len - 14) == "] (?=Help)? : ") {
-        proxy_activate();
-        return;
+    /*
+        if (startswith(prompt, "Command [")) {
+          // if (prompt.substr(0, 9) == "Command [") {
+          //  int len = prompt.length();
+          if (endswith(prompt, "] (?=Help)? : ")) {
+            // if (prompt.substr(len - 14) == "] (?=Help)? : ") {
+            proxy_activate();
+            return;
+          }
+        }
       }
+    */
+    if (at_command_prompt(prompt)) {
+      proxy_activate();
+      return;
     }
   }
   // Ok...
@@ -117,7 +124,8 @@ void Director::server_line(const std::string &line,
     to_client("\rTradeWars Proxy v2++ READY (~ or ESC to activate)\n\r");
     /*
     There's a delay here when I save the game data.
-    I've moved it futher down.  Hide it at a prompt, so it isn't so noticeable.
+    I've moved it futher down.  Hide it at a prompt, so it isn't so
+    noticeable.
      */
     // reset "active game" -- we're at the TWGS main menu
   }
@@ -311,7 +319,7 @@ void Director::proxy_activate(void) {
     std::shared_ptr<Dispatch> readline = std::make_shared<InputDispatch>(*this);
     chain = readline;
     InputDispatch *id = static_cast<InputDispatch *>(&(*readline));
-    id->prompt = "\x1b[0m    \x1b[1;33;44m-=>\x1b[0m \x1b[1;37;44m";
+    id->prompt = "\x1b[0m\x1b[1;33;44m-=>\x1b[0m \x1b[1;37;44m";
     id->max_length = 15;
     id->setNotify([this]() { this->have_input(); });
     readline->activate();
@@ -410,7 +418,8 @@ void Director::menu_choice(void) {
       }
       /*
       std::string text = str(
-          boost::format("Back from Menu [%1%] was selected.\n\r") % md->input);
+          boost::format("Back from Menu [%1%] was selected.\n\r") %
+      md->input); 
       to_client(text);
       */
 
@@ -477,8 +486,8 @@ void Director::scripts_done(void) {
   proxy_activate();
 
   // And to end scripts, we do .. what exactly?
-  // DEBUG:  Ok, why does everything work OK if I reset the scripts_menu here??
-  // probably do want to destroy scripts here.  ;)
+  // DEBUG:  Ok, why does everything work OK if I reset the scripts_menu
+  // here?? probably do want to destroy scripts here.  ;)
   // scripts_menu.reset();
   // proxy_deactivate();
 }
@@ -665,8 +674,8 @@ void Director::SL_cimline(const std::string &line) {
   if (line == ": ") {
     // do I need to do anything special here for this?
     // Maybe -- We would save special ports that don't show up
-    // (StarDock/Special) before. We don't know (at this point) if this is warps
-    // or ports.
+    // (StarDock/Special) before. We don't know (at this point) if this is
+    // warps or ports.
     return;
   }
   if (line.empty()) {
@@ -827,7 +836,47 @@ void Director::SL_infoline(const std::string &line) {
     ++state;
     if (state == 2) {
       SL_parser = nullptr;
+
       // process the parsed information in meta["info"]
+      if (galaxy.meta["info"]["Total Holds"]) {
+        std::string work = galaxy.meta["info"]["Total Holds"].as<std::string>();
+        replace(work, "Fuel Ore", "Fuel");
+        auto parts = split(work, " - ");
+        int total_holds = stoi(parts[0]);
+        BUGZ_LOG(fatal) << "total holds: " << total_holds;
+        auto contents = split(parts[1]);
+        for (auto const &hold : contents) {
+          auto hold_amount = split(hold, "=");
+          BUGZ_LOG(fatal) << hold_amount[0] << " with " << hold_amount[1];
+        }
+      }
+      if (galaxy.meta["info"]["Turns to Warp"]) {
+        int warp_turns = galaxy.meta["info"]["Turns to Warp"].as<int>();
+        BUGZ_LOG(fatal) << "Turns to Warp: " << warp_turns;
+      }
+      if (galaxy.meta["info"]["Turns left"]) {
+        int turns = galaxy.meta["info"]["Turns left"].as<int>();
+        BUGZ_LOG(fatal) << "Turns left: " << turns;
+      }
+      if (galaxy.meta["info"]["LongRange Scan"]) {
+        std::string scanner_text =
+            galaxy.meta["info"]["LongRange Scan"].as<std::string>();
+        char scanner = scanner_text[0];
+        BUGZ_LOG(fatal) << "Scanner: " << scanner;
+      }
+      if (galaxy.meta["info"]["Current Sector"]) {
+        int sector = galaxy.meta["info"]["Current Sector"].as<int>();
+        BUGZ_LOG(fatal) << "Sector: " << sector;
+        // it should already be sector ...
+        current_sector = sector;
+      }
+      if (galaxy.meta["info"]["Credits"]) {
+        std::string credit_text =
+            galaxy.meta["info"]["Credits"].as<std::string>();
+        replace(credit_text, ",", "");
+        int credits = stoi(credit_text);
+        BUGZ_LOG(fatal) << "Credits: " << credits;
+      }
     }
     return;
   }

+ 6 - 5
test-galaxy.cpp

@@ -122,10 +122,10 @@ TEST(ports, trade_types) {
   expected[pair{4, 5}] = 1;
 
   // I'm checking 0's and 1's.
-  expected[pair{1,3}] = 2;
-  expected[pair{2,3}] = 2;
-  expected[pair{4,6}] = 2;
-  expected[pair{5,6}] = 2;
+  expected[pair{1, 3}] = 2;
+  expected[pair{2, 3}] = 2;
+  expected[pair{4, 6}] = 2;
+  expected[pair{5, 6}] = 2;
 
   /*
   GTEST_COUT << "size:" << expected.size() << std::endl;
@@ -149,7 +149,8 @@ TEST(ports, trade_types) {
       if (r != expected.end()) {
         EXPECT_EQ(t, r->second) << "Comparision";
       } else {
-        GTEST_COUT << "TODO: Need type for " << p1 << " , " << p2 << " : " << t
+        GTEST_COUT << "TODO: Need type for " << p1 << " " << text_from_type(p1)
+                   << " , " << p2 << " " << text_from_type(p2) << " : " << t
                    << std::endl;
       }
     }

+ 10 - 7
utils.cpp

@@ -113,12 +113,11 @@ std::vector<std::string> split(const std::string &line, const std::string &by) {
   std::string work = line;
   std::vector<std::string> results;
   size_t pos;
-  while ( (pos = work.find(by) ) != std::string::npos ) {
+  while ((pos = work.find(by)) != std::string::npos) {
     results.push_back(work.substr(0, pos));
     work.erase(0, pos + by.length());
   }
-  if (!work.empty())
-    results.push_back(work);
+  if (!work.empty()) results.push_back(work);
   return results;
 }
 
@@ -136,8 +135,12 @@ bool endswith(const std::string &line, const std::string &has) {
 }
 
 void trim(std::string &str) {
-  while (str.substr(0,1) == " ")
-    str.erase(0, 1);
-  while (str.substr(str.length() -1) == " ")
-    str.erase(str.length() -1);
+  while (str.substr(0, 1) == " ") str.erase(0, 1);
+  while (str.substr(str.length() - 1) == " ") str.erase(str.length() - 1);
 }
+
+bool at_command_prompt(const std::string &prompt) {
+  if (startswith(prompt, "Command ["))
+    if (endswith(prompt, "] (?=Help)? : ")) return true;
+  return false;
+}

+ 1 - 0
utils.h

@@ -22,5 +22,6 @@ bool in(const std::string &line, const std::string &has);
 std::string repr(const std::string &source);
 
 bool file_exists(const std::string &name);
+bool at_command_prompt(const std::string &prompt);
 
 #endif