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