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 $message = "Hello, \n\n".
156 "the game has been canceled due to the request of one of the players.\n";
158 $userids = DB_get_all_userid_by_gameid($gameid);
159 foreach($userids as $user)
161 $To = DB_get_email_by_userid($user);
162 mymail($To,"[DoKo] game over",$message);
165 /* delete everything from the dB */
170 echo "thanks for joining the game... please scroll down";
171 echo "TODO: make this page nicer<br />";
172 echo "TODO: set card pref<br />";
174 $mycards = DB_get_hand($me);
176 echo "<p class=\"mycards\">your cards are: <br />\n";
177 foreach($mycards as $card)
181 check_for_sickness($me,$mycards);
183 DB_set_hand_status_by_hash($me,'check');
189 echo "checking for solo...<br />".
190 " if you have a wedding, please send an email to the other players. <br />".
191 " if you have poverty you need to play a normal game,sorry...<br />".
192 " At the moment you need to reload this page to finish the setup.<br />";
193 if(!myisset("solo","wedding","poverty","nines") )
195 /* all these variables have a pre-selected default,
196 * so we should never get here,
197 * unless a user tries to cheat ;) */
198 echo "something went wrong...please contact the admin.";
202 /* check if this sickness needs to be handled first */
203 $gametype = DB_get_gametype_by_gameid($gameid);
204 $startplayer = DB_get_startplayer_by_gameid($gameid);
206 if( $_REQUEST["solo"]!="No")
208 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
209 DB_set_sickness_by_hash($me,"solo");
210 echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
212 if($gametype == "solo" && $startplayer<$mypos)
216 DB_set_gametype_by_gameid($gameid,"solo");
217 DB_set_startplayer_by_gameid($gameid,$mypos);
218 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
221 else if($_REQUEST["wedding"] == "yes")
223 /* TODO: add silent solo somewhere*/
224 echo "wedding was chosen<br />\n";
225 DB_set_sickness_by_hash($me,"wedding");
227 else if($_REQUEST["poverty"] == "yes")
229 echo "poverty was chosen<br />\n";
230 DB_set_sickness_by_hash($me,"poverty");
232 else if($_REQUEST["nines"] == "yes")
234 echo "nines was chosen<br />\n";
235 DB_set_sickness_by_hash($me,"nines");
239 DB_set_hand_status_by_hash($me,'poverty');
241 /* check all players and set game to final result, e.g. solo, wedding, povert, redeal */
245 /* here we need to check if there is a solo or some other form of sickness.
246 * If so, which one counts
247 * set that one in the Game table, delete other ones form Hand table
248 * tell people about it.
250 echo "<br />poverty not handeled at the moment... you need to play a normal game<br />".
251 "reload this page to finish the setup <br />";
253 /* only set this after all poverty, etc. are handeled*/
254 DB_set_hand_status_by_hash($me,'play');
256 /* check if the game can start */
257 $userids = DB_get_all_userid_by_gameid($gameid);
259 foreach($userids as $user)
260 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='play')
265 /* TODO: check what kind of game we are playing */
266 DB_set_game_status_by_gameid($gameid,'play');
272 /* both entries here, so that the tricks are visible for both.
273 * in case of 'play' there is a break later that skips the last part
276 /* figure out what kind of game we are playing,
277 * set the global variables $TRUMP,$DIAMONDS,$HEARTS,$CLUBS,$SPADES
281 $gametype = DB_get_gametype_by_gameid($gameid);
283 if($gametype=="solo")
285 $gametype = DB_get_solo_by_gameid($gameid);
286 $GT = $gametype." ".$GT;
291 set_gametype($gametype);
294 /* display useful things in divs */
296 /* display local time */
297 echo "<div class=\"time\">\n Local times:<table>";
299 $users = DB_get_all_userid_by_gameid($gameid);
300 foreach($users as $user)
302 $offset = DB_get_user_timezone($user);
303 $zone = return_timezone($offset);
304 date_default_timezone_set($zone);
305 $name = DB_get_name_by_userid($user);
307 echo "<tr> <td>$name</td> <td>".date("Y-m-d H:i:s")."</td></tr>\n";
309 echo "</table>\n</div>\n";
313 /* display links to the users status page */
314 $result = mysql_query("SELECT email,password from User WHERE id='$myid'" );
315 $r = mysql_fetch_array($result,MYSQL_NUM);
316 output_link_to_user_page($r[0],$r[1]);
320 /* end display useful things*/
322 /* has the game started? No, then just wait here...*/
323 $gamestatus = DB_get_game_status_by_gameid($gameid);
324 if($gamestatus == 'pre')
326 echo "you need to wait for the others... <br />";
327 break; /* not sure this works... the idea is that you can
328 * only play a card after everyone is ready to play */
331 /* display the table and the names */
332 $result = mysql_query("SELECT User.fullname as name,".
333 " Hand.position as position ".
335 "LEFT JOIN User ON User.id=Hand.user_id ".
336 "WHERE Hand.game_id='".$gameid."' ".
337 "ORDER BY position ASC");
339 echo "<div class=\"table\">\n".
340 " <img src=\"pics/table.png\" alt=\"table\" />\n";
341 while($r = mysql_fetch_array($result,MYSQL_NUM))
346 echo " <span class=\"table".($pos-1)."\">$name</span>\n";
350 /* get everything relevant to display the tricks */
351 $result = mysql_query("SELECT Hand_Card.card_id as card,".
352 " Hand.position as position,".
353 " Play.sequence as sequence, ".
357 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
358 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
359 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
360 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
361 "WHERE Trick.game_id='".$gameid."' ".
362 "ORDER BY Trick.id,sequence ASC");
367 $lasttrick = DB_get_max_trickid($gameid);
369 $play = array(); /* needed to calculate winner later */
372 $firstcard = ""; /* first card in a trick */
374 echo "\n<ul class=\"tricks\">\n";
375 echo " <li> Hello $myname! History: </li>\n";
377 while($r = mysql_fetch_array($result,MYSQL_NUM))
384 /* save card to be able to find the winner of the trick later */
389 /* first card in a trick, output some html */
390 if($trick!=$lasttrick)
392 /* start of an old trick? */
393 echo " <li onclick=\"hl('$trickNR');\"><a href=\"#\">Trick $trickNR</a>\n".
394 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
395 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
397 else if($trick==$lasttrick)
399 /* start of a last trick? */
400 echo " <li onclick=\"hl('$trickNR');\"><a href=\"#\">Current Trick</a>\n".
401 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
402 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
405 /* remember first card, so that we are able to check, what cards can be played */
410 echo " <div class=\"card".($pos-1)."\">\n";
412 /* display comments */
414 echo " <span class=\"comment\">".$comment."</span>\n";
419 echo " </div>\n"; /* end div card */
425 echo " </div>\n </li>\n"; /* end div table, end li table */
429 if($seq!=4 && $trickNR>1)
430 echo " </div>\n </li>\n"; /* end div table, end li table */
434 /* whos turn is it? */
437 $winner = get_winner($play,$gametype); /* returns the position */
439 $firstcard = ""; /* new trick, no first card */
444 if($next==5) $next=1;
447 /* my turn?, display cards as links, ask for comments*/
448 if(DB_get_pos_by_hash($me) == $next)
453 /* do we want to play a card? */
454 if(myisset("card") && $myturn)
456 $card = $_REQUEST["card"];
457 $handid = DB_get_handid_by_hash($me);
459 /* check if we have card and that we haven't played it yet*/
460 /* set played in hand_card to true where hand_id and card_id*/
461 $result = mysql_query("SELECT id FROM Hand_Card WHERE played='false' and ".
462 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
463 $r = mysql_fetch_array($result,MYSQL_NUM);
468 /* mark card as played */
469 mysql_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
470 DB_quote_smart($card));
472 /* get trick id or start new trick */
473 $a = DB_get_current_trickid($gameid);
477 $playid = DB_play_card($trickid,$handcardid,$sequence);
479 /* check for coment */
480 if(myisset("comment"))
482 DB_insert_comment($_REQUEST["comment"],$playid,$myid);
485 /* display played card */
486 echo "<div class=\"card\">";
487 echo " you played <br />";
491 /*check if we still have cards left, else set status to gameover */
492 if(sizeof(DB_get_hand($me))==0)
494 DB_set_hand_status_by_hash($me,'gameover');
495 $mystatus='gameover';
498 /* if all players are done, set game status to game over,
499 * get the points of the last trick and send out an email
502 $userids = DB_get_all_userid_by_gameid($gameid);
505 foreach($userids as $user)
506 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
511 DB_set_game_status_by_gameid($gameid,"gameover");
512 /* get score for last trick
513 * all other tricks are handled a few lines further down*/
514 $play = DB_get_cards_by_trick($trickid);
515 $winner = get_winner($play,$gametype); /* returns the position */
516 /* get points of last trick and save it */
518 foreach($play as $card)
519 $points = $points + card_value($card);
520 $winnerid = DB_get_handid_by_gameid_and_position($gameid,$winner);
522 mysql_query("INSERT INTO Score VALUES (NULL, '$gameid', '$winnerid', '$points')");
524 echo "ERROR during scoring";
526 /* email all players */
527 $result = mysql_query("SELECT fullname, SUM(score) FROM Score".
528 " LEFT JOIN Hand ON Hand.id=hand_id".
529 " LEFT JOIN User ON Hand.user_id=User.id".
530 " WHERE Hand.game_id=$gameid".
531 " GROUP BY fullname" );
532 $message = "The game is over. Thanks for playing :)\n";
533 while( $r = mysql_fetch_array($result,MYSQL_NUM))
534 $message .= " FINAL SCORE: ".$r[0]." ".$r[1]."\n";
535 $message .= "\nIf your not in the list above your score is zero...\n";
536 foreach($userids as $user)
538 $To = DB_get_email_by_userid($user);
539 mymail($To,"[DoKo] game over",$message);
544 /* email next player */
545 if(DB_get_game_status_by_gameid($gameid)=='play')
549 $play = DB_get_cards_by_trick($trickid);
550 $winner = get_winner($play,$gametype); /* returns the position */
552 /* get points of last trick and save it, last trick is handled
553 * a few lines further up */
555 foreach($play as $card)
556 $points = $points + card_value($card);
558 $winnerid = DB_get_handid_by_gameid_and_position($gameid,$winner);
560 mysql_query("INSERT INTO Score VALUES (NULL, '$gameid', '$winnerid', '$points')");
562 echo "ERROR during scoring";
565 echo "DEBUG: $winner got $points <br />";
567 /* who is the next player? */
572 $next = DB_get_pos_by_hash($me)+1;
574 if($next==5) $next=1;
576 /* email next player */
577 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
578 $email = DB_get_email_by_hash($next_hash);
580 $message = "It's your turn now.\n".
581 "Use this link to play a card: ".$host."?me=".$next_hash."\n\n" ;
582 mymail($email,"[DoKo-debug] a card has been played",$message);
585 echo "DEBUG:<a href=\"index.php?me=".DB_get_hash_from_game_and_pos($gameid,$next).
586 "\"> next player </a> <br />\n";
592 echo "can't find that card?! <br />\n";
595 else if(myisset("card") && !$myturn )
597 echo "please wait until it's your turn! <br />\n";
600 $mycards = DB_get_hand($me);
602 echo "<div class=\"mycards\">\n";
604 if($myturn && !myisset("card"))
606 echo "Hello ".$myname.", it's your turn! <br />\n";
607 echo "Your cards are: <br />\n";
608 echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
610 /* do we have to follow suit? */
612 if(have_suit($mycards,$firstcard))
615 foreach($mycards as $card)
617 if($followsuit && !same_type($card,$firstcard))
620 display_link_card($card);
623 echo "<br />\nA short comments:<input name=\"comment\" type=\"text\" size=\"30\" maxlength=\"50\" />\n";
624 echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />\n";
625 echo "<input type=\"submit\" value=\"move\" />\n";
628 else if($mystatus=='play')
630 echo "Your cards are: <br />\n";
631 foreach($mycards as $card)
636 /* check if we need to set status to 'gameover' is done during playing of the card */
637 if($mystatus=='play')
639 /* the following happens only when the gamestatus is 'gameover' */
640 /* check if game is over, display results */
641 if(DB_get_game_status_by_gameid($gameid)=='play')
643 echo "the game is over for you.. other people still need to play though";
647 echo "the game is over now...<br />\n";
649 $result = mysql_query("SELECT fullname, SUM(score) FROM Score".
650 " LEFT JOIN Hand ON Hand.id=hand_id".
651 " LEFT JOIN User ON Hand.user_id=User.id".
652 " WHERE Hand.game_id=$gameid".
653 " GROUP BY fullname" );
654 while( $r = mysql_fetch_array($result,MYSQL_NUM))
655 echo " FINAL SCORE: ".$r[0]." ".$r[1]."<br />";
657 /* suggest a new game with the same people in it, just rotated once */
658 $names = DB_get_all_names_by_gameid($gameid);
659 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
663 echo "error in testing the status";
667 /* user status page */
668 else if(myisset("email","password"))
670 /* test id and password, should really be done in one step */
671 $email = $_REQUEST["email"];
672 $password = $_REQUEST["password"];
674 if(strlen($password)!=32)
675 $password = md5($password);
678 $uid = DB_get_userid_by_email_and_password($email,$password);
684 $time = DB_get_user_timestamp($uid);
685 $unixtime =strtotime($time);
687 $offset = DB_get_user_timezone($uid);
688 $zone = return_timezone($offset);
689 date_default_timezone_set($zone);
691 echo "last login: ".date("r",$unixtime)."<br />";
693 DB_update_user_timestamp($uid);
695 echo "<p>these are the games you are playing in:<br />\n";
696 $result = mysql_query("SELECT hash,game_id from Hand WHERE user_id='$uid' AND status<>'gameover'" );
697 while( $r = mysql_fetch_array($result,MYSQL_NUM))
698 echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a><br />";
701 echo "<p>and these are your games that are already done:<br />\n";
702 $result = mysql_query("SELECT hash,game_id from Hand WHERE user_id='$uid' AND status='gameover'" );
703 while( $r = mysql_fetch_array($result,MYSQL_NUM))
704 echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a><br />";
707 $names = DB_get_all_names();
708 echo "<p>registered players:<br />\n";
709 foreach ($names as $name)
710 echo "$name <br />\n";
713 echo "<p>Want to start a new game? remember 4 names from the list above and visit ".
714 "<a href=\"".$host."?new\">this page.</a></p>";
718 echo "sorry email and password don't match <br />";
722 /* page for registration */
723 else if(myisset("register") )
727 /* new user wants to register */
728 else if(myisset("Rfullname","Remail","Rpassword","Rtimezone") )
731 if(DB_get_userid_by_name($_REQUEST["Rfullname"]))
733 echo "please chose another name<br />";
736 if(DB_get_userid_by_email($_REQUEST["Remail"]))
738 echo "this email address is already used ?!<br />";
743 $r=mysql_query("INSERT INTO User VALUES(NULL,".DB_quote_smart($_REQUEST["Rfullname"]).
744 ",".DB_quote_smart($_REQUEST["Remail"]).
745 ",".DB_quote_smart(md5($_REQUEST["Rpassword"])).
746 ",".DB_quote_smart($_REQUEST["Rtimezone"]).",NULL)");
749 echo " added you to the database";
751 echo " something went wrong";
754 /* default login page */