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