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 */
12 echo "Working on the database...please check back in a few mintues";
20 /* check if we want to start a new game */
22 output_form_for_new_game();
24 /*check if everything is ready to set up a new game */
25 else if( myisset("PlayerA", "PlayerB","PlayerC","PlayerD" ))
27 $PlayerA = $_REQUEST["PlayerA"];
28 $PlayerB = $_REQUEST["PlayerB"];
29 $PlayerC = $_REQUEST["PlayerC"];
30 $PlayerD = $_REQUEST["PlayerD"];
32 $EmailA = DB_get_email_by_name($PlayerA);
33 $EmailB = DB_get_email_by_name($PlayerB);
34 $EmailC = DB_get_email_by_name($PlayerC);
35 $EmailD = DB_get_email_by_name($PlayerD);
37 if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
39 echo "couldn't find one of the names, please start a new game";
43 $useridA = DB_get_userid_by_name($PlayerA);
44 $useridB = DB_get_userid_by_name($PlayerB);
45 $useridC = DB_get_userid_by_name($PlayerC);
46 $useridD = DB_get_userid_by_name($PlayerD);
48 /* create random numbers */
49 $randomNR = create_array_of_random_numbers();
50 $randomNRstring = join(":",$randomNR);
54 if(myisset("followup") )
56 $followup= $_REQUEST["followup"];
57 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,'1','pre','$followup' ,NULL)");
60 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,'1','pre', NULL ,NULL)");
61 $game_id = mysql_insert_id();
64 $hashA = md5("AGameOfDoko".$game_id.$PlayerA.$EmailA);
65 $hashB = md5("AGameOfDoko".$game_id.$PlayerB.$EmailB);
66 $hashC = md5("AGameOfDoko".$game_id.$PlayerC.$EmailC);
67 $hashD = md5("AGameOfDoko".$game_id.$PlayerD.$EmailD);
70 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridA).
71 ", ".DB_quote_smart($hashA).", 'start','1',NULL,NULL,NULL,'false','false',NULL)");
72 $hand_idA = mysql_insert_id();
73 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridB).
74 ", ".DB_quote_smart($hashB).", 'start','2',NULL,NULL,NULL,'false','false',NULL)");
75 $hand_idB = mysql_insert_id();
76 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridC).
77 ", ".DB_quote_smart($hashC).", 'start','3',NULL,NULL,NULL,'false','false',NULL)");
78 $hand_idC = mysql_insert_id();
79 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridD).
80 ", ".DB_quote_smart($hashD).", 'start','4',NULL,NULL,NULL,'false','false',NULL)");
81 $hand_idD = mysql_insert_id();
85 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idA', '".$randomNR[$i]."', 'false')");
87 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idB', '".$randomNR[$i]."', 'false')");
89 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idC', '".$randomNR[$i]."', 'false')");
91 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idD', '".$randomNR[$i]."', 'false')");
93 /* send out email, TODO: check for error with email */
95 "you are invited to play a game of DoKo (that is to debug the program ;).\n".
96 "Place comments and bug reports here:\n".
97 "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
98 "The whole round would consist of the following players:\n".
103 "If you want to join this game, please follow this link:\n\n".
106 mymail($EmailA,"You are invited to a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
107 mymail($EmailB,"You are invited to a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
108 mymail($EmailC,"You are invited to a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
109 mymail($EmailD,"You are invited to a game of DoKo","Hello $PlayerD,\n".$message.$hashD);
112 /* end set up a new game */
114 else if(myisset("me"))
116 /* handle request from one specific player,
117 * the hash is set on a per game base
120 $me = $_REQUEST["me"];
122 /* test for valid ID */
123 $myid = DB_get_userid_by_hash($me);
126 echo "Can't find you in the database, please check the url.<br />\n";
127 echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
131 DB_update_user_timestamp($myid);
133 /* get some information from the DB */
134 $gameid = DB_get_gameid_by_hash($me);
135 $myname = DB_get_name_by_hash($me);
136 $mystatus = DB_get_status_by_hash($me);
137 $mypos = DB_get_pos_by_hash($me);
142 check_want_to_play($me);
143 DB_set_hand_status_by_hash($me,'init');
146 if( !myisset("in","update") )
148 DB_set_hand_status_by_hash($me,'start');
149 echo "you need to answer both question";
153 if($_REQUEST["in"] == "no")
155 echo "TODO: email everyone that the game has been canceled.<br />";
156 /*something like need to modify for DB backend
159 $message = "Hello ".$player[$hash[$i]]["name"].",\n\n".
160 "the game has been canceled due to the request of one of the players.\n";
161 mymail($player[$hash[$i]]["email"],"[DoKo-Debug] the game has been canceled",$message);
164 /* delete everything from the dB */
169 echo "thanks for joining the game... please scroll down";
170 echo "TODO: make this page nicer<br />";
171 echo "TODO: set card pref<br />";
173 $mycards = DB_get_hand($me);
175 echo "<p class=\"mycards\">your cards are: <br />\n";
176 foreach($mycards as $card)
180 check_for_sickness($me,$mycards);
182 DB_set_hand_status_by_hash($me,'check');
188 echo "checking for solo...<br /> if you have a wedding or poverty you need to play a normal game,sorry...<br />".
189 " At the moment you need to reload this page to finish the setup.<br />";
190 if(!myisset("solo","wedding","poverty","nines") )
192 /* all these variables have a pre-selected default,
193 * so we should never get here,
194 * unless a user tries to cheat ;) */
195 echo "something went wrong...please contact the admin.";
199 /* check if this sickness needs to be handled first */
200 $gametype = DB_get_gametype_by_gameid($gameid);
201 $startplayer = DB_get_startplayer_by_gameid($gameid);
203 if( $_REQUEST["solo"]!="No")
205 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
206 DB_set_sickness_by_hash($me,"solo");
207 echo "Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.\n";
209 if($gametype == "solo" && $startplayer<$mypos)
213 DB_set_gametype_by_gameid($gameid,"solo");
214 DB_set_startplayer_by_gameid($gameid,$mypos);
215 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
218 else if($_REQUEST["wedding"] == "yes")
220 /* TODO: add silent solo somewhere*/
221 echo "wedding was chosen<br />\n";
222 DB_set_sickness_by_hash($me,"wedding");
224 else if($_REQUEST["poverty"] == "yes")
226 echo "poverty was chosen<br />\n";
227 DB_set_sickness_by_hash($me,"poverty");
229 else if($_REQUEST["nines"] == "yes")
231 echo "nines was chosen<br />\n";
232 DB_set_sickness_by_hash($me,"nines");
236 DB_set_hand_status_by_hash($me,'poverty');
238 /* check all players and set game to final result, e.g. solo, wedding, povert, redeal */
242 /* here we need to check if there is a solo or some other form of sickness.
243 * If so, which one counts
244 * set that one in the Game table, delete other ones form Hand table
245 * tell people about it.
247 echo "<br />poverty not handeled at the moment... you need to play a normal game<br />";
249 /* only set this after all poverty, etc. are handeled*/
250 DB_set_hand_status_by_hash($me,'play');
252 /* check if the game can start */
253 $userids = DB_get_all_userid_by_gameid($gameid);
255 foreach($userids as $user)
256 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='play')
261 /* TODO: check what kind of game we are playing */
262 DB_set_game_status_by_gameid($gameid,'play');
268 /* both entries here, so that the tricks are visible for both.
269 * in case of 'play' there is a break later that skips the last part
272 /* figure out what kind of game we are playing,
273 * set the global variables $TRUMP,$DIAMONDS,$HEARTS,$CLUBS,$SPADES
277 $gametype = DB_get_gametype_by_gameid($gameid);
279 if($gametype=="solo")
281 $gametype = DB_get_solo_by_gameid($gameid);
282 $GT = $gametype." ".$GT;
287 set_gametype($gametype);
290 /* display useful things in divs */
292 /* display local time */
293 echo "<div class=\"time\">\n Local times:<table>";
295 $users = DB_get_all_userid_by_gameid($gameid);
296 foreach($users as $user)
298 $offset = DB_get_user_timezone($user);
299 $zone = return_timezone($offset);
300 date_default_timezone_set($zone);
301 $name = DB_get_name_by_userid($user);
303 echo "<tr> <td>$name</td> <td>".date("Y-m-d H:i:s")."</td></tr>\n";
305 echo "</table>\n</div>\n";
309 /* display links to the users status page */
310 $result = mysql_query("SELECT email,password from User WHERE id='$myid'" );
311 $r = mysql_fetch_array($result,MYSQL_NUM);
312 output_link_to_user_page($r[0],$r[1]);
316 /* end display useful things*/
318 /* has the game started? No, then just wait here...*/
319 $gamestatus = DB_get_game_status_by_gameid($gameid);
320 if($gamestatus == 'pre')
322 echo "you need to wait for the others... <br />";
323 break; /* not sure this works... the idea is that you can
324 * only play a card after everyone is ready to play */
327 /* display the table and the names */
328 $result = mysql_query("SELECT User.fullname as name,".
329 " Hand.position as position ".
331 "LEFT JOIN User ON User.id=Hand.user_id ".
332 "WHERE Hand.game_id='".$gameid."' ".
333 "ORDER BY position ASC");
335 echo "<div class=\"table\">\n".
336 " <img src=\"pics/table.png\" alt=\"table\" />\n";
337 while($r = mysql_fetch_array($result,MYSQL_NUM))
342 echo " <span class=\"table".($pos-1)."\">$name</span>\n";
346 /* get everything relevant to display the tricks */
347 $result = mysql_query("SELECT Hand_Card.card_id as card,".
348 " Hand.position as position,".
349 " Play.sequence as sequence, ".
353 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
354 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
355 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
356 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
357 "WHERE Trick.game_id='".$gameid."' ".
358 "ORDER BY Trick.id,sequence ASC");
363 $lasttrick = DB_get_max_trickid($gameid);
365 $play = array(); /* needed to calculate winner later */
368 $firstcard = ""; /* first card in a trick */
370 echo "\n<ul class=\"tricks\">\n";
371 echo " <li> Hello $myname! History: </li>\n";
373 while($r = mysql_fetch_array($result,MYSQL_NUM))
380 /* save card to be able to find the winner of the trick later */
385 /* first card in a trick, output some html */
386 if($trick!=$lasttrick)
388 /* start of an old trick? */
389 echo " <li onclick=\"hl('$trickNR');\"><a href=\"#\">Trick $trickNR</a>\n".
390 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
391 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
393 else if($trick==$lasttrick)
395 /* start of a last trick? */
396 echo " <li onclick=\"hl('$trickNR');\"><a href=\"#\">Current Trick</a>\n".
397 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
398 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
401 /* remember first card, so that we are able to check, what cards can be played */
406 echo " <div class=\"card".($pos-1)."\">\n";
408 /* display comments */
410 echo " <span class=\"comment\">".$comment."</span>\n";
415 echo " </div>\n"; /* end div card */
421 echo " </div>\n </li>\n"; /* end div table, end li table */
425 if($seq!=4 && $trickNR>1)
426 echo " </div>\n </li>\n"; /* end div table, end li table */
430 /* whos turn is it? */
433 $winner = get_winner($play,$gametype); /* returns the position */
435 $firstcard = ""; /* new trick, no first card */
440 if($next==5) $next=1;
443 /* my turn?, display cards as links, ask for comments*/
444 if(DB_get_pos_by_hash($me) == $next)
449 /* do we want to play a card? */
450 if(myisset("card") && $myturn)
452 $card = $_REQUEST["card"];
453 $handid = DB_get_handid_by_hash($me);
455 /* check if we have card and that we haven't played it yet*/
456 /* set played in hand_card to true where hand_id and card_id*/
457 $result = mysql_query("SELECT id FROM Hand_Card WHERE played='false' and ".
458 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
459 $r = mysql_fetch_array($result,MYSQL_NUM);
464 mysql_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".DB_quote_smart($card));
466 /* get trick id or start new trick */
467 $a = DB_get_current_trickid($gameid);
471 $playid = DB_play_card($trickid,$handcardid,$sequence);
473 /*check for coment */
474 if(myisset("comment"))
476 DB_insert_comment($_REQUEST["comment"],$playid,$myid);
479 echo "<div class=\"card\">";
480 echo " you played <br />";
485 /*check if we still have cards left, else set status to gameover */
486 if(sizeof(DB_get_hand($me))==0)
488 DB_set_hand_status_by_hash($me,'gameover');
489 $mystatus='gameover';
492 /* if all players are done, set game status to game over */
493 $userids = DB_get_all_userid_by_gameid($gameid);
495 foreach($userids as $user)
496 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
500 DB_set_game_status_by_gameid($gameid,"gameover");
502 /* email next player */
503 if(DB_get_game_status_by_gameid($gameid)=='play')
507 $play = DB_get_cards_by_trick($trickid);
508 $winner = get_winner($play,$gametype); /* returns the position */
513 $next = DB_get_pos_by_hash($me)+1;
515 if($next==5) $next=1;
517 /* email next player */
518 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
519 $email = DB_get_email_by_hash($next_hash);
521 $message = "It's your turn now.\n".
522 "Use this link to play a card: ".$host."?me=".$next_hash."\n\n" ;
523 mymail($email,"[DoKo-debug] a card has been played",$message);
526 echo "DEBUG:<a href=\"index.php?me=".DB_get_hash_from_game_and_pos($gameid,$next).
527 "\"> next player </a> <br />\n";
533 echo "can't find that card?! <br />\n";
536 else if(myisset("card") && !$myturn )
538 echo "please wait until it's your turn! <br />\n";
541 $mycards = DB_get_hand($me);
543 echo "<div class=\"mycards\">\n";
545 if($myturn && !myisset("card"))
547 echo "Hello ".$myname.", it's your turn! <br />\n";
548 echo "Your cards are: <br />\n";
549 echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
551 /* do we have to follow suit? */
553 if(have_suit($mycards,$firstcard))
556 foreach($mycards as $card)
558 if($followsuit && !same_type($card,$firstcard))
561 display_link_card($card);
564 echo "<br />\nA short comments:<input name=\"comment\" type=\"text\" size=\"30\" maxlength=\"50\" />\n";
565 echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />\n";
566 echo "<input type=\"submit\" value=\"move\" />\n";
569 else if($mystatus=='play')
571 echo "Your cards are: <br />\n";
572 foreach($mycards as $card)
577 /* check if we need to set status to 'gameover' is done during playing of the card */
578 if($mystatus=='play')
580 /* the following happens only when the gamestatus is 'gameover' */
581 /* check if game is over, display results */
582 if(DB_get_game_status_by_gameid($gameid)=='play')
584 echo "the game is over for you.. other people still need to play though";
588 echo "the game is over now... guess the final score should be displayed here...<br />\n";
590 /* suggest a new game with the same people in it, just rotated once */
591 $names = DB_get_all_names_by_gameid($gameid);
592 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
596 echo "error in testing the status";
600 /* user status page */
601 else if(myisset("email","password"))
603 /* test id and password, should really be done in one step */
604 $email = $_REQUEST["email"];
605 $password = $_REQUEST["password"];
607 if(strlen($password)!=32)
608 $password = md5($password);
611 $uid = DB_get_userid_by_email_and_password($email,$password);
617 $time = DB_get_user_timestamp($uid);
618 $unixtime =strtotime($time);
620 $offset = DB_get_user_timezone($uid);
621 $zone = return_timezone($offset);
622 date_default_timezone_set($zone);
624 echo "last login: ".date("r",$unixtime)."<br />";
626 DB_update_user_timestamp($uid);
628 echo "<p>these are the games you are playing in:<br />\n";
629 $result = mysql_query("SELECT hash,game_id from Hand WHERE user_id='$uid' AND status<>'gameover'" );
630 while( $r = mysql_fetch_array($result,MYSQL_NUM))
631 echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a><br />";
634 $names = DB_get_all_names();
635 echo "<p>registered players:<br />\n";
636 foreach ($names as $name)
637 echo "$name <br />\n";
640 echo "<p>Want to start a new game? remember 4 names from the list above and visit ".
641 "<a href=\"".$host."?new\">this page.</a></p>";
645 echo "sorry email and password don't match <br />";
649 /* page for registration */
650 else if(myisset("register") )
654 /* new user wants to register */
655 else if(myisset("Rfullname","Remail","Rpassword","Rtimezone") )
658 if(DB_get_userid_by_name($_REQUEST["Rfullname"]))
660 echo "please chose another name<br />";
663 if(DB_get_userid_by_email($_REQUEST["Remail"]))
665 echo "this email address is already used ?!<br />";
670 $r=mysql_query("INSERT INTO User VALUES(NULL,".DB_quote_smart($_REQUEST["Rfullname"]).
671 ",".DB_quote_smart($_REQUEST["Remail"]).
672 ",".DB_quote_smart(md5($_REQUEST["Rpassword"])).
673 ",".DB_quote_smart($_REQUEST["Rtimezone"]).",NULL)");
676 echo " added you to the database";
678 echo " something went wrong";
681 /* default login page */