0be1deb3f24d5b48c68227d881c29a13888501e9
[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     echo "send out emails to everyone, asking if they want to join";
260     echo "use link <br>";
261     echo "<a href=\"index.php?a=hash1\"> player 1</a> <br />";
262     echo "<a href=\"index.php?a=hash2\"> player 2</a> <br />";
263     echo "<a href=\"index.php?a=hash3\"> player 3</a> <br />";
264     echo "<a href=\"index.php?a=hash4\"> player 4</a> <br />";
265     
266     /* read in random.txt */
267     if(file_exists("random.txt"))
268       $random = file("random.txt");
269     else
270       die("no random file");
271  
272     $randomNR = explode( ":", $random[1] );
273     
274     /* write initial status into file */
275     $output = fopen("status.txt","w");
276     if ($output)
277       {
278         fwrite($output, "hash1:$PlayerA:$EmailA:::" );
279         for($i=0;$i<11;$i++)
280           fwrite($output,"$randomNR[$i];" );
281         fwrite($output,"$randomNR[11]:" ); $i++;
282         fwrite($output,"\n");
283         
284         fwrite($output, "hash2:$PlayerB:$EmailB:::" );
285         for(;$i<23;$i++)
286           fwrite($output,"$randomNR[$i];" );
287         fwrite($output,"$randomNR[23]:" ); $i++;
288         fwrite($output,"\n");
289         
290         fwrite($output, "hash3:$PlayerC:$EmailC:::" );
291         for(;$i<35;$i++)
292           fwrite($output,"$randomNR[$i];" );
293         fwrite($output,"$randomNR[35]:" ); $i++;
294         fwrite($output,"\n");
295         
296         fwrite($output, "hash4:$PlayerD:$EmailD:::");
297         for(;$i<47;$i++)
298           fwrite($output,"$randomNR[$i];" );
299         fwrite($output,"$randomNR[47]:" );
300         fwrite($output,"\n");
301         
302         fclose($output);
303       }
304     else
305       echo "can't open file for writing";
306   };
307
308 /* test if a game is running, else output everything for a new game */
309 if(sizeof($lines)<2)
310   {
311 ?>
312  <p> no game in progress, please input 4 names and email addresses </p>
313  <form action="index.php" method="post">
314    Name:  <input name="PlayerA" type="text" size="10" maxlength="20" /> 
315    Email: <input name="EmailA"  type="text" size="10" maxlength="20" /> <br />
316
317    Name:  <input name="PlayerB" type="text" size="10" maxlength="20" /> 
318    Email: <input name="EmailB"  type="text" size="10" maxlength="20" /> <br />
319
320    Name:  <input name="PlayerC" type="text" size="10" maxlength="20" /> 
321    Email: <input name="EmailC"  type="text" size="10" maxlength="20" /> <br />
322
323    Name:  <input name="PlayerD" type="text" size="10" maxlength="20" /> 
324    Email: <input name="EmailD"  type="text" size="10" maxlength="20" /> <br />
325
326    <input type="submit" value="start game" />
327  </form>
328 <?php
329    }
330  else
331    { /* load game status */
332      $game["init"]=0;
333      
334      $tmp = explode( ":",$lines[0]);
335      $hash[0]   = $tmp[0];
336      $player[$tmp[0]]["number"] = 0;
337      $player[$tmp[0]]["hash"]   = $tmp[0];
338      $player[$tmp[0]]["name"]   = $tmp[1];
339      $player[$tmp[0]]["email"]  = $tmp[2];
340      $player[$tmp[0]]["option"] = $tmp[3];
341      $player[$tmp[0]]["points"] = $tmp[4];
342      $player[$tmp[0]]["cards"]  = $tmp[5];
343      if(ereg("s",$tmp[3])) $game["init"]++;
344
345      $tmp = explode( ":",$lines[1]);
346      $hash[1]   = $tmp[0];
347      $player[$tmp[0]]["number"] = 1;
348      $player[$tmp[0]]["hash"]   = $tmp[0];
349      $player[$tmp[0]]["name"]   = $tmp[1];
350      $player[$tmp[0]]["email"]  = $tmp[2];
351      $player[$tmp[0]]["option"] = $tmp[3];
352      $player[$tmp[0]]["points"] = $tmp[4]; 
353      $player[$tmp[0]]["cards"]  = $tmp[5];
354      if(ereg("s",$tmp[3])) $game["init"]++;
355      
356      $tmp = explode( ":",$lines[2]);
357      $hash[2]   = $tmp[0];
358      $player[$tmp[0]]["number"] = 2;
359      $player[$tmp[0]]["hash"]   = $tmp[0];
360      $player[$tmp[0]]["name"]   = $tmp[1];
361      $player[$tmp[0]]["email"]  = $tmp[2];
362      $player[$tmp[0]]["option"] = $tmp[3];
363      $player[$tmp[0]]["points"] = $tmp[4];
364      $player[$tmp[0]]["cards"]  = $tmp[5];
365      if(ereg("s",$tmp[3])) $game["init"]++;
366      
367      $tmp = explode( ":",$lines[3]);
368      $hash[3]   = $tmp[0];
369      $player[$tmp[0]]["number"] = 3;
370      $player[$tmp[0]]["hash"]   = $tmp[0];
371      $player[$tmp[0]]["name"]   = $tmp[1];
372      $player[$tmp[0]]["email"]  = $tmp[2];
373      $player[$tmp[0]]["option"] = $tmp[3];
374      $player[$tmp[0]]["points"] = $tmp[4];
375      $player[$tmp[0]]["cards"]  = $tmp[5];
376      if(ereg("s",$tmp[3])) $game["init"]++;
377
378      /* save the game history */
379      for($i=4;$i<sizeof($lines);$i++)
380        {
381          if(!ereg("^[[:space:]]*$",trim($lines[$i])))
382            {
383              $history[] = $lines[$i];
384            }
385        }
386      if(sizeof($history)== 0)
387        $history[] = "0:";
388
389 /*     **
390  *    *  *
391  *    ****
392  *    *  *
393  *
394  * check if a player wants to accept a game 
395  */
396      
397      if(isset($_REQUEST["a"]))
398        {
399          $a=$_REQUEST["a"];
400          
401          if( ereg("[is]",$player[$a]["option"]) &&  $game["init"]<4)
402            {
403              echo "just wait for the game to start";
404            }
405          else  if( !ereg("[is]",$player[$a]["option"]) )
406            {
407
408  ?>
409  <form action="index.php" method="post">
410    Do you want to play a game of DoKo?
411    yes<input type="radio" name="in" value="yes" />
412    no<input type="radio" name="in" value="no" /> <br />
413
414    Do you want to get an email for every card played or only if it your move?
415    every card<input type="radio" name="update" value="card" />
416    only on my turn<input type="radio" name="update" value="turn" /> <br />
417 <?php   
418              echo "<input type=\"hidden\" name=\"b\" value=\"$a\" />\n";
419              echo "\n";
420              echo "<input type=\"submit\" value=\"count me in\" />\n";
421              echo " </form>\n";
422            }
423        }
424
425 /*   ***
426  *   *  *
427  *   ***
428  *   *  *
429  *   ***
430  * yes? email him his hand, ask for solo, poverty, email every move or every card? 
431  */
432      if(isset($_REQUEST["b"]))
433        {
434          $b=$_REQUEST["b"];
435          
436          if( ereg("s",$player[$b]["option"])  && $game["init"]<4)
437            {
438              echo "just wait for the game to start";
439            }
440          else if(!isset($_REQUEST["in"])|| !isset($_REQUEST["update"]) )
441            {
442              echo "go back to ";
443              echo "<a href=\"index.php?a=$b\"> here and fill out the form </a> <br />";
444            }
445          else
446            { /* show the hand */
447              if($_REQUEST["in"]=="no")
448                {
449                  for($i=0;$i<4;$i++)
450                    {
451                      echo "Hello ".$player[$hash[$i]]["name"].",\n";
452                      echo "\n";
453                      echo "the game has been cancled due to the request of one of the players.\n";
454                    }
455                    $output = fopen("status.txt","w");
456                    if($output)
457                      fclose($output);
458                    else
459                      echo "problem opening file";
460                }
461              else
462                {
463                  if($_REQUEST["update"]=="card") $player[$b]["option"].="c";
464                  if($_REQUEST["update"]=="turn") $player[$b]["option"].="t";
465                  
466                  $player[$b]["option"].="i";
467                  
468                  save_status();
469                  
470                  $allcards = $player[$b]["cards"];
471                  $mycards = explode(";",$allcards);
472                  
473                  sort($mycards);
474                  echo "your cards are <br>";
475                  foreach($mycards as $card) 
476                    {
477                      display_card($card);
478                    }
479                  echo "<br />\n";   
480  ?>
481  <form action="index.php" method="post">
482    
483    do you want to play solo?
484    yes<input type="radio" name="solo" value="yes" />
485    no<input type="radio" name="solo" value="no" /> <br />
486
487    do you have a wedding?
488    yes<input type="radio" name="wedding" value="yes" />
489    no<input type="radio" name="wedding" value="no" /> <br />
490
491    do you have poverty?
492    yes<input type="radio" name="poverty" value="yes" />
493    no<input type="radio" name="poverty" value="no" /> <br />
494    
495 <?php   
496                  echo "<input type=\"hidden\" name=\"c\" value=\"$b\" />\n";
497                  echo "<input type=\"submit\" value=\"count me in\" />\n";
498                  
499                  echo "</form>\n";
500                }
501            }
502        }
503      if(isset($_REQUEST["c"]))
504        {
505          $c=$_REQUEST["c"];
506          
507          if(!isset($_REQUEST["solo"])|| !isset($_REQUEST["wedding"])|| !isset($_REQUEST["poverty"]) )
508            {
509              echo "go back to ";
510              echo "<a href=\"index.php?b=$c\"> here and fill out the form </a> <br />";
511            }
512          else if(  ereg("s",$player[$c]["option"]) && $game["init"]<4 )
513            {
514              echo "just wait for the game to start";
515            }
516          else if($game["init"]<4)
517            { 
518              echo "handle krankheit <br />";
519
520              echo "email this out: you're in. once everyone has filled out the form,";
521              echo "the game will start and you'll get an eamil about it";
522              
523              $player[$c]["option"].="s";
524
525              save_status();
526            }
527        }
528      if($game["init"]==4)
529        {
530          if(!isset($_REQUEST["me"]))
531            echo "a game is in progress, but you are not playing";
532          else
533            {
534              $me = $_REQUEST["me"];
535
536              echo "game in progress and you are in it<br>";
537              foreach($history as $play)
538                {
539                  echo "<br />";
540
541                  $trick = explode(":",$play);
542                  
543                  $last=-2;
544                  /* found old trick, display it */
545                  for($i=0;$i<sizeof($trick)-1;$i++)
546                    {
547                      $card=$trick[$i];
548                      if(ereg("->",$card))
549                        {
550                          $tmp = explode("->",$card);
551                          echo $player[$hash[$tmp[0]]]["name"]." played ";
552                          display_card($tmp[1]);
553                          $last=$tmp[0];
554                        }
555                    }
556                }
557              
558              $next = $last + 1;
559              if ($next>=4) 
560                $next -= 4 ;
561              if($last<0)
562                {
563                  $next=$history[sizeof($history)-1][0];
564                }
565
566
567              if(isset($_REQUEST["card"]))
568                {
569                  if($hash[$next]==$me)
570                    {
571                      $card=$_REQUEST["card"];
572                      $mycards = explode(";",$player[$me]["cards"]);
573                      if(in_array($card,$mycards))
574                        {
575                          $tmp=array();
576                          foreach($mycards as $m)
577                            if($m!=$card)
578                              $tmp[]=$m;
579                          $tmp2="";
580                          for($i=0;$i<sizeof($tmp)-1;$i++)
581                            {
582                              $tmp2.=$tmp[$i].";";
583                            }
584                          $tmp2.=$tmp[$i];
585                          $player[$me]["cards"]=$tmp2;
586                          
587                          if($last<0)
588                            {
589                              $history[sizeof($history)-1]="".$player[$me]["number"]."->".$card.":\n";
590                            }
591                          else
592                            {
593                              $tmp = explode(":",$history[sizeof($history)-1]);
594                              $tmp[sizeof($tmp)-1] = "".$player[$me]["number"]."->".$card.":";
595                              $history[sizeof($history)-1]=join(":",$tmp);
596                            }
597                          save_status();
598
599                          echo " you played ";
600                          display_card($card);
601                          /* send out email to players who want/need to get informed */
602                          for($i=0;$i<4;$i++)
603                            {
604                              $mynext=$next+1; if($mynext>3)$mynext-=4;
605                              if((ereg("c",$player[$hash[$i]]["option"]) || $i==$mynext) && $hash[$i]!=$me)
606                                {
607                                  echo " <br> ** $next** \n Hello ".$player[$hash[$i]]["name"].",\n";
608                                  echo "\n";
609                                  if($i==$mynext)
610                                    echo "it's your turn  now.\n";
611                                  echo $player[$me]["name"]. "has played the following card ".card_to_name($card)."\n";
612                                  
613                                  echo "<a href=\"index.php?me=hash1\"> player 1</a> <br />";
614                                  echo "<a href=\"index.php?me=hash2\"> player 2</a> <br />";
615                                  echo "<a href=\"index.php?me=hash3\"> player 3</a> <br />";
616                                  echo "<a href=\"index.php?me=hash4\"> player 4</a> <br />";
617                                  
618                                }
619                            }
620                        }
621                      else
622                        echo "seems like you don't have that card<br>";
623                           
624                    }
625                  
626                }
627              else if(isset($_REQUEST["win"]) && strlen($history[sizeof($history)-1])>3)
628                {
629                  $win=$_REQUEST["win"];
630                  $history[]=$win.":\n";
631                  /* count points of the last trick */
632                  $points=0;
633                  echo "<br>".$history[sizeof($history)-2]."is the last trick played<br>";
634                  $tmp = explode(":",$history[sizeof($history)-2]);
635                  for($i=0;$i<4;$i++)
636                    {
637                      $tmp2 = explode("->",$tmp[$i]);
638                      $c = $tmp2[1];
639                      $points += card_value($c);
640                      echo "adding card value ".card_value($c)."<br>";
641                    }
642                  $player[$hash[$win]]["points"]+=$points;
643                  echo "<br> ".$player[$hash[$win]]["name"]." won: $points Points <br>";
644
645                  save_status();
646                }
647              echo "<br />";
648
649              $tmp = explode(":",$history[sizeof($history)-1]);
650
651              if(sizeof($tmp)==5)
652                {
653                  ?>
654 <form action="index.php" method="post">
655                           
656 <?php 
657    for($i=0;$i<4;$i++)
658      echo $player[$hash[$i]]["name"]." <input type=\"radio\" name=\"win\" value=\"$i\" />";
659    echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />";
660 ?>
661 <input type="submit" value="who will win?" />
662
663 </form>
664 <?php
665                }
666              else if(sizeof($tmp)<5 && 1<sizeof($tmp) && !isset($_REQUEST["card"]))
667                {                 
668                  if(sizeof($tmp)==2 && strlen($tmp[0])==1)
669                    {
670                      $next=$tmp[0];
671                      echo "found the start of a new trick at $next<br>";
672                      if(strlen(trim($player[$me]["cards"]))==0)
673                        {
674                          echo "<br> game over, count points <br>";
675                          for($i=0;$i>4;$i++)
676                            {
677                              echo $player[$hash[$i]]["name"]." got ".$player[$hash[$i]]["points"]."<br>";
678                            }
679                        }
680                    }
681                  if($hash[$next]==$me && strlen(trim($player[$me]["cards"]))>0 )
682                    {
683                      
684                      echo "ITS YOUR TURN<br>";
685                      $allcards = trim($player[$me]["cards"]);
686                      $mycards = explode(";",$allcards);
687                      
688                      sort($mycards);
689                      echo "your cards are <br>";
690                      foreach($mycards as $card) 
691                        {
692                          display_link_card($card,$me);
693                        }
694                      echo "<br />\n";   
695                    }
696                  echo "<br />";
697                  
698                }
699            }
700        }
701
702  } 
703
704 ?>
705
706 </body>
707 </html>