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 */
9 /* check if some variables are set in the config file, else set defaults */
10 if(!isset($EmailName))
12 if(isset($EMAIL_REPLY))
14 ini_set("sendmail_from",$EMAIL_REPLY);
16 if(!isset($ADMIN_NAME))
19 echo "<h1>Setup not completed</h1>";
20 echo "You need to set \$ADMIN_NAME in config.php.";
24 if(!isset($ADMIN_EMAIL))
27 echo "<h1>Setup not completed</h1>";
28 echo "You need to set \$ADMIN_EMAIL in config.php. ".
29 "If something goes wrong an email will send to this address.";
34 /* in case work has to be done on the database or other section we can
35 * shut down the server and tell people to come back later
40 echo "Working on the database...please check back in a few mintues";
48 echo "Database error, can't connect... Please wait a while and try again. ".
49 "If the problem doesn't go away feel free to contact $ADMIN_NAME at $ADMIN_EMAIL.";
54 /* done major error checking, output header of HTML page */
57 /* check if we want to start a new game */
60 $names = DB_get_all_names();
61 output_form_for_new_game($names);
63 /*check if everything is ready to set up a new game */
64 else if( myisset("PlayerA", "PlayerB","PlayerC","PlayerD","dullen","schweinchen","call" ))
66 $PlayerA = $_REQUEST["PlayerA"];
67 $PlayerB = $_REQUEST["PlayerB"];
68 $PlayerC = $_REQUEST["PlayerC"];
69 $PlayerD = $_REQUEST["PlayerD"];
71 $dullen = $_REQUEST["dullen"];
72 $schweinchen = $_REQUEST["schweinchen"];
73 $call = $_REQUEST["call"];
75 $EmailA = DB_get_email_by_name($PlayerA);
76 $EmailB = DB_get_email_by_name($PlayerB);
77 $EmailC = DB_get_email_by_name($PlayerC);
78 $EmailD = DB_get_email_by_name($PlayerD);
80 if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
82 echo "couldn't find one of the names, please start a new game";
88 $useridA = DB_get_userid_by_name($PlayerA);
89 $useridB = DB_get_userid_by_name($PlayerB);
90 $useridC = DB_get_userid_by_name($PlayerC);
91 $useridD = DB_get_userid_by_name($PlayerD);
93 /* create random numbers */
94 $randomNR = create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD);
95 $randomNRstring = join(":",$randomNR);
99 if(myisset("followup") )
101 $followup= $_REQUEST["followup"];
102 $session = DB_get_session_by_gameid($followup);
103 $ruleset = DB_get_ruleset_by_gameid($followup); /* just copy ruleset from old game,
104 this way no manipulation is possible */
106 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
107 "'$ruleset','$session' ,NULL)");
110 /* get max session */
111 $max = DB_get_max_session();
113 mysql_query("UPDATE Game SET session='".$max."' WHERE id=".DB_quote_smart($followup));
114 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
115 "'$ruleset','$max' ,NULL)");
120 /* get ruleset information or create new one */
121 $ruleset = DB_get_ruleset($dullen,$schweinchen,$call);
124 myerror("Error defining ruleset: $ruleset");
130 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre', ".
131 "'$ruleset',NULL ,NULL)");
133 $game_id = mysql_insert_id();
136 $TIME = (string) time(); /* to avoid collisions */
137 $hashA = md5("AGameOfDoko".$game_id.$PlayerA.$EmailA.$TIME);
138 $hashB = md5("AGameOfDoko".$game_id.$PlayerB.$EmailB.$TIME);
139 $hashC = md5("AGameOfDoko".$game_id.$PlayerC.$EmailC.$TIME);
140 $hashD = md5("AGameOfDoko".$game_id.$PlayerD.$EmailD.$TIME);
143 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridA).
144 ", ".DB_quote_smart($hashA).", 'start','1',NULL,NULL,NULL,NULL)");
145 $hand_idA = mysql_insert_id();
146 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridB).
147 ", ".DB_quote_smart($hashB).", 'start','2',NULL,NULL,NULL,NULL)");
148 $hand_idB = mysql_insert_id();
149 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridC).
150 ", ".DB_quote_smart($hashC).", 'start','3',NULL,NULL,NULL,NULL)");
151 $hand_idC = mysql_insert_id();
152 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridD).
153 ", ".DB_quote_smart($hashD).", 'start','4',NULL,NULL,NULL,NULL)");
154 $hand_idD = mysql_insert_id();
158 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idA', '".$randomNR[$i]."', 'false')");
159 for($i=12;$i<24;$i++)
160 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idB', '".$randomNR[$i]."', 'false')");
161 for($i=24;$i<36;$i++)
162 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idC', '".$randomNR[$i]."', 'false')");
163 for($i=36;$i<48;$i++)
164 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idD', '".$randomNR[$i]."', 'false')");
166 /* send out email, TODO: check for error with email */
168 "you are invited to play a game of DoKo (that is to debug the program ;).\n".
169 "Place comments and bug reports here:\n".
170 "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
171 "The whole round would consist of the following players:\n".
176 "If you want to join this game, please follow this link:\n\n".
179 mymail($EmailA,"You are invited to a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
180 mymail($EmailB,"You are invited to a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
181 mymail($EmailC,"You are invited to a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
182 mymail($EmailD,"You are invited to a game of DoKo","Hello $PlayerD,\n".$message.$hashD);
184 echo "You started a new game. The emails have been sent out!";
185 } /* end set up a new game */
186 /* cancle a game, if nothing has happend in the last N minutes */
187 else if(myisset("cancle","me"))
189 $me = $_REQUEST["me"];
191 /* test for valid ID */
192 $myid = DB_get_userid_by_hash($me);
195 echo "Can't find you in the database, please check the url.<br />\n";
196 echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
202 DB_update_user_timestamp($myid);
204 /* get some information from the DB */
205 $gameid = DB_get_gameid_by_hash($me);
206 $myname = DB_get_name_by_hash($me);
208 /* check if game really is old enough */
209 $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
210 $r = mysql_fetch_array($result,MYSQL_NUM);
211 if(time()-strtotime($r[0]) > 60*60*24*30) /* = 1 month */
213 $message = "Hello, \n\n".
214 "Game $gameid has been cancled since nothing happend for a while and $myname requested it.\n";
216 $userids = DB_get_all_userid_by_gameid($gameid);
217 foreach($userids as $user)
219 $To = DB_get_email_by_userid($user);
220 mymail($To,$EmailName."game $gameid cancled (timed out)",$message);
223 /* delete everything from the dB */
226 echo "<p style=\"background-color:red\";>Game $gameid has been cancled.<br /><br /></p>";
229 echo "<p>You need to wait longer before you can cancle a game...</p>\n";
231 /* handle request from one specific player for one game,
232 * (the hash is set on a per game base) */
233 else if(myisset("me"))
235 $me = $_REQUEST["me"];
237 /* test for valid ID */
238 $myid = DB_get_userid_by_hash($me);
241 echo "Can't find you in the database, please check the url.<br />\n";
242 echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
248 /* the user had done something, update the timestamp */
249 DB_update_user_timestamp($myid);
251 /* get some information from the DB */
252 $gameid = DB_get_gameid_by_hash($me);
253 $myname = DB_get_name_by_hash($me);
254 $mystatus = DB_get_status_by_hash($me);
255 $mypos = DB_get_pos_by_hash($me);
256 $myhand = DB_get_handid_by_hash($me);
257 $session = DB_get_session_by_gameid($gameid);
259 /* get prefs and save them */
264 /* get rule set for this game */
265 $result = mysql_query("SELECT * FROM Rulesets".
266 " LEFT JOIN Game ON Game.ruleset=Rulesets.id ".
267 " WHERE Game.id='$gameid'" );
268 $r = mysql_fetch_array($result,MYSQL_NUM);
270 $RULES["dullen"] = $r[2];
271 $RULES["schweinchen"] = $r[3];
272 $RULES["call"] = $r[4];
275 /* get some infos about the game */
276 $gametype = DB_get_gametype_by_gameid($gameid);
277 $gamestatus = DB_get_game_status_by_gameid($gameid);
279 if($gametype=="solo")
281 $gametype = DB_get_solo_by_gameid($gameid);
282 $GT = $gametype." ".$GT;
285 /* display rule set for this game */
286 echo "<div class=\"ruleset\">\n";
288 if($gamestatus != 'pre')
289 echo " Gametype: $GT <br />\n";
291 echo "Rules: <br />\n";
292 echo "10ofhearts : ".$r[2]."<br />\n";
293 echo "schweinchen: ".$r[3]."<br />\n";
294 echo "call: ".$r[4]."<br />\n";
297 /* output extra division in case this game is part of a session */
300 echo "<div class=\"session\">\n".
301 "This game is part of session $session: \n";
302 $hashes = DB_get_hashes_by_session($session,$myid);
304 foreach($hashes as $hash)
309 echo "<a href=\"".$host."?me=".$hash."\">$i</a> ";
316 /* does anyone have both foxes */
317 $GAME["schweinchen"]=0;
320 $hash = DB_get_hash_from_game_and_pos($gameid,$i);
321 $cards = DB_get_all_hand($hash);
322 if( in_array("19",$cards) && in_array("20",$cards) )
324 $GAME["schweinchen"]=1;
325 $GAME["schweinchen-who"]=$hash;
329 /* mystatus gets the player through the different stages of a game.
331 * init: check values from start,
333 * check: check for return values from init
334 * poverty: handle poverty, wait here until all player have reached this state
335 * display sickness and move on to game
336 * play: game in progress
337 * gameover: are we revisiting a game
342 check_want_to_play($me);
343 /* move on to the next stage*/
344 DB_set_hand_status_by_hash($me,'init');
347 /* first check if everything went ok in the last step
348 * if not, send user back, if yes, check what he did
352 echo "<p> you need to answer the <a href=\"$host?me=$me\">question</a>.</p>";
353 DB_set_hand_status_by_hash($me,'start');
357 if($_REQUEST["in"] == "no")
359 /* cancle the game */
360 $message = "Hello, \n\n".
361 "the game has been canceled due to the request of one of the players.\n";
363 $userids = DB_get_all_userid_by_gameid($gameid);
364 foreach($userids as $user)
366 $To = DB_get_email_by_userid($user);
367 mymail($To,$EmailName."game $gameid canceled",$message);
370 /* delete everything from the dB */
375 echo "thanks for joining the game...";
377 $mycards = DB_get_hand($me);
379 echo "<p class=\"mycards\" style=\"margin-top:8em;\">your cards are: <br />\n";
380 foreach($mycards as $card)
381 display_card($card,$PREF["cardset"]);
384 output_check_for_sickness($me,$mycards);
386 /* move on to the next stage*/
387 DB_set_hand_status_by_hash($me,'check');
393 /* ok, user is in the game, saw his cards and selected his vorbehalt
394 * so first we check what he selected
396 echo "Processing what you selected in the last step...<br />";
398 if(!myisset("solo","wedding","poverty","nines") )
400 /* all these variables have a pre-selected default,
401 * so we should never get here,
402 * unless a user tries to cheat ;) */
403 echo "something went wrong during the setup...please contact the $ADMIN_NAME at $ADMIN_EMAIL.";
407 /* check if this sickness needs to be handled first */
408 $gametype = DB_get_gametype_by_gameid($gameid);
409 $startplayer = DB_get_startplayer_by_gameid($gameid);
411 if( $_REQUEST["solo"]!="No")
413 /* user wants to play a solo */
415 /* store the info in the user's hand info */
416 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
417 DB_set_sickness_by_hash($me,"solo");
419 echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
421 if($gametype == "solo" && $startplayer<$mypos)
422 {}/* do nothing, since someone else already is playing solo */
425 /* this solo comes first
426 * store info in game table
428 DB_set_gametype_by_gameid($gameid,"solo");
429 DB_set_startplayer_by_gameid($gameid,$mypos);
430 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
433 else if($_REQUEST["wedding"] == "yes")
435 /* TODO: add silent solo somewhere*/
436 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
437 DB_set_sickness_by_hash($me,"wedding");
439 else if($_REQUEST["poverty"] == "yes")
441 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
442 DB_set_sickness_by_hash($me,"poverty");
444 else if($_REQUEST["nines"] == "yes")
446 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
447 " is playing solo, this game will be canceled.<br />\n";
448 DB_set_sickness_by_hash($me,"nines");
451 echo " Ok, done with checking, please go to the <a href=\"$host?me=$me\">next step of the setup</a>.<br />";
453 /* move on to the next stage*/
454 DB_set_hand_status_by_hash($me,'poverty');
456 /* check if everyone has reached this stage, send out email */
457 $userids = DB_get_all_userid_by_gameid($gameid);
459 foreach($userids as $user)
461 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
462 if($userstat!='poverty' && $userstat!='play')
465 DB_set_player_by_gameid($gameid,$user);
470 /* reset player = everyone has to do something now */
471 DB_set_player_by_gameid($gameid,NULL);
473 foreach($userids as $user)
475 $To = DB_get_email_by_userid($user);
476 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
479 $message = "Everyone finish the questionary in game $gameid, ".
480 "please visit this link now to continue: \n".
481 " ".$host."?me=".$userhash."\n\n" ;
482 mymail($To,$EmailName." finished setup in game $gameid",$message);
491 /* here we need to check if there is a solo or some other form of sickness.
492 * If so, which one is the most important one
493 * set that one in the Game table
494 * tell people about it.
496 echo "<br /> Checking if someone else selected solo, nines or wedding or poverty.<br />";
498 /* check if everyone has reached this stage */
499 $userids = DB_get_all_userid_by_gameid($gameid);
501 foreach($userids as $user)
503 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
504 if($userstat!='poverty' && $userstat!='play')
510 echo "This step can only be handled after everyone finished the last step. ".
511 "Seems like this is not the case, so you need to wait a bit... ".
512 "you will get an email once that is the case, please use the link in ".
513 "that email to continue the game.<br />";
517 echo "Everyone has finished checking their cards, let's see what they said...<br />";
519 /* check what kind of game we are playing, in case there are any solos this already
520 *will have the correct information in it */
521 $gametype = DB_get_gametype_by_gameid($gameid);
522 $startplayer = DB_get_startplayer_by_gameid($gameid);
524 /* check for different sickness and just output a general info */
529 foreach($userids as $user)
531 $name = DB_get_name_by_userid($user);
532 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
533 if($usersick == 'nines')
536 echo "$name has a Vorbehalt. <br />";
539 else if($usersick == 'poverty')
542 echo "$name has a Vorbehalt. <br />";
544 else if($usersick == 'wedding')
547 echo "$name has a Vorbehalt. <br />" ;
549 else if($usersick == 'solo')
552 echo "$name has a Vorbehalt. <br />" ;
556 /* now check which sickness comes first and set the gametype to it */
558 if($gametype == "solo")
565 /* TODO: should we keep statistics of this? */
566 $message = "Hello, \n\n".
567 "the game has been canceled because ".DB_get_name_by_userid($nines).
568 " has five or more nines and nobody is playing solo.\n";
570 /* TODO: add info about redeal in case this is a game of a series */
572 $userids = DB_get_all_userid_by_gameid($gameid);
573 foreach($userids as $user)
575 $To = DB_get_email_by_userid($user);
576 mymail($To,$EmailName."game $gameid canceled",$message);
579 /* delete everything from the dB */
582 echo "The game has been canceled because ".DB_get_name_by_userid($nines).
583 " has five or more nines and nobody is playing solo.\n";
588 else if($poverty==1) /* one person has poverty */
590 DB_set_gametype_by_gameid($gameid,"poverty");
591 $gametype = "poverty";
592 $who = DB_get_sickness_by_gameid($gameid);
595 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
596 if($firstsick == "poverty")
597 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
599 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
602 else if($poverty==2) /* two people have poverty */
604 DB_set_gametype_by_gameid($gameid,"dpoverty");
605 $gametype = "dpoverty";
606 $who = DB_get_sickness_by_gameid($gameid);
609 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
610 if($firstsick == "poverty")
612 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
613 if($secondsick == "poverty")
614 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
616 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
619 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
624 DB_set_gametype_by_gameid($gameid,"wedding");
625 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
626 $gametype = "wedding";
631 /* now the gametype is set correctly (shouldn't matter that this is calculated for every user)
632 * output what kind of game we have */
635 foreach($userids as $user)
637 /* userids are sorted by position...
638 * so output whatever the firstone has, then whatever the next one has
639 * stop when the sickness is the same as the gametype
642 $name = DB_get_name_by_userid($user);
643 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
646 echo "$name has $usersick. <br />"; /*TODO: perhaps save this in a string and store in Game? */
648 if($usersick=="poverty")
650 if($usersick == "wedding" && $gametype =="wedding")
652 if($usersick == "poverty" && $gametype =="poverty")
654 if($usersick == "poverty" && $gametype =="dpoverty" && $poverty==2)
656 if($usersick == "solo" && $gametype =="solo")
660 /* output Schweinchen in case the rules need it */
661 if( $gametype != "solo")
662 if($GAME["schweinchen"] && $RULES["schweinchen"]=="both" )
663 echo DB_get_name_by_hash($GAME["schweinchen-who"])." has Schweinchen. <br />";
667 /* finished the setup, set re/contra parties if possible, go to next stage unless there is a case of poverty*/
671 /* are we the solo player? set us to re, else set us to contra */
672 $pos = DB_get_pos_by_hash($me);
673 if($pos == $startplayer)
674 DB_set_party_by_hash($me,"re");
676 DB_set_party_by_hash($me,"contra");
677 DB_set_hand_status_by_hash($me,'play');
681 /* set person with the wedding to re, do the rest during the game */
682 $usersick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
683 if($usersick == "wedding")
684 DB_set_party_by_hash($me,"re");
686 DB_set_party_by_hash($me,"contra");
688 echo "Whoever will make the first trick will be on the re team. <br />\n";
689 echo " Ok, the game can start now, please finish <a href=\"$host?me=$me\">the setup</a>.<br />";
690 DB_set_hand_status_by_hash($me,'play');
694 $hand = DB_get_all_hand($me);
696 if(in_array('3',$hand)||in_array('4',$hand))
697 DB_set_party_by_hash($me,"re");
699 DB_set_party_by_hash($me,"contra");
700 DB_set_hand_status_by_hash($me,'play');
704 /* check if poverty resolved (e.g. DB.Game who set to NULL)
705 * yes? =>trump was taken, start game; break;
707 $who = DB_get_sickness_by_gameid($gameid);
709 { /* trump has been taken */
710 DB_set_hand_status_by_hash($me,'play');
714 if($who>9) /*= two people still have trump on the table*/
719 /* check if we are being asked now
720 * no? display wait message, e.g. player X is asked at the moment
722 $usersick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
723 if(myisset("trump") && $_REQUEST["trump"]=="no" && ($who==$mypos || $who==$mypos*10))
725 /* user doesn't want to take trump */
726 /* set next player who needs to be asked */
727 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
728 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
730 if($firstsick=="poverty")
732 if($secondsick=="poverty")
733 DB_set_sickness_by_gameid($gameid,$who+$add*3);
735 DB_set_sickness_by_gameid($gameid,$who+$add*2);
738 DB_set_sickness_by_gameid($gameid,$who+$add);
740 /* email next player */
741 $who = DB_get_sickness_by_gameid($gameid);
742 if($who>9) $who = $who/10;
746 $To = DB_get_email_by_pos_and_gameid($who,$gameid);
747 $userhash = DB_get_hash_from_game_and_pos($gameid,$who);
748 DB_set_player_by_gameid($gameid,$who);
750 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
751 " ".$host."?me=".$userhash."\n\n" ;
752 mymail($To,$EmailName." poverty (game $gameid)",$message);
755 /* this user is done */
756 DB_set_hand_status_by_hash($me,'play');
759 else if(myisset("trump") && !myisset("exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
761 /* user wants to take trump */
762 $trump = $_REQUEST["trump"];
764 /* get hand id for user $trump */
765 $userhand = DB_get_handid_by_gameid_and_userid($gameid,$trump);
766 /* copy trump from player A to B */
767 $result = mysql_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
769 /* add hidden button with trump in it to get to the next point */
770 echo "<form action=\"index.php\" method=\"post\">\n";
771 echo " <input type=\"hidden\" name=\"exchange\" value=\"-1\" />\n";
772 echo " <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
773 echo " <input type=\"hidden\" name=\"me\" value=\"".$me."\" />\n";
774 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select cards to give back\" />\n";
777 else if(myisset("trump","exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
779 $trump = $_REQUEST["trump"];
780 $exchange = $_REQUEST["exchange"];
781 $userhand = DB_get_handid_by_gameid_and_userid($gameid,$trump);
783 /* if exchange is set to a value>0, exchange that card back to user $trump */
786 $result = mysql_query("UPDATE Hand_Card SET hand_id='$userhand'".
787 " WHERE hand_id='$myhand' AND card_id='$exchange'" );
790 /* if number of cards == 12, set status to play for both users */
791 $result = mysql_query("SELECT COUNT(*) FROM Hand_Card WHERE hand_id='$myhand'" );
792 $r = mysql_fetch_array($result,MYSQL_NUM);
795 myerror("error in poverty");
800 if($gametype=="poverty" || $who<9)
802 DB_set_sickness_by_gameid($gameid,-1); /* done with poverty */
804 else /* reduce poverty count by one, that is go to single digits $who */
809 /* whom to ask next */
810 $firstsick = DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
811 $secondsick = DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
813 if($firstsick!="poverty")
814 DB_set_sickness_by_gameid($gameid,$who+$add);
817 if($secondsick!="poverty")
818 DB_set_sickness_by_gameid($gameid,$who+$add*2);
820 DB_set_sickness_by_gameid($gameid,$who+$add*3);
823 /* email next player */
824 $who = DB_get_sickness_by_gameid($gameid);
827 $To = DB_get_email_by_pos_and_gameid($who,$gameid);
828 $userhash = DB_get_hash_from_game_and_pos($gameid,$who);
829 DB_set_player_by_gameid($gameid,$who);
831 $message = "Someone has poverty, it's your turn to decide, ".
832 "if you want to take the trump. Please visit:".
833 " ".$host."?me=".$userhash."\n\n" ;
834 mymail($To,$EmailName." poverty (game $gameid)",$message);
838 /* this user is done */
839 DB_set_hand_status_by_hash($me,'play');
840 /* and so is his partner */
841 $hash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
842 DB_set_hand_status_by_hash($hash,'play');
844 /* set party to re, unless we had dpoverty, in that case check if we need to set re/contra*/
846 foreach($userids as $user)
848 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
849 $party = DB_get_party_by_hash($userhash);
855 DB_set_party_by_hash($me,"contra");
856 DB_set_party_by_hash($hash,"contra");
860 foreach($userids as $user)
862 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
863 if($userhash==$hash||$userhash==$me)
864 DB_set_party_by_hash($userhash,"re");
866 DB_set_party_by_hash($userhash,"contra");
875 /* else show all trump, have lowest card pre-selected, have hidden setting for */
876 echo "you need to get rid of a few cards<br />\n";
878 set_gametype($gametype); /* this sets the $CARDS variable */
879 $mycards = DB_get_hand($me);
880 $mycards = mysort($mycards,$gametype);
882 echo "<form class=\"exchange\" action=\"index.php\" method=\"post\">\n";
884 foreach($mycards as $card)
885 display_link_card($card,$PREF["cardset"],$type);
886 echo " <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
887 echo " <input type=\"hidden\" name=\"me\" value=\"".$me."\" />\n";
888 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select one card to give back\" />\n";
892 else if($who == $mypos || $who == $mypos*10)
894 foreach($userids as $user)
896 $name = DB_get_name_by_userid($user);
897 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
899 if($usersick=="poverty")
901 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
902 $cards = DB_get_hand($hash);
903 $nrtrump = count_trump($cards);
906 echo "Player $name has $nrtrump trump. Do you want to take them?".
907 "<a href=\"index.php?me=$me&trump=$user\">yes</a> <br />";
910 echo "<a href=\"index.php?me=$me&trump=no\">No,way I take those trump...</a> <br />";
912 echo "Your cards are: <br />\n";
913 $mycards = DB_get_hand($me);
915 echo "<p class=\"mycards\" style=\"margin-top:8em;\">your cards are: <br />\n";
916 foreach($mycards as $card)
917 display_card($card,$PREF["cardset"]);
922 $mysick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
923 if($mysick=="poverty")
924 echo "The others are asked if they want to take your trump, you have to wait (you'll get an email).";
926 echo "it's not your turn yet to decide if you want to take the trump or not.";
929 /* check if no one wanted to take trump, in that case the gamesickness would be set to 5 or 50 */
930 $who = DB_get_sickness_by_gameid($gameid);
931 if($who==5 || $who==50)
933 $message = "Hello, \n\n".
934 "Game $gameid has been cancled since nobody wanted to take the trump.\n";
936 $userids = DB_get_all_userid_by_gameid($gameid);
937 foreach($userids as $user)
939 $To = DB_get_email_by_userid($user);
940 mymail($To,$EmailName."game $gameid cancled (poverty not resolved)",$message);
943 /* delete everything from the dB */
946 echo "<p style=\"background-color:red\";>Game $gameid has been cancled.<br /><br /></p>";
952 /* check if all players are ready to play */
954 foreach($userids as $user)
955 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='play')
958 DB_set_player_by_gameid($gameid,$user);
963 /* only set this after all poverty, etc. are handled*/
964 DB_set_game_status_by_gameid($gameid,'play');
966 /* email startplayer */
967 $startplayer = DB_get_startplayer_by_gameid($gameid);
968 $email = DB_get_email_by_pos_and_gameid($startplayer,$gameid);
969 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
970 $who = DB_get_userid_by_email($email);
971 DB_set_player_by_gameid($gameid,$who);
975 /* email startplayer) */
976 $message = "It's your turn now in game $gameid.\n".
977 "Use this link to play a card: ".$host."?me=".$hash."\n\n" ;
978 mymail($email,$EmailName."ready, set, go... (game $gameid) ",$message);
981 echo " Please, <a href=\"$host?me=$me\">start</a> the game.<br />";
989 /* both entries here, so that the tricks are visible for both.
990 * in case of 'play' there is a break later that skips the last part
993 /* figure out what kind of game we are playing,
994 * set the global variables $CARDS["trump"],$CARDS["diamonds"],$CARDS["hearts"],
995 * $CARDS["clubs"],$CARDS["spades"],$CARDS["foxes"]
999 $gametype = DB_get_gametype_by_gameid($gameid);
1001 if($gametype=="solo")
1003 $gametype = DB_get_solo_by_gameid($gameid);
1004 $GT = $gametype." ".$GT;
1007 $gametype = "normal";
1009 set_gametype($gametype); /* this sets the $CARDS variable */
1011 /* get some infos about the game */
1012 $gamestatus = DB_get_game_status_by_gameid($gameid);
1014 /* display useful things in divs */
1016 /* display links to the users status page */
1017 $result = mysql_query("SELECT email,password from User WHERE id='$myid'" );
1018 $r = mysql_fetch_array($result,MYSQL_NUM);
1020 display_links($r[0],$r[1]);
1022 /* end display useful things*/
1024 /* has the game started? No, then just wait here...*/
1025 if($gamestatus == 'pre')
1027 echo "You finished the setup, but not everyone else finished it... ".
1028 "so you need to wait for the others. Just wait for the an email... <br />";
1029 break; /* not sure this works... the idea is that you can
1030 * only play a card after everyone is ready to play */
1033 /* display the table and the names */
1034 $result = mysql_query("SELECT User.fullname as name,".
1035 " Hand.position as position, ".
1037 " Hand.party as party, ".
1038 " Hand.sickness as sickness, ".
1039 " Hand.point_call, ".
1040 " User.last_login, ".
1043 "LEFT JOIN User ON User.id=Hand.user_id ".
1044 "WHERE Hand.game_id='".$gameid."' ".
1045 "ORDER BY position ASC");
1047 echo "<div class=\"table\">\n".
1048 " <img src=\"pics/table.png\" alt=\"table\" />\n";
1049 while($r = mysql_fetch_array($result,MYSQL_NUM))
1057 $lastlogin = strtotime($r[6]);
1060 $offset = DB_get_user_timezone($user);
1061 $zone = return_timezone($offset);
1062 date_default_timezone_set($zone);
1064 echo " <span class=\"table".($pos-1)."\">\n";
1069 echo "<a href=\"".$host."?me=".$hash."\">$name</a>\n";
1071 /* add hints for poverty, wedding, solo, etc */
1072 if($GT=="poverty" && $party=="re")
1073 if($sickness=="poverty")
1075 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1076 $cards = DB_get_all_hand($userhash);
1077 $trumpNR = count_trump($cards);
1079 echo "<img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" />";
1081 echo "<img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" />";
1084 echo "<img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" />";
1088 if($sickness=="poverty")
1090 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1091 $cards = DB_get_all_hand($userhash);
1092 $trumpNR = count_trump($cards);
1094 echo "<img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" />";
1096 echo "<img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" />";
1099 echo "<img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" />";
1101 if($sickness=="poverty")
1103 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1104 $cards = DB_get_all_hand($userhash);
1105 $trumpNR = count_trump($cards);
1107 echo "<img src=\"pics/button/poverty2_trump_button.png\" class=\"button\" alt=\"poverty2 < trump back\" />";
1109 echo "<img src=\"pics/button/poverty2_notrump_button.png\" class=\"button\" alt=\"poverty2 <\" />";
1112 echo "<img src=\"pics/button/poverty2_partner_button.png\" class=\"button\" alt=\"poverty2 >\" />";
1114 if($GT=="wedding" && $party=="re")
1115 if($sickness=="wedding")
1116 echo "<img src=\"pics/button/wedding_button.png\" class=\"button\" alt=\"wedding\" />";
1118 echo "<img src=\"pics/button/wedding_partner_button.png\" class=\"button\" alt=\"wedding partner\" />";
1120 if(ereg("solo",$GT) && $party=="re")
1122 if(ereg("queen",$GT))
1123 echo "<img src=\"pics/button/queensolo_button.png\" class=\"button\" alt=\"$GT\" />";
1124 else if(ereg("jack",$GT))
1125 echo "<img src=\"pics/button/jacksolo_button.png\" class=\"button\" alt=\"$GT\" />";
1126 else if(ereg("club",$GT))
1127 echo "<img src=\"pics/button/clubsolo_button.png\" class=\"button\" alt=\"$GT\" />";
1128 else if(ereg("spade",$GT))
1129 echo "<img src=\"pics/button/spadesolo_button.png\" class=\"button\" alt=\"$GT\" />";
1130 else if(ereg("heart",$GT))
1131 echo "<img src=\"pics/button/heartsolo_button.png\" class=\"button\" alt=\"$GT\" />";
1132 else if(ereg("trumpless",$GT))
1133 echo "<img src=\"pics/button/notrumpsolo_button.png\" class=\"button\" alt=\"$GT\" />";
1134 else if(ereg("trump",$GT))
1135 echo "<img src=\"pics/button/trumpsolo_button.png\" class=\"button\" alt=\"$GT\" />";
1138 /* add point calls */
1142 echo "<img src=\"pics/button/re_button.png\" class=\"button\" alt=\"re\" />";
1144 echo "<img src=\"pics/button/contra_button.png\" class=\"button\" alt=\"contra\" />";
1148 echo "<img src=\"pics/button/0_button.png\" class=\"button\" alt=\"0\" />";
1151 echo "<img src=\"pics/button/30_button.png\" class=\"button\" alt=\"30\" />";
1154 echo "<img src=\"pics/button/60_button.png\" class=\"button\" alt=\"60\" />";
1157 echo "<img src=\"pics/button/90_button.png\" class=\"button\" alt=\"90\" />";
1163 echo " local time: ".date("Y-m-d H:i:s")."<br />\n";
1164 echo " last login: ".date("Y-m-d H:i:s",$lastlogin)."<br />\n";
1170 /* get everything relevant to display the tricks */
1171 $result = mysql_query("SELECT Hand_Card.card_id as card,".
1172 " Hand.position as position,".
1173 " Play.sequence as sequence, ".
1175 " Comment.comment, ".
1176 " Play.create_date, ".
1179 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1180 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1181 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1182 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1183 "WHERE Trick.game_id='".$gameid."' ".
1184 "ORDER BY Trick.id,sequence ASC");
1186 $lasttrick = DB_get_max_trickid($gameid);
1188 $play = array(); /* needed to calculate winner later */
1190 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1191 $firstcard = ""; /* first card in a trick */
1193 echo "\n<ul class=\"tricks\">\n";
1194 echo " <li class=\"nohighlight\"> Game $gameid: </li>\n";
1196 while($r = mysql_fetch_array($result,MYSQL_NUM))
1202 $timeplayed = strtotime($r[5]);
1205 $offset = DB_get_user_timezone($user);
1206 $zone = return_timezone($offset);
1207 date_default_timezone_set($zone);
1209 /* check if first schweinchen has been played */
1210 if($r[0] == 19 || $r[0] == 20 )
1211 $GAME["schweinchen"]++;
1213 /* save card to be able to find the winner of the trick later */
1214 $play[$seq] = array("card"=>$r[0],"pos"=>$pos);
1218 /* first card in a trick, output some html */
1219 if($trick!=$lasttrick)
1221 /* start of an old trick? */
1222 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1223 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1224 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1226 else if($trick==$lasttrick)
1228 /* start of a last trick? */
1229 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1230 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1231 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1234 /* remember first card, so that we are able to check, what cards can be played */
1239 echo " <div class=\"card".($pos-1)."\">\n";
1241 /* display comments */
1243 echo " <span class=\"comment\">".$comment."</span>\n";
1246 display_card($r[0],$PREF["cardset"]);
1248 echo " </div>\n"; /* end div card */
1254 echo " </div>\n </li>\n"; /* end div table, end li table */
1258 if($seq!=4 && $trickNR>1)
1259 echo " </div>\n </li>\n"; /* end div table, end li table */
1263 /* whos turn is it? */
1266 $winner = get_winner($play,$gametype); /* returns the position */
1268 $firstcard = ""; /* new trick, no first card */
1273 if($next==5) $next = 1;
1276 /* my turn?, display cards as links, ask for comments*/
1277 if(DB_get_pos_by_hash($me) == $next)
1282 /* do we want to play a card? */
1283 if(myisset("card") && $myturn)
1285 $card = $_REQUEST["card"];
1286 $handid = DB_get_handid_by_hash($me);
1288 /* check if we have card and that we haven't played it yet*/
1289 /* set played in hand_card to true where hand_id and card_id*/
1290 $result = mysql_query("SELECT id FROM Hand_Card WHERE played='false' and ".
1291 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1292 $r = mysql_fetch_array($result,MYSQL_NUM);
1293 $handcardid = $r[0];
1295 if($handcardid) /* everything ok, play card */
1299 /* update Game timestamp */
1300 DB_update_game_timestamp($gameid);
1302 /* check if a call was made, must do this before we set the card status to played */
1303 if(myisset("call120") && $_REQUEST["call120"] == "yes" && can_call(120,$me))
1304 $result = mysql_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
1305 if(myisset("call90") && $_REQUEST["call90"] == "yes" && can_call(90,$me))
1306 $result = mysql_query("UPDATE Hand SET point_call='90' WHERE hash='$me' ");
1307 if(myisset("call60") && $_REQUEST["call60"] == "yes" && can_call(60,$me))
1308 $result = mysql_query("UPDATE Hand SET point_call='60' WHERE hash='$me' ");
1309 if(myisset("call30") && $_REQUEST["call30"] == "yes" && can_call(30,$me))
1310 $result = mysql_query("UPDATE Hand SET point_call='30' WHERE hash='$me' ");
1311 if(myisset("call0") && $_REQUEST["call0"] == "yes" && can_call(0,$me))
1312 $result = mysql_query("UPDATE Hand SET point_call='0' WHERE hash='$me' ");
1314 /* mark card as played */
1315 mysql_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1316 DB_quote_smart($card));
1318 /* check for schweinchen */
1319 //echo "schweinchen = ".$GAME["schweinchen"]." --$card-<br />";
1320 if($card == 19 || $card == 20 )
1322 $GAME["schweinchen"]++;
1323 if($GAME["schweinchen"]==3 && $RULES["schweinchen"]=="second" )
1324 $comment="Schweinchen! ";
1325 if($RULES["schweinchen"]=="both" )
1326 $comment="Schweinchen! ";
1327 if ($debug) echo "schweinchen = ".$GAME["schweinchen"]." ---<br />";
1330 /* get trick id or start new trick */
1331 $a = DB_get_current_trickid($gameid);
1336 $playid = DB_play_card($trickid,$handcardid,$sequence);
1338 /* if sequence == 4 check who one in case of wedding */
1339 if($sequence == 4 && $GT == "wedding")
1341 /* is wedding resolve */
1342 $resolved = DB_get_sickness_by_gameid($gameid);
1345 /* who has wedding */
1346 $userids = DB_get_all_userid_by_gameid($gameid);
1347 foreach($userids as $user)
1349 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1350 if($usersick == "wedding")
1353 /* who won the trick */
1354 $play = DB_get_cards_by_trick($trickid);
1355 $winner = get_winner($play,$gametype); /* returns the position */
1356 $winnerid = DB_get_userid_by_gameid_and_position($gameid,$winner);
1357 /* is tricknr <=3 */
1358 if($tricknr <=3 && $winnerid!=$whosick)
1360 /* set resolved at tricknr*/
1361 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1363 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1364 DB_set_party_by_hash($whash,"re");
1366 if($tricknr == 3 && $winnerid==$whosick)
1368 /* set resolved at tricknr*/
1369 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1374 /* if sequence == 4, set winner of the trick, count points and set the next player */
1377 $play = DB_get_cards_by_trick($trickid);
1378 $winner = get_winner($play,$gametype); /* returns the position */
1381 mysql_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1383 echo "ERROR during scoring";
1386 echo "DEBUG: position $winner won the trick <br />";
1388 /* who is the next player? */
1393 $next = DB_get_pos_by_hash($me)+1;
1395 if($next==5) $next=1;
1398 /* check for coment */
1399 if(myisset("comment"))
1401 $comment.=$_REQUEST["comment"];
1404 DB_insert_comment($comment,$playid,$myid);
1406 /* display played card */
1407 echo "<div class=\"card\">";
1408 echo " you played <br />";
1409 display_card($card,$PREF["cardset"]);
1412 /*check if we still have cards left, else set status to gameover */
1413 if(sizeof(DB_get_hand($me))==0)
1415 DB_set_hand_status_by_hash($me,'gameover');
1416 $mystatus='gameover';
1419 /* if all players are done, set game status to game over,
1420 * get the points of the last trick and send out an email
1423 $userids = DB_get_all_userid_by_gameid($gameid);
1426 foreach($userids as $user)
1427 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1431 DB_set_game_status_by_gameid($gameid,"gameover");
1433 /* email next player, if game is still running */
1434 if(DB_get_game_status_by_gameid($gameid)=='play')
1436 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1437 $email = DB_get_email_by_hash($next_hash);
1438 $who = DB_get_userid_by_email($email);
1439 DB_set_player_by_gameid($gameid,$who);
1441 $message = "A card has been played in game $gameid.\n\n".
1442 "It's your turn now.\n".
1443 "Use this link to play a card: ".$host."?me=".$next_hash."\n\n" ;
1444 mymail($email,$EmailName."a card has been played in game $gameid",$message);
1446 else /* send out final email */
1448 /* individual score */
1449 $result = mysql_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1450 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1451 " LEFT JOIN User ON User.id=Hand.user_id".
1452 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1453 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1454 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1455 " WHERE Hand.game_id='$gameid'".
1456 " GROUP BY User.fullname" );
1457 $message = "The game is over. Thanks for playing :)\n";
1458 $message .= "Final score:\n";
1459 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1460 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1462 $result = mysql_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1463 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1464 " LEFT JOIN User ON User.id=Hand.user_id".
1465 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1466 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1467 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1468 " WHERE Hand.game_id='$gameid'".
1469 " GROUP BY User.fullname" );
1470 $message .= "\nTotals:\n";
1471 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1472 $message .= " ".$r[0]." ".$r[1]."\n";
1474 /* send out final email */
1477 foreach($userids as $user)
1478 $all[] = DB_get_email_by_userid($user);
1479 $To = implode(",",$all);
1481 $help = "\n\n (you can use reply all on this email to reach all the players.)\n";
1482 mymail($To,$EmailName."game over (game $gameid) part 1(2)",$message.$help);
1484 foreach($userids as $user)
1486 $To = DB_get_email_by_userid($user);
1487 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1489 $link = "Use this link to have a look at game $gameid: ".$host."?me=".$hash."\n\n" ;
1490 mymail($To,$EmailName."game over (game $gameid) part 2(2)",$link);
1496 echo "can't find that card?! <br />\n";
1499 else if(myisset("card") && !$myturn )
1501 echo "please wait until it's your turn! <br />\n";
1504 $mycards = DB_get_hand($me);
1505 $mycards = mysort($mycards,$gametype);
1506 echo "<div class=\"mycards\">\n";
1508 if($myturn && !myisset("card"))
1510 echo "Hello ".$myname.", it's your turn! <br />\n";
1511 echo "Your cards are: <br />\n";
1512 echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
1514 /* do we have to follow suite? */
1516 if(have_suit($mycards,$firstcard))
1519 foreach($mycards as $card)
1521 if($followsuit && !same_type($card,$firstcard))
1522 display_card($card,$PREF["cardset"]);
1524 display_link_card($card,$PREF["cardset"]);
1527 if( can_call(120,$me) )
1528 echo " re/contra (120):".
1529 " <input type=\"radio\" name=\"call120\" value=\"yes\" /> ";
1530 if( can_call(90,$me) )
1532 " <input type=\"radio\" name=\"call90\" value=\"yes\" /> ";
1533 if( can_call(60,$me) )
1535 " <input type=\"radio\" name=\"call60\" value=\"yes\" /> ";
1536 if( can_call(30,$me) )
1538 " <input type=\"radio\" name=\"call30\" value=\"yes\" /> ";
1539 if( can_call(0,$me) )
1541 " <input type=\"radio\" name=\"call0\" value=\"yes\" /> ".
1543 " <input type=\"radio\" name=\"call0\" value=\"no\" /> ";
1545 echo "<br />\nA short comments:<input name=\"comment\" type=\"text\" size=\"30\" maxlength=\"50\" />\n";
1546 echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />\n";
1547 echo "<input type=\"submit\" value=\"move\" />\n";
1550 else if($mystatus=='play')
1552 echo "Your cards are: <br />\n";
1553 foreach($mycards as $card)
1554 display_card($card,$PREF["cardset"]);
1556 else if($mystatus=='gameover')
1558 $oldcards = DB_get_all_hand($me);
1559 $oldcards = mysort($oldcards,$gametype);
1560 echo "Your cards were: <br />\n";
1561 foreach($oldcards as $card)
1562 display_card($card,$PREF["cardset"]);
1564 $userids = DB_get_all_userid_by_gameid($gameid);
1565 foreach($userids as $user)
1567 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1573 $name = DB_get_name_by_userid($user);
1574 $oldcards = DB_get_all_hand($userhash);
1575 $oldcards = mysort($oldcards,$gametype);
1576 echo "$name's cards were: <br />\n";
1577 foreach($oldcards as $card)
1578 display_card($card,$PREF["cardset"]);
1584 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1585 if($mystatus=='play')
1587 /* the following happens only when the gamestatus is 'gameover' */
1588 /* check if game is over, display results */
1589 if(DB_get_game_status_by_gameid($gameid)=='play')
1591 echo "the game is over for you.. other people still need to play though";
1595 echo "the game is over now...<br />\n";
1597 $result = mysql_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1598 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1599 " LEFT JOIN User ON User.id=Hand.user_id".
1600 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1601 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1602 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1603 " WHERE Hand.game_id='$gameid'".
1604 " GROUP BY User.fullname" );
1605 echo "Final Score:<br />\n".
1607 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1608 echo " <tr><td> ".$r[0]."</td><td>(".$r[2].")</td><td> ".$r[1]."</td></tr>";
1612 $result = mysql_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1613 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1614 " LEFT JOIN User ON User.id=Hand.user_id".
1615 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1616 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1617 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1618 " WHERE Hand.game_id='$gameid'".
1619 " GROUP BY Hand.party" );
1620 echo "Totals:<br />\n".
1622 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1623 echo " <tr><td>".$r[0]."</td><td> ".$r[1]."</td></tr>\n";
1626 $session = DB_get_session_by_gameid($gameid);
1627 $result = mysql_query("SELECT id,create_date FROM Game".
1628 " WHERE session=$session".
1629 " ORDER BY create_date DESC".
1633 $r = mysql_fetch_array($result,MYSQL_NUM);
1635 if(!$session || $gameid==$r[0])
1637 /* suggest a new game with the same people in it, just rotated once */
1638 $names = DB_get_all_names_by_gameid($gameid);
1639 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
1644 myerror("error in testing the status");
1650 /* user status page */
1651 else if(myisset("email","password"))
1653 /* test id and password, should really be done in one step */
1654 $email = $_REQUEST["email"];
1655 $password = $_REQUEST["password"];
1658 if(myisset("forgot"))
1662 $uid = DB_get_userid_by_email($email);
1668 /* check how many entries in recovery table */
1669 $number = DB_get_number_of_passwords_recovery($uid);
1671 /* if less than N recent ones, add a new one and send out email */
1674 echo "Ok, I send you a new password. <br />";
1676 echo "N.B. You tried this already $number times during the last day and it will only work ".
1677 " 5 times during a day.<br />";
1678 echo "The new password will be valid for one day, make sure you reset it to something else.<br />";
1679 echo "Back to the <a href=\"$host\">main page</a>.";
1681 $TIME = (string) time(); /* to avoid collisions */
1682 $hash = md5("Anewpassword".$email.$TIME);
1683 $newpw = substr($hash,1,8);
1685 $message = "Someone (hopefully you) requested a new password. \n".
1686 "You can use this email and the following password: \n".
1688 "to log into the server. The new password is valid for 24h, so make\n".
1689 "sure you reset your password to something new. Your old password will\n".
1690 " also still be valid until you set a new one\n";
1691 mymail($email,$EmailName."recovery ",$message);
1693 DB_set_recovery_password($uid,md5($newpw));
1697 echo "Sorry you already tried 5 times during the last 24h.<br />".
1698 "You need to use one of those passwords or wait to get a new one.<br />";
1699 echo "Back to the <a href=\"$host\">main page</a>.";
1705 echo "You need to give me an email address! <br />".
1706 "Please try <a href=\"$host\">again</a>.";
1708 echo "Couldn't find a player with this email! <br />".
1709 "Please contact Arun, if you think this is a mistake <br />".
1710 "or else try <a href=\"$host\">again</a>.";
1715 /* verify password and email */
1716 if(strlen($password)!=32)
1717 $password = md5($password);
1720 $uid = DB_get_userid_by_email_and_password($email,$password);
1728 if(myisset("setpref"))
1730 $setpref=$_REQUEST["setpref"];
1734 case "englishcards":
1735 $result = mysql_query("SELECT * from User_Prefs".
1736 " WHERE user_id='$uid' AND pref_key='cardset'" );
1737 if( mysql_fetch_array($result,MYSQL_NUM))
1738 $result = mysql_query("UPDATE User_Prefs SET value=".DB_quote_smart($setpref).
1739 " WHERE user_id='$uid' AND pref_key='cardset'" );
1741 $result = mysql_query("INSERT INTO User_Prefs VALUES(NULL,'$uid','cardset',".
1742 DB_quote_smart($setpref).")");
1743 echo "Ok, changed you preferences for the cards.\n";
1747 else if(myisset("passwd"))
1749 if( $_REQUEST["passwd"]=="ask" )
1751 /* reset password form*/
1752 output_password_recovery($email,$password);
1754 else if($_REQUEST["passwd"]=="set")
1756 /* reset password */
1759 /* check if old password matches */
1760 if($password != md5($_REQUEST["password0"]))
1762 /* check if new passwords are types the same twice */
1763 if($_REQUEST["password1"] != $_REQUEST["password2"] )
1769 echo "The new passwords don't match. <br />";
1772 echo "The old password is not correct. <br />";
1775 echo "Changed the password.<br />";
1776 mysql_query("UPDATE User SET password='".md5($_REQUEST["password1"]).
1777 "' WHERE id=".DB_quote_smart($uid));
1783 else /* output default user page */
1785 $time = DB_get_user_timestamp($uid);
1786 $unixtime =strtotime($time);
1788 $offset = DB_get_user_timezone($uid);
1789 $zone = return_timezone($offset);
1790 date_default_timezone_set($zone);
1792 /* display links to settings */
1793 output_user_settings($email,$password);
1795 echo "last login: ".date("r",$unixtime)."<br />";
1797 DB_update_user_timestamp($uid);
1799 echo "<p>These are your games that haven't started yet:<br />\n";
1800 $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.mod_date,Game.player from Hand".
1801 " LEFT JOIN Game On Hand.game_id=Game.id".
1802 " WHERE Hand.user_id='$uid' AND Game.status='pre'" );
1803 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1805 echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a>";
1806 if($r[3]==$uid || $r[3]==NULL)
1807 echo "(it's <strong>your</strong> turn)\n";
1810 $name = DB_get_name_by_userid($r[3]);
1811 echo "(it's $name's turn)\n";
1814 if(time()-strtotime($r[2]) > 60*60*24*30)
1815 echo " The game has been running for over a month.".
1816 " Do you want to cancel it? <a href=\"$host?cancle=1&me=".$r[0]."\">yes</a>".
1817 " (clicking here is final and can't be restored)";
1822 echo "<p>These are the games you are playing in:<br />\n";
1823 $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.mod_date,Game.player from Hand".
1824 " LEFT JOIN Game On Hand.game_id=Game.id".
1825 " WHERE Hand.user_id='$uid' AND Game.status='play'" );
1826 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1828 echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a>";
1832 echo "(it's <strong>your</strong> turn)\n";
1835 $name = DB_get_name_by_userid($r[3]);
1836 echo "(it's $name's turn)\n";
1839 if(time()-strtotime($r[2]) > 60*60*24*30)
1840 echo " The game has been running for over a month.".
1841 " Do you want to cancel it? <a href=\"$host?cancle=1&me=".$r[0]."\">yes</a>".
1842 " (clicking here is final and can't be restored)";
1848 echo "<p>And these are your games that are already done:<br />Game: \n";
1850 $result = mysql_query("SELECT hash,game_id from Hand WHERE user_id='$uid' AND status='gameover'" );
1851 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1852 $output[] = "<a href=\"".$host."?me=".$r[0]."\">#".$r[1]." </a>";
1853 echo implode(", ",$output)."</p>\n";
1855 $names = DB_get_all_names();
1856 echo "<p>Registered players:<br />\n";
1857 echo implode(", ",$names)."\n";
1860 echo "<p>Want to start a new game? Visit <a href=\"".$host."?new\">this page.</a></p>";
1865 echo "Sorry email and password don't match <br />";
1872 /* page for registration */
1873 else if(myisset("register") )
1877 /* new user wants to register */
1878 else if(myisset("Rfullname","Remail","Rpassword","Rtimezone") )
1881 if(DB_get_userid_by_name($_REQUEST["Rfullname"]))
1883 echo "please chose another name<br />";
1886 if(DB_get_userid_by_email($_REQUEST["Remail"]))
1888 echo "this email address is already used ?!<br />";
1893 $r=mysql_query("INSERT INTO User VALUES(NULL,".DB_quote_smart($_REQUEST["Rfullname"]).
1894 ",".DB_quote_smart($_REQUEST["Remail"]).
1895 ",".DB_quote_smart(md5($_REQUEST["Rpassword"])).
1896 ",".DB_quote_smart($_REQUEST["Rtimezone"]).",NULL)");
1899 echo " added you to the database";
1901 echo " something went wrong";
1904 /* default login page */
1907 $pre[0]=0;$game[0]=0;$done[0]=0;
1908 $r=mysql_query("SELECT COUNT(id) FROM Game GROUP BY status");
1910 $pre = mysql_fetch_array($r,MYSQL_NUM);
1911 $game = mysql_fetch_array($r,MYSQL_NUM);
1912 $done = mysql_fetch_array($r,MYSQL_NUM);
1915 $r=mysql_query("SELECT AVG(datediff(mod_date,create_date)) FROM Game where status='gameover' ");
1917 $avgage= mysql_fetch_array($r,MYSQL_NUM);
1921 output_home_page($pre[0],$game[0],$done[0],$avgage[0]);