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