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 echo "You started a new game. The emails have been sent out!";
114 /* end set up a new game */
116 else if(myisset("me"))
118 /* handle request from one specific player,
119 * the hash is set on a per game base
122 $me = $_REQUEST["me"];
124 /* test for valid ID */
125 $myid = DB_get_userid_by_hash($me);
128 echo "Can't find you in the database, please check the url.<br />\n";
129 echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
133 DB_update_user_timestamp($myid);
135 /* get some information from the DB */
136 $gameid = DB_get_gameid_by_hash($me);
137 $myname = DB_get_name_by_hash($me);
138 $mystatus = DB_get_status_by_hash($me);
139 $mypos = DB_get_pos_by_hash($me);
144 check_want_to_play($me);
145 DB_set_hand_status_by_hash($me,'init');
148 if( !myisset("in","update") )
150 DB_set_hand_status_by_hash($me,'start');
151 echo "you need to answer both question";
155 if($_REQUEST["in"] == "no")
157 $message = "Hello, \n\n".
158 "the game has been canceled due to the request of one of the players.\n";
160 $userids = DB_get_all_userid_by_gameid($gameid);
161 foreach($userids as $user)
163 $To = DB_get_email_by_userid($user);
164 mymail($To,"[DoKo] game cancled",$message);
167 /* delete everything from the dB */
172 echo "thanks for joining the game... please scroll down";
173 echo "TODO: make this page nicer<br />";
174 echo "TODO: set card pref<br />";
176 $mycards = DB_get_hand($me);
178 echo "<p class=\"mycards\">your cards are: <br />\n";
179 foreach($mycards as $card)
183 check_for_sickness($me,$mycards);
185 DB_set_hand_status_by_hash($me,'check');
191 echo "checking if you selected solo or nines...<br />".
192 " if you have a wedding, please send an email to the other players. <br />".
193 " if you have poverty you need to play a normal game,sorry...<br />".
194 " At the moment you need to reload this page to finish the setup.<br />";
195 if(!myisset("solo","wedding","poverty","nines") )
197 /* all these variables have a pre-selected default,
198 * so we should never get here,
199 * unless a user tries to cheat ;) */
200 echo "something went wrong...please contact the admin.";
204 /* check if this sickness needs to be handled first */
205 $gametype = DB_get_gametype_by_gameid($gameid);
206 $startplayer = DB_get_startplayer_by_gameid($gameid);
208 if( $_REQUEST["solo"]!="No")
210 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
211 DB_set_sickness_by_hash($me,"solo");
212 echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
214 if($gametype == "solo" && $startplayer<$mypos)
218 /* this solo comes first */
219 DB_set_gametype_by_gameid($gameid,"solo");
220 DB_set_startplayer_by_gameid($gameid,$mypos);
221 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
224 else if($_REQUEST["wedding"] == "yes")
226 /* TODO: add silent solo somewhere*/
227 echo "wedding was chosen<br />\n";
228 DB_set_sickness_by_hash($me,"wedding");
230 else if($_REQUEST["poverty"] == "yes")
232 echo "poverty was chosen<br />\n";
233 DB_set_sickness_by_hash($me,"poverty");
235 else if($_REQUEST["nines"] == "yes")
237 echo "nines was chosen<br />\n";
238 DB_set_sickness_by_hash($me,"nines");
242 DB_set_hand_status_by_hash($me,'poverty');
244 /* check all players and set game to final result, e.g. solo, wedding, povert, redeal */
248 /* here we need to check if there is a solo or some other form of sickness.
249 * If so, which one counts
250 * set that one in the Game table, delete other ones form Hand table
251 * tell people about it.
253 echo "<br />checking if someone else selected solo or nines... wedding and poverty not handled at the moment<br />".
254 "reload this page to finish the setup <br />";
256 /* only set this after all poverty, etc. are handeled*/
257 DB_set_hand_status_by_hash($me,'play');
259 /* check if the game can start */
260 $userids = DB_get_all_userid_by_gameid($gameid);
262 foreach($userids as $user)
263 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='play')
268 DB_set_game_status_by_gameid($gameid,'play');
270 /* check what kind of game we are playing */
271 $gametype = DB_get_gametype_by_gameid($gameid);
272 $startplayer = DB_get_startplayer_by_gameid($gameid);
276 /* check for nines */
277 foreach($userids as $user)
279 if(DB_get_sickness_by_userid_and_gameid($user,$gameid) == 'nines')
283 /* gamestatus == normal, => cancel game */
284 if($nines && $gamestatus == "normal")
286 /* TODO: set game type to nines to be able to keep statistics */
288 $message = "Hello, \n\n".
289 "the game has been cancled becausee someone has five or more nines.\n";
291 $userids = DB_get_all_userid_by_gameid($gameid);
292 foreach($userids as $user)
294 $To = DB_get_email_by_userid($user);
295 mymail($To,"[DoKo] game cancled",$message);
298 /* delete everything from the dB */
306 /* check players for poverty */
307 foreach($userids as $user)
309 if(DB_get_sickness_by_userid_and_gameid($user,$gameid) == 'poverty')
312 $name = DB_get_name_by_userid($user);
313 echo "$name has a Vorbehalt. <br />";
316 /* if gamestatus == normal, set poverty or dpovert (in case two people have poverty) */
320 /* check players for wedding */
321 foreach($userids as $user)
323 if(DB_get_sickness_by_userid_and_gameid($user,$gameid) == 'wedding')
326 $name = DB_get_name_by_userid($user);
327 echo "$name has a Vorbehalt. <br />" ;
331 /* if gamestatus == normal, set wedding */
338 /* both entries here, so that the tricks are visible for both.
339 * in case of 'play' there is a break later that skips the last part
342 /* figure out what kind of game we are playing,
343 * set the global variables $TRUMP,$DIAMONDS,$HEARTS,$CLUBS,$SPADES
347 $gametype = DB_get_gametype_by_gameid($gameid);
349 if($gametype=="solo")
351 $gametype = DB_get_solo_by_gameid($gameid);
352 $GT = $gametype." ".$GT;
357 set_gametype($gametype);
359 /* get some infos about the game */
360 $gamestatus = DB_get_game_status_by_gameid($gameid);
362 /* display useful things in divs */
364 /* display local time */
365 echo "<div class=\"time\">\n Local times:<table>";
367 $users = DB_get_all_userid_by_gameid($gameid);
368 foreach($users as $user)
370 $offset = DB_get_user_timezone($user);
371 $zone = return_timezone($offset);
372 date_default_timezone_set($zone);
373 $name = DB_get_name_by_userid($user);
375 echo "<tr> <td>$name</td> <td>".date("Y-m-d H:i:s")."</td></tr>\n";
377 echo "</table>\n</div>\n";
379 if($gamestatus != 'pre')
382 /* display links to the users status page */
383 $result = mysql_query("SELECT email,password from User WHERE id='$myid'" );
384 $r = mysql_fetch_array($result,MYSQL_NUM);
385 output_link_to_user_page($r[0],$r[1]);
389 /* end display useful things*/
391 /* has the game started? No, then just wait here...*/
392 if($gamestatus == 'pre')
394 echo "you need to wait for the others... <br />";
395 break; /* not sure this works... the idea is that you can
396 * only play a card after everyone is ready to play */
399 /* display the table and the names */
400 $result = mysql_query("SELECT User.fullname as name,".
401 " Hand.position as position ".
403 "LEFT JOIN User ON User.id=Hand.user_id ".
404 "WHERE Hand.game_id='".$gameid."' ".
405 "ORDER BY position ASC");
407 echo "<div class=\"table\">\n".
408 " <img src=\"pics/table.png\" alt=\"table\" />\n";
409 while($r = mysql_fetch_array($result,MYSQL_NUM))
414 echo " <span class=\"table".($pos-1)."\">$name</span>\n";
418 /* get everything relevant to display the tricks */
419 $result = mysql_query("SELECT Hand_Card.card_id as card,".
420 " Hand.position as position,".
421 " Play.sequence as sequence, ".
425 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
426 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
427 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
428 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
429 "WHERE Trick.game_id='".$gameid."' ".
430 "ORDER BY Trick.id,sequence ASC");
433 $lasttrick = DB_get_max_trickid($gameid);
435 $play = array(); /* needed to calculate winner later */
437 $pos = DB_get_startplayer_by_gameid($gameid)-1;
438 $firstcard = ""; /* first card in a trick */
440 echo "\n<ul class=\"tricks\">\n";
441 echo " <li> Hello $myname! History: </li>\n";
443 while($r = mysql_fetch_array($result,MYSQL_NUM))
450 /* save card to be able to find the winner of the trick later */
455 /* first card in a trick, output some html */
456 if($trick!=$lasttrick)
458 /* start of an old trick? */
459 echo " <li onclick=\"hl('$trickNR');\"><a href=\"#\">Trick $trickNR</a>\n".
460 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
461 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
463 else if($trick==$lasttrick)
465 /* start of a last trick? */
466 echo " <li onclick=\"hl('$trickNR');\"><a href=\"#\">Current Trick</a>\n".
467 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
468 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
471 /* remember first card, so that we are able to check, what cards can be played */
476 echo " <div class=\"card".($pos-1)."\">\n";
478 /* display comments */
480 echo " <span class=\"comment\">".$comment."</span>\n";
485 echo " </div>\n"; /* end div card */
491 echo " </div>\n </li>\n"; /* end div table, end li table */
495 if($seq!=4 && $trickNR>1)
496 echo " </div>\n </li>\n"; /* end div table, end li table */
500 /* whos turn is it? */
503 $winner = get_winner($play,$gametype); /* returns the position */
505 $firstcard = ""; /* new trick, no first card */
510 if($next==5) $next=1;
513 /* my turn?, display cards as links, ask for comments*/
514 if(DB_get_pos_by_hash($me) == $next)
519 /* do we want to play a card? */
520 if(myisset("card") && $myturn)
522 $card = $_REQUEST["card"];
523 $handid = DB_get_handid_by_hash($me);
525 /* check if we have card and that we haven't played it yet*/
526 /* set played in hand_card to true where hand_id and card_id*/
527 $result = mysql_query("SELECT id FROM Hand_Card WHERE played='false' and ".
528 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
529 $r = mysql_fetch_array($result,MYSQL_NUM);
534 /* mark card as played */
535 mysql_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
536 DB_quote_smart($card));
538 /* get trick id or start new trick */
539 $a = DB_get_current_trickid($gameid);
543 $playid = DB_play_card($trickid,$handcardid,$sequence);
545 /* check for coment */
546 if(myisset("comment"))
548 DB_insert_comment($_REQUEST["comment"],$playid,$myid);
551 /* display played card */
552 echo "<div class=\"card\">";
553 echo " you played <br />";
557 /*check if we still have cards left, else set status to gameover */
558 if(sizeof(DB_get_hand($me))==0)
560 DB_set_hand_status_by_hash($me,'gameover');
561 $mystatus='gameover';
564 /* if all players are done, set game status to game over,
565 * get the points of the last trick and send out an email
568 $userids = DB_get_all_userid_by_gameid($gameid);
571 foreach($userids as $user)
572 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
577 DB_set_game_status_by_gameid($gameid,"gameover");
578 /* get score for last trick
579 * all other tricks are handled a few lines further down*/
580 $play = DB_get_cards_by_trick($trickid);
581 $winner = get_winner($play,$gametype); /* returns the position */
582 /* get points of last trick and save it */
584 foreach($play as $card)
585 $points = $points + card_value($card);
586 $winnerid = DB_get_handid_by_gameid_and_position($gameid,$winner);
588 mysql_query("INSERT INTO Score VALUES (NULL, '$gameid', '$winnerid', '$points')");
590 echo "ERROR during scoring";
592 /* email all players */
593 $result = mysql_query("SELECT fullname, SUM(score) FROM Score".
594 " LEFT JOIN Hand ON Hand.id=hand_id".
595 " LEFT JOIN User ON Hand.user_id=User.id".
596 " WHERE Hand.game_id=$gameid".
597 " GROUP BY fullname" );
598 $message = "The game is over. Thanks for playing :)\n";
599 while( $r = mysql_fetch_array($result,MYSQL_NUM))
600 $message .= " FINAL SCORE: ".$r[0]." ".$r[1]."\n";
601 $message .= "\nIf your not in the list above your score is zero...\n";
602 foreach($userids as $user)
604 $To = DB_get_email_by_userid($user);
605 mymail($To,"[DoKo] game over",$message);
610 /* email next player */
611 if(DB_get_game_status_by_gameid($gameid)=='play')
615 $play = DB_get_cards_by_trick($trickid);
616 $winner = get_winner($play,$gametype); /* returns the position */
618 /* get points of last trick and save it, last trick is handled
619 * a few lines further up */
621 foreach($play as $card)
622 $points = $points + card_value($card);
624 $winnerid = DB_get_handid_by_gameid_and_position($gameid,$winner);
626 mysql_query("INSERT INTO Score VALUES (NULL, '$gameid', '$winnerid', '$points')");
628 echo "ERROR during scoring";
631 echo "DEBUG: $winner got $points <br />";
633 /* who is the next player? */
638 $next = DB_get_pos_by_hash($me)+1;
640 if($next==5) $next=1;
642 /* email next player */
643 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
644 $email = DB_get_email_by_hash($next_hash);
646 $message = "It's your turn now.\n".
647 "Use this link to play a card: ".$host."?me=".$next_hash."\n\n" ;
648 mymail($email,"[DoKo-debug] a card has been played",$message);
651 echo "DEBUG:<a href=\"index.php?me=".DB_get_hash_from_game_and_pos($gameid,$next).
652 "\"> next player </a> <br />\n";
658 echo "can't find that card?! <br />\n";
661 else if(myisset("card") && !$myturn )
663 echo "please wait until it's your turn! <br />\n";
666 $mycards = DB_get_hand($me);
668 echo "<div class=\"mycards\">\n";
670 if($myturn && !myisset("card"))
672 echo "Hello ".$myname.", it's your turn! <br />\n";
673 echo "Your cards are: <br />\n";
674 echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
676 /* do we have to follow suite? */
678 if(have_suit($mycards,$firstcard))
681 foreach($mycards as $card)
683 if($followsuit && !same_type($card,$firstcard))
686 display_link_card($card);
689 echo "<br />\nA short comments:<input name=\"comment\" type=\"text\" size=\"30\" maxlength=\"50\" />\n";
690 echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />\n";
691 echo "<input type=\"submit\" value=\"move\" />\n";
694 else if($mystatus=='play')
696 echo "Your cards are: <br />\n";
697 foreach($mycards as $card)
702 /* check if we need to set status to 'gameover' is done during playing of the card */
703 if($mystatus=='play')
705 /* the following happens only when the gamestatus is 'gameover' */
706 /* check if game is over, display results */
707 if(DB_get_game_status_by_gameid($gameid)=='play')
709 echo "the game is over for you.. other people still need to play though";
713 echo "the game is over now...<br />\n";
715 $result = mysql_query("SELECT fullname, SUM(score) FROM Score".
716 " LEFT JOIN Hand ON Hand.id=hand_id".
717 " LEFT JOIN User ON Hand.user_id=User.id".
718 " WHERE Hand.game_id=$gameid".
719 " GROUP BY fullname" );
720 while( $r = mysql_fetch_array($result,MYSQL_NUM))
721 echo " FINAL SCORE: ".$r[0]." ".$r[1]."<br />";
723 /* suggest a new game with the same people in it, just rotated once */
724 $names = DB_get_all_names_by_gameid($gameid);
725 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
729 echo "error in testing the status";
733 /* user status page */
734 else if(myisset("email","password"))
736 /* test id and password, should really be done in one step */
737 $email = $_REQUEST["email"];
738 $password = $_REQUEST["password"];
740 if(strlen($password)!=32)
741 $password = md5($password);
744 $uid = DB_get_userid_by_email_and_password($email,$password);
750 $time = DB_get_user_timestamp($uid);
751 $unixtime =strtotime($time);
753 $offset = DB_get_user_timezone($uid);
754 $zone = return_timezone($offset);
755 date_default_timezone_set($zone);
757 echo "last login: ".date("r",$unixtime)."<br />";
759 DB_update_user_timestamp($uid);
761 echo "<p>these are the games you are playing in:<br />\n";
762 $result = mysql_query("SELECT hash,game_id from Hand WHERE user_id='$uid' AND status<>'gameover'" );
763 while( $r = mysql_fetch_array($result,MYSQL_NUM))
764 echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a><br />";
767 echo "<p>and these are your games that are already done:<br />\n";
768 $result = mysql_query("SELECT hash,game_id from Hand WHERE user_id='$uid' AND status='gameover'" );
769 while( $r = mysql_fetch_array($result,MYSQL_NUM))
770 echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a><br />";
773 $names = DB_get_all_names();
774 echo "<p>registered players:<br />\n";
775 foreach ($names as $name)
776 echo "$name <br />\n";
779 echo "<p>Want to start a new game? remember 4 names from the list above and visit ".
780 "<a href=\"".$host."?new\">this page.</a></p>";
784 echo "sorry email and password don't match <br />";
788 /* page for registration */
789 else if(myisset("register") )
793 /* new user wants to register */
794 else if(myisset("Rfullname","Remail","Rpassword","Rtimezone") )
797 if(DB_get_userid_by_name($_REQUEST["Rfullname"]))
799 echo "please chose another name<br />";
802 if(DB_get_userid_by_email($_REQUEST["Remail"]))
804 echo "this email address is already used ?!<br />";
809 $r=mysql_query("INSERT INTO User VALUES(NULL,".DB_quote_smart($_REQUEST["Rfullname"]).
810 ",".DB_quote_smart($_REQUEST["Remail"]).
811 ",".DB_quote_smart(md5($_REQUEST["Rpassword"])).
812 ",".DB_quote_smart($_REQUEST["Rtimezone"]).",NULL)");
815 echo " added you to the database";
817 echo " something went wrong";
820 /* default login page */