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 $names = DB_get_all_names();
27 echo "<div class=\"user\">\n";
28 output_form_for_new_game($names);
35 $name = $_SESSION["name"];
37 /* the names of the four players */
38 $PlayerA = $_REQUEST["PlayerA"];
39 $PlayerB = $_REQUEST["PlayerB"];
40 $PlayerC = $_REQUEST["PlayerC"];
41 $PlayerD = $_REQUEST["PlayerD"];
43 /* the person who sets up the game has to be one of the players */
44 if(!in_array($name,array($PlayerA,$PlayerB,$PlayerC,$PlayerD)))
46 echo "<div class=\"message\">You need to be one of the players to start a <a href=\"$INDEX?action=new\">new game</a>.</div>";
50 /* what rules were selected */
51 $dullen = $_REQUEST["dullen"];
52 $schweinchen = $_REQUEST["schweinchen"];
53 $call = $_REQUEST["callrule"];
55 /* get the emails addresses of the players */
56 $EmailA = DB_get_email('name',$PlayerA);
57 $EmailB = DB_get_email('name',$PlayerB);
58 $EmailC = DB_get_email('name',$PlayerC);
59 $EmailD = DB_get_email('name',$PlayerD);
61 /* this is used to check if the player names are all ok */
62 if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
64 echo "couldn't find one of the names, please start a new game";
69 $useridA = DB_get_userid('name',$PlayerA);
70 $useridB = DB_get_userid('name',$PlayerB);
71 $useridC = DB_get_userid('name',$PlayerC);
72 $useridD = DB_get_userid('name',$PlayerD);
74 /* create random numbers */
75 $randomNR = create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD);
76 $randomNRstring = join(":",$randomNR);
80 /* is this game a follow up in an already started session? */
81 if(myisset("followup") )
83 $followup= $_REQUEST["followup"];
84 $session = DB_get_session_by_gameid($followup);
85 $ruleset = DB_get_ruleset_by_gameid($followup); /* just copy ruleset from old game,
86 this way no manipulation is possible */
88 /* check if there is a game in pre or play mode, in that case do nothing */
89 if( DB_is_session_active($session) > 0 )
91 echo "<p class=\"message\"> There is already a game going on in session $session, you can't start a new one</p>";
94 else if ( DB_is_session_active($session) < 0 )
96 echo "<p class=\"message\"> ERROR: status of session $session couldn't be determined.</p>";
101 DB_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
102 "'$ruleset','$session' ,NULL)");
105 /* get max session and start a new one */
106 $max = DB_get_max_session();
108 DB_query("UPDATE Game SET session='".$max."' WHERE id=".DB_quote_smart($followup));
109 DB_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
110 "'$ruleset','$max' ,NULL)");
113 else /* no follow up, start a new session */
115 /* get ruleset information or create new one */
116 $ruleset = DB_get_ruleset($dullen,$schweinchen,$call);
119 myerror("Error defining ruleset: $ruleset");
122 /* get max session */
123 $max = DB_get_max_session();
126 DB_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre', ".
127 "'$ruleset','$max' ,NULL)");
129 $gameid = DB_insert_id();
132 $TIME = (string) time(); /* to avoid collisions */
133 $hashA = md5("AGameOfDoko".$gameid.$PlayerA.$EmailA.$TIME);
134 $hashB = md5("AGameOfDoko".$gameid.$PlayerB.$EmailB.$TIME);
135 $hashC = md5("AGameOfDoko".$gameid.$PlayerC.$EmailC.$TIME);
136 $hashD = md5("AGameOfDoko".$gameid.$PlayerD.$EmailD.$TIME);
139 DB_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($gameid).",".DB_quote_smart($useridA).
140 ", ".DB_quote_smart($hashA).", 'start','1',NULL,NULL,NULL,NULL)");
141 $hand_idA = DB_insert_id();
142 DB_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($gameid).",".DB_quote_smart($useridB).
143 ", ".DB_quote_smart($hashB).", 'start','2',NULL,NULL,NULL,NULL)");
144 $hand_idB = DB_insert_id();
145 DB_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($gameid).",".DB_quote_smart($useridC).
146 ", ".DB_quote_smart($hashC).", 'start','3',NULL,NULL,NULL,NULL)");
147 $hand_idC = DB_insert_id();
148 DB_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($gameid).",".DB_quote_smart($useridD).
149 ", ".DB_quote_smart($hashD).", 'start','4',NULL,NULL,NULL,NULL)");
150 $hand_idD = DB_insert_id();
154 DB_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idA', '".$randomNR[$i]."', 'false')");
155 for($i=12;$i<24;$i++)
156 DB_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idB', '".$randomNR[$i]."', 'false')");
157 for($i=24;$i<36;$i++)
158 DB_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idC', '".$randomNR[$i]."', 'false')");
159 for($i=36;$i<48;$i++)
160 DB_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idD', '".$randomNR[$i]."', 'false')");
162 /* send out email, TODO: check for error with email */
164 "you are invited to play a game of DoKo (that is to debug the program ;).\n".
165 "Place comments and bug reports here:\n".
166 "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
167 "The whole round would consist of the following players:\n".
172 "If you want to join this game, please follow this link:\n\n".
173 "".$HOST.$INDEX."?action=game&me=";
175 mymail($EmailA,"$EmailName You are invited to a game of DoKo (game ".DB_format_gameid($gameid).")",
176 "Hello $PlayerA,\n".$message.$hashA);
177 mymail($EmailB,"$EmailName You are invited to a game of DoKo (game ".DB_format_gameid($gameid).")",
178 "Hello $PlayerB,\n".$message.$hashB);
179 mymail($EmailC,"$EmailName You are invited to a game of DoKo (game ".DB_format_gameid($gameid).")",
180 "Hello $PlayerC,\n".$message.$hashC);
181 mymail($EmailD,"$EmailName You are invited to a game of DoKo (game ".DB_format_gameid($gameid).")",
182 "Hello $PlayerD,\n".$message.$hashD);
184 echo "<div class=\"message\">You started a new game. The emails have been sent out!</div>\n";