added blackjack game
[NeonServV5.git] / src / modules / NeonFun.mod / cmd_neonfun_blackjack.c
1 /* cmd_neonfun_blackjack.c - NeonServ v5.6
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_blackjack.h"
20
21 CMD_BIND(neonfun_cmd_blackjack) {
22     struct ChanUser *chanuser = getChanUser(user, chan);
23     if(!chanuser) return;
24     struct bj_game *game;
25     for(game = bj_active_games; game; game = game->next) {
26         if(chan == game->channel) 
27             break;
28     }
29     if(game) {
30         //check if player has already joined
31         struct bj_player *player, *last_player = NULL;
32         for(player = game->player; player; player = player->next) {
33             if(player->chanuser == chanuser) {
34                 reply(textclient, user, "NF_BJ_ALREADY_JOINED");
35                 return;
36             } else
37                 last_player = player;
38         }
39         if(!last_player) return; //error (game without players?)
40         player = malloc(sizeof(*player));
41         player->chanuser = chanuser;
42         player->count = 0;
43         player->cards = NULL;
44         player->prev = last_player;
45         player->next = NULL;
46         last_player->next = player;
47         game->players++;
48         bj_reply(game, user, "NF_BJ_USER_JOINED", user->nick);
49     } else {
50         game = malloc(sizeof(*game));
51         game->channel = chan;
52         game->textbot = textclient;
53         game->state = BJ_STATE_WAITING;
54         game->deck = NULL;
55         struct bj_player *player = malloc(sizeof(*player));
56         player->chanuser = chanuser;
57         player->count = 0;
58         player->cards = NULL;
59         player->prev = NULL;
60         player->next = NULL;
61         game->player = player;
62         game->active_player = NULL;
63         game->players = 1;
64         game->timer = timeq_add(30, module_id, bj_game_wait_timeout, game);
65         game->next = bj_active_games;
66         bj_active_games = game;
67         bj_reply(game, user, "NF_BJ_CREATED", user->nick);
68     }
69 }