BUGFIX: show correct name of all players, when looking at games of other players
[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         /* only get players that want to be in new games */
27         $names = DB_get_all_user_names_open_for_games();
28
29         /* add player if he is not open for games */
30         if(!in_array($_SESSION["name"],$names))
31           $names[]=$_SESSION["name"];
32
33         /* add some randomness */
34         shuffle($names);
35
36         echo "<div class=\"user\">\n";
37         output_form_for_new_game($names);
38         echo "</div>\n";
39
40         display_user_menu($myid);
41       }
42     else
43       {
44         /* get my name */
45         $name = $_SESSION["name"];
46
47         /* the names of the four players */
48         $PlayerA = $_REQUEST["PlayerA"];
49         $PlayerB = $_REQUEST["PlayerB"];
50         $PlayerC = $_REQUEST["PlayerC"];
51         $PlayerD = $_REQUEST["PlayerD"];
52
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)))
55           {
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>";
57             return;
58           }
59
60         /* what rules were selected */
61         $dullen      = $_REQUEST["dullen"];
62         $schweinchen = $_REQUEST["schweinchen"];
63         $call        = $_REQUEST["callrule"];
64
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);
70
71         /* this is used to check if the player names are all ok */
72         if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
73           {
74             echo "couldn't find one of the names, please start a new game";
75             return;
76           }
77
78         /* get user ids */
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);
83
84         /* create random numbers */
85         $randomNR       = create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD);
86         $randomNRstring = join(":",$randomNR);
87
88         /* create game */
89         $followup = NULL;
90         /* is this game a follow up in an already started session? */
91         if(myisset("followup") )
92           {
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 */
97
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 )
100               {
101                 echo "<p class=\"message\"> There is already a game going on in session $session, you can't start a new one</p>";
102                 return;
103               }
104             else if ( DB_is_session_active($session) < 0 )
105               {
106                 echo "<p class=\"message\"> ERROR: status of session $session couldn't be determined.</p>";
107                 return;
108               }
109
110             if($session)
111               DB_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
112                        "'$ruleset','$session' ,NULL)");
113             else
114               {
115                 /* get max session and start a new one */
116                 $max = DB_get_max_session();
117                 $max++;
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)");
121               }
122           }
123         else /* no follow up, start a new session */
124           {
125             /* get ruleset information or create new one */
126             $ruleset = DB_get_ruleset($dullen,$schweinchen,$call);
127             if($ruleset <0)
128               {
129                 myerror("Error defining ruleset: $ruleset");
130                 return;
131               };
132             /* get max session */
133             $max = DB_get_max_session();
134             $max++;
135
136             DB_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre', ".
137                      "'$ruleset','$max' ,NULL)");
138           }
139         $gameid = DB_insert_id();
140
141         /* create hash */
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);
147
148         /* create hands */
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();
161
162         /* save cards */
163         for($i=0;$i<12;$i++)
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')");
171
172         /* send out email, TODO: check for error with email */
173         $message = "You are invited to play a game of DoKo.\n".
174           "Please, place comments and bug reports here:\n".
175           "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
176           "The whole round would consist of the following players:\n".
177           "$PlayerA\n".
178           "$PlayerB\n".
179           "$PlayerC\n".
180           "$PlayerD\n\n".
181           "If you want to join this game, please follow this link:\n".
182           "".$HOST.$INDEX."?action=game&me=";
183
184         $subject = 'You are invited to a game of DoKo (game '.DB_format_gameid($gameid).')';
185         mymail($useridA,$subject, $message.$hashA."\n\n");
186         mymail($useridB,$subject, $message.$hashB."\n\n");
187         mymail($useridC,$subject, $message.$hashC."\n\n");
188         mymail($useridD,$subject, $message.$hashD."\n\n");
189
190         echo "<div class=\"message\">You started a new game. The emails have been sent out!</div>\n";
191         display_user_menu($myid);
192       }
193   }
194
195 ?>