5a9c5876905ce2f56c94317a4d7540bbc3f3181c
[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         if((game->player[0]->user->flags & USERFLAG_ISAUTHED) && (game->player[1]->user->flags & USERFLAG_ISAUTHED) && !stricmp(game->player[0]->user->auth, game->player[1]->user->auth)) {
40             fourwins_reply(game, "NF_4WINS_SELF");
41             fourwins_free_game(game);
42             return;
43         }
44         fourwins_reply(game, "NF_4WINS_START", user->nick);
45         if((game->player[0]->user->flags & USERFLAG_ISAUTHED)) {
46             char *tmp;
47             int win_count = ((tmp = getSetting(game->player[0]->user, chan, "4wins_games")) ? atoi(tmp) : 0);
48             int total_win_count = ((tmp = getSetting(game->player[0]->user, NULL, "4wins_games")) ? atoi(tmp) : 0);
49             win_count++;
50             total_win_count++;
51             char buf[10];
52             sprintf(buf, "%d", win_count);
53             setSetting(game->player[0]->user, chan, "4wins_games", buf);
54             sprintf(buf, "%d", total_win_count);
55             setSetting(game->player[0]->user, NULL, "4wins_games", buf);
56         }
57         if((game->player[1]->user->flags & USERFLAG_ISAUTHED)) {
58             char *tmp;
59             int win_count = ((tmp = getSetting(game->player[1]->user, chan, "4wins_games")) ? atoi(tmp) : 0);
60             int total_win_count = ((tmp = getSetting(game->player[1]->user, NULL, "4wins_games")) ? atoi(tmp) : 0);
61             win_count++;
62             total_win_count++;
63             char buf[10];
64             sprintf(buf, "%d", win_count);
65             setSetting(game->player[1]->user, chan, "4wins_games", buf);
66             sprintf(buf, "%d", total_win_count);
67             setSetting(game->player[1]->user, NULL, "4wins_games", buf);
68         }
69         fourwins_show_matrix(game);
70         fourwins_reply(game, "NF_4WINS_USER_HURRY_UP", game->player[game->active_player]->user->nick);
71         game->timer = timeq_add(120, module_id, fourwins_timeout, game);
72     } else {
73         if(!argc) {
74             reply(textclient, user, "NF_4WINS_ENTER_OPPONENT");
75             return;
76         }
77         struct UserNode *opp_user = getUserByNick(argv[0]);
78         if(!opp_user) {
79             reply(textclient, user, "NS_USER_UNKNOWN", argv[0]);
80             return;
81         }
82         if(opp_user == user) {
83             reply(textclient, user, "NF_4WINS_SELF");
84             return;
85         }
86         struct ChanUser *opponent = getChanUser(opp_user, chan);
87         if(!opponent) {
88             reply(textclient, user, "NF_4WINS_OPPONENT_NOT_IN_CHAN", opp_user->nick);
89             return;
90         }
91         game = calloc(1,sizeof(*game));
92         game->player[0] = chanuser;
93         game->player[1] = opponent;
94         game->textbot = textclient;
95         game->state = FOURWINS_STATE_WAITING;
96         game->timer = timeq_add(120, module_id, fourwins_timeout, game);
97         game->next = fourwins_active_games;
98         fourwins_active_games = game;
99         reply(textclient, user, "NF_4WINS_REQUESTED", opp_user->nick);
100         reply(textclient, opp_user, "NF_4WINS_REQUEST", user->nick);
101     }
102 }