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 $names = DB_get_all_names();
37 output_form_for_new_game($names);
39 /*check if everything is ready to set up a new game */
40 else if( myisset("PlayerA", "PlayerB","PlayerC","PlayerD","dullen","schweinchen","call" ))
42 $PlayerA = $_REQUEST["PlayerA"];
43 $PlayerB = $_REQUEST["PlayerB"];
44 $PlayerC = $_REQUEST["PlayerC"];
45 $PlayerD = $_REQUEST["PlayerD"];
47 $dullen = $_REQUEST["dullen"];
48 $schweinchen = $_REQUEST["schweinchen"];
49 $call = $_REQUEST["call"];
51 $EmailA = DB_get_email_by_name($PlayerA);
52 $EmailB = DB_get_email_by_name($PlayerB);
53 $EmailC = DB_get_email_by_name($PlayerC);
54 $EmailD = DB_get_email_by_name($PlayerD);
56 if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
58 echo "couldn't find one of the names, please start a new game";
64 $useridA = DB_get_userid_by_name($PlayerA);
65 $useridB = DB_get_userid_by_name($PlayerB);
66 $useridC = DB_get_userid_by_name($PlayerC);
67 $useridD = DB_get_userid_by_name($PlayerD);
69 /* create random numbers */
70 $randomNR = create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD);
71 $randomNRstring = join(":",$randomNR);
75 if(myisset("followup") )
77 $followup= $_REQUEST["followup"];
78 $session = DB_get_session_by_gameid($followup);
79 $ruleset = DB_get_ruleset_by_gameid($followup); /* just copy ruleset from old game,
80 this way no manipulation is possible */
82 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
83 "'$ruleset','$session' ,NULL)");
87 $max = DB_get_max_session();
89 mysql_query("UPDATE Game SET session='".$max."' WHERE id=".DB_quote_smart($followup));
90 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
91 "'$ruleset','$max' ,NULL)");
96 /* get ruleset information or create new one */
97 $ruleset = DB_get_ruleset($dullen,$schweinchen,$call);
100 myerror("Error defining ruleset: $ruleset");
106 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre', ".
107 "'$ruleset',NULL ,NULL)");
109 $game_id = mysql_insert_id();
112 $TIME = (string) time(); /* to avoid collisions */
113 $hashA = md5("AGameOfDoko".$game_id.$PlayerA.$EmailA.$TIME);
114 $hashB = md5("AGameOfDoko".$game_id.$PlayerB.$EmailB.$TIME);
115 $hashC = md5("AGameOfDoko".$game_id.$PlayerC.$EmailC.$TIME);
116 $hashD = md5("AGameOfDoko".$game_id.$PlayerD.$EmailD.$TIME);
119 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridA).
120 ", ".DB_quote_smart($hashA).", 'start','1',NULL,NULL,NULL,NULL)");
121 $hand_idA = mysql_insert_id();
122 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridB).
123 ", ".DB_quote_smart($hashB).", 'start','2',NULL,NULL,NULL,NULL)");
124 $hand_idB = mysql_insert_id();
125 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridC).
126 ", ".DB_quote_smart($hashC).", 'start','3',NULL,NULL,NULL,NULL)");
127 $hand_idC = mysql_insert_id();
128 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridD).
129 ", ".DB_quote_smart($hashD).", 'start','4',NULL,NULL,NULL,NULL)");
130 $hand_idD = mysql_insert_id();
134 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idA', '".$randomNR[$i]."', 'false')");
135 for($i=12;$i<24;$i++)
136 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idB', '".$randomNR[$i]."', 'false')");
137 for($i=24;$i<36;$i++)
138 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idC', '".$randomNR[$i]."', 'false')");
139 for($i=36;$i<48;$i++)
140 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idD', '".$randomNR[$i]."', 'false')");
142 /* send out email, TODO: check for error with email */
144 "you are invited to play a game of DoKo (that is to debug the program ;).\n".
145 "Place comments and bug reports here:\n".
146 "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
147 "The whole round would consist of the following players:\n".
152 "If you want to join this game, please follow this link:\n\n".
155 mymail($EmailA,"You are invited to a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
156 mymail($EmailB,"You are invited to a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
157 mymail($EmailC,"You are invited to a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
158 mymail($EmailD,"You are invited to a game of DoKo","Hello $PlayerD,\n".$message.$hashD);
160 echo "You started a new game. The emails have been sent out!";
161 } /* end set up a new game */
162 /* cancle a game, if nothing has happend in the last N minutes */
163 else if(myisset("cancle","me"))
165 $me = $_REQUEST["me"];
167 /* test for valid ID */
168 $myid = DB_get_userid_by_hash($me);
171 echo "Can't find you in the database, please check the url.<br />\n";
172 echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
178 DB_update_user_timestamp($myid);
180 /* get some information from the DB */
181 $gameid = DB_get_gameid_by_hash($me);
182 $myname = DB_get_name_by_hash($me);
184 /* check if game really is old enough */
185 $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
186 $r = mysql_fetch_array($result,MYSQL_NUM);
187 if(time()-strtotime($r[0]) > 60*60*24*30) /* = 1 month */
189 $message = "Hello, \n\n".
190 "Game $gameid has been cancled since nothing happend for a while and $myname requested it.\n";
192 $userids = DB_get_all_userid_by_gameid($gameid);
193 foreach($userids as $user)
195 $To = DB_get_email_by_userid($user);
196 mymail($To,$EmailName."game $gameid cancled (timed out)",$message);
199 /* delete everything from the dB */
202 echo "<p style=\"background-color:red\";>Game $gameid has been cancled.<br /><br /></p>";
205 echo "<p>You need to wait longer before you can cancle a game...</p>\n";
207 /* handle request from one specific player for one game,
208 * (the hash is set on a per game base) */
209 else if(myisset("me"))
211 $me = $_REQUEST["me"];
213 /* test for valid ID */
214 $myid = DB_get_userid_by_hash($me);
217 echo "Can't find you in the database, please check the url.<br />\n";
218 echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
224 if(isset($_SESSION["name"]))
225 output_status($_SESSION["name"]);
227 /* the user had done something, update the timestamp */
228 DB_update_user_timestamp($myid);
230 /* get some information from the DB */
231 $gameid = DB_get_gameid_by_hash($me);
232 $myname = DB_get_name_by_hash($me);
233 $mystatus = DB_get_status_by_hash($me);
234 $mypos = DB_get_pos_by_hash($me);
235 $myhand = DB_get_handid_by_hash($me);
236 $session = DB_get_session_by_gameid($gameid);
238 /* get prefs and save them */
242 /* get rule set for this game */
243 $result = mysql_query("SELECT * FROM Rulesets".
244 " LEFT JOIN Game ON Game.ruleset=Rulesets.id ".
245 " WHERE Game.id='$gameid'" );
246 $r = mysql_fetch_array($result,MYSQL_NUM);
248 $RULES["dullen"] = $r[2];
249 $RULES["schweinchen"] = $r[3];
250 $RULES["call"] = $r[4];
252 /* get some infos about the game */
253 $gametype = DB_get_gametype_by_gameid($gameid);
254 $gamestatus = DB_get_game_status_by_gameid($gameid);
256 if($gametype=="solo")
258 $gametype = DB_get_solo_by_gameid($gameid);
259 $GT = $gametype." ".$GT;
262 /* does anyone have both foxes */
263 $GAME["schweinchen"]=0;
266 $hash = DB_get_hash_from_game_and_pos($gameid,$i);
267 $cards = DB_get_all_hand($hash);
268 if( in_array("19",$cards) && in_array("20",$cards) )
270 $GAME["schweinchen"]=1;
271 $GAME["schweinchen-who"]=$hash;
275 /* put everyting in a form */
276 echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
278 /* output left menu */
282 /* output right menu */
284 /* display rule set for this game */
285 echo "<div class=\"gameinfo\">\n";
287 if($gamestatus != 'pre')
288 echo " Gametype: $GT <br />\n";
290 echo "Rules: <br />\n";
291 echo "10ofhearts : ".$RULES["dullen"] ."<br />\n";
292 echo "schweinchen: ".$RULES["schweinchen"] ."<br />\n";
293 echo "call: ".$RULES["call"] ."<br />\n";
296 if($mystatus == 'play' )
298 output_form_calls($me);
300 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
304 echo "<input type=\"submit\" value=\"submit\" />\n";
307 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
311 $session = DB_get_session_by_gameid($gameid);
312 $result = mysql_query("SELECT id,create_date FROM Game".
313 " WHERE session=$session".
314 " ORDER BY create_date DESC".
318 $r = mysql_fetch_array($result,MYSQL_NUM);
320 if(!$session || $gameid==$r[0])
322 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
323 $names = DB_get_all_names_by_gameid($gameid);
324 $type = DB_get_gametype_by_gameid($gameid);
327 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
329 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
337 /* output extra division in case this game is part of a session */
340 echo "<div class=\"session\">\n".
341 "This game is part of session $session: \n";
342 $hashes = DB_get_hashes_by_session($session,$myid);
344 foreach($hashes as $hash)
349 echo "<a href=\"".$host."?me=".$hash."\">$i</a> ";
355 /* display the table and the names */
358 /* mystatus gets the player through the different stages of a game.
360 * init: check values from start,
362 * check: check for return values from init
363 * poverty: handle poverty, wait here until all player have reached this state
364 * display sickness and move on to game
365 * play: game in progress
366 * gameover: are we revisiting a game
373 output_check_want_to_play($me);
378 /* move on to the next stage*/
379 DB_set_hand_status_by_hash($me,'init');
382 /* first check if everything went ok in the last step
383 * if not, send user back, if yes, check what he did
387 echo "<p> You need to answer the <a href=\"$host?me=$me\">question</a>.</p>";
388 DB_set_hand_status_by_hash($me,'start');
392 if($_REQUEST["in"] == "no")
394 /* cancel the game */
395 $message = "Hello, \n\n".
396 "the game has been canceled due to the request of one of the players.\n";
398 $userids = DB_get_all_userid_by_gameid($gameid);
399 foreach($userids as $user)
401 $To = DB_get_email_by_userid($user);
402 mymail($To,$EmailName."game $gameid canceled",$message);
405 /* delete everything from the dB */
410 $mycards = DB_get_hand($me);
413 output_check_for_sickness($me,$mycards);
415 echo "<p class=\"mycards\">your cards are: <br />\n";
416 foreach($mycards as $card)
417 display_card($card,$PREF["cardset"]);
420 /* move on to the next stage*/
421 DB_set_hand_status_by_hash($me,'check');
427 /* ok, user is in the game, saw his cards and selected his vorbehalt
428 * so first we check what he selected
430 if(!myisset("solo","wedding","poverty","nines") )
432 /* all these variables have a pre-selected default,
433 * so we should never get here,
434 * unless a user tries to cheat ;)
435 * can also happen if user reloads the page!
437 echo "<p> You need to answer the <a href=\"$host?me=$me&in=yes\">questions</a>.</p>";
438 DB_set_hand_status_by_hash($me,'init');
442 echo "Processing what you selected in the last step...<br />";
444 /* check if this sickness needs to be handled first */
445 $gametype = DB_get_gametype_by_gameid($gameid);
446 $startplayer = DB_get_startplayer_by_gameid($gameid);
448 if( $_REQUEST["solo"]!="No")
450 /* user wants to play a solo */
452 /* store the info in the user's hand info */
453 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
454 DB_set_sickness_by_hash($me,"solo");
456 echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
458 if($gametype == "solo" && $startplayer<$mypos)
459 {}/* do nothing, since someone else already is playing solo */
462 /* this solo comes first
463 * store info in game table
465 DB_set_gametype_by_gameid($gameid,"solo");
466 DB_set_startplayer_by_gameid($gameid,$mypos);
467 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
470 else if($_REQUEST["wedding"] == "yes")
472 /* TODO: add silent solo somewhere*/
473 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
474 DB_set_sickness_by_hash($me,"wedding");
476 else if($_REQUEST["poverty"] == "yes")
478 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
479 DB_set_sickness_by_hash($me,"poverty");
481 else if($_REQUEST["nines"] == "yes")
483 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
484 " is playing solo, this game will be canceled.<br />\n";
485 DB_set_sickness_by_hash($me,"nines");
488 echo " Ok, done with checking, please go to the <a href=\"$host?me=$me\">next step of the setup</a>.<br />";
490 /* move on to the next stage*/
491 DB_set_hand_status_by_hash($me,'poverty');
493 /* check if everyone has reached this stage, send out email */
494 $userids = DB_get_all_userid_by_gameid($gameid);
496 foreach($userids as $user)
498 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
499 if($userstat!='poverty' && $userstat!='play')
502 DB_set_player_by_gameid($gameid,$user);
507 /* reset player = everyone has to do something now */
508 DB_set_player_by_gameid($gameid,NULL);
510 foreach($userids as $user)
512 $To = DB_get_email_by_userid($user);
513 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
516 $message = "Everyone finish the questionary in game $gameid, ".
517 "please visit this link now to continue: \n".
518 " ".$host."?me=".$userhash."\n\n" ;
519 mymail($To,$EmailName." finished setup in game $gameid",$message);
528 /* here we need to check if there is a solo or some other form of sickness.
529 * If so, which one is the most important one
530 * set that one in the Game table
531 * tell people about it.
533 echo "<br /> Checking if someone else selected solo, nines, wedding or poverty.<br />";
535 /* check if everyone has reached this stage */
536 $userids = DB_get_all_userid_by_gameid($gameid);
538 foreach($userids as $user)
540 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
541 if($userstat!='poverty' && $userstat!='play')
547 echo "This step can only be handled after everyone finished the last step. ".
548 "Seems like this is not the case, so you need to wait a bit... ".
549 "you will get an email once that is the case, please use the link in ".
550 "that email to continue the game.<br />";
554 echo "Everyone has finished checking their cards, let's see what they said...<br />";
556 /* check what kind of game we are playing, in case there are any solos this already
557 *will have the correct information in it */
558 $gametype = DB_get_gametype_by_gameid($gameid);
559 $startplayer = DB_get_startplayer_by_gameid($gameid);
561 /* check for different sickness and just output a general info */
566 foreach($userids as $user)
568 $name = DB_get_name_by_userid($user);
569 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
570 if($usersick == 'nines')
573 echo "$name has a Vorbehalt. <br />";
576 else if($usersick == 'poverty')
579 echo "$name has a Vorbehalt. <br />";
581 else if($usersick == 'wedding')
584 echo "$name has a Vorbehalt. <br />" ;
586 else if($usersick == 'solo')
589 echo "$name has a Vorbehalt. <br />" ;
593 /* now check which sickness comes first and set the gametype to it */
595 if($gametype == "solo")
602 /* TODO: should we keep statistics of this? */
603 $message = "Hello, \n\n".
604 " the game has been canceled because ".DB_get_name_by_userid($nines).
605 " has five or more nines and nobody is playing solo.\n\n".
606 " To redeal either start a new game or, in case the game was part of a tournament, \n".
607 " go to the last game and use the link at the bottom of the page to redeal.";
609 $userids = DB_get_all_userid_by_gameid($gameid);
610 foreach($userids as $user)
612 $To = DB_get_email_by_userid($user);
613 mymail($To,$EmailName."game $gameid canceled",$message);
616 /* delete everything from the dB */
619 echo "The game has been canceled because ".DB_get_name_by_userid($nines).
620 " has five or more nines and nobody is playing solo.\n";
625 else if($poverty==1) /* one person has poverty */
627 DB_set_gametype_by_gameid($gameid,"poverty");
628 $gametype = "poverty";
629 $who = DB_get_sickness_by_gameid($gameid);
632 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
633 if($firstsick == "poverty")
634 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
636 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
639 else if($poverty==2) /* two people have poverty */
641 DB_set_gametype_by_gameid($gameid,"dpoverty");
642 $gametype = "dpoverty";
643 $who = DB_get_sickness_by_gameid($gameid);
646 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
647 if($firstsick == "poverty")
649 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
650 if($secondsick == "poverty")
651 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
653 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
656 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
661 DB_set_gametype_by_gameid($gameid,"wedding");
662 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
663 $gametype = "wedding";
668 /* now the gametype is set correctly (shouldn't matter that this is calculated for every user)
669 * output what kind of game we have */
672 foreach($userids as $user)
674 /* userids are sorted by position...
675 * so output whatever the first one has, then whatever the next one has
676 * stop when the sickness is the same as the gametype
679 $name = DB_get_name_by_userid($user);
680 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
683 echo "$name has $usersick. <br />"; /*TODO: perhaps save this in a string and store in Game? */
685 if($usersick=="poverty")
687 if($usersick == "wedding" && $gametype =="wedding")
689 if($usersick == "poverty" && $gametype =="poverty")
691 if($usersick == "poverty" && $gametype =="dpoverty" && $poverty==2)
693 if($usersick == "solo" && $gametype =="solo")
697 /* output Schweinchen in case the rules need it */
698 if( $gametype != "solo")
699 if($GAME["schweinchen"] && $RULES["schweinchen"]=="both" )
700 echo DB_get_name_by_hash($GAME["schweinchen-who"])." has Schweinchen. <br />";
704 /* finished the setup, set re/contra parties if possible, go to next stage unless there is a case of poverty*/
708 /* are we the solo player? set us to re, else set us to contra */
709 $pos = DB_get_pos_by_hash($me);
710 if($pos == $startplayer)
711 DB_set_party_by_hash($me,"re");
713 DB_set_party_by_hash($me,"contra");
714 DB_set_hand_status_by_hash($me,'play');
718 /* set person with the wedding to re, do the rest during the game */
719 $usersick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
720 if($usersick == "wedding")
721 DB_set_party_by_hash($me,"re");
723 DB_set_party_by_hash($me,"contra");
725 echo "Whoever will make the first trick will be on the re team. <br />\n";
726 echo " Ok, the game can start now, please finish <a href=\"$host?me=$me\">the setup</a>.<br />";
727 DB_set_hand_status_by_hash($me,'play');
731 $hand = DB_get_all_hand($me);
733 if(in_array('3',$hand)||in_array('4',$hand))
734 DB_set_party_by_hash($me,"re");
736 DB_set_party_by_hash($me,"contra");
737 DB_set_hand_status_by_hash($me,'play');
741 /* check if poverty resolved (e.g. DB.Game who set to NULL)
742 * yes? =>trump was taken, start game; break;
744 $who = DB_get_sickness_by_gameid($gameid);
746 { /* trump has been taken */
747 DB_set_hand_status_by_hash($me,'play');
751 if($who>9) /*= two people still have trump on the table*/
756 /* check if we are being asked now
757 * no? display wait message, e.g. player X is asked at the moment
759 $usersick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
760 if(myisset("trump") && $_REQUEST["trump"]=="no" && ($who==$mypos || $who==$mypos*10))
762 /* user doesn't want to take trump */
763 /* set next player who needs to be asked */
764 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
765 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
767 if($firstsick=="poverty")
769 if($secondsick=="poverty")
770 DB_set_sickness_by_gameid($gameid,$who+$add*3);
772 DB_set_sickness_by_gameid($gameid,$who+$add*2);
775 DB_set_sickness_by_gameid($gameid,$who+$add);
777 /* email next player */
778 $who = DB_get_sickness_by_gameid($gameid);
779 if($who>9) $who = $who/10;
783 $To = DB_get_email_by_pos_and_gameid($who,$gameid);
784 $userhash = DB_get_hash_from_game_and_pos($gameid,$who);
785 DB_set_player_by_gameid($gameid,$who);
787 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
788 " ".$host."?me=".$userhash."\n\n" ;
789 mymail($To,$EmailName." poverty (game $gameid)",$message);
792 /* this user is done */
793 DB_set_hand_status_by_hash($me,'play');
796 else if(myisset("trump") && !myisset("exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
798 /* user wants to take trump */
799 $trump = $_REQUEST["trump"];
801 /* get hand id for user $trump */
802 $userhand = DB_get_handid_by_gameid_and_userid($gameid,$trump);
803 /* copy trump from player A to B */
804 $result = mysql_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
806 /* add hidden button with trump in it to get to the next point */
807 echo "<div class=\"poverty\">\n";
808 echo " <input type=\"hidden\" name=\"exchange\" value=\"-1\" />\n";
809 echo " <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
810 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select cards to give back\" />\n";
813 else if(myisset("trump","exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
815 $trump = $_REQUEST["trump"];
816 $exchange = $_REQUEST["exchange"];
817 $userhand = DB_get_handid_by_gameid_and_userid($gameid,$trump);
819 /* if exchange is set to a value>0, exchange that card back to user $trump */
822 $result = mysql_query("UPDATE Hand_Card SET hand_id='$userhand'".
823 " WHERE hand_id='$myhand' AND card_id='$exchange'" );
826 /* if number of cards == 12, set status to play for both users */
827 $result = mysql_query("SELECT COUNT(*) FROM Hand_Card WHERE hand_id='$myhand'" );
828 $r = mysql_fetch_array($result,MYSQL_NUM);
831 myerror("error in poverty");
836 if($gametype=="poverty" || $who<9)
838 DB_set_sickness_by_gameid($gameid,-1); /* done with poverty */
840 else /* reduce poverty count by one, that is go to single digits $who */
845 /* whom to ask next */
846 $firstsick = DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
847 $secondsick = DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
849 if($firstsick!="poverty")
850 DB_set_sickness_by_gameid($gameid,$who+$add);
853 if($secondsick!="poverty")
854 DB_set_sickness_by_gameid($gameid,$who+$add*2);
856 DB_set_sickness_by_gameid($gameid,$who+$add*3);
859 /* email next player */
860 $who = DB_get_sickness_by_gameid($gameid);
863 $To = DB_get_email_by_pos_and_gameid($who,$gameid);
864 $userhash = DB_get_hash_from_game_and_pos($gameid,$who);
865 DB_set_player_by_gameid($gameid,$who);
867 $message = "Someone has poverty, it's your turn to decide, ".
868 "if you want to take the trump. Please visit:".
869 " ".$host."?me=".$userhash."\n\n" ;
870 mymail($To,$EmailName." poverty (game $gameid)",$message);
874 /* this user is done */
875 DB_set_hand_status_by_hash($me,'play');
876 /* and so is his partner */
877 $hash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
878 DB_set_hand_status_by_hash($hash,'play');
880 /* set party to re, unless we had dpoverty, in that case check if we need to set re/contra*/
882 foreach($userids as $user)
884 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
885 $party = DB_get_party_by_hash($userhash);
891 DB_set_party_by_hash($me,"contra");
892 DB_set_party_by_hash($hash,"contra");
896 foreach($userids as $user)
898 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
899 if($userhash==$hash||$userhash==$me)
900 DB_set_party_by_hash($userhash,"re");
902 DB_set_party_by_hash($userhash,"contra");
911 /* else show all trump, have lowest card pre-selected, have hidden setting for */
912 echo "<div class=\"poverty\"> you need to get rid of a few cards</div>\n";
914 set_gametype($gametype); /* this sets the $CARDS variable */
915 $mycards = DB_get_hand($me);
916 $mycards = mysort($mycards,$gametype);
919 echo "<div class=\"mycards\">Your cards are: <br />\n";
920 foreach($mycards as $card)
921 display_link_card($card,$PREF["cardset"],$type);
922 echo " <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
923 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select one card to give back\" />\n";
927 else if($who == $mypos || $who == $mypos*10)
929 echo "<div class=\"poverty\">\n";
930 foreach($userids as $user)
932 $name = DB_get_name_by_userid($user);
933 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
935 if($usersick=="poverty")
937 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
938 $cards = DB_get_hand($hash);
939 $nrtrump = count_trump($cards);
942 echo "Player $name has $nrtrump trump. Do you want to take them?".
943 "<a href=\"index.php?me=$me&trump=$user\">yes</a> <br />\n";
946 echo "<a href=\"index.php?me=$me&trump=no\">No,way I take those trump...</a> <br />\n";
949 echo "Your cards are: <br />\n";
950 $mycards = DB_get_hand($me);
952 echo "<p class=\"mycards\">your cards are: <br />\n";
953 foreach($mycards as $card)
954 display_card($card,$PREF["cardset"]);
959 $mysick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
960 if($mysick=="poverty")
961 echo "The others are asked if they want to take your trump, you have to wait (you'll get an email).";
963 echo "it's not your turn yet to decide if you want to take the trump or not.";
966 /* check if no one wanted to take trump, in that case the gamesickness would be set to 5 or 50 */
967 $who = DB_get_sickness_by_gameid($gameid);
968 if($who==5 || $who==50)
970 $message = "Hello, \n\n".
971 "Game $gameid has been cancled since nobody wanted to take the trump.\n";
973 $userids = DB_get_all_userid_by_gameid($gameid);
974 foreach($userids as $user)
976 $To = DB_get_email_by_userid($user);
977 mymail($To,$EmailName."game $gameid cancled (poverty not resolved)",$message);
980 /* delete everything from the dB */
983 echo "<p style=\"background-color:red\";>Game $gameid has been cancled.<br /><br /></p>";
989 /* check if all players are ready to play */
991 foreach($userids as $user)
992 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='play')
995 DB_set_player_by_gameid($gameid,$user);
1000 /* only set this after all poverty, etc. are handled*/
1001 DB_set_game_status_by_gameid($gameid,'play');
1003 /* email startplayer */
1004 $startplayer = DB_get_startplayer_by_gameid($gameid);
1005 $email = DB_get_email_by_pos_and_gameid($startplayer,$gameid);
1006 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
1007 $who = DB_get_userid_by_email($email);
1008 DB_set_player_by_gameid($gameid,$who);
1012 /* email startplayer) */
1013 $message = "It's your turn now in game $gameid.\n".
1014 "Use this link to play a card: ".$host."?me=".$hash."\n\n" ;
1015 mymail($email,$EmailName."ready, set, go... (game $gameid) ",$message);
1018 echo " Please, <a href=\"$host?me=$me\">start</a> the game.<br />";
1026 /* both entries here, so that the tricks are visible for both.
1027 * in case of 'play' there is a break later that skips the last part
1030 /* figure out what kind of game we are playing,
1031 * set the global variables $CARDS["trump"],$CARDS["diamonds"],$CARDS["hearts"],
1032 * $CARDS["clubs"],$CARDS["spades"],$CARDS["foxes"]
1036 $gametype = DB_get_gametype_by_gameid($gameid);
1038 if($gametype=="solo")
1040 $gametype = DB_get_solo_by_gameid($gameid);
1041 $GT = $gametype." ".$GT;
1044 $gametype = "normal";
1046 set_gametype($gametype); /* this sets the $CARDS variable */
1048 /* get some infos about the game */
1049 $gamestatus = DB_get_game_status_by_gameid($gameid);
1051 /* has the game started? No, then just wait here...*/
1052 if($gamestatus == 'pre')
1054 echo "You finished the setup, but not everyone else finished it... ".
1055 "so you need to wait for the others. Just wait for the an email... <br />";
1056 break; /* not sure this works... the idea is that you can
1057 * only play a card after everyone is ready to play */
1060 /* get time from the last action of the game */
1061 $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
1062 $r = mysql_fetch_array($result,MYSQL_NUM);
1063 $gameend = time() - strtotime($r[0]);
1065 /* handel comments in case player didn't play a card, allow comments a week after the end of the game */
1066 if( (!myisset("card") && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
1067 if(myisset("comment"))
1069 $comment = $_REQUEST["comment"];
1070 $playid = DB_get_current_playid($gameid);
1073 DB_insert_comment($comment,$playid,$myid);
1076 /* get everything relevant to display the tricks */
1077 $result = mysql_query("SELECT Hand_Card.card_id as card,".
1078 " Hand.position as position,".
1079 " Play.sequence as sequence, ".
1081 " GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>') SEPARATOR '\n' ), ".
1082 " Play.create_date, ".
1085 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1086 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1087 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1088 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1089 "LEFT JOIN User On User.id=Comment.user_id ".
1090 "WHERE Trick.game_id='".$gameid."' ".
1091 "GROUP BY Trick.id, sequence ".
1092 "ORDER BY Trick.id, sequence ASC");
1094 $lasttrick = DB_get_max_trickid($gameid);
1096 $play = array(); /* needed to calculate winner later */
1098 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1099 $firstcard = ""; /* first card in a trick */
1101 echo "\n<ul class=\"tricks\">\n";
1102 echo " <li class=\"nohighlight\"> Game $gameid: </li>\n";
1104 while($r = mysql_fetch_array($result,MYSQL_NUM))
1110 $timeplayed = strtotime($r[5]);
1113 $offset = DB_get_user_timezone($user);
1114 $zone = return_timezone($offset);
1115 date_default_timezone_set($zone);
1117 /* check if first schweinchen has been played */
1118 if($r[0] == 19 || $r[0] == 20 )
1119 $GAME["schweinchen"]++;
1121 /* save card to be able to find the winner of the trick later */
1122 $play[$seq] = array("card"=>$r[0],"pos"=>$pos);
1126 /* first card in a trick, output some html */
1127 if($trick!=$lasttrick)
1129 /* start of an old trick? */
1130 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1131 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1132 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1134 else if($trick==$lasttrick)
1136 /* start of a last trick? */
1137 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1138 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1139 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1142 /* remember first card, so that we are able to check, what cards can be played */
1147 echo " <div class=\"card".($pos-1)."\">\n";
1149 /* display comments */
1151 echo " <span class=\"comment\">".$comment."</span>\n";
1154 display_card($r[0],$PREF["cardset"]);
1156 echo " </div>\n"; /* end div card */
1162 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1166 /* whos turn is it? */
1169 $winner = get_winner($play,$gametype); /* returns the position */
1171 $firstcard = ""; /* new trick, no first card */
1176 if($next==5) $next = 1;
1179 /* my turn?, display cards as links, ask for comments*/
1180 if(DB_get_pos_by_hash($me) == $next)
1185 /* do we want to play a card? */
1186 if(myisset("card") && $myturn)
1188 $card = $_REQUEST["card"];
1189 $handid = DB_get_handid_by_hash($me);
1191 /* check if we have card and that we haven't played it yet*/
1192 /* set played in hand_card to true where hand_id and card_id*/
1193 $result = mysql_query("SELECT id FROM Hand_Card WHERE played='false' and ".
1194 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1195 $r = mysql_fetch_array($result,MYSQL_NUM);
1196 $handcardid = $r[0];
1198 if($handcardid) /* everything ok, play card */
1200 /* update Game timestamp */
1201 DB_update_game_timestamp($gameid);
1203 /* check if a call was made, must do this before we set the card status to played */
1204 if(myisset("call120") && $_REQUEST["call120"] == "yes" && can_call(120,$me))
1205 $result = mysql_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
1206 if(myisset("call90") && $_REQUEST["call90"] == "yes" && can_call(90,$me))
1207 $result = mysql_query("UPDATE Hand SET point_call='90' WHERE hash='$me' ");
1208 if(myisset("call60") && $_REQUEST["call60"] == "yes" && can_call(60,$me))
1209 $result = mysql_query("UPDATE Hand SET point_call='60' WHERE hash='$me' ");
1210 if(myisset("call30") && $_REQUEST["call30"] == "yes" && can_call(30,$me))
1211 $result = mysql_query("UPDATE Hand SET point_call='30' WHERE hash='$me' ");
1212 if(myisset("call0") && $_REQUEST["call0"] == "yes" && can_call(0,$me))
1213 $result = mysql_query("UPDATE Hand SET point_call='0' WHERE hash='$me' ");
1215 /* mark card as played */
1216 mysql_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1217 DB_quote_smart($card));
1219 /* get trick id or start new trick */
1220 $a = DB_get_current_trickid($gameid);
1225 $playid = DB_play_card($trickid,$handcardid,$sequence);
1227 /* check for schweinchen */
1228 if($card == 19 || $card == 20 )
1230 $GAME["schweinchen"]++;
1231 if($GAME["schweinchen"]==3 && $RULES["schweinchen"]=="second" )
1232 DB_insert_comment("Schweinchen! ",$playid,$myid);
1233 if($RULES["schweinchen"]=="both" )
1234 DB_insert_comment("Schweinchen! ",$playid,$myid);
1236 echo "schweinchen = ".$GAME["schweinchen"]." ---<br />";
1239 /* if sequence == 4 check who one in case of wedding */
1240 if($sequence == 4 && $GT == "wedding")
1242 /* is wedding resolve */
1243 $resolved = DB_get_sickness_by_gameid($gameid);
1246 /* who has wedding */
1247 $userids = DB_get_all_userid_by_gameid($gameid);
1248 foreach($userids as $user)
1250 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1251 if($usersick == "wedding")
1254 /* who won the trick */
1255 $play = DB_get_cards_by_trick($trickid);
1256 $winner = get_winner($play,$gametype); /* returns the position */
1257 $winnerid = DB_get_userid_by_gameid_and_position($gameid,$winner);
1258 /* is tricknr <=3 */
1259 if($tricknr <=3 && $winnerid!=$whosick)
1261 /* set resolved at tricknr*/
1262 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1264 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1265 DB_set_party_by_hash($whash,"re");
1267 if($tricknr == 3 && $winnerid==$whosick)
1269 /* set resolved at tricknr*/
1270 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1275 /* if sequence == 4, set winner of the trick, count points and set the next player */
1278 $play = DB_get_cards_by_trick($trickid);
1279 $winner = get_winner($play,$gametype); /* returns the position */
1282 mysql_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1284 echo "ERROR during scoring";
1287 echo "DEBUG: position $winner won the trick <br />";
1289 /* who is the next player? */
1294 $next = DB_get_pos_by_hash($me)+1;
1296 if($next==5) $next=1;
1298 /* check for coment */
1299 if(myisset("comment"))
1301 $comment = $_REQUEST["comment"];
1303 DB_insert_comment($comment,$playid,$myid);
1306 /* display played card */
1307 $pos = DB_get_pos_by_hash($me);
1310 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1311 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1312 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1315 echo "<div class=\"card".($pos-1)."\">";
1316 /* display comments */
1317 display_card($card,$PREF["cardset"]);
1319 echo " <span class=\"comment\"> ".$comment."</span>\n";
1320 echo "</div></div></li>\n";
1322 /*check if we still have cards left, else set status to gameover */
1323 if(sizeof(DB_get_hand($me))==0)
1325 DB_set_hand_status_by_hash($me,'gameover');
1326 $mystatus='gameover';
1329 /* if all players are done, set game status to game over,
1330 * get the points of the last trick and send out an email
1333 $userids = DB_get_all_userid_by_gameid($gameid);
1336 foreach($userids as $user)
1337 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1341 DB_set_game_status_by_gameid($gameid,"gameover");
1343 /* email next player, if game is still running */
1344 if(DB_get_game_status_by_gameid($gameid)=='play')
1346 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1347 $email = DB_get_email_by_hash($next_hash);
1348 $who = DB_get_userid_by_email($email);
1349 DB_set_player_by_gameid($gameid,$who);
1351 $message = "A card has been played in game $gameid.\n\n".
1352 "It's your turn now.\n".
1353 "Use this link to play a card: ".$host."?me=".$next_hash."\n\n" ;
1354 mymail($email,$EmailName."a card has been played in game $gameid",$message);
1356 else /* send out final email */
1358 /* individual score */
1359 $result = mysql_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1360 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1361 " LEFT JOIN User ON User.id=Hand.user_id".
1362 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1363 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1364 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1365 " WHERE Hand.game_id='$gameid'".
1366 " GROUP BY User.fullname" );
1367 $message = "The game is over. Thanks for playing :)\n";
1368 $message .= "Final score:\n";
1369 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1370 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1372 $result = mysql_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1373 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1374 " LEFT JOIN User ON User.id=Hand.user_id".
1375 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1376 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1377 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1378 " WHERE Hand.game_id='$gameid'".
1379 " GROUP BY Hand.party" );
1380 $message .= "\nTotals:\n";
1381 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1382 $message .= " ".$r[0]." ".$r[1]."\n";
1384 /* send out final email */
1387 foreach($userids as $user)
1388 $all[] = DB_get_email_by_userid($user);
1389 $To = implode(",",$all);
1391 $help = "\n\n (you can use reply all on this email to reach all the players.)\n";
1392 mymail($To,$EmailName."game over (game $gameid) part 1(2)",$message.$help);
1394 foreach($userids as $user)
1396 $To = DB_get_email_by_userid($user);
1397 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1399 $link = "Use this link to have a look at game $gameid: ".$host."?me=".$hash."\n\n" ;
1400 mymail($To,$EmailName."game over (game $gameid) part 2(2)",$link);
1406 echo "can't find that card?! <br />\n";
1409 else if(myisset("card") && !$myturn )
1411 echo "please wait until it's your turn! <br />\n";
1414 if($seq!=4 && $trickNR>1)
1415 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1417 /* display points in case game is over */
1418 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1420 echo " <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1421 " <div class=\"trick\" id=\"trick13\">\n";
1422 /* add pic for re/contra
1423 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1425 $result = mysql_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1426 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1427 " LEFT JOIN User ON User.id=Hand.user_id".
1428 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1429 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1430 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1431 " WHERE Hand.game_id='$gameid'".
1432 " GROUP BY User.fullname" );
1433 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1434 echo " <div class=\"card".($r[3]-1)."\">\n".
1435 " <span class=\"score\">".$r[2]."<br /> ".$r[1]."</span>\n".
1438 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1442 echo "</ul>\n"; /* end ul tricks*/
1444 $mycards = DB_get_hand($me);
1445 $mycards = mysort($mycards,$gametype);
1446 echo "<div class=\"mycards\">\n";
1448 if($myturn && !myisset("card") && $mystatus=='play' )
1450 echo "Hello ".$myname.", it's your turn! <br />\n";
1451 echo "Your cards are: <br />\n";
1453 /* do we have to follow suite? */
1455 if(have_suit($mycards,$firstcard))
1458 foreach($mycards as $card)
1460 if($followsuit && !same_type($card,$firstcard))
1461 display_card($card,$PREF["cardset"]);
1463 display_link_card($card,$PREF["cardset"]);
1466 else if($mystatus=='play' )
1468 echo "Your cards are: <br />\n";
1469 foreach($mycards as $card)
1470 display_card($card,$PREF["cardset"]);
1472 else if($mystatus=='gameover')
1474 /* get time from the last action of the game */
1475 $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
1476 $r = mysql_fetch_array($result,MYSQL_NUM);
1477 $gameend = time() - strtotime($r[0]);
1479 if( $gameend < 60*60*24*7 )
1481 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"30\" maxlength=\"100\" />\n";
1482 echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />\n";
1483 echo "<input type=\"submit\" value=\"submit\" />\n";
1486 $oldcards = DB_get_all_hand($me);
1487 $oldcards = mysort($oldcards,$gametype);
1488 echo "Your cards were: <br />\n";
1489 foreach($oldcards as $card)
1490 display_card($card,$PREF["cardset"]);
1492 $userids = DB_get_all_userid_by_gameid($gameid);
1493 foreach($userids as $user)
1495 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1501 $name = DB_get_name_by_userid($user);
1502 $oldcards = DB_get_all_hand($userhash);
1503 $oldcards = mysort($oldcards,$gametype);
1504 echo "$name's cards were: <br />\n";
1505 foreach($oldcards as $card)
1506 display_card($card,$PREF["cardset"]);
1512 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1513 if($mystatus=='play')
1516 /* the following happens only when the gamestatus is 'gameover' */
1517 /* check if game is over, display results */
1518 if(DB_get_game_status_by_gameid($gameid)=='play')
1520 echo "the game is over for you.. other people still need to play though";
1524 $result = mysql_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1525 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1526 " LEFT JOIN User ON User.id=Hand.user_id".
1527 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1528 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1529 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1530 " WHERE Hand.game_id='$gameid'".
1531 " GROUP BY Hand.party" );
1532 echo "<div class=\"total\"> Totals:<br />\n";
1533 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1534 echo " ".$r[0]." ".$r[1]."<br />\n";
1540 myerror("error in testing the status");
1547 /* user status page */
1548 else if( myisset("email","password") || isset($_SESSION["name"]) )
1550 /* test id and password, should really be done in one step */
1551 if(!isset($_SESSION["name"]))
1553 $email = $_REQUEST["email"];
1554 $password = $_REQUEST["password"];
1558 $name = $_SESSION["name"];
1559 $email = DB_get_email_by_name($name);
1560 $password = DB_get_passwd_by_name($name);
1563 if(myisset("forgot"))
1567 $uid = DB_get_userid_by_email($email);
1573 /* check how many entries in recovery table */
1574 $number = DB_get_number_of_passwords_recovery($uid);
1576 /* if less than N recent ones, add a new one and send out email */
1579 echo "Ok, I send you a new password. <br />";
1581 echo "N.B. You tried this already $number times during the last day and it will only work ".
1582 " 5 times during a day.<br />";
1583 echo "The new password will be valid for one day, make sure you reset it to something else.<br />";
1584 echo "Back to the <a href=\"$host\">main page</a>.";
1586 $TIME = (string) time(); /* to avoid collisions */
1587 $hash = md5("Anewpassword".$email.$TIME);
1588 $newpw = substr($hash,1,8);
1590 $message = "Someone (hopefully you) requested a new password. \n".
1591 "You can use this email and the following password: \n".
1593 "to log into the server. The new password is valid for 24h, so make\n".
1594 "sure you reset your password to something new. Your old password will\n".
1595 " also still be valid until you set a new one\n";
1596 mymail($email,$EmailName."recovery ",$message);
1598 DB_set_recovery_password($uid,md5($newpw));
1602 echo "Sorry you already tried 5 times during the last 24h.<br />".
1603 "You need to use one of those passwords or wait to get a new one.<br />";
1604 echo "Back to the <a href=\"$host\">main page</a>.";
1610 echo "You need to give me an email address! <br />".
1611 "Please try <a href=\"$host\">again</a>.";
1613 echo "Couldn't find a player with this email! <br />".
1614 "Please contact Arun, if you think this is a mistake <br />".
1615 "or else try <a href=\"$host\">again</a>.";
1620 /* verify password and email */
1621 if(strlen($password)!=32)
1622 $password = md5($password);
1625 $uid = DB_get_userid_by_email_and_password($email,$password);
1633 if(myisset("setpref"))
1635 $setpref=$_REQUEST["setpref"];
1639 case "englishcards":
1640 $result = mysql_query("SELECT * from User_Prefs".
1641 " WHERE user_id='$uid' AND pref_key='cardset'" );
1642 if( mysql_fetch_array($result,MYSQL_NUM))
1643 $result = mysql_query("UPDATE User_Prefs SET value=".DB_quote_smart($setpref).
1644 " WHERE user_id='$uid' AND pref_key='cardset'" );
1646 $result = mysql_query("INSERT INTO User_Prefs VALUES(NULL,'$uid','cardset',".
1647 DB_quote_smart($setpref).")");
1648 echo "Ok, changed you preferences for the cards.\n";
1652 else if(myisset("passwd"))
1654 if( $_REQUEST["passwd"]=="ask" )
1656 /* reset password form*/
1657 output_password_recovery($email,$password);
1659 else if($_REQUEST["passwd"]=="set")
1661 /* reset password */
1664 /* check if old password matches */
1665 $oldpasswd = md5($_REQUEST["password0"]);
1666 if(!( ($password == $oldpasswd) || DB_check_recovery_passwords($oldpasswd,$email) ))
1668 /* check if new passwords are types the same twice */
1669 if($_REQUEST["password1"] != $_REQUEST["password2"] )
1675 echo "The new passwords don't match. <br />";
1678 echo "The old password is not correct. <br />";
1681 echo "Changed the password.<br />";
1682 mysql_query("UPDATE User SET password='".md5($_REQUEST["password1"]).
1683 "' WHERE id=".DB_quote_smart($uid));
1689 else /* output default user page */
1691 $time = DB_get_user_timestamp($uid);
1692 $unixtime = strtotime($time);
1694 $offset = DB_get_user_timezone($uid);
1695 $zone = return_timezone($offset);
1696 date_default_timezone_set($zone);
1698 $myname = DB_get_name_by_email($email);
1699 $_SESSION["name"] = $myname;
1701 if(isset($_SESSION["name"]))
1702 output_status($_SESSION["name"]);
1704 /* display links to settings */
1705 output_user_settings($email,$password);
1707 echo "last login: ".date("r",$unixtime)."<br />";
1709 DB_update_user_timestamp($uid);
1711 echo "<p>These are your games that haven't started yet:<br />\n";
1712 $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.mod_date,Game.player from Hand".
1713 " LEFT JOIN Game On Hand.game_id=Game.id".
1714 " WHERE Hand.user_id='$uid' AND Game.status='pre'" );
1715 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1717 echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a>";
1718 if($r[3]==$uid || $r[3]==NULL)
1719 echo "(it's <strong>your</strong> turn)\n";
1722 $name = DB_get_name_by_userid($r[3]);
1723 echo "(it's $name's turn)\n";
1726 if(time()-strtotime($r[2]) > 60*60*24*30)
1727 echo " The game has been running for over a month.".
1728 " Do you want to cancel it? <a href=\"$host?cancle=1&me=".$r[0]."\">yes</a>".
1729 " (clicking here is final and can't be restored)";
1734 echo "<p>These are the games you are playing in:<br />\n";
1735 $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.mod_date,Game.player from Hand".
1736 " LEFT JOIN Game On Hand.game_id=Game.id".
1737 " WHERE Hand.user_id='$uid' AND Game.status='play'" );
1738 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1740 echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a>";
1744 echo "(it's <strong>your</strong> turn)\n";
1747 $name = DB_get_name_by_userid($r[3]);
1748 echo "(it's $name's turn)\n";
1751 if(time()-strtotime($r[2]) > 60*60*24*30)
1752 echo " The game has been running for over a month.".
1753 " Do you want to cancel it? <a href=\"$host?cancle=1&me=".$r[0]."\">yes</a>".
1754 " (clicking here is final and can't be restored)";
1760 echo "<p>And these are your games that are already done:<br />Game: \n";
1762 $result = mysql_query("SELECT hash,game_id from Hand WHERE user_id='$uid' AND status='gameover'" );
1763 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1764 $output[] = "<a href=\"".$host."?me=".$r[0]."\">#".$r[1]." </a>";
1765 echo implode(", ",$output)."</p>\n";
1767 $names = DB_get_all_names();
1768 echo "<p>Registered players:<br />\n";
1769 echo implode(", ",$names)."\n";
1772 echo "<p>Want to start a new game? Visit <a href=\"".$host."?new\">this page.</a></p>";
1777 echo "Sorry email and password don't match. Please <a href=\"$host\">try again</a>. <br />";
1784 /* default login page */
1787 $pre[0]=0;$game[0]=0;$done[0]=0;
1788 $r=mysql_query("SELECT COUNT(id) FROM Game GROUP BY status");
1790 $pre = mysql_fetch_array($r,MYSQL_NUM);
1791 $game = mysql_fetch_array($r,MYSQL_NUM);
1792 $done = mysql_fetch_array($r,MYSQL_NUM);
1795 $r=mysql_query("SELECT AVG(datediff(mod_date,create_date)) FROM Game where status='gameover' ");
1797 $avgage= mysql_fetch_array($r,MYSQL_NUM);
1801 output_home_page($pre[0],$game[0],$done[0],$avgage[0]);