LAYOUT: added a CSS reset
[e-DoKo.git] / include / newgame.php
1 <?php
2 /* make sure that we are not called from outside the scripts, 
3  * use a variable defined in config.php to check this
4  */
5 if(!isset($HOST))
6   exit;
7
8 output_status();
9 /* user needs to be logged in to do this */
10 if(! isset($_SESSION["name"]) )
11   {
12     echo "<div class=\"message\">Please <a href=\"$INDEX\">log in</a>.</div>";
13   }
14 else
15   {
16     if( myisset("PlayerA", "PlayerB","PlayerC","PlayerD","dullen","schweinchen","callrule" ))
17       {
18         /* get my name */
19         $name = $_SESSION["name"];
20         
21         /* the names of the four players */
22         $PlayerA = $_REQUEST["PlayerA"];
23         $PlayerB = $_REQUEST["PlayerB"];
24         $PlayerC = $_REQUEST["PlayerC"];
25         $PlayerD = $_REQUEST["PlayerD"];
26         
27         /* the person who sets up the game has to be one of the players */
28         if(!in_array($name,array($PlayerA,$PlayerB,$PlayerC,$PlayerD)))
29           {
30             echo "<div class=\"message\">You need to be one of the players to start a <a href=\"$INDEX?action=new\">new game</a>.</div>";
31             output_footer();
32             DB_close();
33             exit();
34           }
35         
36         /* what rules were selected */
37         $dullen      = $_REQUEST["dullen"];
38         $schweinchen = $_REQUEST["schweinchen"];
39         $call        = $_REQUEST["callrule"];
40         
41         /* get the emails addresses of the players */
42         $EmailA  = DB_get_email('name',$PlayerA);
43         $EmailB  = DB_get_email('name',$PlayerB);
44         $EmailC  = DB_get_email('name',$PlayerC);
45         $EmailD  = DB_get_email('name',$PlayerD);
46         
47         /* this is used to check if the player names are all ok */
48         if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
49           {
50             echo "couldn't find one of the names, please start a new game";
51             output_footer();
52             DB_close();
53             exit();
54           }
55         
56         /* get user ids */
57         $useridA  = DB_get_userid('name',$PlayerA);
58         $useridB  = DB_get_userid('name',$PlayerB);
59         $useridC  = DB_get_userid('name',$PlayerC);
60         $useridD  = DB_get_userid('name',$PlayerD);
61         
62         /* create random numbers */
63         $randomNR       = create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD);
64         $randomNRstring = join(":",$randomNR);
65         
66         /* create game */
67         $followup = NULL;
68         /* is this game a follow up in an already started session? */
69         if(myisset("followup") )
70           {
71             $followup= $_REQUEST["followup"];
72             $session = DB_get_session_by_gameid($followup);
73             $ruleset = DB_get_ruleset_by_gameid($followup); /* just copy ruleset from old game,
74                                                              this way no manipulation is possible */
75             
76             /* check if there is a game in pre or play mode, in that case do nothing */
77             if( DB_is_session_active($session) > 0 )
78               {
79                 echo "<p class=\"message\"> There is already a game going on in session $session, you can't start a new one</p>";
80                 output_footer();
81                 DB_close();
82                 exit();
83               }
84             else if ( DB_is_session_active($session) < 0 )
85               {
86                 echo "<p class=\"message\"> ERROR: status of session $session couldn't be determined.</p>";
87                 output_footer();
88                 DB_close();
89                 exit();
90               }
91             
92             if($session)
93               DB_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
94                        "'$ruleset','$session' ,NULL)");
95             else
96               {
97                 /* get max session and start a new one */
98                 $max = DB_get_max_session();
99                 $max++;
100                 DB_query("UPDATE Game SET session='".$max."' WHERE id=".DB_quote_smart($followup));
101                 DB_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
102                             "'$ruleset','$max' ,NULL)");
103               }
104           }
105         else /* no follow up, start a new session */
106           {
107             /* get ruleset information or create new one */
108             $ruleset = DB_get_ruleset($dullen,$schweinchen,$call);
109             if($ruleset <0)
110               {
111                 myerror("Error defining ruleset: $ruleset");
112                 output_footer();
113                 DB_close();
114                 exit();
115               };
116             /* get max session */
117             $max = DB_get_max_session();
118             $max++;
119             
120             DB_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre', ".
121                      "'$ruleset','$max' ,NULL)");
122           }
123         $game_id = DB_insert_id();
124         
125         /* create hash */
126         $TIME  = (string) time(); /* to avoid collisions */
127         $hashA = md5("AGameOfDoko".$game_id.$PlayerA.$EmailA.$TIME);
128         $hashB = md5("AGameOfDoko".$game_id.$PlayerB.$EmailB.$TIME);
129         $hashC = md5("AGameOfDoko".$game_id.$PlayerC.$EmailC.$TIME);
130         $hashD = md5("AGameOfDoko".$game_id.$PlayerD.$EmailD.$TIME);
131         
132         /* create hands */
133         DB_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridA).
134                  ", ".DB_quote_smart($hashA).", 'start','1',NULL,NULL,NULL,NULL)");
135         $hand_idA = DB_insert_id();
136         DB_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridB).
137                  ", ".DB_quote_smart($hashB).", 'start','2',NULL,NULL,NULL,NULL)");
138         $hand_idB = DB_insert_id();
139         DB_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridC).
140                  ", ".DB_quote_smart($hashC).", 'start','3',NULL,NULL,NULL,NULL)");
141         $hand_idC = DB_insert_id();
142         DB_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridD).
143                  ", ".DB_quote_smart($hashD).", 'start','4',NULL,NULL,NULL,NULL)");
144         $hand_idD = DB_insert_id();
145         
146         /* save cards */
147         for($i=0;$i<12;$i++)
148           DB_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idA', '".$randomNR[$i]."', 'false')");
149         for($i=12;$i<24;$i++)
150           DB_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idB', '".$randomNR[$i]."', 'false')");
151         for($i=24;$i<36;$i++)
152           DB_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idC', '".$randomNR[$i]."', 'false')");
153         for($i=36;$i<48;$i++)
154           DB_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idD', '".$randomNR[$i]."', 'false')");
155         
156         /* send out email, TODO: check for error with email */
157         $message = "\n".
158           "you are invited to play a game of DoKo (that is to debug the program ;).\n".
159           "Place comments and bug reports here:\n".
160           "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
161           "The whole round would consist of the following players:\n".
162           "$PlayerA\n".
163           "$PlayerB\n".
164           "$PlayerC\n".
165           "$PlayerD\n\n".
166           "If you want to join this game, please follow this link:\n\n".
167           "".$HOST.$INDEX."?action=game&me=";
168         
169         mymail($EmailA,"You are invited to a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
170         mymail($EmailB,"You are invited to a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
171         mymail($EmailC,"You are invited to a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
172         mymail($EmailD,"You are invited to a game of DoKo","Hello $PlayerD,\n".$message.$hashD);
173         
174         echo "<div class=\"message\">You started a new game. The emails have been sent out!</div>\n";
175       }
176     else
177       {
178         $names = DB_get_all_names();
179         echo "<div class=\"user\">\n";
180         output_form_for_new_game($names);
181         echo "</div>\n";
182         display_user_menu();
183       }
184   }
185
186 ?>