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 */
13 /* check if we want to start a new game */
15 output_form_for_new_game();
17 /*check if everything is ready to set up a new game */
18 else if( myisset("PlayerA", "PlayerB","PlayerC","PlayerD" ))
20 $PlayerA = $_REQUEST["PlayerA"];
21 $PlayerB = $_REQUEST["PlayerB"];
22 $PlayerC = $_REQUEST["PlayerC"];
23 $PlayerD = $_REQUEST["PlayerD"];
25 $EmailA = DB_get_email_by_name($PlayerA);
26 $EmailB = DB_get_email_by_name($PlayerB);
27 $EmailC = DB_get_email_by_name($PlayerC);
28 $EmailD = DB_get_email_by_name($PlayerD);
30 if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
32 echo "couldn't find one of the names, please start a new game";
36 $useridA = DB_get_userid_by_name($PlayerA);
37 $useridB = DB_get_userid_by_name($PlayerB);
38 $useridC = DB_get_userid_by_name($PlayerC);
39 $useridD = DB_get_userid_by_name($PlayerD);
41 /* create random numbers */
42 $randomNR = create_array_of_random_numbers();
43 $randomNRstring = join(":",$randomNR);
47 if(myisset("followup") )
49 $followup= $_REQUEST["followup"];
50 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', NULL, NULL,'pre','$followup' ,NULL)");
53 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', NULL, NULL,'pre', NULL ,NULL)");
54 $game_id = mysql_insert_id();
57 $hashA = md5("AGameOfDoko".$game_id.$PlayerA.$EmailA);
58 $hashB = md5("AGameOfDoko".$game_id.$PlayerB.$EmailB);
59 $hashC = md5("AGameOfDoko".$game_id.$PlayerC.$EmailC);
60 $hashD = md5("AGameOfDoko".$game_id.$PlayerD.$EmailD);
63 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridA).
64 ", ".DB_quote_smart($hashA).", 'start','1',NULL,NULL,NULL,'false','false',NULL)");
65 $hand_idA = mysql_insert_id();
66 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridB).
67 ", ".DB_quote_smart($hashB).", 'start','2',NULL,NULL,NULL,'false','false',NULL)");
68 $hand_idB = mysql_insert_id();
69 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridC).
70 ", ".DB_quote_smart($hashC).", 'start','3',NULL,NULL,NULL,'false','false',NULL)");
71 $hand_idC = mysql_insert_id();
72 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridD).
73 ", ".DB_quote_smart($hashD).", 'start','4',NULL,NULL,NULL,'false','false',NULL)");
74 $hand_idD = mysql_insert_id();
78 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idA', '".$randomNR[$i]."', 'false')");
80 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idB', '".$randomNR[$i]."', 'false')");
82 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idC', '".$randomNR[$i]."', 'false')");
84 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idD', '".$randomNR[$i]."', 'false')");
86 /* send out email, TODO: check for error with email */
88 "you are invited to play a game of DoKo (that is to debug the program ;).\n".
89 "Place comments and bug reports here:\n".
90 "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
91 "The whole round would consist of the following players:\n".
96 "If you want to join this game, please follow this link:\n\n".
99 mymail($EmailA,"You are invited to a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
100 mymail($EmailB,"You are invited to a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
101 mymail($EmailC,"You are invited to a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
102 mymail($EmailD,"You are invited to a game of DoKo","Hello $PlayerD,\n".$message.$hashD);
105 /* end set up a new game */
107 else if(myisset("me"))
109 /* handle request from one specific player,
110 * the hash is set on a per game base
113 $me = $_REQUEST["me"];
115 /* test for valid ID */
116 $myid = DB_get_userid_by_hash($me);
119 echo "Can't find you in the database, please check the url.<br />\n";
120 echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
124 DB_update_user_timestamp($myid);
126 /* get some information from the DB */
127 $gameid = DB_get_gameid_by_hash($me);
128 $myname = DB_get_name_by_hash($me);
129 $mystatus = DB_get_status_by_hash($me);
134 check_want_to_play($me);
135 DB_set_hand_status_by_hash($me,'init');
138 if( !myisset("in","update") )
140 DB_set_hand_status_by_hash($me,'start');
141 echo "you need to answer both question";
145 if($_REQUEST["in"] == "no")
147 echo "TODO: email everyone that the game has been canceled.<br />";
148 /*something like need to modify for DB backend
151 $message = "Hello ".$player[$hash[$i]]["name"].",\n\n".
152 "the game has been canceled due to the request of one of the players.\n";
153 mymail($player[$hash[$i]]["email"],"[DoKo-Debug] the game has been canceled",$message);
156 /* delete everything from the dB */
161 echo "thanks for joining the game... please scroll down";
162 echo "TODO: make this page nicer<br />";
163 echo "TODO: set card pref<br />";
165 $mycards = DB_get_hand($me);
167 echo "<p class=\"mycards\">your cards are: <br />\n";
168 foreach($mycards as $card)
172 check_for_sickness($me,$mycards);
174 DB_set_hand_status_by_hash($me,'check');
180 echo "no checking at the moment... you need to play a normal game.".
181 " At the moment you need to reload this page to finish the setup.";
182 if(!myisset("solo","wedding","poverty","nines") )
184 /* all these variables have a pre-selected default,
185 * so we should never get here,
186 * unless a user tries to cheat ;) */
187 echo "something went wrong...please contact the admin.";
191 if( $_REQUEST["solo"]!="No")
193 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
194 DB_set_sickness_by_hash($me,"solo");
196 else if($_REQUEST["wedding"] == "yes")
198 echo "wedding was chosen<br />\n";
199 DB_set_sickness_by_hash($me,"wedding");
201 else if($_REQUEST["poverty"] == "yes")
203 echo "poverty was chosen<br />\n";
204 DB_set_sickness_by_hash($me,"poverty");
206 else if($_REQUEST["nines"] == "yes")
208 echo "nines was chosen<br />\n";
209 DB_set_sickness_by_hash($me,"nines");
212 DB_set_hand_status_by_hash($me,'poverty');
214 /* check all players and set game to final result, e.g. solo, wedding, povert, redeal */
216 /* reset solo, etc from players who did say something, but it didn't matter? */
219 /* here we need to check if there is a solo or some other form o sickness.
220 * If so, which one counts
221 * set that one in the Game table, delete other ones form Hand table
222 * tell people about it.
224 echo "<br />poverty not handeled at the moment... you need to play a normal game<br />";
226 /* only set this after all poverty, etc. are handeled*/
227 DB_set_hand_status_by_hash($me,'play');
229 /* check if the game can start */
230 $userids = DB_get_all_userid_by_gameid($gameid);
232 foreach($userids as $user)
233 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='play')
238 /* TODO: check what kind of game we are playing */
239 DB_set_game_status_by_gameid($gameid,'play');
245 /* both entries here, so that the tricks are visible for both.
246 * in case of 'play' there is a break later that skips the last part
249 /* display useful things in divs */
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";
268 /* display links to the users status page */
269 $result = mysql_query("SELECT email,password from User WHERE id='$myid'" );
270 $r = mysql_fetch_array($result,MYSQL_NUM);
271 output_link_to_user_page($r[0],$r[1]);
275 /* end display useful things*/
277 /* has the game started? No, then just wait here...*/
278 $gamestatus = DB_get_game_status_by_gameid($gameid);
279 if($gamestatus == 'pre')
281 echo "you need to wait for the others... <br />";
282 break; /* not sure this works... the idea is that you can
283 * only play a card after everyone is ready to play */
286 /* display the table and the names */
287 $result = mysql_query("SELECT User.fullname as name,".
288 " Hand.position as position ".
290 "LEFT JOIN User ON User.id=Hand.user_id ".
291 "WHERE Hand.game_id='".$gameid."' ".
292 "ORDER BY position ASC");
294 echo "<div class=\"table\">\n".
295 " <img src=\"pics/table.png\" alt=\"table\" />\n";
296 while($r = mysql_fetch_array($result,MYSQL_NUM))
301 echo " <span class=\"table".($pos-1)."\">$name</span>\n";
305 /* get everything relevant to display the tricks */
306 $result = mysql_query("SELECT Hand_Card.card_id as card,".
307 " Hand.position as position,".
308 " Play.sequence as sequence, ".
312 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
313 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
314 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
315 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
316 "WHERE Trick.game_id='".$gameid."' ".
317 "ORDER BY Trick.id,sequence ASC");
322 $lasttrick = DB_get_max_trickid($gameid);
324 $play = array(); /* needed to calculate winner later */
328 echo "\n<ul class=\"tricks\">\n";
329 echo " <li> Hello $myname! History: </li>\n";
331 while($r = mysql_fetch_array($result,MYSQL_NUM))
338 /* save card to be able to find the winner of the trick later */
341 if($trick!=$lasttrick && $seq==1)
343 /* start of an old trick? */
344 echo " <li onclick=\"hl('$trickNR');\"><a href=\"#\">Trick $trickNR</a>\n".
345 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
346 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
348 else if($trick==$lasttrick && $seq==1)
350 /* start of a last trick? */
351 echo " <li onclick=\"hl('$trickNR');\"><a href=\"#\">Current Trick</a>\n".
352 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
353 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
357 echo " <div class=\"card".($pos-1)."\">\n";
359 /* display comments */
361 echo " <span class=\"comment\">".$comment."</span>\n";
366 echo " </div>\n"; /* end div card */
372 echo " </div>\n </li>\n"; /* end div table, end li table */
376 if($seq!=4 && $trickNR>1)
377 echo " </div>\n </li>\n"; /* end div table, end li table */
381 /* whos turn is it? */
384 $winner = get_winner($play,"normal"); /* returns the position */
390 if($next==5) $next=1;
393 /* my turn?, display cards as links, ask for comments*/
394 if(DB_get_pos_by_hash($me) == $next)
399 /* do we want to play a card? */
400 if(myisset("card") && $myturn)
402 $card = $_REQUEST["card"];
403 $handid = DB_get_handid_by_hash($me);
405 /* check if we have card and that we haven't played it yet*/
406 /* set played in hand_card to true where hand_id and card_id*/
407 $result = mysql_query("SELECT id FROM Hand_Card WHERE played='false' and ".
408 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
409 $r = mysql_fetch_array($result,MYSQL_NUM);
414 mysql_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".DB_quote_smart($card));
416 /* get trick id or start new trick */
417 $a = DB_get_current_trickid($gameid);
421 $playid = DB_play_card($trickid,$handcardid,$sequence);
423 /*check for coment */
424 if(myisset("comment"))
426 DB_insert_comment($_REQUEST["comment"],$playid,$myid);
429 echo "<div class=\"card\">";
430 echo " you played <br />";
435 /*check if we still have cards left, else set status to gameover */
436 if(sizeof(DB_get_hand($me))==0)
438 DB_set_hand_status_by_hash($me,'gameover');
439 $mystatus='gameover';
442 /* if all players are done, set game status to game over */
443 $userids = DB_get_all_userid_by_gameid($gameid);
445 foreach($userids as $user)
446 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
450 DB_set_game_status_by_gameid($gameid,"gameover");
452 /* email next player */
453 if(DB_get_game_status_by_gameid($gameid)=='play')
457 $play = DB_get_cards_by_trick($trickid);
458 $winner = get_winner($play,"normal"); /* returns the position */
463 $next = DB_get_pos_by_hash($me)+1;
465 if($next==5) $next=1;
467 /* email next player */
468 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
469 $email = DB_get_email_by_hash($next_hash);
471 $message = "It's your turn now.\n".
472 "Use this link to play a card: ".$host."?me=".$next_hash."\n\n" ;
473 mymail($email,"[DoKo-debug] a card has been played",$message);
476 echo "DEBUG:<a href=\"index.php?me=".DB_get_hash_from_game_and_pos($gameid,$next).
477 "\"> next player </a> <br />\n";
483 echo "can't find that card?! <br />\n";
486 else if(myisset("card") && !$myturn )
488 echo "please wait until it's your turn! <br />\n";
491 $mycards = DB_get_hand($me);
493 echo "<div class=\"mycards\">\n";
495 if($myturn && !myisset("card"))
497 echo "Hello ".$myname.", it's your turn! <br />\n";
498 echo "Your cards are: <br />\n";
499 echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
500 foreach($mycards as $card)
501 display_link_card($card);
503 echo "<br />\nA short comments:<input name=\"comment\" type=\"text\" size=\"30\" maxlength=\"50\" />\n";
504 echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />\n";
505 echo "<input type=\"submit\" value=\"move\" />\n";
508 else if($mystatus=='play')
510 echo "Your cards are: <br />\n";
511 foreach($mycards as $card)
516 /* check if we need to set status to 'gameover' is done during playing of the card */
517 if($mystatus=='play')
519 /* the following happens only when the gamestatus is 'gameover' */
520 /* check if game is over, display results */
521 if(DB_get_game_status_by_gameid($gameid)=='play')
523 echo "the game is over for you.. other people still need to play though";
527 echo "the game is over now... guess the final score should be displayed here...<br />\n";
529 /* suggest a new game with the same people in it, just rotated once */
530 $names = DB_get_all_names_by_gameid($gameid);
531 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
535 echo "error in testing the status";
539 /* user status page */
540 else if(myisset("email","password"))
542 /* test id and password, should really be done in one step */
543 $email = $_REQUEST["email"];
544 $password = $_REQUEST["password"];
546 if(strlen($password)!=32)
547 $password = md5($password);
550 $uid = DB_get_userid_by_email_and_password($email,$password);
556 $time = DB_get_user_timestamp($uid);
557 $unixtime =strtotime($time);
559 $offset = DB_get_user_timezone($uid);
560 $zone = return_timezone($offset);
561 date_default_timezone_set($zone);
563 echo "last login: ".date("r",$unixtime)."<br />";
565 DB_update_user_timestamp($uid);
567 echo "<p>these are the games you are playing in:<br />\n";
568 $result = mysql_query("SELECT hash,game_id from Hand WHERE user_id='$uid' AND status<>'gameover'" );
569 while( $r = mysql_fetch_array($result,MYSQL_NUM))
570 echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a><br />";
573 $names = DB_get_all_names();
574 echo "<p>registered players:<br />\n";
575 foreach ($names as $name)
576 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(myisset("register") )
593 /* new user wants to register */
594 else if(myisset("Rfullname","Remail","Rpassword","Rtimezone") )
597 if(DB_get_userid_by_name($_REQUEST["Rfullname"]))
599 echo "please chose another name<br />";
602 if(DB_get_userid_by_email($_REQUEST["Remail"]))
604 echo "this email address is already used ?!<br />";
609 $r=mysql_query("INSERT INTO User VALUES(NULL,".DB_quote_smart($_REQUEST["Rfullname"]).
610 ",".DB_quote_smart($_REQUEST["Remail"]).
611 ",".DB_quote_smart(md5($_REQUEST["Rpassword"])).
612 ",".DB_quote_smart($_REQUEST["Rtimezone"]).",NULL)");
615 echo " added you to the database";
617 echo " something went wrong";
620 /* default login page */