cards are now deleted from your hand, still need to add some tests at the beginning...
[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 display_card($card)
16 {
17   if( $card/2 - (int)($card/2) == 0.5)
18     echo "<img src=\"cards/".$card.".png\" height=\"100\">";
19   else
20     echo "<img src=\"cards/".($card-1).".png\" height=\"100\">";
21   return;
22 }
23    function display_link_card($card,$me)
24 {
25   if( $card/2 - (int)($card/2) == 0.5)
26     echo "<a href=\"index.php?me=$me&card=$card\"><img src=\"cards/".$card.".png\" height=\"100\"></a>";
27   else
28     echo "<a href=\"index.php?me=$me&card=$card\"><img src=\"cards/".($card-1).".png\" height=\"100\"></a>";
29   return;
30 }
31
32 function save_status()
33 {
34   global $player,$game,$hash,$history;
35
36   $output = fopen("status.txt","w");
37   if ($output)
38     {
39       foreach($hash as $key)
40         {
41           $tmp="";
42           if( ereg("i",$player[$key]["option"]) )
43             $tmp.="i";
44           if( ereg("s",$player[$key]["option"]) )
45             $tmp.="s";
46           if( ereg("t",$player[$key]["option"]) )
47             $tmp.="t";
48           if( ereg("c",$player[$key]["option"]) )
49             $tmp.="c";
50           $player[$key]["option"]=$tmp;
51
52           fwrite($output,"".$player[$key]["hash"].":" );
53           fwrite($output,"".$player[$key]["name"].":" );
54           fwrite($output,"".$player[$key]["email"].":" );
55           fwrite($output,"".$player[$key]["option"].":" );
56           fwrite($output,"".$player[$key]["cards"] .":");
57           fwrite($output,"\n");
58         }
59       fwrite($output,"\n");
60       foreach($history as $line)
61         fwrite($output,$line);
62
63       fwrite($output,"\n");
64       fclose($output);
65     }
66   else
67     echo "can't open file for writing, please inform the admin.";
68   
69   return;
70 }
71
72 $history=array();
73
74 /* check for status file and read it, if possible */
75
76 if(file_exists("status.txt"))
77   $lines = file("status.txt");
78  else
79    die("no file");
80
81 /* check if we want to start a new game */
82 if( isset($_REQUEST["PlayerA"]) && 
83     isset($_REQUEST["PlayerB"]) && 
84     isset($_REQUEST["PlayerC"]) && 
85     isset($_REQUEST["PlayerD"]) && 
86     isset($_REQUEST["EmailA"]) && 
87     isset($_REQUEST["EmailB"]) && 
88     isset($_REQUEST["EmailC"]) && 
89     isset($_REQUEST["EmailD"]) )
90   {
91     $PlayerA = $_REQUEST["PlayerA"];
92     $PlayerB = $_REQUEST["PlayerB"];
93     $PlayerC = $_REQUEST["PlayerC"];
94     $PlayerD = $_REQUEST["PlayerD"];
95     $EmailA  = $_REQUEST["EmailA"] ;
96     $EmailB  = $_REQUEST["EmailB"] ;
97     $EmailC  = $_REQUEST["EmailC"] ;
98     $EmailD  = $_REQUEST["EmailD"] ;
99     
100     /* send out email, check for error with email */
101     echo "send out emails to everyone, asking if they want to join";
102     echo "use link <br>";
103     echo "<a href=\"index.php?a=hash1\"> player 1</a> <br />";
104     echo "<a href=\"index.php?a=hash2\"> player 2</a> <br />";
105     echo "<a href=\"index.php?a=hash3\"> player 3</a> <br />";
106     echo "<a href=\"index.php?a=hash4\"> player 4</a> <br />";
107     
108     /* read in random.txt */
109     if(file_exists("random.txt"))
110       $random = file("random.txt");
111     else
112       die("no random file");
113  
114     $randomNR = explode( ":", $random[1] );
115     
116     /* write initial status into file */
117     $output = fopen("status.txt","w");
118     if ($output)
119       {
120         fwrite($output, "hash1:".$PlayerA.":".$EmailA."::" );
121         for($i=0;$i<11;$i++)
122           fwrite($output,"$randomNR[$i];" );
123         fwrite($output,"$randomNR[11]:" ); $i++;
124         fwrite($output,"\n");
125         
126         fwrite($output, "hash2:$PlayerB:$EmailB::" );
127         for(;$i<23;$i++)
128           fwrite($output,"$randomNR[$i];" );
129         fwrite($output,"$randomNR[23]:" ); $i++;
130         fwrite($output,"\n");
131         
132         fwrite($output, "hash3:$PlayerC:$EmailC::" );
133         for(;$i<35;$i++)
134           fwrite($output,"$randomNR[$i];" );
135         fwrite($output,"$randomNR[35]:" ); $i++;
136         fwrite($output,"\n");
137         
138         fwrite($output, "hash4:$PlayerD:$EmailD::");
139         for(;$i<47;$i++)
140           fwrite($output,"$randomNR[$i];" );
141         fwrite($output,"$randomNR[47]:" );
142         fwrite($output,"\n");
143         
144         fclose($output);
145       }
146     else
147       echo "can't open file for writing";
148   };
149
150 /* test if a game is running, else output everything for a new game */
151 if(sizeof($lines)<2)
152   {
153 ?>
154  <p> no game in progress, please input 4 names and email addresses </p>
155  <form action="index.php" method="post">
156    Name:  <input name="PlayerA" type="text" size="10" maxlength="20" /> 
157    Email: <input name="EmailA"  type="text" size="10" maxlength="20" /> <br />
158
159    Name:  <input name="PlayerB" type="text" size="10" maxlength="20" /> 
160    Email: <input name="EmailB"  type="text" size="10" maxlength="20" /> <br />
161
162    Name:  <input name="PlayerC" type="text" size="10" maxlength="20" /> 
163    Email: <input name="EmailC"  type="text" size="10" maxlength="20" /> <br />
164
165    Name:  <input name="PlayerD" type="text" size="10" maxlength="20" /> 
166    Email: <input name="EmailD"  type="text" size="10" maxlength="20" /> <br />
167
168    <input type="submit" value="start game" />
169  </form>
170 <?php
171    }
172  else
173    { /* load game status */
174      $game["init"]=0;
175      
176      $tmp = explode( ":",$lines[0]);
177      $hash[0]   = $tmp[0];
178      $player[$tmp[0]]["number"] = 0;
179      $player[$tmp[0]]["hash"]   = $tmp[0];
180      $player[$tmp[0]]["name"]   = $tmp[1];
181      $player[$tmp[0]]["email"]  = $tmp[2];
182      $player[$tmp[0]]["option"] = $tmp[3];
183      $player[$tmp[0]]["cards"]  = $tmp[4];
184      if(ereg("s",$tmp[3])) $game["init"]++;
185
186      $tmp = explode( ":",$lines[1]);
187      $hash[1]   = $tmp[0];
188      $player[$tmp[0]]["number"] = 1;
189      $player[$tmp[0]]["hash"]   = $tmp[0];
190      $player[$tmp[0]]["name"]   = $tmp[1];
191      $player[$tmp[0]]["email"]  = $tmp[2];
192      $player[$tmp[0]]["option"] = $tmp[3]; 
193      $player[$tmp[0]]["cards"]  = $tmp[4];
194      if(ereg("s",$tmp[3])) $game["init"]++;
195      
196      $tmp = explode( ":",$lines[2]);
197      $hash[2]   = $tmp[0];
198      $player[$tmp[0]]["number"] = 2;
199      $player[$tmp[0]]["hash"]   = $tmp[0];
200      $player[$tmp[0]]["name"]   = $tmp[1];
201      $player[$tmp[0]]["email"]  = $tmp[2];
202      $player[$tmp[0]]["option"] = $tmp[3];
203      $player[$tmp[0]]["cards"]  = $tmp[4];
204      if(ereg("s",$tmp[3])) $game["init"]++;
205      
206      $tmp = explode( ":",$lines[3]);
207      $hash[3]   = $tmp[0];
208      $player[$tmp[0]]["number"] = 3;
209      $player[$tmp[0]]["hash"]   = $tmp[0];
210      $player[$tmp[0]]["name"]   = $tmp[1];
211      $player[$tmp[0]]["email"]  = $tmp[2];
212      $player[$tmp[0]]["option"] = $tmp[3];
213      $player[$tmp[0]]["cards"]  = $tmp[4];
214      if(ereg("s",$tmp[3])) $game["init"]++;
215
216      /* save the game history */
217      for($i=4;$i<sizeof($lines);$i++)
218        {
219          if(!ereg("^[[:space:]]*$",trim($lines[$i])))
220            {
221              $history[] = $lines[$i];
222            }
223        }
224      if(sizeof($history)== 0)
225        $history[] = "0:";
226
227 /*     **
228  *    *  *
229  *    ****
230  *    *  *
231  *
232  * check if a player wants to accept a game 
233  */
234      
235      if(isset($_REQUEST["a"]))
236        {
237          $a=$_REQUEST["a"];
238          
239          if( ereg("[is]",$player[$a]["option"]) &&  $game["init"]<4)
240            {
241              echo "just wait for the game to start";
242            }
243          else  if( !ereg("[is]",$player[$a]["option"]) )
244            {
245
246  ?>
247  <form action="index.php" method="post">
248    Do you want to play a game of DoKo?
249    yes<input type="radio" name="in" value="yes" />
250    no<input type="radio" name="in" value="no" /> <br />
251
252    Do you want to get an email for every card played or only if it your move?
253    every card<input type="radio" name="update" value="card" />
254    only on my turn<input type="radio" name="update" value="turn" /> <br />
255 <?php   
256              echo "<input type=\"hidden\" name=\"b\" value=\"$a\" />\n";
257              echo "\n";
258              echo "<input type=\"submit\" value=\"count me in\" />\n";
259              echo " </form>\n";
260            }
261        }
262
263 /*   ***
264  *   *  *
265  *   ***
266  *   *  *
267  *   ***
268  * yes? email him his hand, ask for solo, poverty, email every move or every card? 
269  */
270      if(isset($_REQUEST["b"]))
271        {
272          $b=$_REQUEST["b"];
273          
274          if( ereg("s",$player[$b]["option"])  && $game["init"]<4)
275            {
276              echo "just wait for the game to start";
277            }
278          else if(!isset($_REQUEST["in"])|| !isset($_REQUEST["update"]) )
279            {
280              echo "go back to ";
281              echo "<a href=\"index.php?a=$b\"> here and fill out the form </a> <br />";
282            }
283          else
284            { /* show the hand */
285              if($_REQUEST["update"]=="card") $player[$b]["option"].="c";
286              if($_REQUEST["update"]=="turn") $player[$b]["option"].="t";
287              
288              $player[$b]["option"].="i";
289              
290              save_status();
291              
292              $allcards = $player[$b]["cards"];
293              $mycards = explode(";",$allcards);
294              
295              sort($mycards);
296              echo "your cards are <br>";
297              foreach($mycards as $card) 
298                {
299                  display_card($card);
300                }
301              echo "<br />\n";   
302  ?>
303  <form action="index.php" method="post">
304    
305    do you want to play solo?
306    yes<input type="radio" name="solo" value="yes" />
307    no<input type="radio" name="solo" value="no" /> <br />
308
309    do you have a wedding?
310    yes<input type="radio" name="wedding" value="yes" />
311    no<input type="radio" name="wedding" value="no" /> <br />
312
313    do you have poverty?
314    yes<input type="radio" name="poverty" value="yes" />
315    no<input type="radio" name="poverty" value="no" /> <br />
316    
317 <?php   
318              echo "<input type=\"hidden\" name=\"c\" value=\"$b\" />\n";
319              echo "<input type=\"submit\" value=\"count me in\" />\n";
320
321              echo "</form>\n";
322            }
323        }
324      if(isset($_REQUEST["c"]))
325        {
326          $c=$_REQUEST["c"];
327          
328          if(!isset($_REQUEST["solo"])|| !isset($_REQUEST["wedding"])|| !isset($_REQUEST["poverty"]) )
329            {
330              echo "go back to ";
331              echo "<a href=\"index.php?b=$c\"> here and fill out the form </a> <br />";
332            }
333          else if(  ereg("s",$player[$c]["option"]) && $game["init"]<4 )
334            {
335              echo "just wait for the game to start";
336            }
337          else if($game["init"]<4)
338            { 
339              echo "handle krankheit <br />";
340
341              echo "email this out: you're in. once everyone has filled out the form,";
342              echo "the game will start and you'll get an eamil about it";
343              
344              $player[$c]["option"].="s";
345
346              save_status();
347            }
348        }
349      if($game["init"]==4)
350        {
351          if(!isset($_REQUEST["me"]))
352            echo "a game is in progress, but you are not playing";
353          else
354            {
355              $me = $_REQUEST["me"];
356
357              echo "game in progress and you are in it<br>";
358              foreach($history as $play)
359                {
360                  echo "<br />";
361
362                  $trick = explode(":",$play);
363                  
364                  $last=-2;
365                  /* found old trick, display it */
366                  for($i=0;$i<sizeof($trick)-1;$i++)
367                    {
368                      $card=$trick[$i];
369                      if(ereg("->",$card))
370                        {
371                          $tmp = explode("->",$card);
372                          echo $player[$hash[$tmp[0]]]["name"]." played ";
373                          display_card($tmp[1]);
374                          $last=$tmp[0];
375                        }
376                    }
377                }
378              
379              $next = $last + 1;
380              if ($next>=4) 
381                $next -= 4 ;
382              if($last<0)
383                {
384                  $next=$history[sizeof($history)-1][0];
385                }
386
387
388              if(isset($_REQUEST["card"]))
389                {
390                  if($hash[$next]==$me)
391                    {
392                      $card=$_REQUEST["card"];
393                      echo "EMAIL: you played $card ";
394                      $mycards = explode(";",$player[$me]["cards"]);
395                      if(in_array($card,$mycards))
396                        {
397                          $tmp=array();
398                          echo "<br>";
399                          foreach($mycards as $m)
400                            {
401                              if($m!=$card)
402                                {
403                                  $tmp[]=$m;
404                                  echo "adding card $m <br>";
405                                }
406                            }
407                          $tmp2="";
408                          for($i=0;$i<sizeof($tmp)-1;$i++)
409                            {
410                              $tmp2.=$tmp[$i].";";
411                              echo "adding card $tmp2 at $i <br>";
412                            }
413                          $tmp2.=$tmp[$i];
414                          echo "adding card $tmp2 at $i *<br>";
415                          $player[$me]["cards"]=$tmp2;
416                          
417                          if($last<0)
418                            {
419                              $history[sizeof($history)-1]="".$player[$me]["number"]."->".$card.":\n";
420                            }
421                          else
422                            {
423                              $tmp = explode(":",$history[sizeof($history)-1]);
424                              $tmp[sizeof($tmp)-1] = "".$player[$me]["number"]."->".$card.":";
425                              $history[sizeof($history)-1]=join(":",$tmp);
426                            }
427                          save_status();
428                          
429                          display_card($card);
430                        }
431                      else
432                        echo "seems like you don't have that card<br>";
433                           
434                    }
435                  
436                }
437              else if(isset($_REQUEST["win"]) && strlen($history[sizeof($history)-1])>3)
438                {
439                  $win=$_REQUEST["win"];
440                  $history[]=$win.":\n";
441                  echo "juhu someone won:$win <br>";
442
443                  save_status();
444                }
445              echo "<br />";
446
447              $tmp = explode(":",$history[sizeof($history)-1]);
448
449              echo sizeof($tmp)." tmp ;;".strlen($tmp[0])." len tmp0  <br>";
450
451              if(sizeof($tmp)==5)
452                {
453                  ?>
454 <form action="index.php" method="post">
455                           
456  
457    player1<input type="radio" name="win" value="0" />
458    player2<input type="radio" name="win" value="1" />
459    player3<input type="radio" name="win" value="2" />
460    player4<input type="radio" name="win" value="3" />
461 <input type="hidden" name="me" value="hash1" />
462 <input type="submit" value="who will win?" />
463
464 </form>
465 <?php
466                }
467              else if(sizeof($tmp)<5 && 1<sizeof($tmp) && !isset($_REQUEST["card"]))
468                {                 
469                  if(sizeof($tmp)==2 && strlen($tmp[0])==1)
470                    {
471                      $next=$tmp[0];
472                      echo "found the start of a new trick at $next<br>";
473                    }
474                  if($hash[$next]==$me)
475                    {
476                      
477                      echo "ITS YOUR TURN<br>";
478                      $allcards = $player[$me]["cards"];
479                      $mycards = explode(";",$allcards);
480                      
481                      sort($mycards);
482                      echo "your cards are <br>";
483                      foreach($mycards as $card) 
484                        {
485                          display_link_card($card,$me);
486                        }
487                      echo "<br />\n";   
488                    }
489                  echo "<br />";
490                  
491                }
492            }
493        }
494
495  }      /* is this the last player that needs to accept? */
496          /* yes, figure out who starts, send out email to first player */
497    /* no, email the rest to cancel game */
498
499 /* player wants to make a move? */
500   /* check if it is this players turn it is (if it's the players turn, convert cards into links) */
501   /* if it is the last card played*/
502      /* add checkbox for who one the trick */
503      /* email next player */
504      /* last card played? */
505         /* count score for each player */
506 ?>
507
508 </body>
509 </html>