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