moved css to new directory
[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="http://doko.nubati.net/css/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 $base  = "http://doko.nubati.net/";
44 $host  = $base."index.php";
45 $wiki  = "http://wiki.nubati.net/index.php?title=EmailDoko";
46 $debug = 0;
47
48 $last=-2;
49
50 /*
51  * end config
52  */     
53
54 echo "</div>\n";
55
56 /* end header */
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=\"".$base."cards/".$card.".png\"  alt=\"".card_to_name($card)."\" />\n";
403   else
404     echo "<img src=\"".$base."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=\"".$base."cards/".$card.".png\" alt=\"\" />\n";
413   else
414     echo "<input type=\"radio\" name=\"card\" value=\"".$card."\" /><img src=\"".$base."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 $history=array();
484
485 /* check for status file and read it, if possible */
486
487 if(file_exists("status.txt"))
488   $lines = file("status.txt");
489 else
490   die("no file");
491
492 /* check if we want to start a new game */
493 if( isset($_REQUEST["PlayerA"]) && 
494     isset($_REQUEST["PlayerB"]) && 
495     isset($_REQUEST["PlayerC"]) && 
496     isset($_REQUEST["PlayerD"]) && 
497     isset($_REQUEST["EmailA"]) && 
498     isset($_REQUEST["EmailB"]) && 
499     isset($_REQUEST["EmailC"]) && 
500     isset($_REQUEST["EmailD"]) && sizeof($lines<2))
501   {
502     $PlayerA = $_REQUEST["PlayerA"];
503     $PlayerB = $_REQUEST["PlayerB"];
504     $PlayerC = $_REQUEST["PlayerC"];
505     $PlayerD = $_REQUEST["PlayerD"];
506     $EmailA  = $_REQUEST["EmailA"] ;
507     $EmailB  = $_REQUEST["EmailB"] ;
508     $EmailC  = $_REQUEST["EmailC"] ;
509     $EmailD  = $_REQUEST["EmailD"] ;
510     
511     $hashA = md5("AGameOfDoko".$PlayerA.$EmailA);
512     $hashB = md5("AGameOfDoko".$PlayerB.$EmailB);
513     $hashC = md5("AGameOfDoko".$PlayerC.$EmailC);
514     $hashD = md5("AGameOfDoko".$PlayerD.$EmailD);
515
516     /* send out email, check for error with email */
517
518     $message = "\n".
519       "you are invited to play a game of DoKo (that is to debug the program ;).\n".
520       "Place comments and bug reports here:\n".
521       "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
522       "The whole round would consist of the following players:\n".
523       "$PlayerA\n".
524       "$PlayerB\n".
525       "$PlayerC\n".
526       "$PlayerD\n\n".
527       "If you want to join this game, please follow this link:\n\n".
528       " ".$host."?a=";
529     
530     mymail($EmailA,"You are invited to a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
531     mymail($EmailB,"You are invited to a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
532     mymail($EmailC,"You are invited to a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
533     mymail($EmailD,"You are invited to a game of DoKo","Hello $PlayerD,\n".$message.$hashD);
534     
535     /* read in random.txt */
536     if(file_exists("random.txt"))
537       $random = file("random.txt");
538     else
539       die("no random file");
540  
541     $randomNR = explode( ":", $random[4] );
542     
543     /* write initial status into file */
544     $output = fopen("status.txt","w");
545     if ($output)
546       {
547         fwrite($output, "$hashA:$PlayerA:$EmailA:::" );
548         for($i=0;$i<11;$i++)
549           fwrite($output,"$randomNR[$i];" );
550         fwrite($output,"$randomNR[11]:" ); $i++;
551         fwrite($output,"\n");
552         
553         fwrite($output, "$hashB:$PlayerB:$EmailB:::" );
554         for(;$i<23;$i++)
555           fwrite($output,"$randomNR[$i];" );
556         fwrite($output,"$randomNR[23]:" ); $i++;
557         fwrite($output,"\n");
558         
559         fwrite($output, "$hashC:$PlayerC:$EmailC:::" );
560         for(;$i<35;$i++)
561           fwrite($output,"$randomNR[$i];" );
562         fwrite($output,"$randomNR[35]:" ); $i++;
563         fwrite($output,"\n");
564         
565         fwrite($output, "$hashD:$PlayerD:$EmailD:::");
566         for(;$i<47;$i++)
567           fwrite($output,"$randomNR[$i];" );
568         fwrite($output,"$randomNR[47]:" );
569         fwrite($output,"\n");
570         
571         fclose($output);
572       }
573     else
574       echo "can't open file for writing";
575   };
576 /* reread file */
577 if(file_exists("status.txt"))
578   $lines = file("status.txt");
579  else
580    die("no file");
581
582 /* test if a game is running, else output everything for a new game */
583 if(sizeof($lines)<2)
584   {
585 ?>
586     <p> no game in progress, please input 4 names and email addresses, please make sure that the addresses are correct! </p>
587  <form action="index.php" method="post">
588    Name:  <input name="PlayerA" type="text" size="10" maxlength="20" /> 
589    Email: <input name="EmailA"  type="text" size="20" maxlength="30" /> <br />
590
591    Name:  <input name="PlayerB" type="text" size="10" maxlength="20" /> 
592    Email: <input name="EmailB"  type="text" size="20" maxlength="30" /> <br />
593
594    Name:  <input name="PlayerC" type="text" size="10" maxlength="20" /> 
595    Email: <input name="EmailC"  type="text" size="20" maxlength="30" /> <br />
596
597    Name:  <input name="PlayerD" type="text" size="10" maxlength="20" /> 
598    Email: <input name="EmailD"  type="text" size="20" maxlength="30" /> <br />
599
600    <input type="submit" value="start game" />
601  </form>
602 <?php
603    }
604 else
605   { /* load game status */
606     parse_status();
607 /*     **
608  *    *  *
609  *    ****
610  *    *  *
611  *
612  * check if a player wants to accept a game 
613  */
614     if(isset($_REQUEST["a"]))
615       {
616         $a = $_REQUEST["a"];
617         
618         if( ereg("[is]",$player[$a]["option"]) &&  $game["init"]<4)
619           {
620             echo "just wait for the game to start";
621           }
622         else if( !ereg("[is]",$player[$a]["option"]) )
623           {
624  ?>
625  <form action="index.php" method="post">
626    Do you want to play a game of DoKo?
627    yes<input type="radio" name="in" value="yes" />
628    no<input type="radio" name="in" value="no" /> <br />
629
630    Do you want to get an email for every card played or only if it your move?
631    every card<input type="radio" name="update" value="card" />
632    only on my turn<input type="radio" name="update" value="turn" /> <br />
633 <?php   
634              echo "<input type=\"hidden\" name=\"b\" value=\"$a\" />\n";
635              echo "\n";
636              echo "<input type=\"submit\" value=\"count me in\" />\n";
637              echo " </form>\n";
638            }
639        }
640 /*   ***
641  *   *  *
642  *   ***
643  *   *  *
644  *   ***
645  * yes? email him his hand, ask for solo, poverty, email every move or every card? 
646  */
647      if(isset($_REQUEST["b"]))
648        {
649          $b = $_REQUEST["b"];
650          
651          if( ereg("s",$player[$b]["option"])  && $game["init"]<4)
652            { /* the player already filled out everything */
653              echo "just wait for the game to start";
654            }
655          else if( (!isset($_REQUEST["in"])|| !isset($_REQUEST["update"])) && !ereg("i",$player[$b]["option"]))
656            { /* the player didn't fill out the form at "a" correctly */
657              echo "go back to ";
658              echo "<a href=\"index.php?a=$b\"> here and fill out the form </a> <br />\n";
659            }
660          else
661            { /* show the hand and check if the player is sick*/
662              if($_REQUEST["in"]=="no")
663                { /* player doesn't want to play, cancel the game */
664                  for($i=0;$i<4;$i++)
665                    {
666                      $message = "Hello ".$player[$hash[$i]]["name"].",\n\n".
667                        "the game has been canceled due to the request of one of the players.\n";
668                      mymail($player[$hash[$i]]["email"],"[DoKo-Debug] the game has been canceled",$message); 
669                    }
670                  /* canceling the game is as easy as removing the contents of the status file*/
671                  $output = fopen("status.txt","w");
672                  if($output)
673                    fclose($output);
674                  else
675                    echo "problem opening file";
676                }
677              else
678                {
679                  /* player wants to play, save information from "a"*/
680                  if($_REQUEST["update"]=="card") 
681                    $player[$b]["option"] .= "c";
682                  else
683                    $player[$b]["option"] .= "t";
684                  
685                  $player[$b]["option"] .= "i"; /* player finished stage "a" */
686                  
687                  save_status();
688                  
689                  $allcards = $player[$b]["cards"];
690                  $mycards  = explode(";",$allcards);
691                  
692                  sort($mycards);
693                  echo "<p class=\"mycards\">your cards are: <br />\n";
694                  foreach($mycards as $card) 
695                    display_card($card);
696                  echo "</p>\n";   
697  ?>
698  <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>                
699
700  <form action="index.php" method="post">
701
702    do you want to play solo? 
703    <select name="solo" size="1">
704      <option>No</option>
705      <option>No trump</option>
706      <option>Normal solo</option>
707      <option>Queen solo</option>
708      <option>Jack solo</option>
709      <option>Club solo</option>
710      <option>Spade solo</option>
711      <option>Heart solo</option>
712    </select>     
713    <br />
714
715 <?php   
716      
717                  echo "wedding?";
718                  if(check_wedding($player[$b]["cards"]))
719                    {
720                      echo " yes<input type=\"radio\" name=\"wedding\" value=\"yes\" />";
721                      echo " no <input type=\"radio\" name=\"wedding\" value=\"no\" /> <br />\n";
722                    }
723                  else
724                    {
725                      echo " no <input type=\"hidden\" name=\"wedding\" value=\"no\" /> <br />\n";
726                    };
727
728                  echo "do you have poverty?";
729                  if(count_trump($player[$b]["cards"])<4)
730                    {
731                      echo " yes<input type=\"radio\" name=\"poverty\" value=\"yes\" />";
732                      echo " no <input type=\"radio\" name=\"poverty\" value=\"no\" /> <br />\n";
733                    }
734                  else
735                    {
736                      echo " no <input type=\"hidden\" name=\"poverty\" value=\"no\" /> <br />\n";
737                    };
738
739                  echo "do you have too many nines?";
740                  if(count_nines($player[$b]["cards"])>4)
741                    {
742                      echo " yes<input type=\"radio\" name=\"nines\" value=\"yes\" />";
743                      echo " no <input type=\"radio\" name=\"nines\" value=\"no\" /> <br />\n";
744                    }
745                  else
746                    {
747                      echo " no <input type=\"hidden\" name=\"nines\" value=\"no\" /> <br />\n";
748                    };
749
750                  echo "<input type=\"hidden\" name=\"c\" value=\"$b\" />\n";
751                  echo "<input type=\"submit\" value=\"count me in\" />\n";
752                  
753                  echo "</form>\n";
754                }
755            }
756        }
757
758      if(isset($_REQUEST["c"]))
759        {
760          $c = $_REQUEST["c"];
761          
762
763          if( ereg("s",$player[$c]["option"]) && $game["init"]<4 )
764            { /* the player already filled out everything */
765              echo "<p>just wait for the game to start</p>\n";
766            }
767          else if(!isset($_REQUEST["solo"])    || 
768                  !isset($_REQUEST["wedding"]) ||
769                  !isset($_REQUEST["poverty"]) ||
770                  !isset($_REQUEST["nines"]) )
771            {/* player still needs to fill out the form */
772              echo "go back to ";
773              echo "<a href=\"index.php?b=$c\"> here and fill out the form </a> <br />\n";
774            }
775          else if($game["init"]<4)
776            { /* save information */
777              if( $_REQUEST["solo"]!="No")
778                {
779                  switch($_REQUEST["solo"])
780                    {
781                    case "No trump":
782                      $player[$c]["option"].="O";
783                      break;
784                    case "Normal solo":
785                      $player[$c]["option"].="S";
786                      break;
787                    case "Queen solo":
788                      $player[$c]["option"].="Q";
789                      break;
790                    case "Jack solo":
791                      $player[$c]["option"].="J";
792                      break;
793                    case "Club solo":
794                      $player[$c]["option"].="C";
795                      break;
796                    case "Spade solo":
797                      $player[$c]["option"].="A";
798                      break;
799                    case "Hear solo":
800                      $player[$c]["option"].="H";
801                      break;
802                    }
803                }
804              else if($_REQUEST["wedding"] == "yes")
805                {
806                  echo "wedding was chosen<br />\n";
807                  $player[$c]["option"].="W";
808                }
809              else if($_REQUEST["poverty"] == "yes")
810                {
811                  echo "poverty was chosen<br />\n";
812                  $player[$c]["option"].="P"; 
813                }
814              else if($_REQUEST["nines"] == "yes")
815                {
816                  echo "nines was chosen<br />\n";
817                  $player[$c]["option"].="N";
818                }
819              
820              /* player finished setup */
821              $player[$c]["option"].="s";
822
823              save_status();
824              /* reread status file, to get the correct startplayer, etc */
825              if(file_exists("status.txt"))
826                $lines = file("status.txt");
827              else
828                die("no file");
829              parse_status();
830
831              if($game["init"]==4 && $player[$c]["number"]==$game["startplayer"])
832                {
833                  echo "<p> The game can start now, it's your turn, please use this <a href=\"".
834                    $host."?me=".$hash[$c]."\">link</a> to play a card.</p>\n";
835                }
836              else if($game["init"]==4)
837                {
838                  $message = "The game can start now, it's your turn, please use this link to play a card:\n".
839                    $host."?me=".$hash[$game["startplayer"]]."\n";
840                  mymail($player[$hash[$game["startplayer"]]]["email"],"[DoKo-debug] let's go",$message);
841                  echo "<p> The game has started. An email has been sent out to the first player.</p>\n";
842                }
843              else
844                {
845                  echo "<p>You're in. Once everyone has filled out the form, ".
846                    "the game will start and you'll get an eamil on your turn.</p>\n";
847                }
848            }
849        }
850      /* END SETUP */
851
852      /* the game */
853      if($game["init"]==4)
854        {
855          /* check for sickness, only would need to do this on the first trick really...*/
856          /***** someone has 5 nines and no one is playing solo => cancel game */
857          if($game["nines"]>=0 && $game["solo-who"]<0)
858            {
859              $message = $player[$hash[$game["poverty"]]]["nines"]." has more than 4 nines. Game aborted!\n";
860              for($i=0;$i<4;$i++)
861                mymail($player[$hash[$i]]["email"],"[DoKo-debug] the game has been canceled",$message); 
862              
863              $output = fopen("status.txt","w");
864              if($output)
865                fclose($output);
866              else
867                echo "problem opening file";
868            };
869          
870          /* who is requesting this*/
871          if(!isset($_REQUEST["me"]))
872            {    
873              if(!isset($_REQUEST["recovery"]))
874                {
875                  echo "A game is in progress and kibitzing is not allowed. Sorry!.<br />\n";
876                  echo "In case you are playing, but lost your email or can't access the game anymore, please input your email here:<br />\n";
877                  ?>
878  <form action="index.php" method="post">
879    recorvery: <input name="recovery"  type="text" size="20" maxlength="30" /> <br />
880    <input type="submit" value="get me back into the game" />
881  </form>
882 <?php
883                }
884              else
885                {
886                  $recovery = $_REQUEST["recovery"];
887                  $ok = -1;
888                  for($i=0;$i<4;$i++)
889                    if(trim($recovery)==trim($player[$hash[$i]]["email"]))
890                      $ok = $i;
891                  if($ok>=0)
892                    {
893                      $message = "Please try this link: ".$host."?me=".$hash[$ok]."\n".
894                        "\n if this doesn't work, contact the admin.error4\n";
895                      mymail($recovery,"[DoKo-Debug] recovery ",$message);
896                      echo "<p> An email with the game information has been sent.</p>\n";
897                    }
898                  else
899                    {
900                      echo "<p> can't find this email address, sorry.</p>\n";
901                    }; 
902                } /* end recovery */
903            }
904          else
905            { /* $me is set */ 
906              $me = $_REQUEST["me"];
907              
908              /* show history */
909              /* old tricks as list */
910              echo "<ul class=\"oldtrick\">\n";
911              echo "  <li> History: </li>\n";
912              $j=0;
913              foreach($history as $play) 
914                {
915                  $j++;
916                  $trick = explode(":",$play);
917                  
918                  /* found old trick, display it */
919                  if(sizeof($trick)==5)
920                    echo "  <li onclick=\"hl('$j');\"><a href=\"#\">Trick $j</a>\n    <div class=\"table\" id=\"trick".$j."\">\n      <img class=\"table\" src=\"".$base."pics/table".$play[0].".png\" alt=\"table\" />\n";
921                  else
922                    {
923                      /* display current trick */
924                      echo "<li onclick=\"hl('$j');\"><a href=\"#\">Current Trick</a>\n  <div class=\"table\" id=\"trick".$j."\">\n      <img class=\"table\" src=\"".$base."pics/table".$play[0].".png\" alt=\"table\" />";
925                    }
926                  for($i=0;$i<sizeof($trick)-1;$i++)
927                    {
928                      $card = $trick[$i];
929
930                      $last=-2;
931                      /* has a card been played? */
932                      if(ereg("->",$card))
933                        {
934                          $tmp = explode("->",$card);
935
936                          echo "      <div class=\"card".$tmp[0]."\">\n";
937
938                          if(strlen($tmp[2])>0)
939                            echo "        <span class=\"comment\">";
940                          else
941                            echo "        <span>";
942                          echo $player[$hash[$tmp[0]]]["name"];
943                          /* check for comment */
944                          if(strlen($tmp[2])>0)
945                            echo "<span>".$tmp[2]."</span>";
946                          echo "</span>\n        ";
947         
948                          display_card($tmp[1]);
949
950                          $last = $tmp[0];
951                          echo "      </div>\n";
952                        }
953                    }
954                  
955                  echo "    </div>\n  </li>\n";
956                }
957              echo "</ul> <hr />\n";
958
959              echo "<div class=\"line\"></div>";
960
961              /* output if we are playing a solo or a wedding */
962              echo "<div class=\"info\">";
963              if($game["solo-who"]>=0)
964                echo $player[$hash[$game["solo-who"]]]["name"]." is playing a ".$game["solo-what"]." solo!<br />\n";
965              else if($game["wedding"]>=0)
966                echo $player[$hash[$game["wedding"]]]["name"]." is playing a wedding!<br />\n";
967              echo "</div>";
968              echo "<div class=\"bug\"> Poverty not working yet! <br />".
969                " Schweinchen not working yet <br />".
970                "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 />".
971                "If you find more bugs, please list them in the <a href=\"".$wiki.
972                "\">wiki</a>.Hit shift reload every now and then too.</div>\n";
973
974
975              /* figure out who needs to play next */
976              $next = $last + 1;
977              if ($next>=4) 
978                $next -= 4 ;
979
980              /* if no one has played yet or we are at the start of a new trick */
981              if($last<0)
982                $next = $history[sizeof($history)-1][0];
983              
984              /* are we trying to play a card? */
985              if(isset($_REQUEST["card"]))
986                {
987                  if($hash[$next]==$me)
988                    {
989                      $card    = $_REQUEST["card"];
990                      $mycards = explode(";",$player[$me]["cards"]);
991                      $comment = $_REQUEST["comment"];
992                      $comment = str_replace(":","",$comment);           /*can't have ":" in comments */
993                      
994                      /* do we have that card */
995                      if(in_array($card,$mycards))
996                        {
997                          /* delete card from array */
998                          $tmp = array();
999                          foreach($mycards as $m)
1000                            if($m != $card)
1001                              $tmp[]=$m;
1002                          
1003                          $tmp2="";
1004                          for($i=0;$i<sizeof($tmp)-1;$i++)
1005                            {
1006                              $tmp2.=$tmp[$i].";";
1007                            }
1008                          $tmp2.=$tmp[$i];
1009                          $player[$me]["cards"]=$tmp2;
1010                          
1011                          /* add card to history, special case if this is the first card */
1012                          if($last<0)
1013                            {
1014                              $history[sizeof($history)-1]="".$player[$me]["number"]."->".$card."->$comment:\n";
1015                            }
1016                          else
1017                            {
1018                              $tmp = explode(":",$history[sizeof($history)-1]);
1019                              $tmp[sizeof($tmp)-1] = "".$player[$me]["number"]."->".$card."->$comment:";
1020                              $history[sizeof($history)-1]=join(":",$tmp);
1021                            }
1022                          save_status();
1023                          
1024                          echo "<div class=\"card\">";
1025                          echo " you played  <br />";
1026                          display_card($card);
1027                          echo "</div>\n";
1028
1029                          /* send out email to players who want/need to get informed */
1030                          /* check if we are in a trick, if trick is done, this needs to be handelt in the
1031                           * who-won-the-trick section further down */
1032                          $tmp = explode(":",$history[sizeof($history)-1]);
1033                          if(sizeof($tmp)<5)
1034                            for($i=0;$i<4;$i++)
1035                              {
1036                                $mynext = $next+1; if($mynext>3)$mynext-=4;
1037                                
1038                                if((ereg("c",$player[$hash[$i]]["option"]) || $i==$mynext) && $hash[$i]!=$me)
1039                                  {
1040                                    $message = " Hello ".$player[$hash[$i]]["name"].",\n\n";
1041                                    
1042                                    if($i==$mynext)
1043                                      {
1044                                        $message .= "it's your turn  now.\n".
1045                                          "Use this link to play a card: ".$host."?me=".$hash[$i]."\n\n" ;
1046                                      }
1047                                    $message .= $player[$me]["name"]." has played the following card ".
1048                                      card_to_name($card)."\n";
1049                                    
1050                                    if($game["solo-who"]>=0)
1051                                      $message .= $player[$hash[$game["solo-who"]]]["name"]." is playing a ".
1052                                        $game["solo-what"]." solo!\n";
1053                                    
1054                                    mymail($player[$hash[$i]]["email"],"[DoKo-debug] a card has been played",$message);
1055                                    
1056                                    if($debug)
1057                                      echo "<a href=\"index.php?me=".$hash[$mynext]."\"> next player </a> <br />\n";
1058                                  }
1059                              }
1060                        }
1061                      else
1062                        echo "seems like you don't have that card<br />\n";
1063                    }
1064                } /* end if card is set */
1065             if(substr_count($history[sizeof($history)-1],":")==4)
1066                { /* count points, email winner */
1067                  $p = array();
1068                  $c = array();
1069
1070                  $tmp  = explode(":",$history[sizeof($history)-1]); /*last played trick */
1071
1072                  /* get player and cards of last trick */
1073                  for($i=0;$i<4;$i++)
1074                    {
1075                      $tmp2 = explode("->",$tmp[$i]);
1076                      $p[] = $tmp2[0];
1077                      $c[] = $tmp2[1];
1078                    };
1079                  
1080                  $win = $p[get_winner($c)];
1081                                  
1082                  if(strlen($player[$hash[0]]["cards"]))
1083                    $history[] = "\n".$win.":\n"; /* not sure why I need the first \n here */
1084
1085                  /* check if game is done */
1086                  $end = 1;
1087                  for($i=0;$i<4;$i++)
1088                    if(strlen(trim($player[$hash[$i]]["cards"]))!=0)
1089                      $end = 0;
1090
1091                  /* email the player who needs to move next*/
1092                  if(!$end)
1093                  for($i=0;$i<4;$i++)
1094                    {
1095                      if((ereg("c",$player[$hash[$i]]["option"]) || $i==$win) )
1096                        {
1097                          $message = " Hello ".$player[$hash[$i]]["name"].",\n\n";
1098                          
1099                          if($i == $win)
1100                            {
1101                              $message .= "You won the last trick,it's your turn  now.\n".
1102                                "Use this link to play a card: ".$host."?me=".$hash[$i]."\n\n" ;
1103                            }
1104                          else
1105                            $message .= $player[$hash[$win]]["name"]." has won the last trick\n".
1106                              "Use this link to look at the game: ".$host."?me=".$hash[$i]."\n\n" ;
1107                          
1108                          if($game["solo-who"]>=0)
1109                            $message.= $player[$hash[$game["solo-who"]]]["name"]." is playing a ".
1110                              $game["solo-what"]." solo!\n";
1111                          
1112                          mymail($player[$hash[$i]]["email"],"[DoKo-debug] a card has been played",$message);
1113                          
1114                          if($debug)
1115                            echo "<a href=\"index.php?me=".$hash[$win]."\"> next player </a> <br />\n";
1116                        }
1117                    }
1118                  
1119                  /* count points of the last trick */
1120                  $points = 0;
1121
1122                  $tmp = explode(":",$history[sizeof($history)-2]);
1123                  for($i=0;$i<4;$i++)
1124                    {
1125                      $tmp2 = explode("->",$tmp[$i]);
1126                      $c = $tmp2[1];
1127                      $points += card_value($c);
1128                    }
1129                  $player[$hash[$win]]["points"] += $points;
1130                  echo "<br />\n ".$player[$hash[$win]]["name"]." won: $points Points <br />\n";
1131                  
1132                  save_status();
1133                }; /* end if win is set */
1134              echo "<br />\n";
1135
1136              /* check if game is done */
1137              $end = 1;
1138              for($i=0;$i<4;$i++)
1139                if(strlen(trim($player[$hash[$i]]["cards"]))!=0)
1140                  $end = 0;
1141              
1142              if($end)
1143                { 
1144                  echo "<br /> game over, count points <br />\n";
1145                  for($i=0;$i<4;$i++)
1146                    {
1147                      echo $player[$hash[$i]]["name"]." got ".$player[$hash[$i]]["points"]."<br />\n";
1148                    }
1149                }
1150              /* check end of game */
1151   
1152              /* do we still have cards? display them */
1153              if(strlen(trim($player[$me]["cards"]))>0 )
1154                {
1155                  $allcards = trim($player[$me]["cards"]);
1156                  $mycards  = explode(";",$allcards);
1157                  
1158                  sort($mycards);
1159                  
1160                  echo "<div class=\"mycards\">\n";
1161                  /* is it our turn? */
1162                  if($hash[$next]==$me && !isset($_REQUEST["card"])) 
1163                    {
1164                      echo "Hello ".$player[$me]["name"].", it's your turn!  <br />\n";
1165                      echo "Your cards are: <br />\n";
1166                      echo "<form action=\"index.php\" method=\"post\">\n";
1167                      foreach($mycards as $card) 
1168                        display_link_card($card,$me);
1169 ?>
1170    <br />A short comment:<input name="comment" type="text" size="30" maxlength="50" /> 
1171    <input type="hidden" name="me" value="<?php echo $me; ?>" />
1172    <input type="submit" value="move" />
1173 </form>
1174 <?php
1175                    }
1176                  else 
1177                    { /* not our turn, just show the hand */
1178                      echo "Your cards are: <br />\n";
1179                      foreach($mycards as $card) 
1180                        display_card($card);
1181                    }
1182                  echo "</div>\n";   
1183                }
1184            }
1185        }
1186
1187  } 
1188
1189 ?>
1190 </body>
1191 </html>
1192
1193 <?php
1194 /*
1195  *Local Variables: 
1196  *mode: php
1197  *mode: hs-minor
1198  *End:
1199  */
1200 ?>