show cards every time, in case you are playing, make them links only if it is your...
[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 <p>
13 <?php
14 /*
15  * config 
16  */
17
18 $host  = "http://doko.nubati.net/index.php";
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)."\" />";
301   else
302     echo "<img src=\"cards/".($card-1).".png\" height=\"100\" alt=\"".card_to_name($card-1)."\" />";
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>";
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>";
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 $history=array();
379
380 /* check for status file and read it, if possible */
381
382 if(file_exists("status.txt"))
383   $lines = file("status.txt");
384  else
385    die("no file");
386
387 /* check if we want to start a new game */
388 if( isset($_REQUEST["PlayerA"]) && 
389     isset($_REQUEST["PlayerB"]) && 
390     isset($_REQUEST["PlayerC"]) && 
391     isset($_REQUEST["PlayerD"]) && 
392     isset($_REQUEST["EmailA"]) && 
393     isset($_REQUEST["EmailB"]) && 
394     isset($_REQUEST["EmailC"]) && 
395     isset($_REQUEST["EmailD"]) && sizeof($lines<2))
396   {
397     $PlayerA = $_REQUEST["PlayerA"];
398     $PlayerB = $_REQUEST["PlayerB"];
399     $PlayerC = $_REQUEST["PlayerC"];
400     $PlayerD = $_REQUEST["PlayerD"];
401     $EmailA  = $_REQUEST["EmailA"] ;
402     $EmailB  = $_REQUEST["EmailB"] ;
403     $EmailC  = $_REQUEST["EmailC"] ;
404     $EmailD  = $_REQUEST["EmailD"] ;
405     
406     $hashA = md5("AGameOfDoko".$PlayerA.$EmailA);
407     $hashB = md5("AGameOfDoko".$PlayerB.$EmailB);
408     $hashC = md5("AGameOfDoko".$PlayerC.$EmailC);
409     $hashD = md5("AGameOfDoko".$PlayerD.$EmailD);
410
411     /* send out email, check for error with email */
412
413     $message = "\n".
414       "you are invited to play a game of DoKo (that is to debug the program ;).\n".
415       "Place comments and bug reports here:\n".
416       "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
417       "The whole round would consist of the following players:\n".
418       "$PlayerA\n".
419       "$PlayerB\n".
420       "$PlayerC\n".
421       "$PlayerD\n\n".
422       "If you want to join this game, please follow this link:\n\n".
423       " ".$host."?a=";
424     
425     mymail($EmailA,"Invite for a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
426     mymail($EmailB,"Invite for a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
427     mymail($EmailC,"Invite for a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
428     mymail($EmailD,"Invite for a game of DoKo","Hello $PlayerD,\n".$message.$hashD);
429     
430     /* read in random.txt */
431     if(file_exists("random.txt"))
432       $random = file("random.txt");
433     else
434       die("no random file");
435  
436     $randomNR = explode( ":", $random[1] );
437     
438     /* write initial status into file */
439     $output = fopen("status.txt","w");
440     if ($output)
441       {
442         fwrite($output, "$hashA:$PlayerA:$EmailA:::" );
443         for($i=0;$i<11;$i++)
444           fwrite($output,"$randomNR[$i];" );
445         fwrite($output,"$randomNR[11]:" ); $i++;
446         fwrite($output,"\n");
447         
448         fwrite($output, "$hashB:$PlayerB:$EmailB:::" );
449         for(;$i<23;$i++)
450           fwrite($output,"$randomNR[$i];" );
451         fwrite($output,"$randomNR[23]:" ); $i++;
452         fwrite($output,"\n");
453         
454         fwrite($output, "$hashC:$PlayerC:$EmailC:::" );
455         for(;$i<35;$i++)
456           fwrite($output,"$randomNR[$i];" );
457         fwrite($output,"$randomNR[35]:" ); $i++;
458         fwrite($output,"\n");
459         
460         fwrite($output, "$hashD:$PlayerD:$EmailD:::");
461         for(;$i<47;$i++)
462           fwrite($output,"$randomNR[$i];" );
463         fwrite($output,"$randomNR[47]:" );
464         fwrite($output,"\n");
465         
466         fclose($output);
467       }
468     else
469       echo "can't open file for writing";
470   };
471
472 /* test if a game is running, else output everything for a new game */
473 if(sizeof($lines)<2)
474   {
475 ?>
476     <p> no game in progress, please input 4 names and email addresses, please make sure that the addresses are correct! </p>
477  <form action="index.php" method="post">
478    Name:  <input name="PlayerA" type="text" size="10" maxlength="20" /> 
479    Email: <input name="EmailA"  type="text" size="20" maxlength="30" /> <br />
480
481    Name:  <input name="PlayerB" type="text" size="10" maxlength="20" /> 
482    Email: <input name="EmailB"  type="text" size="20" maxlength="30" /> <br />
483
484    Name:  <input name="PlayerC" type="text" size="10" maxlength="20" /> 
485    Email: <input name="EmailC"  type="text" size="20" maxlength="30" /> <br />
486
487    Name:  <input name="PlayerD" type="text" size="10" maxlength="20" /> 
488    Email: <input name="EmailD"  type="text" size="20" maxlength="30" /> <br />
489
490    <input type="submit" value="start game" />
491  </form>
492 <?php
493    }
494  else
495    { /* load game status */
496      parse_status();
497 /*     **
498  *    *  *
499  *    ****
500  *    *  *
501  *
502  * check if a player wants to accept a game 
503  */
504      
505      if(isset($_REQUEST["a"]))
506        {
507          $a=$_REQUEST["a"];
508          
509          if( ereg("[is]",$player[$a]["option"]) &&  $game["init"]<4)
510            {
511              echo "just wait for the game to start";
512            }
513          else  if( !ereg("[is]",$player[$a]["option"]) )
514            {
515
516  ?>
517  <form action="index.php" method="post">
518    Do you want to play a game of DoKo?
519    yes<input type="radio" name="in" value="yes" />
520    no<input type="radio" name="in" value="no" /> <br />
521
522    Do you want to get an email for every card played or only if it your move?
523    every card<input type="radio" name="update" value="card" />
524    only on my turn<input type="radio" name="update" value="turn" /> <br />
525 <?php   
526              echo "<input type=\"hidden\" name=\"b\" value=\"$a\" />\n";
527              echo "\n";
528              echo "<input type=\"submit\" value=\"count me in\" />\n";
529              echo " </form>\n";
530            }
531        }
532
533 /*   ***
534  *   *  *
535  *   ***
536  *   *  *
537  *   ***
538  * yes? email him his hand, ask for solo, poverty, email every move or every card? 
539  */
540      if(isset($_REQUEST["b"]))
541        {
542          $b=$_REQUEST["b"];
543          
544          if( ereg("s",$player[$b]["option"])  && $game["init"]<4)
545            {
546              echo "just wait for the game to start";
547            }
548          else if( (!isset($_REQUEST["in"])|| !isset($_REQUEST["update"])) && !ereg("i",$player[$b]["option"]))
549            {
550              echo "go back to ";
551              echo "<a href=\"index.php?a=$b\"> here and fill out the form </a> <br />";
552            }
553          else
554            { /* show the hand */
555              if($_REQUEST["in"]=="no")
556                {
557                  for($i=0;$i<4;$i++)
558                    {
559                      $message = "Hello ".$player[$hash[$i]]["name"].",\n\n".
560                        "the game has been canceled due to the request of one of the players.\n";
561                      mymail($player[$hash[$i]]["email"],"[DoKo-Debug] the game has been canceled",$message); 
562                    }
563                    $output = fopen("status.txt","w");
564                    if($output)
565                      fclose($output);
566                    else
567                      echo "problem opening file";
568                }
569              else
570                {
571                  if($_REQUEST["update"]=="card") $player[$b]["option"].="c";
572                  if($_REQUEST["update"]=="turn") $player[$b]["option"].="t";
573                  
574                  $player[$b]["option"].="i";
575                  
576                  save_status();
577                  
578                  $allcards = $player[$b]["cards"];
579                  $mycards = explode(";",$allcards);
580                  
581                  sort($mycards);
582                  echo "your cards are <br>";
583                  foreach($mycards as $card) 
584                    {
585                      display_card($card);
586                    }
587                  echo "<br />\n";   
588  ?>
589  <p>aehm... at the moment please just answer everything with no, still need to implement this</p>                 
590  <form action="index.php" method="post">
591    
592    do you want to play solo? 
593    <select name="solo" size="1">
594      <option>No</option>
595      <option>No trump</option>
596      <option>Normal solo</option>
597      <option>Queen solo</option>
598      <option>Jack solo</option>
599      <option>Club solo</option>
600      <option>Spade solo</option>
601      <option>Heart solo</option>
602    </select>     
603    <br />
604
605 <?php   
606      
607                  echo "wedding?";
608                  if(check_wedding($player[$b]["cards"]))
609                    {
610                      echo " yes<input type=\"radio\" name=\"wedding\" value=\"yes\" />";
611                      echo " no <input type=\"radio\" name=\"wedding\" value=\"no\" /> <br />";
612                    }
613                  else
614                    {
615                      echo " no <input type=\"hidden\" name=\"wedding\" value=\"no\" /> <br />";
616                    };
617
618                  echo "do you have poverty?";
619                  if(count_trump($player[$b]["cards"])<4)
620                    {
621                      echo " yes<input type=\"radio\" name=\"poverty\" value=\"yes\" />";
622                      echo " no <input type=\"radio\" name=\"poverty\" value=\"no\" /> <br />";
623                    }
624                  else
625                    {
626                      echo " no <input type=\"hidden\" name=\"poverty\" value=\"no\" /> <br />";
627                    };
628
629                  echo "do you have too many nines?";
630                  if(count_nines($player[$b]["cards"])>4)
631                    {
632                      echo " yes<input type=\"radio\" name=\"nines\" value=\"yes\" />";
633                      echo " no <input type=\"radio\" name=\"nines\" value=\"no\" /> <br />";
634                    }
635                  else
636                    {
637                      echo " no <input type=\"hidden\" name=\"nines\" value=\"no\" /> <br />";
638                    };
639
640                  echo "<input type=\"hidden\" name=\"c\" value=\"$b\" />\n";
641                  echo "<input type=\"submit\" value=\"count me in\" />\n";
642                  
643                  echo "</form>\n";
644                }
645            }
646        }
647      if(isset($_REQUEST["c"]))
648        {
649          $c=$_REQUEST["c"];
650          
651          if(!isset($_REQUEST["solo"])|| !isset($_REQUEST["wedding"])|| !isset($_REQUEST["poverty"]) || !isset($_REQUEST["nines"]) )
652            {
653              echo "go back to ";
654              echo "<a href=\"index.php?b=$c\"> here and fill out the form </a> <br />";
655            }
656          else if(  ereg("s",$player[$c]["option"]) && $game["init"]<4 )
657            {
658              echo "just wait for the game to start";
659            }
660          else if($game["init"]<4)
661            { 
662              if( $_REQUEST["solo"]!="No")
663                {
664                  switch($_REQUEST["solo"])
665                    {
666                    case "No trump":
667                      $player[$c]["option"].="O";
668                      break;
669                    case "Normal solo":
670                      $player[$c]["option"].="S";
671                      break;
672                    case "Queen solo":
673                      $player[$c]["option"].="Q";
674                      break;
675                    case "Jack solo":
676                      $player[$c]["option"].="J";
677                      break;
678                    case "Club solo":
679                      $player[$c]["option"].="C";
680                      break;
681                    case "Spade solo":
682                      $player[$c]["option"].="A";
683                      break;
684                    case "Hear solo":
685                      $player[$c]["option"].="H";
686                      break;
687                    }
688                }
689              else if($_REQUEST["wedding"] == "yes")
690                {
691                  echo "wedding was chosen<br>";
692                  $player[$c]["option"].="W";
693                }
694              else if($_REQUEST["poverty"] == "yes")
695                {
696                  echo "poverty was chosen<br>";
697                  $player[$c]["option"].="P"; 
698                }
699              else if($_REQUEST["nines"] == "yes")
700                {
701                  echo "nines was chosen<br>";
702                  $player[$c]["option"].="N";
703                }
704              
705              $message = "you're in. once everyone has filled out the form,".
706                "the game will start and you'll get an eamil on your turn\n";
707              mymail($player[$c]["email"],"[DoKo-debug] the game will start soon",$message); 
708
709              $player[$c]["option"].="s";
710              save_status();
711              if(file_exists("status.txt"))
712                $lines = file("status.txt");
713              else
714                die("no file");
715              parse_status();
716
717              if($game["init"]==4)
718                {
719                  $message = "The game can start now, it's your turn, please use this link to play a card:\n".
720                    $host."?me=".$hash[$game["startplayer"]]."\n";
721                  mymail($player[$hash[$game["startplayer"]]]["email"],"[DoKo-debug] let's go",$message);
722                }
723              
724            }
725        }
726      if($game["init"]==4)
727        {
728          /* check for sickness*/
729          /***** someone has 5 nines and no one is playing solo=> cance5Al game */
730          if($game["nines"]>=0 && $game["solo-who"]<0)
731            {
732              $message = $player[$hash[$game["poverty"]]]["nines"]." has more than 4 nines. Game aborted!\n";
733              for($i=0;$i<4;$i++)
734                {
735                  mymail($player[$hash[$i]]["email"],"[DoKo-debug] the game has been canceled",$message); 
736                }
737              $output = fopen("status.txt","w");
738              if($output)
739                fclose($output);
740              else
741                echo "problem opening file";
742            }
743
744          /* who is requesting this*/
745          if(!isset($_REQUEST["me"]))
746            {    
747              if(!isset($_REQUEST["recovery"]))
748                {
749                  echo "a game is in progress, but you are not playing.<br />";
750                  echo "In case you are playing, but lost your email or can't access the game anymore, please input your email here:<br />";
751                  ?>
752  <form action="index.php" method="post">
753    recorvery: <input name="recovery"  type="text" size="20" maxlength="30" /> <br />
754    <input type="submit" value="get me back into the game" />
755  </form>
756 <?php
757                }
758              else
759                {
760                  $recovery=$_REQUEST["recovery"];
761                  $ok=-1;
762                  for($i=0;$i<4;$i++)
763                    if(trim($recovery)==trim($player[$hash[$i]]["email"]))
764                      $ok=$i;
765                  if($ok>=0)
766                    {
767                      $message = "Please try this link: ".$host."?me=".$hash[$ok]."\n".
768                        "\n if this doesn't work, contact the admin.\n";
769                      mymail($recovery,"[DoKo-Debug] recovery ",$message);
770                      echo "email has been sent.";
771                    }
772                  else
773                    {
774                      echo "can't find this email address, sorry.";
775                    }; 
776                } /* end recovery */
777            }
778          else
779            { /* $me is set */ 
780              $me = $_REQUEST["me"];
781              
782              echo "game in progress and you are in it<br />";
783              if($game["solo-who"]>=0)
784                echo $player[$hash[$game["solo-who"]]]["name"]." is playing a ".$game["solo-what"]." solo!<br />";
785              else if($game["wedding"]>=0)
786                echo $player[$hash[$game["wedding"]]]["name"]." is playing a wedding!<br />";
787              
788              /* show history */
789              foreach($history as $play) 
790                {
791                  echo "<br />";
792                  
793                  $trick = explode(":",$play);
794                  
795                  $last=-2;
796                  /* found old trick, display it */
797                  for($i=0;$i<sizeof($trick)-1;$i++)
798                    {
799                      $card=$trick[$i];
800                      if(ereg("->",$card))
801                        {
802                          $tmp = explode("->",$card);
803                          echo $player[$hash[$tmp[0]]]["name"]." played ";
804                          display_card($tmp[1]);
805                          $last=$tmp[0];
806                        }
807                    }
808                }
809              
810              /* figure out who needs to play next */
811              $next = $last + 1;
812              if ($next>=4) 
813                $next -= 4 ;
814              if($last<0)
815                {
816                  $next=$history[sizeof($history)-1][0];
817                }
818              
819              /* are we trying to play a card? */
820              if(isset($_REQUEST["card"]))
821                {
822                  if($hash[$next]==$me)
823                    {
824                      $card    = $_REQUEST["card"];
825                      $mycards = explode(";",$player[$me]["cards"]);
826                      
827                      /* do we have that card */
828                      if(in_array($card,$mycards))
829                        {
830                          /* delete card from array */
831                          $tmp = array();
832                          foreach($mycards as $m)
833                            if($m != $card)
834                              $tmp[]=$m;
835                          
836                          $tmp2="";
837                          for($i=0;$i<sizeof($tmp)-1;$i++)
838                            {
839                              $tmp2.=$tmp[$i].";";
840                            }
841                          $tmp2.=$tmp[$i];
842                          $player[$me]["cards"]=$tmp2;
843                          
844                          /* add card to history, special case if this is the first card */
845                          if($last<0)
846                            {
847                              $history[sizeof($history)-1]="".$player[$me]["number"]."->".$card.":\n";
848                            }
849                          else
850                            {
851                              $tmp = explode(":",$history[sizeof($history)-1]);
852                              $tmp[sizeof($tmp)-1] = "".$player[$me]["number"]."->".$card.":";
853                              $history[sizeof($history)-1]=join(":",$tmp);
854                            }
855                          save_status();
856                          
857                          echo " you played ";
858                          display_card($card);
859                          
860                          /* send out email to players who want/need to get informed */
861                          for($i=0;$i<4;$i++)
862                            {
863                              $mynext=$next+1; if($mynext>3)$mynext-=4;
864                              if((ereg("c",$player[$hash[$i]]["option"]) || $i==$mynext) && $hash[$i]!=$me)
865                                {
866                                  $message = " Hello ".$player[$hash[$i]]["name"].",\n\n";
867                                  
868                                  if($i==$mynext)
869                                    {
870                                      $message .= "it's your turn  now.\n".
871                                        "Use this link to play a card: ".$host."?me=".$hash[$i]."\n\n" ;
872                                    }
873                                  $message .= $player[$me]["name"]. "has played the following card ".card_to_name($card)."\n";
874                                  
875                                  if($game["solo-who"]>=0)
876                                    $message.= $player[$hash[$game["solo-who"]]]." is playing a ".$game["solo-what"]." solo!\n";
877                                  
878                                  mymail($player[$hash[$i]]["email"],"[DoKo-debug] a card has been played",$message);
879                                  
880                                  if($debug)
881                                    echo "<a href=\"index.php?me=".$hash[$mynext]."\"> next player </a> <br />";
882                                }
883                            }
884                        }
885                      else
886                        echo "seems like you don't have that card<br>";
887                      
888                    }
889                  
890                } /* end if card is set */
891              else if(isset($_REQUEST["win"]) && strlen($history[sizeof($history)-1])>3)
892                {
893                  $win=$_REQUEST["win"];
894                  $history[]=$win.":\n";
895                  /* count points of the last trick */
896                  $points=0;
897
898                  $tmp = explode(":",$history[sizeof($history)-2]);
899                  for($i=0;$i<4;$i++)
900                    {
901                      $tmp2 = explode("->",$tmp[$i]);
902                      $c = $tmp2[1];
903                      $points += card_value($c);
904                    }
905                  $player[$hash[$win]]["points"]+=$points;
906                  echo "<br> ".$player[$hash[$win]]["name"]." won: $points Points <br>";
907                  
908                  save_status();
909                }; /* end if win is set */
910              echo "<br />";
911
912              $tmp = explode(":",$history[sizeof($history)-1]);
913
914              /* check last history entry: end of a trick? ask who won it */
915              if(sizeof($tmp)==5)
916                {
917                  ?>
918 <form action="index.php" method="post">
919 who won?
920 <?php 
921    for($i=0;$i<4;$i++)
922      echo $player[$hash[$i]]["name"]." <input type=\"radio\" name=\"win\" value=\"$i\" />";
923    echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />";
924 ?>
925 <input type="submit" value="submit" />
926
927 </form>
928 <?php
929                }
930              else if(sizeof($tmp)<5 && 1<sizeof($tmp) && !isset($_REQUEST["card"]))
931                {                 
932                  if(sizeof($tmp)==2 && strlen($tmp[0])==1)
933                    {
934                      $next=$tmp[0];
935                      
936                      if($debug)
937                        echo "DEBUG: the next move is for <a href=\"index.php?me=".$hash[$next]."\">the next player</a><br>";
938                      
939                      if(strlen(trim($player[$me]["cards"]))==0)
940                        {
941                          echo "<br> game over, count points <br>";
942                          for($i=0;$i<4;$i++)
943                            {
944                              echo $player[$hash[$i]]["name"]." got ".$player[$hash[$i]]["points"]."<br>";
945                            }
946                        }
947                    }
948                  echo "<br />";
949                } /* end check for winner */
950              
951              /* do we still have cards? display them */
952              if(strlen(trim($player[$me]["cards"]))>0 )
953                {
954                  $allcards = trim($player[$me]["cards"]);
955                  $mycards = explode(";",$allcards);
956                  
957                  sort($mycards);
958                  
959                  /* is it our turn? */
960                  if($hash[$next]==$me) 
961                    {
962                      echo "ITS YOUR TURN   <br />";
963                      echo "your cards are: <br />";
964                      foreach($mycards as $card) 
965                        {
966                          display_link_card($card,$me);
967                        }
968                    }
969                  else 
970                    { /* not our turn, just show the hand */
971                      echo "your cards are: <br />";
972                      foreach($mycards as $card) 
973                        {
974                          display_card($card);
975                        }
976                    }
977                  echo "<br />\n";   
978                }
979            }
980        }
981
982  } 
983
984 ?>
985 </p>
986 </body>
987 </html>