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