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 /* check if there is a game in pre or play mode, in that case do nothing */
90 if( DB_is_session_active($session) > 0 )
92 echo "<p class=\"message\"> There is already a game going on in session $session, you can't start a new one</p>";
97 else if ( DB_is_session_active($session) < 0 )
99 echo "<p class=\"message\"> ERROR: status of session $session couldn't be determined.</p>";
106 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
107 "'$ruleset','$session' ,NULL)");
110 /* get max session */
111 $max = DB_get_max_session();
113 mysql_query("UPDATE Game SET session='".$max."' WHERE id=".DB_quote_smart($followup));
114 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
115 "'$ruleset','$max' ,NULL)");
120 /* get ruleset information or create new one */
121 $ruleset = DB_get_ruleset($dullen,$schweinchen,$call);
124 myerror("Error defining ruleset: $ruleset");
129 /* get max session */
130 $max = DB_get_max_session();
133 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre', ".
134 "'$ruleset','$max' ,NULL)");
136 $game_id = mysql_insert_id();
139 $TIME = (string) time(); /* to avoid collisions */
140 $hashA = md5("AGameOfDoko".$game_id.$PlayerA.$EmailA.$TIME);
141 $hashB = md5("AGameOfDoko".$game_id.$PlayerB.$EmailB.$TIME);
142 $hashC = md5("AGameOfDoko".$game_id.$PlayerC.$EmailC.$TIME);
143 $hashD = md5("AGameOfDoko".$game_id.$PlayerD.$EmailD.$TIME);
146 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridA).
147 ", ".DB_quote_smart($hashA).", 'start','1',NULL,NULL,NULL,NULL)");
148 $hand_idA = mysql_insert_id();
149 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridB).
150 ", ".DB_quote_smart($hashB).", 'start','2',NULL,NULL,NULL,NULL)");
151 $hand_idB = mysql_insert_id();
152 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridC).
153 ", ".DB_quote_smart($hashC).", 'start','3',NULL,NULL,NULL,NULL)");
154 $hand_idC = mysql_insert_id();
155 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridD).
156 ", ".DB_quote_smart($hashD).", 'start','4',NULL,NULL,NULL,NULL)");
157 $hand_idD = mysql_insert_id();
161 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idA', '".$randomNR[$i]."', 'false')");
162 for($i=12;$i<24;$i++)
163 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idB', '".$randomNR[$i]."', 'false')");
164 for($i=24;$i<36;$i++)
165 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idC', '".$randomNR[$i]."', 'false')");
166 for($i=36;$i<48;$i++)
167 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idD', '".$randomNR[$i]."', 'false')");
169 /* send out email, TODO: check for error with email */
171 "you are invited to play a game of DoKo (that is to debug the program ;).\n".
172 "Place comments and bug reports here:\n".
173 "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
174 "The whole round would consist of the following players:\n".
179 "If you want to join this game, please follow this link:\n\n".
182 mymail($EmailA,"You are invited to a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
183 mymail($EmailB,"You are invited to a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
184 mymail($EmailC,"You are invited to a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
185 mymail($EmailD,"You are invited to a game of DoKo","Hello $PlayerD,\n".$message.$hashD);
187 echo "You started a new game. The emails have been sent out!";
188 } /* end set up a new game */
189 /* cancle a game, if nothing has happend in the last N minutes */
190 else if(myisset("cancle","me"))
192 $me = $_REQUEST["me"];
194 /* test for valid ID */
195 $myid = DB_get_userid_by_hash($me);
198 echo "Can't find you in the database, please check the url.<br />\n";
199 echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
205 DB_update_user_timestamp($myid);
207 /* get some information from the DB */
208 $gameid = DB_get_gameid_by_hash($me);
209 $myname = DB_get_name_by_hash($me);
211 /* check if game really is old enough */
212 $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
213 $r = mysql_fetch_array($result,MYSQL_NUM);
214 if(time()-strtotime($r[0]) > 60*60*24*30) /* = 1 month */
216 $message = "Hello, \n\n".
217 "Game ".DB_format_gameid($gameid).
218 " has been cancled since nothing happend for a while and $myname requested it.\n";
220 $userids = DB_get_all_userid_by_gameid($gameid);
221 foreach($userids as $user)
223 $To = DB_get_email_by_userid($user);
224 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." cancled (timed out)",$message);
227 /* delete everything from the dB */
230 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid).
231 " has been cancled.<br /><br /></p>";
234 echo "<p>You need to wait longer before you can cancle a game...</p>\n";
236 /* send out a reminder */
237 else if(myisset("remind","me"))
239 $me = $_REQUEST["me"];
241 /* test for valid ID */
242 $myid = DB_get_userid_by_hash($me);
245 echo "Can't find you in the database, please check the url.<br />\n";
246 echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
252 DB_update_user_timestamp($myid);
254 /* get some information from the DB */
255 $gameid = DB_get_gameid_by_hash($me);
256 $myname = DB_get_name_by_hash($me);
258 /* check if game really is old enough */
259 $result = mysql_query("SELECT mod_date,player,status from Game WHERE id='$gameid' " );
260 $r = mysql_fetch_array($result,MYSQL_NUM);
261 if( (time()-strtotime($r[0]) > 60*60*24*7) && ($r[2]!='gameover') ) /* = 1 week */
263 $name = DB_get_name_by_userid($r[1]);
264 $To = DB_get_email_by_userid($r[1]);
265 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$r[1]);
267 $message = "Hello $name, \n\n".
268 "It's your turn in game ".DB_format_gameid($gameid)." \n".
269 "Actually everyone else is waiting for you for more than a week now ;)\n\n".
270 "Please visit this link now to continue: \n".
271 " ".$host."?me=".$userhash."\n\n" ;
273 if(DB_get_reminder($r[1],$gameid)>0)
275 echo "<p>An email has already been sent out.</p>\n";
279 DB_set_reminder($r[1],$gameid);
280 mymail($To,$EmailName."Reminder: game ".DB_format_gameid($gameid)." it's your turn",$message);
282 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid).
283 ": an email has been sent out.<br /><br /></p>";
287 echo "<p>You need to wait longer before you can send out a reminder...</p>\n";
289 /* handle request from one specific player for one game,
290 * (the hash is set on a per game base) */
291 else if(myisset("me"))
293 $me = $_REQUEST["me"];
295 /* test for valid ID */
296 $myid = DB_get_userid_by_hash($me);
299 echo "Can't find you in the database, please check the url.<br />\n";
300 echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
306 if(isset($_SESSION["name"]))
307 output_status($_SESSION["name"]);
309 /* the user had done something, update the timestamp */
310 DB_update_user_timestamp($myid);
312 /* get some information from the DB */
313 $gameid = DB_get_gameid_by_hash($me);
314 $myname = DB_get_name_by_hash($me);
315 $mystatus = DB_get_status_by_hash($me);
316 $mypos = DB_get_pos_by_hash($me);
317 $myhand = DB_get_handid_by_hash($me);
318 $session = DB_get_session_by_gameid($gameid);
320 /* get prefs and save them */
324 /* get rule set for this game */
325 $result = mysql_query("SELECT * FROM Rulesets".
326 " LEFT JOIN Game ON Game.ruleset=Rulesets.id ".
327 " WHERE Game.id='$gameid'" );
328 $r = mysql_fetch_array($result,MYSQL_NUM);
330 $RULES["dullen"] = $r[2];
331 $RULES["schweinchen"] = $r[3];
332 $RULES["call"] = $r[4];
334 /* get some infos about the game */
335 $gametype = DB_get_gametype_by_gameid($gameid);
336 $gamestatus = DB_get_game_status_by_gameid($gameid);
338 if($gametype=="solo")
340 $gametype = DB_get_solo_by_gameid($gameid);
341 $GT = $gametype." ".$GT;
344 /* does anyone have both foxes */
345 $GAME["schweinchen"]=0;
348 $hash = DB_get_hash_from_game_and_pos($gameid,$i);
349 $cards = DB_get_all_hand($hash);
350 if( in_array("19",$cards) && in_array("20",$cards) )
352 $GAME["schweinchen"]=1;
353 $GAME["schweinchen-who"]=$hash;
357 /* put everyting in a form */
358 echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
362 /* output extra division in case this game is part of a session */
365 echo "<div class=\"session\">\n".
366 "This game is part of session $session: \n";
367 $hashes = DB_get_hashes_by_session($session,$myid);
369 foreach($hashes as $hash)
374 echo "<a href=\"".$host."?me=".$hash."\">$i</a> \n";
380 /* display the table and the names */
383 /* mystatus gets the player through the different stages of a game.
385 * init: check values from start,
387 * check: check for return values from init
388 * poverty: handle poverty, wait here until all player have reached this state
389 * display sickness and move on to game
390 * play: game in progress
391 * gameover: are we revisiting a game
398 output_check_want_to_play($me);
403 /* move on to the next stage*/
404 DB_set_hand_status_by_hash($me,'init');
407 /* first check if everything went ok in the last step
408 * if not, send user back, if yes, check what he did
412 echo "<p class=\"message\"> You need to answer the <a href=\"$host?me=$me\">question</a>.</p>";
413 DB_set_hand_status_by_hash($me,'start');
417 if($_REQUEST["in"] == "no")
419 /* cancel the game */
420 $message = "Hello, \n\n".
421 "the game has been canceled due to the request of one of the players.\n";
423 $userids = DB_get_all_userid_by_gameid($gameid);
424 foreach($userids as $user)
426 $To = DB_get_email_by_userid($user);
427 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
430 /* delete everything from the dB */
435 $mycards = DB_get_hand($me);
438 output_check_for_sickness($me,$mycards);
440 echo "<p class=\"mycards\">your cards are: <br />\n";
441 foreach($mycards as $card)
442 display_card($card,$PREF["cardset"]);
445 /* move on to the next stage*/
446 DB_set_hand_status_by_hash($me,'check');
452 /* ok, user is in the game, saw his cards and selected his vorbehalt
453 * so first we check what he selected
455 if(!myisset("solo","wedding","poverty","nines") )
457 /* all these variables have a pre-selected default,
458 * so we should never get here,
459 * unless a user tries to cheat ;)
460 * can also happen if user reloads the page!
462 echo "<p class=\"message\"> You need to answer the <a href=\"$host?me=$me&in=yes\">questions</a>.</p>";
463 DB_set_hand_status_by_hash($me,'init');
467 /* check if someone selected more than one vorbehalt */
469 if($_REQUEST["solo"]!="No") $Nvorbehalt++;
470 if($_REQUEST["wedding"] == "yes") $Nvorbehalt++;
471 if($_REQUEST["poverty"] == "yes") $Nvorbehalt++;
472 if($_REQUEST["nines"] == "yes") $Nvorbehalt++;
476 echo "<p class=\"message\"> You selected more than one vorbehalt, please go back ".
477 "and answer the <a href=\"$host?me=$me&in=yes\">question</a> again.</p>";
478 DB_set_hand_status_by_hash($me,'init');
482 echo "<p class=\"message\">Processing what you selected in the last step...";
484 /* check if this sickness needs to be handled first */
485 $gametype = DB_get_gametype_by_gameid($gameid);
486 $startplayer = DB_get_startplayer_by_gameid($gameid);
488 if( $_REQUEST["solo"]!="No")
490 /* user wants to play a solo */
492 /* store the info in the user's hand info */
493 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
494 DB_set_sickness_by_hash($me,"solo");
496 echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
498 if($gametype == "solo" && $startplayer<$mypos)
499 {}/* do nothing, since someone else already is playing solo */
502 /* this solo comes first
503 * store info in game table
505 DB_set_gametype_by_gameid($gameid,"solo");
506 DB_set_startplayer_by_gameid($gameid,$mypos);
507 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
510 else if($_REQUEST["wedding"] == "yes")
512 /* TODO: add silent solo somewhere*/
513 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
514 DB_set_sickness_by_hash($me,"wedding");
516 else if($_REQUEST["poverty"] == "yes")
518 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
519 DB_set_sickness_by_hash($me,"poverty");
521 else if($_REQUEST["nines"] == "yes")
523 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
524 " is playing solo, this game will be canceled.<br />\n";
525 DB_set_sickness_by_hash($me,"nines");
528 echo " Ok, done with checking, please go to the <a href=\"$host?me=$me\">next step of the setup</a>.</p>";
530 /* move on to the next stage*/
531 DB_set_hand_status_by_hash($me,'poverty');
533 /* check if everyone has reached this stage, send out email */
534 $userids = DB_get_all_userid_by_gameid($gameid);
536 foreach($userids as $user)
538 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
539 if($userstat!='poverty' && $userstat!='play')
542 DB_set_player_by_gameid($gameid,$user);
547 /* reset player = everyone has to do something now */
548 DB_set_player_by_gameid($gameid,NULL);
550 foreach($userids as $user)
552 $To = DB_get_email_by_userid($user);
553 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
556 $message = "Everyone finish the questionary in game ".DB_format_gameid($gameid).", ".
557 "please visit this link now to continue: \n".
558 " ".$host."?me=".$userhash."\n\n" ;
559 mymail($To,$EmailName." finished setup in game ".DB_format_gameid($gameid),$message);
568 /* here we need to check if there is a solo or some other form of sickness.
569 * If so, which one is the most important one
570 * set that one in the Game table
571 * tell people about it.
573 echo "<div class=\"message\">\n";
574 echo "<p> Checking if someone else selected solo, nines, wedding or poverty.</p>";
576 /* check if everyone has reached this stage */
577 $userids = DB_get_all_userid_by_gameid($gameid);
579 foreach($userids as $user)
581 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
582 if($userstat!='poverty' && $userstat!='play')
588 echo "This step can only be handled after everyone finished the last step. ".
589 "Seems like this is not the case, so you need to wait a bit... ".
590 "you will get an email once that is the case, please use the link in ".
591 "that email to continue the game.<br />";
595 echo "Everyone has finished checking their cards, let's see what they said...<br />";
597 /* check what kind of game we are playing, in case there are any solos this already
598 *will have the correct information in it */
599 $gametype = DB_get_gametype_by_gameid($gameid);
600 $startplayer = DB_get_startplayer_by_gameid($gameid);
602 /* check for different sickness and just output a general info */
607 foreach($userids as $user)
609 $name = DB_get_name_by_userid($user);
610 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
611 if($usersick == 'nines')
614 echo "$name has a Vorbehalt. <br />";
617 else if($usersick == 'poverty')
620 echo "$name has a Vorbehalt. <br />";
622 else if($usersick == 'wedding')
625 echo "$name has a Vorbehalt. <br />" ;
627 else if($usersick == 'solo')
630 echo "$name has a Vorbehalt. <br />" ;
634 /* now check which sickness comes first and set the gametype to it */
636 if($gametype == "solo")
643 /* TODO: should we keep statistics of this? */
644 $message = "Hello, \n\n".
645 " the game has been canceled because ".DB_get_name_by_userid($nines).
646 " has five or more nines and nobody is playing solo.\n\n".
647 " To redeal either start a new game or, in case the game was part of a tournament, \n".
648 " go to the last game and use the link at the bottom of the page to redeal.";
650 $userids = DB_get_all_userid_by_gameid($gameid);
651 foreach($userids as $user)
653 $To = DB_get_email_by_userid($user);
654 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
657 /* delete everything from the dB */
660 echo "The game has been canceled because ".DB_get_name_by_userid($nines).
661 " has five or more nines and nobody is playing solo.\n";
666 else if($poverty==1) /* one person has poverty */
668 DB_set_gametype_by_gameid($gameid,"poverty");
669 $gametype = "poverty";
670 $who = DB_get_sickness_by_gameid($gameid);
673 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
674 if($firstsick == "poverty")
675 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
677 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
680 else if($poverty==2) /* two people have poverty */
682 DB_set_gametype_by_gameid($gameid,"dpoverty");
683 $gametype = "dpoverty";
684 $who = DB_get_sickness_by_gameid($gameid);
687 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
688 if($firstsick == "poverty")
690 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
691 if($secondsick == "poverty")
692 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
694 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
697 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
702 DB_set_gametype_by_gameid($gameid,"wedding");
703 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
704 $gametype = "wedding";
709 /* now the gametype is set correctly (shouldn't matter that this is calculated for every user)
710 * output what kind of game we have */
713 foreach($userids as $user)
715 /* userids are sorted by position...
716 * so output whatever the first one has, then whatever the next one has
717 * stop when the sickness is the same as the gametype
720 $name = DB_get_name_by_userid($user);
721 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
724 echo "$name has $usersick. <br />"; /*TODO: perhaps save this in a string and store in Game? */
726 if($usersick=="poverty")
728 if($usersick == "wedding" && $gametype =="wedding")
730 if($usersick == "poverty" && $gametype =="poverty")
732 if($usersick == "poverty" && $gametype =="dpoverty" && $poverty==2)
734 if($usersick == "solo" && $gametype =="solo")
738 /* output Schweinchen in case the rules need it */
739 if( $gametype != "solo")
740 if($GAME["schweinchen"] && $RULES["schweinchen"]=="both" )
741 echo DB_get_name_by_hash($GAME["schweinchen-who"])." has Schweinchen. <br />";
745 /* finished the setup, set re/contra parties if possible, go to next stage unless there is a case of poverty*/
749 /* are we the solo player? set us to re, else set us to contra */
750 $pos = DB_get_pos_by_hash($me);
751 if($pos == $startplayer)
752 DB_set_party_by_hash($me,"re");
754 DB_set_party_by_hash($me,"contra");
755 DB_set_hand_status_by_hash($me,'play');
759 /* set person with the wedding to re, do the rest during the game */
760 $usersick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
761 if($usersick == "wedding")
762 DB_set_party_by_hash($me,"re");
764 DB_set_party_by_hash($me,"contra");
766 echo "Whoever will make the first trick will be on the re team. <br />\n";
767 echo " Ok, the game can start now, please finish <a href=\"$host?me=$me\">the setup</a>.<br />";
768 DB_set_hand_status_by_hash($me,'play');
772 $hand = DB_get_all_hand($me);
774 if(in_array('3',$hand)||in_array('4',$hand))
775 DB_set_party_by_hash($me,"re");
777 DB_set_party_by_hash($me,"contra");
778 DB_set_hand_status_by_hash($me,'play');
782 /* check if poverty resolved (e.g. DB.Game who set to NULL)
783 * yes? =>trump was taken, start game; break;
785 $who = DB_get_sickness_by_gameid($gameid);
787 { /* trump has been taken */
788 DB_set_hand_status_by_hash($me,'play');
792 if($who>9) /*= two people still have trump on the table*/
797 /* check if we are being asked now
798 * no? display wait message, e.g. player X is asked at the moment
800 $usersick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
801 if(myisset("trump") && $_REQUEST["trump"]=="no" && ($who==$mypos || $who==$mypos*10))
803 /* user doesn't want to take trump */
804 /* set next player who needs to be asked */
805 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
806 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
808 if($firstsick=="poverty")
810 if($secondsick=="poverty")
811 DB_set_sickness_by_gameid($gameid,$who+$add*3);
813 DB_set_sickness_by_gameid($gameid,$who+$add*2);
816 DB_set_sickness_by_gameid($gameid,$who+$add);
818 /* email next player */
819 $who = DB_get_sickness_by_gameid($gameid);
820 if($who>9) $who = $who/10;
824 $To = DB_get_email_by_pos_and_gameid($who,$gameid);
825 $userhash = DB_get_hash_from_game_and_pos($gameid,$who);
826 DB_set_player_by_gameid($gameid,$who);
828 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
829 " ".$host."?me=".$userhash."\n\n" ;
830 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
833 /* this user is done */
834 DB_set_hand_status_by_hash($me,'play');
837 else if(myisset("trump") && !myisset("exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
839 /* user wants to take trump */
840 $trump = $_REQUEST["trump"];
842 /* get hand id for user $trump */
843 $userhand = DB_get_handid_by_gameid_and_userid($gameid,$trump);
844 /* copy trump from player A to B */
845 $result = mysql_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
847 /* add hidden button with trump in it to get to the next point */
848 echo "</div><div class=\"poverty\">\n";
849 echo " <input type=\"hidden\" name=\"exchange\" value=\"-1\" />\n";
850 echo " <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
851 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select cards to give back\" />\n";
852 echo "</div><div>\n";
854 else if(myisset("trump","exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
856 $trump = $_REQUEST["trump"];
857 $exchange = $_REQUEST["exchange"];
858 $userhand = DB_get_handid_by_gameid_and_userid($gameid,$trump);
860 /* if exchange is set to a value>0, exchange that card back to user $trump */
863 $result = mysql_query("UPDATE Hand_Card SET hand_id='$userhand'".
864 " WHERE hand_id='$myhand' AND card_id='$exchange'" );
867 /* if number of cards == 12, set status to play for both users */
868 $result = mysql_query("SELECT COUNT(*) FROM Hand_Card WHERE hand_id='$myhand'" );
869 $r = mysql_fetch_array($result,MYSQL_NUM);
872 myerror("error in poverty");
877 if($gametype=="poverty" || $who<9)
879 DB_set_sickness_by_gameid($gameid,-1); /* done with poverty */
881 else /* reduce poverty count by one, that is go to single digits $who */
886 /* whom to ask next */
887 $firstsick = DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
888 $secondsick = DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
890 if($firstsick!="poverty")
891 DB_set_sickness_by_gameid($gameid,$who+$add);
894 if($secondsick!="poverty")
895 DB_set_sickness_by_gameid($gameid,$who+$add*2);
897 DB_set_sickness_by_gameid($gameid,$who+$add*3);
900 /* email next player */
901 $who = DB_get_sickness_by_gameid($gameid);
904 $To = DB_get_email_by_pos_and_gameid($who,$gameid);
905 $userhash = DB_get_hash_from_game_and_pos($gameid,$who);
906 DB_set_player_by_gameid($gameid,$who);
908 $message = "Someone has poverty, it's your turn to decide, ".
909 "if you want to take the trump. Please visit:".
910 " ".$host."?me=".$userhash."\n\n" ;
911 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
915 /* this user is done */
916 DB_set_hand_status_by_hash($me,'play');
917 /* and so is his partner */
918 $hash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
919 DB_set_hand_status_by_hash($hash,'play');
921 /* set party to re, unless we had dpoverty, in that case check if we need to set re/contra*/
923 foreach($userids as $user)
925 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
926 $party = DB_get_party_by_hash($userhash);
932 DB_set_party_by_hash($me,"contra");
933 DB_set_party_by_hash($hash,"contra");
937 foreach($userids as $user)
939 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
940 if($userhash==$hash||$userhash==$me)
941 DB_set_party_by_hash($userhash,"re");
943 DB_set_party_by_hash($userhash,"contra");
952 /* else show all trump, have lowest card pre-selected, have hidden setting for */
953 echo "</div><div class=\"poverty\"> you need to get rid of a few cards</div>\n";
955 set_gametype($gametype); /* this sets the $CARDS variable */
956 $mycards = DB_get_hand($me);
957 $mycards = mysort($mycards,$gametype);
960 echo "<div class=\"mycards\">Your cards are: <br />\n";
961 foreach($mycards as $card)
962 display_link_card($card,$PREF["cardset"],$type);
963 echo " <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
964 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select one card to give back\" />\n";
965 echo "</div><div>\n";
968 else if($who == $mypos || $who == $mypos*10)
970 echo "</div><div class=\"poverty\">\n";
971 foreach($userids as $user)
973 $name = DB_get_name_by_userid($user);
974 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
976 if($usersick=="poverty")
978 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
979 $cards = DB_get_hand($hash);
980 $nrtrump = count_trump($cards);
983 echo "Player $name has $nrtrump trump. Do you want to take them?".
984 "<a href=\"index.php?me=$me&trump=$user\">yes</a> <br />\n";
987 echo "<a href=\"index.php?me=$me&trump=no\">No,way I take those trump...</a> <br />\n";
988 echo "</div><div>\n";
990 echo "Your cards are: <br />\n";
991 $mycards = DB_get_hand($me);
993 echo "<p class=\"mycards\">your cards are: <br />\n";
994 foreach($mycards as $card)
995 display_card($card,$PREF["cardset"]);
1000 $mysick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
1001 if($mysick=="poverty")
1002 echo "The others are asked if they want to take your trump, you have to wait (you'll get an email).";
1004 echo "it's not your turn yet to decide if you want to take the trump or not.";
1007 /* check if no one wanted to take trump, in that case the gamesickness would be set to 5 or 50 */
1008 $who = DB_get_sickness_by_gameid($gameid);
1009 if($who==5 || $who==50)
1011 $message = "Hello, \n\n".
1012 "Game ".DB_format_gameid($gameid)." has been cancled since nobody wanted to take the trump.\n";
1014 $userids = DB_get_all_userid_by_gameid($gameid);
1015 foreach($userids as $user)
1017 $To = DB_get_email_by_userid($user);
1018 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." cancled (poverty not resolved)",$message);
1021 /* delete everything from the dB */
1022 DB_cancel_game($me);
1024 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been cancled.<br /><br /></p>";
1030 /* check if all players are ready to play */
1032 foreach($userids as $user)
1033 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='play')
1036 DB_set_player_by_gameid($gameid,$user);
1041 /* only set this after all poverty, etc. are handled*/
1042 DB_set_game_status_by_gameid($gameid,'play');
1044 /* email startplayer */
1045 $startplayer = DB_get_startplayer_by_gameid($gameid);
1046 $email = DB_get_email_by_pos_and_gameid($startplayer,$gameid);
1047 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
1048 $who = DB_get_userid_by_email($email);
1049 DB_set_player_by_gameid($gameid,$who);
1053 /* email startplayer) */
1054 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
1055 "Use this link to play a card: ".$host."?me=".$hash."\n\n" ;
1056 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
1059 echo " Please, <a href=\"$host?me=$me\">start</a> the game.<br />";
1068 /* both entries here, so that the tricks are visible for both.
1069 * in case of 'play' there is a break later that skips the last part
1072 /* figure out what kind of game we are playing,
1073 * set the global variables $CARDS["trump"],$CARDS["diamonds"],$CARDS["hearts"],
1074 * $CARDS["clubs"],$CARDS["spades"],$CARDS["foxes"]
1078 $gametype = DB_get_gametype_by_gameid($gameid);
1080 if($gametype=="solo")
1082 $gametype = DB_get_solo_by_gameid($gameid);
1083 $GT = $gametype." ".$GT;
1086 $gametype = "normal";
1088 set_gametype($gametype); /* this sets the $CARDS variable */
1090 /* get some infos about the game */
1091 $gamestatus = DB_get_game_status_by_gameid($gameid);
1093 /* has the game started? No, then just wait here...*/
1094 if($gamestatus == 'pre')
1096 echo "<p class=\"message\"> You finished the setup, but not everyone else finished it... ".
1097 "so you need to wait for the others. Just wait for the an email... </p>";
1098 break; /* not sure this works... the idea is that you can
1099 * only play a card after everyone is ready to play */
1102 /* get time from the last action of the game */
1103 $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
1104 $r = mysql_fetch_array($result,MYSQL_NUM);
1105 $gameend = time() - strtotime($r[0]);
1107 /* handel comments in case player didn't play a card, allow comments a week after the end of the game */ if( (!myisset("card") && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
1108 if(myisset("comment"))
1110 $comment = $_REQUEST["comment"];
1111 $playid = DB_get_current_playid($gameid);
1114 DB_insert_comment($comment,$playid,$myid);
1117 /* get everything relevant to display the tricks */
1118 $result = mysql_query("SELECT Hand_Card.card_id as card,".
1119 " Hand.position as position,".
1120 " Play.sequence as sequence, ".
1122 " GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
1123 " SEPARATOR '\n' ), ".
1124 " Play.create_date, ".
1127 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1128 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1129 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1130 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1131 "LEFT JOIN User On User.id=Comment.user_id ".
1132 "WHERE Trick.game_id='".$gameid."' ".
1133 "GROUP BY Trick.id, sequence ".
1134 "ORDER BY Trick.id, sequence ASC");
1136 $lasttrick = DB_get_max_trickid($gameid);
1138 $play = array(); /* needed to calculate winner later */
1140 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1141 $firstcard = ""; /* first card in a trick */
1143 echo "\n<ul class=\"tricks\">\n";
1144 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
1146 while($r = mysql_fetch_array($result,MYSQL_NUM))
1152 $timeplayed = strtotime($r[5]);
1155 $offset = DB_get_user_timezone($user);
1156 $zone = return_timezone($offset);
1157 date_default_timezone_set($zone);
1159 /* check if first schweinchen has been played */
1160 if( $GAME["schweinchen"] && ($r[0] == 19 || $r[0] == 20) )
1161 $GAME["schweinchen"]++;
1163 /* save card to be able to find the winner of the trick later */
1164 $play[$seq] = array("card"=>$r[0],"pos"=>$pos);
1168 /* first card in a trick, output some html */
1169 if($trick!=$lasttrick)
1171 /* start of an old trick? */
1172 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1173 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1174 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1176 else if($trick==$lasttrick)
1178 /* start of a last trick? */
1179 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1180 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1181 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1184 /* remember first card, so that we are able to check, what cards can be played */
1189 echo " <div class=\"card".($pos-1)."\">\n";
1191 /* display comments */
1193 echo " <span class=\"comment\">".$comment."</span>\n";
1196 display_card($r[0],$PREF["cardset"]);
1198 echo " </div>\n"; /* end div card */
1204 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1208 /* whos turn is it? */
1211 $winner = get_winner($play,$gametype); /* returns the position */
1213 $firstcard = ""; /* new trick, no first card */
1218 if($next==5) $next = 1;
1221 /* my turn?, display cards as links, ask for comments*/
1222 if(DB_get_pos_by_hash($me) == $next)
1227 /* do we want to play a card? */
1228 if(myisset("card") && $myturn)
1230 $card = $_REQUEST["card"];
1231 $handid = DB_get_handid_by_hash($me);
1233 /* check if we have card and that we haven't played it yet*/
1234 /* set played in hand_card to true where hand_id and card_id*/
1235 $result = mysql_query("SELECT id FROM Hand_Card WHERE played='false' and ".
1236 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1237 $r = mysql_fetch_array($result,MYSQL_NUM);
1238 $handcardid = $r[0];
1240 if($handcardid) /* everything ok, play card */
1242 /* update Game timestamp */
1243 DB_update_game_timestamp($gameid);
1245 /* check if a call was made, must do this before we set the card status to played */
1246 if(myisset("call120") && $_REQUEST["call120"] == "yes" && can_call(120,$me))
1247 $result = mysql_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
1248 if(myisset("call90") && $_REQUEST["call90"] == "yes" && can_call(90,$me))
1249 $result = mysql_query("UPDATE Hand SET point_call='90' WHERE hash='$me' ");
1250 if(myisset("call60") && $_REQUEST["call60"] == "yes" && can_call(60,$me))
1251 $result = mysql_query("UPDATE Hand SET point_call='60' WHERE hash='$me' ");
1252 if(myisset("call30") && $_REQUEST["call30"] == "yes" && can_call(30,$me))
1253 $result = mysql_query("UPDATE Hand SET point_call='30' WHERE hash='$me' ");
1254 if(myisset("call0") && $_REQUEST["call0"] == "yes" && can_call(0,$me))
1255 $result = mysql_query("UPDATE Hand SET point_call='0' WHERE hash='$me' ");
1257 /* mark card as played */
1258 mysql_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1259 DB_quote_smart($card));
1261 /* get trick id or start new trick */
1262 $a = DB_get_current_trickid($gameid);
1267 $playid = DB_play_card($trickid,$handcardid,$sequence);
1269 /* check for schweinchen */
1270 if($GAME["schweinchen"] && ($card == 19 || $card == 20) )
1272 $GAME["schweinchen"]++; // count how many have been played including this one
1273 if($GAME["schweinchen"]==3 && $RULES["schweinchen"]=="second" )
1274 DB_insert_comment("Schweinchen! ",$playid,$myid);
1275 if($RULES["schweinchen"]=="both" )
1276 DB_insert_comment("Schweinchen! ",$playid,$myid);
1278 echo "schweinchen = ".$GAME["schweinchen"]." ---<br />";
1281 /* if sequence == 4 check who one in case of wedding */
1282 if($sequence == 4 && $GT == "wedding")
1284 /* is wedding resolve */
1285 $resolved = DB_get_sickness_by_gameid($gameid);
1288 /* who has wedding */
1289 $userids = DB_get_all_userid_by_gameid($gameid);
1290 foreach($userids as $user)
1292 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1293 if($usersick == "wedding")
1296 /* who won the trick */
1297 $play = DB_get_cards_by_trick($trickid);
1298 $winner = get_winner($play,$gametype); /* returns the position */
1299 $winnerid = DB_get_userid_by_gameid_and_position($gameid,$winner);
1300 /* is tricknr <=3 */
1301 if($tricknr <=3 && $winnerid!=$whosick)
1303 /* set resolved at tricknr*/
1304 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1306 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1307 DB_set_party_by_hash($whash,"re");
1309 if($tricknr == 3 && $winnerid==$whosick)
1311 /* set resolved at tricknr*/
1312 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1317 /* if sequence == 4, set winner of the trick, count points and set the next player */
1320 $play = DB_get_cards_by_trick($trickid);
1321 $winner = get_winner($play,$gametype); /* returns the position */
1323 /* check if someone caught a fox */
1324 if(DB_get_gametype_by_gameid($gameid)!="solo")
1325 foreach($play as $played)
1327 if ( $played['card']==19 || $played['card']==20 )
1328 if ($played['pos']!= $winner )
1330 /* possible caught a fox, check party */
1331 $uid1 = DB_get_userid_by_gameid_and_position($gameid,$winner);
1332 $uid2 = DB_get_userid_by_gameid_and_position($gameid,$played['pos']);
1334 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1335 $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1337 if($party1 != $party2)
1338 mysql_query("INSERT INTO Score".
1339 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1342 /* check for karlchen (jack of clubs in the last trick)*/
1343 if(DB_get_gametype_by_gameid($gameid)!="solo" && $tricknr == 12)
1344 foreach($play as $played)
1345 if ( $played['card']==11 || $played['card']==12 )
1346 if ($played['pos'] == $winner )
1348 /* possible caught a fox, check party */
1349 $uid1 = DB_get_userid_by_gameid_and_position($gameid,$winner);
1350 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1352 mysql_query("INSERT INTO Score".
1353 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1355 /* check for doppelopf (>40 points)*/
1357 foreach($play as $played)
1359 $points += DB_get_card_value_by_cardid($played['card']);
1363 $uid1 = DB_get_userid_by_gameid_and_position($gameid,$winner);
1364 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1366 mysql_query("INSERT INTO Score".
1367 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
1371 mysql_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1373 echo "ERROR during scoring";
1376 echo "DEBUG: position $winner won the trick <br />";
1378 /* who is the next player? */
1383 $next = DB_get_pos_by_hash($me)+1;
1385 if($next==5) $next=1;
1387 /* check for coment */
1388 if(myisset("comment"))
1390 $comment = $_REQUEST["comment"];
1392 DB_insert_comment($comment,$playid,$myid);
1395 /* display played card */
1396 $pos = DB_get_pos_by_hash($me);
1399 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1400 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1401 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1404 echo "<div class=\"card".($pos-1)."\">";
1406 /* display comments */
1407 display_card($card,$PREF["cardset"]);
1409 echo " <span class=\"comment\"> ".$comment."</span>\n";
1410 echo "</div></div></li>\n";
1412 /*check if we still have cards left, else set status to gameover */
1413 if(sizeof(DB_get_hand($me))==0)
1415 DB_set_hand_status_by_hash($me,'gameover');
1416 $mystatus = 'gameover';
1419 /* if all players are done, set game status to game over,
1420 * get the points of the last trick and send out an email
1423 $userids = DB_get_all_userid_by_gameid($gameid);
1426 foreach($userids as $user)
1427 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1431 DB_set_game_status_by_gameid($gameid,"gameover");
1433 /* email next player, if game is still running */
1434 if(DB_get_game_status_by_gameid($gameid)=='play')
1436 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1437 $email = DB_get_email_by_hash($next_hash);
1438 $who = DB_get_userid_by_email($email);
1439 DB_set_player_by_gameid($gameid,$who);
1441 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1442 "It's your turn now.\n".
1443 "Use this link to play a card: ".$host."?me=".$next_hash."\n\n" ;
1444 mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
1446 else /* send out final email */
1448 /* individual score */
1449 $result = mysql_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1450 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1451 " LEFT JOIN User ON User.id=Hand.user_id".
1452 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1453 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1454 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1455 " WHERE Hand.game_id='$gameid'".
1456 " GROUP BY User.fullname" );
1457 $message = "The game is over. Thanks for playing :)\n";
1458 $message .= "Final score:\n";
1459 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1460 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1462 $result = mysql_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1463 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1464 " LEFT JOIN User ON User.id=Hand.user_id".
1465 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1466 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1467 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1468 " WHERE Hand.game_id='$gameid'".
1469 " GROUP BY Hand.party" );
1470 $message .= "\nTotals:\n";
1473 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1475 $message .= " ".$r[0]." ".$r[1]."\n";
1478 else if($r[0] == "contra")
1481 /* save score in database */
1482 // get calls from re/contra
1484 $call_contra = NULL;
1485 foreach($userids as $user)
1487 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1488 $call = DB_get_call_by_hash($hash);
1489 $party = DB_get_party_by_hash($hash);
1493 $call = (int) $call;
1499 else if( $call < $call_re)
1502 else if($party=="contra")
1504 if($call_contra==NULL)
1505 $call_contra = $call;
1506 else if( $call < $call_re)
1507 $call_contra = $call;
1511 // no call, check 120:120
1512 if($call_re == NULL && $call_contra==NULL)
1514 mysql_query("INSERT INTO Score".
1515 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1517 // no call or one call do scoring
1518 if($call_re != NULL && $call_contra!=NULL)
1520 // two calls: not sure what to do yet
1527 $offset = 120 - $call_re;
1528 else if($call_contra!=NULL)
1529 $offset = -1*(120 - $call_contra)-1;
1537 if($re > 120 + $offset)
1542 foreach(array(120,150,180,210,240) as $p)
1549 mysql_query("INSERT INTO Score".
1550 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
1553 /* re called something and won */
1555 foreach(array(0,30,60,90,120) as $p)
1558 mysql_query("INSERT INTO Score".
1559 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
1565 mysql_query("INSERT INTO Score".
1566 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1569 foreach(array(120,150,180,210,240) as $p)
1576 mysql_query("INSERT INTO Score".
1577 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
1580 /* contra called something and won */
1581 if($call_contra!=NULL)
1582 foreach(array(0,30,60,90,120) as $p)
1584 if($call_contra<$p+1)
1585 mysql_query("INSERT INTO Score".
1586 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
1590 /* send out final email */
1593 foreach($userids as $user)
1594 $all[] = DB_get_email_by_userid($user);
1595 $To = implode(",",$all);
1597 $help = "\n\n (you can use reply all on this email to reach all the players.)\n";
1598 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 1(2)",$message.$help);
1600 foreach($userids as $user)
1602 $To = DB_get_email_by_userid($user);
1603 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1605 $link = "Use this link to have a look at game ".DB_format_gameid($gameid).": ".$host."?me=".$hash."\n\n" ;
1606 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 2(2)",$link);
1612 echo "can't find that card?! <br />\n";
1615 else if(myisset("card") && !$myturn )
1617 echo "please wait until it's your turn! <br />\n";
1620 if($seq!=4 && $trickNR>1)
1621 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1623 /* display points in case game is over */
1624 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1626 echo " <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1627 " <div class=\"trick\" id=\"trick13\">\n";
1628 /* add pic for re/contra
1629 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1631 $result = mysql_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1632 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1633 " LEFT JOIN User ON User.id=Hand.user_id".
1634 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1635 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1636 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1637 " WHERE Hand.game_id='$gameid'".
1638 " GROUP BY User.fullname" );
1639 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1640 echo " <div class=\"card".($r[3]-1)."\">\n".
1641 " <span class=\"score\">".$r[2]."<br /> ".$r[1]."</span>\n".
1644 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1648 echo "</ul>\n"; /* end ul tricks*/
1650 $mycards = DB_get_hand($me);
1651 $mycards = mysort($mycards,$gametype);
1652 echo "<div class=\"mycards\">\n";
1654 if($myturn && !myisset("card") && $mystatus=='play' )
1656 echo "Hello ".$myname.", it's your turn! <br />\n";
1657 echo "Your cards are: <br />\n";
1659 /* do we have to follow suite? */
1661 if(have_suit($mycards,$firstcard))
1664 foreach($mycards as $card)
1666 if($followsuit && !same_type($card,$firstcard))
1667 display_card($card,$PREF["cardset"]);
1669 display_link_card($card,$PREF["cardset"]);
1672 else if($mystatus=='play' )
1674 echo "Your cards are: <br />\n";
1675 foreach($mycards as $card)
1676 display_card($card,$PREF["cardset"]);
1678 else if($mystatus=='gameover')
1680 $oldcards = DB_get_all_hand($me);
1681 $oldcards = mysort($oldcards,$gametype);
1682 echo "Your cards were: <br />\n";
1683 foreach($oldcards as $card)
1684 display_card($card,$PREF["cardset"]);
1686 $userids = DB_get_all_userid_by_gameid($gameid);
1687 foreach($userids as $user)
1689 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1695 $name = DB_get_name_by_userid($user);
1696 $oldcards = DB_get_all_hand($userhash);
1697 $oldcards = mysort($oldcards,$gametype);
1698 echo "$name's cards were: <br />\n";
1699 foreach($oldcards as $card)
1700 display_card($card,$PREF["cardset"]);
1706 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1707 if($mystatus=='play')
1710 /* the following happens only when the gamestatus is 'gameover' */
1711 /* check if game is over, display results */
1712 if(DB_get_game_status_by_gameid($gameid)=='play')
1714 echo "the game is over for you.. other people still need to play though";
1718 $result = mysql_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1719 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1720 " LEFT JOIN User ON User.id=Hand.user_id".
1721 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1722 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1723 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1724 " WHERE Hand.game_id='$gameid'".
1725 " GROUP BY Hand.party" );
1726 echo "<div class=\"total\"> Totals:<br />\n";
1727 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1728 echo " ".$r[0]." ".$r[1]."<br />\n";
1734 myerror("error in testing the status");
1736 /* output left menu */
1737 display_user_menu();
1739 /* output right menu */
1741 /* display rule set for this game */
1742 echo "<div class=\"gameinfo\">\n";
1744 if($gamestatus != 'pre')
1745 echo " Gametype: $GT <br />\n";
1747 echo "Rules: <br />\n";
1748 echo "10ofhearts : ".$RULES["dullen"] ."<br />\n";
1749 echo "schweinchen: ".$RULES["schweinchen"] ."<br />\n";
1750 echo "call: ".$RULES["call"] ."<br />\n";
1753 if($gamestatus == 'play' )
1754 output_form_calls($me);
1756 /* get time from the last action of the game */
1757 $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
1758 $r = mysql_fetch_array($result,MYSQL_NUM);
1759 $gameend = time() - strtotime($r[0]);
1761 if($gamestatus == 'play' || $gameend < 60*60*24*7)
1763 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1767 echo "<input type=\"submit\" value=\"submit\" />\n";
1770 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1774 $session = DB_get_session_by_gameid($gameid);
1775 $result = mysql_query("SELECT id,create_date FROM Game".
1776 " WHERE session=$session".
1777 " ORDER BY create_date DESC".
1781 $r = mysql_fetch_array($result,MYSQL_NUM);
1783 if(!$session || $gameid==$r[0])
1785 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
1786 $names = DB_get_all_names_by_gameid($gameid);
1787 $type = DB_get_gametype_by_gameid($gameid);
1790 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
1792 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
1796 $session = DB_get_session_by_gameid($gameid);
1797 $score = generate_score_table($session);
1799 // if(size_of($score)>30)
1810 /* user status page */
1811 else if( myisset("email","password") || isset($_SESSION["name"]) )
1813 /* test id and password, should really be done in one step */
1814 if(!isset($_SESSION["name"]))
1816 $email = $_REQUEST["email"];
1817 $password = $_REQUEST["password"];
1821 $name = $_SESSION["name"];
1822 $email = DB_get_email_by_name($name);
1823 $password = DB_get_passwd_by_name($name);
1826 if(myisset("forgot"))
1830 $myid = DB_get_userid_by_email($email);
1836 /* check how many entries in recovery table */
1837 $number = DB_get_number_of_passwords_recovery($myid);
1839 /* if less than N recent ones, add a new one and send out email */
1842 echo "Ok, I send you a new password. <br />";
1844 echo "N.B. You tried this already $number times during the last day and it will only work ".
1845 " 5 times during a day.<br />";
1846 echo "The new password will be valid for one day, make sure you reset it to something else.<br />";
1847 echo "Back to the <a href=\"$host\">main page</a>.";
1849 $TIME = (string) time(); /* to avoid collisions */
1850 $hash = md5("Anewpassword".$email.$TIME);
1851 $newpw = substr($hash,1,8);
1853 $message = "Someone (hopefully you) requested a new password. \n".
1854 "You can use this email and the following password: \n".
1856 "to log into the server. The new password is valid for 24h, so make\n".
1857 "sure you reset your password to something new. Your old password will\n".
1858 " also still be valid until you set a new one\n";
1859 mymail($email,$EmailName."recovery ",$message);
1861 DB_set_recovery_password($myid,md5($newpw));
1865 echo "Sorry you already tried 5 times during the last 24h.<br />".
1866 "You need to use one of those passwords or wait to get a new one.<br />";
1867 echo "Back to the <a href=\"$host\">main page</a>.";
1873 echo "You need to give me an email address! <br />".
1874 "Please try <a href=\"$host\">again</a>.";
1876 echo "Couldn't find a player with this email! <br />".
1877 "Please contact Arun, if you think this is a mistake <br />".
1878 "or else try <a href=\"$host\">again</a>.";
1883 /* verify password and email */
1884 if(strlen($password)!=32)
1885 $password = md5($password);
1888 $myid = DB_get_userid_by_email_and_password($email,$password);
1896 if(myisset("setpref"))
1898 $setpref=$_REQUEST["setpref"];
1902 case "englishcards":
1903 $result = mysql_query("SELECT * from User_Prefs".
1904 " WHERE user_id='$myid' AND pref_key='cardset'" );
1905 if( mysql_fetch_array($result,MYSQL_NUM))
1906 $result = mysql_query("UPDATE User_Prefs SET value=".DB_quote_smart($setpref).
1907 " WHERE user_id='$myid' AND pref_key='cardset'" );
1909 $result = mysql_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','cardset',".
1910 DB_quote_smart($setpref).")");
1911 echo "Ok, changed you preferences for the cards.\n";
1915 else if(myisset("passwd"))
1917 if( $_REQUEST["passwd"]=="ask" )
1919 /* reset password form*/
1920 output_password_recovery($email,$password);
1922 else if($_REQUEST["passwd"]=="set")
1924 /* reset password */
1927 /* check if old password matches */
1928 $oldpasswd = md5($_REQUEST["password0"]);
1929 if(!( ($password == $oldpasswd) || DB_check_recovery_passwords($oldpasswd,$email) ))
1931 /* check if new passwords are types the same twice */
1932 if($_REQUEST["password1"] != $_REQUEST["password2"] )
1938 echo "The new passwords don't match. <br />";
1941 echo "The old password is not correct. <br />";
1944 echo "Changed the password.<br />";
1945 mysql_query("UPDATE User SET password='".md5($_REQUEST["password1"]).
1946 "' WHERE id=".DB_quote_smart($myid));
1952 else /* output default user page */
1954 $time = DB_get_user_timestamp($myid);
1955 $unixtime = strtotime($time);
1957 $offset = DB_get_user_timezone($myid);
1958 $zone = return_timezone($offset);
1959 date_default_timezone_set($zone);
1961 $myname = DB_get_name_by_email($email);
1962 $_SESSION["name"] = $myname;
1964 if(isset($_SESSION["name"]))
1965 output_status($_SESSION["name"]);
1967 /* display links to settings */
1968 output_user_settings();
1970 echo "<div class=\"lastlogin\">last login: ".date("r",$unixtime)."</div>";
1972 DB_update_user_timestamp($myid);
1974 display_user_menu();
1976 echo "<div class=\"user\">";
1977 echo "<h4>These are all your games:</h4>\n";
1978 echo "<p>Session: <br />\n";
1979 echo "<span class=\"gamestatuspre\"> p </span> = pre-game phase ";
1980 echo "<span class=\"gamestatusplay\">P </span> = game in progess ";
1981 echo "<span class=\"gamestatusover\">F </span> = game finished <br />";
1985 $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.mod_date,Game.player,Game.status from Hand".
1986 " LEFT JOIN Game ON Game.id=Hand.game_id".
1987 " WHERE user_id='$myid'".
1988 " ORDER BY Game.session,Game.create_date" );
1990 echo "<table>\n <tr><td>\n";
1991 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1993 $game = DB_format_gameid($r[1]);
1994 $gamenr = (int) $game;
1995 if($gamenrold < $gamenr)
1998 echo "</td></tr>\n <tr> <td>$gamenr:</td><td> ";
2000 echo "$gamenr:</td><td> ";
2001 $gamenrold = $gamenr;
2005 echo "\n <span class=\"gamestatuspre\"><a href=\"".$host."?me=".$r[0]."\">p </a></span> ";
2008 else if ($r[4]=='gameover')
2009 echo "\n <span class=\"gamestatusover\"><a href=\"".$host."?me=".$r[0]."\">F </a></span> ";
2012 echo "\n <span class=\"gamestatusplay\"><a href=\"".$host."?me=".$r[0]."\">P </a></span> ";
2014 if($r[4] != 'gameover')
2016 echo "</td><td>\n ";
2020 echo "(it's <strong>your</strong> turn)\n";
2023 $name = DB_get_name_by_userid($r[3]);
2025 if(DB_get_reminder($r[3],$gameid)==0)
2026 if(time()-strtotime($r[2]) > 60*60*24*7)
2028 "<a href=\"$host?remind=1&me=".$r[0]."\">Send a reminder.</a>";
2029 echo "(it's $name's turn)\n";
2032 if(time()-strtotime($r[2]) > 60*60*24*30)
2034 "<a href=\"$host?cancle=1&me=".$r[0]."\">Cancel?</a>".
2035 " (clicking here is final and can't be restored)";
2039 echo "</td></tr>\n</table>\n";
2040 $names = DB_get_all_names();
2041 echo "<h4>Registered players:</h4>\n<p>\n";
2042 echo implode(", ",$names)."\n";
2043 echo "</p>\n</div>";
2048 echo "Sorry email and password don't match. Please <a href=\"$host\">try again</a>. <br />";
2055 /* default login page */
2058 $pre[0]=0;$game[0]=0;$done[0]=0;
2059 $r=mysql_query("SELECT COUNT(id) FROM Game GROUP BY status");
2061 $pre = mysql_fetch_array($r,MYSQL_NUM);
2062 $game = mysql_fetch_array($r,MYSQL_NUM);
2063 $done = mysql_fetch_array($r,MYSQL_NUM);
2066 $r=mysql_query("SELECT AVG(datediff(mod_date,create_date)) FROM Game where status='gameover' ");
2068 $avgage= mysql_fetch_array($r,MYSQL_NUM);
2072 output_home_page($pre[0],$game[0],$done[0],$avgage[0]);