removed hardcoded reference to wiki and replaced with variable from config.php
[e-DoKo.git] / include / newgame.php
1 <?php
2 /* Copyright 2006, 2007, 2008, 2009, 2010 Arun Persaud <arun@nubati.net>
3  *
4  *   This file is part of e-DoKo.
5  *
6  *   e-DoKo is free software: you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation, either version 3 of the License, or
9  *   (at your option) any later version.
10  *
11  *   e-DoKo is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with e-DoKo.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 /* make sure that we are not called from outside the scripts,
22  * use a variable defined in config.php to check this
23  */
24 if(!isset($HOST))
25   exit;
26
27 /* user needs to be logged in to do this */
28 if(! isset($_SESSION["name"]) )
29   {
30     echo "<div class=\"message\">Please <a href=\"$INDEX\">log in</a>.</div>";
31   }
32 else
33   {
34     $name  = $_SESSION["name"];
35     $email = DB_get_email('name',$name);
36
37     $myid = DB_get_userid('email',$email);
38     if(!$myid)
39       return;
40
41     DB_update_user_timestamp($myid);
42
43     if( !myisset("PlayerA", "PlayerB","PlayerC","PlayerD","dullen","schweinchen","callrule","lowtrump" ))
44       {
45         /* only get players that want to be in new games */
46         $names = DB_get_all_user_names_open_for_games();
47
48         /* add player if he is not open for games */
49         if(!in_array($_SESSION["name"],$names))
50           $names[]=$_SESSION["name"];
51
52         /* add some randomness */
53         shuffle($names);
54
55         echo "<div class=\"user\">\n";
56         output_form_for_new_game($names);
57         echo "</div>\n";
58
59         display_user_menu($myid);
60       }
61     else
62       {
63         /* get my name */
64         $name = $_SESSION["name"];
65
66         /* the names of the four players */
67         $PlayerA = $_REQUEST["PlayerA"];
68         $PlayerB = $_REQUEST["PlayerB"];
69         $PlayerC = $_REQUEST["PlayerC"];
70         $PlayerD = $_REQUEST["PlayerD"];
71
72         /* the person who sets up the game has to be one of the players */
73         if(!in_array($name,array($PlayerA,$PlayerB,$PlayerC,$PlayerD)))
74           {
75             echo "<div class=\"message\">You need to be one of the players to start a <a href=\"$INDEX?action=new\">new game</a>.</div>";
76             return;
77           }
78
79         /* what rules were selected */
80         $dullen      = $_REQUEST["dullen"];
81         $schweinchen = $_REQUEST["schweinchen"];
82         $call        = $_REQUEST["callrule"];
83         $lowtrump    = $_REQUEST["lowtrump"];
84
85         /* get the emails addresses of the players */
86         $EmailA  = DB_get_email('name',$PlayerA);
87         $EmailB  = DB_get_email('name',$PlayerB);
88         $EmailC  = DB_get_email('name',$PlayerC);
89         $EmailD  = DB_get_email('name',$PlayerD);
90
91         /* this is used to check if the player names are all ok */
92         if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
93           {
94             echo "couldn't find one of the names, please start a new game";
95             return;
96           }
97
98         /* get user ids */
99         $useridA  = DB_get_userid('name',$PlayerA);
100         $useridB  = DB_get_userid('name',$PlayerB);
101         $useridC  = DB_get_userid('name',$PlayerC);
102         $useridD  = DB_get_userid('name',$PlayerD);
103
104         /* create random numbers */
105         $randomNR       = create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD);
106         $randomNRstring = join(":",$randomNR);
107
108         /* create game */
109         $followup = NULL;
110         /* is this game a follow up in an already started session? */
111         if(myisset("followup") )
112           {
113             $followup= $_REQUEST["followup"];
114             $session = DB_get_session_by_gameid($followup);
115             $ruleset = DB_get_ruleset_by_gameid($followup); /* just copy ruleset from old game,
116                                                              this way no manipulation is possible */
117
118             /* check if there is a game in pre or play mode, in that case do nothing */
119             if( DB_is_session_active($session) > 0 )
120               {
121                 echo "<p class=\"message\"> There is already a game going on in session $session, you can't start a new one</p>";
122                 return;
123               }
124             else if ( DB_is_session_active($session) < 0 )
125               {
126                 echo "<p class=\"message\"> ERROR: status of session $session couldn't be determined.</p>";
127                 return;
128               }
129
130             if($session)
131               DB_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
132                        "'$ruleset','$session' ,NULL)");
133             else
134               {
135                 /* get max session and start a new one */
136                 $max = DB_get_max_session();
137                 $max++;
138                 DB_query("UPDATE Game SET session='".$max."' WHERE id=".DB_quote_smart($followup));
139                 DB_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
140                             "'$ruleset','$max' ,NULL)");
141               }
142           }
143         else /* no follow up, start a new session */
144           {
145             /* get ruleset information or create new one */
146             $ruleset = DB_get_ruleset($dullen,$schweinchen,$call,$lowtrump);
147             if($ruleset <0)
148               {
149                 myerror("Error defining ruleset: $ruleset");
150                 return;
151               };
152             /* get max session */
153             $max = DB_get_max_session();
154             $max++;
155
156             DB_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre', ".
157                      "'$ruleset','$max' ,NULL)");
158           }
159         $gameid = DB_insert_id();
160
161         /* create hash */
162         $TIME  = (string) time(); /* to avoid collisions */
163         $hashA = md5("AGameOfDoko".$gameid.$PlayerA.$EmailA.$TIME);
164         $hashB = md5("AGameOfDoko".$gameid.$PlayerB.$EmailB.$TIME);
165         $hashC = md5("AGameOfDoko".$gameid.$PlayerC.$EmailC.$TIME);
166         $hashD = md5("AGameOfDoko".$gameid.$PlayerD.$EmailD.$TIME);
167
168         /* create hands */
169         DB_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($gameid).",".DB_quote_smart($useridA).
170                  ", ".DB_quote_smart($hashA).", 'start','1',NULL,NULL,NULL,NULL)");
171         $hand_idA = DB_insert_id();
172         DB_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($gameid).",".DB_quote_smart($useridB).
173                  ", ".DB_quote_smart($hashB).", 'start','2',NULL,NULL,NULL,NULL)");
174         $hand_idB = DB_insert_id();
175         DB_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($gameid).",".DB_quote_smart($useridC).
176                  ", ".DB_quote_smart($hashC).", 'start','3',NULL,NULL,NULL,NULL)");
177         $hand_idC = DB_insert_id();
178         DB_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($gameid).",".DB_quote_smart($useridD).
179                  ", ".DB_quote_smart($hashD).", 'start','4',NULL,NULL,NULL,NULL)");
180         $hand_idD = DB_insert_id();
181
182         /* save cards */
183         for($i=0;$i<12;$i++)
184           DB_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idA', '".$randomNR[$i]."', 'false')");
185         for($i=12;$i<24;$i++)
186           DB_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idB', '".$randomNR[$i]."', 'false')");
187         for($i=24;$i<36;$i++)
188           DB_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idC', '".$randomNR[$i]."', 'false')");
189         for($i=36;$i<48;$i++)
190           DB_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idD', '".$randomNR[$i]."', 'false')");
191
192         /* send out email, TODO: check for error with email */
193         $message = "You are invited to play a game of DoKo.\n".
194           "Please, place comments and bug reports here:\n$WIKI\n\n".
195           "The whole round would consist of the following players:\n".
196           "$PlayerA\n".
197           "$PlayerB\n".
198           "$PlayerC\n".
199           "$PlayerD\n\n".
200           "If you want to join this game, please follow this link:\n".
201           "".$HOST.$INDEX."?action=game&me=";
202
203         $subject = 'You are invited to a game of DoKo (game '.DB_format_gameid($gameid).')';
204         mymail($useridA,$subject, $message.$hashA."\n\n");
205         mymail($useridB,$subject, $message.$hashB."\n\n");
206         mymail($useridC,$subject, $message.$hashC."\n\n");
207         mymail($useridD,$subject, $message.$hashD."\n\n");
208
209         echo "<div class=\"message\">You started a new game. The emails have been sent out!</div>\n";
210         display_user_menu($myid);
211       }
212   }
213
214 ?>