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