58dd1839cb9680f0beb144b30635069bd67e14d4
[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>e-Doko</title>
7      <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type" />
8      <link rel="stylesheet" type="text/css" href="standard.css" />      
9      <script type="text/javascript">
10        function hl(num) {
11          if(document.getElementById){
12            var i;
13            for(i=1;i<13;i++){
14              if(document.getElementById("trick"+i))
15                document.getElementById("trick"+i).style.display = 'none';
16            }
17            document.getElementById("trick"+num).style.display = 'block';
18          }
19        }
20        function high_last(){
21          if(document.getElementById){
22            var i;
23            for(i=12;i>0;i--) {
24              if(document.getElementById("trick"+i))
25                {
26                  hl(i);
27                  break;
28                }
29            }
30          }
31        }
32      </script>
33   </head>
34 <body onload="high_last();">
35 <div class="header">
36 <h1> Welcome to E-Doko </h1>
37 </div>
38
39 <?php
40      
41 include_once("functions.php");
42 include_once("db.php");
43
44 DB_open();
45
46 /* end header */
47
48 /*****************  M A I N **************************/
49
50 /* check if we want to start a new game */
51 if(isset($_REQUEST["new"]))
52   {
53 ?>
54     <p> no game in progress, please input 4 names and email addresses, please make sure that the addresses are correct! </p>
55  <form action="index.php" method="post">
56    Name:  <input name="PlayerA" type="text" size="10" maxlength="20" /> 
57    Name:  <input name="PlayerB" type="text" size="10" maxlength="20" /> 
58    Name:  <input name="PlayerC" type="text" size="10" maxlength="20" /> 
59    Name:  <input name="PlayerD" type="text" size="10" maxlength="20" /> 
60
61    <input type="submit" value="start game" />
62  </form>
63 <?php
64   } 
65 /* end start a new game */
66
67 /*check if everything is ready to set up a new game */
68 else if( isset($_REQUEST["PlayerA"]) && 
69     isset($_REQUEST["PlayerB"]) && 
70     isset($_REQUEST["PlayerC"]) && 
71     isset($_REQUEST["PlayerD"]) )
72   {
73     $PlayerA = $_REQUEST["PlayerA"];
74     $PlayerB = $_REQUEST["PlayerB"];
75     $PlayerC = $_REQUEST["PlayerC"];
76     $PlayerD = $_REQUEST["PlayerD"];
77     
78     $EmailA  = DB_get_email_by_name($PlayerA);
79     $EmailB  = DB_get_email_by_name($PlayerB);
80     $EmailC  = DB_get_email_by_name($PlayerC);
81     $EmailD  = DB_get_email_by_name($PlayerD);
82     
83     if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
84       {
85         echo "couldn't find one of the names, please start a new game";
86         exit();
87       }
88     
89     $useridA  = DB_get_userid_by_name($PlayerA);
90     $useridB  = DB_get_userid_by_name($PlayerB);
91     $useridC  = DB_get_userid_by_name($PlayerC);
92     $useridD  = DB_get_userid_by_name($PlayerD);
93     
94     /* create random numbers */
95     $randomNR       = create_array_of_random_numbers();
96     $randomNRstring = join(":",$randomNR);
97     
98     /* create game */
99     mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', NULL, NULL,'pre', NULL)");
100     $game_id = mysql_insert_id();
101     
102     
103     $hashA = md5("AGameOfDoko".$game_id.$PlayerA.$EmailA);
104     $hashB = md5("AGameOfDoko".$game_id.$PlayerB.$EmailB);
105     $hashC = md5("AGameOfDoko".$game_id.$PlayerC.$EmailC);
106     $hashD = md5("AGameOfDoko".$game_id.$PlayerD.$EmailD);
107     
108     /* create hands */
109     mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridA).
110                 ", ".DB_quote_smart($hashA).", 'start','1',NULL,NULL,NULL,'false','false',NULL)");
111     $hand_idA = mysql_insert_id();                                                             
112     mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridB).
113                 ", ".DB_quote_smart($hashB).", 'start','2',NULL,NULL,NULL,'false','false',NULL)");
114     $hand_idB = mysql_insert_id();                                                             
115     mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridC).
116                 ", ".DB_quote_smart($hashC).", 'start','3',NULL,NULL,NULL,'false','false',NULL)");
117     $hand_idC = mysql_insert_id();                                                             
118     mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridD).
119                 ", ".DB_quote_smart($hashD).", 'start','4',NULL,NULL,NULL,'false','false',NULL)");
120     $hand_idD = mysql_insert_id();
121     
122     /* save cards */
123     for($i=0;$i<12;$i++)
124       mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idA', '".$randomNR[$i]."', 'false')");
125     for($i=12;$i<24;$i++)
126       mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idB', '".$randomNR[$i]."', 'false')");
127     for($i=24;$i<36;$i++)
128       mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idC', '".$randomNR[$i]."', 'false')");
129     for($i=36;$i<48;$i++)
130       mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idD', '".$randomNR[$i]."', 'false')");
131
132     /* send out email, check for error with email */
133     $message = "\n".
134       "you are invited to play a game of DoKo (that is to debug the program ;).\n".
135       "Place comments and bug reports here:\n".
136       "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
137       "The whole round would consist of the following players:\n".
138       "$PlayerA\n".
139       "$PlayerB\n".
140       "$PlayerC\n".
141       "$PlayerD\n\n".
142       "If you want to join this game, please follow this link:\n\n".
143       " ".$host."?me=";
144     
145     mymail($EmailA,"You are invited to a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
146     mymail($EmailB,"You are invited to a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
147     mymail($EmailC,"You are invited to a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
148     mymail($EmailD,"You are invited to a game of DoKo","Hello $PlayerD,\n".$message.$hashD);
149     
150     /*
151
152    do things like:
153      select fullname,strength,suite,game_id from hand_card left join hand on hand_id=hand.id left join user on user.id=user_id left join card on card_id=card.id where game_id='4'
154
155     */
156     
157   }    
158 /* end set up a new game */
159
160 else if(isset($_REQUEST["me"]))
161   {
162      /* handle request from one specifig player,
163       * the hash is set on a  per game base, so first just handle this game
164       * perhaps also show links to other games in a sidebar
165       */
166     
167     $me = $_REQUEST["me"];
168     
169     $myid = DB_get_userid_by_hash($me);
170     
171     if(!$myid)
172       {
173         echo "Can't find you in the database, please check the url.<br />\n";
174         echo "perhaps the game has been cancled.";
175          exit();
176       }
177     
178     $myname   = DB_get_name_by_hash($me);
179     $mystatus = DB_get_status_by_hash($me);
180     
181     switch($mystatus)
182       {
183       case 'start':
184         check_want_to_play($me);
185         DB_set_hand_status_by_hash($me,'init');
186         break;
187         
188       case 'init':
189         if( !isset($_REQUEST["in"]) || !isset($_REQUEST["update"]))
190           {
191             DB_set_hand_status_by_hash($me,'start');
192             echo "you need to answer both question";
193           }
194         else
195           {
196             if($_REQUEST["in"] == "no")
197               {
198                 echo "TODO: email everyone that the game has been canceld<br />";
199                  /*something like
200                  for($i=0;$i<4;$i++)
201                    {
202                      $message = "Hello ".$player[$hash[$i]]["name"].",\n\n".
203                        "the game has been canceled due to the request of one of the players.\n";
204                      mymail($player[$hash[$i]]["email"],"[DoKo-Debug] the game has been canceled",$message); 
205                    }
206                  */
207                 DB_cancel_game($me);
208               }
209             else
210               {
211                 echo "thanks for joining the game... please scroll down";
212                 echo "TODO: make this page nicer<br />";
213                 echo "TODO: set card pref<br />";
214                 
215                 $mycards = DB_get_hand($me);
216                 sort($mycards);
217                 echo "<p class=\"mycards\">your cards are: <br />\n";
218                 foreach($mycards as $card) 
219                   display_card($card);
220                 echo "</p>\n";   
221                 
222                 check_for_sickness($me,$mycards);
223                 
224                 DB_set_hand_status_by_hash($me,'check');
225               }
226            }
227         break;
228         
229       case 'check':
230         echo "no checking at the moment... you need to play a normal game";
231         if(!isset($_REQUEST["solo"])    || 
232            !isset($_REQUEST["wedding"]) ||
233            !isset($_REQUEST["poverty"]) ||
234            !isset($_REQUEST["nines"]) )
235           {
236             DB_set_hand_status_by_hash($me,'init');
237             /* problem: by setting it back to init, variables "in" and "update" are 
238              * not set, so the player will be send back to the start, after seeing his hand
239              */
240             echo "you need to fill out the form";
241           }
242         else
243           {
244             if( $_REQUEST["solo"]!="No")
245               {
246                 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
247                 DB_set_sickness_by_hash($me,"solo");
248               }
249             else if($_REQUEST["wedding"] == "yes")
250               {
251                 echo "wedding was chosen<br />\n";
252                 DB_set_sickness_by_hash($me,"wedding");
253               }
254             else if($_REQUEST["poverty"] == "yes")
255               {
256                 echo "poverty was chosen<br />\n";
257                 DB_set_sickness_by_hash($me,"poverty");
258               }
259             else if($_REQUEST["nines"] == "yes")
260               {
261                 echo "nines was chosen<br />\n";
262                  DB_set_sickness_by_hash($me,"nines");
263               }
264           }
265         DB_set_hand_status_by_hash($me,'poverty');
266         
267         /* check all players and set game to final result, e.g. solo, wedding, povert, redeal */
268         
269         /* reset solo, etc from players who did say something, but it didn't matter? */
270         break;
271       case 'poverty':
272         echo "<br />poverty not handeled at the moment... you need to play a normal game<br />";
273         
274         /* only set this after all poverty, etc. are handeled*/
275         DB_set_hand_status_by_hash($me,'play');
276         break;
277       case 'play':
278         display_news();
279         display_status();
280         
281          /* get game id */
282         $gameid = DB_get_gameid_by_hash($me);
283         
284         /* get trick ids */
285         $result = mysql_query("SELECT hand_card.card_id as card,".
286                               "       user.fullname as name,".
287                               "       hand.position as position,".
288                               "       play.sequence as sequence, ".
289                               "       hand.hash     as hash,     ".
290                               "       trick.id ".
291                               "FROM Trick ".
292                               "LEFT JOIN Play ON trick.id=play.trick_id ".
293                               "LEFT JOIN Hand_Card ON play.hand_card_id=hand_card.id ".
294                               "LEFT JOIN Hand ON hand_card.hand_id=hand.id ".
295                               "LEFT JOIN User ON user.id=hand.user_id ".
296                               "WHERE trick.game_id='".$gameid."' ".
297                               "ORDER BY trick.id,sequence ASC");
298         
299         
300         $trickNR = 1;
301         
302         $lasttrick = DB_get_max_trickid($gameid);
303         
304         $play = array(); /* needed to calculate winner later  */
305         $seq=1;          
306         $pos=0;
307         
308         echo "\n<ul class=\"oldtrick\">\n";
309         echo "  <li> Hello $myname!   History: </li>\n";
310         
311         while($r = mysql_fetch_array($result,MYSQL_NUM))
312           {
313             $seq   = $r[3];
314             $pos   = $r[2];
315             $trick = $r[5];
316             
317             if($trick!=$lasttrick && $seq==1)
318               {
319                 /* start of an old trick? */
320                 echo "  <li onclick=\"hl('$trickNR');\"><a href=\"#\">Trick $trickNR</a>\n".
321                   "    <div class=\"table\" id=\"trick".$trickNR."\">\n".
322                   "      <img class=\"table\" src=\"pics/table".($pos-1).".png\" alt=\"table\" />\n";
323               }
324             else if($trick==$lasttrick && $seq==1)
325               {
326                 /* start of a last trick? */
327                 echo "  <li onclick=\"hl('$trickNR');\"><a href=\"#\">Current Trick</a>\n".
328                   "    <div class=\"table\" id=\"trick".$trickNR."\">\n".
329                   "      <img class=\"table\" src=\"pics/table".($pos-1).".png\" alt=\"table\" />";
330               }
331             
332             /* display card */
333             echo "      <div class=\"card".($pos-1)."\">\n";
334             
335             $play[$pos]=$r[0];
336             
337             $comment=0;
338             if($comment)
339               echo "        <span class=\"comment\">";
340             else
341               echo "        <span>";
342             
343             /* print name */
344             echo $r[1];
345             
346             /* check for comment */
347             if($comment)
348               echo "<span>".$comment."</span>";
349             echo "</span>\n        ";
350             
351             display_card($r[0]);
352             
353             echo "      </div>\n"; /* end div card */
354             
355             /* end of trick? */
356             if($seq==4)
357               {
358                 $trickNR++;
359                 echo "    </div>\n  </li>\n";  /* end div table, end li table */
360               }
361           }
362         if($seq!=4) 
363           echo "    </div>\n  </li>\n";  /* end div table, end li table */
364         
365         echo "</ul>\n";
366         
367         /* whos turn is it? */
368         if($seq==4)
369           {
370              $winner = get_winner($play); /* returns the position */
371              $next = $winner;
372           }
373         else
374           {
375             $next = $pos+1;
376           }
377         if($next==5) $next=1;
378         
379         /* my turn?, display cards as links, ask for comments*/
380         if(DB_get_pos_by_hash($me) == $next)
381           $myturn = 1;
382         else
383           $myturn = 0;
384         
385         /* do we want to play a card? */
386         if(isset($_REQUEST["card"]) && $myturn)
387           {
388             $card   = $_REQUEST["card"];
389             $handid = DB_get_handid_by_hash($me); 
390             
391             /* check if we have card */
392             /* set played in hand_card to true where hand_id and card_id*/
393             $result = mysql_query("SELECT id FROM Hand_Card WHERE hand_id='$handid' AND card_id=".DB_quote_smart($card));
394             $r = mysql_fetch_array($result,MYSQL_NUM);
395             $handcardid = $r[0];
396             
397             if($handcardid)
398               {
399                 mysql_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".DB_quote_smart($card));
400                 
401                 /* get trick id or start new trick */
402                 $a = DB_get_current_trickid($gameid);
403                 $trickid  = $a[0];
404                 $sequence = $a[1];
405                 
406                 DB_play_card($trickid,$handcardid,$sequence);
407                 echo "<div class=\"card\">";
408                 echo " you played  <br />";
409                 display_card($card);
410                 echo "</div>\n";
411                 
412                 if(sizeof(DB_get_hand($me))==0)
413                   DB_set_hand_status_by_hash($me,'gameover');
414                 
415                 echo "TODO: email next player<br />";
416               }
417             else
418               {
419                 echo "couldn't find card <br />\n";
420               }
421           }
422         else if(isset($_REQUEST["card"]) && !$myturn )
423           {
424             echo "please wait until it is your turn! <br />\n";
425           }
426         
427         $mycards = DB_get_hand($me);
428         sort($mycards);
429         echo "<div class=\"mycards\">\n";
430         
431         if($myturn && !isset($_REQUEST["card"]))
432           {
433             echo "Hello ".DB_get_name_by_hash($me).", it's your turn!  <br />\n";
434             echo "Your cards are: <br />\n";
435             echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
436             foreach($mycards as $card) 
437               display_link_card($card);
438 ?>
439     <br />A short comment:<input name="comment" type="text" size="30" maxlength="50" /> 
440     <input type="hidden" name="me" value="<?php echo $me; ?>" />
441     <input type="submit" value="move" />
442  </form>
443  <?php
444          }
445         else
446           {
447             echo "Your cards are: <br />\n";
448             foreach($mycards as $card) 
449               display_card($card);
450           }
451         echo "</div>\n";
452         /*check if we still have cards left, else set status to gameover */
453         
454         break;
455       case 'gameover':
456         echo "the game is over... guess the final score should be displayed here...<br />\n";
457         echo "TODO: suggest a new game with the next person as dealer <br />\n";
458         break;
459       default:
460         echo "error in testing the status";
461       }
462     exit();
463   } 
464  else if(isset($_REQUEST["email"]) && isset($_REQUEST["password"]))
465   {
466     $ok=1;
467     $uid = DB_get_userid_by_email($_REQUEST["email"]);
468     if(!$uid)
469       $ok=0;
470     if(!DB_get_userid_by_passwd(md5($_REQUEST["password"])))
471       $ok=0;
472
473     if($ok)
474       {
475         echo "ok. your logged in, now what? :)<br />";
476         
477       }
478     else
479       {
480         echo "sorry email and password don't match <br />";
481       }
482     exit();
483   }
484 else if(isset($_REQUEST["register"]) )
485   {
486     echo "TODO: convert timezone into a menu<br />\n";
487     echo "TODO: figure out a way to handle passwrods <br />\n";
488 ?>
489         <form action="index.php" method="post">
490           <fieldset>
491             <legend>Register</legend>
492              <table>
493               <tr>
494                <td><label for="Rfullname">Full name:</label></td>
495                <td><input type="text" id="Rfullname" name="Rfullname" size="20" maxsize="30" /> </td>
496               </tr><tr>
497                <td><label for="Remail">Email:</label></td>
498                <td><input type="text" id="Remail" name="Remail" size="20" maxsize="30" /></td>
499               </tr><tr>
500                <td><label for="Rpassword">Password(will be displayed in cleartext on the next page):</label></td>
501                <td><input type="password" id="Rpassword" name="Rpassword" size="20" maxsize="30" /></td>
502               </tr><tr>
503                <td><label for="Rtimezone">Timezone:</label></td>
504                <td><input type="text" id="Rtimezone" name="Rtimezone" size="4" maxsize="4" value="+1"/></td>
505               </tr><tr>
506                <td colspan="2"> <input type="submit" value="register" /></td>
507              </table>
508           </fieldset>
509         </form>
510 <?php
511   }
512 else if(isset($_REQUEST["Rfullname"]) && 
513         isset($_REQUEST["Remail"]   ) && 
514         isset($_REQUEST["Rpassword"]) && 
515         isset($_REQUEST["Rtimezone"]) )
516   {
517         $ok=1;
518         if(DB_get_userid_by_name($_REQUEST["Rfullname"]))
519           {
520             echo "please chose another name<br />";
521             $ok=0;
522           }
523         if(DB_get_userid_by_email($_REQUEST["Remail"]))
524           {
525             echo "this email address is already used ?!<br />";
526             $ok=0;
527           }
528         if($ok)
529           {
530             $r=mysql_query("INSERT INTO User VALUES(NULL,".DB_quote_smart($_REQUEST["Rfullname"]).
531                       ",".DB_quote_smart($_REQUEST["Remail"]).
532                       ",".DB_quote_smart(md5($_REQUEST["Rpassword"])).
533                       ",".DB_quote_smart($_REQUEST["Rtimezone"]).",NULL)"); 
534             
535             if($r)
536               echo "  added you to the database";
537             else
538               echo " something went wrong";
539           }
540   }
541 else
542   { /* no new game, not in a game */
543 ?>
544     <p> If you want to play a game of Doppelkopf, you found the right place ;) </p>
545     <p> Please <a href="index.php?register">register</a>, in case you haven't done yet  <br />
546         or login with you email-address or name and password here:
547         <form action="index.php" method="post">
548           <fieldset>
549             <legend>Login</legend>
550              <table>
551               <tr>
552                <td><label for="email">Email:</label></td><td><input type="text" id="email" name="email" size="20" maxsize="30" /> </td>
553               </tr><tr>
554                <td><label for="password">Password:</label></td><td><input type="password" id="password" name="password" size="20" maxsize="30" /></td>
555               </tr><tr>
556                <td> <input type="submit" value="login" /></td>
557              </table>
558           </fieldset>
559         </form>
560  
561     </p>
562
563
564 <?php
565   }
566 ?>
567 </body>
568 </html>
569
570 <?php
571
572 DB_close();
573
574 /*
575  *Local Variables: 
576  *mode: php
577  *mode: hs-minor
578  *End:
579  */
580 ?>
581
582