still missing some checks, but got a lot further
[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
73 /* check for status file and read it, if possible */
74
75 if(file_exists("status.txt"))
76   $lines = file("status.txt");
77  else
78    die("no file");
79
80 /* check if we want to start a new game */
81 if( isset($_REQUEST["PlayerA"]) && 
82     isset($_REQUEST["PlayerB"]) && 
83     isset($_REQUEST["PlayerC"]) && 
84     isset($_REQUEST["PlayerD"]) && 
85     isset($_REQUEST["EmailA"]) && 
86     isset($_REQUEST["EmailB"]) && 
87     isset($_REQUEST["EmailC"]) && 
88     isset($_REQUEST["EmailD"]) )
89   {
90     $PlayerA = $_REQUEST["PlayerA"];
91     $PlayerB = $_REQUEST["PlayerB"];
92     $PlayerC = $_REQUEST["PlayerC"];
93     $PlayerD = $_REQUEST["PlayerD"];
94     $EmailA  = $_REQUEST["EmailA"] ;
95     $EmailB  = $_REQUEST["EmailB"] ;
96     $EmailC  = $_REQUEST["EmailC"] ;
97     $EmailD  = $_REQUEST["EmailD"] ;
98     
99     /* send out email, check for error with email */
100     echo "send out emails to everyone, asking if they want to join";
101     echo "use link <br>";
102     echo "<a href=\"index.php?a=hash1\"> player 1</a> <br />";
103     echo "<a href=\"index.php?a=hash2\"> player 2</a> <br />";
104     echo "<a href=\"index.php?a=hash3\"> player 3</a> <br />";
105     echo "<a href=\"index.php?a=hash4\"> player 4</a> <br />";
106     
107     /* read in random.txt */
108     if(file_exists("random.txt"))
109       $random = file("random.txt");
110     else
111       die("no random file");
112  
113     $randomNR = explode( ":", $random[1] );
114     
115     /* write initial status into file */
116     $output = fopen("status.txt","w");
117     if ($output)
118       {
119         fwrite($output, "hash1:".$PlayerA.":".$EmailA."::" );
120         for($i=0;$i<11;$i++)
121           fwrite($output,"$randomNR[$i];" );
122         fwrite($output,"$randomNR[11]:" ); $i++;
123         fwrite($output,"\n");
124         
125         fwrite($output, "hash2:$PlayerB:$EmailB::" );
126         for(;$i<23;$i++)
127           fwrite($output,"$randomNR[$i];" );
128         fwrite($output,"$randomNR[23]:" ); $i++;
129         fwrite($output,"\n");
130         
131         fwrite($output, "hash3:$PlayerC:$EmailC::" );
132         for(;$i<35;$i++)
133           fwrite($output,"$randomNR[$i];" );
134         fwrite($output,"$randomNR[35]:" ); $i++;
135         fwrite($output,"\n");
136         
137         fwrite($output, "hash4:$PlayerD:$EmailD::");
138         for(;$i<47;$i++)
139           fwrite($output,"$randomNR[$i];" );
140         fwrite($output,"$randomNR[47]:" );
141         fwrite($output,"\n");
142         
143         fclose($output);
144       }
145     else
146       echo "can't open file for writing";
147   };
148
149 /* test if a game is running, else output everything for a new game */
150 if(sizeof($lines)<2)
151   {
152 ?>
153  <p> no game in progress, please input 4 names and email addresses </p>
154  <form action="index.php" method="post">
155    Name:  <input name="PlayerA" type="text" size="10" maxlength="20" /> 
156    Email: <input name="EmailA"  type="text" size="10" maxlength="20" /> <br />
157
158    Name:  <input name="PlayerB" type="text" size="10" maxlength="20" /> 
159    Email: <input name="EmailB"  type="text" size="10" maxlength="20" /> <br />
160
161    Name:  <input name="PlayerC" type="text" size="10" maxlength="20" /> 
162    Email: <input name="EmailC"  type="text" size="10" maxlength="20" /> <br />
163
164    Name:  <input name="PlayerD" type="text" size="10" maxlength="20" /> 
165    Email: <input name="EmailD"  type="text" size="10" maxlength="20" /> <br />
166
167    <input type="submit" value="start game" />
168  </form>
169 <?php
170    }
171  else
172    { /* load game status */
173      $game["init"]=0;
174      
175      $tmp = explode( ":",$lines[0]);
176      $hash[]   = $tmp[0];
177      $player[$tmp[0]]["number"] = 0;
178      $player[$tmp[0]]["hash"]   = $tmp[0];
179      $player[$tmp[0]]["name"]   = $tmp[1];
180      $player[$tmp[0]]["email"]  = $tmp[2];
181      $player[$tmp[0]]["option"] = $tmp[3];
182      $player[$tmp[0]]["cards"]  = $tmp[4];
183      if(ereg("s",$tmp[3])) $game["init"]++;
184
185      $tmp = explode( ":",$lines[1]);
186      $hash[]   = $tmp[0];
187      $player[$tmp[0]]["number"] = 1;
188      $player[$tmp[0]]["hash"]   = $tmp[0];
189      $player[$tmp[0]]["name"]   = $tmp[1];
190      $player[$tmp[0]]["email"]  = $tmp[2];
191      $player[$tmp[0]]["option"] = $tmp[3]; 
192      $player[$tmp[0]]["cards"]  = $tmp[4];
193      if(ereg("s",$tmp[3])) $game["init"]++;
194      
195      $tmp = explode( ":",$lines[2]);
196      $hash[]   = $tmp[0];
197      $player[$tmp[0]]["number"] = 2;
198      $player[$tmp[0]]["hash"]   = $tmp[0];
199      $player[$tmp[0]]["name"]   = $tmp[1];
200      $player[$tmp[0]]["email"]  = $tmp[2];
201      $player[$tmp[0]]["option"] = $tmp[3];
202      $player[$tmp[0]]["cards"]  = $tmp[4];
203      if(ereg("s",$tmp[3])) $game["init"]++;
204      
205      $tmp = explode( ":",$lines[3]);
206      $hash[]   = $tmp[0];
207      $player[$tmp[0]]["number"] = 3;
208      $player[$tmp[0]]["hash"]   = $tmp[0];
209      $player[$tmp[0]]["name"]   = $tmp[1];
210      $player[$tmp[0]]["email"]  = $tmp[2];
211      $player[$tmp[0]]["option"] = $tmp[3];
212      $player[$tmp[0]]["cards"]  = $tmp[4];
213      if(ereg("s",$tmp[3])) $game["init"]++;
214
215      /* save the game history */
216      for($i=4;$i<sizeof($lines);$i++)
217        {
218          if($lines[$i]!="\n")
219            $history[]=$lines[$i];
220        }
221
222 /*     **
223  *    *  *
224  *    ****
225  *    *  *
226  *
227  * check if a player wants to accept a game 
228  */
229      
230      if(isset($_REQUEST["a"]))
231        {
232          $a=$_REQUEST["a"];
233          
234          if( ereg("[is]",$player[$a]["option"]) &&  $game["init"]<4)
235            {
236              echo "just wait for the game to start";
237            }
238          else 
239            {
240
241  ?>
242  <form action="index.php" method="post">
243    Do you want to play a game of DoKo?
244    yes<input type="radio" name="in" value="yes" />
245    no<input type="radio" name="in" value="no" /> <br />
246
247    Do you want to get an email for every card played or only if it your move?
248    every card<input type="radio" name="update" value="card" />
249    only on my turn<input type="radio" name="update" value="turn" /> <br />
250 <?php   
251              echo "<input type=\"hidden\" name=\"b\" value=\"$a\" />\n";
252              echo "\n";
253              echo "<input type=\"submit\" value=\"count me in\" />\n";
254              echo " </form>\n";
255            }
256        }
257
258 /*   ***
259  *   *  *
260  *   ***
261  *   *  *
262  *   ***
263  * yes? email him his hand, ask for solo, poverty, email every move or every card? 
264  */
265      if(isset($_REQUEST["b"]))
266        {
267          $b=$_REQUEST["b"];
268          
269          if( ereg("[is]",$player[$b]["option"])  && $game["init"]<4)
270            {
271              echo "just wait for the game to start";
272            }
273          else if(!isset($_REQUEST["in"])|| !isset($_REQUEST["update"]) )
274            {
275              echo "go back to ";
276              echo "<a href=\"index.php?a=$b\"> here and fill out the form </a> <br />";
277            }
278          else
279            { /* show the hand */
280              if($_REQUEST["update"]=="card") $player[$b]["option"].="c";
281              if($_REQUEST["update"]=="turn") $player[$b]["option"].="t";
282              
283              $player[$b]["option"].="i";
284              
285              save_status();
286              
287              $allcards = $player[$b]["cards"];
288              $mycards = explode(";",$allcards);
289              
290              sort($mycards);
291              echo "your cards are <br>";
292              foreach($mycards as $card) 
293                {
294                  display_card($card);
295                }
296              echo "<br />\n";   
297  ?>
298  <form action="index.php" method="post">
299    
300    do you want to play solo?
301    yes<input type="radio" name="solo" value="yes" />
302    no<input type="radio" name="solo" value="no" /> <br />
303
304    do you have a wedding?
305    yes<input type="radio" name="wedding" value="yes" />
306    no<input type="radio" name="wedding" value="no" /> <br />
307
308    do you have poverty?
309    yes<input type="radio" name="poverty" value="yes" />
310    no<input type="radio" name="poverty" value="no" /> <br />
311    
312 <?php   
313              echo "<input type=\"hidden\" name=\"c\" value=\"$b\" />\n";
314              echo "<input type=\"submit\" value=\"count me in\" />\n";
315
316              echo "</form>\n";
317            }
318        }
319      if(isset($_REQUEST["c"]))
320        {
321          $c=$_REQUEST["c"];
322          
323          if(!isset($_REQUEST["solo"])|| !isset($_REQUEST["wedding"])|| !isset($_REQUEST["poverty"]) )
324            {
325              echo "go back to ";
326              echo "<a href=\"index.php?b=$c\"> here and fill out the form </a> <br />";
327            }
328          else if(  ereg("s",$player[$c]["option"]) && $game["init"]<4 )
329            {
330              echo "just wait for the game to start";
331            }
332          else if($game["init"]<4)
333            { 
334              echo "handle krankheit <br />";
335
336              echo "email this out: you're in. once everyone has filled out the form,";
337              echo "the game will start and you'll get an eamil about it";
338              
339              $player[$c]["option"].="s";
340
341              save_status();
342            }
343        }
344      if($game["init"]==4)
345        {
346          if(!isset($_REQUEST["me"]))
347            echo "a game is in progress, but you are not playing";
348          else
349            {
350              $me = $_REQUEST["me"];
351              if(isset($_REQUEST["card"]))
352                {
353                  $card=$_REQUEST["card"];
354                  echo "EMAIL: you played $card <br>";
355                  echo "check,if card correct  remove card from deck <br>";
356                  
357
358                  $tmp = explode(":",$history[sizeof($history)-1]);
359                  $tmp[sizeof($tmp)-1] = "".$player[$me]["number"]."->".$card.":";
360                  $history[sizeof($history)-1]=join(":",$tmp);
361
362                  save_status();
363                }
364              echo "game in progress and you are in it<br>";
365              foreach($history as $play)
366                {
367                  $trick = explode(":",$play);
368
369                  $last=-1;
370                  /* found old trick, display it */
371                  for($i=0;$i<sizeof($trick)-1;$i++)
372                    {
373                      $card=$trick[$i];
374                      $tmp = explode("->",$card);
375                      echo $player[$hash[$tmp[0]]]["name"]." played ";
376                      display_card($tmp[1]);
377                      $last=$tmp[0];
378                    }
379                  echo "<br />";
380                  
381                  
382                  $next = $last + 1;
383                  if ($next>=4) 
384                    $next -= 4 ;
385                      
386                  switch(sizeof($trick))
387                    {
388                    case 1:
389                      echo "new trick , next player ".$trick[0]." <br>";
390                      break;
391                    case 4:
392                      echo "figure out who will win<br>";
393                    case 2:
394                    case 3:
395                      echo "some played $next";
396                      if($hash[$next]==$me)
397                        {
398                          
399                        echo "ITS YOUR TURN<br>";
400                        $allcards = $player[$me]["cards"];
401                        $mycards = explode(";",$allcards);
402                        
403                        sort($mycards);
404                        echo "your cards are <br>";
405                        foreach($mycards as $card) 
406                          {
407                            display_link_card($card,$me);
408                          }
409                        echo "<br />\n";   
410                        }
411                      echo "<br />";
412                      break;
413                    default:
414                      echo "default<br>";
415                    }
416                }
417            }
418        }
419      
420 }      /* is this the last player that needs to accept? */
421          /* yes, figure out who starts, send out email to first player */
422    /* no, email the rest to cancel game */
423
424 /* player wants to make a move? */
425   /* check if it is this players turn it is (if it's the players turn, convert cards into links) */
426   /* if it is the last card played*/
427      /* add checkbox for who one the trick */
428      /* email next player */
429      /* last card played? */
430         /* count score for each player */
431 ?>
432
433 </body>
434 </html>