Browse Source

Merge branch 'master' into json

Steve Thielemann 3 years ago
parent
commit
2035637b49
12 changed files with 197 additions and 51 deletions
  1. 10 11
      CMakeLists.conan
  2. 10 9
      CMakeLists.main
  3. 6 5
      CMakeLists.txt
  4. 98 0
      CMakeLists.txt__static_build
  5. 19 12
      TradeWars Proxy Notes.md
  6. 12 0
      director.cpp
  7. 9 10
      dispatchers.cpp
  8. 1 1
      dispatchers.h
  9. 4 2
      scripts.cpp
  10. 1 1
      scripts.h
  11. 1 0
      utils.cpp
  12. 26 0
      yaml-2-json.py

+ 10 - 11
CMakeLists.conan

@@ -37,15 +37,14 @@ conan_basic_setup()
 set(CMAKE_CXX_STANDARD   17)
 set(CMAKE_CXX_EXTENSIONS ON)
 
-ADD_DEFINITIONS(-DBOOST_LOG_DYN_LINK)
+# ADD_DEFINITIONS(-DBOOST_LOG_DYN_LINK)
 
-if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/yaml-cpp)
-  message("***")
-  message("*** ERROR/MISSING *** please run: git clone https://github.com/jbeder/yaml-cpp.git --depth 1")
-  message("***")
-endif()
-
-add_subdirectory(yaml-cpp)
+# We only need the one header file.
+# if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/json)
+#   message("***")
+#   message("*** ERROR/MISSING *** please run: git clone https://github.com/nlohmann/json.git --depth 1")
+#   message("***")
+# endif()
 
 if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/googletest)
   message("***")
@@ -61,11 +60,11 @@ option(gtest_disable_pthreads "Disable uses of pthreads in gtest." OFF)
 ### TESTS
 add_executable(test-galaxy test-galaxy.cpp galaxy.cpp config.cpp utils.cpp buysell.cpp)
 add_dependencies(test-galaxy gtest)
-target_link_libraries(test-galaxy gtest_main ${CONAN_LIBS} yaml-cpp)
+target_link_libraries(test-galaxy gtest_main ${CONAN_LIBS})
 
 add_executable(test-director test-director.cpp galaxy.cpp utils.cpp buysell.cpp director.cpp dispatchers.cpp boxes.cpp scripts.cpp config.cpp ansicolor.cpp)
 add_dependencies(test-director gtest)
-target_link_libraries(test-director gtest_main ${CONAN_LIBS} yaml-cpp)
+target_link_libraries(test-director gtest_main ${CONAN_LIBS})
 
 enable_testing()
 add_test(NAME test-galaxy
@@ -75,7 +74,7 @@ add_test(NAME test-director
 
 # dispatchers.cpp 
 ADD_EXECUTABLE( twproxy twproxy.cpp utils.cpp session.cpp boxes.cpp director.cpp galaxy.cpp dispatchers.cpp scripts.cpp buysell.cpp config.cpp ansicolor.cpp)
-TARGET_LINK_LIBRARIES( twproxy ${CONAN_LIBS} pthread yaml-cpp)
+TARGET_LINK_LIBRARIES( twproxy ${CONAN_LIBS} pthread)
 
 # Remove precompile headers, that also doesn't work.
 # target_precompile_headers(twproxy PRIVATE pch.hpp)

+ 10 - 9
CMakeLists.main

@@ -36,13 +36,14 @@ ADD_DEFINITIONS(-DBOOST_LOG_DYN_LINK)
 FIND_PACKAGE( Boost 1.60 COMPONENTS program_options log_setup log REQUIRED )
 INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} )
 
-if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/yaml-cpp)
-  message("***")
-  message("*** ERROR/MISSING *** please run: git clone https://github.com/jbeder/yaml-cpp.git --depth 1")
-  message("***")
-endif()
+# We only need the one header file.
+# if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/json)
+#   message("***")
+#   message("*** ERROR/MISSING *** please run: git clone https://github.com/nlohmann/json.git --depth 1")
+#   message("***")
+# endif()
 
-add_subdirectory(yaml-cpp)
+add_subdirectory(json)
 
 if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/googletest)
   message("***")
@@ -58,11 +59,11 @@ option(gtest_disable_pthreads "Disable uses of pthreads in gtest." OFF)
 ### TESTS
 add_executable(test-galaxy test-galaxy.cpp galaxy.cpp config.cpp utils.cpp buysell.cpp)
 add_dependencies(test-galaxy gtest)
-target_link_libraries(test-galaxy gtest_main ${Boost_LIBRARIES} yaml-cpp)
+target_link_libraries(test-galaxy gtest_main ${Boost_LIBRARIES})
 
 add_executable(test-director test-director.cpp galaxy.cpp utils.cpp buysell.cpp director.cpp dispatchers.cpp boxes.cpp scripts.cpp config.cpp ansicolor.cpp)
 add_dependencies(test-director gtest)
-target_link_libraries(test-director gtest_main ${Boost_LIBRARIES} yaml-cpp)
+target_link_libraries(test-director gtest_main ${Boost_LIBRARIES})
 
 enable_testing()
 add_test(NAME test-galaxy
@@ -72,7 +73,7 @@ add_test(NAME test-director
 
 # dispatchers.cpp 
 ADD_EXECUTABLE( twproxy twproxy.cpp utils.cpp session.cpp boxes.cpp director.cpp galaxy.cpp dispatchers.cpp scripts.cpp buysell.cpp config.cpp ansicolor.cpp)
-TARGET_LINK_LIBRARIES( twproxy ${Boost_LIBRARIES} pthread yaml-cpp)
+TARGET_LINK_LIBRARIES( twproxy ${Boost_LIBRARIES} pthread)
 
 target_precompile_headers(twproxy PRIVATE pch.hpp)
 

+ 6 - 5
CMakeLists.txt

@@ -36,11 +36,12 @@ ADD_DEFINITIONS(-DBOOST_LOG_DYN_LINK)
 FIND_PACKAGE( Boost 1.60 COMPONENTS program_options log_setup log REQUIRED )
 INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} )
 
-if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/json)
-  message("***")
-  message("*** ERROR/MISSING *** please run: git clone https://github.com/nlohmann/json.git --depth 1")
-  message("***")
-endif()
+# We only need the one header file.
+# if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/json)
+#   message("***")
+#   message("*** ERROR/MISSING *** please run: git clone https://github.com/nlohmann/json.git --depth 1")
+#   message("***")
+# endif()
 
 add_subdirectory(json)
 

+ 98 - 0
CMakeLists.txt__static_build

@@ -0,0 +1,98 @@
+cmake_minimum_required(VERSION 3.0)
+project(ex1
+  VERSION 0.1
+  LANGUAGES CXX)
+
+###########
+# Debug or Release
+###########
+if (NOT CMAKE_BUILD_TYPE)
+  ## set default to Debug
+  set(CMAKE_BUILD_TYPE RelWithDebInfo)  # override with -DCMAKE_BUILD_TYPE=Release
+  message("==> CMAKE_BUILD_TYPE empty. Changing it to Debug.")
+else()
+  message("==> CMAKE_BUILD_TYPE == ${CMAKE_BUILD_TYPE}.")
+endif()
+
+
+
+## set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_GLIBCXX_DEBUG -DGLIBCXX_FORCE_NEW")
+
+## https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
+## During Debug, use debug version of libstdc++ (asserts on access to invalid iterators, etc!)
+## set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_GLIBCXX_DEBUG")
+set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_GLIBCXX_DEBUG")
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
+
+##############
+# C++ Standard
+##############
+# set(CMAKE_CXX_STANDARD   14)
+# C++17 gives me transform
+set(CMAKE_CXX_STANDARD   17)
+set(CMAKE_CXX_EXTENSIONS ON)
+
+# The old fix for getting logging to work/link properly.
+# ADD_DEFINITIONS(-DBOOST_LOG_DYN_LINK)
+
+# This seems to work for static linking the boost libraries
+set(Boost_USE_STATIC_LIBS   ON)
+
+# add log_setup log 
+FIND_PACKAGE( Boost 1.71 COMPONENTS program_options log_setup log REQUIRED )
+INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} )
+
+if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/yaml-cpp)
+  message("***")
+  message("*** ERROR/MISSING *** please run: git clone https://github.com/jbeder/yaml-cpp.git --depth 1")
+  message("***")
+endif()
+
+add_subdirectory(yaml-cpp)
+
+if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/googletest)
+  message("***")
+  message("*** ERROR/MISSING *** please run: git clone https://github.com/google/googletest.git --depth 1")
+  message("***")
+endif()
+
+add_subdirectory(googletest)
+
+option(gtest_build_samples "Build gtest's sample programs." OFF)
+option(gtest_disable_pthreads "Disable uses of pthreads in gtest." OFF)
+
+### TESTS
+add_executable(test-galaxy test-galaxy.cpp galaxy.cpp config.cpp utils.cpp buysell.cpp)
+add_dependencies(test-galaxy gtest)
+target_link_libraries(test-galaxy gtest_main ${Boost_LIBRARIES} yaml-cpp)
+
+add_executable(test-director test-director.cpp galaxy.cpp utils.cpp buysell.cpp director.cpp dispatchers.cpp boxes.cpp scripts.cpp config.cpp)
+add_dependencies(test-director gtest)
+target_link_libraries(test-director gtest_main ${Boost_LIBRARIES} yaml-cpp)
+
+enable_testing()
+add_test(NAME test-galaxy
+  COMMAND test-galaxy)
+add_test(NAME test-director
+  COMMAND test-director)
+
+# dispatchers.cpp 
+ADD_EXECUTABLE( twproxy twproxy.cpp utils.cpp session.cpp boxes.cpp director.cpp galaxy.cpp dispatchers.cpp scripts.cpp buysell.cpp config.cpp)
+
+#Static start
+set_target_properties(twproxy PROPERTIES LINK_SEARCH_START_STATIC 1)
+set_target_properties(twproxy PROPERTIES LINK_SEARCH_END_STATIC 1)
+set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
+#Static Libs
+
+#Set Linker flags
+set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
+
+TARGET_LINK_LIBRARIES( twproxy -static ${Boost_LIBRARIES} pthread.a yaml-cpp)
+# target_link_libraries(test -static)
+
+# private does nothing here
+# TARGET_LINK_LIBRARIES( twproxy PRIVATE ${Boost_LIBRARIES} pthread yaml-cpp)
+
+target_precompile_headers(twproxy PRIVATE pch.hpp)
+

+ 19 - 12
TradeWars Proxy Notes.md

@@ -2,7 +2,13 @@
 
 ## TODO
 
-* I have the ANSI music from Yankee Trader.
+* I have the ANSI music from Yankee Trader.  Sound "works" sort-of using ```padsp syncterm202109``` but crackles randomly.  (If I remember correctly, the static is more frequent on slower cpus/David's system.)  :cry:
+*  VoyagerScript: BUG:  Handle sectors to avoid.  (Query the list, allow sectors to be handled that we should avoid)
+*  Sectors of concern:  Store sectors that show in density that are > 1000.
+*  
+*  Move Failed:  Show Why and Where.  (Which sector, what's the density?)
+*  
+* Static linking the proxy to stuff into docker container.  We dynamic link boost_threads, filesystem and log right now.
 * 
 * TradeDispatch -- return some value that says we've Traded/or there's nothing here to trade with.  (Keep calling TradeDispatch until nothing -- then move on!)
 * MoveDispatch -- return success that we moved to a sector.  (Should we give it two sectors that we want to trade with -- and have it move to the nearest/closest one?)  That way we don't waste a move (going to the furthest one, only to move back to the nearer)?
@@ -13,17 +19,17 @@
 * If I go into a sector with a port, can I do computer/ask about that port?  That might be a nicer way of exploring the galaxy -- no need to update ports, we'd already have the port's information! Yes, this works.  We also get what we have in our holds.  This is great for trading!
 * I'm able to tell which port I can start trading with, should I check that before moving?  Or is it not worth it to waste X turns?
 * FAILED:  I have fuel in my holds, ports are only selling fuel. 
-2021-11-03 00:26:03.586801    fatal (   director.cpp:252 ) Sector: 9994
-2021-11-03 00:26:03.586900    fatal (    scripts.cpp:180 ) trades: falsetruefalse
-2021-11-03 00:26:03.586945    fatal (    scripts.cpp:181 ) port0:"SSB"
-2021-11-03 00:26:03.586985    fatal (    scripts.cpp:182 ) port1:"SBB"
-2021-11-03 00:26:03.587059    fatal (    scripts.cpp:222 ) !have_buy: port 0
-2021-11-03 00:26:03.401724    trace (     galaxy.cpp:478 ) find_trades: Port 9994,4 9873
-2021-11-03 00:26:03.401771    trace (     galaxy.cpp:496 ) sector: 9994 and 987 tt:3
-2021-11-03 00:26:03.401820    fatal (   director.cpp:487 ) Found 1 possible trade(s).
-2021-11-03 00:26:03.401860    fatal (   director.cpp:488 ) 9994,987 : 3
-2021-11-03 00:26:03.401899    fatal (    scripts.cpp:14  ) ScriptTrader::activate 9994 & 987
-2021-11-03 00:26:03.401939    fatal (    scripts.cpp:21  ) 4 and 3
+  2021-11-03 00:26:03.586801    fatal (   director.cpp:252 ) Sector: 9994
+  2021-11-03 00:26:03.586900    fatal (    scripts.cpp:180 ) trades: falsetruefalse
+  2021-11-03 00:26:03.586945    fatal (    scripts.cpp:181 ) port0:"SSB"
+  2021-11-03 00:26:03.586985    fatal (    scripts.cpp:182 ) port1:"SBB"
+  2021-11-03 00:26:03.587059    fatal (    scripts.cpp:222 ) !have_buy: port 0
+  2021-11-03 00:26:03.401724    trace (     galaxy.cpp:478 ) find_trades: Port 9994,4 9873
+  2021-11-03 00:26:03.401771    trace (     galaxy.cpp:496 ) sector: 9994 and 987 tt:3
+  2021-11-03 00:26:03.401820    fatal (   director.cpp:487 ) Found 1 possible trade(s).
+  2021-11-03 00:26:03.401860    fatal (   director.cpp:488 ) 9994,987 : 3
+  2021-11-03 00:26:03.401899    fatal (    scripts.cpp:14  ) ScriptTrader::activate 9994 & 987
+  2021-11-03 00:26:03.401939    fatal (    scripts.cpp:21  ) 4 and 3
 * was able to trade ok with 987 & 15611....
 * I think there's a bug in the detect "burnt" ports.  I don't think it takes into consideration ports that I've seen but not docked with (all percents 0).  Whoops!
 * If there's a port that's BBB, and there's a SBS or BSS, it should use E.
@@ -48,6 +54,7 @@
 
 # Done !  yeah!
 
+* Stale port data:  Warn about it.  (If > 1 hour ago)  (This revealed a meta data loading issue.)
 * ~~Setup the prompt timeout.  Display prompt + prompt_raw (to verify that this is working)~~
 * ~~On the prompt, if there's a \r throw it away and everything left of it.~~
 * work on find trade pairs/find best/find ok trades.  We have data to work with now.  

+ 12 - 0
director.cpp

@@ -533,6 +533,7 @@ MenuDispatch *Director::init_scripts_menu(void) {
                 {"C", "Closest Trade"},
                 {"N", "Nearest Unexplored"},
                 {"V", "Voyager Explorer"},
+                {"E", "Explorer"},
                 {"U", "Upgrade Planet Pants"},
                 {"X", "Exit Scripts"}};
     md->setNotify([this]() { this->scripts_done(); });
@@ -635,6 +636,17 @@ void Director::scripts_done(void) {
           chain->activate();
           return;
         } break;
+        case 'E': {
+          script = std::make_shared<ScriptExplore>(*this);
+          ScriptExplore *se = static_cast<ScriptExplore *>(&(*script));
+          se->setNotify([this]() {
+            script.reset();
+            this->proxy_deactivate();
+          });
+          chain = script;
+          chain->activate();
+          return;
+        } break;
         case 'Q':
           chain = main_menu;
           main_menu->activate();

+ 9 - 10
dispatchers.cpp

@@ -528,8 +528,10 @@ void MoveDispatch::server_line(const std::string &line,
   }
 }
 
+/*
 bool MoveDispatch::density_clear(density d) {  // int sector, int density) {
-  /*
+  */ 
+ /*
   http://wiki.classictw.com/index.php?title=Gypsy%27s_Big_Dummy%27s_Guide_to_TradeWars_Text#Trader_Information
   Density Readings:
     0 = Empty Sector or Ferrengi Dreadanought
@@ -549,7 +551,8 @@ bool MoveDispatch::density_clear(density d) {  // int sector, int density) {
   500 = Planet
   512 = Federation Starship under Admiral Clausewitz
   575 = Destroyed Port (Before 25% Nav-Haz Clears)
-  */
+  */ 
+ /*
   if (d.sector == 0) return false;
 
   switch (d.density) {
@@ -562,7 +565,7 @@ bool MoveDispatch::density_clear(density d) {  // int sector, int density) {
   // special case for sector 1:
   if ((d.sector == 1) && (d.density == 601)) return true;
   return false;
-}
+} */
 
 void MoveDispatch::server_prompt(const std::string &prompt) {
   BUGZ_LOG(fatal) << "server_prompt: " << prompt;
@@ -577,7 +580,7 @@ void MoveDispatch::server_prompt(const std::string &prompt) {
       density d = director.galaxy.dscan.find(move_to);
       if (d.sector == move_to) {
         // Yes! we found the sector in the scan!
-        if (!density_clear(d)) {
+        if (!density_clear(d.sector, d.density, d.navhaz)) {
           BUGZ_LOG(fatal) << "Failed density check on single move.";
           why_failed = str(boost::format("Density of %1% at %2%.") % move_to %
                            d.density);  // density of move_to ...
@@ -612,7 +615,7 @@ void MoveDispatch::server_prompt(const std::string &prompt) {
         int density =
             director.galaxy.meta["density"][to_check]["density"].as<int>();
         */
-        if (density_clear(d)) {  // to_check, density)) {
+        if (density_clear(d.sector, d.density, d.navhaz)) {  // to_check, density)) {
           to_server("S");
           ++warp_pos;
         } else {
@@ -634,11 +637,7 @@ void MoveDispatch::server_prompt(const std::string &prompt) {
     if (prompt == "Stop in this sector (Y,N,E,I,R,S,D,P,?) (?=Help) [N] ? ") {
       int to_check = warp_lane[warp_pos + 1];
       density d = director.galaxy.dscan.find(to_check);
-      /*
-      int density =
-          director.galaxy.meta["density"][to_check]["density"].as<int>();
-      */
-      if (density_clear(d)) {
+      if (density_clear(d.sector, d.density, d.navhaz)) {
         to_server("N");
         ++warp_pos;
         state = 4;

+ 1 - 1
dispatchers.h

@@ -147,7 +147,7 @@ class MoveDispatch : public Dispatch {
   void server_prompt(const std::string &prompt) override;
   void client_input(const std::string &input) override;
  private:
-  bool density_clear(density d); // int sector, int density);  
+  //bool density_clear(density d); // int sector, int density);  
 };
 
 class TraderDispatch : public Dispatch {

+ 4 - 2
scripts.cpp

@@ -328,7 +328,6 @@ ScriptExplore::ScriptExplore(Director &d) : Dispatch(d) {
 
 ScriptExplore::~ScriptExplore() {
   BUGZ_LOG(warning) << "~ScriptExplore()";
-  us.reset();
 }
 
 void ScriptExplore::init() {
@@ -366,10 +365,12 @@ void ScriptExplore::activate() {
 
 void ScriptExplore::deactivate() {
   BUGZ_LOG(warning) << "ScriptExplore::deactivate()";
+  us.reset();
   notify();
 }
 
 void ScriptExplore::move_notify() {
+  director.chain = us;
   if (md->aborted) {
     deactivate();
     return;
@@ -385,6 +386,7 @@ void ScriptExplore::move_notify() {
 }
 
 void ScriptExplore::input_notify() {
+  director.chain = us;
   if (id->input.empty() || id->aborted) {
     to_client("Maybe next time.\n\r");
     deactivate();
@@ -412,7 +414,7 @@ void ScriptExplore::input_notify() {
     BUGZ_LOG(warning) << "Explore loops: INFINITE";
   }
   
-  director.chain = us;
+  
   to_server("SD");
   state = 3;
 }

+ 1 - 1
scripts.h

@@ -67,7 +67,7 @@ class ScriptExplore : public Dispatch {
     std::shared_ptr<Dispatch> input;
     void next(void);
     std::shared_ptr<Dispatch> us;
-    
+
   public:
     ScriptExplore(Director &);
     ~ScriptExplore();

+ 1 - 0
utils.cpp

@@ -4,6 +4,7 @@
 #include <string>
 #include <vector>
 #include <exception>
+#include <chrono>
 
 /**
  * Clean up the trailing ../ in __FILE__

+ 26 - 0
yaml-2-json.py

@@ -0,0 +1,26 @@
+#!/usr/bin/env python3
+
+import yaml
+import json
+import sys
+from pprint import pprint
+
+filename = sys.argv[1]
+
+with open(filename) as fp:
+  data = yaml.safe_load(fp)
+
+# pprint(data)
+
+newfile = filename
+newfile = newfile.replace(".yaml", ".json")
+
+with open(newfile, 'w') as fp:
+    json.dump(data, fp, indent=2)
+
+# Cleaning the json:
+# jq 'del( .meta[ "density", "trade" ])' viper-A-bugz.json
+
+# Pretty-print the json:
+# jq . viper-A-bugz.json > viper-A-bugz-pretty.json
+