3a7ecade205fc00a1235e74a9d425801644c9c18
[e-DoKo.git] / index.php
1 <!DOCTYPE html PUBLIC
2     "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN"
3     "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
5   <head>
6      <title>e-Doko</title>
7      <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type" />
8      <link rel="stylesheet" type="text/css" href="standard.css" />      
9   </head>
10 <body>
11 <h1> Welcome to E-Doko </h1>
12 <?php
13 /*
14  * config 
15  */
16
17 $host  = "http://doko.nubati.net/index.php";
18 $wiki  = "http://wiki.nubati.net/index.php?title=EmailDoko";
19 $debug = 1;
20
21 /*
22  * end config
23  */     
24                                         
25
26 /* helper function */
27 function mymail($To,$Subject,$message)
28 {  
29   global $debug;
30
31   if($debug)
32     {
33       str_replace("\n","<br />",$message);
34       echo "<br />To: $To<br />Subject: $Subject <br />$message<br />\n";
35     }
36   else
37     mail($To,$Subject,$message);
38   return;
39 }
40
41 function parse_status()
42 {
43   global $game,$history,$player,$hash,$lines;
44   
45   $game["init"]        = 0;
46   $game["solo-who"]    = -1;
47   $game["solo-what"]   = "todo";
48   $game["wedding"]     = -1;
49   $game["poverty"]     = "";
50   $game["nines"]       = -1;
51   $game["startplayer"] = 0;
52
53   for($i=0;$i<4;$i++)
54     {
55       $tmp = explode( ":",$lines[$i]);
56       $hash[$i]   = $tmp[0];
57       $player[$tmp[0]]["number"] = $i;
58       $player[$tmp[0]]["hash"]   = $tmp[0];
59       $player[$tmp[0]]["name"]   = $tmp[1];
60       $player[$tmp[0]]["email"]  = $tmp[2];
61       $player[$tmp[0]]["option"] = $tmp[3];
62       $player[$tmp[0]]["points"] = $tmp[4]; 
63       $player[$tmp[0]]["cards"]  = $tmp[5];
64       if(ereg("s",$tmp[3])) $game["init"]++;       /* how many players are ready? */
65       if(ereg("P",$tmp[3])) $game["poverty"].= $i; /* players with poverty, could be two, so use a string */
66       if(ereg("N",$tmp[3])) $game["nines"]   = $i; /* the player with too many nines, only one possible */
67       if(ereg("W",$tmp[3])) $game["wedding"] = $i; /* the player with the wedding, also only one possible */
68       if(ereg("([OSQJCAH])",$tmp[3],$match) && ($game["solo-who"]<0) )
69         {
70           $game["solo-who"]    = $i;     
71           $game["startplayer"] = $i;
72           switch($match[1])
73             {
74             case "O":
75               $game["solo-what"] = "No Trump";
76             case "S":
77               $game["solo-what"] = "Trump";
78             case "Q":
79               $game["solo-what"] = "Queen";
80             case "J":
81               $game["solo-what"] = "Jack";
82             case "C":
83               $game["solo-what"] = "Club";
84             case "A":
85               $game["solo-what"] = "Spade";
86             case "H":
87               $game["solo-what"] = "Heart";
88             }
89         }
90   
91     }  
92   /* save the game history */
93   for($i=4;$i<sizeof($lines);$i++)
94     {
95       if(!ereg("^[[:space:]]*$",trim($lines[$i])))
96         {
97           $history[] = $lines[$i];
98         }
99     }
100   
101   if(sizeof($history)==0 || (sizeof($history)==1 && strlen($history[0])==3 ))
102     $history[0] = $game["startplayer"].":";
103   
104   return;
105 }
106
107 function count_nines($cards)
108 {
109   $card = explode(";",$cards);
110   
111   $nines =0;
112
113   foreach($card as $c)
114     {
115       if($c == "25" || $c == "26") $nines++;
116       else if($c == "33" || $c == "34") $nines++;
117       else if($c == "41" || $c == "42") $nines++;
118       else if($c == "47" || $c == "48") $nines++;
119     }
120   
121   return $nines;
122 }
123
124 function check_wedding($cards)
125 {
126   $card = explode(";",$cards);
127   
128   $count =0;
129
130   if( in_array("3",$card) && in_array("2",$card) )
131     $count=1;
132
133   return $count;
134 }
135
136 function count_trump($cards)
137 {
138   $card = explode(";",$cards);
139   
140   $trump =0;
141
142   /* count each trump */
143   foreach($card as $c)
144     {
145       if( (int)($c) <27) $trump++;
146     }
147
148   /* subtract one, in case player has both foxes */
149   if( in_array("19",$card) && in_array("20",$card) )
150     $trump--;
151
152   return $trump;
153 }
154
155
156
157 function card_to_name($card)
158 {
159   switch($card)
160     {
161       case 1:
162       case 2:
163         return "ten of hearts";
164       case 3:
165       case 4:
166       return "queen of clubs";
167       case 5:
168       case 6:
169       return "queen of spades";
170       case 7:
171       case 8:
172       return "queen of hearts";
173       case 9:
174       case 10:
175       return "queen of diamonds";
176       case 11:
177       case 12:
178       return "jack of clubs";
179       case 13:
180       case 14:
181       return "jack of spades";
182       case 15:
183       case 16:
184       return "jack of hearts";
185       case 17:
186       case 18:
187       return "jack of diamonds";
188       case 19:
189       case 20:
190       return "ace of diamonds";
191       case 21:
192       case 22:
193       return "ten of diamonds";
194       case 23:
195       case 24:
196       return "king of diamonds";
197       case 25:
198       case 26:
199       return "nine of diamonds";;
200       case 27:
201       case 28:
202       return "ace of clubs";
203       case 29:
204       case 30:
205       return "ten of clubs";
206       case 31:
207       case 32:
208       return "king of clubs";
209       case 33:
210       case 34:
211       return "nine of clubs";
212       case 35:
213       case 36:
214       return "ace of spades";
215       case 37:
216       case 38:
217       return "ten of spades";
218       case 39:
219       case 40:
220       return "king of spades";
221       case 41:
222       case 42:
223       return "nine of spades";
224       case 43:
225       case 44:
226       return "ace of hearts";
227       case 45:
228       case 46:
229       return "king of hearts";
230       case 47:
231       case 48:
232       return "nine of hearts";
233       default:
234       return "something went wrong, please contact the admin";
235     }
236 }
237
238 function card_value($card)
239 {
240   switch($card-1)
241     {
242       case 0:
243       case 1:
244         return 10;
245       case 2:
246       case 3:
247       case 4:
248       case 5:
249       case 6:
250       case 7:
251       case 8:
252       case 9:
253         return 3;
254       case 10:
255       case 11:
256       case 12:
257       case 13:
258       case 14:
259       case 15:
260       case 16:
261       case 17:
262         return 2;
263       case 18:
264       case 19:
265         return 11;
266       case 20:
267       case 21:
268         return 10;
269       case 22:
270       case 23:
271         return 4;
272       case 24:
273       case 25:
274       return 0;
275       case 26:
276       case 27:
277       return 11;
278       case 28:
279       case 29:
280       return 10;
281       case 30:
282       case 31:
283       return 4;
284       case 32:
285       case 33:
286       return 0;
287       case 34:
288       case 35:
289       return 11;
290       case 36:
291       case 37:
292       return 10;
293       case 38:
294       case 39:
295       return 4;
296       case 40:
297       case 41:
298       return 0;
299       case 42:
300       case 43:
301       return 11;
302       case 44:
303       case 45:
304       return 4;
305       case 46:
306       case 47:
307       return 0;
308       default:
309       echo "something went wrong, please contact the admin <br>";
310       return 0;
311     }
312 }
313
314 function display_card($card)
315 {
316   if( $card/2 - (int)($card/2) == 0.5)
317     echo "<img src=\"cards/".$card.".png\"  alt=\"".card_to_name($card)."\" />\n";
318   else
319     echo "<img src=\"cards/".($card-1).".png\"  alt=\"".card_to_name($card-1)."\" />\n";
320   return;
321 }
322
323 function display_link_card($card,$me)
324 {
325   if( $card/2 - (int)($card/2) == 0.5)
326     echo "<a href=\"index.php?me=$me&amp;card=$card\"><img src=\"cards/".$card.".png\"  alt=\"".card_to_name($card)."\" /></a>\n";
327   else
328     echo "<a href=\"index.php?me=$me&amp;card=$card\"><img src=\"cards/".($card-1).".png\"  alt=\"".card_to_name($card-1)."\" /></a>\n";
329   return;
330 }
331
332 function save_status()
333 {
334   global $player,$game,$hash,$history;
335
336   $output = fopen("status.txt","w");
337   if ($output)
338     {
339       foreach($hash as $key)
340         {
341           /* sorting the options, not sure why I do that actually */
342           $tmp="";
343           if( ereg("i",$player[$key]["option"]) )
344             $tmp.="i";
345           if( ereg("s",$player[$key]["option"]) )
346             $tmp.="s";
347           if( ereg("t",$player[$key]["option"]) )
348             $tmp.="t";
349           if( ereg("c",$player[$key]["option"]) )
350             $tmp.="c";
351           if( ereg("N",$player[$key]["option"]) )
352             $tmp.="N";
353           if( ereg("W",$player[$key]["option"]) )
354             $tmp.="W";
355           if( ereg("P",$player[$key]["option"]) )
356             $tmp.="P";
357           if( ereg("O",$player[$key]["option"]) ) 
358             $tmp.="O";
359           if( ereg("S",$player[$key]["option"]) )
360             $tmp.="S";
361           if( ereg("Q",$player[$key]["option"]) )
362             $tmp.="Q";
363           if( ereg("J",$player[$key]["option"]) )
364             $tmp.="J";
365           if( ereg("C",$player[$key]["option"]) )
366             $tmp.="C";
367           if( ereg("A",$player[$key]["option"]) )
368             $tmp.="A";
369           if( ereg("H",$player[$key]["option"]) )
370             $tmp.="H";
371           $player[$key]["option"]=$tmp;
372
373           /* saving the player stats */
374           fwrite($output,"".$player[$key]["hash"].":" );
375           fwrite($output,"".$player[$key]["name"].":" );
376           fwrite($output,"".$player[$key]["email"].":" );
377           fwrite($output,"".$player[$key]["option"].":" );
378           fwrite($output,"".$player[$key]["points"].":" );
379           fwrite($output,"".$player[$key]["cards"] .":");
380           fwrite($output,"\n");
381         }
382       fwrite($output,"\n");
383       foreach($history as $line)
384         fwrite($output,$line);
385
386       fwrite($output,"\n");
387       fclose($output);
388     }
389   else
390     echo "can't open file for writing, please inform the admin.";
391   
392   return;
393 }
394
395 echo "<p>If you find bugs, please list them in the <a href=\"".$wiki."\">wiki</a></p>\n";
396         
397 echo "<p> Cards that have a <span class=\"comment\"> green </span> background have a comment, which you can access by hovering over the card with your mouse ;)</p>\n";
398
399 $history=array();
400
401 /* check for status file and read it, if possible */
402
403 if(file_exists("status.txt"))
404   $lines = file("status.txt");
405  else
406    die("no file");
407
408 /* check if we want to start a new game */
409 if( isset($_REQUEST["PlayerA"]) && 
410     isset($_REQUEST["PlayerB"]) && 
411     isset($_REQUEST["PlayerC"]) && 
412     isset($_REQUEST["PlayerD"]) && 
413     isset($_REQUEST["EmailA"]) && 
414     isset($_REQUEST["EmailB"]) && 
415     isset($_REQUEST["EmailC"]) && 
416     isset($_REQUEST["EmailD"]) && sizeof($lines<2))
417   {
418     $PlayerA = $_REQUEST["PlayerA"];
419     $PlayerB = $_REQUEST["PlayerB"];
420     $PlayerC = $_REQUEST["PlayerC"];
421     $PlayerD = $_REQUEST["PlayerD"];
422     $EmailA  = $_REQUEST["EmailA"] ;
423     $EmailB  = $_REQUEST["EmailB"] ;
424     $EmailC  = $_REQUEST["EmailC"] ;
425     $EmailD  = $_REQUEST["EmailD"] ;
426     
427     $hashA = md5("AGameOfDoko".$PlayerA.$EmailA);
428     $hashB = md5("AGameOfDoko".$PlayerB.$EmailB);
429     $hashC = md5("AGameOfDoko".$PlayerC.$EmailC);
430     $hashD = md5("AGameOfDoko".$PlayerD.$EmailD);
431
432     /* send out email, check for error with email */
433
434     $message = "\n".
435       "you are invited to play a game of DoKo (that is to debug the program ;).\n".
436       "Place comments and bug reports here:\n".
437       "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
438       "The whole round would consist of the following players:\n".
439       "$PlayerA\n".
440       "$PlayerB\n".
441       "$PlayerC\n".
442       "$PlayerD\n\n".
443       "If you want to join this game, please follow this link:\n\n".
444       " ".$host."?a=";
445     
446     mymail($EmailA,"Invite for a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
447     mymail($EmailB,"Invite for a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
448     mymail($EmailC,"Invite for a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
449     mymail($EmailD,"Invite for a game of DoKo","Hello $PlayerD,\n".$message.$hashD);
450     
451     /* read in random.txt */
452     if(file_exists("random.txt"))
453       $random = file("random.txt");
454     else
455       die("no random file");
456  
457     $randomNR = explode( ":", $random[2] );
458     
459     /* write initial status into file */
460     $output = fopen("status.txt","w");
461     if ($output)
462       {
463         fwrite($output, "$hashA:$PlayerA:$EmailA:::" );
464         for($i=0;$i<11;$i++)
465           fwrite($output,"$randomNR[$i];" );
466         fwrite($output,"$randomNR[11]:" ); $i++;
467         fwrite($output,"\n");
468         
469         fwrite($output, "$hashB:$PlayerB:$EmailB:::" );
470         for(;$i<23;$i++)
471           fwrite($output,"$randomNR[$i];" );
472         fwrite($output,"$randomNR[23]:" ); $i++;
473         fwrite($output,"\n");
474         
475         fwrite($output, "$hashC:$PlayerC:$EmailC:::" );
476         for(;$i<35;$i++)
477           fwrite($output,"$randomNR[$i];" );
478         fwrite($output,"$randomNR[35]:" ); $i++;
479         fwrite($output,"\n");
480         
481         fwrite($output, "$hashD:$PlayerD:$EmailD:::");
482         for(;$i<47;$i++)
483           fwrite($output,"$randomNR[$i];" );
484         fwrite($output,"$randomNR[47]:" );
485         fwrite($output,"\n");
486         
487         fclose($output);
488       }
489     else
490       echo "can't open file for writing";
491   };
492
493 /* test if a game is running, else output everything for a new game */
494 if(sizeof($lines)<2)
495   {
496 ?>
497     <p> no game in progress, please input 4 names and email addresses, please make sure that the addresses are correct! </p>
498  <form action="index.php" method="post">
499    Name:  <input name="PlayerA" type="text" size="10" maxlength="20" /> 
500    Email: <input name="EmailA"  type="text" size="20" maxlength="30" /> <br />
501
502    Name:  <input name="PlayerB" type="text" size="10" maxlength="20" /> 
503    Email: <input name="EmailB"  type="text" size="20" maxlength="30" /> <br />
504
505    Name:  <input name="PlayerC" type="text" size="10" maxlength="20" /> 
506    Email: <input name="EmailC"  type="text" size="20" maxlength="30" /> <br />
507
508    Name:  <input name="PlayerD" type="text" size="10" maxlength="20" /> 
509    Email: <input name="EmailD"  type="text" size="20" maxlength="30" /> <br />
510
511    <input type="submit" value="start game" />
512  </form>
513 <?php
514    }
515  else
516    { /* load game status */
517      parse_status();
518 /*     **
519  *    *  *
520  *    ****
521  *    *  *
522  *
523  * check if a player wants to accept a game 
524  */
525      
526      if(isset($_REQUEST["a"]))
527        {
528          $a=$_REQUEST["a"];
529          
530          if( ereg("[is]",$player[$a]["option"]) &&  $game["init"]<4)
531            {
532              echo "just wait for the game to start";
533            }
534          else  if( !ereg("[is]",$player[$a]["option"]) )
535            {
536
537  ?>
538  <form action="index.php" method="post">
539    Do you want to play a game of DoKo?
540    yes<input type="radio" name="in" value="yes" />
541    no<input type="radio" name="in" value="no" /> <br />
542
543    Do you want to get an email for every card played or only if it your move?
544    every card<input type="radio" name="update" value="card" />
545    only on my turn<input type="radio" name="update" value="turn" /> <br />
546 <?php   
547              echo "<input type=\"hidden\" name=\"b\" value=\"$a\" />\n";
548              echo "\n";
549              echo "<input type=\"submit\" value=\"count me in\" />\n";
550              echo " </form>\n";
551            }
552        }
553
554 /*   ***
555  *   *  *
556  *   ***
557  *   *  *
558  *   ***
559  * yes? email him his hand, ask for solo, poverty, email every move or every card? 
560  */
561      if(isset($_REQUEST["b"]))
562        {
563          $b=$_REQUEST["b"];
564          
565          if( ereg("s",$player[$b]["option"])  && $game["init"]<4)
566            {
567              echo "just wait for the game to start";
568            }
569          else if( (!isset($_REQUEST["in"])|| !isset($_REQUEST["update"])) && !ereg("i",$player[$b]["option"]))
570            {
571              echo "go back to ";
572              echo "<a href=\"index.php?a=$b\"> here and fill out the form </a> <br />\n";
573            }
574          else
575            { /* show the hand */
576              if($_REQUEST["in"]=="no")
577                {
578                  for($i=0;$i<4;$i++)
579                    {
580                      $message = "Hello ".$player[$hash[$i]]["name"].",\n\n".
581                        "the game has been canceled due to the request of one of the players.\n";
582                      mymail($player[$hash[$i]]["email"],"[DoKo-Debug] the game has been canceled",$message); 
583                    }
584                    $output = fopen("status.txt","w");
585                    if($output)
586                      fclose($output);
587                    else
588                      echo "problem opening file";
589                }
590              else
591                {
592                  if($_REQUEST["update"]=="card") $player[$b]["option"].="c";
593                  if($_REQUEST["update"]=="turn") $player[$b]["option"].="t";
594                  
595                  $player[$b]["option"].="i";
596                  
597                  save_status();
598                  
599                  $allcards = $player[$b]["cards"];
600                  $mycards = explode(";",$allcards);
601                  
602                  sort($mycards);
603                  echo "your cards are <br />\n";
604                  foreach($mycards as $card) 
605                    {
606                      display_card($card);
607                    }
608                  echo "<br />\n";   
609  ?>
610  <p>aehm... at the moment please just answer everything with no, still need to implement this</p>                 
611  <form action="index.php" method="post">
612    
613    do you want to play solo? 
614    <select name="solo" size="1">
615      <option>No</option>
616      <option>No trump</option>
617      <option>Normal solo</option>
618      <option>Queen solo</option>
619      <option>Jack solo</option>
620      <option>Club solo</option>
621      <option>Spade solo</option>
622      <option>Heart solo</option>
623    </select>     
624    <br />
625
626 <?php   
627      
628                  echo "wedding?";
629                  if(check_wedding($player[$b]["cards"]))
630                    {
631                      echo " yes<input type=\"radio\" name=\"wedding\" value=\"yes\" />";
632                      echo " no <input type=\"radio\" name=\"wedding\" value=\"no\" /> <br />\n";
633                    }
634                  else
635                    {
636                      echo " no <input type=\"hidden\" name=\"wedding\" value=\"no\" /> <br />\n";
637                    };
638
639                  echo "do you have poverty?";
640                  if(count_trump($player[$b]["cards"])<4)
641                    {
642                      echo " yes<input type=\"radio\" name=\"poverty\" value=\"yes\" />";
643                      echo " no <input type=\"radio\" name=\"poverty\" value=\"no\" /> <br />\n";
644                    }
645                  else
646                    {
647                      echo " no <input type=\"hidden\" name=\"poverty\" value=\"no\" /> <br />\n";
648                    };
649
650                  echo "do you have too many nines?";
651                  if(count_nines($player[$b]["cards"])>4)
652                    {
653                      echo " yes<input type=\"radio\" name=\"nines\" value=\"yes\" />";
654                      echo " no <input type=\"radio\" name=\"nines\" value=\"no\" /> <br />\n";
655                    }
656                  else
657                    {
658                      echo " no <input type=\"hidden\" name=\"nines\" value=\"no\" /> <br />\n";
659                    };
660
661                  echo "<input type=\"hidden\" name=\"c\" value=\"$b\" />\n";
662                  echo "<input type=\"submit\" value=\"count me in\" />\n";
663                  
664                  echo "</form>\n";
665                }
666            }
667        }
668      if(isset($_REQUEST["c"]))
669        {
670          $c=$_REQUEST["c"];
671          
672          if(!isset($_REQUEST["solo"])|| !isset($_REQUEST["wedding"])|| !isset($_REQUEST["poverty"]) || !isset($_REQUEST["nines"]) )
673            {
674              echo "go back to ";
675              echo "<a href=\"index.php?b=$c\"> here and fill out the form </a> <br />\n";
676            }
677          else if(  ereg("s",$player[$c]["option"]) && $game["init"]<4 )
678            {
679              echo "just wait for the game to start";
680            }
681          else if($game["init"]<4)
682            { 
683              if( $_REQUEST["solo"]!="No")
684                {
685                  switch($_REQUEST["solo"])
686                    {
687                    case "No trump":
688                      $player[$c]["option"].="O";
689                      break;
690                    case "Normal solo":
691                      $player[$c]["option"].="S";
692                      break;
693                    case "Queen solo":
694                      $player[$c]["option"].="Q";
695                      break;
696                    case "Jack solo":
697                      $player[$c]["option"].="J";
698                      break;
699                    case "Club solo":
700                      $player[$c]["option"].="C";
701                      break;
702                    case "Spade solo":
703                      $player[$c]["option"].="A";
704                      break;
705                    case "Hear solo":
706                      $player[$c]["option"].="H";
707                      break;
708                    }
709                }
710              else if($_REQUEST["wedding"] == "yes")
711                {
712                  echo "wedding was chosen<br />\n";
713                  $player[$c]["option"].="W";
714                }
715              else if($_REQUEST["poverty"] == "yes")
716                {
717                  echo "poverty was chosen<br />\n";
718                  $player[$c]["option"].="P"; 
719                }
720              else if($_REQUEST["nines"] == "yes")
721                {
722                  echo "nines was chosen<br />\n";
723                  $player[$c]["option"].="N";
724                }
725              
726              $message = "you're in. once everyone has filled out the form,".
727                "the game will start and you'll get an eamil on your turn\n";
728              mymail($player[$c]["email"],"[DoKo-debug] the game will start soon",$message); 
729
730              $player[$c]["option"].="s";
731              save_status();
732              if(file_exists("status.txt"))
733                $lines = file("status.txt");
734              else
735                die("no file");
736              parse_status();
737
738              if($game["init"]==4)
739                {
740                  $message = "The game can start now, it's your turn, please use this link to play a card:\n".
741                    $host."?me=".$hash[$game["startplayer"]]."\n";
742                  mymail($player[$hash[$game["startplayer"]]]["email"],"[DoKo-debug] let's go",$message);
743                }
744              
745            }
746        }
747      if($game["init"]==4)
748        {
749          /* check for sickness*/
750          /***** someone has 5 nines and no one is playing solo=> cancel game */
751          if($game["nines"]>=0 && $game["solo-who"]<0)
752            {
753              $message = $player[$hash[$game["poverty"]]]["nines"]." has more than 4 nines. Game aborted!\n";
754              for($i=0;$i<4;$i++)
755                {
756                  mymail($player[$hash[$i]]["email"],"[DoKo-debug] the game has been canceled",$message); 
757                };
758
759              $output = fopen("status.txt","w");
760              if($output)
761                fclose($output);
762              else
763                echo "problem opening file";
764            }
765
766          /* who is requesting this*/
767          if(!isset($_REQUEST["me"]))
768            {    
769              if(!isset($_REQUEST["recovery"]))
770                {
771                  echo "A game is in progress and kibitzing is not allowed. Sorry!.<br />\n";
772                  echo "In case you are playing, but lost your email or can't access the game anymore, please input your email here:<br />\n";
773                  ?>
774  <form action="index.php" method="post">
775    recorvery: <input name="recovery"  type="text" size="20" maxlength="30" /> <br />
776    <input type="submit" value="get me back into the game" />
777  </form>
778 <?php
779                }
780              else
781                {
782                  $recovery=$_REQUEST["recovery"];
783                  $ok=-1;
784                  for($i=0;$i<4;$i++)
785                    if(trim($recovery)==trim($player[$hash[$i]]["email"]))
786                      $ok=$i;
787                  if($ok>=0)
788                    {
789                      $message = "Please try this link: ".$host."?me=".$hash[$ok]."\n".
790                        "\n if this doesn't work, contact the admin.\n";
791                      mymail($recovery,"[DoKo-Debug] recovery ",$message);
792                      echo "email has been sent.";
793                    }
794                  else
795                    {
796                      echo "can't find this email address, sorry.";
797                    }; 
798                } /* end recovery */
799            }
800          else
801            { /* $me is set */ 
802              $me = $_REQUEST["me"];
803              
804              /* output if we are playing a solo or a wedding */
805              if($game["solo-who"]>=0)
806                echo $player[$hash[$game["solo-who"]]]["name"]." is playing a ".$game["solo-what"]." solo!<br />\n";
807              else if($game["wedding"]>=0)
808                echo $player[$hash[$game["wedding"]]]["name"]." is playing a wedding!<br />\n";
809              
810              /* show history */
811              foreach($history as $play) 
812                {
813                  $trick = explode(":",$play);
814                  
815                  $last=-2;
816                  /* found old trick, display it */
817                  if(sizeof($trick)==5)
818                    echo "<div class=\"oldtrick background".$play[0]."\"><div class=\"table\">\n";
819                  else
820                    echo "<div class=\"trick back".$play[0]."\"><div class=\"table\">\n";
821                  for($i=0;$i<sizeof($trick)-1;$i++)
822                    {
823                      $card = $trick[$i];
824                      if(ereg("->",$card))
825                        {
826                          $tmp = explode("->",$card);
827
828                          if(strlen($tmp[2])>0)
829                            echo "<div class=\"card".$tmp[0]." comment\">";
830                          else
831                            echo "<div class=\"card".$tmp[0]."\">";
832                          echo $player[$hash[$tmp[0]]]["name"]."  <br />";
833                          display_card($tmp[1]);
834                          /* check for comment */
835                          if(strlen($tmp[2])>0)
836                            echo "<div class=\"comment\">".$tmp[2]."</div>\n";
837                          $last = $tmp[0];
838                          echo "</div>\n";
839                        }
840                    }
841                  echo "</div></div>\n";
842                }
843              
844              /* figure out who needs to play next */
845              $next = $last + 1;
846              if ($next>=4) 
847                $next -= 4 ;
848              if($last<0)
849                {
850                  $next=$history[sizeof($history)-1][0];
851                }
852              
853              /* are we trying to play a card? */
854              if(isset($_REQUEST["card"]))
855                {
856                  if($hash[$next]==$me)
857                    {
858                      $card    = $_REQUEST["card"];
859                      $mycards = explode(";",$player[$me]["cards"]);
860                      
861                      /* do we have that card */
862                      if(in_array($card,$mycards))
863                        {
864                          /* delete card from array */
865                          $tmp = array();
866                          foreach($mycards as $m)
867                            if($m != $card)
868                              $tmp[]=$m;
869                          
870                          $tmp2="";
871                          for($i=0;$i<sizeof($tmp)-1;$i++)
872                            {
873                              $tmp2.=$tmp[$i].";";
874                            }
875                          $tmp2.=$tmp[$i];
876                          $player[$me]["cards"]=$tmp2;
877                          
878                          /* add card to history, special case if this is the first card */
879                          if($last<0)
880                            {
881                              $history[sizeof($history)-1]="".$player[$me]["number"]."->".$card.":\n";
882                            }
883                          else
884                            {
885                              $tmp = explode(":",$history[sizeof($history)-1]);
886                              $tmp[sizeof($tmp)-1] = "".$player[$me]["number"]."->".$card.":";
887                              $history[sizeof($history)-1]=join(":",$tmp);
888                            }
889                          save_status();
890                          
891                          echo "<div class=\"card\">";
892                          echo " you played  <br />";
893                          display_card($card);
894                          echo "</div>\n";
895
896                          ?>
897 <br clear="both" />
898 <form action="index.php" method="post">
899    A short comment:<input name="comment" type="text" size="30" maxlength="50" /> 
900    <input type="hidden" name="me" value="<?php echo $me; ?>" />
901    <input type="submit" value="submit comment" />
902 </form>
903 <?php
904                          /* send out email to players who want/need to get informed */
905                          for($i=0;$i<4;$i++)
906                            {
907                              $mynext=$next+1; if($mynext>3)$mynext-=4;
908                              if((ereg("c",$player[$hash[$i]]["option"]) || $i==$mynext) && $hash[$i]!=$me)
909                                {
910                                  $message = " Hello ".$player[$hash[$i]]["name"].",\n\n";
911                                  
912                                  if($i==$mynext)
913                                    {
914                                      $message .= "it's your turn  now.\n".
915                                        "Use this link to play a card: ".$host."?me=".$hash[$i]."\n\n" ;
916                                    }
917                                  $message .= $player[$me]["name"]." has played the following card ".card_to_name($card)."\n";
918                                  
919                                  if($game["solo-who"]>=0)
920                                    $message.= $player[$hash[$game["solo-who"]]]["name"]." is playing a ".$game["solo-what"]." solo!\n";
921                                  
922                                  mymail($player[$hash[$i]]["email"],"[DoKo-debug] a card has been played",$message);
923                                  
924                                  if($debug)
925                                    echo "<a href=\"index.php?me=".$hash[$mynext]."\"> next player </a> <br />\n";
926                                }
927                            }
928                        }
929                      else
930                        echo "seems like you don't have that card<br />\n";
931                      
932                    }
933                  
934                } /* end if card is set */
935              else if(isset($_REQUEST["comment"]))
936                {
937                  $comment = $_REQUEST["comment"];
938                  $tmp = explode(":",$history[sizeof($history)-1]);
939                  $tmp2 = explode("->",$tmp[sizeof($tmp)-2]);
940                  
941                  $comment = str_replace(":","",$comment);
942
943                  if(sizeof($tmp2)<=2)
944                    $tmp[sizeof($tmp)-2] .= "->".$comment;
945                  $history[sizeof($history)-1]=join(":",$tmp);
946
947                  save_status();
948                }
949              else if(isset($_REQUEST["win"]) && strlen($history[sizeof($history)-1])>3)
950                {
951                  $win=$_REQUEST["win"];
952                  $history[]=$win.":\n";
953                  /* count points of the last trick */
954                  $points=0;
955
956                  $tmp = explode(":",$history[sizeof($history)-2]);
957                  for($i=0;$i<4;$i++)
958                    {
959                      $tmp2 = explode("->",$tmp[$i]);
960                      $c = $tmp2[1];
961                      $points += card_value($c);
962                    }
963                  $player[$hash[$win]]["points"]+=$points;
964                  echo "<br />\n ".$player[$hash[$win]]["name"]." won: $points Points <br />\n";
965                  
966                  save_status();
967                }; /* end if win is set */
968              echo "<br />\n";
969
970              $tmp = explode(":",$history[sizeof($history)-1]);
971
972              /* check last history entry: end of a trick? ask who won it */
973              if(sizeof($tmp)==5)
974                {
975                  ?>
976 <form action="index.php" method="post">
977 who won?
978 <?php 
979    for($i=0;$i<4;$i++)
980      echo $player[$hash[$i]]["name"]." <input type=\"radio\" name=\"win\" value=\"$i\" />";
981    echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />";
982 ?>
983 <input type="submit" value="submit" />
984
985 </form>
986 <?php
987                }
988              else if(sizeof($tmp)<5 && 1<sizeof($tmp) && !isset($_REQUEST["card"]))
989                {                 
990                  if(sizeof($tmp)==2 && strlen($tmp[0])==1)
991                    {
992                      $next=$tmp[0];
993                      
994                      if($debug)
995                        echo "DEBUG: the next move is for <a href=\"index.php?me=".$hash[$next]."\">the next player</a><br />\n";
996                      
997                      if(strlen(trim($player[$me]["cards"]))==0)
998                        {
999                          echo "<br /> game over, count points <br />\n";
1000                          for($i=0;$i<4;$i++)
1001                            {
1002                              echo $player[$hash[$i]]["name"]." got ".$player[$hash[$i]]["points"]."<br />\n";
1003                            }
1004                        }
1005                    }
1006                  echo "<br />\n";
1007                } /* end check for winner */
1008              
1009              echo "<br clear=\"both\" />";
1010              /* do we still have cards? display them */
1011              if(strlen(trim($player[$me]["cards"]))>0 )
1012                {
1013                  $allcards = trim($player[$me]["cards"]);
1014                  $mycards = explode(";",$allcards);
1015                  
1016                  sort($mycards);
1017                  
1018                  echo "<p class=\"mycards\">\n";
1019                  /* is it our turn? */
1020                  if($hash[$next]==$me && !isset($_REQUEST["card"]) && !isset($_REQUEST["win"])) 
1021                    {
1022                      echo "ITS YOUR TURN   <br />\n";
1023                      echo "your cards are: <br />\n";
1024                      foreach($mycards as $card) 
1025                        {
1026                          display_link_card($card,$me);
1027                        }
1028                    }
1029                  else 
1030                    { /* not our turn, just show the hand */
1031                      echo "your cards are: <br />\n";
1032                      foreach($mycards as $card) 
1033                        {
1034                          display_card($card);
1035                        }
1036                    }
1037                  echo "</p>\n";   
1038                }
1039            }
1040        }
1041
1042  } 
1043
1044 ?>
1045 </body>
1046 </html>