2 /* Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 Arun Persaud <arun@nubati.net>
4 * This file is part of e-DoKo.
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.
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.
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/>.
21 /* make sure that we are not called from outside the scripts,
22 * use a variable defined in config.php to check this
27 /* user needs to be logged in to do this */
28 if(! isset($_SESSION["name"]) )
30 echo "<div class=\"message\">Please <a href=\"$INDEX\">log in</a>.</div>";
34 $name = $_SESSION["name"];
35 $email = DB_get_email('name',$name);
37 $myid = DB_get_userid('email',$email);
41 DB_update_user_timestamp($myid);
43 if( !myisset("PlayerA", "PlayerB","PlayerC","PlayerD","dullen","schweinchen","callrule","lowtrump" ))
45 display_user_menu($myid);
46 /* only get players that want to be in new games */
47 $names = DB_get_all_user_names_open_for_games();
49 /* add player if he is not open for games */
50 if(!in_array($_SESSION["name"],$names))
51 $names[]=$_SESSION["name"];
53 /* add some randomness */
56 echo "<div class=\"user\">\n";
57 output_form_for_new_game($names);
64 $name = $_SESSION["name"];
66 /* the names of the four players */
67 $PlayerA = $_REQUEST["PlayerA"];
68 $PlayerB = $_REQUEST["PlayerB"];
69 $PlayerC = $_REQUEST["PlayerC"];
70 $PlayerD = $_REQUEST["PlayerD"];
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)))
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>";
79 /* what rules were selected */
80 $dullen = $_REQUEST["dullen"];
81 $schweinchen = $_REQUEST["schweinchen"];
82 $call = $_REQUEST["callrule"];
83 $lowtrump = $_REQUEST["lowtrump"];
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);
91 /* this is used to check if the player names are all ok */
92 if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
94 echo "couldn't find one of the names, please start a new game";
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);
104 /* create random numbers */
105 $randomNR = create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD);
106 $randomNRstring = join(":",$randomNR);
110 /* is this game a follow up in an already started session? */
111 if(myisset("followup") )
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 */
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 )
121 echo "<p class=\"message\"> There is already a game going on in session $session, you can't start a new one</p>";
124 else if ( DB_is_session_active($session) < 0 )
126 echo "<p class=\"message\"> ERROR: status of session $session couldn't be determined.</p>";
131 DB_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
132 "'$ruleset','$session' ,NULL)");
135 /* get max session and start a new one */
136 $max = DB_get_max_session();
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)");
143 else /* no follow up, start a new session */
145 /* get ruleset information or create new one */
146 $ruleset = DB_get_ruleset($dullen,$schweinchen,$call,$lowtrump);
149 myerror("Error defining ruleset: $ruleset");
152 /* get max session */
153 $max = DB_get_max_session();
156 DB_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre', ".
157 "'$ruleset','$max' ,NULL)");
159 $gameid = DB_insert_id();
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);
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();
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')");
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".
200 "If you want to join this game, please follow this link:\n".
201 "".$HOST.$INDEX."?action=game&me=";
203 mymail($useridA, $gameid, GAME_NEW, $message.$hashA."\n\n");
204 mymail($useridB, $gameid, GAME_NEW, $message.$hashB."\n\n");
205 mymail($useridC, $gameid, GAME_NEW, $message.$hashC."\n\n");
206 mymail($useridD, $gameid, GAME_NEW, $message.$hashD."\n\n");
208 echo "<div class=\"message\">You started a new game. The emails have been sent out!</div>\n";
209 display_user_menu($myid);