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))
13 /* in case work has to be done on the database or other section we can
14 * shut down the server and tell people to come back later
19 echo "Working on the database...please check back in a few mintues";
27 echo "Database error, can't connect...";
32 /* done major error checking, output header of HTML page */
35 /* check if we want to start a new game */
38 $names = DB_get_all_names();
39 output_form_for_new_game($names);
41 /*check if everything is ready to set up a new game */
42 else if( myisset("PlayerA", "PlayerB","PlayerC","PlayerD","dullen","schweinchen" ))
44 $PlayerA = $_REQUEST["PlayerA"];
45 $PlayerB = $_REQUEST["PlayerB"];
46 $PlayerC = $_REQUEST["PlayerC"];
47 $PlayerD = $_REQUEST["PlayerD"];
49 $dullen = $_REQUEST["dullen"];
50 $schweinchen = $_REQUEST["schweinchen"];
52 $EmailA = DB_get_email_by_name($PlayerA);
53 $EmailB = DB_get_email_by_name($PlayerB);
54 $EmailC = DB_get_email_by_name($PlayerC);
55 $EmailD = DB_get_email_by_name($PlayerD);
57 if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
59 echo "couldn't find one of the names, please start a new game";
65 $useridA = DB_get_userid_by_name($PlayerA);
66 $useridB = DB_get_userid_by_name($PlayerB);
67 $useridC = DB_get_userid_by_name($PlayerC);
68 $useridD = DB_get_userid_by_name($PlayerD);
70 /* create random numbers */
71 $randomNR = create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD);
72 $randomNRstring = join(":",$randomNR);
74 /* get ruleset information or create new one */
75 $ruleset = DB_get_ruleset($dullen,$schweinchen);
78 echo "Error defining ruleset: $ruleset";
86 if(myisset("followup") )
88 $followup= $_REQUEST["followup"];
89 $session = DB_get_session_by_gameid($followup);
90 $ruleset = DB_get_ruleset_by_gameid($followup); /* just copy ruleset from old game,
91 this way no manipulation is possible */
93 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1','1','pre',".
94 "'$ruleset','$session' ,NULL)");
98 $max = DB_get_max_session();
100 mysql_query("UPDATE Game SET session='".$max."' WHERE id=".DB_quote_smart($followup));
101 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1','1','pre',".
102 "'$ruleset','$max' ,NULL)");
106 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1','1','pre', ".
107 "'$ruleset',NULL ,NULL)");
108 $game_id = mysql_insert_id();
111 $TIME = (string) time(); /* to avoid collisions */
112 $hashA = md5("AGameOfDoko".$game_id.$PlayerA.$EmailA.$TIME);
113 $hashB = md5("AGameOfDoko".$game_id.$PlayerB.$EmailB.$TIME);
114 $hashC = md5("AGameOfDoko".$game_id.$PlayerC.$EmailC.$TIME);
115 $hashD = md5("AGameOfDoko".$game_id.$PlayerD.$EmailD.$TIME);
118 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridA).
119 ", ".DB_quote_smart($hashA).", 'start','1',NULL,NULL,NULL,NULL)");
120 $hand_idA = mysql_insert_id();
121 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridB).
122 ", ".DB_quote_smart($hashB).", 'start','2',NULL,NULL,NULL,NULL)");
123 $hand_idB = mysql_insert_id();
124 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridC).
125 ", ".DB_quote_smart($hashC).", 'start','3',NULL,NULL,NULL,NULL)");
126 $hand_idC = mysql_insert_id();
127 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridD).
128 ", ".DB_quote_smart($hashD).", 'start','4',NULL,NULL,NULL,NULL)");
129 $hand_idD = mysql_insert_id();
133 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idA', '".$randomNR[$i]."', 'false')");
134 for($i=12;$i<24;$i++)
135 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idB', '".$randomNR[$i]."', 'false')");
136 for($i=24;$i<36;$i++)
137 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idC', '".$randomNR[$i]."', 'false')");
138 for($i=36;$i<48;$i++)
139 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idD', '".$randomNR[$i]."', 'false')");
141 /* send out email, TODO: check for error with email */
143 "you are invited to play a game of DoKo (that is to debug the program ;).\n".
144 "Place comments and bug reports here:\n".
145 "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
146 "The whole round would consist of the following players:\n".
151 "If you want to join this game, please follow this link:\n\n".
154 mymail($EmailA,"You are invited to a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
155 mymail($EmailB,"You are invited to a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
156 mymail($EmailC,"You are invited to a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
157 mymail($EmailD,"You are invited to a game of DoKo","Hello $PlayerD,\n".$message.$hashD);
159 echo "You started a new game. The emails have been sent out!";
160 } /* end set up a new game */
161 /* cancle a game, if nothing has happend in the last N minutes */
162 else if(myisset("cancle","me"))
164 $me = $_REQUEST["me"];
166 /* test for valid ID */
167 $myid = DB_get_userid_by_hash($me);
170 echo "Can't find you in the database, please check the url.<br />\n";
171 echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
177 DB_update_user_timestamp($myid);
179 /* get some information from the DB */
180 $gameid = DB_get_gameid_by_hash($me);
181 $myname = DB_get_name_by_hash($me);
183 /* check if game really is old enough */
184 $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
185 $r = mysql_fetch_array($result,MYSQL_NUM);
186 if(time()-strtotime($r[0]) > 60*60*24*30) /* = 1 month */
188 $message = "Hello, \n\n".
189 "Game $gameid has been cancled since nothing happend for a while and $myname requested it.\n";
191 $userids = DB_get_all_userid_by_gameid($gameid);
192 foreach($userids as $user)
194 $To = DB_get_email_by_userid($user);
195 mymail($To,$EmailName."game $gameid cancled (timed out)",$message);
198 /* delete everything from the dB */
201 echo "<p style=\"background-color:red\";>Game $gameid has been cancled.<br /><br /></p>";
204 echo "<p>You need to wait longer before you can cancle a game...</p>\n";
206 /* handle request from one specific player for one game,
207 * (the hash is set on a per game base) */
208 else if(myisset("me"))
210 $me = $_REQUEST["me"];
212 /* test for valid ID */
213 $myid = DB_get_userid_by_hash($me);
216 echo "Can't find you in the database, please check the url.<br />\n";
217 echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
223 /* the user had done something, update the timestamp */
224 DB_update_user_timestamp($myid);
226 /* get some information from the DB */
227 $gameid = DB_get_gameid_by_hash($me);
228 $myname = DB_get_name_by_hash($me);
229 $mystatus = DB_get_status_by_hash($me);
230 $mypos = DB_get_pos_by_hash($me);
231 $myhand = DB_get_handid_by_hash($me);
232 $session = DB_get_session_by_gameid($gameid);
234 /* get prefs and save them */
239 /* get rule set for this game */
240 $result = mysql_query("SELECT * FROM Rulesets".
241 " LEFT JOIN Game ON Game.ruleset=Rulesets.id ".
242 " WHERE Game.id='$gameid'" );
243 $r = mysql_fetch_array($result,MYSQL_NUM);
245 $RULES["dullen"] = $r[2];
246 $RULES["schweinchen"] = $r[3];
247 $RULES["call"] = $r[4];
250 /* get some infos about the game */
251 $gametype = DB_get_gametype_by_gameid($gameid);
252 $gamestatus = DB_get_game_status_by_gameid($gameid);
254 if($gametype=="solo")
256 $gametype = DB_get_solo_by_gameid($gameid);
257 $GT = $gametype." ".$GT;
260 /* display rule set for this game */
261 echo "<div class=\"ruleset\">\n";
263 if($gamestatus != 'pre')
264 echo " Gametype: $GT <br />\n";
266 echo "Rules: <br />\n";
267 echo "10ofhearts : ".$r[2]."<br />\n";
268 echo "schweinchen: ".$r[3]."<br />\n";
269 echo "call: ".$r[4]."<br />\n";
272 /* output extra division in case this game is part of a session */
275 echo "<div class=\"session\">\n".
276 "This game is part of session $session: \n";
277 $hashes = DB_get_hashes_by_session($session,$myid);
279 foreach($hashes as $hash)
284 echo "<a href=\"".$host."?me=".$hash."\">$i</a> ";
291 /* does anyone have both foxes */
292 $GAME["schweinchen"]=0;
295 $hash = DB_get_hash_from_game_and_pos($gameid,$i);
296 $cards = DB_get_all_hand($hash);
297 if( in_array("19",$cards) && in_array("20",$cards) )
299 $GAME["schweinchen"]=1;
300 $GAME["schweinchen-who"]=$hash;
304 /* mystatus gets the player through the different stages of a game.
306 * init: check values from start,
308 * check: check for return values from init
309 * poverty: handle poverty, wait here until all player have reached this state
310 * display sickness and move on to game
311 * play: game in progress
312 * gameover: are we revisiting a game
317 check_want_to_play($me);
318 /* move on to the next stage*/
319 DB_set_hand_status_by_hash($me,'init');
322 /* first check if everything went ok in the last step
323 * if not, send user back, if yes, check what he did
327 echo "<p> you need to answer the <a href=\"$host?me=$me\">question</a>.</p>";
328 DB_set_hand_status_by_hash($me,'start');
332 if($_REQUEST["in"] == "no")
334 /* cancle the game */
335 $message = "Hello, \n\n".
336 "the game has been canceled due to the request of one of the players.\n";
338 $userids = DB_get_all_userid_by_gameid($gameid);
339 foreach($userids as $user)
341 $To = DB_get_email_by_userid($user);
342 mymail($To,$EmailName."game $gameid canceled",$message);
345 /* delete everything from the dB */
350 echo "thanks for joining the game...";
352 $mycards = DB_get_hand($me);
354 echo "<p class=\"mycards\" style=\"margin-top:8em;\">your cards are: <br />\n";
355 foreach($mycards as $card)
356 display_card($card,$PREF["cardset"]);
359 output_check_for_sickness($me,$mycards);
361 /* move on to the next stage*/
362 DB_set_hand_status_by_hash($me,'check');
368 /* ok, user is in the game, saw his cards and selected his vorbehalt
369 * so first we check what he selected
371 echo "Processing what you selected in the last step...<br />";
373 if(!myisset("solo","wedding","poverty","nines") )
375 /* all these variables have a pre-selected default,
376 * so we should never get here,
377 * unless a user tries to cheat ;) */
378 echo "something went wrong...please contact the admin.";
382 /* check if this sickness needs to be handled first */
383 $gametype = DB_get_gametype_by_gameid($gameid);
384 $startplayer = DB_get_startplayer_by_gameid($gameid);
386 if( $_REQUEST["solo"]!="No")
388 /* user wants to play a solo */
390 /* store the info in the user's hand info */
391 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
392 DB_set_sickness_by_hash($me,"solo");
394 echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
396 if($gametype == "solo" && $startplayer<$mypos)
397 {}/* do nothing, since someone else already is playing solo */
400 /* this solo comes first
401 * store info in game table
403 DB_set_gametype_by_gameid($gameid,"solo");
404 DB_set_startplayer_by_gameid($gameid,$mypos);
405 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
408 else if($_REQUEST["wedding"] == "yes")
410 /* TODO: add silent solo somewhere*/
411 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
412 DB_set_sickness_by_hash($me,"wedding");
414 else if($_REQUEST["poverty"] == "yes")
416 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
417 DB_set_sickness_by_hash($me,"poverty");
419 else if($_REQUEST["nines"] == "yes")
421 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
422 " is playing solo, this game will be canceled.<br />\n";
423 DB_set_sickness_by_hash($me,"nines");
426 echo " Ok, done with checking, please go to the <a href=\"$host?me=$me\">next step of the setup</a>.<br />";
428 /* move on to the next stage*/
429 DB_set_hand_status_by_hash($me,'poverty');
431 /* check if everyone has reached this stage, send out email */
432 $userids = DB_get_all_userid_by_gameid($gameid);
434 foreach($userids as $user)
436 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
437 if($userstat!='poverty' && $userstat!='play')
441 foreach($userids as $user)
443 $To = DB_get_email_by_userid($user);
444 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
447 $message = "Everyone finish the questionary in game $gameid, ".
448 "please visit this link now to continue: \n".
449 " ".$host."?me=".$userhash."\n\n" ;
450 mymail($To,$EmailName." finished setup in game $gameid",$message);
458 /* here we need to check if there is a solo or some other form of sickness.
459 * If so, which one is the most important one
460 * set that one in the Game table
461 * tell people about it.
463 echo "<br /> Checking if someone else selected solo, nines or wedding or poverty.<br />";
465 /* check if everyone has reached this stage */
466 $userids = DB_get_all_userid_by_gameid($gameid);
468 foreach($userids as $user)
470 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
471 if($userstat!='poverty' && $userstat!='play')
477 echo "This step can only be handled after everyone finished the last step. ".
478 "Seems like this is not the case, so you need to wait a bit... ".
479 "you will get an email once that is the case, please use the link in ".
480 "that email to continue the game.<br />";
484 echo "Everyone has finished checking their cards, let's see what they said...<br />";
486 /* check what kind of game we are playing, in case there are any solos this already
487 *will have the correct information in it */
488 $gametype = DB_get_gametype_by_gameid($gameid);
489 $startplayer = DB_get_startplayer_by_gameid($gameid);
491 /* check for different sickness and just output a general info */
496 foreach($userids as $user)
498 $name = DB_get_name_by_userid($user);
499 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
500 if($usersick == 'nines')
503 echo "$name has a Vorbehalt. <br />";
506 else if($usersick == 'poverty')
509 echo "$name has a Vorbehalt. <br />";
511 else if($usersick == 'wedding')
514 echo "$name has a Vorbehalt. <br />" ;
516 else if($usersick == 'solo')
519 echo "$name has a Vorbehalt. <br />" ;
523 /* now check which sickness comes first and set the gametype to it */
525 if($gametype == "solo")
532 /* TODO: should we keep statistics of this? */
533 $message = "Hello, \n\n".
534 "the game has been canceled because ".DB_get_name_by_userid($nines).
535 " has five or more nines and nobody is playing solo.\n";
537 /* TODO: add info about redeal in case this is a game of a series */
539 $userids = DB_get_all_userid_by_gameid($gameid);
540 foreach($userids as $user)
542 $To = DB_get_email_by_userid($user);
543 mymail($To,$EmailName."game $gameid canceled",$message);
546 /* delete everything from the dB */
549 echo "The game has been canceled because ".DB_get_name_by_userid($nines).
550 " has five or more nines and nobody is playing solo.\n";
555 else if($poverty==1) /* one person has poverty */
557 DB_set_gametype_by_gameid($gameid,"poverty");
558 $gametype = "poverty";
559 $who = DB_get_sickness_by_gameid($gameid);
562 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
563 if($firstsick == "poverty")
564 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
566 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
569 else if($poverty==2) /* two people have poverty */
571 DB_set_gametype_by_gameid($gameid,"dpoverty");
572 $gametype = "dpoverty";
573 $who = DB_get_sickness_by_gameid($gameid);
576 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
577 if($firstsick == "poverty")
579 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
580 if($secondsick == "poverty")
581 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
583 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
586 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
591 DB_set_gametype_by_gameid($gameid,"wedding");
592 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
593 $gametype = "wedding";
598 /* now the gametype is set correctly (shouldn't matter that this is calculated for every user)
599 * output what kind of game we have */
602 foreach($userids as $user)
604 /* userids are sorted by position...
605 * so output whatever the firstone has, then whatever the next one has
606 * stop when the sickness is the same as the gametype
609 $name = DB_get_name_by_userid($user);
610 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
613 echo "$name has $usersick. <br />"; /*TODO: perhaps save this in a string and store in Game? */
615 if($usersick=="poverty")
617 if($usersick == "wedding" && $gametype =="wedding")
619 if($usersick == "poverty" && $gametype =="poverty")
621 if($usersick == "poverty" && $gametype =="dpoverty" && $poverty==2)
623 if($usersick == "solo" && $gametype =="solo")
627 /* output Schweinchen in case the rules need it */
628 if( $gametype != "solo")
629 if($GAME["schweinchen"] && $RULES["schweinchen"]=="both" )
630 echo DB_get_name_by_hash($GAME["schweinchen-who"])." has Schweinchen. <br />";
634 /* finished the setup, set re/contra parties if possible, go to next stage unless there is a case of poverty*/
638 /* are we the solo player? set us to re, else set us to contra */
639 $pos = DB_get_pos_by_hash($me);
640 if($pos == $startplayer)
641 DB_set_party_by_hash($me,"re");
643 DB_set_party_by_hash($me,"contra");
644 DB_set_hand_status_by_hash($me,'play');
648 /* set person with the wedding to re, do the rest during the game */
649 $usersick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
650 if($usersick == "wedding")
651 DB_set_party_by_hash($me,"re");
653 DB_set_party_by_hash($me,"contra");
655 echo "Whoever will make the first trick will be on the re team. <br />\n";
656 echo " Ok, the game can start now, please finish <a href=\"$host?me=$me\">the setup</a>.<br />";
657 DB_set_hand_status_by_hash($me,'play');
661 $hand = DB_get_all_hand($me);
663 if(in_array('3',$hand)||in_array('4',$hand))
664 DB_set_party_by_hash($me,"re");
666 DB_set_party_by_hash($me,"contra");
667 DB_set_hand_status_by_hash($me,'play');
671 /* check if poverty resolved (e.g. DB.Game who set to NULL)
672 * yes? =>trump was taken, start game; break;
674 $who = DB_get_sickness_by_gameid($gameid);
676 { /* trump has been taken */
677 DB_set_hand_status_by_hash($me,'play');
681 if($who>9) /*= two people still have trump on the table*/
686 /* check if we are being asked now
687 * no? display wait message, e.g. player X is asked at the moment
689 $usersick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
690 if(myisset("trump") && $_REQUEST["trump"]=="no" && ($who==$mypos || $who==$mypos*10))
692 /* user doesn't want to take trump */
693 /* set next player who needs to be asked */
694 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
695 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
697 if($firstsick=="poverty")
699 if($secondsick=="poverty")
700 DB_set_sickness_by_gameid($gameid,$who+$add*3);
702 DB_set_sickness_by_gameid($gameid,$who+$add*2);
705 DB_set_sickness_by_gameid($gameid,$who+$add);
707 /* email next player */
708 $who = DB_get_sickness_by_gameid($gameid);
709 if($who>9) $who = $who/10;
713 $To = DB_get_email_by_pos_and_gameid($who,$gameid);
714 $userhash = DB_get_hash_from_game_and_pos($gameid,$who);
715 DB_set_player_by_gameid($gameid,$who);
717 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
718 " ".$host."?me=".$userhash."\n\n" ;
719 mymail($To,$EmailName." poverty (game $gameid)",$message);
722 /* this user is done */
723 DB_set_hand_status_by_hash($me,'play');
726 else if(myisset("trump") && !myisset("exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
728 /* user wants to take trump */
729 $trump = $_REQUEST["trump"];
731 /* get hand id for user $trump */
732 $userhand = DB_get_handid_by_gameid_and_userid($gameid,$trump);
733 /* copy trump from player A to B */
734 $result = mysql_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
736 /* add hidden button with trump in it to get to the next point */
737 echo "<form action=\"index.php\" method=\"post\">\n";
738 echo " <input type=\"hidden\" name=\"exchange\" value=\"-1\" />\n";
739 echo " <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
740 echo " <input type=\"hidden\" name=\"me\" value=\"".$me."\" />\n";
741 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select cards to give back\" />\n";
744 else if(myisset("trump","exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
746 $trump = $_REQUEST["trump"];
747 $exchange = $_REQUEST["exchange"];
748 $userhand = DB_get_handid_by_gameid_and_userid($gameid,$trump);
750 /* if exchange is set to a value>0, exchange that card back to user $trump */
753 $result = mysql_query("UPDATE Hand_Card SET hand_id='$userhand'".
754 " WHERE hand_id='$myhand' AND card_id='$exchange'" );
757 /* if number of cards == 12, set status to play for both users */
758 $result = mysql_query("SELECT COUNT(*) FROM Hand_Card WHERE hand_id='$myhand'" );
759 $r = mysql_fetch_array($result,MYSQL_NUM);
762 die("error in poverty");
766 if($gametype=="poverty" || $who<9)
768 DB_set_sickness_by_gameid($gameid,-1); /* done with poverty */
770 else /* reduce poverty count by one, that is go to single digits $who */
775 /* whom to ask next */
776 $firstsick = DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
777 $secondsick = DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
779 if($firstsick!="poverty")
780 DB_set_sickness_by_gameid($gameid,$who+$add);
783 if($secondsick!="poverty")
784 DB_set_sickness_by_gameid($gameid,$who+$add*2);
786 DB_set_sickness_by_gameid($gameid,$who+$add*3);
789 /* email next player */
790 $who = DB_get_sickness_by_gameid($gameid);
793 $To = DB_get_email_by_pos_and_gameid($who,$gameid);
794 $userhash = DB_get_hash_from_game_and_pos($gameid,$who);
795 DB_set_player_by_gameid($gameid,$who);
797 $message = "Someone has poverty, it's your turn to decide, ".
798 "if you want to take the trump. Please visit:".
799 " ".$host."?me=".$userhash."\n\n" ;
800 mymail($To,$EmailName." poverty (game $gameid)",$message);
805 /* this user is done */
806 DB_set_hand_status_by_hash($me,'play');
807 /* and so is his partner */
808 $hash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
809 DB_set_hand_status_by_hash($hash,'play');
811 /* set party to re, unless we had dpoverty, in that case check if we need to set re/contra*/
813 foreach($userids as $user)
815 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
816 $party = DB_get_party_by_hash($userhash);
822 DB_set_party_by_hash($me,"contra");
823 DB_set_party_by_hash($hash,"contra");
827 foreach($userids as $user)
829 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
830 if($userhash==$hash||$userhash==$me)
831 DB_set_party_by_hash($userhash,"re");
833 DB_set_party_by_hash($userhash,"contra");
842 /* else show all trump, have lowest card pre-selected, have hidden setting for */
843 echo "you need to get rid of a few cards<br />\n";
845 set_gametype($gametype); /* this sets the $CARDS variable */
846 $mycards = DB_get_hand($me);
847 $mycards = mysort($mycards,$gametype);
849 echo "<form class=\"exchange\" action=\"index.php\" method=\"post\">\n";
851 foreach($mycards as $card)
852 display_link_card($card,$PREF["cardset"],$type);
853 echo " <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
854 echo " <input type=\"hidden\" name=\"me\" value=\"".$me."\" />\n";
855 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select one card to give back\" />\n";
859 else if($who == $mypos || $who == $mypos*10)
861 foreach($userids as $user)
863 $name = DB_get_name_by_userid($user);
864 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
866 if($usersick=="poverty")
868 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
869 $cards = DB_get_hand($hash);
870 $nrtrump = count_trump($cards);
873 echo "Player $name has $nrtrump trump. Do you want to take them?".
874 "<a href=\"index.php?me=$me&trump=$user\">yes</a> <br />";
877 echo "<a href=\"index.php?me=$me&trump=no\">No,way I take those trump...</a> <br />";
879 echo "Your cards are: <br />\n";
880 $mycards = DB_get_hand($me);
882 echo "<p class=\"mycards\" style=\"margin-top:8em;\">your cards are: <br />\n";
883 foreach($mycards as $card)
884 display_card($card,$PREF["cardset"]);
889 $mysick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
890 if($mysick=="poverty")
891 echo "The others are asked if they want to take your trump, you have to wait (you'll get an email).";
893 echo "it's not your turn yet to decide if you want to take the trump or not.";
898 /* check if no one wanted to take trump, in that case the gamesickness would be set to 5 or 50 */
899 $who = DB_get_sickness_by_gameid($gameid);
900 if($who==5 || $who==50)
902 $message = "Hello, \n\n".
903 "Game $gameid has been cancled since nobody wanted to take the trump.\n";
905 $userids = DB_get_all_userid_by_gameid($gameid);
906 foreach($userids as $user)
908 $To = DB_get_email_by_userid($user);
909 mymail($To,$EmailName."game $gameid cancled (poverty not resolved)",$message);
912 /* delete everything from the dB */
915 echo "<p style=\"background-color:red\";>Game $gameid has been cancled.<br /><br /></p>";
921 /* check if all players are ready to play */
923 foreach($userids as $user)
924 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='play')
929 /* only set this after all poverty, etc. are handled*/
930 DB_set_game_status_by_gameid($gameid,'play');
932 /* email startplayer */
933 $startplayer = DB_get_startplayer_by_gameid($gameid);
934 $email = DB_get_email_by_pos_and_gameid($startplayer,$gameid);
935 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
936 $who = DB_get_userid_by_email($email);
937 DB_set_player_by_gameid($gameid,$who);
941 /* email startplayer) */
942 $message = "It's your turn now in game $gameid.\n".
943 "Use this link to play a card: ".$host."?me=".$hash."\n\n" ;
944 mymail($email,$EmailName."ready, set, go... (game $gameid) ",$message);
947 echo " Please, <a href=\"$host?me=$me\">start</a> the game.<br />";
955 /* both entries here, so that the tricks are visible for both.
956 * in case of 'play' there is a break later that skips the last part
959 /* figure out what kind of game we are playing,
960 * set the global variables $CARDS["trump"],$CARDS["diamonds"],$CARDS["hearts"],
961 * $CARDS["clubs"],$CARDS["spades"],$CARDS["foxes"]
965 $gametype = DB_get_gametype_by_gameid($gameid);
967 if($gametype=="solo")
969 $gametype = DB_get_solo_by_gameid($gameid);
970 $GT = $gametype." ".$GT;
973 $gametype = "normal";
975 set_gametype($gametype); /* this sets the $CARDS variable */
977 /* get some infos about the game */
978 $gamestatus = DB_get_game_status_by_gameid($gameid);
980 /* display useful things in divs */
982 /* display links to the users status page */
983 $result = mysql_query("SELECT email,password from User WHERE id='$myid'" );
984 $r = mysql_fetch_array($result,MYSQL_NUM);
986 display_links($r[0],$r[1]);
988 /* end display useful things*/
990 /* has the game started? No, then just wait here...*/
991 if($gamestatus == 'pre')
993 echo "You finished the setup, but not everyone else finished it... ".
994 "so you need to wait for the others. Just wait for the an email... <br />";
995 break; /* not sure this works... the idea is that you can
996 * only play a card after everyone is ready to play */
999 /* display the table and the names */
1000 $result = mysql_query("SELECT User.fullname as name,".
1001 " Hand.position as position, ".
1003 " Hand.party as party, ".
1004 " Hand.sickness as sickness, ".
1005 " Hand.point_call, ".
1006 " User.last_login, ".
1009 "LEFT JOIN User ON User.id=Hand.user_id ".
1010 "WHERE Hand.game_id='".$gameid."' ".
1011 "ORDER BY position ASC");
1013 echo "<div class=\"table\">\n".
1014 " <img src=\"pics/table.png\" alt=\"table\" />\n";
1015 while($r = mysql_fetch_array($result,MYSQL_NUM))
1023 $lastlogin = strtotime($r[6]);
1026 $offset = DB_get_user_timezone($user);
1027 $zone = return_timezone($offset);
1028 date_default_timezone_set($zone);
1030 echo " <span class=\"table".($pos-1)."\">\n";
1035 echo "<a href=\"".$host."?me=".$hash."\">$name</a>\n";
1037 /* add hints for poverty, wedding, solo, etc */
1038 if($GT=="poverty" && $party=="re")
1039 if($sickness=="poverty")
1041 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1042 $cards = DB_get_all_hand($userhash);
1043 $trumpNR = count_trump($cards);
1045 echo "<img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" />";
1047 echo "<img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" />";
1050 echo "<img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" />";
1054 if($sickness=="poverty")
1056 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1057 $cards = DB_get_all_hand($userhash);
1058 $trumpNR = count_trump($cards);
1060 echo "<img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" />";
1062 echo "<img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" />";
1065 echo "<img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" />";
1067 if($sickness=="poverty")
1069 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1070 $cards = DB_get_all_hand($userhash);
1071 $trumpNR = count_trump($cards);
1073 echo "<img src=\"pics/button/poverty2_trump_button.png\" class=\"button\" alt=\"poverty2 < trump back\" />";
1075 echo "<img src=\"pics/button/poverty2_notrump_button.png\" class=\"button\" alt=\"poverty2 <\" />";
1078 echo "<img src=\"pics/button/poverty2_partner_button.png\" class=\"button\" alt=\"poverty2 >\" />";
1080 if($GT=="wedding" && $party=="re")
1081 if($sickness=="wedding")
1082 echo "<img src=\"pics/button/wedding_button.png\" class=\"button\" alt=\"wedding\" />";
1084 echo "<img src=\"pics/button/wedding_partner_button.png\" class=\"button\" alt=\"wedding partner\" />";
1086 if(ereg("solo",$GT) && $party=="re")
1088 if(ereg("queen",$GT))
1089 echo "<img src=\"pics/button/queensolo_button.png\" class=\"button\" alt=\"$GT\" />";
1090 else if(ereg("jack",$GT))
1091 echo "<img src=\"pics/button/jacksolo_button.png\" class=\"button\" alt=\"$GT\" />";
1092 else if(ereg("club",$GT))
1093 echo "<img src=\"pics/button/clubsolo_button.png\" class=\"button\" alt=\"$GT\" />";
1094 else if(ereg("spade",$GT))
1095 echo "<img src=\"pics/button/spadesolo_button.png\" class=\"button\" alt=\"$GT\" />";
1096 else if(ereg("heart",$GT))
1097 echo "<img src=\"pics/button/heartsolo_button.png\" class=\"button\" alt=\"$GT\" />";
1098 else if(ereg("trumpless",$GT))
1099 echo "<img src=\"pics/button/notrumpsolo_button.png\" class=\"button\" alt=\"$GT\" />";
1100 else if(ereg("trump",$GT))
1101 echo "<img src=\"pics/button/diamandsolo_button.png\" class=\"button\" alt=\"$GT\" />";
1104 /* add point calls */
1108 echo "<img src=\"pics/button/re_button.png\" class=\"button\" alt=\"re\" />";
1110 echo "<img src=\"pics/button/contra_button.png\" class=\"button\" alt=\"contra\" />";
1114 echo "<img src=\"pics/button/0_button.png\" class=\"button\" alt=\"0\" />";
1117 echo "<img src=\"pics/button/30_button.png\" class=\"button\" alt=\"30\" />";
1120 echo "<img src=\"pics/button/60_button.png\" class=\"button\" alt=\"60\" />";
1123 echo "<img src=\"pics/button/90_button.png\" class=\"button\" alt=\"90\" />";
1129 echo " local time: ".date("Y-m-d H:i:s")."<br />\n";
1130 echo " last login: ".date("Y-m-d H:i:s",$lastlogin)."<br />\n";
1136 /* get everything relevant to display the tricks */
1137 $result = mysql_query("SELECT Hand_Card.card_id as card,".
1138 " Hand.position as position,".
1139 " Play.sequence as sequence, ".
1141 " Comment.comment, ".
1142 " Play.create_date, ".
1145 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1146 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1147 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1148 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1149 "WHERE Trick.game_id='".$gameid."' ".
1150 "ORDER BY Trick.id,sequence ASC");
1152 $lasttrick = DB_get_max_trickid($gameid);
1154 $play = array(); /* needed to calculate winner later */
1156 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1157 $firstcard = ""; /* first card in a trick */
1159 echo "\n<ul class=\"tricks\">\n";
1160 echo " <li class=\"nohighlight\"> Game $gameid: </li>\n";
1162 while($r = mysql_fetch_array($result,MYSQL_NUM))
1168 $timeplayed = strtotime($r[5]);
1171 $offset = DB_get_user_timezone($user);
1172 $zone = return_timezone($offset);
1173 date_default_timezone_set($zone);
1175 /* check if first schweinchen has been played */
1176 if($r[0] == 19 || $r[0] == 20 )
1177 $GAME["schweinchen"]++;
1179 /* save card to be able to find the winner of the trick later */
1180 $play[$seq] = array("card"=>$r[0],"pos"=>$pos);
1184 /* first card in a trick, output some html */
1185 if($trick!=$lasttrick)
1187 /* start of an old trick? */
1188 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1189 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1190 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1192 else if($trick==$lasttrick)
1194 /* start of a last trick? */
1195 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1196 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1197 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1200 /* remember first card, so that we are able to check, what cards can be played */
1205 echo " <div class=\"card".($pos-1)."\">\n";
1207 /* display comments */
1209 echo " <span class=\"comment\">".$comment."</span>\n";
1212 display_card($r[0],$PREF["cardset"]);
1214 echo " </div>\n"; /* end div card */
1220 echo " </div>\n </li>\n"; /* end div table, end li table */
1224 if($seq!=4 && $trickNR>1)
1225 echo " </div>\n </li>\n"; /* end div table, end li table */
1229 /* whos turn is it? */
1232 $winner = get_winner($play,$gametype); /* returns the position */
1234 $firstcard = ""; /* new trick, no first card */
1239 if($next==5) $next = 1;
1242 /* my turn?, display cards as links, ask for comments*/
1243 if(DB_get_pos_by_hash($me) == $next)
1248 /* do we want to play a card? */
1249 if(myisset("card") && $myturn)
1251 $card = $_REQUEST["card"];
1252 $handid = DB_get_handid_by_hash($me);
1254 /* check if we have card and that we haven't played it yet*/
1255 /* set played in hand_card to true where hand_id and card_id*/
1256 $result = mysql_query("SELECT id FROM Hand_Card WHERE played='false' and ".
1257 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1258 $r = mysql_fetch_array($result,MYSQL_NUM);
1259 $handcardid = $r[0];
1261 if($handcardid) /* everything ok, play card */
1265 /* update Game timestamp */
1266 DB_update_game_timestamp($gameid);
1268 /* check if a call was made, must do this before we set the card status to played */
1269 if(myisset("call120") && $_REQUEST["call120"] == "yes" && can_call(120,$me))
1270 $result = mysql_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
1271 if(myisset("call90") && $_REQUEST["call90"] == "yes" && can_call(90,$me))
1272 $result = mysql_query("UPDATE Hand SET point_call='90' WHERE hash='$me' ");
1273 if(myisset("call60") && $_REQUEST["call60"] == "yes" && can_call(60,$me))
1274 $result = mysql_query("UPDATE Hand SET point_call='60' WHERE hash='$me' ");
1275 if(myisset("call30") && $_REQUEST["call30"] == "yes" && can_call(30,$me))
1276 $result = mysql_query("UPDATE Hand SET point_call='30' WHERE hash='$me' ");
1277 if(myisset("call0") && $_REQUEST["call0"] == "yes" && can_call(0,$me))
1278 $result = mysql_query("UPDATE Hand SET point_call='0' WHERE hash='$me' ");
1280 /* mark card as played */
1281 mysql_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1282 DB_quote_smart($card));
1284 /* check for schweinchen */
1285 //echo "schweinchen = ".$GAME["schweinchen"]." --$card-<br />";
1286 if($card == 19 || $card == 20 )
1288 $GAME["schweinchen"]++;
1289 if($GAME["schweinchen"]==3 && $RULES["schweinchen"]=="second" )
1290 $comment="Schweinchen! ";
1291 if($RULES["schweinchen"]=="both" )
1292 $comment="Schweinchen! ";
1293 if ($debug) echo "schweinchen = ".$GAME["schweinchen"]." ---<br />";
1296 /* get trick id or start new trick */
1297 $a = DB_get_current_trickid($gameid);
1302 $playid = DB_play_card($trickid,$handcardid,$sequence);
1304 /* if sequence == 4 check who one in case of wedding */
1305 if($sequence == 4 && $GT == "wedding")
1307 /* is wedding resolve */
1308 $resolved = DB_get_sickness_by_gameid($gameid);
1311 /* who has wedding */
1312 $userids = DB_get_all_userid_by_gameid($gameid);
1313 foreach($userids as $user)
1315 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1316 if($usersick == "wedding")
1319 /* who won the trick */
1320 $play = DB_get_cards_by_trick($trickid);
1321 $winner = get_winner($play,$gametype); /* returns the position */
1322 $winnerid = DB_get_userid_by_gameid_and_position($gameid,$winner);
1323 /* is tricknr <=3 */
1324 if($tricknr <=3 && $winnerid!=$whosick)
1326 /* set resolved at tricknr*/
1327 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1329 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1330 DB_set_party_by_hash($whash,"re");
1332 if($tricknr == 3 && $winnerid==$whosick)
1334 /* set resolved at tricknr*/
1335 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1340 /* check for coment */
1341 if(myisset("comment"))
1343 $comment.=$_REQUEST["comment"];
1346 DB_insert_comment($comment,$playid,$myid);
1348 /* display played card */
1349 echo "<div class=\"card\">";
1350 echo " you played <br />";
1351 display_card($card,$PREF["cardset"]);
1354 /*check if we still have cards left, else set status to gameover */
1355 if(sizeof(DB_get_hand($me))==0)
1357 DB_set_hand_status_by_hash($me,'gameover');
1358 $mystatus='gameover';
1361 /* if all players are done, set game status to game over,
1362 * get the points of the last trick and send out an email
1365 $userids = DB_get_all_userid_by_gameid($gameid);
1368 foreach($userids as $user)
1369 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1374 DB_set_game_status_by_gameid($gameid,"gameover");
1375 /* get score for last trick
1376 * all other tricks are handled a few lines further down*/
1377 $play = DB_get_cards_by_trick($trickid);
1378 $winner = get_winner($play,$gametype); /* returns the position */
1379 /* get points of last trick and save it */
1381 foreach($play as $card)
1382 $points = $points + card_value($card["card"]);
1383 $winnerid = DB_get_handid_by_gameid_and_position($gameid,$winner);
1385 mysql_query("INSERT INTO Score VALUES (NULL, '$gameid', '$winnerid', '$points')");
1387 echo "ERROR during scoring";
1389 /* email all players */
1390 /* individual score */
1391 $result = mysql_query("SELECT fullname, IFNULL(SUM(score),0), Hand.party FROM Hand".
1392 " LEFT JOIN Score ON Hand.id=Score.hand_id".
1393 " LEFT JOIN User ON Hand.user_id=User.id".
1394 " WHERE Hand.game_id=$gameid".
1395 " GROUP BY fullname" );
1396 $message = "The game is over. Thanks for playing :)\n";
1397 $message .= "Final score:\n";
1398 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1399 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1401 $result = mysql_query("SELECT Hand.party, IFNULL(SUM(score),0) FROM Hand".
1402 " LEFT JOIN Score ON Hand.id=Score.hand_id".
1403 " LEFT JOIN User ON Hand.user_id=User.id".
1404 " WHERE Hand.game_id=$gameid".
1405 " GROUP BY Hand.party" );
1406 $message .= "\nTotals:\n";
1407 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1408 $message .= " ".$r[0]." ".$r[1]."\n";
1410 /* send out final email */
1413 foreach($userids as $user)
1414 $all[] = DB_get_email_by_userid($user);
1415 $TO = implode(",",$all);
1417 $help = "\n\n (you can use reply all on this email to reach all the players.)\n";
1418 mymail($To,$EmailName."game over (game $gameid) part 1(2)",$message.$help);
1420 foreach($userids as $user)
1422 $To = DB_get_email_by_userid($user);
1423 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1425 $link = "Use this link to have a look at game $gameid: ".$host."?me=".$hash."\n\n" ;
1426 mymail($To,$EmailName."game over (game $gameid) part 2(2)",$link);
1431 /* email next player */
1432 if(DB_get_game_status_by_gameid($gameid)=='play')
1436 $play = DB_get_cards_by_trick($trickid);
1437 $winner = get_winner($play,$gametype); /* returns the position */
1439 /* get points of last trick and save it, last trick is handled
1440 * a few lines further up */
1442 foreach($play as $card)
1443 $points = $points + card_value($card["card"]);
1445 $winnerid = DB_get_handid_by_gameid_and_position($gameid,$winner);
1447 mysql_query("INSERT INTO Score VALUES (NULL, '$gameid', '$winnerid', '$points')");
1449 echo "ERROR during scoring";
1452 echo "DEBUG: $winner got $points <br />";
1454 /* who is the next player? */
1459 $next = DB_get_pos_by_hash($me)+1;
1461 if($next==5) $next=1;
1463 /* email next player */
1464 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1465 $email = DB_get_email_by_hash($next_hash);
1466 $who = DB_get_userid_by_email($email);
1467 DB_set_player_by_gameid($gameid,$who);
1469 $message = "A card has been played in game $gameid.\n\n".
1470 "It's your turn now.\n".
1471 "Use this link to play a card: ".$host."?me=".$next_hash."\n\n" ;
1472 mymail($email,$EmailName."a card has been played in game $gameid",$message);
1477 echo "can't find that card?! <br />\n";
1480 else if(myisset("card") && !$myturn )
1482 echo "please wait until it's your turn! <br />\n";
1485 $mycards = DB_get_hand($me);
1486 $mycards = mysort($mycards,$gametype);
1487 echo "<div class=\"mycards\">\n";
1489 if($myturn && !myisset("card"))
1491 echo "Hello ".$myname.", it's your turn! <br />\n";
1492 echo "Your cards are: <br />\n";
1493 echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
1495 /* do we have to follow suite? */
1497 if(have_suit($mycards,$firstcard))
1500 foreach($mycards as $card)
1502 if($followsuit && !same_type($card,$firstcard))
1503 display_card($card,$PREF["cardset"]);
1505 display_link_card($card,$PREF["cardset"]);
1508 if( can_call(120,$me) )
1509 echo " re/contra (120):".
1510 " <input type=\"radio\" name=\"call120\" value=\"yes\" /> ";
1511 if( can_call(90,$me) )
1513 " <input type=\"radio\" name=\"call90\" value=\"yes\" /> ";
1514 if( can_call(60,$me) )
1516 " <input type=\"radio\" name=\"call60\" value=\"yes\" /> ";
1517 if( can_call(30,$me) )
1519 " <input type=\"radio\" name=\"call30\" value=\"yes\" /> ";
1520 if( can_call(0,$me) )
1522 " <input type=\"radio\" name=\"call0\" value=\"yes\" /> ".
1524 " <input type=\"radio\" name=\"call0\" value=\"no\" /> ";
1526 echo "<br />\nA short comments:<input name=\"comment\" type=\"text\" size=\"30\" maxlength=\"50\" />\n";
1527 echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />\n";
1528 echo "<input type=\"submit\" value=\"move\" />\n";
1531 else if($mystatus=='play')
1533 echo "Your cards are: <br />\n";
1534 foreach($mycards as $card)
1535 display_card($card,$PREF["cardset"]);
1537 else if($mystatus=='gameover')
1539 $oldcards = DB_get_all_hand($me);
1540 $oldcards = mysort($oldcards,$gametype);
1541 echo "Your cards were: <br />\n";
1542 foreach($oldcards as $card)
1543 display_card($card,$PREF["cardset"]);
1545 $userids = DB_get_all_userid_by_gameid($gameid);
1546 foreach($userids as $user)
1548 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1554 $name = DB_get_name_by_userid($user);
1555 $oldcards = DB_get_all_hand($userhash);
1556 $oldcards = mysort($oldcards,$gametype);
1557 echo "$name's cards were: <br />\n";
1558 foreach($oldcards as $card)
1559 display_card($card,$PREF["cardset"]);
1565 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1566 if($mystatus=='play')
1568 /* the following happens only when the gamestatus is 'gameover' */
1569 /* check if game is over, display results */
1570 if(DB_get_game_status_by_gameid($gameid)=='play')
1572 echo "the game is over for you.. other people still need to play though";
1576 echo "the game is over now...<br />\n";
1578 $result = mysql_query("SELECT fullname, IFNULL(SUM(score),0), Hand.party FROM Hand".
1579 " LEFT JOIN Score ON Hand.id=Score.hand_id".
1580 " LEFT JOIN User ON Hand.user_id=User.id".
1581 " WHERE Hand.game_id=$gameid".
1582 " GROUP BY fullname" );
1583 echo "Final Score:<br />\n".
1585 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1586 echo " <tr><td> ".$r[0]."</td><td>(".$r[2].")</td><td> ".$r[1]."</td></tr>";
1589 $result = mysql_query("SELECT Hand.party, IFNULL(SUM(score),0) FROM Hand".
1590 " LEFT JOIN Score ON Hand.id=Score.hand_id".
1591 " LEFT JOIN User ON Hand.user_id=User.id".
1592 " WHERE Hand.game_id=$gameid".
1593 " GROUP BY Hand.party" );
1594 echo "Totals:<br />\n".
1596 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1597 echo " <tr><td>".$r[0]."</td><td> ".$r[1]."</td></tr>\n";
1600 $session = DB_get_session_by_gameid($gameid);
1601 $result = mysql_query("SELECT id,create_date FROM Game".
1602 " WHERE session=$session".
1603 " ORDER BY create_date DESC".
1607 $r = mysql_fetch_array($result,MYSQL_NUM);
1609 if(!$session || $gameid==$r[0])
1611 /* suggest a new game with the same people in it, just rotated once */
1612 $names = DB_get_all_names_by_gameid($gameid);
1613 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
1618 echo "error in testing the status";
1624 /* user status page */
1625 else if(myisset("email","password"))
1627 /* test id and password, should really be done in one step */
1628 $email = $_REQUEST["email"];
1629 $password = $_REQUEST["password"];
1632 if(myisset("forgot"))
1636 $uid = DB_get_userid_by_email($email);
1642 echo "Hmm, you forgot your passwort...nothing I can do at the moment:( ";
1643 echo " you need to email Arun for now... in the future it will be all automated and an ";
1644 echo "email with a new password will go to $email.";
1649 echo "you need to give me an email address!";
1651 echo "couldn't find a player with this email, please contact Arun, if you think this is a mistake";
1656 /* verify password and email */
1657 if(strlen($password)!=32)
1658 $password = md5($password);
1661 $uid = DB_get_userid_by_email_and_password($email,$password);
1669 if(myisset("setpref"))
1671 $setpref=$_REQUEST["setpref"];
1675 case "englishcards":
1676 $result = mysql_query("SELECT * from User_Prefs".
1677 " WHERE user_id='$uid' AND pref_key='cardset'" );
1678 if( mysql_fetch_array($result,MYSQL_NUM))
1679 $result = mysql_query("UPDATE User_Prefs SET value=".DB_quote_smart($setpref).
1680 " WHERE user_id='$uid' AND pref_key='cardset'" );
1682 $result = mysql_query("INSERT INTO User_Prefs VALUES(NULL,'$uid','cardset',".DB_quote_smart($setpref).")");
1683 echo "Ok, changed you preferences for the cards.\n";
1687 else /* output default user page */
1689 $time = DB_get_user_timestamp($uid);
1690 $unixtime =strtotime($time);
1692 $offset = DB_get_user_timezone($uid);
1693 $zone = return_timezone($offset);
1694 date_default_timezone_set($zone);
1696 /* display links to settings */
1697 output_user_settings($email,$password);
1699 echo "last login: ".date("r",$unixtime)."<br />";
1701 DB_update_user_timestamp($uid);
1703 echo "<p>These are your games that haven't started yet:<br />\n";
1704 $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.mod_date,Game.player from Hand".
1705 " LEFT JOIN Game On Hand.game_id=Game.id".
1706 " WHERE Hand.user_id='$uid' AND Game.status='pre'" );
1707 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1709 echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a>";
1713 echo "(it's <strong>your</strong> turn)\n";
1716 $name = DB_get_name_by_userid($r[3]);
1717 echo "(it's $name's turn)\n";
1720 if(time()-strtotime($r[2]) > 60*60*24*30)
1721 echo " The game has been running for over a month.".
1722 " Do you want to cancel it? <a href=\"$host?cancle=1&me=".$r[0]."\">yes</a>".
1723 " (clicking here is final and can't be restored)";
1728 echo "<p>These are the games you are playing in:<br />\n";
1729 $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.mod_date,Game.player from Hand".
1730 " LEFT JOIN Game On Hand.game_id=Game.id".
1731 " WHERE Hand.user_id='$uid' AND Game.status='play'" );
1732 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1734 echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a>";
1738 echo "(it's <strong>your</strong> turn)\n";
1741 $name = DB_get_name_by_userid($r[3]);
1742 echo "(it's $name's turn)\n";
1745 if(time()-strtotime($r[2]) > 60*60*24*30)
1746 echo " The game has been running for over a month.".
1747 " Do you want to cancel it? <a href=\"$host?cancle=1&me=".$r[0]."\">yes</a>".
1748 " (clicking here is final and can't be restored)";
1754 echo "<p>And these are your games that are already done:<br />Game: \n";
1756 $result = mysql_query("SELECT hash,game_id from Hand WHERE user_id='$uid' AND status='gameover'" );
1757 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1758 $output[] = "<a href=\"".$host."?me=".$r[0]."\">#".$r[1]." </a>";
1759 echo implode(", ",$output)."</p>\n";
1761 $names = DB_get_all_names();
1762 echo "<p>Registered players:<br />\n";
1763 echo implode(", ",$names)."\n";
1766 echo "<p>Want to start a new game? Visit <a href=\"".$host."?new\">this page.</a></p>";
1771 echo "Sorry email and password don't match <br />";
1778 /* page for registration */
1779 else if(myisset("register") )
1783 /* new user wants to register */
1784 else if(myisset("Rfullname","Remail","Rpassword","Rtimezone") )
1787 if(DB_get_userid_by_name($_REQUEST["Rfullname"]))
1789 echo "please chose another name<br />";
1792 if(DB_get_userid_by_email($_REQUEST["Remail"]))
1794 echo "this email address is already used ?!<br />";
1799 $r=mysql_query("INSERT INTO User VALUES(NULL,".DB_quote_smart($_REQUEST["Rfullname"]).
1800 ",".DB_quote_smart($_REQUEST["Remail"]).
1801 ",".DB_quote_smart(md5($_REQUEST["Rpassword"])).
1802 ",".DB_quote_smart($_REQUEST["Rtimezone"]).",NULL)");
1805 echo " added you to the database";
1807 echo " something went wrong";
1810 /* default login page */
1813 $pre[0]=0;$game[0]=0;$done[0]=0;
1814 $r=mysql_query("SELECT COUNT(id) FROM Game GROUP BY status");
1816 $pre = mysql_fetch_array($r,MYSQL_NUM);
1817 $game = mysql_fetch_array($r,MYSQL_NUM);
1818 $done = mysql_fetch_array($r,MYSQL_NUM);
1821 $r=mysql_query("SELECT AVG(datediff(mod_date,create_date)) FROM Game where status='gameover' ");
1823 $avgage= mysql_fetch_array($r,MYSQL_NUM);
1827 output_home_page($pre[0],$game[0],$done[0],$avgage[0]);