added funcmd_8ball
[NeonServV5.git] / src / cmd_funcmds.c
index 3eadbf8ab22c7e270ccea456b82767c344297921..860ac5fa503bf927d9beca1869c903cebe65b72f 100644 (file)
 #include "UserNode.h"
 #include "ChanNode.h"
 #include "lang.h"
+#include "tools.h"
 
 static const struct default_language_entry msgtab[] = {
     {"FUN_DICE", "$b%s$b: A $b%d$b shows on the %d-sided die."}, /* {ARGS: "TestUser", 5, 6} */
     {"FUN_DICE_NUM", "I do not understand $b%s$b. Please use a single number above 1."}, /* {ARGS: "bla"} */
+    {"FUN_8BALL", "$b%s$b: %s"}, /* {ARGS: "TestUser", "Not a chance."} */
+    {"FUN_8BALL_REPLIES", "Not a chance.|In your dreams.|Absolutely!|Could be, could be.|No!"},
     {NULL, NULL}
 };
 
@@ -105,3 +108,36 @@ CMD_BIND(funcmd_dice) {
     } else
         funcmd_reply("FUN_DICE_NUM", argv[0]);
 }
+
+CMD_BIND(funcmd_8ball) {
+    FUNCMD_HEADER;
+    char *message = merge_argv(argv, 0, argc);
+    const char *const_replies = get_language_string(current_funcmd.user, "FUN_8BALL_REPLIES");
+    char replies[MAXLEN];
+    int i, reply_count = 1;
+    for(i = 0; const_replies[i]; i++) {
+        if(const_replies[i] == '|')
+            reply_count++;
+        replies[i] = const_replies[i];
+    }
+    replies[i] = '\0';
+    unsigned int crc32_val = (crc32(message)) % reply_count;
+    char *creply = (crc32_val == 0 ? replies : NULL);
+    reply_count = 0;
+    for(i = 0; replies[i]; i++) {
+        if(replies[i] == '|') {
+            if(creply) {
+                replies[i] = '\0';
+                break;
+            } else {
+                reply_count++;
+                if(reply_count == crc32_val) {
+                    creply = &replies[i+1];
+                }
+            }
+        }
+    }
+    if(creply) {
+        funcmd_reply("FUN_8BALL", user->nick, creply);
+    }
+}