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 /* check if we want to start a new game */
14 output_form_for_new_game();
16 /*check if everything is ready to set up a new game */
17 else if( myisset("PlayerA", "PlayerB","PlayerC","PlayerD" ))
19 $PlayerA = $_REQUEST["PlayerA"];
20 $PlayerB = $_REQUEST["PlayerB"];
21 $PlayerC = $_REQUEST["PlayerC"];
22 $PlayerD = $_REQUEST["PlayerD"];
24 $EmailA = DB_get_email_by_name($PlayerA);
25 $EmailB = DB_get_email_by_name($PlayerB);
26 $EmailC = DB_get_email_by_name($PlayerC);
27 $EmailD = DB_get_email_by_name($PlayerD);
29 if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
31 echo "couldn't find one of the names, please start a new game";
35 $useridA = DB_get_userid_by_name($PlayerA);
36 $useridB = DB_get_userid_by_name($PlayerB);
37 $useridC = DB_get_userid_by_name($PlayerC);
38 $useridD = DB_get_userid_by_name($PlayerD);
40 /* create random numbers */
41 $randomNR = create_array_of_random_numbers();
42 $randomNRstring = join(":",$randomNR);
46 if(myisset("followup") )
48 $followup= $_REQUEST["followup"];
49 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,'1','pre','$followup' ,NULL)");
52 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,'1','pre', NULL ,NULL)");
53 $game_id = mysql_insert_id();
56 $hashA = md5("AGameOfDoko".$game_id.$PlayerA.$EmailA);
57 $hashB = md5("AGameOfDoko".$game_id.$PlayerB.$EmailB);
58 $hashC = md5("AGameOfDoko".$game_id.$PlayerC.$EmailC);
59 $hashD = md5("AGameOfDoko".$game_id.$PlayerD.$EmailD);
62 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridA).
63 ", ".DB_quote_smart($hashA).", 'start','1',NULL,NULL,NULL,'false','false',NULL)");
64 $hand_idA = mysql_insert_id();
65 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridB).
66 ", ".DB_quote_smart($hashB).", 'start','2',NULL,NULL,NULL,'false','false',NULL)");
67 $hand_idB = mysql_insert_id();
68 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridC).
69 ", ".DB_quote_smart($hashC).", 'start','3',NULL,NULL,NULL,'false','false',NULL)");
70 $hand_idC = mysql_insert_id();
71 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridD).
72 ", ".DB_quote_smart($hashD).", 'start','4',NULL,NULL,NULL,'false','false',NULL)");
73 $hand_idD = mysql_insert_id();
77 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idA', '".$randomNR[$i]."', 'false')");
79 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idB', '".$randomNR[$i]."', 'false')");
81 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idC', '".$randomNR[$i]."', 'false')");
83 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idD', '".$randomNR[$i]."', 'false')");
85 /* send out email, TODO: check for error with email */
87 "you are invited to play a game of DoKo (that is to debug the program ;).\n".
88 "Place comments and bug reports here:\n".
89 "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
90 "The whole round would consist of the following players:\n".
95 "If you want to join this game, please follow this link:\n\n".
98 mymail($EmailA,"You are invited to a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
99 mymail($EmailB,"You are invited to a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
100 mymail($EmailC,"You are invited to a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
101 mymail($EmailD,"You are invited to a game of DoKo","Hello $PlayerD,\n".$message.$hashD);
104 /* end set up a new game */
106 else if(myisset("me"))
108 /* handle request from one specific player,
109 * the hash is set on a per game base
112 $me = $_REQUEST["me"];
114 /* test for valid ID */
115 $myid = DB_get_userid_by_hash($me);
118 echo "Can't find you in the database, please check the url.<br />\n";
119 echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
123 DB_update_user_timestamp($myid);
125 /* get some information from the DB */
126 $gameid = DB_get_gameid_by_hash($me);
127 $myname = DB_get_name_by_hash($me);
128 $mystatus = DB_get_status_by_hash($me);
129 $mypos = DB_get_pos_by_hash($me);
134 check_want_to_play($me);
135 DB_set_hand_status_by_hash($me,'init');
138 if( !myisset("in","update") )
140 DB_set_hand_status_by_hash($me,'start');
141 echo "you need to answer both question";
145 if($_REQUEST["in"] == "no")
147 echo "TODO: email everyone that the game has been canceled.<br />";
148 /*something like need to modify for DB backend
151 $message = "Hello ".$player[$hash[$i]]["name"].",\n\n".
152 "the game has been canceled due to the request of one of the players.\n";
153 mymail($player[$hash[$i]]["email"],"[DoKo-Debug] the game has been canceled",$message);
156 /* delete everything from the dB */
161 echo "thanks for joining the game... please scroll down";
162 echo "TODO: make this page nicer<br />";
163 echo "TODO: set card pref<br />";
165 $mycards = DB_get_hand($me);
167 echo "<p class=\"mycards\">your cards are: <br />\n";
168 foreach($mycards as $card)
172 check_for_sickness($me,$mycards);
174 DB_set_hand_status_by_hash($me,'check');
180 echo "checking for solo...<br /> if you have a wedding or poverty you need to play a normal game,sorry...<br />".
181 " At the moment you need to reload this page to finish the setup.<br />";
182 if(!myisset("solo","wedding","poverty","nines") )
184 /* all these variables have a pre-selected default,
185 * so we should never get here,
186 * unless a user tries to cheat ;) */
187 echo "something went wrong...please contact the admin.";
191 /* check if this sickness needs to be handled first */
192 $gametype = DB_get_gametype_by_gameid($gameid);
193 $startplayer = DB_get_startplayer_by_gameid($gameid);
195 if( $_REQUEST["solo"]!="No")
197 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
198 DB_set_sickness_by_hash($me,"solo");
199 echo "Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.\n";
201 if($gametype == "solo" && $startplayer<$mypos)
205 DB_set_gametype_by_gameid($gameid,"solo");
206 DB_set_startplayer_by_gameid($gameid,$mypos);
207 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
210 else if($_REQUEST["wedding"] == "yes")
212 /* TODO: add silent solo somewhere*/
213 echo "wedding was chosen<br />\n";
214 DB_set_sickness_by_hash($me,"wedding");
216 else if($_REQUEST["poverty"] == "yes")
218 echo "poverty was chosen<br />\n";
219 DB_set_sickness_by_hash($me,"poverty");
221 else if($_REQUEST["nines"] == "yes")
223 echo "nines was chosen<br />\n";
224 DB_set_sickness_by_hash($me,"nines");
228 DB_set_hand_status_by_hash($me,'poverty');
230 /* check all players and set game to final result, e.g. solo, wedding, povert, redeal */
234 /* here we need to check if there is a solo or some other form of sickness.
235 * If so, which one counts
236 * set that one in the Game table, delete other ones form Hand table
237 * tell people about it.
239 echo "<br />poverty not handeled at the moment... you need to play a normal game<br />";
241 /* only set this after all poverty, etc. are handeled*/
242 DB_set_hand_status_by_hash($me,'play');
244 /* check if the game can start */
245 $userids = DB_get_all_userid_by_gameid($gameid);
247 foreach($userids as $user)
248 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='play')
253 /* TODO: check what kind of game we are playing */
254 DB_set_game_status_by_gameid($gameid,'play');
260 /* both entries here, so that the tricks are visible for both.
261 * in case of 'play' there is a break later that skips the last part
264 /* figure out what kind of game we are playing,
265 * set the global variables $TRUMP,$DIAMONDS,$HEARTS,$CLUBS,$SPADES
269 $gametype = DB_get_gametype_by_gameid($gameid);
271 if($gametype=="solo")
273 $gametype = DB_get_solo_by_gameid($gameid);
274 $GT = $gametype." ".$GT;
279 set_gametype($gametype);
282 /* display useful things in divs */
284 /* display local time */
285 echo "<div class=\"time\">\n Local times:<table>";
287 $users = DB_get_all_userid_by_gameid($gameid);
288 foreach($users as $user)
290 $offset = DB_get_user_timezone($user);
291 $zone = return_timezone($offset);
292 date_default_timezone_set($zone);
293 $name = DB_get_name_by_userid($user);
295 echo "<tr> <td>$name</td> <td>".date("Y-m-d H:i:s")."</td></tr>\n";
297 echo "</table>\n</div>\n";
301 /* display links to the users status page */
302 $result = mysql_query("SELECT email,password from User WHERE id='$myid'" );
303 $r = mysql_fetch_array($result,MYSQL_NUM);
304 output_link_to_user_page($r[0],$r[1]);
308 /* end display useful things*/
310 /* has the game started? No, then just wait here...*/
311 $gamestatus = DB_get_game_status_by_gameid($gameid);
312 if($gamestatus == 'pre')
314 echo "you need to wait for the others... <br />";
315 break; /* not sure this works... the idea is that you can
316 * only play a card after everyone is ready to play */
319 /* display the table and the names */
320 $result = mysql_query("SELECT User.fullname as name,".
321 " Hand.position as position ".
323 "LEFT JOIN User ON User.id=Hand.user_id ".
324 "WHERE Hand.game_id='".$gameid."' ".
325 "ORDER BY position ASC");
327 echo "<div class=\"table\">\n".
328 " <img src=\"pics/table.png\" alt=\"table\" />\n";
329 while($r = mysql_fetch_array($result,MYSQL_NUM))
334 echo " <span class=\"table".($pos-1)."\">$name</span>\n";
338 /* get everything relevant to display the tricks */
339 $result = mysql_query("SELECT Hand_Card.card_id as card,".
340 " Hand.position as position,".
341 " Play.sequence as sequence, ".
345 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
346 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
347 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
348 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
349 "WHERE Trick.game_id='".$gameid."' ".
350 "ORDER BY Trick.id,sequence ASC");
355 $lasttrick = DB_get_max_trickid($gameid);
357 $play = array(); /* needed to calculate winner later */
360 $firstcard = ""; /* first card in a trick */
362 echo "\n<ul class=\"tricks\">\n";
363 echo " <li> Hello $myname! History: </li>\n";
365 while($r = mysql_fetch_array($result,MYSQL_NUM))
372 /* save card to be able to find the winner of the trick later */
377 /* first card in a trick, output some html */
378 if($trick!=$lasttrick)
380 /* start of an old trick? */
381 echo " <li onclick=\"hl('$trickNR');\"><a href=\"#\">Trick $trickNR</a>\n".
382 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
383 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
385 else if($trick==$lasttrick)
387 /* start of a last trick? */
388 echo " <li onclick=\"hl('$trickNR');\"><a href=\"#\">Current Trick</a>\n".
389 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
390 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
393 /* remember first card, so that we are able to check, what cards can be played */
398 echo " <div class=\"card".($pos-1)."\">\n";
400 /* display comments */
402 echo " <span class=\"comment\">".$comment."</span>\n";
407 echo " </div>\n"; /* end div card */
413 echo " </div>\n </li>\n"; /* end div table, end li table */
417 if($seq!=4 && $trickNR>1)
418 echo " </div>\n </li>\n"; /* end div table, end li table */
422 /* whos turn is it? */
425 $winner = get_winner($play,$gametype); /* returns the position */
427 $firstcard = ""; /* new trick, no first card */
432 if($next==5) $next=1;
435 /* my turn?, display cards as links, ask for comments*/
436 if(DB_get_pos_by_hash($me) == $next)
441 /* do we want to play a card? */
442 if(myisset("card") && $myturn)
444 $card = $_REQUEST["card"];
445 $handid = DB_get_handid_by_hash($me);
447 /* check if we have card and that we haven't played it yet*/
448 /* set played in hand_card to true where hand_id and card_id*/
449 $result = mysql_query("SELECT id FROM Hand_Card WHERE played='false' and ".
450 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
451 $r = mysql_fetch_array($result,MYSQL_NUM);
456 mysql_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".DB_quote_smart($card));
458 /* get trick id or start new trick */
459 $a = DB_get_current_trickid($gameid);
463 $playid = DB_play_card($trickid,$handcardid,$sequence);
465 /*check for coment */
466 if(myisset("comment"))
468 DB_insert_comment($_REQUEST["comment"],$playid,$myid);
471 echo "<div class=\"card\">";
472 echo " you played <br />";
477 /*check if we still have cards left, else set status to gameover */
478 if(sizeof(DB_get_hand($me))==0)
480 DB_set_hand_status_by_hash($me,'gameover');
481 $mystatus='gameover';
484 /* if all players are done, set game status to game over */
485 $userids = DB_get_all_userid_by_gameid($gameid);
487 foreach($userids as $user)
488 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
492 DB_set_game_status_by_gameid($gameid,"gameover");
494 /* email next player */
495 if(DB_get_game_status_by_gameid($gameid)=='play')
499 $play = DB_get_cards_by_trick($trickid);
500 $winner = get_winner($play,$gametype); /* returns the position */
505 $next = DB_get_pos_by_hash($me)+1;
507 if($next==5) $next=1;
509 /* email next player */
510 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
511 $email = DB_get_email_by_hash($next_hash);
513 $message = "It's your turn now.\n".
514 "Use this link to play a card: ".$host."?me=".$next_hash."\n\n" ;
515 mymail($email,"[DoKo-debug] a card has been played",$message);
518 echo "DEBUG:<a href=\"index.php?me=".DB_get_hash_from_game_and_pos($gameid,$next).
519 "\"> next player </a> <br />\n";
525 echo "can't find that card?! <br />\n";
528 else if(myisset("card") && !$myturn )
530 echo "please wait until it's your turn! <br />\n";
533 $mycards = DB_get_hand($me);
535 echo "<div class=\"mycards\">\n";
537 if($myturn && !myisset("card"))
539 echo "Hello ".$myname.", it's your turn! <br />\n";
540 echo "Your cards are: <br />\n";
541 echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
543 /* do we have to follow suit? */
545 if(have_suit($mycards,$firstcard))
548 foreach($mycards as $card)
550 if($followsuit && !same_type($card,$firstcard))
553 display_link_card($card);
556 echo "<br />\nA short comments:<input name=\"comment\" type=\"text\" size=\"30\" maxlength=\"50\" />\n";
557 echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />\n";
558 echo "<input type=\"submit\" value=\"move\" />\n";
561 else if($mystatus=='play')
563 echo "Your cards are: <br />\n";
564 foreach($mycards as $card)
569 /* check if we need to set status to 'gameover' is done during playing of the card */
570 if($mystatus=='play')
572 /* the following happens only when the gamestatus is 'gameover' */
573 /* check if game is over, display results */
574 if(DB_get_game_status_by_gameid($gameid)=='play')
576 echo "the game is over for you.. other people still need to play though";
580 echo "the game is over now... guess the final score should be displayed here...<br />\n";
582 /* suggest a new game with the same people in it, just rotated once */
583 $names = DB_get_all_names_by_gameid($gameid);
584 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
588 echo "error in testing the status";
592 /* user status page */
593 else if(myisset("email","password"))
595 /* test id and password, should really be done in one step */
596 $email = $_REQUEST["email"];
597 $password = $_REQUEST["password"];
599 if(strlen($password)!=32)
600 $password = md5($password);
603 $uid = DB_get_userid_by_email_and_password($email,$password);
609 $time = DB_get_user_timestamp($uid);
610 $unixtime =strtotime($time);
612 $offset = DB_get_user_timezone($uid);
613 $zone = return_timezone($offset);
614 date_default_timezone_set($zone);
616 echo "last login: ".date("r",$unixtime)."<br />";
618 DB_update_user_timestamp($uid);
620 echo "<p>these are the games you are playing in:<br />\n";
621 $result = mysql_query("SELECT hash,game_id from Hand WHERE user_id='$uid' AND status<>'gameover'" );
622 while( $r = mysql_fetch_array($result,MYSQL_NUM))
623 echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a><br />";
626 $names = DB_get_all_names();
627 echo "<p>registered players:<br />\n";
628 foreach ($names as $name)
629 echo "$name <br />\n";
632 echo "<p>Want to start a new game? remember 4 names from the list above and visit ".
633 "<a href=\"".$host."?new\">this page.</a></p>";
637 echo "sorry email and password don't match <br />";
641 /* page for registration */
642 else if(myisset("register") )
646 /* new user wants to register */
647 else if(myisset("Rfullname","Remail","Rpassword","Rtimezone") )
650 if(DB_get_userid_by_name($_REQUEST["Rfullname"]))
652 echo "please chose another name<br />";
655 if(DB_get_userid_by_email($_REQUEST["Remail"]))
657 echo "this email address is already used ?!<br />";
662 $r=mysql_query("INSERT INTO User VALUES(NULL,".DB_quote_smart($_REQUEST["Rfullname"]).
663 ",".DB_quote_smart($_REQUEST["Remail"]).
664 ",".DB_quote_smart(md5($_REQUEST["Rpassword"])).
665 ",".DB_quote_smart($_REQUEST["Rtimezone"]).",NULL)");
668 echo " added you to the database";
670 echo " something went wrong";
673 /* default login page */