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