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