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);
24 if( !myisset("PlayerA", "PlayerB","PlayerC","PlayerD","dullen","schweinchen","callrule" ))
26 /* only get players that want to be in new games */
27 $names = DB_get_all_user_names_open_for_games();
29 /* add player if he is not open for games */
30 if(!in_array($_SESSION["name"],$names))
31 $names[]=$_SESSION["name"];
33 /* add some randomness */
36 echo "<div class=\"user\">\n";
37 output_form_for_new_game($names);
40 display_user_menu($myid);
45 $name = $_SESSION["name"];
47 /* the names of the four players */
48 $PlayerA = $_REQUEST["PlayerA"];
49 $PlayerB = $_REQUEST["PlayerB"];
50 $PlayerC = $_REQUEST["PlayerC"];
51 $PlayerD = $_REQUEST["PlayerD"];
53 /* the person who sets up the game has to be one of the players */
54 if(!in_array($name,array($PlayerA,$PlayerB,$PlayerC,$PlayerD)))
56 echo "<div class=\"message\">You need to be one of the players to start a <a href=\"$INDEX?action=new\">new game</a>.</div>";
60 /* what rules were selected */
61 $dullen = $_REQUEST["dullen"];
62 $schweinchen = $_REQUEST["schweinchen"];
63 $call = $_REQUEST["callrule"];
65 /* get the emails addresses of the players */
66 $EmailA = DB_get_email('name',$PlayerA);
67 $EmailB = DB_get_email('name',$PlayerB);
68 $EmailC = DB_get_email('name',$PlayerC);
69 $EmailD = DB_get_email('name',$PlayerD);
71 /* this is used to check if the player names are all ok */
72 if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
74 echo "couldn't find one of the names, please start a new game";
79 $useridA = DB_get_userid('name',$PlayerA);
80 $useridB = DB_get_userid('name',$PlayerB);
81 $useridC = DB_get_userid('name',$PlayerC);
82 $useridD = DB_get_userid('name',$PlayerD);
84 /* create random numbers */
85 $randomNR = create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD);
86 $randomNRstring = join(":",$randomNR);
90 /* is this game a follow up in an already started session? */
91 if(myisset("followup") )
93 $followup= $_REQUEST["followup"];
94 $session = DB_get_session_by_gameid($followup);
95 $ruleset = DB_get_ruleset_by_gameid($followup); /* just copy ruleset from old game,
96 this way no manipulation is possible */
98 /* check if there is a game in pre or play mode, in that case do nothing */
99 if( DB_is_session_active($session) > 0 )
101 echo "<p class=\"message\"> There is already a game going on in session $session, you can't start a new one</p>";
104 else if ( DB_is_session_active($session) < 0 )
106 echo "<p class=\"message\"> ERROR: status of session $session couldn't be determined.</p>";
111 DB_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
112 "'$ruleset','$session' ,NULL)");
115 /* get max session and start a new one */
116 $max = DB_get_max_session();
118 DB_query("UPDATE Game SET session='".$max."' WHERE id=".DB_quote_smart($followup));
119 DB_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
120 "'$ruleset','$max' ,NULL)");
123 else /* no follow up, start a new session */
125 /* get ruleset information or create new one */
126 $ruleset = DB_get_ruleset($dullen,$schweinchen,$call);
129 myerror("Error defining ruleset: $ruleset");
132 /* get max session */
133 $max = DB_get_max_session();
136 DB_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre', ".
137 "'$ruleset','$max' ,NULL)");
139 $gameid = DB_insert_id();
142 $TIME = (string) time(); /* to avoid collisions */
143 $hashA = md5("AGameOfDoko".$gameid.$PlayerA.$EmailA.$TIME);
144 $hashB = md5("AGameOfDoko".$gameid.$PlayerB.$EmailB.$TIME);
145 $hashC = md5("AGameOfDoko".$gameid.$PlayerC.$EmailC.$TIME);
146 $hashD = md5("AGameOfDoko".$gameid.$PlayerD.$EmailD.$TIME);
149 DB_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($gameid).",".DB_quote_smart($useridA).
150 ", ".DB_quote_smart($hashA).", 'start','1',NULL,NULL,NULL,NULL)");
151 $hand_idA = DB_insert_id();
152 DB_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($gameid).",".DB_quote_smart($useridB).
153 ", ".DB_quote_smart($hashB).", 'start','2',NULL,NULL,NULL,NULL)");
154 $hand_idB = DB_insert_id();
155 DB_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($gameid).",".DB_quote_smart($useridC).
156 ", ".DB_quote_smart($hashC).", 'start','3',NULL,NULL,NULL,NULL)");
157 $hand_idC = DB_insert_id();
158 DB_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($gameid).",".DB_quote_smart($useridD).
159 ", ".DB_quote_smart($hashD).", 'start','4',NULL,NULL,NULL,NULL)");
160 $hand_idD = DB_insert_id();
164 DB_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idA', '".$randomNR[$i]."', 'false')");
165 for($i=12;$i<24;$i++)
166 DB_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idB', '".$randomNR[$i]."', 'false')");
167 for($i=24;$i<36;$i++)
168 DB_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idC', '".$randomNR[$i]."', 'false')");
169 for($i=36;$i<48;$i++)
170 DB_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idD', '".$randomNR[$i]."', 'false')");
172 /* send out email, TODO: check for error with email */
174 "you are invited to play a game of DoKo (that is to debug the program ;).\n".
175 "Place comments and bug reports here:\n".
176 "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
177 "The whole round would consist of the following players:\n".
182 "If you want to join this game, please follow this link:\n\n".
183 "".$HOST.$INDEX."?action=game&me=";
185 mymail($EmailA,"$EmailName You are invited to a game of DoKo (game ".DB_format_gameid($gameid).")",
186 "Hello $PlayerA,\n".$message.$hashA);
187 mymail($EmailB,"$EmailName You are invited to a game of DoKo (game ".DB_format_gameid($gameid).")",
188 "Hello $PlayerB,\n".$message.$hashB);
189 mymail($EmailC,"$EmailName You are invited to a game of DoKo (game ".DB_format_gameid($gameid).")",
190 "Hello $PlayerC,\n".$message.$hashC);
191 mymail($EmailD,"$EmailName You are invited to a game of DoKo (game ".DB_format_gameid($gameid).")",
192 "Hello $PlayerD,\n".$message.$hashD);
194 echo "<div class=\"message\">You started a new game. The emails have been sent out!</div>\n";
195 display_user_menu($myid);