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