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 />".
189 " if you have a wedding, please send an email to the other players. <br />".
190 " if you have poverty you need to play a normal game,sorry...<br />".
191 " At the moment you need to reload this page to finish the setup.<br />";
192 if(!myisset("solo","wedding","poverty","nines") )
194 /* all these variables have a pre-selected default,
195 * so we should never get here,
196 * unless a user tries to cheat ;) */
197 echo "something went wrong...please contact the admin.";
201 /* check if this sickness needs to be handled first */
202 $gametype = DB_get_gametype_by_gameid($gameid);
203 $startplayer = DB_get_startplayer_by_gameid($gameid);
205 if( $_REQUEST["solo"]!="No")
207 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
208 DB_set_sickness_by_hash($me,"solo");
209 echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
211 if($gametype == "solo" && $startplayer<$mypos)
215 DB_set_gametype_by_gameid($gameid,"solo");
216 DB_set_startplayer_by_gameid($gameid,$mypos);
217 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
220 else if($_REQUEST["wedding"] == "yes")
222 /* TODO: add silent solo somewhere*/
223 echo "wedding was chosen<br />\n";
224 DB_set_sickness_by_hash($me,"wedding");
226 else if($_REQUEST["poverty"] == "yes")
228 echo "poverty was chosen<br />\n";
229 DB_set_sickness_by_hash($me,"poverty");
231 else if($_REQUEST["nines"] == "yes")
233 echo "nines was chosen<br />\n";
234 DB_set_sickness_by_hash($me,"nines");
238 DB_set_hand_status_by_hash($me,'poverty');
240 /* check all players and set game to final result, e.g. solo, wedding, povert, redeal */
244 /* here we need to check if there is a solo or some other form of sickness.
245 * If so, which one counts
246 * set that one in the Game table, delete other ones form Hand table
247 * tell people about it.
249 echo "<br />poverty not handeled at the moment... you need to play a normal game<br />".
250 "reload this page to finish the setup <br />";
252 /* only set this after all poverty, etc. are handeled*/
253 DB_set_hand_status_by_hash($me,'play');
255 /* check if the game can start */
256 $userids = DB_get_all_userid_by_gameid($gameid);
258 foreach($userids as $user)
259 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='play')
264 /* TODO: check what kind of game we are playing */
265 DB_set_game_status_by_gameid($gameid,'play');
271 /* both entries here, so that the tricks are visible for both.
272 * in case of 'play' there is a break later that skips the last part
275 /* figure out what kind of game we are playing,
276 * set the global variables $TRUMP,$DIAMONDS,$HEARTS,$CLUBS,$SPADES
280 $gametype = DB_get_gametype_by_gameid($gameid);
282 if($gametype=="solo")
284 $gametype = DB_get_solo_by_gameid($gameid);
285 $GT = $gametype." ".$GT;
290 set_gametype($gametype);
293 /* display useful things in divs */
295 /* display local time */
296 echo "<div class=\"time\">\n Local times:<table>";
298 $users = DB_get_all_userid_by_gameid($gameid);
299 foreach($users as $user)
301 $offset = DB_get_user_timezone($user);
302 $zone = return_timezone($offset);
303 date_default_timezone_set($zone);
304 $name = DB_get_name_by_userid($user);
306 echo "<tr> <td>$name</td> <td>".date("Y-m-d H:i:s")."</td></tr>\n";
308 echo "</table>\n</div>\n";
312 /* display links to the users status page */
313 $result = mysql_query("SELECT email,password from User WHERE id='$myid'" );
314 $r = mysql_fetch_array($result,MYSQL_NUM);
315 output_link_to_user_page($r[0],$r[1]);
319 /* end display useful things*/
321 /* has the game started? No, then just wait here...*/
322 $gamestatus = DB_get_game_status_by_gameid($gameid);
323 if($gamestatus == 'pre')
325 echo "you need to wait for the others... <br />";
326 break; /* not sure this works... the idea is that you can
327 * only play a card after everyone is ready to play */
330 /* display the table and the names */
331 $result = mysql_query("SELECT User.fullname as name,".
332 " Hand.position as position ".
334 "LEFT JOIN User ON User.id=Hand.user_id ".
335 "WHERE Hand.game_id='".$gameid."' ".
336 "ORDER BY position ASC");
338 echo "<div class=\"table\">\n".
339 " <img src=\"pics/table.png\" alt=\"table\" />\n";
340 while($r = mysql_fetch_array($result,MYSQL_NUM))
345 echo " <span class=\"table".($pos-1)."\">$name</span>\n";
349 /* get everything relevant to display the tricks */
350 $result = mysql_query("SELECT Hand_Card.card_id as card,".
351 " Hand.position as position,".
352 " Play.sequence as sequence, ".
356 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
357 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
358 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
359 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
360 "WHERE Trick.game_id='".$gameid."' ".
361 "ORDER BY Trick.id,sequence ASC");
366 $lasttrick = DB_get_max_trickid($gameid);
368 $play = array(); /* needed to calculate winner later */
371 $firstcard = ""; /* first card in a trick */
373 echo "\n<ul class=\"tricks\">\n";
374 echo " <li> Hello $myname! History: </li>\n";
376 while($r = mysql_fetch_array($result,MYSQL_NUM))
383 /* save card to be able to find the winner of the trick later */
388 /* first card in a trick, output some html */
389 if($trick!=$lasttrick)
391 /* start of an old trick? */
392 echo " <li onclick=\"hl('$trickNR');\"><a href=\"#\">Trick $trickNR</a>\n".
393 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
394 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
396 else if($trick==$lasttrick)
398 /* start of a last trick? */
399 echo " <li onclick=\"hl('$trickNR');\"><a href=\"#\">Current Trick</a>\n".
400 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
401 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
404 /* remember first card, so that we are able to check, what cards can be played */
409 echo " <div class=\"card".($pos-1)."\">\n";
411 /* display comments */
413 echo " <span class=\"comment\">".$comment."</span>\n";
418 echo " </div>\n"; /* end div card */
424 echo " </div>\n </li>\n"; /* end div table, end li table */
428 if($seq!=4 && $trickNR>1)
429 echo " </div>\n </li>\n"; /* end div table, end li table */
433 /* whos turn is it? */
436 $winner = get_winner($play,$gametype); /* returns the position */
438 $firstcard = ""; /* new trick, no first card */
443 if($next==5) $next=1;
446 /* my turn?, display cards as links, ask for comments*/
447 if(DB_get_pos_by_hash($me) == $next)
452 /* do we want to play a card? */
453 if(myisset("card") && $myturn)
455 $card = $_REQUEST["card"];
456 $handid = DB_get_handid_by_hash($me);
458 /* check if we have card and that we haven't played it yet*/
459 /* set played in hand_card to true where hand_id and card_id*/
460 $result = mysql_query("SELECT id FROM Hand_Card WHERE played='false' and ".
461 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
462 $r = mysql_fetch_array($result,MYSQL_NUM);
467 /* mark card as played */
468 mysql_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
469 DB_quote_smart($card));
471 /* get trick id or start new trick */
472 $a = DB_get_current_trickid($gameid);
476 $playid = DB_play_card($trickid,$handcardid,$sequence);
478 /* check for coment */
479 if(myisset("comment"))
481 DB_insert_comment($_REQUEST["comment"],$playid,$myid);
484 /* display played card */
485 echo "<div class=\"card\">";
486 echo " you played <br />";
490 /*check if we still have cards left, else set status to gameover */
491 if(sizeof(DB_get_hand($me))==0)
493 DB_set_hand_status_by_hash($me,'gameover');
494 $mystatus='gameover';
497 /* if all players are done, set game status to game over,
498 * get the points of the last trick and send out an email
501 $userids = DB_get_all_userid_by_gameid($gameid);
504 foreach($userids as $user)
505 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
510 DB_set_game_status_by_gameid($gameid,"gameover");
511 /* get score for last trick
512 * all other tricks are handled a few lines further down*/
513 $play = DB_get_cards_by_trick($trickid);
514 $winner = get_winner($play,$gametype); /* returns the position */
515 /* get points of last trick and save it */
517 foreach($play as $card)
518 $points = $points + card_value($card);
519 $winnerid = DB_get_handid_by_gameid_and_position($gameid,$winner);
521 mysql_query("INSERT INTO Score VALUES (NULL, '$gameid', '$winnerid', '$points')");
523 echo "ERROR during scoring";
525 /* email all players */
526 $result = mysql_query("SELECT fullname, SUM(score) FROM Score".
527 " LEFT JOIN Hand ON Hand.id=hand_id".
528 " LEFT JOIN User ON Hand.user_id=User.id".
529 " GROUP BY fullname" );
530 $message = "The game is over. Thanks for playing :)\n";
531 while( $r = mysql_fetch_array($result,MYSQL_NUM))
532 $message .= " FINAL SCORE: ".$r[0]." ".$r[1]."\n";
533 $message .= "\nIf your not in the list above your score is zero...\n";
534 foreach($userids as $user)
536 $To = DB_get_email_by_userid($user);
537 mymail($To,"[DoKo] game over",$message);
542 /* email next player */
543 if(DB_get_game_status_by_gameid($gameid)=='play')
547 $play = DB_get_cards_by_trick($trickid);
548 $winner = get_winner($play,$gametype); /* returns the position */
550 /* get points of last trick and save it, last trick is handled
551 * a few lines further up */
553 foreach($play as $card)
554 $points = $points + card_value($card);
556 $winnerid = DB_get_handid_by_gameid_and_position($gameid,$winner);
558 mysql_query("INSERT INTO Score VALUES (NULL, '$gameid', '$winnerid', '$points')");
560 echo "ERROR during scoring";
563 echo "DEBUG: $winner got $points <br />";
565 /* who is the next player? */
570 $next = DB_get_pos_by_hash($me)+1;
572 if($next==5) $next=1;
574 /* email next player */
575 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
576 $email = DB_get_email_by_hash($next_hash);
578 $message = "It's your turn now.\n".
579 "Use this link to play a card: ".$host."?me=".$next_hash."\n\n" ;
580 mymail($email,"[DoKo-debug] a card has been played",$message);
583 echo "DEBUG:<a href=\"index.php?me=".DB_get_hash_from_game_and_pos($gameid,$next).
584 "\"> next player </a> <br />\n";
590 echo "can't find that card?! <br />\n";
593 else if(myisset("card") && !$myturn )
595 echo "please wait until it's your turn! <br />\n";
598 $mycards = DB_get_hand($me);
600 echo "<div class=\"mycards\">\n";
602 if($myturn && !myisset("card"))
604 echo "Hello ".$myname.", it's your turn! <br />\n";
605 echo "Your cards are: <br />\n";
606 echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
608 /* do we have to follow suit? */
610 if(have_suit($mycards,$firstcard))
613 foreach($mycards as $card)
615 if($followsuit && !same_type($card,$firstcard))
618 display_link_card($card);
621 echo "<br />\nA short comments:<input name=\"comment\" type=\"text\" size=\"30\" maxlength=\"50\" />\n";
622 echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />\n";
623 echo "<input type=\"submit\" value=\"move\" />\n";
626 else if($mystatus=='play')
628 echo "Your cards are: <br />\n";
629 foreach($mycards as $card)
634 /* check if we need to set status to 'gameover' is done during playing of the card */
635 if($mystatus=='play')
637 /* the following happens only when the gamestatus is 'gameover' */
638 /* check if game is over, display results */
639 if(DB_get_game_status_by_gameid($gameid)=='play')
641 echo "the game is over for you.. other people still need to play though";
645 echo "the game is over now...<br />\n";
647 $result = mysql_query("SELECT fullname, SUM(score) FROM Score".
648 " LEFT JOIN Hand ON Hand.id=hand_id".
649 " LEFT JOIN User ON Hand.user_id=User.id".
650 " GROUP BY fullname" );
651 while( $r = mysql_fetch_array($result,MYSQL_NUM))
652 echo " FINAL SCORE: ".$r[0]." ".$r[1]."<br />";
654 /* suggest a new game with the same people in it, just rotated once */
655 $names = DB_get_all_names_by_gameid($gameid);
656 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
660 echo "error in testing the status";
664 /* user status page */
665 else if(myisset("email","password"))
667 /* test id and password, should really be done in one step */
668 $email = $_REQUEST["email"];
669 $password = $_REQUEST["password"];
671 if(strlen($password)!=32)
672 $password = md5($password);
675 $uid = DB_get_userid_by_email_and_password($email,$password);
681 $time = DB_get_user_timestamp($uid);
682 $unixtime =strtotime($time);
684 $offset = DB_get_user_timezone($uid);
685 $zone = return_timezone($offset);
686 date_default_timezone_set($zone);
688 echo "last login: ".date("r",$unixtime)."<br />";
690 DB_update_user_timestamp($uid);
692 echo "<p>these are the games you are playing in:<br />\n";
693 $result = mysql_query("SELECT hash,game_id from Hand WHERE user_id='$uid' AND status<>'gameover'" );
694 while( $r = mysql_fetch_array($result,MYSQL_NUM))
695 echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a><br />";
698 echo "<p>and these are your games that are already done:<br />\n";
699 $result = mysql_query("SELECT hash,game_id from Hand WHERE user_id='$uid' AND status='gameover'" );
700 while( $r = mysql_fetch_array($result,MYSQL_NUM))
701 echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a><br />";
704 $names = DB_get_all_names();
705 echo "<p>registered players:<br />\n";
706 foreach ($names as $name)
707 echo "$name <br />\n";
710 echo "<p>Want to start a new game? remember 4 names from the list above and visit ".
711 "<a href=\"".$host."?new\">this page.</a></p>";
715 echo "sorry email and password don't match <br />";
719 /* page for registration */
720 else if(myisset("register") )
724 /* new user wants to register */
725 else if(myisset("Rfullname","Remail","Rpassword","Rtimezone") )
728 if(DB_get_userid_by_name($_REQUEST["Rfullname"]))
730 echo "please chose another name<br />";
733 if(DB_get_userid_by_email($_REQUEST["Remail"]))
735 echo "this email address is already used ?!<br />";
740 $r=mysql_query("INSERT INTO User VALUES(NULL,".DB_quote_smart($_REQUEST["Rfullname"]).
741 ",".DB_quote_smart($_REQUEST["Remail"]).
742 ",".DB_quote_smart(md5($_REQUEST["Rpassword"])).
743 ",".DB_quote_smart($_REQUEST["Rtimezone"]).",NULL)");
746 echo " added you to the database";
748 echo " something went wrong";
751 /* default login page */