BUGFIX: error in javascript, sickness didn't get cleared
[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
24     if( !myisset("PlayerA", "PlayerB","PlayerC","PlayerD","dullen","schweinchen","callrule" ))
25       {
26         $names = DB_get_all_names();
27         echo "<div class=\"user\">\n";
28         output_form_for_new_game($names);
29         echo "</div>\n";
30         display_user_menu();
31       }
32     else
33       {
34         /* get my name */
35         $name = $_SESSION["name"];
36
37         /* the names of the four players */
38         $PlayerA = $_REQUEST["PlayerA"];
39         $PlayerB = $_REQUEST["PlayerB"];
40         $PlayerC = $_REQUEST["PlayerC"];
41         $PlayerD = $_REQUEST["PlayerD"];
42
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)))
45           {
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>";
47             return;
48           }
49
50         /* what rules were selected */
51         $dullen      = $_REQUEST["dullen"];
52         $schweinchen = $_REQUEST["schweinchen"];
53         $call        = $_REQUEST["callrule"];
54
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);
60
61         /* this is used to check if the player names are all ok */
62         if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
63           {
64             echo "couldn't find one of the names, please start a new game";
65             return;
66           }
67
68         /* get user ids */
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);
73
74         /* create random numbers */
75         $randomNR       = create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD);
76         $randomNRstring = join(":",$randomNR);
77
78         /* create game */
79         $followup = NULL;
80         /* is this game a follow up in an already started session? */
81         if(myisset("followup") )
82           {
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 */
87
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 )
90               {
91                 echo "<p class=\"message\"> There is already a game going on in session $session, you can't start a new one</p>";
92                 return;
93               }
94             else if ( DB_is_session_active($session) < 0 )
95               {
96                 echo "<p class=\"message\"> ERROR: status of session $session couldn't be determined.</p>";
97                 return;
98               }
99
100             if($session)
101               DB_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
102                        "'$ruleset','$session' ,NULL)");
103             else
104               {
105                 /* get max session and start a new one */
106                 $max = DB_get_max_session();
107                 $max++;
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)");
111               }
112           }
113         else /* no follow up, start a new session */
114           {
115             /* get ruleset information or create new one */
116             $ruleset = DB_get_ruleset($dullen,$schweinchen,$call);
117             if($ruleset <0)
118               {
119                 myerror("Error defining ruleset: $ruleset");
120                 return;
121               };
122             /* get max session */
123             $max = DB_get_max_session();
124             $max++;
125
126             DB_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre', ".
127                      "'$ruleset','$max' ,NULL)");
128           }
129         $gameid = DB_insert_id();
130
131         /* create hash */
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);
137
138         /* create hands */
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();
151
152         /* save cards */
153         for($i=0;$i<12;$i++)
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')");
161
162         /* send out email, TODO: check for error with email */
163         $message = "\n".
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".
168           "$PlayerA\n".
169           "$PlayerB\n".
170           "$PlayerC\n".
171           "$PlayerD\n\n".
172           "If you want to join this game, please follow this link:\n\n".
173           "".$HOST.$INDEX."?action=game&me=";
174
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);
183
184         echo "<div class=\"message\">You started a new game. The emails have been sent out!</div>\n";
185         display_user_menu();
186       }
187   }
188
189 ?>