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