added 4wins game
[NeonServV5.git] / src / modules / NeonFun.mod / cmd_neonfun_4wins.c
diff --git a/src/modules/NeonFun.mod/cmd_neonfun_4wins.c b/src/modules/NeonFun.mod/cmd_neonfun_4wins.c
new file mode 100644 (file)
index 0000000..a282a14
--- /dev/null
@@ -0,0 +1,93 @@
+/* cmd_neonfun_4wins.c - NeonServ v5.4
+ * Copyright (C) 2011-2012  Philipp Kreil (pk910)
+ * 
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. 
+ */
+
+#include "cmd_neonfun.h"
+#include "game_4wins.h"
+
+CMD_BIND(neonfun_cmd_4wins) {
+    struct ChanUser *chanuser = getChanUser(user, chan);
+    if(!chanuser) return;
+    struct fourwins_game *game;
+    for(game = fourwins_active_games; game; game = game->next) {
+        if(chanuser == game->player[0]) {
+            return;
+        } else if(chanuser == game->player[1]) {
+            if(game->state == FOURWINS_STATE_WAITING)
+                break;
+            else 
+                return;
+        }
+    }
+    if(game) {
+        timeq_del(game->timer);
+        game->timer = NULL;
+        game->state = FOURWINS_STATE_RUNNING;
+        fourwins_reply(game, "NF_4WINS_START", user->nick);
+        if((game->player[0]->user->flags & USERFLAG_ISAUTHED)) {
+            char *tmp;
+            int win_count = ((tmp = getSetting(game->player[0]->user, chan, "4wins_games")) ? atoi(tmp) : 0);
+            int total_win_count = ((tmp = getSetting(game->player[0]->user, NULL, "4wins_games")) ? atoi(tmp) : 0);
+            win_count++;
+            total_win_count++;
+            char buf[10];
+            sprintf(buf, "%d", win_count);
+            setSetting(game->player[0]->user, chan, "4wins_games", buf);
+            sprintf(buf, "%d", total_win_count);
+            setSetting(game->player[0]->user, NULL, "4wins_games", buf);
+        }
+        if((game->player[1]->user->flags & USERFLAG_ISAUTHED)) {
+            char *tmp;
+            int win_count = ((tmp = getSetting(game->player[1]->user, chan, "4wins_games")) ? atoi(tmp) : 0);
+            int total_win_count = ((tmp = getSetting(game->player[1]->user, NULL, "4wins_games")) ? atoi(tmp) : 0);
+            win_count++;
+            total_win_count++;
+            char buf[10];
+            sprintf(buf, "%d", win_count);
+            setSetting(game->player[1]->user, chan, "4wins_games", buf);
+            sprintf(buf, "%d", total_win_count);
+            setSetting(game->player[1]->user, NULL, "4wins_games", buf);
+        }
+        fourwins_show_matrix(game);
+        fourwins_reply(game, "NF_4WINS_USER_HURRY_UP", game->player[game->active_player]->user->nick);
+        game->timer = timeq_add(120, module_id, fourwins_timeout, game);
+    } else {
+        if(!argc) {
+            reply(getTextBot(), user, "NF_4WINS_ENTER_OPPONENT");
+            return;
+        }
+        struct UserNode *opp_user = getUserByNick(argv[0]);
+        if(!opp_user) {
+            reply(getTextBot(), user, "NS_USER_UNKNOWN", argv[0]);
+            return;
+        }
+        struct ChanUser *opponent = getChanUser(opp_user, chan);
+        if(!opponent) {
+            reply(getTextBot(), user, "NF_4WINS_OPPONENT_NOT_IN_CHAN", opp_user->nick);
+            return;
+        }
+        game = calloc(1,sizeof(*game));
+        game->player[0] = chanuser;
+        game->player[1] = opponent;
+        game->textbot = getTextBot();
+        game->state = FOURWINS_STATE_WAITING;
+        game->timer = timeq_add(120, module_id, fourwins_timeout, game);
+        game->next = fourwins_active_games;
+        fourwins_active_games = game;
+        reply(getTextBot(), user, "NF_4WINS_REQUESTED", opp_user->nick);
+        reply(getTextBot(), opp_user, "NF_4WINS_REQUEST", user->nick);
+    }
+}