CLEANUP: moved more statistics into the sortable table
[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" ))
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
84         /* get the emails addresses of the players */
85         $EmailA  = DB_get_email('name',$PlayerA);
86         $EmailB  = DB_get_email('name',$PlayerB);
87         $EmailC  = DB_get_email('name',$PlayerC);
88         $EmailD  = DB_get_email('name',$PlayerD);
89
90         /* this is used to check if the player names are all ok */
91         if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
92           {
93             echo "couldn't find one of the names, please start a new game";
94             return;
95           }
96
97         /* get user ids */
98         $useridA  = DB_get_userid('name',$PlayerA);
99         $useridB  = DB_get_userid('name',$PlayerB);
100         $useridC  = DB_get_userid('name',$PlayerC);
101         $useridD  = DB_get_userid('name',$PlayerD);
102
103         /* create random numbers */
104         $randomNR       = create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD);
105         $randomNRstring = join(":",$randomNR);
106
107         /* create game */
108         $followup = NULL;
109         /* is this game a follow up in an already started session? */
110         if(myisset("followup") )
111           {
112             $followup= $_REQUEST["followup"];
113             $session = DB_get_session_by_gameid($followup);
114             $ruleset = DB_get_ruleset_by_gameid($followup); /* just copy ruleset from old game,
115                                                              this way no manipulation is possible */
116
117             /* check if there is a game in pre or play mode, in that case do nothing */
118             if( DB_is_session_active($session) > 0 )
119               {
120                 echo "<p class=\"message\"> There is already a game going on in session $session, you can't start a new one</p>";
121                 return;
122               }
123             else if ( DB_is_session_active($session) < 0 )
124               {
125                 echo "<p class=\"message\"> ERROR: status of session $session couldn't be determined.</p>";
126                 return;
127               }
128
129             if($session)
130               DB_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
131                        "'$ruleset','$session' ,NULL)");
132             else
133               {
134                 /* get max session and start a new one */
135                 $max = DB_get_max_session();
136                 $max++;
137                 DB_query("UPDATE Game SET session='".$max."' WHERE id=".DB_quote_smart($followup));
138                 DB_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
139                             "'$ruleset','$max' ,NULL)");
140               }
141           }
142         else /* no follow up, start a new session */
143           {
144             /* get ruleset information or create new one */
145             $ruleset = DB_get_ruleset($dullen,$schweinchen,$call);
146             if($ruleset <0)
147               {
148                 myerror("Error defining ruleset: $ruleset");
149                 return;
150               };
151             /* get max session */
152             $max = DB_get_max_session();
153             $max++;
154
155             DB_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre', ".
156                      "'$ruleset','$max' ,NULL)");
157           }
158         $gameid = DB_insert_id();
159
160         /* create hash */
161         $TIME  = (string) time(); /* to avoid collisions */
162         $hashA = md5("AGameOfDoko".$gameid.$PlayerA.$EmailA.$TIME);
163         $hashB = md5("AGameOfDoko".$gameid.$PlayerB.$EmailB.$TIME);
164         $hashC = md5("AGameOfDoko".$gameid.$PlayerC.$EmailC.$TIME);
165         $hashD = md5("AGameOfDoko".$gameid.$PlayerD.$EmailD.$TIME);
166
167         /* create hands */
168         DB_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($gameid).",".DB_quote_smart($useridA).
169                  ", ".DB_quote_smart($hashA).", 'start','1',NULL,NULL,NULL,NULL)");
170         $hand_idA = DB_insert_id();
171         DB_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($gameid).",".DB_quote_smart($useridB).
172                  ", ".DB_quote_smart($hashB).", 'start','2',NULL,NULL,NULL,NULL)");
173         $hand_idB = DB_insert_id();
174         DB_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($gameid).",".DB_quote_smart($useridC).
175                  ", ".DB_quote_smart($hashC).", 'start','3',NULL,NULL,NULL,NULL)");
176         $hand_idC = DB_insert_id();
177         DB_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($gameid).",".DB_quote_smart($useridD).
178                  ", ".DB_quote_smart($hashD).", 'start','4',NULL,NULL,NULL,NULL)");
179         $hand_idD = DB_insert_id();
180
181         /* save cards */
182         for($i=0;$i<12;$i++)
183           DB_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idA', '".$randomNR[$i]."', 'false')");
184         for($i=12;$i<24;$i++)
185           DB_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idB', '".$randomNR[$i]."', 'false')");
186         for($i=24;$i<36;$i++)
187           DB_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idC', '".$randomNR[$i]."', 'false')");
188         for($i=36;$i<48;$i++)
189           DB_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idD', '".$randomNR[$i]."', 'false')");
190
191         /* send out email, TODO: check for error with email */
192         $message = "You are invited to play a game of DoKo.\n".
193           "Please, place comments and bug reports here:\n".
194           "http://wiki.nubati.net/index.php?title=EmailDoko\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 ?>