aec7c7d1ef7f27808183fab71876b442a927bf79
[e-DoKo.git] / index.php
1 <?php
2 error_reporting(E_ALL);
3
4 include_once("config.php");      
5 include_once("output.php");      /* html output only */
6 include_once("db.php");          /* database only */
7 include_once("functions.php");   /* the rest */
8
9 /* check if some variables are set in the config file, else set defaults */
10 if(!isset($EmailName))
11      $EmailName="[DoKo] ";
12
13 /* in case work has to be done on the database or other section we can
14  * shut down the server and tell people to come back later 
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 if(DB_open()<0)
25   {
26     output_header();
27     echo "Database error, can't connect...";
28     output_footer(); 
29     exit(); 
30   }
31
32 /* done major error checking, output header of HTML page */
33 output_header();
34
35 /* check if we want to start a new game */
36 if(myisset("new"))
37   {
38     $names = DB_get_all_names();
39     output_form_for_new_game($names);
40   }
41 /*check if everything is ready to set up a new game */
42  else if( myisset("PlayerA", "PlayerB","PlayerC","PlayerD","dullen","schweinchen" ))
43   {
44     $PlayerA = $_REQUEST["PlayerA"];
45     $PlayerB = $_REQUEST["PlayerB"];
46     $PlayerC = $_REQUEST["PlayerC"];
47     $PlayerD = $_REQUEST["PlayerD"];
48
49     $dullen      = $_REQUEST["dullen"];
50     $schweinchen = $_REQUEST["schweinchen"];
51     
52     $EmailA  = DB_get_email_by_name($PlayerA);
53     $EmailB  = DB_get_email_by_name($PlayerB);
54     $EmailC  = DB_get_email_by_name($PlayerC);
55     $EmailD  = DB_get_email_by_name($PlayerD);
56     
57     if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
58       {
59         echo "couldn't find one of the names, please start a new game";
60         output_footer();
61         DB_close();
62         exit();
63       }
64     
65     $useridA  = DB_get_userid_by_name($PlayerA);
66     $useridB  = DB_get_userid_by_name($PlayerB);
67     $useridC  = DB_get_userid_by_name($PlayerC);
68     $useridD  = DB_get_userid_by_name($PlayerD);
69     
70     /* create random numbers */
71     $randomNR       = create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD);
72     $randomNRstring = join(":",$randomNR);
73     
74     /* get ruleset information or create new one */
75     $ruleset = DB_get_ruleset($dullen,$schweinchen);
76     if($ruleset <0) 
77       {
78         echo "Error defining ruleset: $ruleset";
79         output_footer();
80         DB_close();
81         exit();
82       };
83     
84     /* create game */
85     $followup = NULL;
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         if($session)
93           mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1','1','pre',".
94                       "'$ruleset','$session' ,NULL)");
95         else
96           {
97             /* get max session */
98             $max = DB_get_max_session();
99             $max++;
100             mysql_query("UPDATE Game SET session='".$max."' WHERE id=".DB_quote_smart($followup));
101             mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1','1','pre',".
102                         "'$ruleset','$max' ,NULL)");
103           }
104       }
105     else
106       mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1','1','pre', ".
107                   "'$ruleset',NULL ,NULL)");
108     $game_id = mysql_insert_id();
109     
110     /* create hash */
111     $TIME  = (string) time(); /* to avoid collisions */
112     $hashA = md5("AGameOfDoko".$game_id.$PlayerA.$EmailA.$TIME);
113     $hashB = md5("AGameOfDoko".$game_id.$PlayerB.$EmailB.$TIME);
114     $hashC = md5("AGameOfDoko".$game_id.$PlayerC.$EmailC.$TIME);
115     $hashD = md5("AGameOfDoko".$game_id.$PlayerD.$EmailD.$TIME);
116     
117     /* create hands */
118     mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridA).
119                 ", ".DB_quote_smart($hashA).", 'start','1',NULL,NULL,NULL,NULL)");
120     $hand_idA = mysql_insert_id();                                                             
121     mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridB).
122                 ", ".DB_quote_smart($hashB).", 'start','2',NULL,NULL,NULL,NULL)");
123     $hand_idB = mysql_insert_id();                                                             
124     mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridC).
125                 ", ".DB_quote_smart($hashC).", 'start','3',NULL,NULL,NULL,NULL)");
126     $hand_idC = mysql_insert_id();                                                             
127     mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridD).
128                 ", ".DB_quote_smart($hashD).", 'start','4',NULL,NULL,NULL,NULL)");
129     $hand_idD = mysql_insert_id();
130     
131     /* save cards */
132     for($i=0;$i<12;$i++)
133       mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idA', '".$randomNR[$i]."', 'false')");
134     for($i=12;$i<24;$i++)
135       mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idB', '".$randomNR[$i]."', 'false')");
136     for($i=24;$i<36;$i++)
137       mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idC', '".$randomNR[$i]."', 'false')");
138     for($i=36;$i<48;$i++)
139       mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idD', '".$randomNR[$i]."', 'false')");
140     
141     /* send out email, TODO: check for error with email */
142     $message = "\n".
143       "you are invited to play a game of DoKo (that is to debug the program ;).\n".
144       "Place comments and bug reports here:\n".
145       "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
146       "The whole round would consist of the following players:\n".
147       "$PlayerA\n".
148       "$PlayerB\n".
149       "$PlayerC\n".
150       "$PlayerD\n\n".
151       "If you want to join this game, please follow this link:\n\n".
152       "".$host."?me=";
153     
154     mymail($EmailA,"You are invited to a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
155     mymail($EmailB,"You are invited to a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
156     mymail($EmailC,"You are invited to a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
157     mymail($EmailD,"You are invited to a game of DoKo","Hello $PlayerD,\n".$message.$hashD);
158     
159     echo "You started a new game. The emails have been sent out!";    
160   }    /* end set up a new game */
161 /* cancle a game, if nothing has happend in the last N minutes */
162 else if(myisset("cancle","me"))
163   {
164     $me = $_REQUEST["me"];
165     
166     /* test for valid ID */
167     $myid = DB_get_userid_by_hash($me);
168     if(!$myid)
169       {
170         echo "Can't find you in the database, please check the url.<br />\n";
171         echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
172         output_footer();
173         DB_close();
174         exit();
175       }
176     
177     DB_update_user_timestamp($myid);
178     
179     /* get some information from the DB */
180     $gameid   = DB_get_gameid_by_hash($me);
181     $myname   = DB_get_name_by_hash($me);
182     
183     /* check if game really is old enough */
184     $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
185     $r = mysql_fetch_array($result,MYSQL_NUM);
186     if(time()-strtotime($r[0]) > 60*60*24*30) /* = 1 month */
187       {
188         $message = "Hello, \n\n".
189           "Game $gameid has been cancled since nothing happend for a while and $myname requested it.\n";
190         
191         $userids = DB_get_all_userid_by_gameid($gameid);
192         foreach($userids as $user)
193           {
194             $To = DB_get_email_by_userid($user);
195             mymail($To,$EmailName."game $gameid cancled (timed out)",$message);
196           }
197         
198         /* delete everything from the dB */
199         DB_cancel_game($me);
200         
201         echo "<p style=\"background-color:red\";>Game $gameid has been cancled.<br /><br /></p>";
202       }
203     else
204       echo "<p>You need to wait longer before you can cancle a game...</p>\n";
205   }
206 /* handle request from one specific player for one game,
207  * (the hash is set on a per game base) */
208 else if(myisset("me"))
209   {
210     $me = $_REQUEST["me"];
211     
212     /* test for valid ID */
213     $myid = DB_get_userid_by_hash($me);
214     if(!$myid)
215       {
216         echo "Can't find you in the database, please check the url.<br />\n";
217         echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
218         output_footer();
219         DB_close();
220         exit();
221       }
222
223     /* the user had done something, update the timestamp */
224     DB_update_user_timestamp($myid);
225     
226     /* get some information from the DB */
227     $gameid   = DB_get_gameid_by_hash($me);
228     $myname   = DB_get_name_by_hash($me);
229     $mystatus = DB_get_status_by_hash($me);
230     $mypos    = DB_get_pos_by_hash($me);
231     $myhand   = DB_get_handid_by_hash($me);
232     $session  = DB_get_session_by_gameid($gameid);
233
234     /* get prefs and save them */
235     DB_get_PREF($myid);
236     /* end set pref */
237       
238       
239     /* get rule set for this game */
240     $result = mysql_query("SELECT * FROM Rulesets".
241                           " LEFT JOIN Game ON Game.ruleset=Rulesets.id ".
242                           " WHERE Game.id='$gameid'" );
243     $r      = mysql_fetch_array($result,MYSQL_NUM);
244
245     $RULES["dullen"]      = $r[2];
246     $RULES["schweinchen"] = $r[3];
247     $RULES["call"]        = $r[4];
248
249
250     /* get some infos about the game */
251     $gametype   = DB_get_gametype_by_gameid($gameid);
252     $gamestatus = DB_get_game_status_by_gameid($gameid);
253     $GT         = $gametype;
254     if($gametype=="solo")
255       {
256         $gametype = DB_get_solo_by_gameid($gameid);
257         $GT  = $gametype." ".$GT;
258       }
259
260     /* display rule set for this game */
261     echo "<div class=\"ruleset\">\n";
262
263     if($gamestatus != 'pre')
264       echo " Gametype: $GT <br />\n";
265     
266     echo "Rules: <br />\n";
267     echo "10ofhearts : ".$r[2]."<br />\n";
268     echo "schweinchen: ".$r[3]."<br />\n";
269     echo "call:        ".$r[4]."<br />\n";
270     echo "</div>\n";
271
272     /* output extra division in case this game is part of a session */
273     if($session)
274       {
275         echo "<div class=\"session\">\n".
276           "This game is part of session $session: \n";
277         $hashes = DB_get_hashes_by_session($session,$myid);
278         $i = 1;
279         foreach($hashes as $hash)
280           {
281             if($hash == $me)
282               echo "$i ";
283             else 
284               echo "<a href=\"".$host."?me=".$hash."\">$i</a> ";
285             $i++;
286           }
287         echo "</div>\n";
288       }
289
290     
291     /* does anyone have both foxes */
292     $GAME["schweinchen"]=0; 
293     for($i=1;$i<5;$i++)
294       {
295         $hash  = DB_get_hash_from_game_and_pos($gameid,$i);
296         $cards = DB_get_all_hand($hash);
297         if( in_array("19",$cards) && in_array("20",$cards) )
298           {
299             $GAME["schweinchen"]=1;
300             $GAME["schweinchen-who"]=$hash;
301           }
302       };
303
304     /* mystatus gets the player through the different stages of a game.
305      * start:    yes/no
306      * init:     check values from start,
307      *           check for sickness
308      * check:    check for return values from init
309      * poverty:  handle poverty, wait here until all player have reached this state
310      *           display sickness and move on to game
311      * play:     game in progress
312      * gameover: are we revisiting a game
313      */
314     switch($mystatus)
315       {
316       case 'start':
317         check_want_to_play($me);
318         /* move on to the next stage*/
319         DB_set_hand_status_by_hash($me,'init');
320         break;
321       case 'init':
322         /* first check if everything went ok  in the last step
323          * if not, send user back, if yes, check what he did
324          */
325         if( !myisset("in") )
326           {
327             echo "<p> you need to answer the <a href=\"$host?me=$me\">question</a>.</p>";
328             DB_set_hand_status_by_hash($me,'start');
329           }
330         else
331           {
332             if($_REQUEST["in"] == "no")
333               {
334                 /* cancle the game */
335                 $message = "Hello, \n\n".
336                   "the game has been canceled due to the request of one of the players.\n";
337                 
338                 $userids = DB_get_all_userid_by_gameid($gameid);
339                 foreach($userids as $user)
340                   {
341                     $To = DB_get_email_by_userid($user);
342                     mymail($To,$EmailName."game $gameid canceled",$message);
343                   }
344                 
345                 /* delete everything from the dB */
346                 DB_cancel_game($me);
347               }
348             else
349               {
350                 echo "thanks for joining the game...";
351                 
352                 $mycards = DB_get_hand($me);
353                 sort($mycards);
354                 echo "<p class=\"mycards\" style=\"margin-top:8em;\">your cards are: <br />\n";
355                 foreach($mycards as $card) 
356                   display_card($card,$PREF["cardset"]);
357                 echo "</p>\n";   
358                 
359                 output_check_for_sickness($me,$mycards);
360                 
361                 /* move on to the next stage*/
362                 DB_set_hand_status_by_hash($me,'check');
363               }
364           }
365         break;
366
367     case 'check':
368       /* ok, user is in the game, saw his cards and selected his vorbehalt
369        * so first we check what he selected
370        */
371       echo "Processing what you selected in the last step...<br />";
372
373       if(!myisset("solo","wedding","poverty","nines") )
374         {
375           /* all these variables have a pre-selected default,
376            * so we should never get here,
377            * unless a user tries to cheat ;) */
378           echo "something went wrong...please contact the admin.";
379         }
380       else
381         {
382           /* check if this sickness needs to be handled first */
383           $gametype    = DB_get_gametype_by_gameid($gameid);
384           $startplayer = DB_get_startplayer_by_gameid($gameid);
385           
386           if( $_REQUEST["solo"]!="No")
387             {
388               /* user wants to play a solo */
389
390               /* store the info in the user's hand info */
391               DB_set_solo_by_hash($me,$_REQUEST["solo"]);
392               DB_set_sickness_by_hash($me,"solo");
393
394               echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
395               
396               if($gametype == "solo" && $startplayer<$mypos)
397                 {}/* do nothing, since someone else already is playing solo */
398               else
399                 {
400                   /* this solo comes first 
401                    * store info in game table
402                    */
403                   DB_set_gametype_by_gameid($gameid,"solo");
404                   DB_set_startplayer_by_gameid($gameid,$mypos);
405                   DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
406                 };
407             }
408           else if($_REQUEST["wedding"] == "yes")
409             {
410               /* TODO: add silent solo somewhere*/
411               echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
412               DB_set_sickness_by_hash($me,"wedding");
413             }
414           else if($_REQUEST["poverty"] == "yes")
415             {
416               echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
417               DB_set_sickness_by_hash($me,"poverty");
418             }
419           else if($_REQUEST["nines"] == "yes")
420             {
421               echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
422                 " is playing solo, this game will be canceled.<br />\n";
423               DB_set_sickness_by_hash($me,"nines");
424             }
425           
426           echo " Ok, done with checking, please go to the <a href=\"$host?me=$me\">next step of the setup</a>.<br />";
427           
428           /* move on to the next stage*/
429           DB_set_hand_status_by_hash($me,'poverty');
430           
431           /* check if everyone has reached this stage, send out email */
432           $userids = DB_get_all_userid_by_gameid($gameid);
433           $ok = 1;
434           foreach($userids as $user)
435             {
436               $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
437               if($userstat!='poverty' && $userstat!='play')
438                 $ok = 0;
439             };
440           if($ok)
441             foreach($userids as $user)
442               {
443                 $To       = DB_get_email_by_userid($user);
444                 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
445                 if($userhash != $me)
446                   {
447                     $message = "Everyone finish the questionary in game $gameid, ".
448                       "please visit this link now to continue: \n".
449                       " ".$host."?me=".$userhash."\n\n" ;
450                     mymail($To,$EmailName." finished setup in game $gameid",$message);
451                   }
452               };
453         };
454
455       break;
456
457     case 'poverty':
458       /* here we need to check if there is a solo or some other form of sickness.
459        * If so, which one is the most important one
460        * set that one in the Game table
461        * tell people about it.
462        */
463       echo "<br /> Checking if someone else selected solo, nines or wedding or poverty.<br />";
464       
465       /* check if everyone has reached this stage */
466       $userids = DB_get_all_userid_by_gameid($gameid);
467       $ok = 1;
468       foreach($userids as $user)
469         {
470           $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
471           if($userstat!='poverty' && $userstat!='play')
472             $ok = 0;
473         };
474
475       if(!$ok)
476         {
477           echo "This step can only be handled after everyone finished the last step. ".
478                "Seems like this is not the case, so you need to wait a bit... ".
479                "you will get an email once that is the case, please use the link in ".
480                "that email to continue the game.<br />";
481         }
482       else
483         {
484           echo "Everyone has finished checking their cards, let's see what they said...<br />";
485
486           /* check what kind of game we are playing,  in case there are any solos this already 
487            *will have the correct information in it */
488           $gametype    = DB_get_gametype_by_gameid($gameid);
489           $startplayer = DB_get_startplayer_by_gameid($gameid);
490
491           /* check for different sickness and just output a general info */
492           $nines   = 0;
493           $poverty = 0;
494           $wedding = 0;
495           $solo    = 0;
496           foreach($userids as $user)
497             {
498               $name     = DB_get_name_by_userid($user);
499               $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
500               if($usersick == 'nines')
501                 {
502                   $nines = $user;
503                   echo "$name has a Vorbehalt. <br />";
504                   break;
505                 }
506               else if($usersick == 'poverty')
507                 {
508                   $poverty++;
509                   echo "$name has a Vorbehalt. <br />";
510                 }
511               else if($usersick == 'wedding')
512                 {
513                   $wedding=$user;
514                   echo "$name has a Vorbehalt. <br />"  ;
515                 }
516               else if($usersick == 'solo')
517                 {
518                   $solo++;
519                   echo "$name has a Vorbehalt. <br />"  ;
520                 }
521             }
522
523           /* now check which sickness comes first and set the gametype to it */
524
525           if($gametype == "solo")
526             {
527               /* do nothing */
528             }
529           else if($nines)
530             {
531               /* cancle game */
532               /* TODO: should we keep statistics of this? */
533               $message = "Hello, \n\n".
534                 "the game has been canceled because ".DB_get_name_by_userid($nines).
535                 " has five or more nines and nobody is playing solo.\n";
536               
537               /* TODO: add info about redeal in case this is a game of a series */
538               
539               $userids = DB_get_all_userid_by_gameid($gameid);
540               foreach($userids as $user)
541                 {
542                   $To = DB_get_email_by_userid($user);
543                   mymail($To,$EmailName."game $gameid canceled",$message);
544                 }
545               
546               /* delete everything from the dB */
547               DB_cancel_game($me);
548               
549               echo "The game has been canceled because ".DB_get_name_by_userid($nines).
550                 " has five or more nines and nobody is playing solo.\n";
551               output_footer();
552               DB_close();
553               exit();
554             }
555           else if($poverty==1) /* one person has poverty */
556             {
557               DB_set_gametype_by_gameid($gameid,"poverty");
558               $gametype = "poverty";
559               $who      = DB_get_sickness_by_gameid($gameid);
560               if(!$who)
561                 {
562                   $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
563                   if($firstsick == "poverty")
564                     DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
565                   else
566                     DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
567                 }
568             }
569           else if($poverty==2) /* two people have poverty */
570             {
571               DB_set_gametype_by_gameid($gameid,"dpoverty");
572               $gametype = "dpoverty";
573               $who      = DB_get_sickness_by_gameid($gameid);
574               if(!$who)
575                 {
576                   $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
577                   if($firstsick == "poverty")
578                     {
579                       $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
580                       if($secondsick == "poverty")
581                         DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
582                       else
583                         DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
584                     }
585                   else
586                     DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
587                 }
588             }
589           else if($wedding> 0)
590             {
591               DB_set_gametype_by_gameid($gameid,"wedding");
592               DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
593               $gametype = "wedding";
594             };
595
596           echo "<br />\n";
597
598           /* now the gametype is set correctly (shouldn't matter that this is calculated for every user)
599            * output what kind of game we have */
600           
601           $poverty = 0;
602           foreach($userids as $user)
603             {
604               /* userids are sorted by position... 
605                * so output whatever the firstone has, then whatever the next one has
606                * stop when the sickness is the same as the gametype 
607                */
608               
609               $name     = DB_get_name_by_userid($user);
610               $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
611
612               if($usersick)
613                 echo "$name has $usersick. <br />"; /*TODO: perhaps save this in a string and store in Game? */
614
615               if($usersick=="poverty")
616                 $poverty++;
617               if($usersick == "wedding" && $gametype =="wedding")
618                 break;
619               if($usersick == "poverty" && $gametype =="poverty")
620                 break;
621               if($usersick == "poverty" && $gametype =="dpoverty" && $poverty==2)
622                 break;
623               if($usersick == "solo" && $gametype =="solo")
624                 break;
625             };
626
627           /* output Schweinchen in case the rules need it */
628           if( $gametype != "solo")
629             if($GAME["schweinchen"] && $RULES["schweinchen"]=="both" )
630               echo DB_get_name_by_hash($GAME["schweinchen-who"])." has Schweinchen. <br />";
631           
632           echo "<br />\n";
633           
634           /* finished the setup, set re/contra parties if possible, go to next stage unless there is a case of poverty*/
635           switch($gametype)
636             {
637             case "solo":
638               /* are we the solo player? set us to re, else set us to contra */
639               $pos = DB_get_pos_by_hash($me);
640               if($pos == $startplayer)
641                 DB_set_party_by_hash($me,"re");
642               else
643                 DB_set_party_by_hash($me,"contra");
644               DB_set_hand_status_by_hash($me,'play');
645               break;
646
647             case "wedding":
648               /* set person with the wedding to re, do the rest during the game */
649               $usersick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
650               if($usersick == "wedding")
651                 DB_set_party_by_hash($me,"re");
652               else
653                 DB_set_party_by_hash($me,"contra");
654               
655               echo "Whoever will make the first trick will be on the re team. <br />\n";
656               echo " Ok, the game can start now, please finish <a href=\"$host?me=$me\">the setup</a>.<br />";       
657               DB_set_hand_status_by_hash($me,'play');
658               break;
659
660             case "normal":
661               $hand = DB_get_all_hand($me);
662               
663               if(in_array('3',$hand)||in_array('4',$hand))
664                 DB_set_party_by_hash($me,"re");
665               else
666                 DB_set_party_by_hash($me,"contra");
667               DB_set_hand_status_by_hash($me,'play');
668               break;
669             case "poverty":
670             case "dpoverty":
671               /* check if poverty resolved (e.g. DB.Game who set to NULL)
672                *   yes? =>trump was taken, start game; break; 
673                */
674               $who = DB_get_sickness_by_gameid($gameid);
675               if($who<0)
676                 { /* trump has been taken */
677                   DB_set_hand_status_by_hash($me,'play');
678                   break;
679                 };
680               
681               if($who>9) /*= two people still have trump on the table*/
682                 $add = 10;
683               else
684                 $add = 1;
685
686               /* check if we are being asked now
687                *    no? display wait message, e.g. player X is asked at the moment 
688                */
689               $usersick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
690               if(myisset("trump") && $_REQUEST["trump"]=="no" && ($who==$mypos || $who==$mypos*10))
691                 {
692                   /* user doesn't want to take trump */
693                   /* set next player who needs to be asked */
694                   $firstsick  = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
695                   $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
696                   
697                   if($firstsick=="poverty")
698                     {
699                       if($secondsick=="poverty")
700                         DB_set_sickness_by_gameid($gameid,$who+$add*3);
701                       else
702                         DB_set_sickness_by_gameid($gameid,$who+$add*2);
703                     }
704                   else
705                     DB_set_sickness_by_gameid($gameid,$who+$add);
706
707                   /* email next player */
708                   $who = DB_get_sickness_by_gameid($gameid);
709                   if($who>9) $who = $who/10;
710                   
711                   if($who<=4)
712                     {
713                       $To       = DB_get_email_by_pos_and_gameid($who,$gameid);
714                       $userhash = DB_get_hash_from_game_and_pos($gameid,$who);
715                       DB_set_player_by_gameid($gameid,$who);
716
717                       $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
718                         " ".$host."?me=".$userhash."\n\n" ;
719                       mymail($To,$EmailName." poverty (game $gameid)",$message);
720                     }
721
722                   /* this user is done */
723                   DB_set_hand_status_by_hash($me,'play');
724                   break;                
725                 }
726               else if(myisset("trump") && !myisset("exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
727                 {
728                   /* user wants to take trump */
729                   $trump = $_REQUEST["trump"];
730
731                   /* get hand id for user $trump */
732                   $userhand = DB_get_handid_by_gameid_and_userid($gameid,$trump);
733                   /* copy trump from player A to B */
734                   $result = mysql_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
735                   
736                   /* add hidden button with trump in it to get to the next point */
737                   echo "<form action=\"index.php\" method=\"post\">\n";
738                   echo "  <input type=\"hidden\" name=\"exchange\" value=\"-1\" />\n";
739                   echo "  <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
740                   echo "  <input type=\"hidden\" name=\"me\" value=\"".$me."\" />\n";
741                   echo "  <input type=\"submit\" class=\"submitbutton\" value=\"select cards to give back\" />\n";
742                   echo "</form>\n";
743                 }
744               else if(myisset("trump","exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
745                 {
746                   $trump    = $_REQUEST["trump"];
747                   $exchange = $_REQUEST["exchange"];
748                   $userhand = DB_get_handid_by_gameid_and_userid($gameid,$trump);
749
750                   /* if exchange is set to a value>0, exchange that card back to user $trump */
751                   if($exchange >0)
752                     {
753                       $result = mysql_query("UPDATE Hand_Card SET hand_id='$userhand'".
754                                             " WHERE hand_id='$myhand' AND card_id='$exchange'" );
755                     };
756                   
757                   /* if number of cards == 12, set status to play for both users */
758                   $result = mysql_query("SELECT COUNT(*) FROM Hand_Card  WHERE hand_id='$myhand'" );
759                   $r      = mysql_fetch_array($result,MYSQL_NUM);
760                   if(!$r)
761                     {
762                       die("error in poverty");
763                     };
764                   if($r[0]==12)
765                     {
766                       if($gametype=="poverty" || $who<9)
767                         {
768                           DB_set_sickness_by_gameid($gameid,-1); /* done with poverty */                          
769                         }
770                       else /* reduce poverty count by one, that is go to single digits $who */
771                         {
772                           $add = 1;
773                           $who = $who/10;
774
775                           /* whom to ask next */
776                           $firstsick  = DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
777                           $secondsick = DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
778
779                           if($firstsick!="poverty")
780                             DB_set_sickness_by_gameid($gameid,$who+$add);
781                           else
782                             {
783                               if($secondsick!="poverty")
784                                 DB_set_sickness_by_gameid($gameid,$who+$add*2);
785                               else
786                                 DB_set_sickness_by_gameid($gameid,$who+$add*3);
787                             };
788
789                           /* email next player */
790                           $who = DB_get_sickness_by_gameid($gameid);
791                           if($who<=4)
792                             {
793                               $To       = DB_get_email_by_pos_and_gameid($who,$gameid);
794                               $userhash = DB_get_hash_from_game_and_pos($gameid,$who);
795                               DB_set_player_by_gameid($gameid,$who);
796
797                               $message = "Someone has poverty, it's your turn to decide, ".
798                                          "if you want to take the trump. Please visit:".
799                                          " ".$host."?me=".$userhash."\n\n" ;
800                               mymail($To,$EmailName." poverty (game $gameid)",$message);
801                             }
802
803                         }
804                       
805                       /* this user is done */
806                       DB_set_hand_status_by_hash($me,'play');
807                       /* and so is his partner */
808                       $hash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
809                       DB_set_hand_status_by_hash($hash,'play');
810
811                       /* set party to re, unless we had dpoverty, in that case check if we need to set re/contra*/
812                       $re_set = 0;
813                       foreach($userids as $user)
814                         {
815                           $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
816                           $party    = DB_get_party_by_hash($userhash);
817                           if($party=="re")
818                             $re_set = 1;
819                         }
820                       if($re_set)
821                         {
822                           DB_set_party_by_hash($me,"contra");
823                           DB_set_party_by_hash($hash,"contra");
824                         }
825                       else
826                         {
827                           foreach($userids as $user)
828                             {
829                               $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
830                               if($userhash==$hash||$userhash==$me)
831                                 DB_set_party_by_hash($userhash,"re");
832                               else
833                                 DB_set_party_by_hash($userhash,"contra");
834                             }
835                         }
836
837
838                       break;
839                     }
840                   else
841                     {
842                       /* else show all trump, have lowest card pre-selected, have hidden setting for */
843                       echo "you need to get rid of a few cards<br />\n";
844                       
845                       set_gametype($gametype); /* this sets the $CARDS variable */
846                       $mycards = DB_get_hand($me);
847                       $mycards = mysort($mycards,$gametype);
848
849                       echo "<form class=\"exchange\" action=\"index.php\" method=\"post\">\n";
850                       $type="exchange";
851                       foreach($mycards as $card) 
852                         display_link_card($card,$PREF["cardset"],$type);
853                       echo "  <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
854                       echo "  <input type=\"hidden\" name=\"me\" value=\"".$me."\" />\n";
855                       echo "  <input type=\"submit\" class=\"submitbutton\" value=\"select one card to give back\" />\n";
856                       echo "</form>\n";
857                     }
858                 }
859               else if($who == $mypos || $who == $mypos*10)
860                 {
861                   foreach($userids as $user)
862                     {
863                       $name     = DB_get_name_by_userid($user);
864                       $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
865                       
866                       if($usersick=="poverty")
867                         {
868                           $hash    = DB_get_hash_from_gameid_and_userid($gameid,$user);
869                           $cards   = DB_get_hand($hash);
870                           $nrtrump = count_trump($cards);
871                           /* count trump */
872                           if($nrtrump<4)
873                             echo "Player $name has $nrtrump trump. Do you want to take them?".
874                               "<a href=\"index.php?me=$me&amp;trump=$user\">yes</a> <br />";
875                         }
876                     }
877                   echo "<a href=\"index.php?me=$me&amp;trump=no\">No,way I take those trump...</a> <br />";
878
879                   echo "Your cards are: <br />\n";
880                   $mycards = DB_get_hand($me);
881                   sort($mycards);
882                   echo "<p class=\"mycards\" style=\"margin-top:8em;\">your cards are: <br />\n";
883                   foreach($mycards as $card) 
884                     display_card($card,$PREF["cardset"]);
885                   echo "</p>\n";   
886                 }
887               else
888                 {
889                   $mysick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
890                   if($mysick=="poverty")
891                     echo "The others are asked if they want to take your trump, you have to wait (you'll get an email).";
892                   else
893                     echo "it's not your turn yet to decide if you want to take the trump or not.";
894                 }
895
896             };
897         }
898       /* check if no one wanted to take trump, in that case the gamesickness would be set to 5 or 50 */
899       $who = DB_get_sickness_by_gameid($gameid);
900       if($who==5 || $who==50)
901         {
902           $message = "Hello, \n\n".
903             "Game $gameid has been cancled since nobody wanted to take the trump.\n";
904           
905           $userids = DB_get_all_userid_by_gameid($gameid);
906           foreach($userids as $user)
907             {
908               $To = DB_get_email_by_userid($user);
909               mymail($To,$EmailName."game $gameid cancled (poverty not resolved)",$message);
910             }
911           
912           /* delete everything from the dB */
913           DB_cancel_game($me);
914           
915           echo "<p style=\"background-color:red\";>Game $gameid has been cancled.<br /><br /></p>";
916           output_footer();
917           DB_close();
918           exit();
919         }
920
921       /* check if all players are ready to play */
922       $ok = 1;
923       foreach($userids as $user)
924         if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='play')
925           $ok = 0;
926
927       if($ok)
928         {
929           /* only set this after all poverty, etc. are handled*/
930           DB_set_game_status_by_gameid($gameid,'play');
931
932           /* email startplayer */
933           $startplayer = DB_get_startplayer_by_gameid($gameid);
934           $email       = DB_get_email_by_pos_and_gameid($startplayer,$gameid);
935           $hash        = DB_get_hash_from_game_and_pos($gameid,$startplayer);
936           $who         = DB_get_userid_by_email($email);
937           DB_set_player_by_gameid($gameid,$who);
938           
939           if($hash!=$me)
940             {
941               /* email startplayer) */
942               $message = "It's your turn now in game $gameid.\n".
943                 "Use this link to play a card: ".$host."?me=".$hash."\n\n" ;
944               mymail($email,$EmailName."ready, set, go... (game $gameid) ",$message);
945             }
946           else
947             echo " Please, <a href=\"$host?me=$me\">start</a> the game.<br />";  
948         }
949       else
950         echo "\n <br />";        
951
952       break;
953     case 'play':
954     case 'gameover': 
955       /* both entries here,  so that the tricks are visible for both.
956        * in case of 'play' there is a break later that skips the last part
957        */
958       
959       /* figure out what kind of game we are playing, 
960        * set the global variables $CARDS["trump"],$CARDS["diamonds"],$CARDS["hearts"],
961        * $CARDS["clubs"],$CARDS["spades"],$CARDS["foxes"]
962        * accordingly
963        */
964       
965       $gametype = DB_get_gametype_by_gameid($gameid);
966       $GT       = $gametype;
967       if($gametype=="solo")
968         {
969           $gametype = DB_get_solo_by_gameid($gameid);
970           $GT       = $gametype." ".$GT;
971         }
972       else
973         $gametype = "normal";
974       
975       set_gametype($gametype); /* this sets the $CARDS variable */
976       
977       /* get some infos about the game */
978       $gamestatus = DB_get_game_status_by_gameid($gameid);
979       
980       /* display useful things in divs */
981       
982       /* display links to the users status page */
983       $result = mysql_query("SELECT email,password from User WHERE id='$myid'" );
984       $r      = mysql_fetch_array($result,MYSQL_NUM);
985       
986       display_links($r[0],$r[1]);
987       
988       /* end display useful things*/
989       
990       /* has the game started? No, then just wait here...*/
991       if($gamestatus == 'pre')
992         {
993           echo "You finished the setup, but not everyone else finished it... ".
994                "so you need to wait for the others. Just wait for the an email... <br />";
995           break; /* not sure this works... the idea is that you can 
996                   * only  play a card after everyone is ready to play */
997         }
998       
999       /* display the table and the names */
1000       $result = mysql_query("SELECT  User.fullname as name,".
1001                             "        Hand.position as position, ".
1002                             "        User.id, ".
1003                             "        Hand.party as party, ".
1004                             "        Hand.sickness as sickness, ".
1005                             "        Hand.point_call, ".
1006                             "        User.last_login, ".
1007                             "        Hand.hash        ".
1008                             "FROM Hand ".
1009                             "LEFT JOIN User ON User.id=Hand.user_id ".
1010                             "WHERE Hand.game_id='".$gameid."' ".
1011                             "ORDER BY position ASC");
1012       
1013       echo "<div class=\"table\">\n".
1014         "  <img src=\"pics/table.png\" alt=\"table\" />\n";
1015       while($r = mysql_fetch_array($result,MYSQL_NUM))
1016         {
1017           $name  = $r[0];
1018           $pos   = $r[1];
1019           $user  = $r[2];
1020           $party = $r[3];
1021           $sickness  = $r[4];
1022           $call      = $r[5];
1023           $lastlogin = strtotime($r[6]);
1024           $hash      = $r[7];
1025
1026           $offset = DB_get_user_timezone($user);
1027           $zone   = return_timezone($offset);
1028           date_default_timezone_set($zone);
1029
1030           echo " <span class=\"table".($pos-1)."\">\n";
1031           if(!$debug)
1032             echo " $name \n";
1033           else
1034             {
1035               echo "<a href=\"".$host."?me=".$hash."\">$name</a>\n";
1036             }
1037           /* add hints for poverty, wedding, solo, etc */
1038           if($GT=="poverty" && $party=="re")
1039             if($sickness=="poverty")
1040               {
1041                 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1042                 $cards    = DB_get_all_hand($userhash);
1043                 $trumpNR  = count_trump($cards);
1044                 if($trumpNR)
1045                   echo "<img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" />";
1046                 else
1047                   echo "<img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" />";
1048               }
1049             else
1050               echo "<img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" />";
1051
1052           if($GT=="dpoverty")
1053             if($party=="re")
1054               if($sickness=="poverty")
1055                 {
1056                 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1057                 $cards    = DB_get_all_hand($userhash);
1058                 $trumpNR  = count_trump($cards);
1059                 if($trumpNR)
1060                   echo "<img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" />";
1061                 else
1062                   echo "<img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" />";
1063                 }
1064               else
1065                 echo "<img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" />";
1066             else
1067               if($sickness=="poverty")
1068                 {
1069                 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1070                 $cards    = DB_get_all_hand($userhash);
1071                 $trumpNR  = count_trump($cards);
1072                 if($trumpNR)
1073                   echo "<img src=\"pics/button/poverty2_trump_button.png\" class=\"button\" alt=\"poverty2 < trump back\" />";
1074                 else
1075                   echo "<img src=\"pics/button/poverty2_notrump_button.png\" class=\"button\" alt=\"poverty2 <\" />";
1076                 }
1077               else
1078                 echo "<img src=\"pics/button/poverty2_partner_button.png\" class=\"button\" alt=\"poverty2 >\" />";
1079               
1080           if($GT=="wedding" && $party=="re")
1081               if($sickness=="wedding")
1082                 echo "<img src=\"pics/button/wedding_button.png\" class=\"button\" alt=\"wedding\" />";
1083               else
1084                 echo "<img src=\"pics/button/wedding_partner_button.png\" class=\"button\" alt=\"wedding partner\" />";
1085           
1086           if(ereg("solo",$GT) && $party=="re")
1087             {
1088               if(ereg("queen",$GT))
1089                 echo "<img src=\"pics/button/queensolo_button.png\" class=\"button\" alt=\"$GT\" />";
1090               else if(ereg("jack",$GT))
1091                 echo "<img src=\"pics/button/jacksolo_button.png\" class=\"button\" alt=\"$GT\" />";
1092               else if(ereg("club",$GT))
1093                 echo "<img src=\"pics/button/clubsolo_button.png\" class=\"button\" alt=\"$GT\" />";
1094               else if(ereg("spade",$GT))
1095                 echo "<img src=\"pics/button/spadesolo_button.png\" class=\"button\" alt=\"$GT\" />";
1096               else if(ereg("heart",$GT))
1097                 echo "<img src=\"pics/button/heartsolo_button.png\" class=\"button\" alt=\"$GT\" />";
1098               else if(ereg("trumpless",$GT))
1099                 echo "<img src=\"pics/button/notrumpsolo_button.png\" class=\"button\" alt=\"$GT\" />";
1100               else if(ereg("trump",$GT))
1101                 echo "<img src=\"pics/button/diamandsolo_button.png\" class=\"button\" alt=\"$GT\" />";
1102             }
1103
1104           /* add point calls */
1105           if($call!=NULL)
1106             {
1107               if($party=="re")
1108                 echo "<img src=\"pics/button/re_button.png\" class=\"button\" alt=\"re\" />";
1109               else
1110                 echo "<img src=\"pics/button/contra_button.png\" class=\"button\" alt=\"contra\" />";
1111               switch($call)
1112                 {
1113                 case "0":
1114                   echo "<img src=\"pics/button/0_button.png\" class=\"button\" alt=\"0\" />";
1115                   break;
1116                 case "30":
1117                   echo "<img src=\"pics/button/30_button.png\" class=\"button\" alt=\"30\" />";
1118                   break;
1119                 case "60":
1120                   echo "<img src=\"pics/button/60_button.png\" class=\"button\" alt=\"60\" />";
1121                   break;
1122                 case "90":
1123                   echo "<img src=\"pics/button/90_button.png\" class=\"button\" alt=\"90\" />";
1124                   break;
1125                 }
1126             }
1127
1128           echo "<br />\n";
1129           echo " local time: ".date("Y-m-d H:i:s")."<br />\n";
1130           echo " last login: ".date("Y-m-d H:i:s",$lastlogin)."<br />\n";
1131           echo " </span>\n";
1132
1133         }
1134       echo  "</div>\n";
1135
1136       /* get everything relevant to display the tricks */
1137       $result = mysql_query("SELECT Hand_Card.card_id as card,".
1138                             "       Hand.position as position,".
1139                             "       Play.sequence as sequence, ".
1140                             "       Trick.id, ".
1141                             "       Comment.comment, ".
1142                             "       Play.create_date, ".
1143                             "       Hand.user_id ".
1144                             "FROM Trick ".
1145                             "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1146                             "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1147                             "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1148                             "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1149                             "WHERE Trick.game_id='".$gameid."' ".
1150                             "ORDER BY Trick.id,sequence ASC");
1151       $trickNR   = 1;
1152       $lasttrick = DB_get_max_trickid($gameid);
1153       
1154       $play = array(); /* needed to calculate winner later  */
1155       $seq  = 1;          
1156       $pos  = DB_get_startplayer_by_gameid($gameid)-1; 
1157       $firstcard = ""; /* first card in a trick */
1158       
1159       echo "\n<ul class=\"tricks\">\n";
1160       echo "  <li class=\"nohighlight\"> Game $gameid: </li>\n";
1161       
1162       while($r = mysql_fetch_array($result,MYSQL_NUM))
1163         {
1164           $pos     = $r[1];
1165           $seq     = $r[2];
1166           $trick   = $r[3];
1167           $comment = $r[4];
1168           $timeplayed = strtotime($r[5]);
1169           $user    = $r[6];
1170
1171           $offset = DB_get_user_timezone($user);
1172           $zone   = return_timezone($offset);
1173           date_default_timezone_set($zone);
1174
1175           /* check if first schweinchen has been played */
1176           if($r[0] == 19 || $r[0] == 20 )
1177             $GAME["schweinchen"]++;
1178           
1179           /* save card to be able to find the winner of the trick later */
1180           $play[$seq] = array("card"=>$r[0],"pos"=>$pos); 
1181           
1182           if($seq==1)
1183             {
1184               /* first card in a trick, output some html */
1185               if($trick!=$lasttrick)
1186                 {
1187                   /* start of an old trick? */
1188                   echo "  <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1189                     "    <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1190                     "      <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1191                 }
1192               else if($trick==$lasttrick)
1193                 {
1194                   /* start of a last trick? */
1195                   echo "  <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1196                     "    <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1197                     "      <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1198                 };
1199               
1200               /* remember first card, so that we are able to check, what cards can be played */
1201               $firstcard = $r[0];
1202             };
1203           
1204           /* display card */
1205           echo "      <div class=\"card".($pos-1)."\">\n";
1206           
1207           /* display comments */
1208           if($comment!="")
1209             echo "        <span class=\"comment\">".$comment."</span>\n";
1210           
1211           echo "        ";
1212           display_card($r[0],$PREF["cardset"]);
1213           
1214           echo "      </div>\n"; /* end div card */
1215           
1216           /* end of trick? */
1217           if($seq==4)
1218             {
1219               $trickNR++;
1220               echo "    </div>\n  </li>\n";  /* end div table, end li table */
1221             }
1222         }
1223       
1224       if($seq!=4 && $trickNR>1) 
1225         echo "    </div>\n  </li>\n";  /* end div table, end li table */
1226       
1227       echo "</ul>\n";
1228       
1229       /* whos turn is it? */
1230       if($seq==4)
1231         {
1232           $winner    = get_winner($play,$gametype); /* returns the position */
1233           $next      = $winner;
1234           $firstcard = ""; /* new trick, no first card */
1235         }
1236       else
1237         {
1238           $next = $pos+1;
1239           if($next==5) $next = 1;
1240         }
1241       
1242       /* my turn?, display cards as links, ask for comments*/
1243       if(DB_get_pos_by_hash($me) == $next)
1244         $myturn = 1;
1245       else
1246         $myturn = 0;
1247       
1248       /* do we want to play a card? */
1249       if(myisset("card") && $myturn)
1250         {
1251           $card   = $_REQUEST["card"];
1252           $handid = DB_get_handid_by_hash($me); 
1253           
1254           /* check if we have card and that we haven't played it yet*/
1255           /* set played in hand_card to true where hand_id and card_id*/
1256           $result = mysql_query("SELECT id FROM Hand_Card WHERE played='false' and ".
1257                                 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1258           $r = mysql_fetch_array($result,MYSQL_NUM);
1259           $handcardid = $r[0];
1260           
1261           if($handcardid) /* everything ok, play card  */
1262             {
1263               $comment = "";
1264
1265               /* update Game timestamp */
1266               DB_update_game_timestamp($gameid);
1267
1268               /* check if a call was made, must do this before we set the card status to played */
1269               if(myisset("call120") && $_REQUEST["call120"] == "yes" && can_call(120,$me))
1270                 $result = mysql_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
1271               if(myisset("call90")  && $_REQUEST["call90"]  == "yes" && can_call(90,$me))
1272                 $result = mysql_query("UPDATE Hand SET point_call='90'  WHERE hash='$me' ");
1273               if(myisset("call60")  && $_REQUEST["call60"]  == "yes" && can_call(60,$me))
1274                 $result = mysql_query("UPDATE Hand SET point_call='60'  WHERE hash='$me' ");
1275               if(myisset("call30")  && $_REQUEST["call30"]  == "yes" && can_call(30,$me))
1276                 $result = mysql_query("UPDATE Hand SET point_call='30'  WHERE hash='$me' ");
1277               if(myisset("call0")   && $_REQUEST["call0"]   == "yes" && can_call(0,$me))
1278                 $result = mysql_query("UPDATE Hand SET point_call='0'   WHERE hash='$me' ");
1279                 
1280               /* mark card as played */
1281               mysql_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1282                           DB_quote_smart($card));
1283
1284               /* check for schweinchen */
1285               //echo "schweinchen = ".$GAME["schweinchen"]." --$card-<br />";
1286               if($card == 19 || $card == 20 )
1287                 {
1288                   $GAME["schweinchen"]++;
1289                   if($GAME["schweinchen"]==3 && $RULES["schweinchen"]=="second" )
1290                     $comment="Schweinchen! ";
1291                   if($RULES["schweinchen"]=="both" )
1292                     $comment="Schweinchen! ";
1293                   if ($debug) echo "schweinchen = ".$GAME["schweinchen"]." ---<br />";
1294                 }
1295
1296               /* get trick id or start new trick */
1297               $a = DB_get_current_trickid($gameid);
1298               $trickid  = $a[0];
1299               $sequence = $a[1];
1300               $tricknr  = $a[2];
1301               
1302               $playid = DB_play_card($trickid,$handcardid,$sequence);
1303               
1304               /* if sequence == 4 check who one in case of wedding */
1305               if($sequence == 4 && $GT == "wedding") 
1306                 {
1307                   /* is wedding resolve */
1308                   $resolved = DB_get_sickness_by_gameid($gameid); 
1309                   if($resolved<0)
1310                     {
1311                       /* who has wedding */
1312                       $userids = DB_get_all_userid_by_gameid($gameid);
1313                       foreach($userids as $user)
1314                         {
1315                           $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1316                           if($usersick == "wedding")
1317                             $whosick = $user;
1318                         }
1319                       /* who won the trick */
1320                       $play     = DB_get_cards_by_trick($trickid);
1321                       $winner   = get_winner($play,$gametype); /* returns the position */
1322                       $winnerid = DB_get_userid_by_gameid_and_position($gameid,$winner);
1323                       /* is tricknr <=3 */
1324                       if($tricknr <=3 && $winnerid!=$whosick)
1325                         {
1326                           /* set resolved at tricknr*/
1327                           $resolved = DB_set_sickness_by_gameid($gameid,$tricknr); 
1328                           /* set partner */
1329                           $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1330                           DB_set_party_by_hash($whash,"re");
1331                         }
1332                       if($tricknr == 3 && $winnerid==$whosick)
1333                         {
1334                           /* set resolved at tricknr*/
1335                           $resolved = DB_set_sickness_by_gameid($gameid,'3'); 
1336                         }
1337                     }
1338                 }
1339               
1340               /* check for coment */
1341               if(myisset("comment"))
1342                 {
1343                   $comment.=$_REQUEST["comment"];
1344                 };  
1345               if($comment != "")
1346                 DB_insert_comment($comment,$playid,$myid);
1347
1348               /* display played card */
1349               echo "<div class=\"card\">";
1350               echo " you played  <br />";
1351               display_card($card,$PREF["cardset"]);
1352               echo "</div>\n";
1353               
1354               /*check if we still have cards left, else set status to gameover */
1355               if(sizeof(DB_get_hand($me))==0)
1356                 {
1357                   DB_set_hand_status_by_hash($me,'gameover');
1358                   $mystatus='gameover';
1359                 }
1360               
1361               /* if all players are done, set game status to game over, 
1362                * get the points of the last trick and send out an email 
1363                * to all players
1364                */
1365               $userids = DB_get_all_userid_by_gameid($gameid);
1366               
1367               $done=1;
1368               foreach($userids as $user)
1369                 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1370                   $done=0;
1371               
1372               if($done)
1373                 {
1374                   DB_set_game_status_by_gameid($gameid,"gameover");
1375                   /* get score for last trick 
1376                    * all other tricks are handled a few lines further down*/
1377                   $play   = DB_get_cards_by_trick($trickid);
1378                   $winner = get_winner($play,$gametype); /* returns the position */
1379                   /* get points of last trick and save it */
1380                   $points = 0;
1381                   foreach($play as $card)
1382                     $points = $points + card_value($card["card"]);
1383                   $winnerid = DB_get_handid_by_gameid_and_position($gameid,$winner);
1384                   if($winnerid>0)
1385                     mysql_query("INSERT INTO Score VALUES (NULL, '$gameid', '$winnerid', '$points')");
1386                   else
1387                     echo "ERROR during scoring";
1388                   
1389                   /* email all players */
1390                   /* individual score */
1391                   $result = mysql_query("SELECT fullname, IFNULL(SUM(score),0), Hand.party FROM Hand".
1392                                         " LEFT JOIN Score ON Hand.id=Score.hand_id".
1393                                         " LEFT JOIN User ON Hand.user_id=User.id".
1394                                         " WHERE Hand.game_id=$gameid".
1395                                         " GROUP BY fullname" );
1396                   $message  = "The game is over. Thanks for playing :)\n";
1397                   $message .= "Final score:\n";
1398                   while( $r = mysql_fetch_array($result,MYSQL_NUM))
1399                     $message .= "   ".$r[0]."(".$r[2].") ".$r[1]."\n";
1400
1401                   $result = mysql_query("SELECT Hand.party, IFNULL(SUM(score),0) FROM Hand".
1402                                         " LEFT JOIN Score ON Hand.id=Score.hand_id".
1403                                         " LEFT JOIN User ON Hand.user_id=User.id".
1404                                         " WHERE Hand.game_id=$gameid".
1405                                         " GROUP BY Hand.party" );
1406                   $message .= "\nTotals:\n";
1407                   while( $r = mysql_fetch_array($result,MYSQL_NUM))
1408                     $message .= "    ".$r[0]." ".$r[1]."\n";
1409                   
1410                   /* send out final email */
1411                   $all = array();
1412
1413                   foreach($userids as $user)
1414                     $all[] = DB_get_email_by_userid($user);
1415                   $TO = implode(",",$all);
1416
1417                   $help = "\n\n (you can use reply all on this email to reach all the players.)\n";
1418                   mymail($To,$EmailName."game over (game $gameid) part 1(2)",$message.$help);
1419
1420                   foreach($userids as $user)
1421                     {
1422                       $To   = DB_get_email_by_userid($user);
1423                       $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1424                       
1425                       $link = "Use this link to have a look at game $gameid: ".$host."?me=".$hash."\n\n" ;
1426                       mymail($To,$EmailName."game over (game $gameid) part 2(2)",$link);
1427                     }
1428                 }
1429               
1430               
1431               /* email next player */
1432               if(DB_get_game_status_by_gameid($gameid)=='play')
1433                 {
1434                   if($sequence==4)
1435                     {
1436                       $play   = DB_get_cards_by_trick($trickid);
1437                       $winner = get_winner($play,$gametype); /* returns the position */
1438                       
1439                       /* get points of last trick and save it, last trick is handled 
1440                        * a few lines further up  */
1441                       $points = 0;
1442                       foreach($play as $card)
1443                         $points = $points + card_value($card["card"]);
1444                       
1445                       $winnerid = DB_get_handid_by_gameid_and_position($gameid,$winner);
1446                       if($winnerid>0)
1447                         mysql_query("INSERT INTO Score VALUES (NULL, '$gameid', '$winnerid', '$points')");
1448                       else
1449                         echo "ERROR during scoring";
1450                       
1451                       if($debug)
1452                         echo "DEBUG: $winner got $points <br />";
1453                       
1454                       /* who is the next player? */
1455                       $next = $winner;
1456                     }
1457                   else
1458                     {
1459                       $next = DB_get_pos_by_hash($me)+1;
1460                     }
1461                   if($next==5) $next=1;
1462                   
1463                   /* email next player */
1464                   $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1465                   $email     = DB_get_email_by_hash($next_hash);
1466                   $who       = DB_get_userid_by_email($email);
1467                   DB_set_player_by_gameid($gameid,$who);
1468
1469                   $message = "A card has been played in game $gameid.\n\n".
1470                     "It's your turn  now.\n".
1471                     "Use this link to play a card: ".$host."?me=".$next_hash."\n\n" ;
1472                   mymail($email,$EmailName."a card has been played in game $gameid",$message);            
1473                 }
1474             }
1475           else
1476             {
1477               echo "can't find that card?! <br />\n";
1478             }
1479         }
1480       else if(myisset("card") && !$myturn )
1481         {
1482           echo "please wait until it's your turn! <br />\n";
1483         }
1484       
1485       $mycards = DB_get_hand($me);
1486       $mycards = mysort($mycards,$gametype);
1487       echo "<div class=\"mycards\">\n";
1488       
1489       if($myturn && !myisset("card"))
1490         {
1491           echo "Hello ".$myname.", it's your turn!  <br />\n";
1492           echo "Your cards are: <br />\n";
1493           echo "<form  action=\"index.php?me=$me\" method=\"post\">\n";
1494           
1495           /* do we have to follow suite? */
1496           $followsuit = 0;
1497           if(have_suit($mycards,$firstcard))
1498             $followsuit = 1;
1499           
1500           foreach($mycards as $card) 
1501             {
1502               if($followsuit && !same_type($card,$firstcard))
1503                 display_card($card,$PREF["cardset"]);
1504               else
1505                 display_link_card($card,$PREF["cardset"]);
1506             }
1507           
1508           if( can_call(120,$me) )
1509               echo " re/contra (120):".
1510                 " <input type=\"radio\" name=\"call120\" value=\"yes\" /> ";
1511           if( can_call(90,$me) )
1512               echo " 90:".
1513                 " <input type=\"radio\" name=\"call90\" value=\"yes\" /> ";
1514           if( can_call(60,$me) )
1515               echo " 60:".
1516                 " <input type=\"radio\" name=\"call60\" value=\"yes\" /> ";
1517           if( can_call(30,$me) )
1518               echo " 30:".
1519                 " <input type=\"radio\" name=\"call30\" value=\"yes\" /> ";
1520           if( can_call(0,$me) )
1521               echo " 0:".
1522                 " <input type=\"radio\" name=\"call0\" value=\"yes\" /> ".
1523                 " no call:".
1524                 " <input type=\"radio\" name=\"call0\" value=\"no\" /> ";
1525
1526           echo "<br />\nA short comments:<input name=\"comment\" type=\"text\" size=\"30\" maxlength=\"50\" />\n";
1527           echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />\n";
1528           echo "<input type=\"submit\" value=\"move\" />\n";
1529           echo "</form>\n";
1530         }
1531       else if($mystatus=='play')
1532         {
1533           echo "Your cards are: <br />\n";
1534           foreach($mycards as $card) 
1535             display_card($card,$PREF["cardset"]);
1536         }
1537       else if($mystatus=='gameover')
1538         {
1539           $oldcards = DB_get_all_hand($me);
1540           $oldcards = mysort($oldcards,$gametype);
1541           echo "Your cards were: <br />\n";
1542           foreach($oldcards as $card) 
1543             display_card($card,$PREF["cardset"]);
1544           
1545           $userids = DB_get_all_userid_by_gameid($gameid);
1546           foreach($userids as $user)
1547             {
1548               $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1549               
1550               if($userhash!=$me)
1551                 {
1552                   echo "<br />";
1553                   
1554                   $name = DB_get_name_by_userid($user);
1555                   $oldcards = DB_get_all_hand($userhash);
1556                   $oldcards = mysort($oldcards,$gametype);
1557                   echo "$name's cards were: <br />\n";
1558                   foreach($oldcards as $card)
1559                     display_card($card,$PREF["cardset"]);
1560                 }
1561             }
1562         }
1563       echo "</div>\n";
1564       
1565       /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1566       if($mystatus=='play')
1567         break;
1568       /* the following happens only when the gamestatus is 'gameover' */
1569       /* check if game is over, display results */
1570       if(DB_get_game_status_by_gameid($gameid)=='play')
1571         {
1572           echo "the game is over for you.. other people still need to play though";
1573         }
1574       else
1575         {
1576           echo "the game is over now...<br />\n";
1577           
1578           $result = mysql_query("SELECT fullname, IFNULL(SUM(score),0), Hand.party FROM Hand".
1579                                 " LEFT JOIN Score ON Hand.id=Score.hand_id".
1580                                 " LEFT JOIN User ON Hand.user_id=User.id".
1581                                 " WHERE Hand.game_id=$gameid".
1582                                 " GROUP BY fullname" );
1583           echo "Final Score:<br />\n".
1584             " <table>\n";;
1585           while( $r = mysql_fetch_array($result,MYSQL_NUM))
1586             echo "  <tr><td>  ".$r[0]."</td><td>(".$r[2].")</td><td> ".$r[1]."</td></tr>";
1587           echo "</table>\n";
1588
1589           $result = mysql_query("SELECT Hand.party, IFNULL(SUM(score),0) FROM Hand".
1590                                 " LEFT JOIN Score ON Hand.id=Score.hand_id".
1591                                 " LEFT JOIN User ON Hand.user_id=User.id".
1592                                 " WHERE Hand.game_id=$gameid".
1593                                 " GROUP BY Hand.party" );
1594           echo "Totals:<br />\n".
1595             " <table> \n";
1596           while( $r = mysql_fetch_array($result,MYSQL_NUM))
1597             echo "  <tr><td>".$r[0]."</td><td> ".$r[1]."</td></tr>\n";
1598           echo "</table>\n";
1599           
1600           $session = DB_get_session_by_gameid($gameid);
1601           $result  = mysql_query("SELECT id,create_date FROM Game".
1602                                  " WHERE session=$session".
1603                                  " ORDER BY create_date DESC".
1604                                  " LIMIT 1");
1605           $r = -1;
1606           if($result)
1607             $r = mysql_fetch_array($result,MYSQL_NUM);
1608           
1609           if(!$session || $gameid==$r[0])
1610             {
1611               /* suggest a new game with the same people in it, just rotated once */
1612               $names = DB_get_all_names_by_gameid($gameid);
1613               output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
1614             }
1615         }
1616       break;
1617     default:
1618       echo "error in testing the status";
1619     }
1620     output_footer();
1621     DB_close();
1622     exit();
1623  } 
1624 /* user status page */ 
1625  else if(myisset("email","password"))
1626    {
1627      /* test id and password, should really be done in one step */
1628      $email     = $_REQUEST["email"];
1629      $password  = $_REQUEST["password"];
1630      
1631
1632      if(myisset("forgot"))
1633        {
1634          $ok = 1;
1635
1636          $uid = DB_get_userid_by_email($email);
1637          if(!$uid)
1638            $ok = 0;
1639          
1640          if($ok)
1641            {
1642              echo "Hmm, you forgot your passwort...nothing I can do at the moment:(  ";
1643              echo " you need to email Arun for now... in the future it will be all automated and an ";
1644              echo "email with a new password will go to $email.";
1645            }
1646          else
1647            {
1648              if($email=="")
1649                echo "you need to give me an email address!";
1650              else
1651                echo "couldn't find a player with this email, please contact Arun, if you think this is a mistake";
1652            } 
1653        }
1654      else 
1655      {
1656        /* verify password and email */
1657        if(strlen($password)!=32)
1658          $password = md5($password);
1659        
1660        $ok  = 1;
1661        $uid = DB_get_userid_by_email_and_password($email,$password);
1662        if(!$uid)
1663          $ok = 0;
1664        
1665        if($ok)
1666          {
1667            DB_get_PREF($uid);
1668
1669            if(myisset("setpref"))
1670              {
1671                $setpref=$_REQUEST["setpref"];
1672                switch($setpref)
1673                  {
1674                  case "germancards":
1675                  case "englishcards":
1676                    $result = mysql_query("SELECT * from User_Prefs".
1677                                          " WHERE user_id='$uid' AND pref_key='cardset'" );
1678                    if( mysql_fetch_array($result,MYSQL_NUM))
1679                      $result = mysql_query("UPDATE User_Prefs SET value=".DB_quote_smart($setpref).
1680                                            " WHERE user_id='$uid' AND pref_key='cardset'" );
1681                    else
1682                      $result = mysql_query("INSERT INTO User_Prefs VALUES(NULL,'$uid','cardset',".DB_quote_smart($setpref).")");
1683                    echo "Ok, changed you preferences for the cards.\n";
1684                    break;
1685                  }
1686              }
1687            else /* output default user page */
1688              {
1689                $time = DB_get_user_timestamp($uid);
1690                $unixtime =strtotime($time);
1691                
1692                $offset = DB_get_user_timezone($uid);
1693                $zone = return_timezone($offset);
1694                date_default_timezone_set($zone);
1695                
1696                /* display links to settings */
1697                output_user_settings($email,$password);
1698                
1699                echo "last login: ".date("r",$unixtime)."<br />";
1700                
1701                DB_update_user_timestamp($uid);
1702                
1703                echo "<p>These are your games that haven't started yet:<br />\n";
1704                $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.mod_date,Game.player from Hand".
1705                                      " LEFT JOIN Game On Hand.game_id=Game.id".
1706                                      " WHERE Hand.user_id='$uid' AND Game.status='pre'" );
1707                while( $r = mysql_fetch_array($result,MYSQL_NUM))
1708                  {
1709                    echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a>";
1710                    if($r[3])
1711                      {
1712                        if($r[3]==$uid)
1713                          echo "(it's <strong>your</strong> turn)\n";
1714                        else
1715                          {
1716                            $name = DB_get_name_by_userid($r[3]);
1717                            echo "(it's $name's turn)\n";
1718                          };
1719                      }
1720                    if(time()-strtotime($r[2]) > 60*60*24*30)
1721                      echo " The game has been running for over a month.".
1722                        " Do you want to cancel it? <a href=\"$host?cancle=1&amp;me=".$r[0]."\">yes</a>".
1723                        " (clicking here is final and can't be restored)";
1724                    echo "<br />";
1725                  }
1726                echo "</p>\n";
1727
1728                echo "<p>These are the games you are playing in:<br />\n";
1729                $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.mod_date,Game.player from Hand".
1730                                      " LEFT JOIN Game On Hand.game_id=Game.id".
1731                                      " WHERE Hand.user_id='$uid' AND Game.status='play'" );
1732                while( $r = mysql_fetch_array($result,MYSQL_NUM))
1733                  {
1734                    echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a>";
1735                    if($r[3])
1736                      {
1737                        if($r[3]==$uid)
1738                          echo "(it's <strong>your</strong> turn)\n";
1739                        else
1740                          {
1741                            $name = DB_get_name_by_userid($r[3]);
1742                            echo "(it's $name's turn)\n";
1743                          };
1744                      }
1745                    if(time()-strtotime($r[2]) > 60*60*24*30)
1746                      echo " The game has been running for over a month.".
1747                        " Do you want to cancel it? <a href=\"$host?cancle=1&amp;me=".$r[0]."\">yes</a>".
1748                        " (clicking here is final and can't be restored)";
1749                    echo "<br />";
1750                  }
1751                echo "</p>\n";
1752                
1753                
1754                echo "<p>And these are your games that are already done:<br />Game: \n";
1755                $output = array();
1756                $result = mysql_query("SELECT hash,game_id from Hand WHERE user_id='$uid' AND status='gameover'" );
1757                while( $r = mysql_fetch_array($result,MYSQL_NUM))
1758                  $output[] = "<a href=\"".$host."?me=".$r[0]."\">#".$r[1]." </a>";
1759                echo implode(", ",$output)."</p>\n";
1760                
1761                $names = DB_get_all_names();
1762                echo "<p>Registered players:<br />\n";
1763                echo implode(", ",$names)."\n";
1764                echo "</p>\n";
1765                
1766                echo "<p>Want to start a new game? Visit <a href=\"".$host."?new\">this page.</a></p>";
1767              }
1768          }
1769        else
1770          {
1771            echo "Sorry email and password don't match <br />";
1772          }
1773      };
1774      output_footer();
1775      DB_close();
1776      exit();
1777    }
1778 /* page for registration */
1779  else if(myisset("register") )
1780    {
1781      output_register();
1782    }
1783 /* new user wants to register */
1784  else if(myisset("Rfullname","Remail","Rpassword","Rtimezone") )
1785    {
1786      $ok=1;
1787      if(DB_get_userid_by_name($_REQUEST["Rfullname"]))
1788        {
1789          echo "please chose another name<br />";
1790          $ok=0;
1791        }
1792      if(DB_get_userid_by_email($_REQUEST["Remail"]))
1793        {
1794          echo "this email address is already used ?!<br />";
1795          $ok=0;
1796        }
1797      if($ok)
1798        {
1799          $r=mysql_query("INSERT INTO User VALUES(NULL,".DB_quote_smart($_REQUEST["Rfullname"]).
1800                         ",".DB_quote_smart($_REQUEST["Remail"]).
1801                         ",".DB_quote_smart(md5($_REQUEST["Rpassword"])).
1802                         ",".DB_quote_smart($_REQUEST["Rtimezone"]).",NULL)"); 
1803          
1804          if($r)
1805            echo " added you to the database";
1806          else
1807            echo " something went wrong";
1808        }
1809    }
1810 /* default login page */
1811  else
1812    { 
1813      $pre[0]=0;$game[0]=0;$done[0]=0;
1814      $r=mysql_query("SELECT COUNT(id) FROM Game GROUP BY status");
1815      if($r) {
1816        $pre  = mysql_fetch_array($r,MYSQL_NUM);     
1817        $game = mysql_fetch_array($r,MYSQL_NUM);     
1818        $done = mysql_fetch_array($r,MYSQL_NUM);     
1819      }
1820
1821      $r=mysql_query("SELECT AVG(datediff(mod_date,create_date)) FROM Game where status='gameover' ");
1822      if($r)
1823        $avgage= mysql_fetch_array($r,MYSQL_NUM);     
1824      else
1825        $avgage[0]=0;
1826
1827      output_home_page($pre[0],$game[0],$done[0],$avgage[0]);
1828    }
1829
1830 output_footer();
1831
1832 DB_close();
1833
1834 /*
1835  *Local Variables: 
1836  *mode: php
1837  *mode: hs-minor
1838  *End:
1839  */
1840 ?>
1841
1842