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