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 */
11 /***************** M A I N **************************/
15 /* check if we want to start a new game */
16 if(isset($_REQUEST["new"]))
17 output_form_for_new_game();
19 /*check if everything is ready to set up a new game */
20 else if( isset($_REQUEST["PlayerA"]) &&
21 isset($_REQUEST["PlayerB"]) &&
22 isset($_REQUEST["PlayerC"]) &&
23 isset($_REQUEST["PlayerD"]) )
25 $PlayerA = $_REQUEST["PlayerA"];
26 $PlayerB = $_REQUEST["PlayerB"];
27 $PlayerC = $_REQUEST["PlayerC"];
28 $PlayerD = $_REQUEST["PlayerD"];
30 $EmailA = DB_get_email_by_name($PlayerA);
31 $EmailB = DB_get_email_by_name($PlayerB);
32 $EmailC = DB_get_email_by_name($PlayerC);
33 $EmailD = DB_get_email_by_name($PlayerD);
35 if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
37 echo "couldn't find one of the names, please start a new game";
41 $useridA = DB_get_userid_by_name($PlayerA);
42 $useridB = DB_get_userid_by_name($PlayerB);
43 $useridC = DB_get_userid_by_name($PlayerC);
44 $useridD = DB_get_userid_by_name($PlayerD);
46 /* create random numbers */
47 $randomNR = create_array_of_random_numbers();
48 $randomNRstring = join(":",$randomNR);
52 if(isset($_REQUEST["followup"]))
54 $followup= $_REQUEST["followup"];
55 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', NULL, NULL,'pre','$followup' ,NULL)");
58 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', NULL, NULL,'pre', NULL ,NULL)");
59 $game_id = mysql_insert_id();
62 $hashA = md5("AGameOfDoko".$game_id.$PlayerA.$EmailA);
63 $hashB = md5("AGameOfDoko".$game_id.$PlayerB.$EmailB);
64 $hashC = md5("AGameOfDoko".$game_id.$PlayerC.$EmailC);
65 $hashD = md5("AGameOfDoko".$game_id.$PlayerD.$EmailD);
68 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridA).
69 ", ".DB_quote_smart($hashA).", 'start','1',NULL,NULL,NULL,'false','false',NULL)");
70 $hand_idA = mysql_insert_id();
71 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridB).
72 ", ".DB_quote_smart($hashB).", 'start','2',NULL,NULL,NULL,'false','false',NULL)");
73 $hand_idB = mysql_insert_id();
74 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridC).
75 ", ".DB_quote_smart($hashC).", 'start','3',NULL,NULL,NULL,'false','false',NULL)");
76 $hand_idC = mysql_insert_id();
77 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridD).
78 ", ".DB_quote_smart($hashD).", 'start','4',NULL,NULL,NULL,'false','false',NULL)");
79 $hand_idD = mysql_insert_id();
83 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idA', '".$randomNR[$i]."', 'false')");
85 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idB', '".$randomNR[$i]."', 'false')");
87 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idC', '".$randomNR[$i]."', 'false')");
89 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idD', '".$randomNR[$i]."', 'false')");
91 /* send out email, TODO: check for error with email */
93 "you are invited to play a game of DoKo (that is to debug the program ;).\n".
94 "Place comments and bug reports here:\n".
95 "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
96 "The whole round would consist of the following players:\n".
101 "If you want to join this game, please follow this link:\n\n".
104 mymail($EmailA,"You are invited to a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
105 mymail($EmailB,"You are invited to a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
106 mymail($EmailC,"You are invited to a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
107 mymail($EmailD,"You are invited to a game of DoKo","Hello $PlayerD,\n".$message.$hashD);
110 /* end set up a new game */
112 else if(isset($_REQUEST["me"]))
114 /* handle request from one specifig player,
115 * the hash is set on a per game base, so first just handle this game
116 * perhaps also show links to other games in a sidebar
119 $me = $_REQUEST["me"];
121 /* test for valid ID */
122 $myid = DB_get_userid_by_hash($me);
125 echo "Can't find you in the database, please check the url.<br />\n";
126 echo "perhaps the game has been cancled.";
130 DB_update_user_timestamp($myid);
132 /* get some information from the DB */
133 $gameid = DB_get_gameid_by_hash($me);
134 $myname = DB_get_name_by_hash($me);
135 $mystatus = DB_get_status_by_hash($me);
140 check_want_to_play($me);
141 DB_set_hand_status_by_hash($me,'init');
144 if( !isset($_REQUEST["in"]) || !isset($_REQUEST["update"]))
146 DB_set_hand_status_by_hash($me,'start');
147 echo "you need to answer both question";
151 if($_REQUEST["in"] == "no")
153 echo "TODO: email everyone that the game has been canceled.<br />";
154 /*something like need to modify for DB backend
157 $message = "Hello ".$player[$hash[$i]]["name"].",\n\n".
158 "the game has been canceled due to the request of one of the players.\n";
159 mymail($player[$hash[$i]]["email"],"[DoKo-Debug] the game has been canceled",$message);
162 /* delete everything from the dB */
167 echo "thanks for joining the game... please scroll down";
168 echo "TODO: make this page nicer<br />";
169 echo "TODO: set card pref<br />";
171 $mycards = DB_get_hand($me);
173 echo "<p class=\"mycards\">your cards are: <br />\n";
174 foreach($mycards as $card)
178 check_for_sickness($me,$mycards);
180 DB_set_hand_status_by_hash($me,'check');
186 echo "no checking at the moment... you need to play a normal game. At the moment you need to reload this page to finish the setup.";
187 if(!isset($_REQUEST["solo"]) ||
188 !isset($_REQUEST["wedding"]) ||
189 !isset($_REQUEST["poverty"]) ||
190 !isset($_REQUEST["nines"]) )
192 DB_set_hand_status_by_hash($me,'init');
193 /* problem: by setting it back to init, variables "in" and "update" are
194 * not set, so the player will be send back to the start, after seeing his hand
196 echo "you need to fill out the form";
200 if( $_REQUEST["solo"]!="No")
202 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
203 DB_set_sickness_by_hash($me,"solo");
205 else if($_REQUEST["wedding"] == "yes")
207 echo "wedding was chosen<br />\n";
208 DB_set_sickness_by_hash($me,"wedding");
210 else if($_REQUEST["poverty"] == "yes")
212 echo "poverty was chosen<br />\n";
213 DB_set_sickness_by_hash($me,"poverty");
215 else if($_REQUEST["nines"] == "yes")
217 echo "nines was chosen<br />\n";
218 DB_set_sickness_by_hash($me,"nines");
221 DB_set_hand_status_by_hash($me,'poverty');
223 /* check all players and set game to final result, e.g. solo, wedding, povert, redeal */
225 /* reset solo, etc from players who did say something, but it didn't matter? */
228 echo "<br />poverty not handeled at the moment... you need to play a normal game<br />";
230 /* only set this after all poverty, etc. are handeled*/
231 DB_set_hand_status_by_hash($me,'play');
233 /* check if the game can start */
234 $userids = DB_get_all_userid_by_gameid($gameid);
236 foreach($userids as $user)
237 if(DB_get_hand_status_by_userid($user)!='play')
241 DB_set_game_status_by_gameid($gameid,'play');
245 case 'gameover': /*both entries here, so that the tricks are visible for both in case of 'play' there is a break later that skips the last part*/
246 /* display local time */
247 echo "<div class=\"time\">\n Local times:<table>";
249 $users = DB_get_all_userid_by_gameid($gameid);
250 foreach($users as $user)
252 $offset = DB_get_user_timezone($user);
253 $zone = return_timezone($offset);
254 date_default_timezone_set($zone);
255 $name = DB_get_name_by_userid($user);
257 echo "<tr> <td>$name</td> <td>".date("Y-m-d H:i:s")."</td></tr>\n";
259 echo "</table>\n</div>\n";
262 /* display links to other games */
263 echo "<div class=\"over\">\n";
264 $result = mysql_query("SELECT email,password from User WHERE id='$myid'" );
265 $r = mysql_fetch_array($result,MYSQL_NUM);
266 echo "<form action=\"index.php\" method=\"post\">\n";
267 echo " <input type=\"hidden\" name=\"email\" value=\"".$r[0]."\" />\n";
268 echo " <input type=\"hidden\" name=\"password\" value=\"".$r[1]."\" />\n";
269 echo " <input type=\"submit\" value=\"go to my user page\" />\n";
275 $gamestatus = DB_get_game_status_by_gameid($gameid);
276 if($gamestatus == 'pre')
278 echo "you need to wait for the others... <br />";
281 /* get everythin relevant to display the tricks */
282 $result = mysql_query("SELECT Hand_Card.card_id as card,".
283 " User.fullname as name,".
284 " Hand.position as position,".
285 " Play.sequence as sequence, ".
286 " Hand.hash as hash, ".
290 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
291 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
292 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
293 "LEFT JOIN User ON User.id=Hand.user_id ".
294 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
295 "WHERE Trick.game_id='".$gameid."' ".
296 "ORDER BY Trick.id,sequence ASC");
301 $lasttrick = DB_get_max_trickid($gameid);
303 $play = array(); /* needed to calculate winner later */
307 echo "\n<ul class=\"oldtrick\">\n";
308 echo " <li> Hello $myname! History: </li>\n";
310 while($r = mysql_fetch_array($result,MYSQL_NUM))
317 if($trick!=$lasttrick && $seq==1)
319 /* start of an old trick? */
320 echo " <li onclick=\"hl('$trickNR');\"><a href=\"#\">Trick $trickNR</a>\n".
321 " <div class=\"table\" id=\"trick".$trickNR."\">\n".
322 " <img class=\"table\" src=\"pics/table".($pos-1).".png\" alt=\"table\" />\n";
324 else if($trick==$lasttrick && $seq==1)
326 /* start of a last trick? */
327 echo " <li onclick=\"hl('$trickNR');\"><a href=\"#\">Current Trick</a>\n".
328 " <div class=\"table\" id=\"trick".$trickNR."\">\n".
329 " <img class=\"table\" src=\"pics/table".($pos-1).".png\" alt=\"table\" />\n";
333 echo " <div class=\"card".($pos-1)."\">\n";
338 echo " <span class=\"comment\">";
345 /* check for comment */
347 echo "<span>".$comment."</span>";
352 echo " </div>\n"; /* end div card */
358 echo " </div>\n </li>\n"; /* end div table, end li table */
362 if($seq!=4 && $trickNR>1)
363 echo " </div>\n </li>\n"; /* end div table, end li table */
367 /* whos turn is it? */
370 $winner = get_winner($play); /* returns the position */
377 if($next==5) $next=1;
379 /* my turn?, display cards as links, ask for comments*/
380 if(DB_get_pos_by_hash($me) == $next)
385 /* do we want to play a card? */
386 if(isset($_REQUEST["card"]) && $myturn)
388 $card = $_REQUEST["card"];
389 $handid = DB_get_handid_by_hash($me);
391 /* check if we have card and that we haven't played it yet*/
392 /* set played in hand_card to true where hand_id and card_id*/
393 $result = mysql_query("SELECT id FROM Hand_Card WHERE played='false' and ".
394 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
395 $r = mysql_fetch_array($result,MYSQL_NUM);
400 mysql_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".DB_quote_smart($card));
402 /* get trick id or start new trick */
403 $a = DB_get_current_trickid($gameid);
407 $playid = DB_play_card($trickid,$handcardid,$sequence);
409 /*check for coment */
410 if(isset($_REQUEST["comment"]))
412 DB_insert_comment($_REQUEST["comment"],$playid,$myid);
415 echo "<div class=\"card\">";
416 echo " you played <br />";
421 /*check if we still have cards left, else set status to gameover */
422 if(sizeof(DB_get_hand($me))==0)
424 DB_set_hand_status_by_hash($me,'gameover');
425 $mystatus='gameover';
428 /* if all players are done, set game status also to game over */
429 $userids = DB_get_all_userid_by_gameid($gameid);
431 foreach($userids as $user)
432 if(DB_get_hand_status_by_userid($user)!='gameover')
436 DB_set_game_status_by_gameid($gameid,"gameover");
438 /* email next player */
439 if(DB_get_game_status_by_gameid($gameid)=='play')
443 $play = DB_get_cards_by_trick($trickid);
444 $winner = get_winner($play); /* returns the position */
449 $next = DB_get_pos_by_hash($me)+1;
451 if($next==5) $next=1;
453 /* email next player */
454 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
455 $email = DB_get_email_by_hash($next_hash);
457 $message = "It's your turn now.\n".
458 "Use this link to play a card: ".$host."?me=".$next_hash."\n\n" ;
459 mymail($email,"[DoKo-debug] a card has been played",$message);
462 echo "DEBUG:<a href=\"index.php?me=".DB_get_hash_from_game_and_pos($gameid,$next).
463 "\"> next player </a> <br />\n";
469 echo "couldn't find card <br />\n";
472 else if(isset($_REQUEST["card"]) && !$myturn )
474 echo "please wait until it is your turn! <br />\n";
477 $mycards = DB_get_hand($me);
479 echo "<div class=\"mycards\">\n";
481 if($myturn && !isset($_REQUEST["card"]))
483 echo "Hello ".$myname.", it's your turn! <br />\n";
484 echo "Your cards are: <br />\n";
485 echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
486 foreach($mycards as $card)
487 display_link_card($card);
489 <br />A short comments:<input name="comment" type="text" size="30" maxlength="50" />
490 <input type="hidden" name="me" value="<?php echo $me; ?>" />
491 <input type="submit" value="move" />
495 else if($mystatus=='play')
497 echo "Your cards are: <br />\n";
498 foreach($mycards as $card)
503 /* check if we need to set status to 'gameover' is done during playing of the card */
504 if($mystatus=='play')
506 /* the following happens only when the gamestatus is 'gameover' */
507 /* check if game is over, display results */
508 if(DB_get_game_status_by_gameid($gameid)=='play')
510 echo "the game is over for you.. other people still need to play though";
514 echo "the game is over now... guess the final score should be displayed here...<br />\n";
516 /* suggest a new game with the same people in it, just rotated once */
517 $names = DB_get_all_names_by_gameid($gameid);
519 echo "Do you want to continue playing?(This will start a new game, with the next person as dealer.)\n";
520 echo "<form action=\"index.php\" method=\"post\">\n";
521 echo " <input type=\"hidden\" name=\"PlayerA\" value=\"".($names[1])."\" />\n";
522 echo " <input type=\"hidden\" name=\"PlayerB\" value=\"".($names[2])."\" />\n";
523 echo " <input type=\"hidden\" name=\"PlayerC\" value=\"".($names[3])."\" />\n";
524 echo " <input type=\"hidden\" name=\"PlayerD\" value=\"".($names[0])."\" />\n";
525 echo " <input type=\"hidden\" name=\"followup\" value=\"".($gameid)."\" />\n";
526 echo " <input type=\"submit\" value=\"keep playing\" />\n";
531 echo "error in testing the status";
535 /* user status page */
536 else if(isset($_REQUEST["email"]) && isset($_REQUEST["password"]))
538 /* test id and password, should really be done in one step */
539 $email = $_REQUEST["email"];
540 $password = $_REQUEST["password"];
542 if(strlen($password)!=32)
543 $password = md5($password);
546 $uid = DB_get_userid_by_email_and_password($email,$password);
552 $time = DB_get_user_timestamp($uid);
553 $unixtime =strtotime($time);
555 $offset = DB_get_user_timezone($uid);
556 $zone = return_timezone($offset);
557 date_default_timezone_set($zone);
559 echo "last login: ".date("r",$unixtime)."<br />";
561 DB_update_user_timestamp($uid);
563 echo "<p>these are the games you are playing in:<br />\n";
564 $result = mysql_query("SELECT hash,game_id from Hand WHERE user_id='$uid' AND status<>'gameover'" );
565 while( $r = mysql_fetch_array($result,MYSQL_NUM))
566 echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a><br />";
569 $names = DB_get_all_names();
570 echo "<p>registered players:<br />\n";
571 foreach ($names as $name)
572 echo "$name <br />\n";
574 echo "<p>Want to start a new game? remember 4 names from the list above and visit ".
575 "<a href=\"".$host."?new\">this page.</a></p>";
579 echo "sorry email and password don't match <br />";
583 /* page for registration */
584 else if(isset($_REQUEST["register"]) )
586 echo "IMPORTANT: passwords are going over the net as clear text, so pick an easy password. No need to pick anything complicated here ;)<br /><br />";
587 echo "TODO: convert timezone into a menu<br />\n";
588 echo "TODO: figure out a way to handle passwrods <br />\n";
590 <form action="index.php" method="post">
592 <legend>Register</legend>
595 <td><label for="Rfullname">Full name:</label></td>
596 <td><input type="text" id="Rfullname" name="Rfullname" size="20" maxsize="30" /> </td>
598 <td><label for="Remail">Email:</label></td>
599 <td><input type="text" id="Remail" name="Remail" size="20" maxsize="30" /></td>
601 <td><label for="Rpassword">Password(will be displayed in cleartext on the next page):</label></td>
602 <td><input type="password" id="Rpassword" name="Rpassword" size="20" maxsize="30" /></td>
604 <td><label for="Rtimezone">Timezone:</label></td>
606 <input type="text" id="Rtimezone" name="Rtimezone" size="4" maxsize="4" value="+1" />
609 <td colspan="2"> <input type="submit" value="register" /></td>
615 /* new user wants to register */
616 else if(isset($_REQUEST["Rfullname"]) &&
617 isset($_REQUEST["Remail"] ) &&
618 isset($_REQUEST["Rpassword"]) &&
619 isset($_REQUEST["Rtimezone"]) )
622 if(DB_get_userid_by_name($_REQUEST["Rfullname"]))
624 echo "please chose another name<br />";
627 if(DB_get_userid_by_email($_REQUEST["Remail"]))
629 echo "this email address is already used ?!<br />";
634 $r=mysql_query("INSERT INTO User VALUES(NULL,".DB_quote_smart($_REQUEST["Rfullname"]).
635 ",".DB_quote_smart($_REQUEST["Remail"]).
636 ",".DB_quote_smart(md5($_REQUEST["Rpassword"])).
637 ",".DB_quote_smart($_REQUEST["Rtimezone"]).",NULL)");
640 echo " added you to the database";
642 echo " something went wrong";
645 /* default login page */
647 { /* no new game, not in a game */