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