added NeonFun Bot with UNO Game
[NeonServV5.git] / src / modules / NeonFun.mod / cmd_neonfun_uno.c
1 /* cmd_neonfun_uno.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_uno.h"
20
21 CMD_BIND(neonfun_cmd_uno) {
22     struct ChanUser *chanuser = getChanUser(user, chan);
23     if(!chanuser) return;
24     struct uno_game *game;
25     for(game = uno_active_games; game; game = game->next) {
26         if(chan == game->channel) {
27             if(game->state == UNO_STATE_WAITING)
28                 break;
29             else {
30                 reply(getTextBot(), user, "NF_UNO_ALREADY_RUNNING", chan->name);
31                 return;
32             }
33         }
34     }
35     if(game) {
36         //check if player has already joined
37         struct uno_player *player, *last_player = NULL;
38         for(player = game->player; player; player = player->next) {
39             if(player->chanuser == chanuser) {
40                 reply(getTextBot(), user, "NF_UNO_ALREADY_JOINED");
41                 return;
42             } else
43                 last_player = player;
44         }
45         if(!last_player) return; //error (game without players?)
46         player = malloc(sizeof(*player));
47         player->chanuser = chanuser;
48         player->count = 0;
49         player->cards = NULL;
50         player->prev = last_player;
51         player->next = NULL;
52         last_player->next = player;
53         game->players++;
54         uno_reply(game, user, "NF_UNO_USER_JOINED", user->nick);
55     } else {
56         game = malloc(sizeof(*game));
57         game->channel = chan;
58         game->textbot = getTextBot();
59         game->state = UNO_STATE_WAITING;
60         game->reverse_direction = 0;
61         game->take_cards_pending = 0;
62         game->deck = NULL;
63         game->top_card = NULL;
64         struct uno_player *player = malloc(sizeof(*player));
65         player->chanuser = chanuser;
66         player->count = 0;
67         player->cards = NULL;
68         player->prev = NULL;
69         player->next = NULL;
70         game->player = player;
71         game->winner = NULL;
72         game->active_player = NULL;
73         game->players = 1;
74         game->active_player = 0;
75         game->timer = timeq_add(30, module_id, uno_game_wait_timeout, game);
76         game->next = uno_active_games;
77         uno_active_games = game;
78         uno_reply(game, user, "NF_UNO_CREATED", user->nick);
79     }
80 }