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 cancled",$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 if you selected solo or nines...<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 /* this solo comes first */
217 DB_set_gametype_by_gameid($gameid,"solo");
218 DB_set_startplayer_by_gameid($gameid,$mypos);
219 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
222 else if($_REQUEST["wedding"] == "yes")
224 /* TODO: add silent solo somewhere*/
225 echo "wedding was chosen<br />\n";
226 DB_set_sickness_by_hash($me,"wedding");
228 else if($_REQUEST["poverty"] == "yes")
230 echo "poverty was chosen<br />\n";
231 DB_set_sickness_by_hash($me,"poverty");
233 else if($_REQUEST["nines"] == "yes")
235 echo "nines was chosen<br />\n";
236 DB_set_sickness_by_hash($me,"nines");
240 DB_set_hand_status_by_hash($me,'poverty');
242 /* check all players and set game to final result, e.g. solo, wedding, povert, redeal */
246 /* here we need to check if there is a solo or some other form of sickness.
247 * If so, which one counts
248 * set that one in the Game table, delete other ones form Hand table
249 * tell people about it.
251 echo "<br />checking if someone else selected solo or nines... wedding and poverty not handled at the moment<br />".
252 "reload this page to finish the setup <br />";
254 /* only set this after all poverty, etc. are handeled*/
255 DB_set_hand_status_by_hash($me,'play');
257 /* check if the game can start */
258 $userids = DB_get_all_userid_by_gameid($gameid);
260 foreach($userids as $user)
261 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='play')
266 DB_set_game_status_by_gameid($gameid,'play');
268 /* check what kind of game we are playing */
269 $gametype = DB_get_gametype_by_gameid($gameid);
270 $startplayer = DB_get_startplayer_by_gameid($gameid);
274 /* check for nines */
275 foreach($userids as $user)
277 if(DB_get_sickness_by_userid_and_gameid($user,$gameid) == 'nines')
281 /* gamestatus == normal, => cancel game */
282 if($nines && $gamestatus == "normal")
284 /* TODO: set game type to nines to be able to keep statistics */
286 $message = "Hello, \n\n".
287 "the game has been cancled becausee someone has five or more nines.\n";
289 $userids = DB_get_all_userid_by_gameid($gameid);
290 foreach($userids as $user)
292 $To = DB_get_email_by_userid($user);
293 mymail($To,"[DoKo] game cancled",$message);
296 /* delete everything from the dB */
304 /* check players for poverty */
305 foreach($userids as $user)
307 if(DB_get_sickness_by_userid_and_gameid($user,$gameid) == 'poverty')
310 $name = DB_get_name_by_userid($user);
311 echo "$name has a Vorbehalt. <br />";
314 /* if gamestatus == normal, set poverty or dpovert (in case two people have poverty) */
318 /* check players for wedding */
319 foreach($userids as $user)
321 if(DB_get_sickness_by_userid_and_gameid($user,$gameid) == 'wedding')
324 $name = DB_get_name_by_userid($user);
325 echo "$name has a Vorbehalt. <br />" ;
329 /* if gamestatus == normal, set wedding */
336 /* both entries here, so that the tricks are visible for both.
337 * in case of 'play' there is a break later that skips the last part
340 /* figure out what kind of game we are playing,
341 * set the global variables $TRUMP,$DIAMONDS,$HEARTS,$CLUBS,$SPADES
345 $gametype = DB_get_gametype_by_gameid($gameid);
347 if($gametype=="solo")
349 $gametype = DB_get_solo_by_gameid($gameid);
350 $GT = $gametype." ".$GT;
355 set_gametype($gametype);
357 /* get some infos about the game */
358 $gamestatus = DB_get_game_status_by_gameid($gameid);
360 /* display useful things in divs */
362 /* display local time */
363 echo "<div class=\"time\">\n Local times:<table>";
365 $users = DB_get_all_userid_by_gameid($gameid);
366 foreach($users as $user)
368 $offset = DB_get_user_timezone($user);
369 $zone = return_timezone($offset);
370 date_default_timezone_set($zone);
371 $name = DB_get_name_by_userid($user);
373 echo "<tr> <td>$name</td> <td>".date("Y-m-d H:i:s")."</td></tr>\n";
375 echo "</table>\n</div>\n";
377 if($gamestatus != 'pre')
380 /* display links to the users status page */
381 $result = mysql_query("SELECT email,password from User WHERE id='$myid'" );
382 $r = mysql_fetch_array($result,MYSQL_NUM);
383 output_link_to_user_page($r[0],$r[1]);
387 /* end display useful things*/
389 /* has the game started? No, then just wait here...*/
390 if($gamestatus == 'pre')
392 echo "you need to wait for the others... <br />";
393 break; /* not sure this works... the idea is that you can
394 * only play a card after everyone is ready to play */
397 /* display the table and the names */
398 $result = mysql_query("SELECT User.fullname as name,".
399 " Hand.position as position ".
401 "LEFT JOIN User ON User.id=Hand.user_id ".
402 "WHERE Hand.game_id='".$gameid."' ".
403 "ORDER BY position ASC");
405 echo "<div class=\"table\">\n".
406 " <img src=\"pics/table.png\" alt=\"table\" />\n";
407 while($r = mysql_fetch_array($result,MYSQL_NUM))
412 echo " <span class=\"table".($pos-1)."\">$name</span>\n";
416 /* get everything relevant to display the tricks */
417 $result = mysql_query("SELECT Hand_Card.card_id as card,".
418 " Hand.position as position,".
419 " Play.sequence as sequence, ".
423 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
424 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
425 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
426 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
427 "WHERE Trick.game_id='".$gameid."' ".
428 "ORDER BY Trick.id,sequence ASC");
431 $lasttrick = DB_get_max_trickid($gameid);
433 $play = array(); /* needed to calculate winner later */
436 $firstcard = ""; /* first card in a trick */
438 echo "\n<ul class=\"tricks\">\n";
439 echo " <li> Hello $myname! History: </li>\n";
441 while($r = mysql_fetch_array($result,MYSQL_NUM))
448 /* save card to be able to find the winner of the trick later */
453 /* first card in a trick, output some html */
454 if($trick!=$lasttrick)
456 /* start of an old trick? */
457 echo " <li onclick=\"hl('$trickNR');\"><a href=\"#\">Trick $trickNR</a>\n".
458 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
459 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
461 else if($trick==$lasttrick)
463 /* start of a last trick? */
464 echo " <li onclick=\"hl('$trickNR');\"><a href=\"#\">Current Trick</a>\n".
465 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
466 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
469 /* remember first card, so that we are able to check, what cards can be played */
474 echo " <div class=\"card".($pos-1)."\">\n";
476 /* display comments */
478 echo " <span class=\"comment\">".$comment."</span>\n";
483 echo " </div>\n"; /* end div card */
489 echo " </div>\n </li>\n"; /* end div table, end li table */
493 if($seq!=4 && $trickNR>1)
494 echo " </div>\n </li>\n"; /* end div table, end li table */
498 /* whos turn is it? */
501 $winner = get_winner($play,$gametype); /* returns the position */
503 $firstcard = ""; /* new trick, no first card */
508 if($next==5) $next=1;
511 /* my turn?, display cards as links, ask for comments*/
512 if(DB_get_pos_by_hash($me) == $next)
517 /* do we want to play a card? */
518 if(myisset("card") && $myturn)
520 $card = $_REQUEST["card"];
521 $handid = DB_get_handid_by_hash($me);
523 /* check if we have card and that we haven't played it yet*/
524 /* set played in hand_card to true where hand_id and card_id*/
525 $result = mysql_query("SELECT id FROM Hand_Card WHERE played='false' and ".
526 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
527 $r = mysql_fetch_array($result,MYSQL_NUM);
532 /* mark card as played */
533 mysql_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
534 DB_quote_smart($card));
536 /* get trick id or start new trick */
537 $a = DB_get_current_trickid($gameid);
541 $playid = DB_play_card($trickid,$handcardid,$sequence);
543 /* check for coment */
544 if(myisset("comment"))
546 DB_insert_comment($_REQUEST["comment"],$playid,$myid);
549 /* display played card */
550 echo "<div class=\"card\">";
551 echo " you played <br />";
555 /*check if we still have cards left, else set status to gameover */
556 if(sizeof(DB_get_hand($me))==0)
558 DB_set_hand_status_by_hash($me,'gameover');
559 $mystatus='gameover';
562 /* if all players are done, set game status to game over,
563 * get the points of the last trick and send out an email
566 $userids = DB_get_all_userid_by_gameid($gameid);
569 foreach($userids as $user)
570 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
575 DB_set_game_status_by_gameid($gameid,"gameover");
576 /* get score for last trick
577 * all other tricks are handled a few lines further down*/
578 $play = DB_get_cards_by_trick($trickid);
579 $winner = get_winner($play,$gametype); /* returns the position */
580 /* get points of last trick and save it */
582 foreach($play as $card)
583 $points = $points + card_value($card);
584 $winnerid = DB_get_handid_by_gameid_and_position($gameid,$winner);
586 mysql_query("INSERT INTO Score VALUES (NULL, '$gameid', '$winnerid', '$points')");
588 echo "ERROR during scoring";
590 /* email all players */
591 $result = mysql_query("SELECT fullname, SUM(score) FROM Score".
592 " LEFT JOIN Hand ON Hand.id=hand_id".
593 " LEFT JOIN User ON Hand.user_id=User.id".
594 " WHERE Hand.game_id=$gameid".
595 " GROUP BY fullname" );
596 $message = "The game is over. Thanks for playing :)\n";
597 while( $r = mysql_fetch_array($result,MYSQL_NUM))
598 $message .= " FINAL SCORE: ".$r[0]." ".$r[1]."\n";
599 $message .= "\nIf your not in the list above your score is zero...\n";
600 foreach($userids as $user)
602 $To = DB_get_email_by_userid($user);
603 mymail($To,"[DoKo] game over",$message);
608 /* email next player */
609 if(DB_get_game_status_by_gameid($gameid)=='play')
613 $play = DB_get_cards_by_trick($trickid);
614 $winner = get_winner($play,$gametype); /* returns the position */
616 /* get points of last trick and save it, last trick is handled
617 * a few lines further up */
619 foreach($play as $card)
620 $points = $points + card_value($card);
622 $winnerid = DB_get_handid_by_gameid_and_position($gameid,$winner);
624 mysql_query("INSERT INTO Score VALUES (NULL, '$gameid', '$winnerid', '$points')");
626 echo "ERROR during scoring";
629 echo "DEBUG: $winner got $points <br />";
631 /* who is the next player? */
636 $next = DB_get_pos_by_hash($me)+1;
638 if($next==5) $next=1;
640 /* email next player */
641 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
642 $email = DB_get_email_by_hash($next_hash);
644 $message = "It's your turn now.\n".
645 "Use this link to play a card: ".$host."?me=".$next_hash."\n\n" ;
646 mymail($email,"[DoKo-debug] a card has been played",$message);
649 echo "DEBUG:<a href=\"index.php?me=".DB_get_hash_from_game_and_pos($gameid,$next).
650 "\"> next player </a> <br />\n";
656 echo "can't find that card?! <br />\n";
659 else if(myisset("card") && !$myturn )
661 echo "please wait until it's your turn! <br />\n";
664 $mycards = DB_get_hand($me);
666 echo "<div class=\"mycards\">\n";
668 if($myturn && !myisset("card"))
670 echo "Hello ".$myname.", it's your turn! <br />\n";
671 echo "Your cards are: <br />\n";
672 echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
674 /* do we have to follow suite? */
676 if(have_suit($mycards,$firstcard))
679 foreach($mycards as $card)
681 if($followsuit && !same_type($card,$firstcard))
684 display_link_card($card);
687 echo "<br />\nA short comments:<input name=\"comment\" type=\"text\" size=\"30\" maxlength=\"50\" />\n";
688 echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />\n";
689 echo "<input type=\"submit\" value=\"move\" />\n";
692 else if($mystatus=='play')
694 echo "Your cards are: <br />\n";
695 foreach($mycards as $card)
700 /* check if we need to set status to 'gameover' is done during playing of the card */
701 if($mystatus=='play')
703 /* the following happens only when the gamestatus is 'gameover' */
704 /* check if game is over, display results */
705 if(DB_get_game_status_by_gameid($gameid)=='play')
707 echo "the game is over for you.. other people still need to play though";
711 echo "the game is over now...<br />\n";
713 $result = mysql_query("SELECT fullname, SUM(score) FROM Score".
714 " LEFT JOIN Hand ON Hand.id=hand_id".
715 " LEFT JOIN User ON Hand.user_id=User.id".
716 " WHERE Hand.game_id=$gameid".
717 " GROUP BY fullname" );
718 while( $r = mysql_fetch_array($result,MYSQL_NUM))
719 echo " FINAL SCORE: ".$r[0]." ".$r[1]."<br />";
721 /* suggest a new game with the same people in it, just rotated once */
722 $names = DB_get_all_names_by_gameid($gameid);
723 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
727 echo "error in testing the status";
731 /* user status page */
732 else if(myisset("email","password"))
734 /* test id and password, should really be done in one step */
735 $email = $_REQUEST["email"];
736 $password = $_REQUEST["password"];
738 if(strlen($password)!=32)
739 $password = md5($password);
742 $uid = DB_get_userid_by_email_and_password($email,$password);
748 $time = DB_get_user_timestamp($uid);
749 $unixtime =strtotime($time);
751 $offset = DB_get_user_timezone($uid);
752 $zone = return_timezone($offset);
753 date_default_timezone_set($zone);
755 echo "last login: ".date("r",$unixtime)."<br />";
757 DB_update_user_timestamp($uid);
759 echo "<p>these are the games you are playing in:<br />\n";
760 $result = mysql_query("SELECT hash,game_id from Hand WHERE user_id='$uid' AND status<>'gameover'" );
761 while( $r = mysql_fetch_array($result,MYSQL_NUM))
762 echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a><br />";
765 echo "<p>and these are your games that are already done:<br />\n";
766 $result = mysql_query("SELECT hash,game_id from Hand WHERE user_id='$uid' AND status='gameover'" );
767 while( $r = mysql_fetch_array($result,MYSQL_NUM))
768 echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a><br />";
771 $names = DB_get_all_names();
772 echo "<p>registered players:<br />\n";
773 foreach ($names as $name)
774 echo "$name <br />\n";
777 echo "<p>Want to start a new game? remember 4 names from the list above and visit ".
778 "<a href=\"".$host."?new\">this page.</a></p>";
782 echo "sorry email and password don't match <br />";
786 /* page for registration */
787 else if(myisset("register") )
791 /* new user wants to register */
792 else if(myisset("Rfullname","Remail","Rpassword","Rtimezone") )
795 if(DB_get_userid_by_name($_REQUEST["Rfullname"]))
797 echo "please chose another name<br />";
800 if(DB_get_userid_by_email($_REQUEST["Remail"]))
802 echo "this email address is already used ?!<br />";
807 $r=mysql_query("INSERT INTO User VALUES(NULL,".DB_quote_smart($_REQUEST["Rfullname"]).
808 ",".DB_quote_smart($_REQUEST["Remail"]).
809 ",".DB_quote_smart(md5($_REQUEST["Rpassword"])).
810 ",".DB_quote_smart($_REQUEST["Rtimezone"]).",NULL)");
813 echo " added you to the database";
815 echo " something went wrong";
818 /* default login page */