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