played the first complete game :)
[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>Doko via email</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 /* helper function */
14 function mymail($To,$Subject,$message)
15 {  
16   $debug = 1;
17   if($debug)
18     echo "<br>To: $To<br>Subject: $Subject <br>$message<br>";
19   else
20     mail($To,$Subject,$message);
21   return;
22 }
23
24 function card_to_name($card)
25 {
26   switch($card)
27     {
28       case 1:
29       case 2:
30         return "ten of hearts";
31       case 3:
32       case 4:
33       return "queen of clubs";
34       case 5:
35       case 6:
36       return "queen of spades";
37       case 7:
38       case 8:
39       return "queen of hearts";
40       case 9:
41       case 10:
42       return "queen of diamonds";
43       case 11:
44       case 12:
45       return "jack of clubs";
46       case 13:
47       case 14:
48       return "jack of spades";
49       case 15:
50       case 16:
51       return "jack of hearts";
52       case 17:
53       case 18:
54       return "jack of diamonds";
55       case 19:
56       case 20:
57       return "ace of diamonds";
58       case 21:
59       case 22:
60       return "ten of diamonds";
61       case 23:
62       case 24:
63       return "king of diamonds";
64       case 25:
65       case 26:
66       return "nine of diamonds";;
67       case 27:
68       case 28:
69       return "ace of clubs";
70       case 29:
71       case 30:
72       return "ten of clubs";
73       case 31:
74       case 32:
75       return "king of clubs";
76       case 33:
77       case 34:
78       return "nine of clubs";
79       case 35:
80       case 36:
81       return "ace of spades";
82       case 37:
83       case 38:
84       return "ten of spades";
85       case 39:
86       case 40:
87       return "king of spades";
88       case 41:
89       case 42:
90       return "nine of spades";
91       case 43:
92       case 44:
93       return "ace of hearts";
94       case 45:
95       case 46:
96       return "ace of diamonds";
97       case 47:
98       case 48:
99       return "nine of diamonds";
100       default:
101       return "something went wrong, please contact the admin";
102     }
103 }
104
105 function card_value($card)
106 {
107   switch($card-1)
108     {
109       case 0:
110       case 1:
111         return 10;
112       case 2:
113       case 3:
114       case 4:
115       case 5:
116       case 6:
117       case 7:
118       case 8:
119       case 9:
120         return 3;
121       case 10:
122       case 11:
123       case 12:
124       case 13:
125       case 14:
126       case 15:
127       case 16:
128       case 17:
129         return 2;
130       case 18:
131       case 19:
132         return 11;
133       case 20:
134       case 21:
135         return 10;
136       case 22:
137       case 23:
138         return 4;
139       case 24:
140       case 25:
141       return 0;
142       case 26:
143       case 27:
144       return 11;
145       case 28:
146       case 29:
147       return 10;
148       case 30:
149       case 31:
150       return 4;
151       case 32:
152       case 33:
153       return 0;
154       case 34:
155       case 35:
156       return 11;
157       case 36:
158       case 37:
159       return 10;
160       case 38:
161       case 39:
162       return 4;
163       case 40:
164       case 41:
165       return 0;
166       case 42:
167       case 43:
168       return 11;
169       case 44:
170       case 45:
171       return 4;
172       case 46:
173       case 47:
174       return 0;
175       default:
176       echo "something went wrong, please contact the admin <br>";
177       return 0;
178     }
179 }
180
181 function display_card($card)
182 {
183   if( $card/2 - (int)($card/2) == 0.5)
184     echo "<img src=\"cards/".$card.".png\" height=\"100\">";
185   else
186     echo "<img src=\"cards/".($card-1).".png\" height=\"100\">";
187   return;
188 }
189    function display_link_card($card,$me)
190 {
191   if( $card/2 - (int)($card/2) == 0.5)
192     echo "<a href=\"index.php?me=$me&card=$card\"><img src=\"cards/".$card.".png\" height=\"100\"></a>";
193   else
194     echo "<a href=\"index.php?me=$me&card=$card\"><img src=\"cards/".($card-1).".png\" height=\"100\"></a>";
195   return;
196 }
197
198 function save_status()
199 {
200   global $player,$game,$hash,$history;
201
202   $output = fopen("status.txt","w");
203   if ($output)
204     {
205       foreach($hash as $key)
206         {
207           $tmp="";
208           if( ereg("i",$player[$key]["option"]) )
209             $tmp.="i";
210           if( ereg("s",$player[$key]["option"]) )
211             $tmp.="s";
212           if( ereg("t",$player[$key]["option"]) )
213             $tmp.="t";
214           if( ereg("c",$player[$key]["option"]) )
215             $tmp.="c";
216           $player[$key]["option"]=$tmp;
217
218           fwrite($output,"".$player[$key]["hash"].":" );
219           fwrite($output,"".$player[$key]["name"].":" );
220           fwrite($output,"".$player[$key]["email"].":" );
221           fwrite($output,"".$player[$key]["option"].":" );
222           fwrite($output,"".$player[$key]["points"].":" );
223           fwrite($output,"".$player[$key]["cards"] .":");
224           fwrite($output,"\n");
225         }
226       fwrite($output,"\n");
227       foreach($history as $line)
228         fwrite($output,$line);
229
230       fwrite($output,"\n");
231       fclose($output);
232     }
233   else
234     echo "can't open file for writing, please inform the admin.";
235   
236   return;
237 }
238
239 $history=array();
240
241 /* check for status file and read it, if possible */
242
243 if(file_exists("status.txt"))
244   $lines = file("status.txt");
245  else
246    die("no file");
247
248 /* check if we want to start a new game */
249 if( isset($_REQUEST["PlayerA"]) && 
250     isset($_REQUEST["PlayerB"]) && 
251     isset($_REQUEST["PlayerC"]) && 
252     isset($_REQUEST["PlayerD"]) && 
253     isset($_REQUEST["EmailA"]) && 
254     isset($_REQUEST["EmailB"]) && 
255     isset($_REQUEST["EmailC"]) && 
256     isset($_REQUEST["EmailD"]) && sizeof($lines<2))
257   {
258     $PlayerA = $_REQUEST["PlayerA"];
259     $PlayerB = $_REQUEST["PlayerB"];
260     $PlayerC = $_REQUEST["PlayerC"];
261     $PlayerD = $_REQUEST["PlayerD"];
262     $EmailA  = $_REQUEST["EmailA"] ;
263     $EmailB  = $_REQUEST["EmailB"] ;
264     $EmailC  = $_REQUEST["EmailC"] ;
265     $EmailD  = $_REQUEST["EmailD"] ;
266     
267     $hashA = md5("AGameOfDoko".$PlayerA.$EmailA);
268     $hashB = md5("AGameOfDoko".$PlayerB.$EmailB);
269     $hashC = md5("AGameOfDoko".$PlayerC.$EmailC);
270     $hashD = md5("AGameOfDoko".$PlayerD.$EmailD);
271
272     /* send out email, check for error with email */
273
274     $message = "\n".
275       "you are invited to play a game of DoKo.\n".
276       "The whole round would consist of the following players:\n".
277       "$PlayerA\n".
278       "$PlayerB\n".
279       "$PlayerC\n".
280       "$PlayerD\n\n".
281       "If you want to join this game, please follow this link:\n\n".
282       " http://doko.nubati.net/index.php?a=";
283     
284     mymail($EmailA,"Invite for a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
285     mymail($EmailB,"Invite for a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
286     mymail($EmailC,"Invite for a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
287     mymail($EmailD,"Invite for a game of DoKo","Hello $PlayerD,\n".$message.$hashD);
288     
289     /* read in random.txt */
290     if(file_exists("random.txt"))
291       $random = file("random.txt");
292     else
293       die("no random file");
294  
295     $randomNR = explode( ":", $random[1] );
296     
297     /* write initial status into file */
298     $output = fopen("status.txt","w");
299     if ($output)
300       {
301         fwrite($output, "$hashA:$PlayerA:$EmailA:::" );
302         for($i=0;$i<11;$i++)
303           fwrite($output,"$randomNR[$i];" );
304         fwrite($output,"$randomNR[11]:" ); $i++;
305         fwrite($output,"\n");
306         
307         fwrite($output, "$hashB:$PlayerB:$EmailB:::" );
308         for(;$i<23;$i++)
309           fwrite($output,"$randomNR[$i];" );
310         fwrite($output,"$randomNR[23]:" ); $i++;
311         fwrite($output,"\n");
312         
313         fwrite($output, "$hashC:$PlayerC:$EmailC:::" );
314         for(;$i<35;$i++)
315           fwrite($output,"$randomNR[$i];" );
316         fwrite($output,"$randomNR[35]:" ); $i++;
317         fwrite($output,"\n");
318         
319         fwrite($output, "$hashD:$PlayerD:$EmailD:::");
320         for(;$i<47;$i++)
321           fwrite($output,"$randomNR[$i];" );
322         fwrite($output,"$randomNR[47]:" );
323         fwrite($output,"\n");
324         
325         fclose($output);
326       }
327     else
328       echo "can't open file for writing";
329   };
330
331 /* test if a game is running, else output everything for a new game */
332 if(sizeof($lines)<2)
333   {
334 ?>
335     <p> no game in progress, please input 4 names and email addresses, please make sure that the addresses are correct! </p>
336  <form action="index.php" method="post">
337    Name:  <input name="PlayerA" type="text" size="10" maxlength="20" /> 
338    Email: <input name="EmailA"  type="text" size="10" maxlength="20" /> <br />
339
340    Name:  <input name="PlayerB" type="text" size="10" maxlength="20" /> 
341    Email: <input name="EmailB"  type="text" size="10" maxlength="20" /> <br />
342
343    Name:  <input name="PlayerC" type="text" size="10" maxlength="20" /> 
344    Email: <input name="EmailC"  type="text" size="10" maxlength="20" /> <br />
345
346    Name:  <input name="PlayerD" type="text" size="10" maxlength="20" /> 
347    Email: <input name="EmailD"  type="text" size="10" maxlength="20" /> <br />
348
349    <input type="submit" value="start game" />
350  </form>
351 <?php
352    }
353  else
354    { /* load game status */
355      $game["init"]=0;
356      
357      $tmp = explode( ":",$lines[0]);
358      $hash[0]   = $tmp[0];
359      $player[$tmp[0]]["number"] = 0;
360      $player[$tmp[0]]["hash"]   = $tmp[0];
361      $player[$tmp[0]]["name"]   = $tmp[1];
362      $player[$tmp[0]]["email"]  = $tmp[2];
363      $player[$tmp[0]]["option"] = $tmp[3];
364      $player[$tmp[0]]["points"] = $tmp[4];
365      $player[$tmp[0]]["cards"]  = $tmp[5];
366      if(ereg("s",$tmp[3])) $game["init"]++;
367
368      $tmp = explode( ":",$lines[1]);
369      $hash[1]   = $tmp[0];
370      $player[$tmp[0]]["number"] = 1;
371      $player[$tmp[0]]["hash"]   = $tmp[0];
372      $player[$tmp[0]]["name"]   = $tmp[1];
373      $player[$tmp[0]]["email"]  = $tmp[2];
374      $player[$tmp[0]]["option"] = $tmp[3];
375      $player[$tmp[0]]["points"] = $tmp[4]; 
376      $player[$tmp[0]]["cards"]  = $tmp[5];
377      if(ereg("s",$tmp[3])) $game["init"]++;
378      
379      $tmp = explode( ":",$lines[2]);
380      $hash[2]   = $tmp[0];
381      $player[$tmp[0]]["number"] = 2;
382      $player[$tmp[0]]["hash"]   = $tmp[0];
383      $player[$tmp[0]]["name"]   = $tmp[1];
384      $player[$tmp[0]]["email"]  = $tmp[2];
385      $player[$tmp[0]]["option"] = $tmp[3];
386      $player[$tmp[0]]["points"] = $tmp[4];
387      $player[$tmp[0]]["cards"]  = $tmp[5];
388      if(ereg("s",$tmp[3])) $game["init"]++;
389      
390      $tmp = explode( ":",$lines[3]);
391      $hash[3]   = $tmp[0];
392      $player[$tmp[0]]["number"] = 3;
393      $player[$tmp[0]]["hash"]   = $tmp[0];
394      $player[$tmp[0]]["name"]   = $tmp[1];
395      $player[$tmp[0]]["email"]  = $tmp[2];
396      $player[$tmp[0]]["option"] = $tmp[3];
397      $player[$tmp[0]]["points"] = $tmp[4];
398      $player[$tmp[0]]["cards"]  = $tmp[5];
399      if(ereg("s",$tmp[3])) $game["init"]++;
400
401      /* save the game history */
402      for($i=4;$i<sizeof($lines);$i++)
403        {
404          if(!ereg("^[[:space:]]*$",trim($lines[$i])))
405            {
406              $history[] = $lines[$i];
407            }
408        }
409      if(sizeof($history)== 0)
410        $history[] = "0:";
411
412 /*     **
413  *    *  *
414  *    ****
415  *    *  *
416  *
417  * check if a player wants to accept a game 
418  */
419      
420      if(isset($_REQUEST["a"]))
421        {
422          $a=$_REQUEST["a"];
423          
424          if( ereg("[is]",$player[$a]["option"]) &&  $game["init"]<4)
425            {
426              echo "just wait for the game to start";
427            }
428          else  if( !ereg("[is]",$player[$a]["option"]) )
429            {
430
431  ?>
432  <form action="index.php" method="post">
433    Do you want to play a game of DoKo?
434    yes<input type="radio" name="in" value="yes" />
435    no<input type="radio" name="in" value="no" /> <br />
436
437    Do you want to get an email for every card played or only if it your move?
438    every card<input type="radio" name="update" value="card" />
439    only on my turn<input type="radio" name="update" value="turn" /> <br />
440 <?php   
441              echo "<input type=\"hidden\" name=\"b\" value=\"$a\" />\n";
442              echo "\n";
443              echo "<input type=\"submit\" value=\"count me in\" />\n";
444              echo " </form>\n";
445            }
446        }
447
448 /*   ***
449  *   *  *
450  *   ***
451  *   *  *
452  *   ***
453  * yes? email him his hand, ask for solo, poverty, email every move or every card? 
454  */
455      if(isset($_REQUEST["b"]))
456        {
457          $b=$_REQUEST["b"];
458          
459          if( ereg("s",$player[$b]["option"])  && $game["init"]<4)
460            {
461              echo "just wait for the game to start";
462            }
463          else if(!isset($_REQUEST["in"])|| !isset($_REQUEST["update"]) )
464            {
465              echo "go back to ";
466              echo "<a href=\"index.php?a=$b\"> here and fill out the form </a> <br />";
467            }
468          else
469            { /* show the hand */
470              if($_REQUEST["in"]=="no")
471                {
472                  for($i=0;$i<4;$i++)
473                    {
474                      $message = "Hello ".$player[$hash[$i]]["name"].",\n\n".
475                        "the game has been cancled due to the request of one of the players.\n";
476                      mymail($player[$hash[$i]]["email"],"[DoKo] the game has been cancled",$message); 
477                    }
478                    $output = fopen("status.txt","w");
479                    if($output)
480                      fclose($output);
481                    else
482                      echo "problem opening file";
483                }
484              else
485                {
486                  if($_REQUEST["update"]=="card") $player[$b]["option"].="c";
487                  if($_REQUEST["update"]=="turn") $player[$b]["option"].="t";
488                  
489                  $player[$b]["option"].="i";
490                  
491                  save_status();
492                  
493                  $allcards = $player[$b]["cards"];
494                  $mycards = explode(";",$allcards);
495                  
496                  sort($mycards);
497                  echo "your cards are <br>";
498                  foreach($mycards as $card) 
499                    {
500                      display_card($card);
501                    }
502                  echo "<br />\n";   
503  ?>
504  <p>aehm... at the moment please just answer everything with no, still need to implement this</p>                 
505  <form action="index.php" method="post">
506    
507    do you want to play solo?
508    yes<input type="radio" name="solo" value="yes" />
509    no<input type="radio" name="solo" value="no" /> <br />
510
511    do you have a wedding?
512    yes<input type="radio" name="wedding" value="yes" />
513    no<input type="radio" name="wedding" value="no" /> <br />
514
515    do you have poverty?
516    yes<input type="radio" name="poverty" value="yes" />
517    no<input type="radio" name="poverty" value="no" /> <br />
518    
519 <?php   
520                  echo "<input type=\"hidden\" name=\"c\" value=\"$b\" />\n";
521                  echo "<input type=\"submit\" value=\"count me in\" />\n";
522                  
523                  echo "</form>\n";
524                }
525            }
526        }
527      if(isset($_REQUEST["c"]))
528        {
529          $c=$_REQUEST["c"];
530          
531          if(!isset($_REQUEST["solo"])|| !isset($_REQUEST["wedding"])|| !isset($_REQUEST["poverty"]) )
532            {
533              echo "go back to ";
534              echo "<a href=\"index.php?b=$c\"> here and fill out the form </a> <br />";
535            }
536          else if(  ereg("s",$player[$c]["option"]) && $game["init"]<4 )
537            {
538              echo "just wait for the game to start";
539            }
540          else if($game["init"]<4)
541            { 
542              echo "handle krankheit <br />";
543
544              $message = "you're in. once everyone has filled out the form,".
545                "the game will start and you'll get an eamil on your turn\n";
546              mymail($player[$c]["email"],"[DoKo] the game will start soon",$message); 
547              
548              $player[$c]["option"].="s";
549
550              save_status();
551            }
552        }
553      if($game["init"]==4)
554        {
555          if(!isset($_REQUEST["me"]))
556            echo "a game is in progress, but you are not playing";
557          else
558            {
559              $me = $_REQUEST["me"];
560
561              echo "game in progress and you are in it<br>";
562              foreach($history as $play)
563                {
564                  echo "<br />";
565
566                  $trick = explode(":",$play);
567                  
568                  $last=-2;
569                  /* found old trick, display it */
570                  for($i=0;$i<sizeof($trick)-1;$i++)
571                    {
572                      $card=$trick[$i];
573                      if(ereg("->",$card))
574                        {
575                          $tmp = explode("->",$card);
576                          echo $player[$hash[$tmp[0]]]["name"]." played ";
577                          display_card($tmp[1]);
578                          $last=$tmp[0];
579                        }
580                    }
581                }
582              
583              $next = $last + 1;
584              if ($next>=4) 
585                $next -= 4 ;
586              if($last<0)
587                {
588                  $next=$history[sizeof($history)-1][0];
589                }
590
591
592              if(isset($_REQUEST["card"]))
593                {
594                  if($hash[$next]==$me)
595                    {
596                      $card=$_REQUEST["card"];
597                      $mycards = explode(";",$player[$me]["cards"]);
598                      if(in_array($card,$mycards))
599                        {
600                          $tmp=array();
601                          foreach($mycards as $m)
602                            if($m!=$card)
603                              $tmp[]=$m;
604                          $tmp2="";
605                          for($i=0;$i<sizeof($tmp)-1;$i++)
606                            {
607                              $tmp2.=$tmp[$i].";";
608                            }
609                          $tmp2.=$tmp[$i];
610                          $player[$me]["cards"]=$tmp2;
611                          
612                          if($last<0)
613                            {
614                              $history[sizeof($history)-1]="".$player[$me]["number"]."->".$card.":\n";
615                            }
616                          else
617                            {
618                              $tmp = explode(":",$history[sizeof($history)-1]);
619                              $tmp[sizeof($tmp)-1] = "".$player[$me]["number"]."->".$card.":";
620                              $history[sizeof($history)-1]=join(":",$tmp);
621                            }
622                          save_status();
623
624                          echo " you played ";
625                          display_card($card);
626                          /* send out email to players who want/need to get informed */
627                          for($i=0;$i<4;$i++)
628                            {
629                              $mynext=$next+1; if($mynext>3)$mynext-=4;
630                              if((ereg("c",$player[$hash[$i]]["option"]) || $i==$mynext) && $hash[$i]!=$me)
631                                {
632                                  $message = " Hello ".$player[$hash[$i]]["name"].",\n\n";
633                                    
634                                  if($i==$mynext)
635                                    $message .= "it's your turn  now.\n";
636                                  $message .= $player[$me]["name"]. "has played the following card ".card_to_name($card)."\n";
637                                  
638                                  mymail($player[$hash[$i]]["email"],"[DoKo] a card has been played",$message);
639                                  echo "<a href=\"index.php?me=".$hash[$mynext]."\"> next player </a> <br />";
640                                }
641                            }
642                        }
643                      else
644                        echo "seems like you don't have that card<br>";
645                           
646                    }
647                  
648                }
649              else if(isset($_REQUEST["win"]) && strlen($history[sizeof($history)-1])>3)
650                {
651                  $win=$_REQUEST["win"];
652                  $history[]=$win.":\n";
653                  /* count points of the last trick */
654                  $points=0;
655
656                  $tmp = explode(":",$history[sizeof($history)-2]);
657                  for($i=0;$i<4;$i++)
658                    {
659                      $tmp2 = explode("->",$tmp[$i]);
660                      $c = $tmp2[1];
661                      $points += card_value($c);
662                    }
663                  $player[$hash[$win]]["points"]+=$points;
664                  echo "<br> ".$player[$hash[$win]]["name"]." won: $points Points <br>";
665
666                  save_status();
667                }
668              echo "<br />";
669
670              $tmp = explode(":",$history[sizeof($history)-1]);
671
672              if(sizeof($tmp)==5)
673                {
674                  ?>
675 <form action="index.php" method="post">
676                           
677 <?php 
678    for($i=0;$i<4;$i++)
679      echo $player[$hash[$i]]["name"]." <input type=\"radio\" name=\"win\" value=\"$i\" />";
680    echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />";
681 ?>
682 <input type="submit" value="who will win?" />
683
684 </form>
685 <?php
686                }
687              else if(sizeof($tmp)<5 && 1<sizeof($tmp) && !isset($_REQUEST["card"]))
688                {                 
689                  if(sizeof($tmp)==2 && strlen($tmp[0])==1)
690                    {
691                      $next=$tmp[0];
692
693                      /*
694                       * echo "DEBUG: the next move is for <a href=\"index.php?me=".$hash[$next]."\">the next player</a><br>";
695                       */
696                      if(strlen(trim($player[$me]["cards"]))==0)
697                        {
698                          echo "<br> game over, count points <br>";
699                          for($i=0;$i<4;$i++)
700                            {
701                              echo $player[$hash[$i]]["name"]." got ".$player[$hash[$i]]["points"]."<br>";
702                            }
703                        }
704                    }
705                  if($hash[$next]==$me && strlen(trim($player[$me]["cards"]))>0 )
706                    {
707                      
708                      echo "ITS YOUR TURN<br>";
709                      $allcards = trim($player[$me]["cards"]);
710                      $mycards = explode(";",$allcards);
711                      
712                      sort($mycards);
713                      echo "your cards are <br>";
714                      foreach($mycards as $card) 
715                        {
716                          display_link_card($card,$me);
717                        }
718                      echo "<br />\n";   
719                    }
720                  echo "<br />";
721                  
722                }
723            }
724        }
725
726  } 
727
728 ?>
729
730 </body>
731 </html>