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