2 /* make sure that we are not called from outside the scripts,
3 * use a variable defined in config.php to check this
8 /* user needs to be logged in to do this */
9 if(! isset($_SESSION["name"]) )
11 echo "<div class=\"message\">Please <a href=\"$INDEX\">log in</a>.</div>";
15 $name = $_SESSION["name"];
16 $email = DB_get_email('name',$name);
18 $myid = DB_get_userid('email',$email);
22 DB_update_user_timestamp($myid);
25 if( !myisset("PlayerA", "PlayerB","PlayerC","PlayerD","dullen","schweinchen","callrule" ))
27 $names = DB_get_all_names();
28 echo "<div class=\"user\">\n";
29 output_form_for_new_game($names);
36 $name = $_SESSION["name"];
38 /* the names of the four players */
39 $PlayerA = $_REQUEST["PlayerA"];
40 $PlayerB = $_REQUEST["PlayerB"];
41 $PlayerC = $_REQUEST["PlayerC"];
42 $PlayerD = $_REQUEST["PlayerD"];
44 /* the person who sets up the game has to be one of the players */
45 if(!in_array($name,array($PlayerA,$PlayerB,$PlayerC,$PlayerD)))
47 echo "<div class=\"message\">You need to be one of the players to start a <a href=\"$INDEX?action=new\">new game</a>.</div>";
53 /* what rules were selected */
54 $dullen = $_REQUEST["dullen"];
55 $schweinchen = $_REQUEST["schweinchen"];
56 $call = $_REQUEST["callrule"];
58 /* get the emails addresses of the players */
59 $EmailA = DB_get_email('name',$PlayerA);
60 $EmailB = DB_get_email('name',$PlayerB);
61 $EmailC = DB_get_email('name',$PlayerC);
62 $EmailD = DB_get_email('name',$PlayerD);
64 /* this is used to check if the player names are all ok */
65 if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
67 echo "couldn't find one of the names, please start a new game";
74 $useridA = DB_get_userid('name',$PlayerA);
75 $useridB = DB_get_userid('name',$PlayerB);
76 $useridC = DB_get_userid('name',$PlayerC);
77 $useridD = DB_get_userid('name',$PlayerD);
79 /* create random numbers */
80 $randomNR = create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD);
81 $randomNRstring = join(":",$randomNR);
85 /* is this game a follow up in an already started session? */
86 if(myisset("followup") )
88 $followup= $_REQUEST["followup"];
89 $session = DB_get_session_by_gameid($followup);
90 $ruleset = DB_get_ruleset_by_gameid($followup); /* just copy ruleset from old game,
91 this way no manipulation is possible */
93 /* check if there is a game in pre or play mode, in that case do nothing */
94 if( DB_is_session_active($session) > 0 )
96 echo "<p class=\"message\"> There is already a game going on in session $session, you can't start a new one</p>";
101 else if ( DB_is_session_active($session) < 0 )
103 echo "<p class=\"message\"> ERROR: status of session $session couldn't be determined.</p>";
110 DB_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
111 "'$ruleset','$session' ,NULL)");
114 /* get max session and start a new one */
115 $max = DB_get_max_session();
117 DB_query("UPDATE Game SET session='".$max."' WHERE id=".DB_quote_smart($followup));
118 DB_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
119 "'$ruleset','$max' ,NULL)");
122 else /* no follow up, start a new session */
124 /* get ruleset information or create new one */
125 $ruleset = DB_get_ruleset($dullen,$schweinchen,$call);
128 myerror("Error defining ruleset: $ruleset");
133 /* get max session */
134 $max = DB_get_max_session();
137 DB_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre', ".
138 "'$ruleset','$max' ,NULL)");
140 $game_id = DB_insert_id();
143 $TIME = (string) time(); /* to avoid collisions */
144 $hashA = md5("AGameOfDoko".$game_id.$PlayerA.$EmailA.$TIME);
145 $hashB = md5("AGameOfDoko".$game_id.$PlayerB.$EmailB.$TIME);
146 $hashC = md5("AGameOfDoko".$game_id.$PlayerC.$EmailC.$TIME);
147 $hashD = md5("AGameOfDoko".$game_id.$PlayerD.$EmailD.$TIME);
150 DB_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridA).
151 ", ".DB_quote_smart($hashA).", 'start','1',NULL,NULL,NULL,NULL)");
152 $hand_idA = DB_insert_id();
153 DB_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridB).
154 ", ".DB_quote_smart($hashB).", 'start','2',NULL,NULL,NULL,NULL)");
155 $hand_idB = DB_insert_id();
156 DB_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridC).
157 ", ".DB_quote_smart($hashC).", 'start','3',NULL,NULL,NULL,NULL)");
158 $hand_idC = DB_insert_id();
159 DB_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridD).
160 ", ".DB_quote_smart($hashD).", 'start','4',NULL,NULL,NULL,NULL)");
161 $hand_idD = DB_insert_id();
165 DB_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idA', '".$randomNR[$i]."', 'false')");
166 for($i=12;$i<24;$i++)
167 DB_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idB', '".$randomNR[$i]."', 'false')");
168 for($i=24;$i<36;$i++)
169 DB_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idC', '".$randomNR[$i]."', 'false')");
170 for($i=36;$i<48;$i++)
171 DB_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idD', '".$randomNR[$i]."', 'false')");
173 /* send out email, TODO: check for error with email */
175 "you are invited to play a game of DoKo (that is to debug the program ;).\n".
176 "Place comments and bug reports here:\n".
177 "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
178 "The whole round would consist of the following players:\n".
183 "If you want to join this game, please follow this link:\n\n".
184 "".$HOST.$INDEX."?action=game&me=";
186 mymail($EmailA,"You are invited to a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
187 mymail($EmailB,"You are invited to a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
188 mymail($EmailC,"You are invited to a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
189 mymail($EmailD,"You are invited to a game of DoKo","Hello $PlayerD,\n".$message.$hashD);
191 echo "<div class=\"message\">You started a new game. The emails have been sent out!</div>\n";