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