LAYOUT: more tables on the statistics page
[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 /* user needs to be logged in to do this */
9 if(! isset($_SESSION["name"]) )
10   {
11     echo "<div class=\"message\">Please <a href=\"$INDEX\">log in</a>.</div>";
12   }
13 else
14   {
15     $name  = $_SESSION["name"];
16     $email = DB_get_email('name',$name);
17
18     $myid = DB_get_userid('email',$email);
19     if(!$myid)
20       return;
21
22     DB_update_user_timestamp($myid);
23     output_status();
24
25     if( !myisset("PlayerA", "PlayerB","PlayerC","PlayerD","dullen","schweinchen","callrule" ))
26       {
27         $names = DB_get_all_names();
28         echo "<div class=\"user\">\n";
29         output_form_for_new_game($names);
30         echo "</div>\n";
31         display_user_menu();
32       }
33     else
34       {
35         /* get my name */
36         $name = $_SESSION["name"];
37
38         /* the names of the four players */
39         $PlayerA = $_REQUEST["PlayerA"];
40         $PlayerB = $_REQUEST["PlayerB"];
41         $PlayerC = $_REQUEST["PlayerC"];
42         $PlayerD = $_REQUEST["PlayerD"];
43
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)))
46           {
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>";
48             output_footer();
49             DB_close();
50             exit();
51           }
52
53         /* what rules were selected */
54         $dullen      = $_REQUEST["dullen"];
55         $schweinchen = $_REQUEST["schweinchen"];
56         $call        = $_REQUEST["callrule"];
57
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);
63
64         /* this is used to check if the player names are all ok */
65         if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
66           {
67             echo "couldn't find one of the names, please start a new game";
68             output_footer();
69             DB_close();
70             exit();
71           }
72
73         /* get user ids */
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);
78
79         /* create random numbers */
80         $randomNR       = create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD);
81         $randomNRstring = join(":",$randomNR);
82
83         /* create game */
84         $followup = NULL;
85         /* is this game a follow up in an already started session? */
86         if(myisset("followup") )
87           {
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 */
92
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 )
95               {
96                 echo "<p class=\"message\"> There is already a game going on in session $session, you can't start a new one</p>";
97                 output_footer();
98                 DB_close();
99                 exit();
100               }
101             else if ( DB_is_session_active($session) < 0 )
102               {
103                 echo "<p class=\"message\"> ERROR: status of session $session couldn't be determined.</p>";
104                 output_footer();
105                 DB_close();
106                 exit();
107               }
108
109             if($session)
110               DB_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
111                        "'$ruleset','$session' ,NULL)");
112             else
113               {
114                 /* get max session and start a new one */
115                 $max = DB_get_max_session();
116                 $max++;
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)");
120               }
121           }
122         else /* no follow up, start a new session */
123           {
124             /* get ruleset information or create new one */
125             $ruleset = DB_get_ruleset($dullen,$schweinchen,$call);
126             if($ruleset <0)
127               {
128                 myerror("Error defining ruleset: $ruleset");
129                 output_footer();
130                 DB_close();
131                 exit();
132               };
133             /* get max session */
134             $max = DB_get_max_session();
135             $max++;
136
137             DB_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre', ".
138                      "'$ruleset','$max' ,NULL)");
139           }
140         $game_id = DB_insert_id();
141
142         /* create hash */
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);
148
149         /* create hands */
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();
162
163         /* save cards */
164         for($i=0;$i<12;$i++)
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')");
172
173         /* send out email, TODO: check for error with email */
174         $message = "\n".
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".
179           "$PlayerA\n".
180           "$PlayerB\n".
181           "$PlayerC\n".
182           "$PlayerD\n\n".
183           "If you want to join this game, please follow this link:\n\n".
184           "".$HOST.$INDEX."?action=game&me=";
185
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);
190
191         echo "<div class=\"message\">You started a new game. The emails have been sent out!</div>\n";
192         display_user_menu();
193       }
194   }
195
196 ?>