added bomb funcmd
[NeonServV5.git] / src / modules / funcmd.mod / cmd_funcmds.c
index bae54d191593375e20bcb106619bbb21be933a9c..56a7eab60b8b5dc69b1a85c6322a9729e8c3324a 100644 (file)
 #include "../../lang.h"
 #include "../../tools.h"
 #include "../../DBHelper.h"
+#include "../../IRCEvents.h"
+#include "../../timeq.h"
+#include "../../WHOHandler.h"
+#include "../../EventLogger.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} */
@@ -32,12 +36,42 @@ static const struct default_language_entry msgtab[] = {
     {"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!"},
     {"FUN_COOKIE", "gives %1$s a very big chocolate cookie. %1$s has got %2$d cookies until now (%3$d in this channel)."}, /* {ARGS: "TestUser", 20, 50} */
+    {"FUN_BOMB_MENU", "There are the following commands:"},
+    {"FUN_BOMB_MENU_PLANT",  "$b%s plant <user> [wires] $b Plant a bomb in front of another users feet ([wires] is the number of wires - default: %d)"}, /* {ARGS: "bomb", 3} */
+    {"FUN_BOMB_MENU_KPLANT", "$b%s kplant <user> [wires]$b Plant a kick-bomb in front of another users feet ([wires] is the number of wires - default: %d) (OP only)"}, /* {ARGS: "bomb", 3} */
+    {"FUN_BOMB_MENU_DEFUSE", "$b%s defuse <wire>        $b Defuses a bomb (maybe :D)"}, /* {ARGS: "bomb"} */
+    {"FUN_BOMB_MENU_RETURN", "$b%s return               $b Pushes a bomb planted infront of you back to the owner ($k4WARNING$k: There is a highly chance that the bomb detonates)"}, /* {ARGS: "bomb"} */
+    {"FUN_BOMB_SELF", "You can not plant a bomb in front of yourself..."},
+    {"FUN_BOMB_TARGET_ACTIVE", "%s has already an active bomb..."},
+    {"FUN_BOMB_OWNER_ACTIVE", "You have already planted another bomb..."},
+    {"FUN_BOMB_PLANTED", "%1$s planted a bomb in front of %2$s's feet. %2$s, you have $b%3$d seconds$b to defuse the bomb by cutting one of the following wires: %4$s ($uuse:$u %5$s defuse <wire>)"}, /* {ARGS: "TestUser", "TestTarget", 30, "blue, red, green", "bomb"} */
+    {"FUN_BOMB_KPLANTED", "%1$s planted a kick-bomb in front of %2$s's feet. %2$s, you have $b%3$d seconds$b to defuse the bomb by cutting one of the following wires: %4$s ($uuse:$u %5$s defuse <wire>)"}, /* {ARGS: "TestUser", "TestTarget", 30, "blue, red, green", "bomb"} */
+    {"FUN_BOMB_PLANT_SERVICE", "You can't plant a bomb in front of this user (bot)..."},
+    {"FUN_BOMB_PLANT_PROTECTED", "You can't plant a bomb in front of this user (protect)..."},
+    {"FUN_BOMB_PLANT_MAXWIRES", "A bomb can only have %d-%d wires!"}, /* {ARGS: 2, 8} */
+    {"FUN_BOMB_WIRES", "$k4red$k|$k3green$k|$k8yellow$k|$k12blue$k|$k1black$k|$k6purple$k|$k7orange$k|$k11aqua$k"},
+    {"FUN_BOMB_NOBOMB", "There is no bomb you could defuse..."},
+    {"FUN_BOMB_UNKNOWN_WIRE", "%s is an unknown wire..."}, /* {ARGS: "white"} */
+    {"FUN_BOMB_DEFUSED", "%1$s has successfully defused the bomb! %1$s got %2$d bombs (%3$d in this channel) and has defused %4$d of them."}, /* {ARGS: "TestUser", 20, 10, 5} */
+    {"FUN_BOMB_DETONATED", "*BOOOOOOM* The bomb explodes in front of %1$s!!! %2$s was the right wire. %1$s got %3$d bombs (%4$d in this channel)"}, /* {ARGS: "TestUser", "white", 20, 10} */
     {NULL, NULL}
 };
 
+#define FUNCMD_BOMB_WIRES_DEFAULT 3
+#define FUNCMD_BOMB_WIRES_MIN     2
+#define FUNCMD_BOMB_WIRES_MAX     8
+#define FUNCMD_BOMB_TIME          30
+
+static int funcmd_bomb_freeuser(struct UserNode *user);
+static int funcmd_bomb_freechan(struct ChanNode *chan);
+static void funcmd_bomb_freeclient(struct ClientSocket *client);
+
 void init_funcmds() {
     register_default_language_table(msgtab);
     srand(time(NULL));
+    bind_freeuser(funcmd_bomb_freeuser, module_id);
+    bind_freechan(funcmd_bomb_freechan, module_id);
+    bind_freeclient(funcmd_bomb_freeclient, module_id);
 }
 
 void register_commands() {
@@ -51,22 +85,24 @@ void register_commands() {
     USER_COMMAND("dice",         funcmd_dice,            1,  CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_FUNCMD);
     USER_COMMAND("8ball",        funcmd_8ball,           1,  CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_FUNCMD);
     USER_COMMAND("cookie",       funcmd_cookie,          0,  CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_FUNCMD);
+    USER_COMMAND("bomb",         funcmd_bomb,            0,  CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_FUNCMD);
     #undef USER_COMMAND
 }
 
-struct current_funcmd_header {
+struct funcmd_header_info {
     struct ClientSocket *client;
     struct UserNode *user;
     struct ChanNode *chan;
     char send_notice;
+    char null_language;
 };
 
-static struct current_funcmd_header current_funcmd;
-
 #define FUNCMD_HEADER \
-current_funcmd.client = textclient; \
-current_funcmd.user = user; \
-current_funcmd.chan = chan; \
+struct funcmd_header_info *header = malloc(sizeof(*header)); \
+header->client = textclient; \
+header->user = user; \
+header->chan = chan; \
+header->null_language = 0; \
 {\
     MYSQL_RES *res; \
     MYSQL_ROW row; \
@@ -77,29 +113,33 @@ current_funcmd.chan = chan; \
         reply(textclient, user, "NS_FUN_DISABLED", chan->name); \
         return; \
     } else if(!strcmp(row[0], "1")) \
-        current_funcmd.send_notice = 1; \
+        header->send_notice = 1; \
     else \
-        current_funcmd.send_notice = 0; \
+        header->send_notice = 0; \
 }
 
-#define REPLYTYPE_NORMAL 0
-#define REPLYTYPE_ACTION 1
-static void funcmd_reply(const char *text, int type, ...) {
-    if (!(current_funcmd.client->flags & SOCKET_FLAG_CONNECTED)) return;
-    const char *reply_format = get_language_string(current_funcmd.user, text);
+#define FUNCMD_FOOTER \
+free(header);
+
+#define REPLYTYPE_NORMAL 0x01
+#define REPLYTYPE_ACTION 0x02
+#define REPLYTYPE_NOTICE 0x04
+static void funcmd_reply(struct funcmd_header_info *header, const char *text, int type, ...) {
+    if (!(header->client->flags & SOCKET_FLAG_CONNECTED)) return;
+    const char *reply_format = get_language_string((header->null_language ? NULL : header->user), text);
     if(reply_format)
         text = reply_format;
     char formatBuf[MAXLEN];
-    if(current_funcmd.send_notice) {
-        if(type == REPLYTYPE_ACTION)
-            sprintf(formatBuf, "NOTICE %s :%s %s", current_funcmd.user->nick, current_funcmd.client->user->nick, text);
+    if(header->send_notice || (type & REPLYTYPE_NOTICE)) {
+        if(type & REPLYTYPE_ACTION)
+            sprintf(formatBuf, "NOTICE %s :%s %s", header->user->nick, header->client->user->nick, text);
         else
-            sprintf(formatBuf, "NOTICE %s :%s", current_funcmd.user->nick, text);
+            sprintf(formatBuf, "NOTICE %s :%s", header->user->nick, text);
     } else {
-        if(type == REPLYTYPE_ACTION)
-            sprintf(formatBuf, "PRIVMSG %s :\001ACTION %s\001", current_funcmd.chan->name, text);
+        if(type & REPLYTYPE_ACTION)
+            sprintf(formatBuf, "PRIVMSG %s :\001ACTION %s\001", header->chan->name, text);
         else
-            sprintf(formatBuf, "PRIVMSG %s :%s", current_funcmd.chan->name, text);
+            sprintf(formatBuf, "PRIVMSG %s :%s", header->chan->name, text);
     }
     va_list arg_list;
     char sendBuf[MAXLEN];
@@ -111,7 +151,7 @@ static void funcmd_reply(const char *text, int type, ...) {
     if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
     sendBuf[pos] = '\n';
     sendBuf[pos+1] = '\0';
-    write_socket(current_funcmd.client, sendBuf, pos+1);
+    write_socket(header->client, sendBuf, pos+1);
 }
 
 static char* getSetting(struct UserNode *user, struct ChanNode *chan, const char *setting) {
@@ -157,14 +197,19 @@ static void setSetting(struct UserNode *user, struct ChanNode *chan, const char
         printf_mysql_query("INSERT INTO `fundata` (`user`, `cid`, `name`, `value`) VALUES ('%s', '%d', '%s', '%s')", escape_string(uname), cid, escape_string(setting), escape_string(value));
 }
 
+#include "cmd_funcmds_bomb.c"
+
+
 CMD_BIND(funcmd_ping) {
     FUNCMD_HEADER;
-    funcmd_reply("\002%s\002: Pong!", REPLYTYPE_NORMAL, user->nick);
+    funcmd_reply(header, "\002%s\002: Pong!", REPLYTYPE_NORMAL, user->nick);
+    FUNCMD_FOOTER;
 }
 
 CMD_BIND(funcmd_pong) {
     FUNCMD_HEADER;
-    funcmd_reply("\002%s\002: Ping!", REPLYTYPE_NORMAL, user->nick);
+    funcmd_reply(header, "\002%s\002: Ping!", REPLYTYPE_NORMAL, user->nick);
+    FUNCMD_FOOTER;
 }
 
 CMD_BIND(funcmd_dice) {
@@ -172,15 +217,16 @@ CMD_BIND(funcmd_dice) {
     int max = atoi(argv[0]);
     if(max > 1) {
         int val = (rand() % max) + 1;
-        funcmd_reply("FUN_DICE", REPLYTYPE_NORMAL, user->nick, val, max);
+        funcmd_reply(header, "FUN_DICE", REPLYTYPE_NORMAL, user->nick, val, max);
     } else
-        funcmd_reply("FUN_DICE_NUM", REPLYTYPE_NORMAL, argv[0]);
+        funcmd_reply(header, "FUN_DICE_NUM", REPLYTYPE_NORMAL, argv[0]);
+    FUNCMD_FOOTER;
 }
 
 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");
+    const char *const_replies = get_language_string(header->user, "FUN_8BALL_REPLIES");
     char replies[MAXLEN];
     int i, reply_count = 1;
     for(i = 0; const_replies[i]; i++) {
@@ -206,15 +252,17 @@ CMD_BIND(funcmd_8ball) {
         }
     }
     if(creply) {
-        funcmd_reply("FUN_8BALL", REPLYTYPE_NORMAL, user->nick, creply);
+        funcmd_reply(header, "FUN_8BALL", REPLYTYPE_NORMAL, user->nick, creply);
     }
+    FUNCMD_FOOTER;
 }
 
 CMD_BIND(funcmd_cookie) {
     FUNCMD_HEADER;
     if(argc) {
         if(!(user = getUserByNick(argv[0]))) {
-            reply(current_funcmd.client, current_funcmd.user, "NS_USER_UNKNOWN", argv[0]);
+            reply(header->client, header->user, "NS_USER_UNKNOWN", argv[0]);
+            FUNCMD_FOOTER;
             return;
         }
     }
@@ -228,5 +276,6 @@ CMD_BIND(funcmd_cookie) {
     setSetting(user, chan, "cookies", buf);
     sprintf(buf, "%d", total_count);
     setSetting(user, NULL, "cookies", buf);
-    funcmd_reply("FUN_COOKIE", REPLYTYPE_ACTION, user->nick, total_count, user_count);
+    funcmd_reply(header, "FUN_COOKIE", REPLYTYPE_ACTION, user->nick, total_count, user_count);
+    FUNCMD_FOOTER;
 }