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