2 error_reporting(E_ALL);
7 include_once("config.php");
8 include_once("output.php"); /* html output only */
9 include_once("db.php"); /* database only */
10 include_once("functions.php"); /* the rest */
15 echo "Working on the database...please check back in a few mintues";
23 /* check if we want to start a new game */
25 output_form_for_new_game();
27 /*check if everything is ready to set up a new game */
28 else if( myisset("PlayerA", "PlayerB","PlayerC","PlayerD" ))
30 $PlayerA = $_REQUEST["PlayerA"];
31 $PlayerB = $_REQUEST["PlayerB"];
32 $PlayerC = $_REQUEST["PlayerC"];
33 $PlayerD = $_REQUEST["PlayerD"];
35 $EmailA = DB_get_email_by_name($PlayerA);
36 $EmailB = DB_get_email_by_name($PlayerB);
37 $EmailC = DB_get_email_by_name($PlayerC);
38 $EmailD = DB_get_email_by_name($PlayerD);
40 if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
42 echo "couldn't find one of the names, please start a new game";
46 $useridA = DB_get_userid_by_name($PlayerA);
47 $useridB = DB_get_userid_by_name($PlayerB);
48 $useridC = DB_get_userid_by_name($PlayerC);
49 $useridD = DB_get_userid_by_name($PlayerD);
51 /* create random numbers */
52 $randomNR = create_array_of_random_numbers();
53 $randomNRstring = join(":",$randomNR);
57 if(myisset("followup") )
59 $followup= $_REQUEST["followup"];
60 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,'1','pre','$followup' ,NULL)");
63 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,'1','pre', NULL ,NULL)");
64 $game_id = mysql_insert_id();
67 $hashA = md5("AGameOfDoko".$game_id.$PlayerA.$EmailA);
68 $hashB = md5("AGameOfDoko".$game_id.$PlayerB.$EmailB);
69 $hashC = md5("AGameOfDoko".$game_id.$PlayerC.$EmailC);
70 $hashD = md5("AGameOfDoko".$game_id.$PlayerD.$EmailD);
73 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridA).
74 ", ".DB_quote_smart($hashA).", 'start','1',NULL,NULL,NULL,'false','false',NULL)");
75 $hand_idA = mysql_insert_id();
76 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridB).
77 ", ".DB_quote_smart($hashB).", 'start','2',NULL,NULL,NULL,'false','false',NULL)");
78 $hand_idB = mysql_insert_id();
79 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridC).
80 ", ".DB_quote_smart($hashC).", 'start','3',NULL,NULL,NULL,'false','false',NULL)");
81 $hand_idC = mysql_insert_id();
82 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridD).
83 ", ".DB_quote_smart($hashD).", 'start','4',NULL,NULL,NULL,'false','false',NULL)");
84 $hand_idD = mysql_insert_id();
88 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idA', '".$randomNR[$i]."', 'false')");
90 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idB', '".$randomNR[$i]."', 'false')");
92 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idC', '".$randomNR[$i]."', 'false')");
94 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idD', '".$randomNR[$i]."', 'false')");
96 /* send out email, TODO: check for error with email */
98 "you are invited to play a game of DoKo (that is to debug the program ;).\n".
99 "Place comments and bug reports here:\n".
100 "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
101 "The whole round would consist of the following players:\n".
106 "If you want to join this game, please follow this link:\n\n".
109 mymail($EmailA,"You are invited to a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
110 mymail($EmailB,"You are invited to a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
111 mymail($EmailC,"You are invited to a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
112 mymail($EmailD,"You are invited to a game of DoKo","Hello $PlayerD,\n".$message.$hashD);
115 echo "You started a new game. The emails have been sent out!";
117 /* end set up a new game */
119 else if(myisset("me"))
121 /* handle request from one specific player,
122 * the hash is set on a per game base
125 $me = $_REQUEST["me"];
127 /* test for valid ID */
128 $myid = DB_get_userid_by_hash($me);
131 echo "Can't find you in the database, please check the url.<br />\n";
132 echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
136 DB_update_user_timestamp($myid);
138 /* get some information from the DB */
139 $gameid = DB_get_gameid_by_hash($me);
140 $myname = DB_get_name_by_hash($me);
141 $mystatus = DB_get_status_by_hash($me);
142 $mypos = DB_get_pos_by_hash($me);
147 check_want_to_play($me);
148 DB_set_hand_status_by_hash($me,'init');
151 if( !myisset("in","update") )
153 DB_set_hand_status_by_hash($me,'start');
154 echo "you need to answer both question";
158 if($_REQUEST["in"] == "no")
160 $message = "Hello, \n\n".
161 "the game has been canceled due to the request of one of the players.\n";
163 $userids = DB_get_all_userid_by_gameid($gameid);
164 foreach($userids as $user)
166 $To = DB_get_email_by_userid($user);
167 mymail($To,"[DoKo] game cancled",$message);
170 /* delete everything from the dB */
175 echo "thanks for joining the game... please scroll down";
176 echo "TODO: make this page nicer<br />";
177 echo "TODO: set card pref<br />";
179 $mycards = DB_get_hand($me);
181 echo "<p class=\"mycards\">your cards are: <br />\n";
182 foreach($mycards as $card)
186 check_for_sickness($me,$mycards);
188 DB_set_hand_status_by_hash($me,'check');
194 echo "checking if you selected solo or nines...<br />".
195 " if you have a wedding, please send an email to the other players. <br />".
196 " if you have poverty you need to play a normal game,sorry...<br />".
197 " Please click <a href=\"$host?me=$me\">here</a> to finish the setup.<br />";
198 if(!myisset("solo","wedding","poverty","nines") )
200 /* all these variables have a pre-selected default,
201 * so we should never get here,
202 * unless a user tries to cheat ;) */
203 echo "something went wrong...please contact the admin.";
207 /* check if this sickness needs to be handled first */
208 $gametype = DB_get_gametype_by_gameid($gameid);
209 $startplayer = DB_get_startplayer_by_gameid($gameid);
211 if( $_REQUEST["solo"]!="No")
213 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
214 DB_set_sickness_by_hash($me,"solo");
215 echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
217 if($gametype == "solo" && $startplayer<$mypos)
221 /* this solo comes first */
222 DB_set_gametype_by_gameid($gameid,"solo");
223 DB_set_startplayer_by_gameid($gameid,$mypos);
224 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
227 else if($_REQUEST["wedding"] == "yes")
229 /* TODO: add silent solo somewhere*/
230 echo "wedding was chosen<br />\n";
231 DB_set_sickness_by_hash($me,"wedding");
233 else if($_REQUEST["poverty"] == "yes")
235 echo "poverty was chosen<br />\n";
236 DB_set_sickness_by_hash($me,"poverty");
238 else if($_REQUEST["nines"] == "yes")
240 echo "nines was chosen<br />\n";
241 DB_set_sickness_by_hash($me,"nines");
245 DB_set_hand_status_by_hash($me,'poverty');
247 /* check all players and set game to final result, e.g. solo, wedding, povert, redeal */
251 /* here we need to check if there is a solo or some other form of sickness.
252 * If so, which one counts
253 * set that one in the Game table, delete other ones form Hand table
254 * tell people about it.
256 echo "<br />checking if someone else selected solo or nines... wedding and poverty not handled at the moment<br />".
257 " Please click <a href=\"$host?me=$me\">here</a> to finish the setup.<br />";
259 /* only set this after all poverty, etc. are handeled*/
260 DB_set_hand_status_by_hash($me,'play');
262 /* check if the game can start */
263 $userids = DB_get_all_userid_by_gameid($gameid);
265 foreach($userids as $user)
266 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='play')
271 DB_set_game_status_by_gameid($gameid,'play');
273 /* check what kind of game we are playing */
274 $gametype = DB_get_gametype_by_gameid($gameid);
275 $startplayer = DB_get_startplayer_by_gameid($gameid);
279 /* check for nines */
280 foreach($userids as $user)
282 if(DB_get_sickness_by_userid_and_gameid($user,$gameid) == 'nines')
286 /* gamestatus == normal, => cancel game */
287 if($nines && $gamestatus == "normal")
289 /* TODO: set game type to nines to be able to keep statistics */
291 $message = "Hello, \n\n".
292 "the game has been cancled becausee someone has five or more nines.\n";
294 $userids = DB_get_all_userid_by_gameid($gameid);
295 foreach($userids as $user)
297 $To = DB_get_email_by_userid($user);
298 mymail($To,"[DoKo] game cancled",$message);
301 /* delete everything from the dB */
309 /* check players for poverty */
310 foreach($userids as $user)
312 if(DB_get_sickness_by_userid_and_gameid($user,$gameid) == 'poverty')
315 $name = DB_get_name_by_userid($user);
316 echo "$name has a Vorbehalt. <br />";
319 /* if gamestatus == normal, set poverty or dpovert (in case two people have poverty) */
323 /* check players for wedding */
324 foreach($userids as $user)
326 if(DB_get_sickness_by_userid_and_gameid($user,$gameid) == 'wedding')
329 $name = DB_get_name_by_userid($user);
330 echo "$name has a Vorbehalt. <br />" ;
334 /* if gamestatus == normal, set wedding */
341 /* both entries here, so that the tricks are visible for both.
342 * in case of 'play' there is a break later that skips the last part
345 /* figure out what kind of game we are playing,
346 * set the global variables $TRUMP,$DIAMONDS,$HEARTS,$CLUBS,$SPADES
350 $gametype = DB_get_gametype_by_gameid($gameid);
352 if($gametype=="solo")
354 $gametype = DB_get_solo_by_gameid($gameid);
355 $GT = $gametype." ".$GT;
360 set_gametype($gametype);
362 /* get some infos about the game */
363 $gamestatus = DB_get_game_status_by_gameid($gameid);
365 /* display useful things in divs */
367 /* display local time */
368 echo "<div class=\"time\">\n Local times:<table>";
370 $users = DB_get_all_userid_by_gameid($gameid);
371 foreach($users as $user)
373 $offset = DB_get_user_timezone($user);
374 $zone = return_timezone($offset);
375 date_default_timezone_set($zone);
376 $name = DB_get_name_by_userid($user);
378 echo "<tr> <td>$name</td> <td>".date("Y-m-d H:i:s")."</td></tr>\n";
380 echo "</table>\n</div>\n";
382 if($gamestatus != 'pre')
385 /* display links to the users status page */
386 $result = mysql_query("SELECT email,password from User WHERE id='$myid'" );
387 $r = mysql_fetch_array($result,MYSQL_NUM);
388 output_link_to_user_page($r[0],$r[1]);
392 /* end display useful things*/
394 /* has the game started? No, then just wait here...*/
395 if($gamestatus == 'pre')
397 echo "you need to wait for the others... <br />";
398 break; /* not sure this works... the idea is that you can
399 * only play a card after everyone is ready to play */
402 /* display the table and the names */
403 $result = mysql_query("SELECT User.fullname as name,".
404 " Hand.position as position ".
406 "LEFT JOIN User ON User.id=Hand.user_id ".
407 "WHERE Hand.game_id='".$gameid."' ".
408 "ORDER BY position ASC");
410 echo "<div class=\"table\">\n".
411 " <img src=\"pics/table.png\" alt=\"table\" />\n";
412 while($r = mysql_fetch_array($result,MYSQL_NUM))
417 echo " <span class=\"table".($pos-1)."\">$name</span>\n";
421 /* get everything relevant to display the tricks */
422 $result = mysql_query("SELECT Hand_Card.card_id as card,".
423 " Hand.position as position,".
424 " Play.sequence as sequence, ".
428 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
429 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
430 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
431 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
432 "WHERE Trick.game_id='".$gameid."' ".
433 "ORDER BY Trick.id,sequence ASC");
436 $lasttrick = DB_get_max_trickid($gameid);
438 $play = array(); /* needed to calculate winner later */
440 $pos = DB_get_startplayer_by_gameid($gameid)-1;
441 $firstcard = ""; /* first card in a trick */
443 echo "\n<ul class=\"tricks\">\n";
444 echo " <li> Hello $myname! History: </li>\n";
446 while($r = mysql_fetch_array($result,MYSQL_NUM))
453 /* save card to be able to find the winner of the trick later */
458 /* first card in a trick, output some html */
459 if($trick!=$lasttrick)
461 /* start of an old trick? */
462 echo " <li onclick=\"hl('$trickNR');\"><a href=\"#\">Trick $trickNR</a>\n".
463 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
464 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
466 else if($trick==$lasttrick)
468 /* start of a last trick? */
469 echo " <li onclick=\"hl('$trickNR');\"><a href=\"#\">Current Trick</a>\n".
470 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
471 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
474 /* remember first card, so that we are able to check, what cards can be played */
479 echo " <div class=\"card".($pos-1)."\">\n";
481 /* display comments */
483 echo " <span class=\"comment\">".$comment."</span>\n";
488 echo " </div>\n"; /* end div card */
494 echo " </div>\n </li>\n"; /* end div table, end li table */
498 if($seq!=4 && $trickNR>1)
499 echo " </div>\n </li>\n"; /* end div table, end li table */
503 /* whos turn is it? */
506 $winner = get_winner($play,$gametype); /* returns the position */
508 $firstcard = ""; /* new trick, no first card */
513 if($next==5) $next=1;
516 /* my turn?, display cards as links, ask for comments*/
517 if(DB_get_pos_by_hash($me) == $next)
522 /* do we want to play a card? */
523 if(myisset("card") && $myturn)
525 $card = $_REQUEST["card"];
526 $handid = DB_get_handid_by_hash($me);
528 /* check if we have card and that we haven't played it yet*/
529 /* set played in hand_card to true where hand_id and card_id*/
530 $result = mysql_query("SELECT id FROM Hand_Card WHERE played='false' and ".
531 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
532 $r = mysql_fetch_array($result,MYSQL_NUM);
537 /* mark card as played */
538 mysql_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
539 DB_quote_smart($card));
541 /* get trick id or start new trick */
542 $a = DB_get_current_trickid($gameid);
546 $playid = DB_play_card($trickid,$handcardid,$sequence);
548 /* check for coment */
549 if(myisset("comment"))
551 DB_insert_comment($_REQUEST["comment"],$playid,$myid);
554 /* display played card */
555 echo "<div class=\"card\">";
556 echo " you played <br />";
560 /*check if we still have cards left, else set status to gameover */
561 if(sizeof(DB_get_hand($me))==0)
563 DB_set_hand_status_by_hash($me,'gameover');
564 $mystatus='gameover';
567 /* if all players are done, set game status to game over,
568 * get the points of the last trick and send out an email
571 $userids = DB_get_all_userid_by_gameid($gameid);
574 foreach($userids as $user)
575 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
580 DB_set_game_status_by_gameid($gameid,"gameover");
581 /* get score for last trick
582 * all other tricks are handled a few lines further down*/
583 $play = DB_get_cards_by_trick($trickid);
584 $winner = get_winner($play,$gametype); /* returns the position */
585 /* get points of last trick and save it */
587 foreach($play as $card)
588 $points = $points + card_value($card);
589 $winnerid = DB_get_handid_by_gameid_and_position($gameid,$winner);
591 mysql_query("INSERT INTO Score VALUES (NULL, '$gameid', '$winnerid', '$points')");
593 echo "ERROR during scoring";
595 /* email all players */
596 $result = mysql_query("SELECT fullname, SUM(score) FROM Score".
597 " LEFT JOIN Hand ON Hand.id=hand_id".
598 " LEFT JOIN User ON Hand.user_id=User.id".
599 " WHERE Hand.game_id=$gameid".
600 " GROUP BY fullname" );
601 $message = "The game is over. Thanks for playing :)\n";
602 while( $r = mysql_fetch_array($result,MYSQL_NUM))
603 $message .= " FINAL SCORE: ".$r[0]." ".$r[1]."\n";
604 $message .= "\nIf your not in the list above your score is zero...\n";
605 foreach($userids as $user)
607 $To = DB_get_email_by_userid($user);
608 mymail($To,"[DoKo] game over",$message);
613 /* email next player */
614 if(DB_get_game_status_by_gameid($gameid)=='play')
618 $play = DB_get_cards_by_trick($trickid);
619 $winner = get_winner($play,$gametype); /* returns the position */
621 /* get points of last trick and save it, last trick is handled
622 * a few lines further up */
624 foreach($play as $card)
625 $points = $points + card_value($card);
627 $winnerid = DB_get_handid_by_gameid_and_position($gameid,$winner);
629 mysql_query("INSERT INTO Score VALUES (NULL, '$gameid', '$winnerid', '$points')");
631 echo "ERROR during scoring";
634 echo "DEBUG: $winner got $points <br />";
636 /* who is the next player? */
641 $next = DB_get_pos_by_hash($me)+1;
643 if($next==5) $next=1;
645 /* email next player */
646 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
647 $email = DB_get_email_by_hash($next_hash);
649 $message = "It's your turn now.\n".
650 "Use this link to play a card: ".$host."?me=".$next_hash."\n\n" ;
651 mymail($email,"[DoKo-debug] a card has been played",$message);
654 echo "DEBUG:<a href=\"index.php?me=".DB_get_hash_from_game_and_pos($gameid,$next).
655 "\"> next player </a> <br />\n";
661 echo "can't find that card?! <br />\n";
664 else if(myisset("card") && !$myturn )
666 echo "please wait until it's your turn! <br />\n";
669 $mycards = DB_get_hand($me);
670 $mycards = mysort($mycards,$gametype);
671 echo "<div class=\"mycards\">\n";
673 if($myturn && !myisset("card"))
675 echo "Hello ".$myname.", it's your turn! <br />\n";
676 echo "Your cards are: <br />\n";
677 echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
679 /* do we have to follow suite? */
681 if(have_suit($mycards,$firstcard))
684 foreach($mycards as $card)
686 if($followsuit && !same_type($card,$firstcard))
689 display_link_card($card);
692 echo "<br />\nA short comments:<input name=\"comment\" type=\"text\" size=\"30\" maxlength=\"50\" />\n";
693 echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />\n";
694 echo "<input type=\"submit\" value=\"move\" />\n";
697 else if($mystatus=='play')
699 echo "Your cards are: <br />\n";
700 foreach($mycards as $card)
705 /* check if we need to set status to 'gameover' is done during playing of the card */
706 if($mystatus=='play')
708 /* the following happens only when the gamestatus is 'gameover' */
709 /* check if game is over, display results */
710 if(DB_get_game_status_by_gameid($gameid)=='play')
712 echo "the game is over for you.. other people still need to play though";
716 echo "the game is over now...<br />\n";
718 $result = mysql_query("SELECT fullname, SUM(score) FROM Score".
719 " LEFT JOIN Hand ON Hand.id=hand_id".
720 " LEFT JOIN User ON Hand.user_id=User.id".
721 " WHERE Hand.game_id=$gameid".
722 " GROUP BY fullname" );
723 while( $r = mysql_fetch_array($result,MYSQL_NUM))
724 echo " FINAL SCORE: ".$r[0]." ".$r[1]."<br />";
726 /* suggest a new game with the same people in it, just rotated once */
727 $names = DB_get_all_names_by_gameid($gameid);
728 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
732 echo "error in testing the status";
736 /* user status page */
737 else if(myisset("email","password"))
739 /* test id and password, should really be done in one step */
740 $email = $_REQUEST["email"];
741 $password = $_REQUEST["password"];
743 if(strlen($password)!=32)
744 $password = md5($password);
747 $uid = DB_get_userid_by_email_and_password($email,$password);
753 $time = DB_get_user_timestamp($uid);
754 $unixtime =strtotime($time);
756 $offset = DB_get_user_timezone($uid);
757 $zone = return_timezone($offset);
758 date_default_timezone_set($zone);
760 echo "last login: ".date("r",$unixtime)."<br />";
762 DB_update_user_timestamp($uid);
764 echo "<p>these are the games you are playing in:<br />\n";
765 $result = mysql_query("SELECT hash,game_id from Hand WHERE user_id='$uid' AND status<>'gameover'" );
766 while( $r = mysql_fetch_array($result,MYSQL_NUM))
767 echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a><br />";
770 echo "<p>and these are your games that are already done:<br />\n";
771 $result = mysql_query("SELECT hash,game_id from Hand WHERE user_id='$uid' AND status='gameover'" );
772 while( $r = mysql_fetch_array($result,MYSQL_NUM))
773 echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a><br />";
776 $names = DB_get_all_names();
777 echo "<p>registered players:<br />\n";
778 foreach ($names as $name)
779 echo "$name <br />\n";
782 echo "<p>Want to start a new game? remember 4 names from the list above and visit ".
783 "<a href=\"".$host."?new\">this page.</a></p>";
787 echo "sorry email and password don't match <br />";
791 /* page for registration */
792 else if(myisset("register") )
796 /* new user wants to register */
797 else if(myisset("Rfullname","Remail","Rpassword","Rtimezone") )
800 if(DB_get_userid_by_name($_REQUEST["Rfullname"]))
802 echo "please chose another name<br />";
805 if(DB_get_userid_by_email($_REQUEST["Remail"]))
807 echo "this email address is already used ?!<br />";
812 $r=mysql_query("INSERT INTO User VALUES(NULL,".DB_quote_smart($_REQUEST["Rfullname"]).
813 ",".DB_quote_smart($_REQUEST["Remail"]).
814 ",".DB_quote_smart(md5($_REQUEST["Rpassword"])).
815 ",".DB_quote_smart($_REQUEST["Rtimezone"]).",NULL)");
818 echo " added you to the database";
820 echo " something went wrong";
823 /* default login page */