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