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 */
14 echo "Database error, can't connect... Please wait a while and try again. ".
15 "If the problem doesn't go away feel free to contact $ADMIN_NAME at $ADMIN_EMAIL.";
20 /* start a session, if it is not already running */
23 /* done major error checking, output header of HTML page */
26 /* check if we want to start a new game */
32 echo "you are now logged out!";
34 else if(myisset("new"))
36 if( isset($_SESSION["name"]) )
38 $names = DB_get_all_names();
39 output_form_for_new_game($names);
43 echo "Please log in.";
46 /*check if everything is ready to set up a new game */
47 else if( myisset("PlayerA", "PlayerB","PlayerC","PlayerD","dullen","schweinchen","call" ))
49 $PlayerA = $_REQUEST["PlayerA"];
50 $PlayerB = $_REQUEST["PlayerB"];
51 $PlayerC = $_REQUEST["PlayerC"];
52 $PlayerD = $_REQUEST["PlayerD"];
54 $dullen = $_REQUEST["dullen"];
55 $schweinchen = $_REQUEST["schweinchen"];
56 $call = $_REQUEST["call"];
58 $EmailA = DB_get_email_by_name($PlayerA);
59 $EmailB = DB_get_email_by_name($PlayerB);
60 $EmailC = DB_get_email_by_name($PlayerC);
61 $EmailD = DB_get_email_by_name($PlayerD);
63 if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
65 echo "couldn't find one of the names, please start a new game";
71 $useridA = DB_get_userid_by_name($PlayerA);
72 $useridB = DB_get_userid_by_name($PlayerB);
73 $useridC = DB_get_userid_by_name($PlayerC);
74 $useridD = DB_get_userid_by_name($PlayerD);
76 /* create random numbers */
77 $randomNR = create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD);
78 $randomNRstring = join(":",$randomNR);
82 if(myisset("followup") )
84 $followup= $_REQUEST["followup"];
85 $session = DB_get_session_by_gameid($followup);
86 $ruleset = DB_get_ruleset_by_gameid($followup); /* just copy ruleset from old game,
87 this way no manipulation is possible */
89 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
90 "'$ruleset','$session' ,NULL)");
94 $max = DB_get_max_session();
96 mysql_query("UPDATE Game SET session='".$max."' WHERE id=".DB_quote_smart($followup));
97 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
98 "'$ruleset','$max' ,NULL)");
103 /* get ruleset information or create new one */
104 $ruleset = DB_get_ruleset($dullen,$schweinchen,$call);
107 myerror("Error defining ruleset: $ruleset");
112 /* get max session */
113 $max = DB_get_max_session();
116 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre', ".
117 "'$ruleset','$max' ,NULL)");
119 $game_id = mysql_insert_id();
122 $TIME = (string) time(); /* to avoid collisions */
123 $hashA = md5("AGameOfDoko".$game_id.$PlayerA.$EmailA.$TIME);
124 $hashB = md5("AGameOfDoko".$game_id.$PlayerB.$EmailB.$TIME);
125 $hashC = md5("AGameOfDoko".$game_id.$PlayerC.$EmailC.$TIME);
126 $hashD = md5("AGameOfDoko".$game_id.$PlayerD.$EmailD.$TIME);
129 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridA).
130 ", ".DB_quote_smart($hashA).", 'start','1',NULL,NULL,NULL,NULL)");
131 $hand_idA = mysql_insert_id();
132 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridB).
133 ", ".DB_quote_smart($hashB).", 'start','2',NULL,NULL,NULL,NULL)");
134 $hand_idB = mysql_insert_id();
135 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridC).
136 ", ".DB_quote_smart($hashC).", 'start','3',NULL,NULL,NULL,NULL)");
137 $hand_idC = mysql_insert_id();
138 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridD).
139 ", ".DB_quote_smart($hashD).", 'start','4',NULL,NULL,NULL,NULL)");
140 $hand_idD = mysql_insert_id();
144 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idA', '".$randomNR[$i]."', 'false')");
145 for($i=12;$i<24;$i++)
146 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idB', '".$randomNR[$i]."', 'false')");
147 for($i=24;$i<36;$i++)
148 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idC', '".$randomNR[$i]."', 'false')");
149 for($i=36;$i<48;$i++)
150 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idD', '".$randomNR[$i]."', 'false')");
152 /* send out email, TODO: check for error with email */
154 "you are invited to play a game of DoKo (that is to debug the program ;).\n".
155 "Place comments and bug reports here:\n".
156 "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
157 "The whole round would consist of the following players:\n".
162 "If you want to join this game, please follow this link:\n\n".
165 mymail($EmailA,"You are invited to a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
166 mymail($EmailB,"You are invited to a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
167 mymail($EmailC,"You are invited to a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
168 mymail($EmailD,"You are invited to a game of DoKo","Hello $PlayerD,\n".$message.$hashD);
170 echo "You started a new game. The emails have been sent out!";
171 } /* end set up a new game */
172 /* cancle a game, if nothing has happend in the last N minutes */
173 else if(myisset("cancle","me"))
175 $me = $_REQUEST["me"];
177 /* test for valid ID */
178 $myid = DB_get_userid_by_hash($me);
181 echo "Can't find you in the database, please check the url.<br />\n";
182 echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
188 DB_update_user_timestamp($myid);
190 /* get some information from the DB */
191 $gameid = DB_get_gameid_by_hash($me);
192 $myname = DB_get_name_by_hash($me);
194 /* check if game really is old enough */
195 $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
196 $r = mysql_fetch_array($result,MYSQL_NUM);
197 if(time()-strtotime($r[0]) > 60*60*24*30) /* = 1 month */
199 $message = "Hello, \n\n".
200 "Game ".DB_format_gameid($gameid)." has been cancled since nothing happend for a while and $myname requested it.\n";
202 $userids = DB_get_all_userid_by_gameid($gameid);
203 foreach($userids as $user)
205 $To = DB_get_email_by_userid($user);
206 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." cancled (timed out)",$message);
209 /* delete everything from the dB */
212 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been cancled.<br /><br /></p>";
215 echo "<p>You need to wait longer before you can cancle a game...</p>\n";
217 /* handle request from one specific player for one game,
218 * (the hash is set on a per game base) */
219 else if(myisset("me"))
221 $me = $_REQUEST["me"];
223 /* test for valid ID */
224 $myid = DB_get_userid_by_hash($me);
227 echo "Can't find you in the database, please check the url.<br />\n";
228 echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
234 if(isset($_SESSION["name"]))
235 output_status($_SESSION["name"]);
237 /* the user had done something, update the timestamp */
238 DB_update_user_timestamp($myid);
240 /* get some information from the DB */
241 $gameid = DB_get_gameid_by_hash($me);
242 $myname = DB_get_name_by_hash($me);
243 $mystatus = DB_get_status_by_hash($me);
244 $mypos = DB_get_pos_by_hash($me);
245 $myhand = DB_get_handid_by_hash($me);
246 $session = DB_get_session_by_gameid($gameid);
248 /* get prefs and save them */
252 /* get rule set for this game */
253 $result = mysql_query("SELECT * FROM Rulesets".
254 " LEFT JOIN Game ON Game.ruleset=Rulesets.id ".
255 " WHERE Game.id='$gameid'" );
256 $r = mysql_fetch_array($result,MYSQL_NUM);
258 $RULES["dullen"] = $r[2];
259 $RULES["schweinchen"] = $r[3];
260 $RULES["call"] = $r[4];
262 /* get some infos about the game */
263 $gametype = DB_get_gametype_by_gameid($gameid);
264 $gamestatus = DB_get_game_status_by_gameid($gameid);
266 if($gametype=="solo")
268 $gametype = DB_get_solo_by_gameid($gameid);
269 $GT = $gametype." ".$GT;
272 /* does anyone have both foxes */
273 $GAME["schweinchen"]=0;
276 $hash = DB_get_hash_from_game_and_pos($gameid,$i);
277 $cards = DB_get_all_hand($hash);
278 if( in_array("19",$cards) && in_array("20",$cards) )
280 $GAME["schweinchen"]=1;
281 $GAME["schweinchen-who"]=$hash;
285 /* put everyting in a form */
286 echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
288 /* output left menu */
292 /* output right menu */
294 /* display rule set for this game */
295 echo "<div class=\"gameinfo\">\n";
297 if($gamestatus != 'pre')
298 echo " Gametype: $GT <br />\n";
300 echo "Rules: <br />\n";
301 echo "10ofhearts : ".$RULES["dullen"] ."<br />\n";
302 echo "schweinchen: ".$RULES["schweinchen"] ."<br />\n";
303 echo "call: ".$RULES["call"] ."<br />\n";
306 if($mystatus == 'play' )
308 output_form_calls($me);
310 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
314 echo "<input type=\"submit\" value=\"submit\" />\n";
317 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
321 $session = DB_get_session_by_gameid($gameid);
322 $result = mysql_query("SELECT id,create_date FROM Game".
323 " WHERE session=$session".
324 " ORDER BY create_date DESC".
328 $r = mysql_fetch_array($result,MYSQL_NUM);
330 if(!$session || $gameid==$r[0])
332 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
333 $names = DB_get_all_names_by_gameid($gameid);
334 $type = DB_get_gametype_by_gameid($gameid);
337 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
339 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
347 /* output extra division in case this game is part of a session */
350 echo "<div class=\"session\">\n".
351 "This game is part of session $session: \n";
352 $hashes = DB_get_hashes_by_session($session,$myid);
354 foreach($hashes as $hash)
359 echo "<a href=\"".$host."?me=".$hash."\">$i</a> ";
365 /* display the table and the names */
368 /* mystatus gets the player through the different stages of a game.
370 * init: check values from start,
372 * check: check for return values from init
373 * poverty: handle poverty, wait here until all player have reached this state
374 * display sickness and move on to game
375 * play: game in progress
376 * gameover: are we revisiting a game
383 output_check_want_to_play($me);
388 /* move on to the next stage*/
389 DB_set_hand_status_by_hash($me,'init');
392 /* first check if everything went ok in the last step
393 * if not, send user back, if yes, check what he did
397 echo "<p> You need to answer the <a href=\"$host?me=$me\">question</a>.</p>";
398 DB_set_hand_status_by_hash($me,'start');
402 if($_REQUEST["in"] == "no")
404 /* cancel the game */
405 $message = "Hello, \n\n".
406 "the game has been canceled due to the request of one of the players.\n";
408 $userids = DB_get_all_userid_by_gameid($gameid);
409 foreach($userids as $user)
411 $To = DB_get_email_by_userid($user);
412 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
415 /* delete everything from the dB */
420 $mycards = DB_get_hand($me);
423 output_check_for_sickness($me,$mycards);
425 echo "<p class=\"mycards\">your cards are: <br />\n";
426 foreach($mycards as $card)
427 display_card($card,$PREF["cardset"]);
430 /* move on to the next stage*/
431 DB_set_hand_status_by_hash($me,'check');
437 /* ok, user is in the game, saw his cards and selected his vorbehalt
438 * so first we check what he selected
440 if(!myisset("solo","wedding","poverty","nines") )
442 /* all these variables have a pre-selected default,
443 * so we should never get here,
444 * unless a user tries to cheat ;)
445 * can also happen if user reloads the page!
447 echo "<p> You need to answer the <a href=\"$host?me=$me&in=yes\">questions</a>.</p>";
448 DB_set_hand_status_by_hash($me,'init');
452 echo "Processing what you selected in the last step...<br />";
454 /* check if this sickness needs to be handled first */
455 $gametype = DB_get_gametype_by_gameid($gameid);
456 $startplayer = DB_get_startplayer_by_gameid($gameid);
458 if( $_REQUEST["solo"]!="No")
460 /* user wants to play a solo */
462 /* store the info in the user's hand info */
463 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
464 DB_set_sickness_by_hash($me,"solo");
466 echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
468 if($gametype == "solo" && $startplayer<$mypos)
469 {}/* do nothing, since someone else already is playing solo */
472 /* this solo comes first
473 * store info in game table
475 DB_set_gametype_by_gameid($gameid,"solo");
476 DB_set_startplayer_by_gameid($gameid,$mypos);
477 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
480 else if($_REQUEST["wedding"] == "yes")
482 /* TODO: add silent solo somewhere*/
483 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
484 DB_set_sickness_by_hash($me,"wedding");
486 else if($_REQUEST["poverty"] == "yes")
488 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
489 DB_set_sickness_by_hash($me,"poverty");
491 else if($_REQUEST["nines"] == "yes")
493 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
494 " is playing solo, this game will be canceled.<br />\n";
495 DB_set_sickness_by_hash($me,"nines");
498 echo " Ok, done with checking, please go to the <a href=\"$host?me=$me\">next step of the setup</a>.<br />";
500 /* move on to the next stage*/
501 DB_set_hand_status_by_hash($me,'poverty');
503 /* check if everyone has reached this stage, send out email */
504 $userids = DB_get_all_userid_by_gameid($gameid);
506 foreach($userids as $user)
508 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
509 if($userstat!='poverty' && $userstat!='play')
512 DB_set_player_by_gameid($gameid,$user);
517 /* reset player = everyone has to do something now */
518 DB_set_player_by_gameid($gameid,NULL);
520 foreach($userids as $user)
522 $To = DB_get_email_by_userid($user);
523 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
526 $message = "Everyone finish the questionary in game ".DB_format_gameid($gameid).", ".
527 "please visit this link now to continue: \n".
528 " ".$host."?me=".$userhash."\n\n" ;
529 mymail($To,$EmailName." finished setup in game ".DB_format_gameid($gameid),$message);
538 /* here we need to check if there is a solo or some other form of sickness.
539 * If so, which one is the most important one
540 * set that one in the Game table
541 * tell people about it.
543 echo "<br /> Checking if someone else selected solo, nines, wedding or poverty.<br />";
545 /* check if everyone has reached this stage */
546 $userids = DB_get_all_userid_by_gameid($gameid);
548 foreach($userids as $user)
550 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
551 if($userstat!='poverty' && $userstat!='play')
557 echo "This step can only be handled after everyone finished the last step. ".
558 "Seems like this is not the case, so you need to wait a bit... ".
559 "you will get an email once that is the case, please use the link in ".
560 "that email to continue the game.<br />";
564 echo "Everyone has finished checking their cards, let's see what they said...<br />";
566 /* check what kind of game we are playing, in case there are any solos this already
567 *will have the correct information in it */
568 $gametype = DB_get_gametype_by_gameid($gameid);
569 $startplayer = DB_get_startplayer_by_gameid($gameid);
571 /* check for different sickness and just output a general info */
576 foreach($userids as $user)
578 $name = DB_get_name_by_userid($user);
579 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
580 if($usersick == 'nines')
583 echo "$name has a Vorbehalt. <br />";
586 else if($usersick == 'poverty')
589 echo "$name has a Vorbehalt. <br />";
591 else if($usersick == 'wedding')
594 echo "$name has a Vorbehalt. <br />" ;
596 else if($usersick == 'solo')
599 echo "$name has a Vorbehalt. <br />" ;
603 /* now check which sickness comes first and set the gametype to it */
605 if($gametype == "solo")
612 /* TODO: should we keep statistics of this? */
613 $message = "Hello, \n\n".
614 " the game has been canceled because ".DB_get_name_by_userid($nines).
615 " has five or more nines and nobody is playing solo.\n\n".
616 " To redeal either start a new game or, in case the game was part of a tournament, \n".
617 " go to the last game and use the link at the bottom of the page to redeal.";
619 $userids = DB_get_all_userid_by_gameid($gameid);
620 foreach($userids as $user)
622 $To = DB_get_email_by_userid($user);
623 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
626 /* delete everything from the dB */
629 echo "The game has been canceled because ".DB_get_name_by_userid($nines).
630 " has five or more nines and nobody is playing solo.\n";
635 else if($poverty==1) /* one person has poverty */
637 DB_set_gametype_by_gameid($gameid,"poverty");
638 $gametype = "poverty";
639 $who = DB_get_sickness_by_gameid($gameid);
642 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
643 if($firstsick == "poverty")
644 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
646 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
649 else if($poverty==2) /* two people have poverty */
651 DB_set_gametype_by_gameid($gameid,"dpoverty");
652 $gametype = "dpoverty";
653 $who = DB_get_sickness_by_gameid($gameid);
656 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
657 if($firstsick == "poverty")
659 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
660 if($secondsick == "poverty")
661 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
663 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
666 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
671 DB_set_gametype_by_gameid($gameid,"wedding");
672 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
673 $gametype = "wedding";
678 /* now the gametype is set correctly (shouldn't matter that this is calculated for every user)
679 * output what kind of game we have */
682 foreach($userids as $user)
684 /* userids are sorted by position...
685 * so output whatever the first one has, then whatever the next one has
686 * stop when the sickness is the same as the gametype
689 $name = DB_get_name_by_userid($user);
690 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
693 echo "$name has $usersick. <br />"; /*TODO: perhaps save this in a string and store in Game? */
695 if($usersick=="poverty")
697 if($usersick == "wedding" && $gametype =="wedding")
699 if($usersick == "poverty" && $gametype =="poverty")
701 if($usersick == "poverty" && $gametype =="dpoverty" && $poverty==2)
703 if($usersick == "solo" && $gametype =="solo")
707 /* output Schweinchen in case the rules need it */
708 if( $gametype != "solo")
709 if($GAME["schweinchen"] && $RULES["schweinchen"]=="both" )
710 echo DB_get_name_by_hash($GAME["schweinchen-who"])." has Schweinchen. <br />";
714 /* finished the setup, set re/contra parties if possible, go to next stage unless there is a case of poverty*/
718 /* are we the solo player? set us to re, else set us to contra */
719 $pos = DB_get_pos_by_hash($me);
720 if($pos == $startplayer)
721 DB_set_party_by_hash($me,"re");
723 DB_set_party_by_hash($me,"contra");
724 DB_set_hand_status_by_hash($me,'play');
728 /* set person with the wedding to re, do the rest during the game */
729 $usersick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
730 if($usersick == "wedding")
731 DB_set_party_by_hash($me,"re");
733 DB_set_party_by_hash($me,"contra");
735 echo "Whoever will make the first trick will be on the re team. <br />\n";
736 echo " Ok, the game can start now, please finish <a href=\"$host?me=$me\">the setup</a>.<br />";
737 DB_set_hand_status_by_hash($me,'play');
741 $hand = DB_get_all_hand($me);
743 if(in_array('3',$hand)||in_array('4',$hand))
744 DB_set_party_by_hash($me,"re");
746 DB_set_party_by_hash($me,"contra");
747 DB_set_hand_status_by_hash($me,'play');
751 /* check if poverty resolved (e.g. DB.Game who set to NULL)
752 * yes? =>trump was taken, start game; break;
754 $who = DB_get_sickness_by_gameid($gameid);
756 { /* trump has been taken */
757 DB_set_hand_status_by_hash($me,'play');
761 if($who>9) /*= two people still have trump on the table*/
766 /* check if we are being asked now
767 * no? display wait message, e.g. player X is asked at the moment
769 $usersick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
770 if(myisset("trump") && $_REQUEST["trump"]=="no" && ($who==$mypos || $who==$mypos*10))
772 /* user doesn't want to take trump */
773 /* set next player who needs to be asked */
774 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
775 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
777 if($firstsick=="poverty")
779 if($secondsick=="poverty")
780 DB_set_sickness_by_gameid($gameid,$who+$add*3);
782 DB_set_sickness_by_gameid($gameid,$who+$add*2);
785 DB_set_sickness_by_gameid($gameid,$who+$add);
787 /* email next player */
788 $who = DB_get_sickness_by_gameid($gameid);
789 if($who>9) $who = $who/10;
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, if you want to take the trump. Please visit:".
798 " ".$host."?me=".$userhash."\n\n" ;
799 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
802 /* this user is done */
803 DB_set_hand_status_by_hash($me,'play');
806 else if(myisset("trump") && !myisset("exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
808 /* user wants to take trump */
809 $trump = $_REQUEST["trump"];
811 /* get hand id for user $trump */
812 $userhand = DB_get_handid_by_gameid_and_userid($gameid,$trump);
813 /* copy trump from player A to B */
814 $result = mysql_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
816 /* add hidden button with trump in it to get to the next point */
817 echo "<div class=\"poverty\">\n";
818 echo " <input type=\"hidden\" name=\"exchange\" value=\"-1\" />\n";
819 echo " <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
820 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select cards to give back\" />\n";
823 else if(myisset("trump","exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
825 $trump = $_REQUEST["trump"];
826 $exchange = $_REQUEST["exchange"];
827 $userhand = DB_get_handid_by_gameid_and_userid($gameid,$trump);
829 /* if exchange is set to a value>0, exchange that card back to user $trump */
832 $result = mysql_query("UPDATE Hand_Card SET hand_id='$userhand'".
833 " WHERE hand_id='$myhand' AND card_id='$exchange'" );
836 /* if number of cards == 12, set status to play for both users */
837 $result = mysql_query("SELECT COUNT(*) FROM Hand_Card WHERE hand_id='$myhand'" );
838 $r = mysql_fetch_array($result,MYSQL_NUM);
841 myerror("error in poverty");
846 if($gametype=="poverty" || $who<9)
848 DB_set_sickness_by_gameid($gameid,-1); /* done with poverty */
850 else /* reduce poverty count by one, that is go to single digits $who */
855 /* whom to ask next */
856 $firstsick = DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
857 $secondsick = DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
859 if($firstsick!="poverty")
860 DB_set_sickness_by_gameid($gameid,$who+$add);
863 if($secondsick!="poverty")
864 DB_set_sickness_by_gameid($gameid,$who+$add*2);
866 DB_set_sickness_by_gameid($gameid,$who+$add*3);
869 /* email next player */
870 $who = DB_get_sickness_by_gameid($gameid);
873 $To = DB_get_email_by_pos_and_gameid($who,$gameid);
874 $userhash = DB_get_hash_from_game_and_pos($gameid,$who);
875 DB_set_player_by_gameid($gameid,$who);
877 $message = "Someone has poverty, it's your turn to decide, ".
878 "if you want to take the trump. Please visit:".
879 " ".$host."?me=".$userhash."\n\n" ;
880 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
884 /* this user is done */
885 DB_set_hand_status_by_hash($me,'play');
886 /* and so is his partner */
887 $hash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
888 DB_set_hand_status_by_hash($hash,'play');
890 /* set party to re, unless we had dpoverty, in that case check if we need to set re/contra*/
892 foreach($userids as $user)
894 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
895 $party = DB_get_party_by_hash($userhash);
901 DB_set_party_by_hash($me,"contra");
902 DB_set_party_by_hash($hash,"contra");
906 foreach($userids as $user)
908 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
909 if($userhash==$hash||$userhash==$me)
910 DB_set_party_by_hash($userhash,"re");
912 DB_set_party_by_hash($userhash,"contra");
921 /* else show all trump, have lowest card pre-selected, have hidden setting for */
922 echo "<div class=\"poverty\"> you need to get rid of a few cards</div>\n";
924 set_gametype($gametype); /* this sets the $CARDS variable */
925 $mycards = DB_get_hand($me);
926 $mycards = mysort($mycards,$gametype);
929 echo "<div class=\"mycards\">Your cards are: <br />\n";
930 foreach($mycards as $card)
931 display_link_card($card,$PREF["cardset"],$type);
932 echo " <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
933 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select one card to give back\" />\n";
937 else if($who == $mypos || $who == $mypos*10)
939 echo "<div class=\"poverty\">\n";
940 foreach($userids as $user)
942 $name = DB_get_name_by_userid($user);
943 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
945 if($usersick=="poverty")
947 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
948 $cards = DB_get_hand($hash);
949 $nrtrump = count_trump($cards);
952 echo "Player $name has $nrtrump trump. Do you want to take them?".
953 "<a href=\"index.php?me=$me&trump=$user\">yes</a> <br />\n";
956 echo "<a href=\"index.php?me=$me&trump=no\">No,way I take those trump...</a> <br />\n";
959 echo "Your cards are: <br />\n";
960 $mycards = DB_get_hand($me);
962 echo "<p class=\"mycards\">your cards are: <br />\n";
963 foreach($mycards as $card)
964 display_card($card,$PREF["cardset"]);
969 $mysick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
970 if($mysick=="poverty")
971 echo "The others are asked if they want to take your trump, you have to wait (you'll get an email).";
973 echo "it's not your turn yet to decide if you want to take the trump or not.";
976 /* check if no one wanted to take trump, in that case the gamesickness would be set to 5 or 50 */
977 $who = DB_get_sickness_by_gameid($gameid);
978 if($who==5 || $who==50)
980 $message = "Hello, \n\n".
981 "Game ".DB_format_gameid($gameid)." has been cancled since nobody wanted to take the trump.\n";
983 $userids = DB_get_all_userid_by_gameid($gameid);
984 foreach($userids as $user)
986 $To = DB_get_email_by_userid($user);
987 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." cancled (poverty not resolved)",$message);
990 /* delete everything from the dB */
993 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been cancled.<br /><br /></p>";
999 /* check if all players are ready to play */
1001 foreach($userids as $user)
1002 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='play')
1005 DB_set_player_by_gameid($gameid,$user);
1010 /* only set this after all poverty, etc. are handled*/
1011 DB_set_game_status_by_gameid($gameid,'play');
1013 /* email startplayer */
1014 $startplayer = DB_get_startplayer_by_gameid($gameid);
1015 $email = DB_get_email_by_pos_and_gameid($startplayer,$gameid);
1016 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
1017 $who = DB_get_userid_by_email($email);
1018 DB_set_player_by_gameid($gameid,$who);
1022 /* email startplayer) */
1023 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
1024 "Use this link to play a card: ".$host."?me=".$hash."\n\n" ;
1025 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
1028 echo " Please, <a href=\"$host?me=$me\">start</a> the game.<br />";
1036 /* both entries here, so that the tricks are visible for both.
1037 * in case of 'play' there is a break later that skips the last part
1040 /* figure out what kind of game we are playing,
1041 * set the global variables $CARDS["trump"],$CARDS["diamonds"],$CARDS["hearts"],
1042 * $CARDS["clubs"],$CARDS["spades"],$CARDS["foxes"]
1046 $gametype = DB_get_gametype_by_gameid($gameid);
1048 if($gametype=="solo")
1050 $gametype = DB_get_solo_by_gameid($gameid);
1051 $GT = $gametype." ".$GT;
1054 $gametype = "normal";
1056 set_gametype($gametype); /* this sets the $CARDS variable */
1058 /* get some infos about the game */
1059 $gamestatus = DB_get_game_status_by_gameid($gameid);
1061 /* has the game started? No, then just wait here...*/
1062 if($gamestatus == 'pre')
1064 echo "You finished the setup, but not everyone else finished it... ".
1065 "so you need to wait for the others. Just wait for the an email... <br />";
1066 break; /* not sure this works... the idea is that you can
1067 * only play a card after everyone is ready to play */
1070 /* get time from the last action of the game */
1071 $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
1072 $r = mysql_fetch_array($result,MYSQL_NUM);
1073 $gameend = time() - strtotime($r[0]);
1075 /* handel comments in case player didn't play a card, allow comments a week after the end of the game */
1076 if( (!myisset("card") && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
1077 if(myisset("comment"))
1079 $comment = $_REQUEST["comment"];
1080 $playid = DB_get_current_playid($gameid);
1083 DB_insert_comment($comment,$playid,$myid);
1086 /* get everything relevant to display the tricks */
1087 $result = mysql_query("SELECT Hand_Card.card_id as card,".
1088 " Hand.position as position,".
1089 " Play.sequence as sequence, ".
1091 " GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>') SEPARATOR '\n' ), ".
1092 " Play.create_date, ".
1095 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1096 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1097 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1098 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1099 "LEFT JOIN User On User.id=Comment.user_id ".
1100 "WHERE Trick.game_id='".$gameid."' ".
1101 "GROUP BY Trick.id, sequence ".
1102 "ORDER BY Trick.id, sequence ASC");
1104 $lasttrick = DB_get_max_trickid($gameid);
1106 $play = array(); /* needed to calculate winner later */
1108 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1109 $firstcard = ""; /* first card in a trick */
1111 echo "\n<ul class=\"tricks\">\n";
1112 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
1114 while($r = mysql_fetch_array($result,MYSQL_NUM))
1120 $timeplayed = strtotime($r[5]);
1123 $offset = DB_get_user_timezone($user);
1124 $zone = return_timezone($offset);
1125 date_default_timezone_set($zone);
1127 /* check if first schweinchen has been played */
1128 if($r[0] == 19 || $r[0] == 20 )
1129 $GAME["schweinchen"]++;
1131 /* save card to be able to find the winner of the trick later */
1132 $play[$seq] = array("card"=>$r[0],"pos"=>$pos);
1136 /* first card in a trick, output some html */
1137 if($trick!=$lasttrick)
1139 /* start of an old trick? */
1140 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1141 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1142 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1144 else if($trick==$lasttrick)
1146 /* start of a last trick? */
1147 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1148 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1149 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1152 /* remember first card, so that we are able to check, what cards can be played */
1157 echo " <div class=\"card".($pos-1)."\">\n";
1159 /* display comments */
1161 echo " <span class=\"comment\">".$comment."</span>\n";
1164 display_card($r[0],$PREF["cardset"]);
1166 echo " </div>\n"; /* end div card */
1172 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1176 /* whos turn is it? */
1179 $winner = get_winner($play,$gametype); /* returns the position */
1181 $firstcard = ""; /* new trick, no first card */
1186 if($next==5) $next = 1;
1189 /* my turn?, display cards as links, ask for comments*/
1190 if(DB_get_pos_by_hash($me) == $next)
1195 /* do we want to play a card? */
1196 if(myisset("card") && $myturn)
1198 $card = $_REQUEST["card"];
1199 $handid = DB_get_handid_by_hash($me);
1201 /* check if we have card and that we haven't played it yet*/
1202 /* set played in hand_card to true where hand_id and card_id*/
1203 $result = mysql_query("SELECT id FROM Hand_Card WHERE played='false' and ".
1204 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1205 $r = mysql_fetch_array($result,MYSQL_NUM);
1206 $handcardid = $r[0];
1208 if($handcardid) /* everything ok, play card */
1210 /* update Game timestamp */
1211 DB_update_game_timestamp($gameid);
1213 /* check if a call was made, must do this before we set the card status to played */
1214 if(myisset("call120") && $_REQUEST["call120"] == "yes" && can_call(120,$me))
1215 $result = mysql_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
1216 if(myisset("call90") && $_REQUEST["call90"] == "yes" && can_call(90,$me))
1217 $result = mysql_query("UPDATE Hand SET point_call='90' WHERE hash='$me' ");
1218 if(myisset("call60") && $_REQUEST["call60"] == "yes" && can_call(60,$me))
1219 $result = mysql_query("UPDATE Hand SET point_call='60' WHERE hash='$me' ");
1220 if(myisset("call30") && $_REQUEST["call30"] == "yes" && can_call(30,$me))
1221 $result = mysql_query("UPDATE Hand SET point_call='30' WHERE hash='$me' ");
1222 if(myisset("call0") && $_REQUEST["call0"] == "yes" && can_call(0,$me))
1223 $result = mysql_query("UPDATE Hand SET point_call='0' WHERE hash='$me' ");
1225 /* mark card as played */
1226 mysql_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1227 DB_quote_smart($card));
1229 /* get trick id or start new trick */
1230 $a = DB_get_current_trickid($gameid);
1235 $playid = DB_play_card($trickid,$handcardid,$sequence);
1237 /* check for schweinchen */
1238 if($card == 19 || $card == 20 )
1240 $GAME["schweinchen"]++;
1241 if($GAME["schweinchen"]==3 && $RULES["schweinchen"]=="second" )
1242 DB_insert_comment("Schweinchen! ",$playid,$myid);
1243 if($RULES["schweinchen"]=="both" )
1244 DB_insert_comment("Schweinchen! ",$playid,$myid);
1246 echo "schweinchen = ".$GAME["schweinchen"]." ---<br />";
1249 /* if sequence == 4 check who one in case of wedding */
1250 if($sequence == 4 && $GT == "wedding")
1252 /* is wedding resolve */
1253 $resolved = DB_get_sickness_by_gameid($gameid);
1256 /* who has wedding */
1257 $userids = DB_get_all_userid_by_gameid($gameid);
1258 foreach($userids as $user)
1260 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1261 if($usersick == "wedding")
1264 /* who won the trick */
1265 $play = DB_get_cards_by_trick($trickid);
1266 $winner = get_winner($play,$gametype); /* returns the position */
1267 $winnerid = DB_get_userid_by_gameid_and_position($gameid,$winner);
1268 /* is tricknr <=3 */
1269 if($tricknr <=3 && $winnerid!=$whosick)
1271 /* set resolved at tricknr*/
1272 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1274 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1275 DB_set_party_by_hash($whash,"re");
1277 if($tricknr == 3 && $winnerid==$whosick)
1279 /* set resolved at tricknr*/
1280 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1285 /* if sequence == 4, set winner of the trick, count points and set the next player */
1288 $play = DB_get_cards_by_trick($trickid);
1289 $winner = get_winner($play,$gametype); /* returns the position */
1292 mysql_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1294 echo "ERROR during scoring";
1297 echo "DEBUG: position $winner won the trick <br />";
1299 /* who is the next player? */
1304 $next = DB_get_pos_by_hash($me)+1;
1306 if($next==5) $next=1;
1308 /* check for coment */
1309 if(myisset("comment"))
1311 $comment = $_REQUEST["comment"];
1313 DB_insert_comment($comment,$playid,$myid);
1316 /* display played card */
1317 $pos = DB_get_pos_by_hash($me);
1320 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1321 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1322 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1325 echo "<div class=\"card".($pos-1)."\">";
1326 /* display comments */
1327 display_card($card,$PREF["cardset"]);
1329 echo " <span class=\"comment\"> ".$comment."</span>\n";
1330 echo "</div></div></li>\n";
1332 /*check if we still have cards left, else set status to gameover */
1333 if(sizeof(DB_get_hand($me))==0)
1335 DB_set_hand_status_by_hash($me,'gameover');
1336 $mystatus='gameover';
1339 /* if all players are done, set game status to game over,
1340 * get the points of the last trick and send out an email
1343 $userids = DB_get_all_userid_by_gameid($gameid);
1346 foreach($userids as $user)
1347 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1351 DB_set_game_status_by_gameid($gameid,"gameover");
1353 /* email next player, if game is still running */
1354 if(DB_get_game_status_by_gameid($gameid)=='play')
1356 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1357 $email = DB_get_email_by_hash($next_hash);
1358 $who = DB_get_userid_by_email($email);
1359 DB_set_player_by_gameid($gameid,$who);
1361 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1362 "It's your turn now.\n".
1363 "Use this link to play a card: ".$host."?me=".$next_hash."\n\n" ;
1364 mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
1366 else /* send out final email */
1368 /* individual score */
1369 $result = mysql_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1370 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1371 " LEFT JOIN User ON User.id=Hand.user_id".
1372 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1373 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1374 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1375 " WHERE Hand.game_id='$gameid'".
1376 " GROUP BY User.fullname" );
1377 $message = "The game is over. Thanks for playing :)\n";
1378 $message .= "Final score:\n";
1379 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1380 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1382 $result = mysql_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1383 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1384 " LEFT JOIN User ON User.id=Hand.user_id".
1385 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1386 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1387 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1388 " WHERE Hand.game_id='$gameid'".
1389 " GROUP BY Hand.party" );
1390 $message .= "\nTotals:\n";
1391 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1392 $message .= " ".$r[0]." ".$r[1]."\n";
1394 /* send out final email */
1397 foreach($userids as $user)
1398 $all[] = DB_get_email_by_userid($user);
1399 $To = implode(",",$all);
1401 $help = "\n\n (you can use reply all on this email to reach all the players.)\n";
1402 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 1(2)",$message.$help);
1404 foreach($userids as $user)
1406 $To = DB_get_email_by_userid($user);
1407 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1409 $link = "Use this link to have a look at game ".DB_format_gameid($gameid).": ".$host."?me=".$hash."\n\n" ;
1410 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 2(2)",$link);
1416 echo "can't find that card?! <br />\n";
1419 else if(myisset("card") && !$myturn )
1421 echo "please wait until it's your turn! <br />\n";
1424 if($seq!=4 && $trickNR>1)
1425 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1427 /* display points in case game is over */
1428 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1430 echo " <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1431 " <div class=\"trick\" id=\"trick13\">\n";
1432 /* add pic for re/contra
1433 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1435 $result = mysql_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1436 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1437 " LEFT JOIN User ON User.id=Hand.user_id".
1438 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1439 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1440 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1441 " WHERE Hand.game_id='$gameid'".
1442 " GROUP BY User.fullname" );
1443 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1444 echo " <div class=\"card".($r[3]-1)."\">\n".
1445 " <span class=\"score\">".$r[2]."<br /> ".$r[1]."</span>\n".
1448 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1452 echo "</ul>\n"; /* end ul tricks*/
1454 $mycards = DB_get_hand($me);
1455 $mycards = mysort($mycards,$gametype);
1456 echo "<div class=\"mycards\">\n";
1458 if($myturn && !myisset("card") && $mystatus=='play' )
1460 echo "Hello ".$myname.", it's your turn! <br />\n";
1461 echo "Your cards are: <br />\n";
1463 /* do we have to follow suite? */
1465 if(have_suit($mycards,$firstcard))
1468 foreach($mycards as $card)
1470 if($followsuit && !same_type($card,$firstcard))
1471 display_card($card,$PREF["cardset"]);
1473 display_link_card($card,$PREF["cardset"]);
1476 else if($mystatus=='play' )
1478 echo "Your cards are: <br />\n";
1479 foreach($mycards as $card)
1480 display_card($card,$PREF["cardset"]);
1482 else if($mystatus=='gameover')
1484 /* get time from the last action of the game */
1485 $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
1486 $r = mysql_fetch_array($result,MYSQL_NUM);
1487 $gameend = time() - strtotime($r[0]);
1489 if( $gameend < 60*60*24*7 )
1491 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"30\" maxlength=\"100\" />\n";
1492 echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />\n";
1493 echo "<input type=\"submit\" value=\"submit\" />\n";
1496 $oldcards = DB_get_all_hand($me);
1497 $oldcards = mysort($oldcards,$gametype);
1498 echo "Your cards were: <br />\n";
1499 foreach($oldcards as $card)
1500 display_card($card,$PREF["cardset"]);
1502 $userids = DB_get_all_userid_by_gameid($gameid);
1503 foreach($userids as $user)
1505 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1511 $name = DB_get_name_by_userid($user);
1512 $oldcards = DB_get_all_hand($userhash);
1513 $oldcards = mysort($oldcards,$gametype);
1514 echo "$name's cards were: <br />\n";
1515 foreach($oldcards as $card)
1516 display_card($card,$PREF["cardset"]);
1522 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1523 if($mystatus=='play')
1526 /* the following happens only when the gamestatus is 'gameover' */
1527 /* check if game is over, display results */
1528 if(DB_get_game_status_by_gameid($gameid)=='play')
1530 echo "the game is over for you.. other people still need to play though";
1534 $result = mysql_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1535 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1536 " LEFT JOIN User ON User.id=Hand.user_id".
1537 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1538 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1539 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1540 " WHERE Hand.game_id='$gameid'".
1541 " GROUP BY Hand.party" );
1542 echo "<div class=\"total\"> Totals:<br />\n";
1543 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1544 echo " ".$r[0]." ".$r[1]."<br />\n";
1550 myerror("error in testing the status");
1557 /* user status page */
1558 else if( myisset("email","password") || isset($_SESSION["name"]) )
1560 /* test id and password, should really be done in one step */
1561 if(!isset($_SESSION["name"]))
1563 $email = $_REQUEST["email"];
1564 $password = $_REQUEST["password"];
1568 $name = $_SESSION["name"];
1569 $email = DB_get_email_by_name($name);
1570 $password = DB_get_passwd_by_name($name);
1573 if(myisset("forgot"))
1577 $uid = DB_get_userid_by_email($email);
1583 /* check how many entries in recovery table */
1584 $number = DB_get_number_of_passwords_recovery($uid);
1586 /* if less than N recent ones, add a new one and send out email */
1589 echo "Ok, I send you a new password. <br />";
1591 echo "N.B. You tried this already $number times during the last day and it will only work ".
1592 " 5 times during a day.<br />";
1593 echo "The new password will be valid for one day, make sure you reset it to something else.<br />";
1594 echo "Back to the <a href=\"$host\">main page</a>.";
1596 $TIME = (string) time(); /* to avoid collisions */
1597 $hash = md5("Anewpassword".$email.$TIME);
1598 $newpw = substr($hash,1,8);
1600 $message = "Someone (hopefully you) requested a new password. \n".
1601 "You can use this email and the following password: \n".
1603 "to log into the server. The new password is valid for 24h, so make\n".
1604 "sure you reset your password to something new. Your old password will\n".
1605 " also still be valid until you set a new one\n";
1606 mymail($email,$EmailName."recovery ",$message);
1608 DB_set_recovery_password($uid,md5($newpw));
1612 echo "Sorry you already tried 5 times during the last 24h.<br />".
1613 "You need to use one of those passwords or wait to get a new one.<br />";
1614 echo "Back to the <a href=\"$host\">main page</a>.";
1620 echo "You need to give me an email address! <br />".
1621 "Please try <a href=\"$host\">again</a>.";
1623 echo "Couldn't find a player with this email! <br />".
1624 "Please contact Arun, if you think this is a mistake <br />".
1625 "or else try <a href=\"$host\">again</a>.";
1630 /* verify password and email */
1631 if(strlen($password)!=32)
1632 $password = md5($password);
1635 $uid = DB_get_userid_by_email_and_password($email,$password);
1643 if(myisset("setpref"))
1645 $setpref=$_REQUEST["setpref"];
1649 case "englishcards":
1650 $result = mysql_query("SELECT * from User_Prefs".
1651 " WHERE user_id='$uid' AND pref_key='cardset'" );
1652 if( mysql_fetch_array($result,MYSQL_NUM))
1653 $result = mysql_query("UPDATE User_Prefs SET value=".DB_quote_smart($setpref).
1654 " WHERE user_id='$uid' AND pref_key='cardset'" );
1656 $result = mysql_query("INSERT INTO User_Prefs VALUES(NULL,'$uid','cardset',".
1657 DB_quote_smart($setpref).")");
1658 echo "Ok, changed you preferences for the cards.\n";
1662 else if(myisset("passwd"))
1664 if( $_REQUEST["passwd"]=="ask" )
1666 /* reset password form*/
1667 output_password_recovery($email,$password);
1669 else if($_REQUEST["passwd"]=="set")
1671 /* reset password */
1674 /* check if old password matches */
1675 $oldpasswd = md5($_REQUEST["password0"]);
1676 if(!( ($password == $oldpasswd) || DB_check_recovery_passwords($oldpasswd,$email) ))
1678 /* check if new passwords are types the same twice */
1679 if($_REQUEST["password1"] != $_REQUEST["password2"] )
1685 echo "The new passwords don't match. <br />";
1688 echo "The old password is not correct. <br />";
1691 echo "Changed the password.<br />";
1692 mysql_query("UPDATE User SET password='".md5($_REQUEST["password1"]).
1693 "' WHERE id=".DB_quote_smart($uid));
1699 else /* output default user page */
1701 $time = DB_get_user_timestamp($uid);
1702 $unixtime = strtotime($time);
1704 $offset = DB_get_user_timezone($uid);
1705 $zone = return_timezone($offset);
1706 date_default_timezone_set($zone);
1708 $myname = DB_get_name_by_email($email);
1709 $_SESSION["name"] = $myname;
1711 if(isset($_SESSION["name"]))
1712 output_status($_SESSION["name"]);
1714 /* display links to settings */
1715 output_user_settings($email,$password);
1717 echo "last login: ".date("r",$unixtime)."<br />";
1719 DB_update_user_timestamp($uid);
1721 echo "<p>These are your games that haven't started yet:<br />\n";
1722 $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.mod_date,Game.player from Hand".
1723 " LEFT JOIN Game On Hand.game_id=Game.id".
1724 " WHERE Hand.user_id='$uid' AND Game.status='pre'".
1725 " ORDER BY Game.session" );
1726 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1728 echo "<a href=\"".$host."?me=".$r[0]."\">game ".DB_format_gameid($r[1])." </a>";
1729 if($r[3]==$uid || $r[3]==NULL)
1730 echo "(it's <strong>your</strong> turn)\n";
1733 $name = DB_get_name_by_userid($r[3]);
1734 echo "(it's $name's turn)\n";
1737 if(time()-strtotime($r[2]) > 60*60*24*30)
1738 echo " The game has been running for over a month.".
1739 " Do you want to cancel it? <a href=\"$host?cancle=1&me=".$r[0]."\">yes</a>".
1740 " (clicking here is final and can't be restored)";
1745 echo "<p>These are the games you are playing in:<br />\n";
1746 $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.mod_date,Game.player from Hand".
1747 " LEFT JOIN Game On Hand.game_id=Game.id".
1748 " WHERE Hand.user_id='$uid' AND Game.status='play'".
1749 " ORDER BY Game.session" );
1750 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1752 echo "<a href=\"".$host."?me=".$r[0]."\">game ".DB_format_gameid($r[1])." </a>";
1756 echo "(it's <strong>your</strong> turn)\n";
1759 $name = DB_get_name_by_userid($r[3]);
1760 echo "(it's $name's turn)\n";
1763 if(time()-strtotime($r[2]) > 60*60*24*30)
1764 echo " The game has been running for over a month.".
1765 " Do you want to cancel it? <a href=\"$host?cancle=1&me=".$r[0]."\">yes</a>".
1766 " (clicking here is final and can't be restored)";
1772 echo "<p>And these are your games that are already done:<br />Game: \n";
1774 $result = mysql_query("SELECT hash,game_id from Hand".
1775 " LEFT JOIN Game ON Game.id=Hand.game_id".
1776 " WHERE user_id='$uid' AND Game.status='gameover'".
1777 " ORDER BY Game.session,Game.create_date" );
1778 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1779 $output[] = "<a href=\"".$host."?me=".$r[0]."\">".DB_format_gameid($r[1])." </a>";
1780 echo implode(", ",$output)."</p>\n";
1782 $names = DB_get_all_names();
1783 echo "<p>Registered players:<br />\n";
1784 echo implode(", ",$names)."\n";
1787 echo "<p>Want to start a new game? Visit <a href=\"".$host."?new\">this page.</a></p>";
1792 echo "Sorry email and password don't match. Please <a href=\"$host\">try again</a>. <br />";
1799 /* default login page */
1802 $pre[0]=0;$game[0]=0;$done[0]=0;
1803 $r=mysql_query("SELECT COUNT(id) FROM Game GROUP BY status");
1805 $pre = mysql_fetch_array($r,MYSQL_NUM);
1806 $game = mysql_fetch_array($r,MYSQL_NUM);
1807 $done = mysql_fetch_array($r,MYSQL_NUM);
1810 $r=mysql_query("SELECT AVG(datediff(mod_date,create_date)) FROM Game where status='gameover' ");
1812 $avgage= mysql_fetch_array($r,MYSQL_NUM);
1816 output_home_page($pre[0],$game[0],$done[0],$avgage[0]);