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