2 error_reporting(E_ALL);
4 include_once("config.php");
5 include_once("output.php"); /* html output only */
6 include_once("db.php"); /* database only */
7 include_once("functions.php"); /* the rest */
11 /***************** M A I N **************************/
15 /* check if we want to start a new game */
16 if(isset($_REQUEST["new"]))
17 output_form_for_new_game();
19 /*check if everything is ready to set up a new game */
20 else if( isset($_REQUEST["PlayerA"]) &&
21 isset($_REQUEST["PlayerB"]) &&
22 isset($_REQUEST["PlayerC"]) &&
23 isset($_REQUEST["PlayerD"]) )
25 $PlayerA = $_REQUEST["PlayerA"];
26 $PlayerB = $_REQUEST["PlayerB"];
27 $PlayerC = $_REQUEST["PlayerC"];
28 $PlayerD = $_REQUEST["PlayerD"];
30 $EmailA = DB_get_email_by_name($PlayerA);
31 $EmailB = DB_get_email_by_name($PlayerB);
32 $EmailC = DB_get_email_by_name($PlayerC);
33 $EmailD = DB_get_email_by_name($PlayerD);
35 if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
37 echo "couldn't find one of the names, please start a new game";
41 $useridA = DB_get_userid_by_name($PlayerA);
42 $useridB = DB_get_userid_by_name($PlayerB);
43 $useridC = DB_get_userid_by_name($PlayerC);
44 $useridD = DB_get_userid_by_name($PlayerD);
46 /* create random numbers */
47 $randomNR = create_array_of_random_numbers();
48 $randomNRstring = join(":",$randomNR);
52 if(isset($_REQUEST["followup"]))
54 $followup= $_REQUEST["followup"];
55 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', NULL, NULL,'pre','$followup' ,NULL)");
58 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', NULL, NULL,'pre', NULL ,NULL)");
59 $game_id = mysql_insert_id();
62 $hashA = md5("AGameOfDoko".$game_id.$PlayerA.$EmailA);
63 $hashB = md5("AGameOfDoko".$game_id.$PlayerB.$EmailB);
64 $hashC = md5("AGameOfDoko".$game_id.$PlayerC.$EmailC);
65 $hashD = md5("AGameOfDoko".$game_id.$PlayerD.$EmailD);
68 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridA).
69 ", ".DB_quote_smart($hashA).", 'start','1',NULL,NULL,NULL,'false','false',NULL)");
70 $hand_idA = mysql_insert_id();
71 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridB).
72 ", ".DB_quote_smart($hashB).", 'start','2',NULL,NULL,NULL,'false','false',NULL)");
73 $hand_idB = mysql_insert_id();
74 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridC).
75 ", ".DB_quote_smart($hashC).", 'start','3',NULL,NULL,NULL,'false','false',NULL)");
76 $hand_idC = mysql_insert_id();
77 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridD).
78 ", ".DB_quote_smart($hashD).", 'start','4',NULL,NULL,NULL,'false','false',NULL)");
79 $hand_idD = mysql_insert_id();
83 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idA', '".$randomNR[$i]."', 'false')");
85 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idB', '".$randomNR[$i]."', 'false')");
87 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idC', '".$randomNR[$i]."', 'false')");
89 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idD', '".$randomNR[$i]."', 'false')");
91 /* send out email, TODO: check for error with email */
93 "you are invited to play a game of DoKo (that is to debug the program ;).\n".
94 "Place comments and bug reports here:\n".
95 "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
96 "The whole round would consist of the following players:\n".
101 "If you want to join this game, please follow this link:\n\n".
104 mymail($EmailA,"You are invited to a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
105 mymail($EmailB,"You are invited to a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
106 mymail($EmailC,"You are invited to a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
107 mymail($EmailD,"You are invited to a game of DoKo","Hello $PlayerD,\n".$message.$hashD);
110 /* end set up a new game */
112 else if(isset($_REQUEST["me"]))
114 /* handle request from one specifig player,
115 * the hash is set on a per game base, so first just handle this game
116 * perhaps also show links to other games in a sidebar
119 $me = $_REQUEST["me"];
121 /* test for valid ID */
122 $myid = DB_get_userid_by_hash($me);
125 echo "Can't find you in the database, please check the url.<br />\n";
126 echo "perhaps the game has been cancled.";
130 DB_update_user_timestamp($myid);
132 /* get some information from the DB */
133 $gameid = DB_get_gameid_by_hash($me);
134 $myname = DB_get_name_by_hash($me);
135 $mystatus = DB_get_status_by_hash($me);
140 check_want_to_play($me);
141 DB_set_hand_status_by_hash($me,'init');
144 if( !isset($_REQUEST["in"]) || !isset($_REQUEST["update"]))
146 DB_set_hand_status_by_hash($me,'start');
147 echo "you need to answer both question";
151 if($_REQUEST["in"] == "no")
153 echo "TODO: email everyone that the game has been canceled.<br />";
154 /*something like need to modify for DB backend
157 $message = "Hello ".$player[$hash[$i]]["name"].",\n\n".
158 "the game has been canceled due to the request of one of the players.\n";
159 mymail($player[$hash[$i]]["email"],"[DoKo-Debug] the game has been canceled",$message);
162 /* delete everything from the dB */
167 echo "thanks for joining the game... please scroll down";
168 echo "TODO: make this page nicer<br />";
169 echo "TODO: set card pref<br />";
171 $mycards = DB_get_hand($me);
173 echo "<p class=\"mycards\">your cards are: <br />\n";
174 foreach($mycards as $card)
178 check_for_sickness($me,$mycards);
180 DB_set_hand_status_by_hash($me,'check');
186 echo "no checking at the moment... you need to play a normal game. At the moment you need to reload this page to finish the setup.";
187 if(!isset($_REQUEST["solo"]) ||
188 !isset($_REQUEST["wedding"]) ||
189 !isset($_REQUEST["poverty"]) ||
190 !isset($_REQUEST["nines"]) )
192 DB_set_hand_status_by_hash($me,'init');
193 /* problem: by setting it back to init, variables "in" and "update" are
194 * not set, so the player will be send back to the start, after seeing his hand
196 echo "you need to fill out the form";
200 if( $_REQUEST["solo"]!="No")
202 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
203 DB_set_sickness_by_hash($me,"solo");
205 else if($_REQUEST["wedding"] == "yes")
207 echo "wedding was chosen<br />\n";
208 DB_set_sickness_by_hash($me,"wedding");
210 else if($_REQUEST["poverty"] == "yes")
212 echo "poverty was chosen<br />\n";
213 DB_set_sickness_by_hash($me,"poverty");
215 else if($_REQUEST["nines"] == "yes")
217 echo "nines was chosen<br />\n";
218 DB_set_sickness_by_hash($me,"nines");
221 DB_set_hand_status_by_hash($me,'poverty');
223 /* check all players and set game to final result, e.g. solo, wedding, povert, redeal */
225 /* reset solo, etc from players who did say something, but it didn't matter? */
228 /* here we need to check if there is a solo or some other form o sickness.
229 * If so, which one counts
230 * set that one in the Game table, delete other ones form Hand table
231 * tell people about it.
233 echo "<br />poverty not handeled at the moment... you need to play a normal game<br />";
235 /* only set this after all poverty, etc. are handeled*/
236 DB_set_hand_status_by_hash($me,'play');
238 /* check if the game can start */
239 $userids = DB_get_all_userid_by_gameid($gameid);
241 foreach($userids as $user)
242 if(DB_get_hand_status_by_userid($user)!='play')
246 DB_set_game_status_by_gameid($gameid,'play');
250 case 'gameover': /*both entries here, so that the tricks are visible for both in case of 'play' there is a break later that skips the last part*/
251 /* display local time */
252 echo "<div class=\"time\">\n Local times:<table>";
254 $users = DB_get_all_userid_by_gameid($gameid);
255 foreach($users as $user)
257 $offset = DB_get_user_timezone($user);
258 $zone = return_timezone($offset);
259 date_default_timezone_set($zone);
260 $name = DB_get_name_by_userid($user);
262 echo "<tr> <td>$name</td> <td>".date("Y-m-d H:i:s")."</td></tr>\n";
264 echo "</table>\n</div>\n";
267 /* display links to other games */
268 echo "<div class=\"over\">\n";
269 $result = mysql_query("SELECT email,password from User WHERE id='$myid'" );
270 $r = mysql_fetch_array($result,MYSQL_NUM);
271 echo "<form action=\"index.php\" method=\"post\">\n";
272 echo " <input type=\"hidden\" name=\"email\" value=\"".$r[0]."\" />\n";
273 echo " <input type=\"hidden\" name=\"password\" value=\"".$r[1]."\" />\n";
274 echo " <input type=\"submit\" value=\"go to my user page\" />\n";
280 $gamestatus = DB_get_game_status_by_gameid($gameid);
281 if($gamestatus == 'pre')
283 echo "you need to wait for the others... <br />";
286 /* get everythin relevant to display the tricks */
287 $result = mysql_query("SELECT Hand_Card.card_id as card,".
288 " User.fullname as name,".
289 " Hand.position as position,".
290 " Play.sequence as sequence, ".
291 " Hand.hash as hash, ".
295 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
296 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
297 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
298 "LEFT JOIN User ON User.id=Hand.user_id ".
299 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
300 "WHERE Trick.game_id='".$gameid."' ".
301 "ORDER BY Trick.id,sequence ASC");
306 $lasttrick = DB_get_max_trickid($gameid);
308 $play = array(); /* needed to calculate winner later */
312 echo "\n<ul class=\"oldtrick\">\n";
313 echo " <li> Hello $myname! History: </li>\n";
315 while($r = mysql_fetch_array($result,MYSQL_NUM))
322 if($trick!=$lasttrick && $seq==1)
324 /* start of an old trick? */
325 echo " <li onclick=\"hl('$trickNR');\"><a href=\"#\">Trick $trickNR</a>\n".
326 " <div class=\"table\" id=\"trick".$trickNR."\">\n".
327 " <img class=\"table\" src=\"pics/table".($pos-1).".png\" alt=\"table\" />\n";
329 else if($trick==$lasttrick && $seq==1)
331 /* start of a last trick? */
332 echo " <li onclick=\"hl('$trickNR');\"><a href=\"#\">Current Trick</a>\n".
333 " <div class=\"table\" id=\"trick".$trickNR."\">\n".
334 " <img class=\"table\" src=\"pics/table".($pos-1).".png\" alt=\"table\" />\n";
338 echo " <div class=\"card".($pos-1)."\">\n";
343 echo " <span class=\"comment\">";
350 /* check for comment */
352 echo "<span>".$comment."</span>";
357 echo " </div>\n"; /* end div card */
363 echo " </div>\n </li>\n"; /* end div table, end li table */
367 if($seq!=4 && $trickNR>1)
368 echo " </div>\n </li>\n"; /* end div table, end li table */
372 /* whos turn is it? */
375 $winner = get_winner($play,"normal"); /* returns the position */
382 if($next==5) $next=1;
384 /* my turn?, display cards as links, ask for comments*/
385 if(DB_get_pos_by_hash($me) == $next)
390 /* do we want to play a card? */
391 if(isset($_REQUEST["card"]) && $myturn)
393 $card = $_REQUEST["card"];
394 $handid = DB_get_handid_by_hash($me);
396 /* check if we have card and that we haven't played it yet*/
397 /* set played in hand_card to true where hand_id and card_id*/
398 $result = mysql_query("SELECT id FROM Hand_Card WHERE played='false' and ".
399 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
400 $r = mysql_fetch_array($result,MYSQL_NUM);
405 mysql_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".DB_quote_smart($card));
407 /* get trick id or start new trick */
408 $a = DB_get_current_trickid($gameid);
412 $playid = DB_play_card($trickid,$handcardid,$sequence);
414 /*check for coment */
415 if(isset($_REQUEST["comment"]))
417 DB_insert_comment($_REQUEST["comment"],$playid,$myid);
420 echo "<div class=\"card\">";
421 echo " you played <br />";
426 /*check if we still have cards left, else set status to gameover */
427 if(sizeof(DB_get_hand($me))==0)
429 DB_set_hand_status_by_hash($me,'gameover');
430 $mystatus='gameover';
433 /* if all players are done, set game status also to game over */
434 $userids = DB_get_all_userid_by_gameid($gameid);
436 foreach($userids as $user)
437 if(DB_get_hand_status_by_userid($user)!='gameover')
441 DB_set_game_status_by_gameid($gameid,"gameover");
443 /* email next player */
444 if(DB_get_game_status_by_gameid($gameid)=='play')
448 $play = DB_get_cards_by_trick($trickid);
449 $winner = get_winner($play,"normal"); /* returns the position */
454 $next = DB_get_pos_by_hash($me)+1;
456 if($next==5) $next=1;
458 /* email next player */
459 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
460 $email = DB_get_email_by_hash($next_hash);
462 $message = "It's your turn now.\n".
463 "Use this link to play a card: ".$host."?me=".$next_hash."\n\n" ;
464 mymail($email,"[DoKo-debug] a card has been played",$message);
467 echo "DEBUG:<a href=\"index.php?me=".DB_get_hash_from_game_and_pos($gameid,$next).
468 "\"> next player </a> <br />\n";
474 echo "couldn't find card <br />\n";
477 else if(isset($_REQUEST["card"]) && !$myturn )
479 echo "please wait until it is your turn! <br />\n";
482 $mycards = DB_get_hand($me);
484 echo "<div class=\"mycards\">\n";
486 if($myturn && !isset($_REQUEST["card"]))
488 echo "Hello ".$myname.", it's your turn! <br />\n";
489 echo "Your cards are: <br />\n";
490 echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
491 foreach($mycards as $card)
492 display_link_card($card);
494 <br />A short comments:<input name="comment" type="text" size="30" maxlength="50" />
495 <input type="hidden" name="me" value="<?php echo $me; ?>" />
496 <input type="submit" value="move" />
500 else if($mystatus=='play')
502 echo "Your cards are: <br />\n";
503 foreach($mycards as $card)
508 /* check if we need to set status to 'gameover' is done during playing of the card */
509 if($mystatus=='play')
511 /* the following happens only when the gamestatus is 'gameover' */
512 /* check if game is over, display results */
513 if(DB_get_game_status_by_gameid($gameid)=='play')
515 echo "the game is over for you.. other people still need to play though";
519 echo "the game is over now... guess the final score should be displayed here...<br />\n";
521 /* suggest a new game with the same people in it, just rotated once */
522 $names = DB_get_all_names_by_gameid($gameid);
524 echo "Do you want to continue playing?(This will start a new game, with the next person as dealer.)\n";
525 echo "<form action=\"index.php\" method=\"post\">\n";
526 echo " <input type=\"hidden\" name=\"PlayerA\" value=\"".($names[1])."\" />\n";
527 echo " <input type=\"hidden\" name=\"PlayerB\" value=\"".($names[2])."\" />\n";
528 echo " <input type=\"hidden\" name=\"PlayerC\" value=\"".($names[3])."\" />\n";
529 echo " <input type=\"hidden\" name=\"PlayerD\" value=\"".($names[0])."\" />\n";
530 echo " <input type=\"hidden\" name=\"followup\" value=\"".($gameid)."\" />\n";
531 echo " <input type=\"submit\" value=\"keep playing\" />\n";
536 echo "error in testing the status";
540 /* user status page */
541 else if(isset($_REQUEST["email"]) && isset($_REQUEST["password"]))
543 /* test id and password, should really be done in one step */
544 $email = $_REQUEST["email"];
545 $password = $_REQUEST["password"];
547 if(strlen($password)!=32)
548 $password = md5($password);
551 $uid = DB_get_userid_by_email_and_password($email,$password);
557 $time = DB_get_user_timestamp($uid);
558 $unixtime =strtotime($time);
560 $offset = DB_get_user_timezone($uid);
561 $zone = return_timezone($offset);
562 date_default_timezone_set($zone);
564 echo "last login: ".date("r",$unixtime)."<br />";
566 DB_update_user_timestamp($uid);
568 echo "<p>these are the games you are playing in:<br />\n";
569 $result = mysql_query("SELECT hash,game_id from Hand WHERE user_id='$uid' AND status<>'gameover'" );
570 while( $r = mysql_fetch_array($result,MYSQL_NUM))
571 echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a><br />";
574 $names = DB_get_all_names();
575 echo "<p>registered players:<br />\n";
576 foreach ($names as $name)
577 echo "$name <br />\n";
579 echo "<p>Want to start a new game? remember 4 names from the list above and visit ".
580 "<a href=\"".$host."?new\">this page.</a></p>";
584 echo "sorry email and password don't match <br />";
588 /* page for registration */
589 else if(isset($_REQUEST["register"]) )
591 echo "IMPORTANT: passwords are going over the net as clear text, so pick an easy password. No need to pick anything complicated here ;)<br /><br />";
592 echo "TODO: convert timezone into a menu<br />\n";
593 echo "TODO: figure out a way to handle passwrods <br />\n";
595 <form action="index.php" method="post">
597 <legend>Register</legend>
600 <td><label for="Rfullname">Full name:</label></td>
601 <td><input type="text" id="Rfullname" name="Rfullname" size="20" maxsize="30" /> </td>
603 <td><label for="Remail">Email:</label></td>
604 <td><input type="text" id="Remail" name="Remail" size="20" maxsize="30" /></td>
606 <td><label for="Rpassword">Password(will be displayed in cleartext on the next page):</label></td>
607 <td><input type="password" id="Rpassword" name="Rpassword" size="20" maxsize="30" /></td>
609 <td><label for="Rtimezone">Timezone:</label></td>
611 <input type="text" id="Rtimezone" name="Rtimezone" size="4" maxsize="4" value="+1" />
614 <td colspan="2"> <input type="submit" value="register" /></td>
620 /* new user wants to register */
621 else if(isset($_REQUEST["Rfullname"]) &&
622 isset($_REQUEST["Remail"] ) &&
623 isset($_REQUEST["Rpassword"]) &&
624 isset($_REQUEST["Rtimezone"]) )
627 if(DB_get_userid_by_name($_REQUEST["Rfullname"]))
629 echo "please chose another name<br />";
632 if(DB_get_userid_by_email($_REQUEST["Remail"]))
634 echo "this email address is already used ?!<br />";
639 $r=mysql_query("INSERT INTO User VALUES(NULL,".DB_quote_smart($_REQUEST["Rfullname"]).
640 ",".DB_quote_smart($_REQUEST["Remail"]).
641 ",".DB_quote_smart(md5($_REQUEST["Rpassword"])).
642 ",".DB_quote_smart($_REQUEST["Rtimezone"]).",NULL)");
645 echo " added you to the database";
647 echo " something went wrong";
650 /* default login page */
652 { /* no new game, not in a game */