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