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