*** VERSION 5.5.0 ***
[NeonServV5.git] / src / modules / NeonFun.mod / cmd_neonfun_4stone.c
1 /* cmd_neonfun_4stone.c - NeonServ v5.5
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_4stone) {
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] || chanuser == game->player[1]) {
27             if(game->state == FOURWINS_STATE_WAITING)
28                 return;
29             else 
30                 break;
31         }
32     }
33     if(game) {
34         //check if it's the player's turn
35         if(game->player[game->active_player] != chanuser) {
36             reply(textclient, user, "NF_4WINS_NOT_YOUR_TURN");
37             return;
38         }
39         int x = atoi(argv[0])-1;
40         if(x < 0 || x >= FOURWINS_MATRIX_WIDTH) {
41             reply(textclient, user, "NF_4WINS_INVALID_COLUMN");
42             return;
43         }
44         int y = fourwins_next_free_y(game, x);
45         if(y < 0) {
46             reply(textclient, user, "NF_4WINS_COLUMN_FULL");
47             return;
48         }
49         timeq_del(game->timer);
50         game->timer = NULL;
51         game->matrix[x][y].field = game->active_player+1;
52         fourwins_show_matrix(game);
53         if(fourwins_check_win(game, x, y)) {
54             fourwins_reply(game, "NF_4WINS_USER_WON", game->player[game->active_player]->user->nick);
55             if((game->player[game->active_player]->user->flags & USERFLAG_ISAUTHED)) {
56                 char *tmp;
57                 int win_count = ((tmp = getSetting(game->player[game->active_player]->user, chan, "4wins_win")) ? atoi(tmp) : 0);
58                 int total_win_count = ((tmp = getSetting(game->player[game->active_player]->user, NULL, "4wins_win")) ? atoi(tmp) : 0);
59                 win_count++;
60                 total_win_count++;
61                 char buf[10];
62                 sprintf(buf, "%d", win_count);
63                 setSetting(game->player[game->active_player]->user, chan, "4wins_win", buf);
64                 sprintf(buf, "%d", total_win_count);
65                 setSetting(game->player[game->active_player]->user, NULL, "4wins_win", buf);
66             }
67             fourwins_free_game(game);
68         } else {
69             game->total_stones++;
70             if(game->total_stones == FOURWINS_MAX_STONES) {
71                 fourwins_reply(game, "NF_4WINS_DRAW");
72                 if((game->player[0]->user->flags & USERFLAG_ISAUTHED)) {
73                     char *tmp;
74                     int win_count = ((tmp = getSetting(game->player[0]->user, chan, "4wins_draw")) ? atoi(tmp) : 0);
75                     int total_win_count = ((tmp = getSetting(game->player[0]->user, NULL, "4wins_draw")) ? atoi(tmp) : 0);
76                     win_count++;
77                     total_win_count++;
78                     char buf[10];
79                     sprintf(buf, "%d", win_count);
80                     setSetting(game->player[0]->user, chan, "4wins_draw", buf);
81                     sprintf(buf, "%d", total_win_count);
82                     setSetting(game->player[0]->user, NULL, "4wins_draw", buf);
83                 }
84                 if((game->player[1]->user->flags & USERFLAG_ISAUTHED)) {
85                     char *tmp;
86                     int win_count = ((tmp = getSetting(game->player[1]->user, chan, "4wins_draw")) ? atoi(tmp) : 0);
87                     int total_win_count = ((tmp = getSetting(game->player[1]->user, NULL, "4wins_draw")) ? atoi(tmp) : 0);
88                     win_count++;
89                     total_win_count++;
90                     char buf[10];
91                     sprintf(buf, "%d", win_count);
92                     setSetting(game->player[1]->user, chan, "4wins_draw", buf);
93                     sprintf(buf, "%d", total_win_count);
94                     setSetting(game->player[1]->user, NULL, "4wins_draw", buf);
95                 }
96                 fourwins_free_game(game);
97                 return;
98             }
99             if(game->active_player)
100                 game->active_player = 0;
101             else
102                 game->active_player = 1;
103             fourwins_reply(game, "NF_4WINS_USER_HURRY_UP", game->player[game->active_player]->user->nick);
104             game->timer = timeq_add(120, module_id, fourwins_timeout, game);
105         }
106     }
107 }