de9d3820179a8ab386ab40ae11bc5bb35171f716
[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 = 0;
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       $message = 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> Names that are underlined have a comment, which you can access by hovering over the name 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,"You are invited to a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
447     mymail($EmailB,"You are invited to a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
448     mymail($EmailC,"You are invited to a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
449     mymail($EmailD,"You are invited to 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 /* reread file */
493 if(file_exists("status.txt"))
494   $lines = file("status.txt");
495  else
496    die("no file");
497
498 /* test if a game is running, else output everything for a new game */
499 if(sizeof($lines)<2)
500   {
501 ?>
502     <p> no game in progress, please input 4 names and email addresses, please make sure that the addresses are correct! </p>
503  <form action="index.php" method="post">
504    Name:  <input name="PlayerA" type="text" size="10" maxlength="20" /> 
505    Email: <input name="EmailA"  type="text" size="20" maxlength="30" /> <br />
506
507    Name:  <input name="PlayerB" type="text" size="10" maxlength="20" /> 
508    Email: <input name="EmailB"  type="text" size="20" maxlength="30" /> <br />
509
510    Name:  <input name="PlayerC" type="text" size="10" maxlength="20" /> 
511    Email: <input name="EmailC"  type="text" size="20" maxlength="30" /> <br />
512
513    Name:  <input name="PlayerD" type="text" size="10" maxlength="20" /> 
514    Email: <input name="EmailD"  type="text" size="20" maxlength="30" /> <br />
515
516    <input type="submit" value="start game" />
517  </form>
518 <?php
519    }
520  else
521    { /* load game status */
522      parse_status();
523 /*     **
524  *    *  *
525  *    ****
526  *    *  *
527  *
528  * check if a player wants to accept a game 
529  */
530      
531      if(isset($_REQUEST["a"]))
532        {
533          $a=$_REQUEST["a"];
534          
535          if( ereg("[is]",$player[$a]["option"]) &&  $game["init"]<4)
536            {
537              echo "just wait for the game to start";
538            }
539          else  if( !ereg("[is]",$player[$a]["option"]) )
540            {
541
542  ?>
543  <form action="index.php" method="post">
544    Do you want to play a game of DoKo?
545    yes<input type="radio" name="in" value="yes" />
546    no<input type="radio" name="in" value="no" /> <br />
547
548    Do you want to get an email for every card played or only if it your move?
549    every card<input type="radio" name="update" value="card" />
550    only on my turn<input type="radio" name="update" value="turn" /> <br />
551 <?php   
552              echo "<input type=\"hidden\" name=\"b\" value=\"$a\" />\n";
553              echo "\n";
554              echo "<input type=\"submit\" value=\"count me in\" />\n";
555              echo " </form>\n";
556            }
557        }
558
559 /*   ***
560  *   *  *
561  *   ***
562  *   *  *
563  *   ***
564  * yes? email him his hand, ask for solo, poverty, email every move or every card? 
565  */
566      if(isset($_REQUEST["b"]))
567        {
568          $b=$_REQUEST["b"];
569          
570          if( ereg("s",$player[$b]["option"])  && $game["init"]<4)
571            {
572              echo "just wait for the game to start";
573            }
574          else if( (!isset($_REQUEST["in"])|| !isset($_REQUEST["update"])) && !ereg("i",$player[$b]["option"]))
575            {
576              echo "go back to ";
577              echo "<a href=\"index.php?a=$b\"> here and fill out the form </a> <br />\n";
578            }
579          else
580            { /* show the hand */
581              if($_REQUEST["in"]=="no")
582                {
583                  for($i=0;$i<4;$i++)
584                    {
585                      $message = "Hello ".$player[$hash[$i]]["name"].",\n\n".
586                        "the game has been canceled due to the request of one of the players.\n";
587                      mymail($player[$hash[$i]]["email"],"[DoKo-Debug] the game has been canceled",$message); 
588                    }
589                    $output = fopen("status.txt","w");
590                    if($output)
591                      fclose($output);
592                    else
593                      echo "problem opening file";
594                }
595              else
596                {
597                  if($_REQUEST["update"]=="card") $player[$b]["option"].="c";
598                  if($_REQUEST["update"]=="turn") $player[$b]["option"].="t";
599                  
600                  $player[$b]["option"].="i";
601                  
602                  save_status();
603                  
604                  $allcards = $player[$b]["cards"];
605                  $mycards = explode(";",$allcards);
606                  
607                  sort($mycards);
608                  echo "your cards are <br />\n";
609                  foreach($mycards as $card) 
610                    {
611                      display_card($card);
612                    }
613                  echo "<br />\n";   
614  ?>
615  <p>aehm... at the moment poverty is not implented. so I guess you need to play a normal game, even if you have less than 3 turmp :(...sorry</p>                  
616  <form action="index.php" method="post">
617    
618    do you want to play solo? 
619    <select name="solo" size="1">
620      <option>No</option>
621      <option>No trump</option>
622      <option>Normal solo</option>
623      <option>Queen solo</option>
624      <option>Jack solo</option>
625      <option>Club solo</option>
626      <option>Spade solo</option>
627      <option>Heart solo</option>
628    </select>     
629    <br />
630
631 <?php   
632      
633                  echo "wedding?";
634                  if(check_wedding($player[$b]["cards"]))
635                    {
636                      echo " yes<input type=\"radio\" name=\"wedding\" value=\"yes\" />";
637                      echo " no <input type=\"radio\" name=\"wedding\" value=\"no\" /> <br />\n";
638                    }
639                  else
640                    {
641                      echo " no <input type=\"hidden\" name=\"wedding\" value=\"no\" /> <br />\n";
642                    };
643
644                  echo "do you have poverty?";
645                  if(count_trump($player[$b]["cards"])<4)
646                    {
647                      echo " yes<input type=\"radio\" name=\"poverty\" value=\"yes\" />";
648                      echo " no <input type=\"radio\" name=\"poverty\" value=\"no\" /> <br />\n";
649                    }
650                  else
651                    {
652                      echo " no <input type=\"hidden\" name=\"poverty\" value=\"no\" /> <br />\n";
653                    };
654
655                  echo "do you have too many nines?";
656                  if(count_nines($player[$b]["cards"])>4)
657                    {
658                      echo " yes<input type=\"radio\" name=\"nines\" value=\"yes\" />";
659                      echo " no <input type=\"radio\" name=\"nines\" value=\"no\" /> <br />\n";
660                    }
661                  else
662                    {
663                      echo " no <input type=\"hidden\" name=\"nines\" value=\"no\" /> <br />\n";
664                    };
665
666                  echo "<input type=\"hidden\" name=\"c\" value=\"$b\" />\n";
667                  echo "<input type=\"submit\" value=\"count me in\" />\n";
668                  
669                  echo "</form>\n";
670                }
671            }
672        }
673      if(isset($_REQUEST["c"]))
674        {
675          $c=$_REQUEST["c"];
676          
677          if(!isset($_REQUEST["solo"])|| !isset($_REQUEST["wedding"])|| !isset($_REQUEST["poverty"]) || !isset($_REQUEST["nines"]) )
678            {
679              echo "go back to ";
680              echo "<a href=\"index.php?b=$c\"> here and fill out the form </a> <br />\n";
681            }
682          else if(  ereg("s",$player[$c]["option"]) && $game["init"]<4 )
683            {
684              echo "just wait for the game to start";
685            }
686          else if($game["init"]<4)
687            { 
688              if( $_REQUEST["solo"]!="No")
689                {
690                  switch($_REQUEST["solo"])
691                    {
692                    case "No trump":
693                      $player[$c]["option"].="O";
694                      break;
695                    case "Normal solo":
696                      $player[$c]["option"].="S";
697                      break;
698                    case "Queen solo":
699                      $player[$c]["option"].="Q";
700                      break;
701                    case "Jack solo":
702                      $player[$c]["option"].="J";
703                      break;
704                    case "Club solo":
705                      $player[$c]["option"].="C";
706                      break;
707                    case "Spade solo":
708                      $player[$c]["option"].="A";
709                      break;
710                    case "Hear solo":
711                      $player[$c]["option"].="H";
712                      break;
713                    }
714                }
715              else if($_REQUEST["wedding"] == "yes")
716                {
717                  echo "wedding was chosen<br />\n";
718                  $player[$c]["option"].="W";
719                }
720              else if($_REQUEST["poverty"] == "yes")
721                {
722                  echo "poverty was chosen<br />\n";
723                  $player[$c]["option"].="P"; 
724                }
725              else if($_REQUEST["nines"] == "yes")
726                {
727                  echo "nines was chosen<br />\n";
728                  $player[$c]["option"].="N";
729                }
730              
731              $message = "You're in. Once everyone has filled out the form, ".
732                "the game will start and you'll get an eamil on your turn.\n";
733              mymail($player[$c]["email"],"[DoKo-debug] the game will start soon",$message); 
734
735              $player[$c]["option"].="s";
736              save_status();
737              if(file_exists("status.txt"))
738                $lines = file("status.txt");
739              else
740                die("no file");
741              parse_status();
742
743              if($game["init"]==4)
744                {
745                  $message = "The game can start now, it's your turn, please use this link to play a card:\n".
746                    $host."?me=".$hash[$game["startplayer"]]."\n";
747                  mymail($player[$hash[$game["startplayer"]]]["email"],"[DoKo-debug] let's go",$message);
748                }
749              
750            }
751        }
752      if($game["init"]==4)
753        {
754          /* check for sickness*/
755          /***** someone has 5 nines and no one is playing solo=> cancel game */
756          if($game["nines"]>=0 && $game["solo-who"]<0)
757            {
758              $message = $player[$hash[$game["poverty"]]]["nines"]." has more than 4 nines. Game aborted!\n";
759              for($i=0;$i<4;$i++)
760                {
761                  mymail($player[$hash[$i]]["email"],"[DoKo-debug] the game has been canceled",$message); 
762                };
763
764              $output = fopen("status.txt","w");
765              if($output)
766                fclose($output);
767              else
768                echo "problem opening file";
769            }
770
771          /* who is requesting this*/
772          if(!isset($_REQUEST["me"]))
773            {    
774              if(!isset($_REQUEST["recovery"]))
775                {
776                  echo "A game is in progress and kibitzing is not allowed. Sorry!.<br />\n";
777                  echo "In case you are playing, but lost your email or can't access the game anymore, please input your email here:<br />\n";
778                  ?>
779  <form action="index.php" method="post">
780    recorvery: <input name="recovery"  type="text" size="20" maxlength="30" /> <br />
781    <input type="submit" value="get me back into the game" />
782  </form>
783 <?php
784                }
785              else
786                {
787                  $recovery=$_REQUEST["recovery"];
788                  $ok=-1;
789                  for($i=0;$i<4;$i++)
790                    if(trim($recovery)==trim($player[$hash[$i]]["email"]))
791                      $ok=$i;
792                  if($ok>=0)
793                    {
794                      $message = "Please try this link: ".$host."?me=".$hash[$ok]."\n".
795                        "\n if this doesn't work, contact the admin.\n";
796                      mymail($recovery,"[DoKo-Debug] recovery ",$message);
797                      echo "email has been sent.";
798                    }
799                  else
800                    {
801                      echo "can't find this email address, sorry.";
802                    }; 
803                } /* end recovery */
804            }
805          else
806            { /* $me is set */ 
807              $me = $_REQUEST["me"];
808              
809              /* output if we are playing a solo or a wedding */
810              if($game["solo-who"]>=0)
811                echo $player[$hash[$game["solo-who"]]]["name"]." is playing a ".$game["solo-what"]." solo!<br />\n";
812              else if($game["wedding"]>=0)
813                echo $player[$hash[$game["wedding"]]]["name"]." is playing a wedding!<br />\n";
814              
815              /* show history */
816              foreach($history as $play) 
817                {
818                  $trick = explode(":",$play);
819                  
820                  $last=-2;
821                  /* found old trick, display it */
822                  if(sizeof($trick)==5)
823                    echo "<div class=\"oldtrick background".$play[0]."\"><div class=\"table\">\n";
824                  else
825                    echo "<div class=\"trick back".$play[0]."\"><div class=\"table\">\n";
826                  for($i=0;$i<sizeof($trick)-1;$i++)
827                    {
828                      $card = $trick[$i];
829                      if(ereg("->",$card))
830                        {
831                          $tmp = explode("->",$card);
832
833                          echo "<div class=\"card".$tmp[0]."\">";
834
835                          if(strlen($tmp[2])>0)
836                            echo "<span class=\"comment\">";
837                          else
838                            echo " <span>";
839                          echo $player[$hash[$tmp[0]]]["name"];
840                          /* check for comment */
841                          if(strlen($tmp[2])>0)
842                            echo " <span>".$tmp[2]."</span>";
843                          echo " </span>\n  ";
844         
845                          display_card($tmp[1]);
846
847                          $last = $tmp[0];
848                          echo "</div>\n";
849                        }
850                    }
851                  echo "</div></div>\n";
852                }
853              
854              /* figure out who needs to play next */
855              $next = $last + 1;
856              if ($next>=4) 
857                $next -= 4 ;
858              if($last<0)
859                {
860                  $next = $history[sizeof($history)-1][0];
861                }
862              
863              /* are we trying to play a card? */
864              if(isset($_REQUEST["card"]))
865                {
866                  if($hash[$next]==$me)
867                    {
868                      $card    = $_REQUEST["card"];
869                      $mycards = explode(";",$player[$me]["cards"]);
870                      
871                      /* do we have that card */
872                      if(in_array($card,$mycards))
873                        {
874                          /* delete card from array */
875                          $tmp = array();
876                          foreach($mycards as $m)
877                            if($m != $card)
878                              $tmp[]=$m;
879                          
880                          $tmp2="";
881                          for($i=0;$i<sizeof($tmp)-1;$i++)
882                            {
883                              $tmp2.=$tmp[$i].";";
884                            }
885                          $tmp2.=$tmp[$i];
886                          $player[$me]["cards"]=$tmp2;
887                          
888                          /* add card to history, special case if this is the first card */
889                          if($last<0)
890                            {
891                              $history[sizeof($history)-1]="".$player[$me]["number"]."->".$card.":\n";
892                            }
893                          else
894                            {
895                              $tmp = explode(":",$history[sizeof($history)-1]);
896                              $tmp[sizeof($tmp)-1] = "".$player[$me]["number"]."->".$card.":";
897                              $history[sizeof($history)-1]=join(":",$tmp);
898                            }
899                          save_status();
900                          
901                          echo "<div class=\"card\">";
902                          echo " you played  <br />";
903                          display_card($card);
904                          echo "</div>\n";
905
906                          ?>
907 <br clear="both" />
908 <form action="index.php" method="post">
909    A short comment:<input name="comment" type="text" size="30" maxlength="50" /> 
910    <input type="hidden" name="me" value="<?php echo $me; ?>" />
911    <input type="submit" value="submit comment" />
912 </form>
913 <?php
914                          /* send out email to players who want/need to get informed */
915                          /* check if we are in a trick, if trick is done, this needs to be handelt in the
916                           * who-won-the-trick section further down */
917                          $tmp = explode(":",$history[sizeof($history)-1]);
918                          if(sizeof($tmp)<5)
919                            for($i=0;$i<4;$i++)
920                              {
921                                $mynext = $next+1; if($mynext>3)$mynext-=4;
922                                
923                                if((ereg("c",$player[$hash[$i]]["option"]) || $i==$mynext) && $hash[$i]!=$me)
924                                  {
925                                    $message = " Hello ".$player[$hash[$i]]["name"].",\n\n";
926                                    
927                                    if($i==$mynext)
928                                      {
929                                        $message .= "it's your turn  now.\n".
930                                          "Use this link to play a card: ".$host."?me=".$hash[$i]."\n\n" ;
931                                      }
932                                    $message .= $player[$me]["name"]." has played the following card ".
933                                      card_to_name($card)."\n";
934                                    
935                                    if($game["solo-who"]>=0)
936                                      $message .= $player[$hash[$game["solo-who"]]]["name"]." is playing a ".
937                                        $game["solo-what"]." solo!\n";
938                                    
939                                    mymail($player[$hash[$i]]["email"],"[DoKo-debug] a card has been played",$message);
940                                    
941                                    if($debug)
942                                      echo "<a href=\"index.php?me=".$hash[$mynext]."\"> next player </a> <br />\n";
943                                  }
944                              }
945                        }
946                      else
947                        echo "seems like you don't have that card<br />\n";
948                      
949                    }
950                  
951                } /* end if card is set */
952              else if(isset($_REQUEST["comment"]))
953                {
954                  $comment = $_REQUEST["comment"];
955                  $tmp = explode(":",$history[sizeof($history)-1]);
956                  $tmp2 = explode("->",$tmp[sizeof($tmp)-2]);
957                  
958                  $comment = str_replace(":","",$comment);
959
960                  if(sizeof($tmp2)<=2)
961                    $tmp[sizeof($tmp)-2] .= "->".$comment;
962                  $history[sizeof($history)-1]=join(":",$tmp);
963
964                  save_status();
965                }
966              else if(isset($_REQUEST["win"]) && strlen($history[sizeof($history)-1])>3)
967                {
968                  $win       = $_REQUEST["win"];
969                  
970                  if(strlen($player[$hash[0]]["cards"]))
971                     $history[] = $win.":\n";
972
973                  /* email the player who needs to move next*/
974                  for($i=0;$i<4;$i++)
975                    {
976                      if((ereg("c",$player[$hash[$i]]["option"]) || $i==$win) )
977                        {
978                          $message = " Hello ".$player[$hash[$i]]["name"].",\n\n";
979                          
980                          if($i == $win)
981                            {
982                              $message .= "You won the last trick,it's your turn  now.\n".
983                                "Use this link to play a card: ".$host."?me=".$hash[$i]."\n\n" ;
984                            }
985                          else
986                            $message .= $player[$hash[$win]]["name"]." has won the last trick\n".
987                              "Use this link to look at the game: ".$host."?me=".$hash[$i]."\n\n" ;
988                          
989                          if($game["solo-who"]>=0)
990                            $message.= $player[$hash[$game["solo-who"]]]["name"]." is playing a ".
991                              $game["solo-what"]." solo!\n";
992                          
993                          mymail($player[$hash[$i]]["email"],"[DoKo-debug] a card has been played",$message);
994                          
995                          if($debug)
996                            echo "<a href=\"index.php?me=".$hash[$win]."\"> next player </a> <br />\n";
997                        }
998                    }
999                  
1000                  
1001                  /* count points of the last trick */
1002                  $points = 0;
1003
1004                  $tmp = explode(":",$history[sizeof($history)-2]);
1005                  for($i=0;$i<4;$i++)
1006                    {
1007                      $tmp2 = explode("->",$tmp[$i]);
1008                      $c = $tmp2[1];
1009                      $points += card_value($c);
1010                    }
1011                  $player[$hash[$win]]["points"] += $points;
1012                  echo "<br />\n ".$player[$hash[$win]]["name"]." won: $points Points <br />\n";
1013                  
1014                  save_status();
1015                }; /* end if win is set */
1016              echo "<br />\n";
1017
1018              $tmp = explode(":",$history[sizeof($history)-1]);
1019
1020              /* check last history entry: end of a trick? ask who won it, unless it was the last trick */
1021              if(sizeof($tmp)==5 && strlen($player[$hash[0]]["cards"]))
1022                {
1023                  ?>
1024 <form action="index.php" method="post">
1025 who won?
1026 <?php 
1027    for($i=0;$i<4;$i++)
1028      echo $player[$hash[$i]]["name"]." <input type=\"radio\" name=\"win\" value=\"$i\" />";
1029    echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />";
1030 ?>
1031 <input type="submit" value="submit" />
1032
1033 </form>
1034 <?php
1035                }
1036              else if(sizeof($tmp)<5 && 1<sizeof($tmp) && !isset($_REQUEST["card"]))
1037                {                 
1038                  if(sizeof($tmp)==2 && strlen($tmp[0])==1)
1039                    {
1040                      $next=$tmp[0];
1041                      
1042                      if($debug)
1043                        echo "DEBUG: the next move is for <a href=\"index.php?me=".$hash[$next]."\">the next player</a><br />\n";
1044                      
1045                      if(strlen(trim($player[$me]["cards"]))==0)
1046                        {
1047                          echo "<br /> game over, count points <br />\n";
1048                          for($i=0;$i<4;$i++)
1049                            {
1050                              echo $player[$hash[$i]]["name"]." got ".$player[$hash[$i]]["points"]."<br />\n";
1051                            }
1052                        }
1053                    }
1054                  echo "<br />\n";
1055                } /* end check for winner */
1056              
1057              /* do we still have cards? display them */
1058              if(strlen(trim($player[$me]["cards"]))>0 )
1059                {
1060                  $allcards = trim($player[$me]["cards"]);
1061                  $mycards = explode(";",$allcards);
1062                  
1063                  sort($mycards);
1064                  
1065                  echo "<p class=\"mycards\">\n";
1066                  /* is it our turn? */
1067                  if($hash[$next]==$me && !isset($_REQUEST["card"]) && !isset($_REQUEST["win"])) 
1068                    {
1069                      echo "ITS YOUR TURN   <br />\n";
1070                      echo "your cards are: <br />\n";
1071                      foreach($mycards as $card) 
1072                        {
1073                          display_link_card($card,$me);
1074                        }
1075                    }
1076                  else 
1077                    { /* not our turn, just show the hand */
1078                      echo "your cards are: <br />\n";
1079                      foreach($mycards as $card) 
1080                        {
1081                          display_card($card);
1082                        }
1083                    }
1084                  echo "</p>\n";   
1085                }
1086            }
1087        }
1088
1089  } 
1090
1091 ?>
1092 </body>
1093 </html>