Browse Source

Overhauled. Use TRIGGER for the trigger.

We now allow the TRIGGER to be changed!
I have ^V (Control-V) '\x16' as the current
trigger.  That shouldn't show up anywhere
in normal text.  The bat containing a ^
bothered me.  What if someone has a message
containing ^'s and we mangle it?  Yikes!

I want the system going crazy when I tell it
to, not because someone happened to use a
certain ^ in their message.

The tests were updated, and all places that
have triggers are now using whatever is
defined as TRIGGER in render.h.
bugz 4 years ago
parent
commit
8754ee287b
6 changed files with 64 additions and 36 deletions
  1. 4 4
      charman.cpp
  2. 4 3
      render.cpp
  3. 5 1
      render.h
  4. 14 2
      test-lastseen.cpp
  5. 4 4
      test-render.cpp
  6. 33 22
      wordplay.cpp

+ 4 - 4
charman.cpp

@@ -2,10 +2,10 @@
 #include <regex>
 #include <string>
 #include <vector>
-
 #include "charman.h"
 #include "utils.h"
 #include "zf_log.h"
+#include "render.h"
 
 void CharMan::validate(void) {
   // Control buffer debugging output.
@@ -230,11 +230,11 @@ int CharMan::word_tangler(std::pair<int, int> pos_len) {
     std::string tangle;
     int r = 1; // randint(2) + 1;
 
-    buffer << "^P1^R" << r;
+    buffer << TRIGGER "P1" TRIGGER "R" << r;
     r = randint(4) + 1;
-    buffer << "^S" << r;
+    buffer << TRIGGER "S" << r;
     tangle = buffer.str();
-    std::string reset = "^R0^S0";
+    std::string reset = TRIGGER "R0" TRIGGER "S0";
     ZF_LOGD("insert reset %s", reset.c_str());
     this->insert(p + len, reset);
     // this->validate();

+ 4 - 3
render.cpp

@@ -214,11 +214,10 @@ int send_image(int fd) {
   for (i = 0; i < image_size; i++) {
     std::string line(lines[i]);
     line.append("\r\n");
-    render(fd, line);
-    /*
+    // rendering the ^BAT^ was a bad idea. (uses ^ chars).
+    // render(fd, line);
     write(fd, lines[i], strlen(lines[i]));
     write(fd, "\r\n", 2);
-    */
   }
 
   // success
@@ -430,6 +429,7 @@ void process_trigger(int fd, std::string str, size_t &pos) {
   }
 }
 
+#ifdef UNWANTED
 /**
  * process_trigger( fd, *cp )
  *
@@ -591,6 +591,7 @@ const char *process_trigger(int fd, const char *cp) {
   }
   return cp;
 }
+#endif
 
 /**
  * render_effect( fd, ch )

+ 5 - 1
render.h

@@ -27,7 +27,11 @@ void render(int fd, const char *string_out, int len);
 void render(int fd, std::string &string_out);
 void render_image(const char **lines, int size);
 
-#define TRIGGER "^"
+// Use this in everything, so it is easily changed!
+#define TRIGGER "\x16"
+
+// "@"  // "^" // "\x16"
+
 // Max limit we'll sleep before ignoring effects/speed.
 #define SLEEP_LIMIT 30
 

+ 14 - 2
test-lastseen.cpp

@@ -10,7 +10,7 @@
 namespace {
 
 TEST(LastSeenTest, negative) {
-  LastSeen zero(2);
+  LastSeen zero(4);
 
   // buffer: -1, -1
   EXPECT_TRUE(zero.seen_before(-1));
@@ -56,10 +56,22 @@ TEST(LastSeenTest, More) {
   for (x = 0; x < 7; x++) {
     EXPECT_FALSE(seven.seen_before(x));
   }
+  // 0 - 6
   for (x = 0; x < 7; x++) {
     EXPECT_TRUE(seven.seen_before(x));
   }
-  EXPECT_FALSE(seven.seen_before(8));
+  // 0 - 6
+  EXPECT_FALSE(seven.seen_before(7));
+  // 1 - 7
+  for (x = 1; x < 8; x++) {
+    EXPECT_TRUE(seven.seen_before(x));
+  }
+  EXPECT_FALSE(seven.seen_before(0));
+  // 2 -6, 0
+  for (x = 2; x < 7; x++) {
+    EXPECT_TRUE(seven.seen_before(x));
+  }
+  EXPECT_TRUE(seven.seen_before(0));
 }
 
 } // namespace

+ 4 - 4
test-render.cpp

@@ -43,7 +43,7 @@ protected:
 };
 
 TEST_F(RenderFixture, CheckPause) {
-  std::string pause("^P1");
+  std::string pause( TRIGGER "P1");
   render(fd, pause);
   int elapsed = time(NULL) - start;
   ASSERT_GT(elapsed, 0);
@@ -51,7 +51,7 @@ TEST_F(RenderFixture, CheckPause) {
 }
 
 TEST_F(RenderFixture, CheckPauseTwo) {
-  std::string pause("^P2");
+  std::string pause( TRIGGER "P2");
   render(fd, pause);
   int elapsed = time(NULL) - start;
   ASSERT_GT(elapsed, 1);
@@ -60,7 +60,7 @@ TEST_F(RenderFixture, CheckPauseTwo) {
 
 TEST_F(RenderFixture, ColorTest) {
   ASSERT_GT(fd, 0);
-  std::string colors("^C01One^C02Two^C03Three^C04Four");
+  std::string colors( TRIGGER "C01One" TRIGGER "C02Two" TRIGGER "C03Three" TRIGGER "C04Four");
   render(fd, colors);
 
   int lfd = open(filename, O_RDONLY);
@@ -95,7 +95,7 @@ TEST_F(RenderFixture, ColorTest) {
 }
 
 TEST_F(RenderFixture, ColorEffectTest) {
-  std::string colors("^S1^R1\x1b[1;33;44mMEOW!\x1b[0m");
+  std::string colors( TRIGGER "S1" TRIGGER "R1\x1b[1;33;44mMEOW!\x1b[0m");
   render(fd, colors);
 
   int lfd = open(filename, O_RDONLY);

+ 33 - 22
wordplay.cpp

@@ -74,7 +74,8 @@ void harry_idle_event(int fd) {
   };
   const int total_user_phrases = sizeof(user_phrases) / sizeof(char *);
 
-  static LastSeen last_seen_harry_event(2);
+  // I'd like to use total_possible, but ... it isn't possible. (NNY)
+  static LastSeen last_seen_harry_event(LastSeen::best_guess(total_phrases));
 
   int total_possible =
       have_userinfo ? total_phrases + total_user_phrases : total_phrases;
@@ -112,9 +113,11 @@ void harry_idle_event(int fd) {
     buffer << "^CS^S2^C" << std::setfill('0') << std::setw(2) << color
            << phrases[r] << "^P2^CR^D" << std::setw(2) << strlen(phrases[r]);
     */
-    buffer << "^CS^S2^C" << std::setfill('0') << std::setw(2) << color
-           << selected << "^P" << pause << "^CR^D" << std::setw(2)
-           << selected.size();
+    buffer << TRIGGER "CS" TRIGGER "S2" TRIGGER "C" << std::setfill('0')
+           << std::setw(2) << color << selected
+           // Reset SPEED and RENDER
+           << TRIGGER "S0" TRIGGER "R0" TRIGGER "P" << pause
+           << TRIGGER "CR" TRIGGER "D" << std::setw(2) << selected.size();
 
     std::string str = buffer.str();
     ZF_LOGD("harry_event: render(%d, \"%s\")", fd, str.c_str());
@@ -165,12 +168,12 @@ int mangle_clrscr(std::string &buffer, std::string &work, size_t pos) {
                     {owl, sizeof(owl) / sizeof(char *), 0, 70}
 
       };
-
-      static LastSeen last_files(2);
+      const int total_images = sizeof(images) / sizeof(image);
+      static LastSeen last_files(LastSeen::best_guess(total_images));
       int r;
 
       do {
-        r = randint((sizeof(images) / sizeof(image)));
+        r = randint(total_images);
       } while (last_files.seen_before(r));
 
       std::string fgoto;
@@ -181,8 +184,8 @@ int mangle_clrscr(std::string &buffer, std::string &work, size_t pos) {
         x = randint(79 - images[r].width) + 1;
         y = randint(24 - images[r].size) + 1;
 
-        display << "^CS^f" << std::setfill('0') << std::setw(2) << x
-                << std::setw(2) << y << "\x1b[1;1H";
+        display << TRIGGER "CS" TRIGGER "f" << std::setfill('0') << std::setw(2)
+                << x << std::setw(2) << y << "\x1b[1;1H";
         fgoto = display.str();
         // reset display
         display.str(std::string());
@@ -190,9 +193,9 @@ int mangle_clrscr(std::string &buffer, std::string &work, size_t pos) {
       } else {
         if (images[r].lines == specter) {
           // specter!
-          fgoto.assign("^S1^CS^F");
+          fgoto.assign(TRIGGER "S1" TRIGGER "CS" TRIGGER "F");
         } else
-          fgoto.assign("^CS^F");
+          fgoto.assign(TRIGGER "CS" TRIGGER "F");
       }
 
       needs_cls = images[r].cls;
@@ -208,7 +211,8 @@ int mangle_clrscr(std::string &buffer, std::string &work, size_t pos) {
       // Ok, yes, there's no filename being sent.  :P
       render_image(images[r].lines, images[r].size);
 
-      display << (needs_cls ? "\x1b[2J" : "") << fgoto << "^S0^CR^P3";
+      display << (needs_cls ? "\x1b[2J" : "") << fgoto
+              << TRIGGER "S0" TRIGGER "CR" TRIGGER "P3";
 
       std::string display_output = display.str();
 
@@ -226,19 +230,21 @@ int mangle_clrscr(std::string &buffer, std::string &work, size_t pos) {
         std::ostringstream display;
 
         const char *phrasing[] = {
-            "^R1Haha^P1ha^P1ha", "Poof!", "Got U", "Anyone there?",
-            "^R1Knock, ^P1Knock",
+            TRIGGER "R1Haha" TRIGGER "P1ha" TRIGGER "P1ha", "Poof!", "Got U",
+            "Anyone there?", TRIGGER "R1Knock, " TRIGGER "P1Knock",
             /*
             This picks random color and position -- then
             homes cursor and changes to another color.  (This can be seen.)
              */
-            "^G0101^C07^S9Segmentation fault (core dumped)^P2"};
-        static LastSeen last_phrasing(2);
+            TRIGGER "G0101" TRIGGER "C07" TRIGGER
+                    "S9Segmentation fault (core dumped)" TRIGGER "P2"};
+        const int max_phrases = sizeof(phrasing) / sizeof(char *);
+        static LastSeen last_phrasing(LastSeen::best_guess(max_phrases));
 
         ZF_LOGI("mangle(ANSI_CLS)");
 
         do {
-          r = randint(sizeof(phrasing) / sizeof(char *));
+          r = randint(max_phrases);
         } while (last_phrasing.seen_before(r));
 
         int color = randint(14) + 1;
@@ -259,13 +265,18 @@ int mangle_clrscr(std::string &buffer, std::string &work, size_t pos) {
         HOME, CLS, HOME, ...  Not sure what others do there.  We'll see.
         */
 
-        if (strncmp(phrasing[r], "^G", 2) == 0) {
-          display << "^CS^S3^P1" << phrasing[r] << "^S0^R0^CR^P1^G0101";
+        if (strncmp(phrasing[r], TRIGGER "G", 2) == 0) {
+          display << TRIGGER "CS" TRIGGER "S3" TRIGGER "P1" << phrasing[r]
+                  << TRIGGER "S0" TRIGGER "R0" TRIGGER "CR" TRIGGER "P1" TRIGGER
+                             "G0101";
           // This starts with a GOTO, so don't use our random position
         } else {
-          display << "^CS^G" << std::setw(2) << std::setfill('0') << x
-                  << std::setw(2) << y << "^S3^C" << std::setw(2) << color
-                  << "^P1" << phrasing[r] << "^S0^R0^CR^P1^G0101";
+          display << TRIGGER "CS" TRIGGER "G" << std::setw(2)
+                  << std::setfill('0') << x << std::setw(2) << y
+                  << TRIGGER "S3" TRIGGER "C" << std::setw(2) << color
+                  << TRIGGER "P1" << phrasing[r]
+                  << TRIGGER "S0" TRIGGER "R0" TRIGGER "CR" TRIGGER "P1" TRIGGER
+                             "G0101";
         };
 
         std::string display_output = display.str();