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