added a uniform list name for emails
[e-DoKo.git] / index.php
1 <?php
2 error_reporting(E_ALL);
3
4 global $REV;
5 $REV  ="\$Rev$";
6
7 include_once("config.php");      
8 include_once("output.php");      /* html output only */
9 include_once("db.php");          /* database only */
10 include_once("functions.php");   /* the rest */
11
12 /* check if some variables are set in the config file, else set defaults */
13 if(!isset($EmailName))
14      $EmailName="[DoKo] ";
15
16 if(0)
17 {
18   output_header();
19   echo "Working on the database...please check back in a few mintues";
20   output_footer();
21   exit();
22 }
23
24 DB_open();
25 output_header();
26
27 /* check if we want to start a new game */
28 if(myisset("new"))
29 {
30   $names = DB_get_all_names();
31   output_form_for_new_game($names);
32 }
33 /*check if everything is ready to set up a new game */
34 else if( myisset("PlayerA", "PlayerB","PlayerC","PlayerD" ))
35   {
36     $PlayerA = $_REQUEST["PlayerA"];
37     $PlayerB = $_REQUEST["PlayerB"];
38     $PlayerC = $_REQUEST["PlayerC"];
39     $PlayerD = $_REQUEST["PlayerD"];
40     
41     $EmailA  = DB_get_email_by_name($PlayerA);
42     $EmailB  = DB_get_email_by_name($PlayerB);
43     $EmailC  = DB_get_email_by_name($PlayerC);
44     $EmailD  = DB_get_email_by_name($PlayerD);
45     
46     if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
47       {
48         echo "couldn't find one of the names, please start a new game";
49         exit();
50       }
51     
52     $useridA  = DB_get_userid_by_name($PlayerA);
53     $useridB  = DB_get_userid_by_name($PlayerB);
54     $useridC  = DB_get_userid_by_name($PlayerC);
55     $useridD  = DB_get_userid_by_name($PlayerD);
56     
57     /* create random numbers */
58     $randomNR       = create_array_of_random_numbers();
59     $randomNRstring = join(":",$randomNR);
60     
61     /* create game */
62     $followup = NULL;
63     if(myisset("followup") )
64       {
65         $followup= $_REQUEST["followup"];
66         mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,'1','pre','$followup' ,NULL)");
67       }
68     else
69       mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,'1','pre', NULL ,NULL)");
70     $game_id = mysql_insert_id();
71     
72     /* create hash */
73     $hashA = md5("AGameOfDoko".$game_id.$PlayerA.$EmailA);
74     $hashB = md5("AGameOfDoko".$game_id.$PlayerB.$EmailB);
75     $hashC = md5("AGameOfDoko".$game_id.$PlayerC.$EmailC);
76     $hashD = md5("AGameOfDoko".$game_id.$PlayerD.$EmailD);
77     
78     /* create hands */
79     mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridA).
80                 ", ".DB_quote_smart($hashA).", 'start','1',NULL,NULL,NULL,'false','false',NULL)");
81     $hand_idA = mysql_insert_id();                                                             
82     mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridB).
83                 ", ".DB_quote_smart($hashB).", 'start','2',NULL,NULL,NULL,'false','false',NULL)");
84     $hand_idB = mysql_insert_id();                                                             
85     mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridC).
86                 ", ".DB_quote_smart($hashC).", 'start','3',NULL,NULL,NULL,'false','false',NULL)");
87     $hand_idC = mysql_insert_id();                                                             
88     mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridD).
89                 ", ".DB_quote_smart($hashD).", 'start','4',NULL,NULL,NULL,'false','false',NULL)");
90     $hand_idD = mysql_insert_id();
91     
92     /* save cards */
93     for($i=0;$i<12;$i++)
94       mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idA', '".$randomNR[$i]."', 'false')");
95     for($i=12;$i<24;$i++)
96       mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idB', '".$randomNR[$i]."', 'false')");
97     for($i=24;$i<36;$i++)
98       mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idC', '".$randomNR[$i]."', 'false')");
99     for($i=36;$i<48;$i++)
100       mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idD', '".$randomNR[$i]."', 'false')");
101
102     /* send out email, TODO: check for error with email */
103     $message = "\n".
104       "you are invited to play a game of DoKo (that is to debug the program ;).\n".
105       "Place comments and bug reports here:\n".
106       "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
107       "The whole round would consist of the following players:\n".
108       "$PlayerA\n".
109       "$PlayerB\n".
110       "$PlayerC\n".
111       "$PlayerD\n\n".
112       "If you want to join this game, please follow this link:\n\n".
113       " ".$host."?me=";
114     
115     mymail($EmailA,"You are invited to a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
116     mymail($EmailB,"You are invited to a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
117     mymail($EmailC,"You are invited to a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
118     mymail($EmailD,"You are invited to a game of DoKo","Hello $PlayerD,\n".$message.$hashD);
119
120     
121     echo "You started a new game. The emails have been sent out!";    
122   }    
123 /* end set up a new game */
124
125 else if(myisset("me"))
126   {
127     /* handle request from one specific player,
128      * the hash is set on a per game base
129      */
130     
131     $me = $_REQUEST["me"];
132
133     /* test for valid ID */
134     $myid = DB_get_userid_by_hash($me);
135     if(!$myid)
136       {
137         echo "Can't find you in the database, please check the url.<br />\n";
138         echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
139         exit();
140       }
141     
142     DB_update_user_timestamp($myid);
143
144     /* get some information from the DB */
145     $gameid   = DB_get_gameid_by_hash($me);
146     $myname   = DB_get_name_by_hash($me);
147     $mystatus = DB_get_status_by_hash($me);
148     $mypos    = DB_get_pos_by_hash($me);
149
150     echo "<p class=\"gamenumber\"> Game $gameid </p>\n";
151     
152
153     
154     if(myisset("cancle"))
155       echo "<p style=\"background-color:red\";>canceling a game is not implemented at the moment, but will be soon...<br /><br /></p>";
156
157     switch($mystatus)
158       {
159       case 'start':
160         check_want_to_play($me);
161         DB_set_hand_status_by_hash($me,'init');
162         break;
163       case 'init':
164         if( !myisset("in","update") )
165           {
166             DB_set_hand_status_by_hash($me,'start');
167             echo "you need to answer both question";
168           }
169         else
170           {
171             if($_REQUEST["in"] == "no")
172               {
173                 $message = "Hello, \n\n".
174                   "the game has been canceled due to the request of one of the players.\n";
175                 
176                 $userids = DB_get_all_userid_by_gameid($gameid);
177                 foreach($userids as $user)
178                   {
179                     $To = DB_get_email_by_userid($user);
180                     mymail($To,$EmailName."game cancled",$message);
181                   }
182                 
183                 /* delete everything from the dB */
184                 DB_cancel_game($me);
185               }
186             else
187               {
188                 echo "thanks for joining the game... please scroll down";
189                 echo "TODO: make this page nicer<br />";
190                 echo "TODO: set card pref<br />";
191                 
192                 $mycards = DB_get_hand($me);
193                 sort($mycards);
194                 echo "<p class=\"mycards\">your cards are: <br />\n";
195                 foreach($mycards as $card) 
196                   display_card($card);
197                 echo "</p>\n";   
198                 
199                 check_for_sickness($me,$mycards);
200                 
201                 DB_set_hand_status_by_hash($me,'check');
202               }
203            }
204         break;
205         
206       case 'check':
207         echo "checking if you selected solo or nines...<br />".
208           " if you have a wedding, please send an email to the other players. <br />".
209           " if you have poverty you need to play a normal game,sorry...<br />".
210           " Please click <a href=\"$host?me=$me\">here</a> to finish the setup.<br />";
211         if(!myisset("solo","wedding","poverty","nines") )
212           {
213             /* all these variables have a pre-selected default,
214              * so we should never get here,
215              * unless a user tries to cheat ;) */
216             echo "something went wrong...please contact the admin.";
217           }
218         else
219           {
220             /* check if this sickness needs to be handled first */
221             $gametype    = DB_get_gametype_by_gameid($gameid);
222             $startplayer = DB_get_startplayer_by_gameid($gameid);
223
224             if( $_REQUEST["solo"]!="No")
225               {
226                 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
227                 DB_set_sickness_by_hash($me,"solo");
228                 echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
229                 
230                 if($gametype == "solo" && $startplayer<$mypos)
231                   {}/* do nothing */
232                 else
233                   {
234                     /* this solo comes first */
235                     DB_set_gametype_by_gameid($gameid,"solo");
236                     DB_set_startplayer_by_gameid($gameid,$mypos);
237                     DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
238                   };
239               }
240             else if($_REQUEST["wedding"] == "yes")
241               {
242                 /* TODO: add silent solo somewhere*/
243                 echo "wedding was chosen<br />\n";
244                 DB_set_sickness_by_hash($me,"wedding");
245               }
246             else if($_REQUEST["poverty"] == "yes")
247               {
248                 echo "poverty was chosen<br />\n";
249                 DB_set_sickness_by_hash($me,"poverty");
250               }
251             else if($_REQUEST["nines"] == "yes")
252               {
253                 echo "nines was chosen<br />\n";
254                  DB_set_sickness_by_hash($me,"nines");
255               }
256           }
257
258         DB_set_hand_status_by_hash($me,'poverty');
259
260         /* check all players and set game to final result, e.g. solo, wedding, povert, redeal */
261
262         break;
263       case 'poverty':
264         /* here we need to check if there is a solo or some other form of sickness.
265          * If so, which one counts
266          * set that one in the Game table, delete other ones form Hand table 
267          * tell people about it.
268          */
269         echo "<br />checking if someone else selected solo or nines... wedding and poverty not handled at the moment<br />".
270           " Please click <a href=\"$host?me=$me\">here</a> to finish the setup.<br />";  
271         
272         /* only set this after all poverty, etc. are handeled*/
273         DB_set_hand_status_by_hash($me,'play');
274
275         /* check if the game can start  */
276         $userids = DB_get_all_userid_by_gameid($gameid);
277         $ok=1;
278         foreach($userids as $user)
279           if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='play')
280             $ok=0;
281
282         if($ok)
283           {
284             DB_set_game_status_by_gameid($gameid,'play');
285
286             /* check what kind of game we are playing */
287             $gametype    = DB_get_gametype_by_gameid($gameid);
288             $startplayer = DB_get_startplayer_by_gameid($gameid);
289
290             /* nines? */
291             $nines = 0;
292             /* check for nines */
293             foreach($userids as $user)
294               {
295                 if(DB_get_sickness_by_userid_and_gameid($user,$gameid) == 'nines')
296                   $nines = 1;
297               }
298             
299             /* gamestatus == normal, => cancel game */
300             if($nines && $gamestatus == "normal")
301               {
302                 /* TODO: set game type to nines to be able to keep statistics */
303                 
304                 $message = "Hello, \n\n".
305                   "the game has been cancled becausee someone has five or more nines.\n";
306                 
307                 $userids = DB_get_all_userid_by_gameid($gameid);
308                 foreach($userids as $user)
309                   {
310                     $To = DB_get_email_by_userid($user);
311                     mymail($To,$EmailName."game cancled",$message);
312                   }
313                 
314                 /* delete everything from the dB */
315                 DB_cancel_game($me);
316
317               }
318
319             /* poverty? */
320             $poverty = 0;
321
322             /* check players for poverty */
323             foreach($userids as $user)
324               {
325                 if(DB_get_sickness_by_userid_and_gameid($user,$gameid) == 'poverty')
326                   {
327                     $poverty++;
328                     $name = DB_get_name_by_userid($user);
329                     echo "$name has a Vorbehalt. <br />";
330                   }
331               }
332             /* if gamestatus == normal, set poverty or dpovert (in case two people have poverty) */
333
334             /* wedding? */
335             $wedding = 0;
336             /* check players for wedding */
337             foreach($userids as $user)
338               {
339                 if(DB_get_sickness_by_userid_and_gameid($user,$gameid) == 'wedding')
340                   {
341                     $wedding++;
342                     $name = DB_get_name_by_userid($user);
343                     echo "$name has a Vorbehalt. <br />"  ;
344                   }
345               }
346
347             /* if gamestatus == normal, set wedding  */
348             
349           }
350         
351         break;
352       case 'play':
353       case 'gameover': 
354         /* both entries here,  so that the tricks are visible for both.
355          * in case of 'play' there is a break later that skips the last part
356          */
357
358         /* figure out what kind of game we are playing, 
359          * set the global variables $TRUMP,$DIAMONDS,$HEARTS,$CLUBS,$SPADES
360          * accordingly
361          */
362         
363         $gametype = DB_get_gametype_by_gameid($gameid);
364         $GT = $gametype;
365         if($gametype=="solo")
366           {
367             $gametype = DB_get_solo_by_gameid($gameid);
368             $GT = $gametype." ".$GT;
369           }
370         else
371           $gametype="normal";
372         
373         set_gametype($gametype);
374
375         /* get some infos about the game */
376         $gamestatus = DB_get_game_status_by_gameid($gameid);
377
378         /* display useful things in divs */
379         
380         /* display local time */
381         echo "<div class=\"time\">\n Local times:<table>";
382         $users = array();
383         $users = DB_get_all_userid_by_gameid($gameid);
384         foreach($users as $user)
385           {
386             $offset = DB_get_user_timezone($user);
387             $zone   = return_timezone($offset);
388             date_default_timezone_set($zone);
389             $name   = DB_get_name_by_userid($user);
390             
391             echo "<tr> <td>$name</td> <td>".date("Y-m-d H:i:s")."</td></tr>\n";
392           };
393         echo "</table>\n</div>\n";
394
395         if($gamestatus != 'pre')
396           display_status($GT);
397
398         /* display links to the users status page */
399         $result = mysql_query("SELECT email,password from User WHERE id='$myid'" );
400         $r      = mysql_fetch_array($result,MYSQL_NUM);
401         output_link_to_user_page($r[0],$r[1]);
402           
403         display_news();
404
405         /* end display useful things*/
406
407         /* has the game started? No, then just wait here...*/
408         if($gamestatus == 'pre')
409           {
410             echo "you need to wait for the others... <br />";
411             break; /* not sure this works... the idea is that you can 
412                     * only  play a card after everyone is ready to play */
413           }
414         
415         /* display the table and the names */
416         $result = mysql_query("SELECT  User.fullname as name,".
417                               "        Hand.position as position ".
418                               "FROM Hand ".
419                               "LEFT JOIN User ON User.id=Hand.user_id ".
420                               "WHERE Hand.game_id='".$gameid."' ".
421                               "ORDER BY position ASC");
422         
423         echo "<div class=\"table\">\n".
424           "  <img src=\"pics/table.png\" alt=\"table\" />\n";
425         while($r = mysql_fetch_array($result,MYSQL_NUM))
426           {
427             $name = $r[0];
428             $pos  = $r[1];
429             
430             echo " <span class=\"table".($pos-1)."\">$name</span>\n";
431           }
432         echo  "</div>\n";
433         
434         /* get everything relevant to display the tricks */
435         $result = mysql_query("SELECT Hand_Card.card_id as card,".
436                               "       Hand.position as position,".
437                               "       Play.sequence as sequence, ".
438                               "       Trick.id, ".
439                               "       Comment.comment ".
440                               "FROM Trick ".
441                               "LEFT JOIN Play ON Trick.id=Play.trick_id ".
442                               "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
443                               "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
444                               "LEFT JOIN Comment ON Play.id=Comment.play_id ".
445                               "WHERE Trick.game_id='".$gameid."' ".
446                               "ORDER BY Trick.id,sequence ASC");
447         $trickNR = 1;
448         
449         $lasttrick = DB_get_max_trickid($gameid);
450         
451         $play = array(); /* needed to calculate winner later  */
452         $seq  = 1;          
453         $pos  = DB_get_startplayer_by_gameid($gameid)-1; 
454         $firstcard = ""; /* first card in a trick */
455         
456         echo "\n<ul class=\"tricks\">\n";
457         echo "  <li> Hello $myname!   History: </li>\n";
458         
459         while($r = mysql_fetch_array($result,MYSQL_NUM))
460           {
461             $pos     = $r[1];
462             $seq     = $r[2];
463             $trick   = $r[3];
464             $comment = $r[4];
465
466             /* save card to be able to find the winner of the trick later */
467             $play[$pos] = $r[0]; 
468             
469             if($seq==1)
470               {
471                 /* first card in a trick, output some html */
472                 if($trick!=$lasttrick)
473                   {
474                     /* start of an old trick? */
475                     echo "  <li onclick=\"hl('$trickNR');\"><a href=\"#\">Trick $trickNR</a>\n".
476                       "    <div class=\"trick\" id=\"trick".$trickNR."\">\n".
477                       "      <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
478                   }
479                 else if($trick==$lasttrick)
480                   {
481                     /* start of a last trick? */
482                     echo "  <li onclick=\"hl('$trickNR');\"><a href=\"#\">Current Trick</a>\n".
483                       "    <div class=\"trick\" id=\"trick".$trickNR."\">\n".
484                       "      <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
485                   };
486                 
487                 /* remember first card, so that we are able to check, what cards can be played */
488                 $firstcard = $r[0];
489               };
490             
491             /* display card */
492             echo "      <div class=\"card".($pos-1)."\">\n";
493             
494             /* display comments */
495             if($comment!="")
496               echo "        <span class=\"comment\">".$comment."</span>\n";
497             
498             echo "        ";
499             display_card($r[0]);
500             
501             echo "      </div>\n"; /* end div card */
502             
503             /* end of trick? */
504             if($seq==4)
505               {
506                 $trickNR++;
507                 echo "    </div>\n  </li>\n";  /* end div table, end li table */
508               }
509           }
510
511         if($seq!=4 && $trickNR>1) 
512           echo "    </div>\n  </li>\n";  /* end div table, end li table */
513         
514         echo "</ul>\n";
515         
516         /* whos turn is it? */
517         if($seq==4)
518           {
519              $winner = get_winner($play,$gametype); /* returns the position */
520              $next = $winner;
521              $firstcard = ""; /* new trick, no first card */
522           }
523         else
524           {
525             $next = $pos+1;
526             if($next==5) $next=1;
527           }
528         
529         /* my turn?, display cards as links, ask for comments*/
530         if(DB_get_pos_by_hash($me) == $next)
531           $myturn = 1;
532         else
533           $myturn = 0;
534
535         /* do we want to play a card? */
536         if(myisset("card") && $myturn)
537           {
538             $card   = $_REQUEST["card"];
539             $handid = DB_get_handid_by_hash($me); 
540             
541             /* check if we have card and that we haven't played it yet*/
542             /* set played in hand_card to true where hand_id and card_id*/
543             $result = mysql_query("SELECT id FROM Hand_Card WHERE played='false' and ".
544                                   "hand_id='$handid' AND card_id=".DB_quote_smart($card));
545             $r = mysql_fetch_array($result,MYSQL_NUM);
546             $handcardid = $r[0];
547             
548             if($handcardid)
549               {
550                 /* mark card as played */
551                 mysql_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
552                             DB_quote_smart($card));
553                 
554                 /* get trick id or start new trick */
555                 $a = DB_get_current_trickid($gameid);
556                 $trickid  = $a[0];
557                 $sequence = $a[1];
558                 
559                 $playid = DB_play_card($trickid,$handcardid,$sequence);
560
561                 /* check for coment */
562                 if(myisset("comment"))
563                   {
564                     DB_insert_comment($_REQUEST["comment"],$playid,$myid);
565                   };  
566
567                 /* display played card */
568                 echo "<div class=\"card\">";
569                 echo " you played  <br />";
570                 display_card($card);
571                 echo "</div>\n";
572
573                 /*check if we still have cards left, else set status to gameover */
574                 if(sizeof(DB_get_hand($me))==0)
575                   {
576                     DB_set_hand_status_by_hash($me,'gameover');
577                     $mystatus='gameover';
578                   }
579                 
580                 /* if all players are done, set game status to game over, 
581                  * get the points of the last trick and send out an email 
582                  * to all players
583                  */
584                 $userids = DB_get_all_userid_by_gameid($gameid);
585
586                 $done=1;
587                 foreach($userids as $user)
588                   if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
589                     $done=0;
590
591                 if($done)
592                   {
593                     DB_set_game_status_by_gameid($gameid,"gameover");
594                     /* get score for last trick 
595                      * all other tricks are handled a few lines further down*/
596                     $play   = DB_get_cards_by_trick($trickid);
597                     $winner = get_winner($play,$gametype); /* returns the position */
598                     /* get points of last trick and save it */
599                     $points = 0;
600                     foreach($play as $card)
601                       $points = $points + card_value($card);
602                     $winnerid = DB_get_handid_by_gameid_and_position($gameid,$winner);
603                     if($winnerid>0)
604                       mysql_query("INSERT INTO Score VALUES (NULL, '$gameid', '$winnerid', '$points')");
605                     else
606                       echo "ERROR during scoring";
607
608                     /* email all players */
609                     $result = mysql_query("SELECT fullname, SUM(score) FROM Score".
610                                           " LEFT JOIN Hand ON Hand.id=hand_id".
611                                           " LEFT JOIN User ON Hand.user_id=User.id".
612                                           " WHERE Hand.game_id=$gameid".
613                                           " GROUP BY fullname" );
614                     $message = "The game is over. Thanks for playing :)\n";
615                     while( $r = mysql_fetch_array($result,MYSQL_NUM))
616                       $message .= " FINAL SCORE: ".$r[0]." ".$r[1]."\n";
617                     $message .= "\nIf your not in the list above your score is zero...\n";
618                     foreach($userids as $user)
619                       {
620                         $To = DB_get_email_by_userid($user);
621                         mymail($To,$EmailName."game over",$message);
622                       }
623                   }
624                 
625
626                 /* email next player */
627                 if(DB_get_game_status_by_gameid($gameid)=='play')
628                   {
629                     if($sequence==4)
630                       {
631                         $play   = DB_get_cards_by_trick($trickid);
632                         $winner = get_winner($play,$gametype); /* returns the position */
633
634                         /* get points of last trick and save it, last trick is handled 
635                          * a few lines further up  */
636                         $points = 0;
637                         foreach($play as $card)
638                           $points = $points + card_value($card);
639
640                         $winnerid = DB_get_handid_by_gameid_and_position($gameid,$winner);
641                         if($winnerid>0)
642                           mysql_query("INSERT INTO Score VALUES (NULL, '$gameid', '$winnerid', '$points')");
643                         else
644                           echo "ERROR during scoring";
645                         
646                         if($debug)
647                           echo "DEBUG: $winner got $points <br />";
648                         
649                         /* who is the next player? */
650                         $next = $winner;
651                       }
652                     else
653                       {
654                         $next = DB_get_pos_by_hash($me)+1;
655                       }
656                     if($next==5) $next=1;
657
658                     /* email next player */
659                     $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
660                     $email     = DB_get_email_by_hash($next_hash);
661
662                     $message = "It's your turn  now.\n".
663                       "Use this link to play a card: ".$host."?me=".$next_hash."\n\n" ;
664                     mymail($email,$EmailName."a card has been played",$message);
665                     
666                     if($debug)
667                       echo "DEBUG:<a href=\"index.php?me=".DB_get_hash_from_game_and_pos($gameid,$next).
668                         "\"> next player </a> <br />\n";
669
670                   }
671               }
672             else
673               {
674                 echo "can't find that card?! <br />\n";
675               }
676           }
677         else if(myisset("card") && !$myturn )
678           {
679             echo "please wait until it's your turn! <br />\n";
680           }
681         
682         $mycards = DB_get_hand($me);
683         $mycards = mysort($mycards,$gametype);
684         echo "<div class=\"mycards\">\n";
685         
686         if($myturn && !myisset("card"))
687           {
688             echo "Hello ".$myname.", it's your turn!  <br />\n";
689             echo "Your cards are: <br />\n";
690             echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
691
692             /* do we have to follow suite? */
693             $followsuit = 0;
694             if(have_suit($mycards,$firstcard))
695               $followsuit = 1;
696
697             foreach($mycards as $card) 
698               {
699                 if($followsuit && !same_type($card,$firstcard))
700                   display_card($card);
701                 else
702                   display_link_card($card);
703               }
704
705             echo "<br />\nA short comments:<input name=\"comment\" type=\"text\" size=\"30\" maxlength=\"50\" />\n";
706             echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />\n";
707             echo "<input type=\"submit\" value=\"move\" />\n";
708             echo "</form>\n";
709          }
710         else if($mystatus=='play')
711           {
712             echo "Your cards are: <br />\n";
713             foreach($mycards as $card) 
714               display_card($card);
715           }
716         echo "</div>\n";
717
718         /* check if we need to set status to 'gameover' is done during playing of the card */
719         if($mystatus=='play')
720           break;
721         /* the following happens only when the gamestatus is 'gameover' */
722         /* check if game is over, display results */
723         if(DB_get_game_status_by_gameid($gameid)=='play')
724           {
725             echo "the game is over for you.. other people still need to play though";
726           }
727         else
728           {
729             echo "the game is over now...<br />\n";
730             
731             $result = mysql_query("SELECT fullname, SUM(score) FROM Score".
732                                   " LEFT JOIN Hand ON Hand.id=hand_id".
733                                   " LEFT JOIN User ON Hand.user_id=User.id".
734                                   " WHERE Hand.game_id=$gameid".
735                                   " GROUP BY fullname" );
736             while( $r = mysql_fetch_array($result,MYSQL_NUM))
737               echo " FINAL SCORE: ".$r[0]." ".$r[1]."<br />";
738
739             /* suggest a new game with the same people in it, just rotated once */
740             $names = DB_get_all_names_by_gameid($gameid);
741             output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
742           }
743         break;
744       default:
745         echo "error in testing the status";
746       }
747     exit();
748   } 
749 /* user status page */ 
750     else if(myisset("email","password"))
751   {
752     /* test id and password, should really be done in one step */
753     $email     = $_REQUEST["email"];
754     $password  = $_REQUEST["password"];
755
756     if(strlen($password)!=32)
757       $password = md5($password);
758
759     $ok=1;
760     $uid = DB_get_userid_by_email_and_password($email,$password);
761     if(!$uid)
762       $ok=0;
763
764     if($ok)
765       {
766         $time = DB_get_user_timestamp($uid);
767         $unixtime =strtotime($time);
768         
769         $offset = DB_get_user_timezone($uid);
770         $zone = return_timezone($offset);
771         date_default_timezone_set($zone);
772
773         echo "last login: ".date("r",$unixtime)."<br />";
774
775         DB_update_user_timestamp($uid);
776
777         echo "<p>these are the games you are playing in:<br />\n";
778         $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.mod_date from Hand".
779                               " LEFT JOIN Game On Hand.game_id=Game.id".
780                               " WHERE Hand.user_id='$uid' AND Game.status<>'gameover'" );
781         while( $r = mysql_fetch_array($result,MYSQL_NUM))
782           {
783             echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a>";
784             if(time()-strtotime($r[2]) > 60*60*24*30)
785                echo " The game has been running for over a month. Do you want to cancel it? <a href=\"$host?cancle=1&me=".$r[0]."\">yes</a>";
786             echo "<br />";
787           }
788         echo "</p>\n";
789
790
791         echo "<p>and these are your games that are already done:<br />\n";
792         $result = mysql_query("SELECT hash,game_id from Hand WHERE user_id='$uid' AND status='gameover'" );
793         while( $r = mysql_fetch_array($result,MYSQL_NUM))
794           echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a><br />";
795         echo "</p>\n";
796
797         $names = DB_get_all_names();
798         echo "<p>registered players:<br />\n";
799         foreach ($names as $name)
800           echo "$name <br />\n";
801         echo "</p>\n";
802
803         echo "<p>Want to start a new game? remember 4 names from the list above and visit ".
804           "<a href=\"".$host."?new\">this page.</a></p>";
805       }
806     else
807       {
808         echo "sorry email and password don't match <br />";
809       }
810     exit();
811   }
812 /* page for registration */
813 else if(myisset("register") )
814   {
815     output_register();
816   }
817 /* new user wants to register */
818  else if(myisset("Rfullname","Remail","Rpassword","Rtimezone") )
819   {
820         $ok=1;
821         if(DB_get_userid_by_name($_REQUEST["Rfullname"]))
822           {
823             echo "please chose another name<br />";
824             $ok=0;
825           }
826         if(DB_get_userid_by_email($_REQUEST["Remail"]))
827           {
828             echo "this email address is already used ?!<br />";
829             $ok=0;
830           }
831         if($ok)
832           {
833             $r=mysql_query("INSERT INTO User VALUES(NULL,".DB_quote_smart($_REQUEST["Rfullname"]).
834                       ",".DB_quote_smart($_REQUEST["Remail"]).
835                       ",".DB_quote_smart(md5($_REQUEST["Rpassword"])).
836                       ",".DB_quote_smart($_REQUEST["Rtimezone"]).",NULL)"); 
837             
838             if($r)
839               echo " added you to the database";
840             else
841               echo " something went wrong";
842           }
843   }
844 /* default login page */
845 else
846   { 
847     output_home_page();
848   }
849
850 output_footer();
851
852 DB_close();
853
854 /*
855  *Local Variables: 
856  *mode: php
857  *mode: hs-minor
858  *End:
859  */
860 ?>
861
862