added 4wins game
[NeonServV5.git] / src / modules / NeonFun.mod / cmd_neonfun_4wins.c
1 /* cmd_neonfun_4wins.c - NeonServ v5.4
2  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17
18 #include "cmd_neonfun.h"
19 #include "game_4wins.h"
20
21 CMD_BIND(neonfun_cmd_4wins) {
22     struct ChanUser *chanuser = getChanUser(user, chan);
23     if(!chanuser) return;
24     struct fourwins_game *game;
25     for(game = fourwins_active_games; game; game = game->next) {
26         if(chanuser == game->player[0]) {
27             return;
28         } else if(chanuser == game->player[1]) {
29             if(game->state == FOURWINS_STATE_WAITING)
30                 break;
31             else 
32                 return;
33         }
34     }
35     if(game) {
36         timeq_del(game->timer);
37         game->timer = NULL;
38         game->state = FOURWINS_STATE_RUNNING;
39         fourwins_reply(game, "NF_4WINS_START", user->nick);
40         if((game->player[0]->user->flags & USERFLAG_ISAUTHED)) {
41             char *tmp;
42             int win_count = ((tmp = getSetting(game->player[0]->user, chan, "4wins_games")) ? atoi(tmp) : 0);
43             int total_win_count = ((tmp = getSetting(game->player[0]->user, NULL, "4wins_games")) ? atoi(tmp) : 0);
44             win_count++;
45             total_win_count++;
46             char buf[10];
47             sprintf(buf, "%d", win_count);
48             setSetting(game->player[0]->user, chan, "4wins_games", buf);
49             sprintf(buf, "%d", total_win_count);
50             setSetting(game->player[0]->user, NULL, "4wins_games", buf);
51         }
52         if((game->player[1]->user->flags & USERFLAG_ISAUTHED)) {
53             char *tmp;
54             int win_count = ((tmp = getSetting(game->player[1]->user, chan, "4wins_games")) ? atoi(tmp) : 0);
55             int total_win_count = ((tmp = getSetting(game->player[1]->user, NULL, "4wins_games")) ? atoi(tmp) : 0);
56             win_count++;
57             total_win_count++;
58             char buf[10];
59             sprintf(buf, "%d", win_count);
60             setSetting(game->player[1]->user, chan, "4wins_games", buf);
61             sprintf(buf, "%d", total_win_count);
62             setSetting(game->player[1]->user, NULL, "4wins_games", buf);
63         }
64         fourwins_show_matrix(game);
65         fourwins_reply(game, "NF_4WINS_USER_HURRY_UP", game->player[game->active_player]->user->nick);
66         game->timer = timeq_add(120, module_id, fourwins_timeout, game);
67     } else {
68         if(!argc) {
69             reply(getTextBot(), user, "NF_4WINS_ENTER_OPPONENT");
70             return;
71         }
72         struct UserNode *opp_user = getUserByNick(argv[0]);
73         if(!opp_user) {
74             reply(getTextBot(), user, "NS_USER_UNKNOWN", argv[0]);
75             return;
76         }
77         struct ChanUser *opponent = getChanUser(opp_user, chan);
78         if(!opponent) {
79             reply(getTextBot(), user, "NF_4WINS_OPPONENT_NOT_IN_CHAN", opp_user->nick);
80             return;
81         }
82         game = calloc(1,sizeof(*game));
83         game->player[0] = chanuser;
84         game->player[1] = opponent;
85         game->textbot = getTextBot();
86         game->state = FOURWINS_STATE_WAITING;
87         game->timer = timeq_add(120, module_id, fourwins_timeout, game);
88         game->next = fourwins_active_games;
89         fourwins_active_games = game;
90         reply(getTextBot(), user, "NF_4WINS_REQUESTED", opp_user->nick);
91         reply(getTextBot(), opp_user, "NF_4WINS_REQUEST", user->nick);
92     }
93 }