2 /* Copyright 2006, 2007, 2008, 2009, 2010 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" ))
45 /* only get players that want to be in new games */
46 $names = DB_get_all_user_names_open_for_games();
48 /* add player if he is not open for games */
49 if(!in_array($_SESSION["name"],$names))
50 $names[]=$_SESSION["name"];
52 /* add some randomness */
55 echo "<div class=\"user\">\n";
56 output_form_for_new_game($names);
59 display_user_menu($myid);
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"];
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);
90 /* this is used to check if the player names are all ok */
91 if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
93 echo "couldn't find one of the names, please start a new game";
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);
103 /* create random numbers */
104 $randomNR = create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD);
105 $randomNRstring = join(":",$randomNR);
109 /* is this game a follow up in an already started session? */
110 if(myisset("followup") )
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 */
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 )
120 echo "<p class=\"message\"> There is already a game going on in session $session, you can't start a new one</p>";
123 else if ( DB_is_session_active($session) < 0 )
125 echo "<p class=\"message\"> ERROR: status of session $session couldn't be determined.</p>";
130 DB_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
131 "'$ruleset','$session' ,NULL)");
134 /* get max session and start a new one */
135 $max = DB_get_max_session();
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)");
142 else /* no follow up, start a new session */
144 /* get ruleset information or create new one */
145 $ruleset = DB_get_ruleset($dullen,$schweinchen,$call);
148 myerror("Error defining ruleset: $ruleset");
151 /* get max session */
152 $max = DB_get_max_session();
155 DB_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre', ".
156 "'$ruleset','$max' ,NULL)");
158 $gameid = DB_insert_id();
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);
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();
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')");
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".
200 "If you want to join this game, please follow this link:\n".
201 "".$HOST.$INDEX."?action=game&me=";
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");
209 echo "<div class=\"message\">You started a new game. The emails have been sent out!</div>\n";
210 display_user_menu($myid);