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 */
12 /* check if some variables are set in the config file, else set defaults */
13 if(!isset($EmailName))
17 /* in case work has to be done on the database or other section we can
18 * shut down the server and tell people to come back later
23 echo "Working on the database...please check back in a few mintues";
31 /* check if we want to start a new game */
34 $names = DB_get_all_names();
35 output_form_for_new_game($names);
37 /*check if everything is ready to set up a new game */
38 else if( myisset("PlayerA", "PlayerB","PlayerC","PlayerD","dullen","schweinchen" ))
40 $PlayerA = $_REQUEST["PlayerA"];
41 $PlayerB = $_REQUEST["PlayerB"];
42 $PlayerC = $_REQUEST["PlayerC"];
43 $PlayerD = $_REQUEST["PlayerD"];
45 $dullen = $_REQUEST["dullen"];
46 $schweinchen = $_REQUEST["schweinchen"];
48 $EmailA = DB_get_email_by_name($PlayerA);
49 $EmailB = DB_get_email_by_name($PlayerB);
50 $EmailC = DB_get_email_by_name($PlayerC);
51 $EmailD = DB_get_email_by_name($PlayerD);
53 if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
55 echo "couldn't find one of the names, please start a new game";
60 $useridA = DB_get_userid_by_name($PlayerA);
61 $useridB = DB_get_userid_by_name($PlayerB);
62 $useridC = DB_get_userid_by_name($PlayerC);
63 $useridD = DB_get_userid_by_name($PlayerD);
65 /* create random numbers */
66 $randomNR = create_array_of_random_numbers();
67 $randomNRstring = join(":",$randomNR);
69 /* get ruleset information or create new one */
70 $ruleset = DB_get_ruleset($dullen,$schweinchen);
73 echo "Error defining ruleset: $ruleset";
80 if(myisset("followup") )
82 $followup= $_REQUEST["followup"];
83 $session = DB_get_session_by_gameid($followup);
84 $ruleset = DB_get_ruleset_by_gameid($followup); /* just copy ruleset from old game,
85 this way no manipulation is possible */
87 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,'1','pre',".
88 "'$ruleset','$session' ,NULL)");
92 $max = DB_get_max_session();
94 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,'1','pre',".
95 "'$ruleset','$max' ,NULL)");
96 mysql_query("UPDATE Game SET session='".$max."' WHERE id=".DB_quote_smart($followup));
100 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,'1','pre', ".
101 "'$ruleset',NULL ,NULL)");
102 $game_id = mysql_insert_id();
105 $hashA = md5("AGameOfDoko".$game_id.$PlayerA.$EmailA);
106 $hashB = md5("AGameOfDoko".$game_id.$PlayerB.$EmailB);
107 $hashC = md5("AGameOfDoko".$game_id.$PlayerC.$EmailC);
108 $hashD = md5("AGameOfDoko".$game_id.$PlayerD.$EmailD);
111 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridA).
112 ", ".DB_quote_smart($hashA).", 'start','1',NULL,NULL,NULL,'false','false',NULL)");
113 $hand_idA = mysql_insert_id();
114 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridB).
115 ", ".DB_quote_smart($hashB).", 'start','2',NULL,NULL,NULL,'false','false',NULL)");
116 $hand_idB = mysql_insert_id();
117 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridC).
118 ", ".DB_quote_smart($hashC).", 'start','3',NULL,NULL,NULL,'false','false',NULL)");
119 $hand_idC = mysql_insert_id();
120 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridD).
121 ", ".DB_quote_smart($hashD).", 'start','4',NULL,NULL,NULL,'false','false',NULL)");
122 $hand_idD = mysql_insert_id();
126 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idA', '".$randomNR[$i]."', 'false')");
127 for($i=12;$i<24;$i++)
128 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idB', '".$randomNR[$i]."', 'false')");
129 for($i=24;$i<36;$i++)
130 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idC', '".$randomNR[$i]."', 'false')");
131 for($i=36;$i<48;$i++)
132 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idD', '".$randomNR[$i]."', 'false')");
134 /* send out email, TODO: check for error with email */
136 "you are invited to play a game of DoKo (that is to debug the program ;).\n".
137 "Place comments and bug reports here:\n".
138 "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
139 "The whole round would consist of the following players:\n".
144 "If you want to join this game, please follow this link:\n\n".
147 mymail($EmailA,"You are invited to a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
148 mymail($EmailB,"You are invited to a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
149 mymail($EmailC,"You are invited to a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
150 mymail($EmailD,"You are invited to a game of DoKo","Hello $PlayerD,\n".$message.$hashD);
152 echo "You started a new game. The emails have been sent out!";
153 } /* end set up a new game */
154 /* cancle a game, if nothing has happend in the last N minutes */
155 else if(myisset("cancle","me"))
157 $me = $_REQUEST["me"];
159 /* test for valid ID */
160 $myid = DB_get_userid_by_hash($me);
163 echo "Can't find you in the database, please check the url.<br />\n";
164 echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
169 DB_update_user_timestamp($myid);
171 /* get some information from the DB */
172 $gameid = DB_get_gameid_by_hash($me);
173 $myname = DB_get_name_by_hash($me);
175 /* check if game really is old enough */
176 $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
177 $r = mysql_fetch_array($result,MYSQL_NUM);
178 if(time()-strtotime($r[0]) > 60*60*24*30) /* = 1 month */
180 $message = "Hello, \n\n".
181 "Game $gameid has been cancled since nothing happend for a while and $myname requested it.\n";
183 $userids = DB_get_all_userid_by_gameid($gameid);
184 foreach($userids as $user)
186 $To = DB_get_email_by_userid($user);
187 mymail($To,$EmailName."game cancled (timed out)",$message);
190 /* delete everything from the dB */
193 echo "<p style=\"background-color:red\";>Game $gameid has been cancled.<br /><br /></p>";
196 echo "<p>You need to wait longer before you can cancle a game...</p>\n";
198 /* handle request from one specific player for one game,
199 * (the hash is set on a per game base) */
200 else if(myisset("me"))
202 $me = $_REQUEST["me"];
204 /* test for valid ID */
205 $myid = DB_get_userid_by_hash($me);
208 echo "Can't find you in the database, please check the url.<br />\n";
209 echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
214 /* the user had done something, update the timestamp */
215 DB_update_user_timestamp($myid);
217 /* get some information from the DB */
218 $gameid = DB_get_gameid_by_hash($me);
219 $myname = DB_get_name_by_hash($me);
220 $mystatus = DB_get_status_by_hash($me);
221 $mypos = DB_get_pos_by_hash($me);
224 /* get rule set for this game */
225 $result = mysql_query("SELECT * FROM Rulesets".
226 " LEFT JOIN Game ON Game.ruleset=Rulesets.id ".
227 " WHERE Game.id='$gameid'" );
228 $r = mysql_fetch_array($result,MYSQL_NUM);
230 $RULES["dullen"] = $r[2];
231 $RULES["schweinchen"] = $r[3];
233 /* get some infos about the game */
234 $gametype = DB_get_gametype_by_gameid($gameid);
235 $gamestatus = DB_get_game_status_by_gameid($gameid);
237 if($gametype=="solo")
239 $gametype = DB_get_solo_by_gameid($gameid);
240 $GT = $gametype." ".$GT;
243 /* display rule set for this game */
244 echo "<div class=\"ruleset\">\n";
246 if($gamestatus != 'pre')
247 echo " Gametype: $GT <br />\n";
249 echo "Rules: <br />\n";
250 echo "10ofhearts : ".$r[2]."<br />\n";
251 echo "schweinchen: ".$r[3]."<br />\n";
254 /* does anyone have both foxes */
255 $GAME["schweinchen"]=0;
258 $hash = DB_get_hash_from_game_and_pos($gameid,$i);
259 $cards = DB_get_all_hand($hash);
260 if( in_array("19",$cards) && in_array("20",$cards) )
262 $GAME["schweinchen"]=1;
263 $GAME["schweinchen-who"]=$hash;
267 /* mystatus gets the player through the different stages of a game.
269 * init: check values from start,
271 * check: check for return values from init
272 * poverty: handle poverty, wait here until all player have reached this state
273 * display sickness and move on to game
274 * play: game in progress
275 * gameover: are we revisiting a game
280 check_want_to_play($me);
281 /* move on to the next stage*/
282 DB_set_hand_status_by_hash($me,'init');
285 /* first check if everything went ok in the last step
286 * if not, send user back, if yes, check what he did
290 echo "you need to answer the <a href=\"$host?me=$me\">question</a>.";
291 DB_set_hand_status_by_hash($me,'start');
295 if($_REQUEST["in"] == "no")
297 /* cancle the game */
298 $message = "Hello, \n\n".
299 "the game has been canceled due to the request of one of the players.\n";
301 $userids = DB_get_all_userid_by_gameid($gameid);
302 foreach($userids as $user)
304 $To = DB_get_email_by_userid($user);
305 mymail($To,$EmailName."game canceled",$message);
308 /* delete everything from the dB */
313 echo "thanks for joining the game...";
315 $mycards = DB_get_hand($me);
317 echo "<p class=\"mycards\" style=\"margin-top:8em;\">your cards are: <br />\n";
318 foreach($mycards as $card)
322 check_for_sickness($me,$mycards);
324 /* move on to the next stage*/
325 DB_set_hand_status_by_hash($me,'check');
331 /* ok, user is in the game, saw his cards and selected his vorbehalt
332 * so first we check what he selected
334 echo "Processing what you selected in the last step...<br />";
336 if(!myisset("solo","wedding","poverty","nines") )
338 /* all these variables have a pre-selected default,
339 * so we should never get here,
340 * unless a user tries to cheat ;) */
341 echo "something went wrong...please contact the admin.";
345 /* check if this sickness needs to be handled first */
346 $gametype = DB_get_gametype_by_gameid($gameid);
347 $startplayer = DB_get_startplayer_by_gameid($gameid);
349 if( $_REQUEST["solo"]!="No")
351 /* user wants to play a solo */
353 /* store the info in the user's hand info */
354 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
355 DB_set_sickness_by_hash($me,"solo");
357 echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
359 if($gametype == "solo" && $startplayer<$mypos)
360 {}/* do nothing, since someone else already is playing solo */
363 /* this solo comes first
364 * store info in game table
366 DB_set_gametype_by_gameid($gameid,"solo");
367 DB_set_startplayer_by_gameid($gameid,$mypos);
368 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
371 else if($_REQUEST["wedding"] == "yes")
373 /* TODO: add silent solo somewhere*/
374 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
375 DB_set_sickness_by_hash($me,"wedding");
377 else if($_REQUEST["poverty"] == "yes")
379 echo "So you got poverty. You might as well have said nothing, since this is not implemented yet,".
380 " you need to play a normal game...to make it a bit harder, I'll tell the other people that".
381 " you only have a few trump... should make the game more interesting (although perhaps not for you:))<br />\n";
382 DB_set_sickness_by_hash($me,"poverty");
384 else if($_REQUEST["nines"] == "yes")
386 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
387 " is playing solo, this game will be canceled.<br />\n";
388 DB_set_sickness_by_hash($me,"nines");
392 echo " Ok, done with checking, please go to the <a href=\"$host?me=$me\">next step of the setup</a>.<br />";
394 /* move on to the next stage*/
395 DB_set_hand_status_by_hash($me,'poverty');
399 /* here we need to check if there is a solo or some other form of sickness.
400 * If so, which one is the most important one
401 * set that one in the Game table
402 * tell people about it.
404 echo "<br /> Checking if someone else selected solo, nines or wedding... Poverty not handled at the moment<br />";
406 /* check if everyone has reached this stage */
407 $userids = DB_get_all_userid_by_gameid($gameid);
409 foreach($userids as $user)
411 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
412 if($userstat!='poverty' && $userstat!='play')
418 echo "This step can only be handled after everyone finished the last step. ".
419 "Seems like this is not the case, so you need to wait a bit... please check back later....<br />";
423 echo "Everyone has finished checking their cards, let's see what they said...<br />";
425 /* check what kind of game we are playing, in case there are any solos this already
426 *will have the correct information in it */
427 $gametype = DB_get_gametype_by_gameid($gameid);
428 $startplayer = DB_get_startplayer_by_gameid($gameid);
430 /* check for different sickness and just output a general info */
432 /* check for nines */
434 foreach($userids as $user)
435 if(DB_get_sickness_by_userid_and_gameid($user,$gameid) == 'nines')
438 $name = DB_get_name_by_userid($user);
439 echo "$name has a Vorbehalt. <br />";
443 /* check players for poverty */
445 foreach($userids as $user)
447 if(DB_get_sickness_by_userid_and_gameid($user,$gameid) == 'poverty')
450 $name = DB_get_name_by_userid($user);
451 echo "$name has a Vorbehalt. <br />";
455 /* check players for wedding */
457 foreach($userids as $user)
459 if(DB_get_sickness_by_userid_and_gameid($user,$gameid) == 'wedding')
462 $name = DB_get_name_by_userid($user);
463 echo "$name has a Vorbehalt. <br />" ;
467 /* check for solo, output vorbehalt */
469 foreach($userids as $user)
471 if(DB_get_sickness_by_userid_and_gameid($user,$gameid) == 'solo')
474 $name = DB_get_name_by_userid($user);
475 echo "$name has a Vorbehalt. <br />" ;
479 /* now check which sickness comes first and set the gametype to it */
481 /* gamestatus == normal, => cancel game */
482 if($gametype == "solo")
489 /* TODO: should we keep statistics of this? */
490 $message = "Hello, \n\n".
491 "the game has been canceled because ".DB_get_name_by_userid($nines).
492 " has five or more nines and nobody is playing solo.\n";
494 /* TODO: add info about redeal in case this is a game of a series */
496 $userids = DB_get_all_userid_by_gameid($gameid);
497 foreach($userids as $user)
499 $To = DB_get_email_by_userid($user);
500 mymail($To,$EmailName."game canceled",$message);
503 /* delete everything from the dB */
506 echo "The game has been canceled because ".DB_get_name_by_userid($nines).
507 " has five or more nines and nobody is playing solo.\n";
511 else if($poverty==1 && $gametype !="poverty")
513 DB_set_gametype_by_gameid($gameid,"poverty");
514 $gametype = "poverty";
516 else if($poverty==2 && $gametype !="dpoverty")
518 DB_set_gametype_by_gameid($gameid,"dpoverty");
519 $gametype = "dpoverty";
521 else if($wedding> 0 && $gametype !="wedding")
523 DB_set_gametype_by_gameid($gameid,"wedding");
524 $gametype = "wedding";
529 /* now the gametype is set correctly (shouldn't matter that this is calculated for every user)
530 * output what kind of game we have */
533 foreach($userids as $user)
535 /* userids are sorted by position...
536 * so output whatever the firstone has, then whatever the next one has
537 * stop when the sickness is the same as the gametype
540 $name = DB_get_name_by_userid($user);
541 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
544 echo "$name has $usersick <br />"; /*TODO: perhaps save this in a string and store in Game? */
546 if($usersick=="poverty")
548 if($usersick == "wedding" && $gametype =="wedding")
550 if($usersick == "poverty" && $gametype =="poverty")
552 if($usersick == "poverty" && $gametype =="dpoverty" && $poverty==2)
554 if($usersick == "solo" && $gametype =="solo")
558 /* output Schweinchen in case the rules need it */
559 if( $gametype != "solo")
560 if($GAME["schweinchen"] && $RULES["schweinchen"]=="both" )
561 echo DB_get_name_by_hash($GAME["schweinchen-who"])." has Schweinchen. <br />";
565 /* finished the setup, set re/contra parties if possible, go to next stage unless there is a case of poverty*/
569 /* are we the solo player? set us to re, else set us to contra */
570 $pos = DB_get_pos_by_hash($me);
571 if($pos == $startplayer)
572 DB_set_party_by_hash($me,"re");
574 DB_set_party_by_hash($me,"contra");
575 DB_set_hand_status_by_hash($me,'play');
579 /* set person with the wedding to re, do the rest during the game */
580 $usersick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
581 if($usersick == "wedding")
582 DB_set_party_by_hash($me,"re");
584 echo "Don't know who will be Re and Contra, you need to ".
585 "figure that out at the end of the game yourself <br />\n";
586 DB_set_hand_status_by_hash($me,'play');
590 $hand = DB_get_all_hand($me);
592 if(in_array('3',$hand)||in_array('4',$hand))
593 DB_set_party_by_hash($me,"re");
595 DB_set_party_by_hash($me,"contra");
596 DB_set_hand_status_by_hash($me,'play');
599 /* use extra column in DB.Game to store whom to ask,
600 * should be set for poverty and dpoverty(use two digits for dpoverty?) earlier*/
602 /* check if poverty resolved (e.g. DB.Game who set to NULL)
603 * yes? =>trump was taken, start game; break;
605 * check if we are being asked now
606 * no, display wait message, e.g. player X is asked at the moment
607 * yes, display number of trump and user's hand, ask if he wants to take it
608 * no, set whom-to-ask to next player, email next player, cancle game if no next player
609 * yes -> link to new page:display all cards, ask for N return cards
614 echo "TODO: handle poverty here (almost done in my developing version)";
615 DB_set_hand_status_by_hash($me,'play');
619 /* check if all players are ready to play */
621 foreach($userids as $user)
622 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='play')
627 /* only set this after all poverty, etc. are handled*/
628 DB_set_game_status_by_gameid($gameid,'play');
630 /* email startplayer */
631 $startplayer = DB_get_startplayer_by_gameid($gameid);
632 $email = DB_get_email_by_pos_and_gameid($startplayer,$gameid);
633 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
637 /* email startplayer) */
638 $message = "It's your turn now.\n".
639 "Use this link to play a card: ".$host."?me=".$hash."\n\n" ;
640 mymail($email,$EmailName."ready, set, go... ",$message);
643 echo " Please, <a href=\"$host?me=$me\">start</a> the game.<br />";
646 echo "You finished the setup, once everyone else has done the same you'll get an email when it is your turn..<br />";
651 /* both entries here, so that the tricks are visible for both.
652 * in case of 'play' there is a break later that skips the last part
655 /* figure out what kind of game we are playing,
656 * set the global variables $CARDS["trump"],$CARDS["diamonds"],$CARDS["hearts"],
657 * $CARDS["clubs"],$CARDS["spades"],$CARDS["foxes"]
661 $gametype = DB_get_gametype_by_gameid($gameid);
663 if($gametype=="solo")
665 $gametype = DB_get_solo_by_gameid($gameid);
666 $GT = $gametype." ".$GT;
671 set_gametype($gametype); /* this sets the $CARDS variable */
673 /* get some infos about the game */
674 $gamestatus = DB_get_game_status_by_gameid($gameid);
676 /* display useful things in divs */
678 /* display links to the users status page */
679 $result = mysql_query("SELECT email,password from User WHERE id='$myid'" );
680 $r = mysql_fetch_array($result,MYSQL_NUM);
682 display_links($r[0],$r[1]);
684 /* end display useful things*/
686 /* has the game started? No, then just wait here...*/
687 if($gamestatus == 'pre')
689 echo "You finished the setup, but not everyone else finished it...so you need to wait for the others... <br />";
690 break; /* not sure this works... the idea is that you can
691 * only play a card after everyone is ready to play */
694 /* display the table and the names */
695 $result = mysql_query("SELECT User.fullname as name,".
696 " Hand.position as position, ".
699 "LEFT JOIN User ON User.id=Hand.user_id ".
700 "WHERE Hand.game_id='".$gameid."' ".
701 "ORDER BY position ASC");
703 echo "<div class=\"table\">\n".
704 " <img src=\"pics/table.png\" alt=\"table\" />\n";
705 while($r = mysql_fetch_array($result,MYSQL_NUM))
711 $offset = DB_get_user_timezone($user);
712 $zone = return_timezone($offset);
713 date_default_timezone_set($zone);
715 echo " <span class=\"table".($pos-1)."\">\n";
716 echo " $name <br />\n";
717 echo " local time: ".date("Y-m-d H:i:s")."\n";
723 /* get everything relevant to display the tricks */
724 $result = mysql_query("SELECT Hand_Card.card_id as card,".
725 " Hand.position as position,".
726 " Play.sequence as sequence, ".
730 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
731 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
732 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
733 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
734 "WHERE Trick.game_id='".$gameid."' ".
735 "ORDER BY Trick.id,sequence ASC");
738 $lasttrick = DB_get_max_trickid($gameid);
740 $play = array(); /* needed to calculate winner later */
742 $pos = DB_get_startplayer_by_gameid($gameid)-1;
743 $firstcard = ""; /* first card in a trick */
745 echo "\n<ul class=\"tricks\">\n";
746 echo " <li class=\"nohighlight\"> Game $gameid: </li>\n";
748 while($r = mysql_fetch_array($result,MYSQL_NUM))
755 /* check if first schweinchen has been played */
756 if($r[0] == 19 || $r[0] == 20 )
757 $GAME["schweinchen"]++;
759 /* save card to be able to find the winner of the trick later */
760 $play[$seq] = array("card"=>$r[0],"pos"=>$pos);
764 /* first card in a trick, output some html */
765 if($trick!=$lasttrick)
767 /* start of an old trick? */
768 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
769 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
770 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
772 else if($trick==$lasttrick)
774 /* start of a last trick? */
775 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
776 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
777 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
780 /* remember first card, so that we are able to check, what cards can be played */
785 echo " <div class=\"card".($pos-1)."\">\n";
787 /* display comments */
789 echo " <span class=\"comment\">".$comment."</span>\n";
794 echo " </div>\n"; /* end div card */
800 echo " </div>\n </li>\n"; /* end div table, end li table */
804 if($seq!=4 && $trickNR>1)
805 echo " </div>\n </li>\n"; /* end div table, end li table */
809 /* whos turn is it? */
812 $winner = get_winner($play,$gametype); /* returns the position */
814 $firstcard = ""; /* new trick, no first card */
819 if($next==5) $next=1;
822 /* my turn?, display cards as links, ask for comments*/
823 if(DB_get_pos_by_hash($me) == $next)
828 /* do we want to play a card? */
829 if(myisset("card") && $myturn)
831 $card = $_REQUEST["card"];
832 $handid = DB_get_handid_by_hash($me);
834 /* check if we have card and that we haven't played it yet*/
835 /* set played in hand_card to true where hand_id and card_id*/
836 $result = mysql_query("SELECT id FROM Hand_Card WHERE played='false' and ".
837 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
838 $r = mysql_fetch_array($result,MYSQL_NUM);
845 /* mark card as played */
846 mysql_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
847 DB_quote_smart($card));
848 /* update Game timestamp */
849 DB_update_game_timestamp($gameid);
851 /* check for schweinchen */
852 //echo "schweinchen = ".$GAME["schweinchen"]." --$card-<br />";
853 if($card == 19 || $card == 20 )
855 $GAME["schweinchen"]++;
856 if($GAME["schweinchen"]==3 && $RULES["schweinchen"]=="second" )
857 $comment="Schweinchen! ";
858 if($RULES["schweinchen"]=="both" )
859 $comment="Schweinchen! ";
860 echo "schweinchen = ".$GAME["schweinchen"]." ---<br />";
863 /* get trick id or start new trick */
864 $a = DB_get_current_trickid($gameid);
868 $playid = DB_play_card($trickid,$handcardid,$sequence);
870 /* check for coment */
871 if(myisset("comment"))
873 $comment.=$_REQUEST["comment"];
876 DB_insert_comment($comment,$playid,$myid);
878 /* display played card */
879 echo "<div class=\"card\">";
880 echo " you played <br />";
884 /*check if we still have cards left, else set status to gameover */
885 if(sizeof(DB_get_hand($me))==0)
887 DB_set_hand_status_by_hash($me,'gameover');
888 $mystatus='gameover';
891 /* if all players are done, set game status to game over,
892 * get the points of the last trick and send out an email
895 $userids = DB_get_all_userid_by_gameid($gameid);
898 foreach($userids as $user)
899 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
904 DB_set_game_status_by_gameid($gameid,"gameover");
905 /* get score for last trick
906 * all other tricks are handled a few lines further down*/
907 $play = DB_get_cards_by_trick($trickid);
908 $winner = get_winner($play,$gametype); /* returns the position */
909 /* get points of last trick and save it */
911 foreach($play as $card)
912 $points = $points + card_value($card["card"]);
913 $winnerid = DB_get_handid_by_gameid_and_position($gameid,$winner);
915 mysql_query("INSERT INTO Score VALUES (NULL, '$gameid', '$winnerid', '$points')");
917 echo "ERROR during scoring";
919 /* email all players */
920 /* individual score */
921 $result = mysql_query("SELECT fullname, SUM(score), Hand.party FROM Score".
922 " LEFT JOIN Hand ON Hand.id=hand_id".
923 " LEFT JOIN User ON Hand.user_id=User.id".
924 " WHERE Hand.game_id=$gameid".
925 " GROUP BY fullname" );
926 $message = "The game is over. Thanks for playing :)\n";
927 while( $r = mysql_fetch_array($result,MYSQL_NUM))
928 $message .= " FINAL SCORE: ".$r[0]."(".$r[2].") ".$r[1]."\n";
929 $message .= "\nIf your not in the list above your score is zero...\n\n";
931 $result = mysql_query("SELECT Hand.party, SUM(score) FROM Score".
932 " LEFT JOIN Hand ON Hand.id=hand_id".
933 " LEFT JOIN User ON Hand.user_id=User.id".
934 " WHERE Hand.game_id=$gameid".
935 " GROUP BY Hand.party" );
937 while( $r = mysql_fetch_array($result,MYSQL_NUM))
938 $message .= " FINAL SCORE: ".$r[0]." ".$r[1]."\n";
940 foreach($userids as $user)
942 $To = DB_get_email_by_userid($user);
943 mymail($To,$EmailName."game over",$message);
948 /* email next player */
949 if(DB_get_game_status_by_gameid($gameid)=='play')
953 $play = DB_get_cards_by_trick($trickid);
954 $winner = get_winner($play,$gametype); /* returns the position */
956 /* get points of last trick and save it, last trick is handled
957 * a few lines further up */
959 foreach($play as $card)
960 $points = $points + card_value($card["card"]);
962 $winnerid = DB_get_handid_by_gameid_and_position($gameid,$winner);
964 mysql_query("INSERT INTO Score VALUES (NULL, '$gameid', '$winnerid', '$points')");
966 echo "ERROR during scoring";
969 echo "DEBUG: $winner got $points <br />";
971 /* who is the next player? */
976 $next = DB_get_pos_by_hash($me)+1;
978 if($next==5) $next=1;
980 /* email next player */
981 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
982 $email = DB_get_email_by_hash($next_hash);
984 $message = "It's your turn now.\n".
985 "Use this link to play a card: ".$host."?me=".$next_hash."\n\n" ;
986 mymail($email,$EmailName."a card has been played",$message);
989 echo "DEBUG:<a href=\"index.php?me=".DB_get_hash_from_game_and_pos($gameid,$next).
990 "\"> next player </a> <br />\n";
996 echo "can't find that card?! <br />\n";
999 else if(myisset("card") && !$myturn )
1001 echo "please wait until it's your turn! <br />\n";
1004 $mycards = DB_get_hand($me);
1005 $mycards = mysort($mycards,$gametype);
1006 echo "<div class=\"mycards\">\n";
1008 if($myturn && !myisset("card"))
1010 echo "Hello ".$myname.", it's your turn! <br />\n";
1011 echo "Your cards are: <br />\n";
1012 echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
1014 /* do we have to follow suite? */
1016 if(have_suit($mycards,$firstcard))
1019 foreach($mycards as $card)
1021 if($followsuit && !same_type($card,$firstcard))
1022 display_card($card);
1024 display_link_card($card);
1027 echo "<br />\nA short comments:<input name=\"comment\" type=\"text\" size=\"30\" maxlength=\"50\" />\n";
1028 echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />\n";
1029 echo "<input type=\"submit\" value=\"move\" />\n";
1032 else if($mystatus=='play')
1034 echo "Your cards are: <br />\n";
1035 foreach($mycards as $card)
1036 display_card($card);
1038 else if($mystatus=='gameover')
1040 $oldcards = DB_get_all_hand($me);
1041 $oldcards = mysort($oldcards,$gametype);
1042 echo "Your cards were: <br />\n";
1043 foreach($oldcards as $card)
1044 display_card($card);
1048 /* check if we need to set status to 'gameover' is done during playing of the card */
1049 if($mystatus=='play')
1051 /* the following happens only when the gamestatus is 'gameover' */
1052 /* check if game is over, display results */
1053 if(DB_get_game_status_by_gameid($gameid)=='play')
1055 echo "the game is over for you.. other people still need to play though";
1059 echo "the game is over now...<br />\n";
1061 $result = mysql_query("SELECT fullname, SUM(score), Hand.party FROM Score".
1062 " LEFT JOIN Hand ON Hand.id=hand_id".
1063 " LEFT JOIN User ON Hand.user_id=User.id".
1064 " WHERE Hand.game_id=$gameid".
1065 " GROUP BY fullname" );
1066 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1067 echo " FINAL SCORE: ".$r[0]."(".$r[2].") ".$r[1]."<br />";
1069 $result = mysql_query("SELECT Hand.party, SUM(score) FROM Score".
1070 " LEFT JOIN Hand ON Hand.id=hand_id".
1071 " LEFT JOIN User ON Hand.user_id=User.id".
1072 " WHERE Hand.game_id=$gameid".
1073 " GROUP BY Hand.party" );
1074 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1075 echo " FINAL SCORE: ".$r[0]." ".$r[1]."<br />\n";
1078 $session = DB_get_session_by_gameid($gameid);
1079 $result = mysql_query("SELECT id,create_date FROM Game".
1080 " WHERE session=$session".
1081 " ORDER BY create_date DESC".
1085 $r = mysql_fetch_array($result,MYSQL_NUM);
1087 if(!$session || $gameid==$r[0])
1089 /* suggest a new game with the same people in it, just rotated once */
1090 $names = DB_get_all_names_by_gameid($gameid);
1091 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
1096 echo "error in testing the status";
1101 /* user status page */
1102 else if(myisset("email","password"))
1104 /* test id and password, should really be done in one step */
1105 $email = $_REQUEST["email"];
1106 $password = $_REQUEST["password"];
1108 if(myisset("forgot"))
1112 $uid = DB_get_userid_by_email($email);
1118 echo "Hmm, you forgot your passwort...nothing I can do at the moment:( ";
1119 echo " you need to email Arun for now... in the future it will be all automated and an ";
1120 echo "email with a new password will go to $email.";
1125 echo "you need to give me an email address!";
1127 echo "couldn't find a player with this email, please contact Arun, if you think this is a mistake";
1132 if(strlen($password)!=32)
1133 $password = md5($password);
1136 $uid = DB_get_userid_by_email_and_password($email,$password);
1142 $time = DB_get_user_timestamp($uid);
1143 $unixtime =strtotime($time);
1145 $offset = DB_get_user_timezone($uid);
1146 $zone = return_timezone($offset);
1147 date_default_timezone_set($zone);
1149 echo "last login: ".date("r",$unixtime)."<br />";
1151 DB_update_user_timestamp($uid);
1153 echo "<p>these are the games you are playing in:<br />\n";
1154 $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.mod_date from Hand".
1155 " LEFT JOIN Game On Hand.game_id=Game.id".
1156 " WHERE Hand.user_id='$uid' AND Game.status<>'gameover'" );
1157 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1159 echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a>";
1160 if(time()-strtotime($r[2]) > 60*60*24*30)
1161 echo " The game has been running for over a month.".
1162 " Do you want to cancel it? <a href=\"$host?cancle=1&me=".$r[0]."\">yes</a>".
1163 " (clicking here is final and can't be restored)";
1169 echo "<p>and these are your games that are already done:<br />Game: \n";
1170 $result = mysql_query("SELECT hash,game_id from Hand WHERE user_id='$uid' AND status='gameover'" );
1171 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1172 echo "<a href=\"".$host."?me=".$r[0]."\">#".$r[1]." </a>, ";
1175 $names = DB_get_all_names();
1176 echo "<p>registered players:<br />\n";
1177 foreach ($names as $name)
1181 echo "<p>Want to start a new game? Visit <a href=\"".$host."?new\">this page.</a></p>";
1185 echo "sorry email and password don't match <br />";
1191 /* page for registration */
1192 else if(myisset("register") )
1196 /* new user wants to register */
1197 else if(myisset("Rfullname","Remail","Rpassword","Rtimezone") )
1200 if(DB_get_userid_by_name($_REQUEST["Rfullname"]))
1202 echo "please chose another name<br />";
1205 if(DB_get_userid_by_email($_REQUEST["Remail"]))
1207 echo "this email address is already used ?!<br />";
1212 $r=mysql_query("INSERT INTO User VALUES(NULL,".DB_quote_smart($_REQUEST["Rfullname"]).
1213 ",".DB_quote_smart($_REQUEST["Remail"]).
1214 ",".DB_quote_smart(md5($_REQUEST["Rpassword"])).
1215 ",".DB_quote_smart($_REQUEST["Rtimezone"]).",NULL)");
1218 echo " added you to the database";
1220 echo " something went wrong";
1223 /* default login page */
1226 $pre=0;$game=0;$done=0;
1227 $r=mysql_query("SELECT COUNT(id) FROM Game GROUP BY status");
1229 $pre = mysql_fetch_array($r,MYSQL_NUM);
1230 $game = mysql_fetch_array($r,MYSQL_NUM);
1231 $done = mysql_fetch_array($r,MYSQL_NUM);
1233 output_home_page($pre[0],$game[0],$done[0]);