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 "<div class=\"message\"><span class=\"bigger\">You are now logged out!</span><br />\n".
33 "(<a href=\"$INDEX\">This will take you back to the home-page</a>)</div>";
35 else if(myisset("new"))
38 if( isset($_SESSION["name"]) )
40 $names = DB_get_all_names();
41 echo "<div class=\"user\">\n";
42 output_form_for_new_game($names);
48 echo "<div class=\"message\">Please <a href=\"$INDEX\">log in</a>.</div>";
51 /*check if everything is ready to set up a new game */
52 else if( myisset("PlayerA", "PlayerB","PlayerC","PlayerD","dullen","schweinchen","call" ))
54 $PlayerA = $_REQUEST["PlayerA"];
55 $PlayerB = $_REQUEST["PlayerB"];
56 $PlayerC = $_REQUEST["PlayerC"];
57 $PlayerD = $_REQUEST["PlayerD"];
59 $dullen = $_REQUEST["dullen"];
60 $schweinchen = $_REQUEST["schweinchen"];
61 $call = $_REQUEST["call"];
63 $EmailA = DB_get_email_by_name($PlayerA);
64 $EmailB = DB_get_email_by_name($PlayerB);
65 $EmailC = DB_get_email_by_name($PlayerC);
66 $EmailD = DB_get_email_by_name($PlayerD);
68 if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
70 echo "couldn't find one of the names, please start a new game";
76 $useridA = DB_get_userid_by_name($PlayerA);
77 $useridB = DB_get_userid_by_name($PlayerB);
78 $useridC = DB_get_userid_by_name($PlayerC);
79 $useridD = DB_get_userid_by_name($PlayerD);
81 /* create random numbers */
82 $randomNR = create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD);
83 $randomNRstring = join(":",$randomNR);
87 if(myisset("followup") )
89 $followup= $_REQUEST["followup"];
90 $session = DB_get_session_by_gameid($followup);
91 $ruleset = DB_get_ruleset_by_gameid($followup); /* just copy ruleset from old game,
92 this way no manipulation is possible */
94 /* check if there is a game in pre or play mode, in that case do nothing */
95 if( DB_is_session_active($session) > 0 )
97 echo "<p class=\"message\"> There is already a game going on in session $session, you can't start a new one</p>";
102 else if ( DB_is_session_active($session) < 0 )
104 echo "<p class=\"message\"> ERROR: status of session $session couldn't be determined.</p>";
111 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
112 "'$ruleset','$session' ,NULL)");
115 /* get max session */
116 $max = DB_get_max_session();
118 mysql_query("UPDATE Game SET session='".$max."' WHERE id=".DB_quote_smart($followup));
119 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
120 "'$ruleset','$max' ,NULL)");
125 /* get ruleset information or create new one */
126 $ruleset = DB_get_ruleset($dullen,$schweinchen,$call);
129 myerror("Error defining ruleset: $ruleset");
134 /* get max session */
135 $max = DB_get_max_session();
138 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre', ".
139 "'$ruleset','$max' ,NULL)");
141 $game_id = mysql_insert_id();
144 $TIME = (string) time(); /* to avoid collisions */
145 $hashA = md5("AGameOfDoko".$game_id.$PlayerA.$EmailA.$TIME);
146 $hashB = md5("AGameOfDoko".$game_id.$PlayerB.$EmailB.$TIME);
147 $hashC = md5("AGameOfDoko".$game_id.$PlayerC.$EmailC.$TIME);
148 $hashD = md5("AGameOfDoko".$game_id.$PlayerD.$EmailD.$TIME);
151 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridA).
152 ", ".DB_quote_smart($hashA).", 'start','1',NULL,NULL,NULL,NULL)");
153 $hand_idA = mysql_insert_id();
154 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridB).
155 ", ".DB_quote_smart($hashB).", 'start','2',NULL,NULL,NULL,NULL)");
156 $hand_idB = mysql_insert_id();
157 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridC).
158 ", ".DB_quote_smart($hashC).", 'start','3',NULL,NULL,NULL,NULL)");
159 $hand_idC = mysql_insert_id();
160 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridD).
161 ", ".DB_quote_smart($hashD).", 'start','4',NULL,NULL,NULL,NULL)");
162 $hand_idD = mysql_insert_id();
166 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idA', '".$randomNR[$i]."', 'false')");
167 for($i=12;$i<24;$i++)
168 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idB', '".$randomNR[$i]."', 'false')");
169 for($i=24;$i<36;$i++)
170 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idC', '".$randomNR[$i]."', 'false')");
171 for($i=36;$i<48;$i++)
172 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idD', '".$randomNR[$i]."', 'false')");
174 /* send out email, TODO: check for error with email */
176 "you are invited to play a game of DoKo (that is to debug the program ;).\n".
177 "Place comments and bug reports here:\n".
178 "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
179 "The whole round would consist of the following players:\n".
184 "If you want to join this game, please follow this link:\n\n".
185 "".$HOST.$INDEX."?me=";
187 mymail($EmailA,"You are invited to a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
188 mymail($EmailB,"You are invited to a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
189 mymail($EmailC,"You are invited to a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
190 mymail($EmailD,"You are invited to a game of DoKo","Hello $PlayerD,\n".$message.$hashD);
193 echo "<div class=\"message\">You started a new game. The emails have been sent out!</div>\n";
194 } /* end set up a new game */
195 /* cancel a game, if nothing has happend in the last N minutes */
196 else if(myisset("cancel","me"))
200 $me = $_REQUEST["me"];
202 /* test for valid ID */
203 $myid = DB_get_userid_by_hash($me);
206 echo "Can't find you in the database, please check the url.<br />\n";
207 echo "perhaps the game has been canceled, check by login in <a href=\"$INDEX\">here</a>.";
213 DB_update_user_timestamp($myid);
215 /* get some information from the DB */
216 $gameid = DB_get_gameid_by_hash($me);
217 $myname = DB_get_name_by_hash($me);
219 /* check if game really is old enough */
220 $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
221 $r = mysql_fetch_array($result,MYSQL_NUM);
222 if(time()-strtotime($r[0]) > 60*60*24*30) /* = 1 month */
224 $message = "Hello, \n\n".
225 "Game ".DB_format_gameid($gameid).
226 " has been canceled since nothing happend for a while and $myname requested it.\n";
228 $userids = DB_get_all_userid_by_gameid($gameid);
229 foreach($userids as $user)
231 $To = DB_get_email_by_userid($user);
232 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled (timed out)",$message);
235 /* delete everything from the dB */
238 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid).
239 " has been canceled.<br /><br /></p>";
242 echo "<p>You need to wait longer before you can cancel a game...</p>\n";
244 /* send out a reminder */
245 else if(myisset("remind","me"))
249 $me = $_REQUEST["me"];
251 /* test for valid ID */
252 $myid = DB_get_userid_by_hash($me);
255 echo "Can't find you in the database, please check the url.<br />\n";
256 echo "perhaps the game has been canceled, check by login in <a href=\"$INDEX\">here</a>.";
262 DB_update_user_timestamp($myid);
264 /* get some information from the DB */
265 $gameid = DB_get_gameid_by_hash($me);
266 $myname = DB_get_name_by_hash($me);
268 /* check if game really is old enough */
269 $result = mysql_query("SELECT mod_date,player,status from Game WHERE id='$gameid' " );
270 $r = mysql_fetch_array($result,MYSQL_NUM);
271 if( (time()-strtotime($r[0]) > 60*60*24*7) && ($r[2]!='gameover') ) /* = 1 week */
273 $name = DB_get_name_by_userid($r[1]);
274 $To = DB_get_email_by_userid($r[1]);
275 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$r[1]);
277 $message = "Hello $name, \n\n".
278 "It's your turn in game ".DB_format_gameid($gameid)." \n".
279 "Actually everyone else is waiting for you for more than a week now ;)\n\n".
280 "Please visit this link now to continue: \n".
281 " ".$HOST.$INDEX."?me=".$userhash."\n\n" ;
283 if(DB_get_reminder($r[1],$gameid)>0)
285 echo "<p>An email has already been sent out.</p>\n";
289 DB_set_reminder($r[1],$gameid);
290 mymail($To,$EmailName."Reminder: game ".DB_format_gameid($gameid)." it's your turn",$message);
292 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid).
293 ": an email has been sent out.<br /><br /></p>";
297 echo "<p>You need to wait longer before you can send out a reminder...</p>\n";
299 /* handle request from one specific player for one game,
300 * (the hash is set on a per game base) */
301 else if(myisset("me"))
303 $me = $_REQUEST["me"];
305 /* test for valid ID */
306 $myid = DB_get_userid_by_hash($me);
309 echo "Can't find you in the database, please check the url.<br />\n";
310 echo "perhaps the game has been canceled, check by login in <a href=\"$INDEX\">here</a>.";
316 if(isset($_SESSION["name"]))
317 output_status($_SESSION["name"]);
319 /* the user had done something, update the timestamp */
320 DB_update_user_timestamp($myid);
322 /* get some information from the DB */
323 $gameid = DB_get_gameid_by_hash($me);
324 $myname = DB_get_name_by_hash($me);
325 $mystatus = DB_get_status_by_hash($me);
326 $mypos = DB_get_pos_by_hash($me);
327 $myhand = DB_get_handid_by_hash($me);
328 $session = DB_get_session_by_gameid($gameid);
330 /* get prefs and save them */
334 /* get rule set for this game */
335 $result = mysql_query("SELECT * FROM Rulesets".
336 " LEFT JOIN Game ON Game.ruleset=Rulesets.id ".
337 " WHERE Game.id='$gameid'" );
338 $r = mysql_fetch_array($result,MYSQL_NUM);
340 $RULES["dullen"] = $r[2];
341 $RULES["schweinchen"] = $r[3];
342 $RULES["call"] = $r[4];
344 /* get some infos about the game */
345 $gametype = DB_get_gametype_by_gameid($gameid);
346 $gamestatus = DB_get_game_status_by_gameid($gameid);
348 if($gametype=="solo")
350 $gametype = DB_get_solo_by_gameid($gameid);
351 $GT = $gametype." ".$GT;
354 /* does anyone have both foxes */
355 $GAME["schweinchen"]=0;
358 $hash = DB_get_hash_from_game_and_pos($gameid,$i);
359 $cards = DB_get_all_hand($hash);
360 if( in_array("19",$cards) && in_array("20",$cards) )
362 $GAME["schweinchen"]=1;
363 $GAME["schweinchen-who"]=$hash;
367 /* put everyting in a form */
368 echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
372 /* output extra division in case this game is part of a session */
375 echo "<div class=\"session\">\n".
376 "This game is part of session $session: \n";
377 $hashes = DB_get_hashes_by_session($session,$myid);
379 foreach($hashes as $hash)
384 echo "<a href=\"".$INDEX."?me=".$hash."\">$i</a> \n";
390 /* display the table and the names */
393 /* mystatus gets the player through the different stages of a game.
395 * init: check values from start,
397 * check: check for return values from init
398 * poverty: handle poverty, wait here until all player have reached this state
399 * display sickness and move on to game
400 * play: game in progress
401 * gameover: are we revisiting a game
408 output_check_want_to_play($me);
413 if($_REQUEST["in"] == "no")
415 /* cancel the game */
416 $message = "Hello, \n\n".
417 "the game has been canceled due to the request of one of the players.\n";
419 $userids = DB_get_all_userid_by_gameid($gameid);
420 foreach($userids as $user)
422 $To = DB_get_email_by_userid($user);
423 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
426 /* delete everything from the dB */
432 /* user wants to join the game */
434 /* move on to the next stage,
435 * no break statement to immediately go to the next stage
438 DB_set_hand_status_by_hash($me,'init');
440 /* check if everyone has reached this stage, send out email */
441 $userids = DB_get_all_userid_by_gameid($gameid);
443 foreach($userids as $user)
445 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
446 if($userstat!='init')
448 /* whos turn is it? */
449 DB_set_player_by_gameid($gameid,$user);
455 /* all done, send out email unless this player is the startplayer */
456 $startplayer = DB_get_startplayer_by_gameid($gameid);
457 if($mypos == $startplayer)
459 /* do nothing, go to next stage */
463 /* email startplayer */
465 $email = DB_get_email_by_pos_and_gameid($startplayer,$gameid);
466 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
467 $who = DB_get_userid_by_email($email);
468 DB_set_player_by_gameid($gameid,$who);
470 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
471 "Use this link to play a card: ".$HOST.$INDEX."?me=".$hash."\n\n" ;
472 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
480 $mycards = DB_get_hand($me);
483 output_check_for_sickness($me,$mycards);
485 echo "<p class=\"mycards\">Your cards are: <br />\n";
486 foreach($mycards as $card)
487 display_card($card,$PREF["cardset"]);
490 /* move on to the next stage*/
491 DB_set_hand_status_by_hash($me,'check');
495 /* ok, user is in the game, saw his cards and selected his vorbehalt
496 * so first we check what he selected
498 if(!myisset("solo","wedding","poverty","nines") )
500 /* all these variables have a pre-selected default,
501 * so we should never get here,
502 * unless a user tries to cheat ;)
503 * can also happen if user reloads the page!
505 echo "<p class=\"message\"> You need to answer the <a href=\"$INDEX?me=$me&in=yes\">questions</a>.</p>";
506 DB_set_hand_status_by_hash($me,'init');
510 /* check if someone selected more than one vorbehalt */
512 if($_REQUEST["solo"]!="No") $Nvorbehalt++;
513 if($_REQUEST["wedding"] == "yes") $Nvorbehalt++;
514 if($_REQUEST["poverty"] == "yes") $Nvorbehalt++;
515 if($_REQUEST["nines"] == "yes") $Nvorbehalt++;
519 echo "<p class=\"message\"> You selected more than one vorbehalt, please go back ".
520 "and answer the <a href=\"$INDEX?me=$me&in=yes\">question</a> again.</p>";
521 DB_set_hand_status_by_hash($me,'init');
525 echo "<p class=\"message\">Processing what you selected in the last step...";
527 /* check if this sickness needs to be handled first */
528 $gametype = DB_get_gametype_by_gameid($gameid);
529 $startplayer = DB_get_startplayer_by_gameid($gameid);
531 if( $_REQUEST["solo"]!="No")
533 /* user wants to play a solo */
535 /* store the info in the user's hand info */
536 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
537 DB_set_sickness_by_hash($me,"solo");
539 echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
541 if($gametype == "solo" && $startplayer<$mypos)
542 {}/* do nothing, since someone else already is playing solo */
545 /* this solo comes first
546 * store info in game table
548 DB_set_gametype_by_gameid($gameid,"solo");
549 DB_set_startplayer_by_gameid($gameid,$mypos);
550 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
553 else if($_REQUEST["wedding"] == "yes")
555 /* TODO: add silent solo somewhere*/
556 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
557 DB_set_sickness_by_hash($me,"wedding");
559 else if($_REQUEST["poverty"] == "yes")
561 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
562 DB_set_sickness_by_hash($me,"poverty");
564 else if($_REQUEST["nines"] == "yes")
566 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
567 " is playing solo, this game will be canceled.<br />\n";
568 DB_set_sickness_by_hash($me,"nines");
571 echo " Ok, done with checking, please go to the <a href=\"$INDEX?me=$me\">next step of the setup</a>.</p>";
573 /* move on to the next stage*/
574 DB_set_hand_status_by_hash($me,'poverty');
576 /* check if everyone has reached this stage, send out email */
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')
585 DB_set_player_by_gameid($gameid,$user);
590 /* reset player = everyone has to do something now */
591 DB_set_player_by_gameid($gameid,NULL);
593 foreach($userids as $user)
595 $To = DB_get_email_by_userid($user);
596 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
599 $message = "Everyone finish the questionary in game ".DB_format_gameid($gameid).", ".
600 "please visit this link now to continue: \n".
601 " ".$HOST.$INDEX."?me=".$userhash."\n\n" ;
602 mymail($To,$EmailName." finished setup in game ".DB_format_gameid($gameid),$message);
611 /* here we need to check if there is a solo or some other form of sickness.
612 * If so, which one is the most important one
613 * set that one in the Game table
614 * tell people about it.
616 echo "<div class=\"message\">\n";
617 echo "<p> Checking if someone else selected solo, nines, wedding or poverty.</p>";
619 /* check if everyone has reached this stage */
620 $userids = DB_get_all_userid_by_gameid($gameid);
622 foreach($userids as $user)
624 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
625 if($userstat!='poverty' && $userstat!='play')
631 echo "This step can only be handled after everyone finished the last step. ".
632 "Seems like this is not the case, so you need to wait a bit... ".
633 "you will get an email once that is the case, please use the link in ".
634 "that email to continue the game.<br />";
638 echo "Everyone has finished checking their cards, let's see what they said...<br />";
640 /* check what kind of game we are playing, in case there are any solos this already
641 *will have the correct information in it */
642 $gametype = DB_get_gametype_by_gameid($gameid);
643 $startplayer = DB_get_startplayer_by_gameid($gameid);
645 /* check for different sickness and just output a general info */
650 foreach($userids as $user)
652 $name = DB_get_name_by_userid($user);
653 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
654 if($usersick == 'nines')
657 echo "$name has a Vorbehalt. <br />";
660 else if($usersick == 'poverty')
663 echo "$name has a Vorbehalt. <br />";
665 else if($usersick == 'wedding')
668 echo "$name has a Vorbehalt. <br />" ;
670 else if($usersick == 'solo')
673 echo "$name has a Vorbehalt. <br />" ;
677 /* now check which sickness comes first and set the gametype to it */
679 if($gametype == "solo")
686 /* TODO: should we keep statistics of this? */
687 $message = "Hello, \n\n".
688 " the game has been canceled because ".DB_get_name_by_userid($nines).
689 " has five or more nines and nobody is playing solo.\n\n".
690 " To redeal either start a new game or, in case the game was part of a tournament, \n".
691 " go to the last game and use the link at the bottom of the page to redeal.";
693 $userids = DB_get_all_userid_by_gameid($gameid);
694 foreach($userids as $user)
696 $To = DB_get_email_by_userid($user);
697 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
700 /* delete everything from the dB */
703 echo "The game has been canceled because ".DB_get_name_by_userid($nines).
704 " has five or more nines and nobody is playing solo.\n";
709 else if($poverty==1) /* one person has poverty */
711 DB_set_gametype_by_gameid($gameid,"poverty");
712 $gametype = "poverty";
713 $who = DB_get_sickness_by_gameid($gameid);
716 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
717 if($firstsick == "poverty")
718 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
720 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
723 else if($poverty==2) /* two people have poverty */
725 DB_set_gametype_by_gameid($gameid,"dpoverty");
726 $gametype = "dpoverty";
727 $who = DB_get_sickness_by_gameid($gameid);
730 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
731 if($firstsick == "poverty")
733 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
734 if($secondsick == "poverty")
735 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
737 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
740 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
745 DB_set_gametype_by_gameid($gameid,"wedding");
746 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
747 $gametype = "wedding";
752 /* now the gametype is set correctly (shouldn't matter that this is calculated for every user)
753 * output what kind of game we have */
756 foreach($userids as $user)
758 /* userids are sorted by position...
759 * so output whatever the first one has, then whatever the next one has
760 * stop when the sickness is the same as the gametype
763 $name = DB_get_name_by_userid($user);
764 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
767 echo "$name has $usersick. <br />"; /*TODO: perhaps save this in a string and store in Game? */
769 if($usersick=="poverty")
771 if($usersick == "wedding" && $gametype =="wedding")
773 if($usersick == "poverty" && $gametype =="poverty")
775 if($usersick == "poverty" && $gametype =="dpoverty" && $poverty==2)
777 if($usersick == "solo" && $gametype =="solo")
781 /* output Schweinchen in case the rules need it */
782 if( $gametype != "solo")
783 if($GAME["schweinchen"] && $RULES["schweinchen"]=="both" )
784 echo DB_get_name_by_hash($GAME["schweinchen-who"])." has Schweinchen. <br />";
788 /* finished the setup, set re/contra parties if possible, go to next stage unless there is a case of poverty*/
792 /* are we the solo player? set us to re, else set us to contra */
793 $pos = DB_get_pos_by_hash($me);
794 if($pos == $startplayer)
795 DB_set_party_by_hash($me,"re");
797 DB_set_party_by_hash($me,"contra");
798 DB_set_hand_status_by_hash($me,'play');
802 /* set person with the wedding to re, do the rest during the game */
803 $usersick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
804 if($usersick == "wedding")
805 DB_set_party_by_hash($me,"re");
807 DB_set_party_by_hash($me,"contra");
809 echo "Whoever will make the first trick will be on the re team. <br />\n";
810 echo " Ok, the game can start now, please finish <a href=\"$INDEX?me=$me\">the setup</a>.<br />";
811 DB_set_hand_status_by_hash($me,'play');
815 $hand = DB_get_all_hand($me);
817 if(in_array('3',$hand)||in_array('4',$hand))
818 DB_set_party_by_hash($me,"re");
820 DB_set_party_by_hash($me,"contra");
821 DB_set_hand_status_by_hash($me,'play');
825 /* check if poverty resolved (e.g. DB.Game who set to NULL)
826 * yes? =>trump was taken, start game; break;
828 $who = DB_get_sickness_by_gameid($gameid);
830 { /* trump has been taken */
831 DB_set_hand_status_by_hash($me,'play');
835 if($who>9) /*= two people still have trump on the table*/
840 /* check if we are being asked now
841 * no? display wait message, e.g. player X is asked at the moment
843 $usersick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
844 if(myisset("trump") && $_REQUEST["trump"]=="no" && ($who==$mypos || $who==$mypos*10))
846 /* user doesn't want to take trump */
847 /* set next player who needs to be asked */
848 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
849 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
851 if($firstsick=="poverty")
853 if($secondsick=="poverty")
854 DB_set_sickness_by_gameid($gameid,$who+$add*3);
856 DB_set_sickness_by_gameid($gameid,$who+$add*2);
859 DB_set_sickness_by_gameid($gameid,$who+$add);
861 /* email next player */
862 $who = DB_get_sickness_by_gameid($gameid);
863 if($who>9) $who = $who/10;
867 $To = DB_get_email_by_pos_and_gameid($who,$gameid);
868 $userhash = DB_get_hash_from_game_and_pos($gameid,$who);
869 $userid = DB_get_userid_by_email($To);
870 DB_set_player_by_gameid($gameid,$userid);
872 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
873 " ".$HOST.$INDEX."?me=".$userhash."\n\n" ;
874 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
877 /* this user is done */
878 DB_set_hand_status_by_hash($me,'play');
881 else if(myisset("trump") && !myisset("exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
883 /* user wants to take trump */
884 $trump = $_REQUEST["trump"];
886 /* get hand id for user $trump */
887 $userhand = DB_get_handid_by_gameid_and_userid($gameid,$trump);
888 /* copy trump from player A to B */
889 $result = mysql_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
891 /* add hidden button with trump in it to get to the next point */
892 echo "</div><div class=\"poverty\">\n";
893 echo " <input type=\"hidden\" name=\"exchange\" value=\"-1\" />\n";
894 echo " <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
895 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select cards to give back\" />\n";
896 echo "</div><div>\n";
898 else if(myisset("trump","exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
900 $trump = $_REQUEST["trump"];
901 $exchange = $_REQUEST["exchange"];
902 $userhand = DB_get_handid_by_gameid_and_userid($gameid,$trump);
904 /* if exchange is set to a value>0, exchange that card back to user $trump */
907 $result = mysql_query("UPDATE Hand_Card SET hand_id='$userhand'".
908 " WHERE hand_id='$myhand' AND card_id='$exchange'" );
911 /* if number of cards == 12, set status to play for both users */
912 $result = mysql_query("SELECT COUNT(*) FROM Hand_Card WHERE hand_id='$myhand'" );
913 $r = mysql_fetch_array($result,MYSQL_NUM);
916 myerror("error in poverty");
921 if($gametype=="poverty" || $who<9)
923 DB_set_sickness_by_gameid($gameid,-1); /* done with poverty */
925 else /* reduce poverty count by one, that is go to single digits $who */
930 /* whom to ask next */
931 $firstsick = DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
932 $secondsick = DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
934 if($firstsick!="poverty")
935 DB_set_sickness_by_gameid($gameid,$who+$add);
938 if($secondsick!="poverty")
939 DB_set_sickness_by_gameid($gameid,$who+$add*2);
941 DB_set_sickness_by_gameid($gameid,$who+$add*3);
944 /* email next player */
945 $who = DB_get_sickness_by_gameid($gameid);
948 $To = DB_get_email_by_pos_and_gameid($who,$gameid);
949 $userhash = DB_get_hash_from_game_and_pos($gameid,$who);
950 $userid = DB_get_userid_by_email($To);
951 DB_set_player_by_gameid($gameid,$userid);
953 $message = "Someone has poverty, it's your turn to decide, ".
954 "if you want to take the trump. Please visit:".
955 " ".$HOST.$INDEX."?me=".$userhash."\n\n" ;
956 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
960 /* this user is done */
961 DB_set_hand_status_by_hash($me,'play');
962 /* and so is his partner */
963 $hash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
964 DB_set_hand_status_by_hash($hash,'play');
966 /* set party to re, unless we had dpoverty, in that case check if we need to set re/contra*/
968 foreach($userids as $user)
970 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
971 $party = DB_get_party_by_hash($userhash);
977 DB_set_party_by_hash($me,"contra");
978 DB_set_party_by_hash($hash,"contra");
982 foreach($userids as $user)
984 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
985 if($userhash==$hash||$userhash==$me)
986 DB_set_party_by_hash($userhash,"re");
988 DB_set_party_by_hash($userhash,"contra");
997 /* else show all trump, have lowest card pre-selected, have hidden setting for */
998 echo "</div><div class=\"poverty\"> you need to get rid of a few cards</div>\n";
1000 set_gametype($gametype); /* this sets the $CARDS variable */
1001 $mycards = DB_get_hand($me);
1002 $mycards = mysort($mycards,$gametype);
1005 echo "<div class=\"mycards\">Your cards are: <br />\n";
1006 foreach($mycards as $card)
1007 display_link_card($card,$PREF["cardset"],$type);
1008 echo " <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
1009 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select one card to give back\" />\n";
1010 echo "</div><div>\n";
1013 else if($who == $mypos || $who == $mypos*10)
1015 echo "</div><div class=\"poverty\">\n";
1016 foreach($userids as $user)
1018 $name = DB_get_name_by_userid($user);
1019 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1021 if($usersick=="poverty")
1023 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1024 $cards = DB_get_hand($hash);
1025 $nrtrump = count_trump($cards);
1028 echo "Player $name has $nrtrump trump. Do you want to take them?".
1029 "<a href=\"index.php?me=$me&trump=$user\">yes</a> <br />\n";
1032 echo "<a href=\"index.php?me=$me&trump=no\">No,way I take those trump...</a> <br />\n";
1033 echo "</div><div>\n";
1035 echo "Your cards are: <br />\n";
1036 $mycards = DB_get_hand($me);
1038 echo "<p class=\"mycards\">Your cards are: <br />\n";
1039 foreach($mycards as $card)
1040 display_card($card,$PREF["cardset"]);
1045 $mysick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
1046 if($mysick=="poverty")
1047 echo "The others are asked if they want to take your trump, you have to wait (you'll get an email).";
1049 echo "it's not your turn yet to decide if you want to take the trump or not.";
1052 /* check if no one wanted to take trump, in that case the gamesickness would be set to 5 or 50 */
1053 $who = DB_get_sickness_by_gameid($gameid);
1054 if($who==5 || $who==50)
1056 $message = "Hello, \n\n".
1057 "Game ".DB_format_gameid($gameid)." has been canceled since nobody wanted to take the trump.\n";
1059 $userids = DB_get_all_userid_by_gameid($gameid);
1060 foreach($userids as $user)
1062 $To = DB_get_email_by_userid($user);
1063 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled (poverty not resolved)",$message);
1066 /* delete everything from the dB */
1067 DB_cancel_game($me);
1069 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been canceled.<br /><br /></p>";
1075 /* check if all players are ready to play */
1077 foreach($userids as $user)
1078 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='play')
1081 DB_set_player_by_gameid($gameid,$user);
1086 /* only set this after all poverty, etc. are handled*/
1087 DB_set_game_status_by_gameid($gameid,'play');
1089 /* email startplayer */
1090 $startplayer = DB_get_startplayer_by_gameid($gameid);
1091 $email = DB_get_email_by_pos_and_gameid($startplayer,$gameid);
1092 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
1093 $who = DB_get_userid_by_email($email);
1094 DB_set_player_by_gameid($gameid,$who);
1096 if($hash!=$me && DB_get_email_pref_by_hash($hash)!="emailaddict")
1098 /* email startplayer) */
1099 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
1100 "Use this link to play a card: ".$HOST.$INDEX."?me=".$hash."\n\n" ;
1101 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
1104 echo " Please, <a href=\"$INDEX?me=$me\">start</a> the game.<br />";
1113 /* both entries here, so that the tricks are visible for both.
1114 * in case of 'play' there is a break later that skips the last part
1117 /* figure out what kind of game we are playing,
1118 * set the global variables $CARDS["trump"],$CARDS["diamonds"],$CARDS["hearts"],
1119 * $CARDS["clubs"],$CARDS["spades"],$CARDS["foxes"]
1123 $gametype = DB_get_gametype_by_gameid($gameid);
1125 if($gametype=="solo")
1127 $gametype = DB_get_solo_by_gameid($gameid);
1128 $GT = $gametype." ".$GT;
1131 $gametype = "normal";
1133 set_gametype($gametype); /* this sets the $CARDS variable */
1135 /* get some infos about the game */
1136 $gamestatus = DB_get_game_status_by_gameid($gameid);
1138 /* has the game started? No, then just wait here...*/
1139 if($gamestatus == 'pre')
1141 echo "<p class=\"message\"> You finished the setup, but not everyone else finished it... ".
1142 "so you need to wait for the others. Just wait for the an email... </p>";
1143 break; /* not sure this works... the idea is that you can
1144 * only play a card after everyone is ready to play */
1147 /* get time from the last action of the game */
1148 $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
1149 $r = mysql_fetch_array($result,MYSQL_NUM);
1150 $gameend = time() - strtotime($r[0]);
1152 /* handel comments in case player didn't play a card, allow comments a week after the end of the game */
1153 if( (!myisset("card") && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
1154 if(myisset("comment"))
1156 $comment = $_REQUEST["comment"];
1157 $playid = DB_get_current_playid($gameid);
1160 DB_insert_comment($comment,$playid,$myid);
1163 /* get everything relevant to display the tricks */
1164 $result = mysql_query("SELECT Hand_Card.card_id as card,".
1165 " Hand.position as position,".
1166 " Play.sequence as sequence, ".
1168 " GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
1169 " SEPARATOR '\n' ), ".
1170 " Play.create_date, ".
1173 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1174 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1175 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1176 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1177 "LEFT JOIN User On User.id=Comment.user_id ".
1178 "WHERE Trick.game_id='".$gameid."' ".
1179 "GROUP BY Trick.id, sequence ".
1180 "ORDER BY Trick.id, sequence ASC");
1182 $lasttrick = DB_get_max_trickid($gameid);
1184 $play = array(); /* needed to calculate winner later */
1186 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1187 $firstcard = ""; /* first card in a trick */
1189 echo "\n<ul class=\"tricks\">\n";
1190 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
1192 /* output vorbehalte */
1193 $mygametype = DB_get_gametype_by_gameid($gameid);
1194 if($mygametype != "normal") /* only show when needed */
1196 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
1197 " <div class=\"trick\" id=\"trick0\">\n";
1199 for($mypos=1;$mypos<5;$mypos++)
1201 $usersick = DB_get_sickness_by_pos_and_gameid($mypos,$gameid);
1204 echo " <div class=\"vorbehalt".($mypos-1)."\"> Vorbehalt <br />";
1206 echo " $usersick <br />";
1209 if($mygametype == $usersick)
1213 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1217 while($r = mysql_fetch_array($result,MYSQL_NUM))
1225 /* check if first schweinchen has been played */
1226 if( $GAME["schweinchen"] && ($r[0] == 19 || $r[0] == 20) )
1227 $GAME["schweinchen"]++;
1229 /* save card to be able to find the winner of the trick later */
1230 $play[$seq] = array("card"=>$r[0],"pos"=>$pos);
1234 /* first card in a trick, output some html */
1235 if($trick!=$lasttrick)
1237 /* start of an old trick? */
1238 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1239 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1240 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1242 else if($trick==$lasttrick)
1244 /* start of a last trick? */
1245 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1246 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1247 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1250 /* remember first card, so that we are able to check, what cards can be played */
1255 echo " <div class=\"card".($pos-1)."\">\n";
1257 /* display comments */
1259 echo " <span class=\"comment\">".$comment."</span>\n";
1262 display_card($r[0],$PREF["cardset"]);
1264 echo " </div>\n"; /* end div card */
1270 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1274 /* whos turn is it? */
1277 $winner = get_winner($play,$gametype); /* returns the position */
1279 $firstcard = ""; /* new trick, no first card */
1284 if($next==5) $next = 1;
1287 /* my turn?, display cards as links, ask for comments*/
1288 if(DB_get_pos_by_hash($me) == $next)
1293 /* do we want to play a card? */
1294 if(myisset("card") && $myturn)
1296 $card = $_REQUEST["card"];
1297 $handid = DB_get_handid_by_hash($me);
1299 /* check if we have card and that we haven't played it yet*/
1300 /* set played in hand_card to true where hand_id and card_id*/
1301 $result = mysql_query("SELECT id FROM Hand_Card WHERE played='false' and ".
1302 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1303 $r = mysql_fetch_array($result,MYSQL_NUM);
1304 $handcardid = $r[0];
1306 if($handcardid) /* everything ok, play card */
1308 /* update Game timestamp */
1309 DB_update_game_timestamp($gameid);
1311 /* check if a call was made, must do this before we set the card status to played */
1312 if(myisset("call120") && $_REQUEST["call120"] == "yes" && can_call(120,$me))
1313 $result = mysql_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
1314 if(myisset("call90") && $_REQUEST["call90"] == "yes" && can_call(90,$me))
1315 $result = mysql_query("UPDATE Hand SET point_call='90' WHERE hash='$me' ");
1316 if(myisset("call60") && $_REQUEST["call60"] == "yes" && can_call(60,$me))
1317 $result = mysql_query("UPDATE Hand SET point_call='60' WHERE hash='$me' ");
1318 if(myisset("call30") && $_REQUEST["call30"] == "yes" && can_call(30,$me))
1319 $result = mysql_query("UPDATE Hand SET point_call='30' WHERE hash='$me' ");
1320 if(myisset("call0") && $_REQUEST["call0"] == "yes" && can_call(0,$me))
1321 $result = mysql_query("UPDATE Hand SET point_call='0' WHERE hash='$me' ");
1323 /* mark card as played */
1324 mysql_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1325 DB_quote_smart($card));
1327 /* get trick id or start new trick */
1328 $a = DB_get_current_trickid($gameid);
1333 $playid = DB_play_card($trickid,$handcardid,$sequence);
1335 /* check for schweinchen */
1336 if($GAME["schweinchen"] && ($card == 19 || $card == 20) )
1338 $GAME["schweinchen"]++; // count how many have been played including this one
1339 if($GAME["schweinchen"]==3 && $RULES["schweinchen"]=="second" )
1340 DB_insert_comment("Schweinchen! ",$playid,$myid);
1341 if($RULES["schweinchen"]=="both" )
1342 DB_insert_comment("Schweinchen! ",$playid,$myid);
1344 echo "schweinchen = ".$GAME["schweinchen"]." ---<br />";
1347 /* if sequence == 4 check who one in case of wedding */
1348 if($sequence == 4 && $GT == "wedding")
1350 /* is wedding resolve */
1351 $resolved = DB_get_sickness_by_gameid($gameid);
1354 /* who has wedding */
1355 $userids = DB_get_all_userid_by_gameid($gameid);
1356 foreach($userids as $user)
1358 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1359 if($usersick == "wedding")
1362 /* who won the trick */
1363 $play = DB_get_cards_by_trick($trickid);
1364 $winner = get_winner($play,$gametype); /* returns the position */
1365 $winnerid = DB_get_userid_by_gameid_and_position($gameid,$winner);
1366 /* is tricknr <=3 */
1367 if($tricknr <=3 && $winnerid!=$whosick)
1369 /* set resolved at tricknr*/
1370 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1372 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1373 DB_set_party_by_hash($whash,"re");
1375 if($tricknr == 3 && $winnerid==$whosick)
1377 /* set resolved at tricknr*/
1378 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1383 /* if sequence == 4, set winner of the trick, count points and set the next player */
1386 $play = DB_get_cards_by_trick($trickid);
1387 $winner = get_winner($play,$gametype); /* returns the position */
1389 /* check if someone caught a fox */
1390 if(DB_get_gametype_by_gameid($gameid)!="solo")
1391 foreach($play as $played)
1393 if ( $played['card']==19 || $played['card']==20 )
1394 if ($played['pos']!= $winner )
1396 /* possible caught a fox, check party */
1397 $uid1 = DB_get_userid_by_gameid_and_position($gameid,$winner);
1398 $uid2 = DB_get_userid_by_gameid_and_position($gameid,$played['pos']);
1400 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1401 $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1403 if($party1 != $party2)
1404 mysql_query("INSERT INTO Score".
1405 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1408 /* check for karlchen (jack of clubs in the last trick)*/
1409 if(DB_get_gametype_by_gameid($gameid)!="solo" && $tricknr == 12)
1410 foreach($play as $played)
1411 if ( $played['card']==11 || $played['card']==12 )
1412 if ($played['pos'] == $winner )
1414 /* possible caught a fox, check party */
1415 $uid1 = DB_get_userid_by_gameid_and_position($gameid,$winner);
1416 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1418 mysql_query("INSERT INTO Score".
1419 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1421 /* check for doppelopf (>40 points)*/
1423 foreach($play as $played)
1425 $points += DB_get_card_value_by_cardid($played['card']);
1429 $uid1 = DB_get_userid_by_gameid_and_position($gameid,$winner);
1430 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1432 mysql_query("INSERT INTO Score".
1433 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
1437 mysql_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1439 echo "ERROR during scoring";
1442 echo "DEBUG: position $winner won the trick <br />";
1444 /* who is the next player? */
1449 $next = DB_get_pos_by_hash($me)+1;
1451 if($next==5) $next=1;
1453 /* check for coment */
1454 if(myisset("comment"))
1456 $comment = $_REQUEST["comment"];
1458 DB_insert_comment($comment,$playid,$myid);
1461 /* display played card */
1462 $pos = DB_get_pos_by_hash($me);
1465 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1466 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1467 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1470 echo " <div class=\"card".($pos-1)."\">\n ";
1472 /* display comments */
1473 display_card($card,$PREF["cardset"]);
1475 echo "\n <span class=\"comment\"> ".$comment."</span>\n";
1478 /*check if we still have cards left, else set status to gameover */
1479 if(sizeof(DB_get_hand($me))==0)
1481 DB_set_hand_status_by_hash($me,'gameover');
1482 $mystatus = 'gameover';
1485 /* if all players are done, set game status to game over,
1486 * get the points of the last trick and send out an email
1489 $userids = DB_get_all_userid_by_gameid($gameid);
1492 foreach($userids as $user)
1493 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1497 DB_set_game_status_by_gameid($gameid,"gameover");
1499 /* email next player, if game is still running */
1500 if(DB_get_game_status_by_gameid($gameid)=='play')
1502 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1503 $email = DB_get_email_by_hash($next_hash);
1504 $who = DB_get_userid_by_email($email);
1505 DB_set_player_by_gameid($gameid,$who);
1507 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1508 "It's your turn now.\n".
1509 "Use this link to play a card: ".$HOST.$INDEX."?me=".$next_hash."\n\n" ;
1510 if( DB_get_email_pref_by_uid($who)!="emailaddict" )
1511 mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
1513 else /* send out final email */
1515 /* individual score */
1516 $result = mysql_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1517 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1518 " LEFT JOIN User ON User.id=Hand.user_id".
1519 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1520 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1521 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1522 " WHERE Hand.game_id='$gameid'".
1523 " GROUP BY User.fullname" );
1524 $message = "The game is over. Thanks for playing :)\n";
1525 $message .= "Final score:\n";
1526 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1527 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1529 $result = mysql_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1530 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1531 " LEFT JOIN User ON User.id=Hand.user_id".
1532 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1533 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1534 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1535 " WHERE Hand.game_id='$gameid'".
1536 " GROUP BY Hand.party" );
1537 $message .= "\nTotals:\n";
1540 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1542 $message .= " ".$r[0]." ".$r[1]."\n";
1545 else if($r[0] == "contra")
1550 * save score in database
1554 /* get calls from re/contra */
1556 $call_contra = NULL;
1557 foreach($userids as $user)
1559 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1560 $call = DB_get_call_by_hash($hash);
1561 $party = DB_get_party_by_hash($hash);
1565 $call = (int) $call;
1571 else if( $call < $call_re)
1574 else if($party=="contra")
1576 if($call_contra==NULL)
1577 $call_contra = $call;
1578 else if( $call < $call_re)
1579 $call_contra = $call;
1584 /* figure out who one */
1585 $winning_party = NULL;
1587 if($call_re == NULL && $call_contra==NULL)
1589 $winning_party="re";
1591 $winning_party="contra";
1596 $offset = 120 - $call_re;
1598 $offset--; /* since we use a > in the next equation */
1600 if($re > 120+$offset)
1601 $winning_party="re";
1602 else if ( $call_contra == NULL )
1603 $winning_party="contra";
1608 $offset = 120 - $call_contra;
1609 if($call_contra == 0)
1610 $offset--; /* since we use a > in the next equation */
1612 if($contra > 120+$offset)
1613 $winning_party="contra";
1614 else if ( $call_contra == NULL )
1615 $winning_party="re";
1619 /* one point for each call of the other party in case the other party didn't win
1620 * and one point each in case the party made more than points than one of the calls
1622 if($winning_party!="contra" && $call_contra!=NULL)
1624 for( $p=$call_contra;$p<=120; $p+=30 )
1626 mysql_query("INSERT INTO Score".
1627 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
1630 for( $p=$call_contra; $p<120; $p+=30)
1633 mysql_query("INSERT INTO Score".
1634 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'made$p')");
1637 if($winning_party!="re" and $call_re!=NULL)
1639 for( $p=$call_re;$p<=120; $p+=30 )
1641 mysql_query("INSERT INTO Score".
1642 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'against$p')");
1645 for( $p=$call_re; $p<120; $p+=30)
1648 mysql_query("INSERT INTO Score".
1649 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
1653 /* point in case contra won */
1654 if($winning_party=="contra")
1656 mysql_query("INSERT INTO Score".
1657 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1660 /* one point each for winning and each 30 points + calls */
1661 if($winning_party=="re")
1663 foreach(array(120,150,180,210,240) as $p)
1666 if($p==240 || $call_contra!=NULL)
1670 mysql_query("INSERT INTO Score".
1671 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
1673 /* re called something and won */
1674 foreach(array(0,30,60,90,120) as $p)
1676 if($call_re!=NULL && $call_re<$p+1)
1677 mysql_query("INSERT INTO Score".
1678 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
1681 else if( $winning_party=="contra")
1683 foreach(array(120,150,180,210,240) as $p)
1686 if($p==240 || $call_re!=NULL)
1689 if($contra>$p-$offset)
1690 mysql_query("INSERT INTO Score".
1691 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
1693 /* re called something and won */
1694 foreach(array(0,30,60,90,120) as $p)
1696 if($call_contra!=NULL && $call_contra<$p+1)
1697 mysql_query("INSERT INTO Score".
1698 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
1703 /* add score points to email */
1706 $message .= " Points Re: \n";
1707 $queryresult = mysql_query("SELECT score FROM Score ".
1708 " WHERE game_id=$gameid AND party='re'".
1710 while($r = mysql_fetch_array($queryresult,MYSQL_NUM) )
1712 $message .= " ".$r[0]."\n";
1715 $message .= " Points Contra: \n";
1716 $queryresult = mysql_query("SELECT score FROM Score ".
1717 " WHERE game_id=$gameid AND party='contra'".
1719 while($r = mysql_fetch_array($queryresult,MYSQL_NUM) )
1721 $message .= " ".$r[0]."\n";
1724 $message .= " Total Points (from the Re point of view): $Tpoint\n";
1727 /* send out final email */
1730 foreach($userids as $user)
1731 $all[] = DB_get_email_by_userid($user);
1732 $To = implode(",",$all);
1734 $help = "\n\n (you can use reply all on this email to reach all the players.)\n";
1735 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 1(2)",$message.$help);
1737 foreach($userids as $user)
1739 $To = DB_get_email_by_userid($user);
1740 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1742 $link = "Use this link to have a look at game ".DB_format_gameid($gameid).": ".
1743 $HOST.$INDEX."?me=".$hash."\n\n" ;
1744 if( DB_get_email_pref_by_uid($user) != "emailaddict" )
1745 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 2(2)",$link);
1751 echo "can't find that card?! <br />\n";
1754 else if(myisset("card") && !$myturn )
1756 echo "please wait until it's your turn! <br />\n";
1759 if($seq!=4 && $trickNR>1)
1760 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1762 /* display points in case game is over */
1763 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1765 echo " <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1766 " <div class=\"trick\" id=\"trick13\">\n";
1767 /* add pic for re/contra
1768 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1770 $result = mysql_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1771 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1772 " LEFT JOIN User ON User.id=Hand.user_id".
1773 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1774 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1775 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1776 " WHERE Hand.game_id='$gameid'".
1777 " GROUP BY User.fullname" );
1778 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1779 echo " <div class=\"card".($r[3]-1)."\">\n".
1780 " <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
1783 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1787 echo "</ul>\n"; /* end ul tricks*/
1789 $mycards = DB_get_hand($me);
1790 $mycards = mysort($mycards,$gametype);
1791 echo "<div class=\"mycards\">\n";
1793 if($myturn && !myisset("card") && $mystatus=='play' )
1795 echo "Hello ".$myname.", it's your turn! <br />\n";
1796 echo "Your cards are: <br />\n";
1798 /* do we have to follow suite? */
1800 if(have_suit($mycards,$firstcard))
1803 foreach($mycards as $card)
1805 if($followsuit && !same_type($card,$firstcard))
1806 display_card($card,$PREF["cardset"]);
1808 display_link_card($card,$PREF["cardset"]);
1811 else if($mystatus=='play' )
1813 echo "Your cards are: <br />\n";
1814 foreach($mycards as $card)
1815 display_card($card,$PREF["cardset"]);
1817 else if($mystatus=='gameover')
1819 $oldcards = DB_get_all_hand($me);
1820 $oldcards = mysort($oldcards,$gametype);
1821 echo "Your cards were: <br />\n";
1822 foreach($oldcards as $card)
1823 display_card($card,$PREF["cardset"]);
1825 $userids = DB_get_all_userid_by_gameid($gameid);
1826 foreach($userids as $user)
1828 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1834 $name = DB_get_name_by_userid($user);
1835 $oldcards = DB_get_all_hand($userhash);
1836 $oldcards = mysort($oldcards,$gametype);
1837 echo "$name's cards were: <br />\n";
1838 foreach($oldcards as $card)
1839 display_card($card,$PREF["cardset"]);
1845 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1846 if($mystatus=='play')
1849 /* the following happens only when the gamestatus is 'gameover' */
1850 /* check if game is over, display results */
1851 if(DB_get_game_status_by_gameid($gameid)=='play')
1853 echo "The game is over for you.. other people still need to play though";
1857 $result = mysql_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1858 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1859 " LEFT JOIN User ON User.id=Hand.user_id".
1860 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1861 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1862 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1863 " WHERE Hand.game_id='$gameid'".
1864 " GROUP BY Hand.party" );
1865 echo "<div class=\"total\"> Totals:<br />\n";
1866 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1867 echo " ".$r[0]." ".$r[1]."<br />\n";
1869 echo "<div class=\"re\">\n Points Re: <br />\n";
1870 $queryresult = mysql_query("SELECT score FROM Score ".
1871 " WHERE game_id=$gameid AND party='re'".
1873 while($r = mysql_fetch_array($queryresult,MYSQL_NUM) )
1874 echo " ".$r[0]."<br />\n";
1877 echo "<div class=\"contra\">\n Points Contra: <br />\n";
1878 $queryresult = mysql_query("SELECT score FROM Score ".
1879 " WHERE game_id=$gameid AND party='contra'".
1881 while($r = mysql_fetch_array($queryresult,MYSQL_NUM) )
1882 echo " ".$r[0]."<br />\n";
1891 myerror("error in testing the status");
1893 /* output left menu */
1894 display_user_menu();
1896 /* output right menu */
1898 /* display rule set for this game */
1899 echo "<div class=\"gameinfo\">\n";
1901 if($gamestatus != 'pre')
1902 echo " Gametype: $GT <br />\n";
1904 echo "Rules: <br />\n";
1905 echo "10ofhearts : ".$RULES["dullen"] ."<br />\n";
1906 echo "schweinchen: ".$RULES["schweinchen"] ."<br />\n";
1907 echo "call: ".$RULES["call"] ."<br />\n";
1910 if($gamestatus == 'play' )
1911 output_form_calls($me);
1913 /* get time from the last action of the game */
1914 $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
1915 $r = mysql_fetch_array($result,MYSQL_NUM);
1916 $gameend = time() - strtotime($r[0]);
1918 if($gamestatus == 'play' || $gameend < 60*60*24*7)
1920 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1924 echo "<input type=\"submit\" value=\"submit\" />\n";
1927 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1931 $session = DB_get_session_by_gameid($gameid);
1932 $result = mysql_query("SELECT id,create_date FROM Game".
1933 " WHERE session=$session".
1934 " ORDER BY create_date DESC".
1938 $r = mysql_fetch_array($result,MYSQL_NUM);
1940 if(!$session || $gameid==$r[0])
1942 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
1943 $names = DB_get_all_names_by_gameid($gameid);
1944 $type = DB_get_gametype_by_gameid($gameid);
1947 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
1949 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
1953 $session = DB_get_session_by_gameid($gameid);
1954 $score = generate_score_table($session);
1956 // if(size_of($score)>30)
1967 /* user status page */
1968 else if( myisset("email","password") || isset($_SESSION["name"]) )
1970 /* test id and password, should really be done in one step */
1971 if(!isset($_SESSION["name"]))
1973 $email = $_REQUEST["email"];
1974 $password = $_REQUEST["password"];
1978 $name = $_SESSION["name"];
1979 $email = DB_get_email_by_name($name);
1980 $password = DB_get_passwd_by_name($name);
1983 if(myisset("forgot"))
1987 $myid = DB_get_userid_by_email($email);
1993 /* check how many entries in recovery table */
1994 $number = DB_get_number_of_passwords_recovery($myid);
1996 /* if less than N recent ones, add a new one and send out email */
1999 echo "Ok, I send you a new password. <br />";
2001 echo "N.B. You tried this already $number times during the last day and it will only work ".
2002 " 5 times during a day.<br />";
2003 echo "The new password will be valid for one day, make sure you reset it to something else.<br />";
2004 echo "Back to the <a href=\"$INDEX\">main page</a>.";
2006 $TIME = (string) time(); /* to avoid collisions */
2007 $hash = md5("Anewpassword".$email.$TIME);
2008 $newpw = substr($hash,1,8);
2010 $message = "Someone (hopefully you) requested a new password. \n".
2011 "You can use this email and the following password: \n".
2013 "to log into the server. The new password is valid for 24h, so make\n".
2014 "sure you reset your password to something new. Your old password will\n".
2015 " also still be valid until you set a new one\n";
2016 mymail($email,$EmailName."recovery ",$message);
2018 DB_set_recovery_password($myid,md5($newpw));
2022 echo "Sorry you already tried 5 times during the last 24h.<br />".
2023 "You need to use one of those passwords or wait to get a new one.<br />";
2024 echo "Back to the <a href=\"$INDEX\">main page</a>.";
2030 echo "You need to give me an email address! <br />".
2031 "Please try <a href=\"$INDEX\">again</a>.";
2033 echo "Couldn't find a player with this email! <br />".
2034 "Please contact Arun, if you think this is a mistake <br />".
2035 "or else try <a href=\"$INDEX\">again</a>.";
2040 /* verify password and email */
2041 if(strlen($password)!=32)
2042 $password = md5($password);
2045 $myid = DB_get_userid_by_email_and_password($email,$password);
2051 $myname = DB_get_name_by_email($email);
2052 $_SESSION["name"] = $myname;
2057 if(myisset("setpref"))
2059 $setpref=$_REQUEST["setpref"];
2063 case "englishcards":
2064 $result = mysql_query("SELECT * from User_Prefs".
2065 " WHERE user_id='$myid' AND pref_key='cardset'" );
2066 if( mysql_fetch_array($result,MYSQL_NUM))
2067 $result = mysql_query("UPDATE User_Prefs SET value=".DB_quote_smart($setpref).
2068 " WHERE user_id='$myid' AND pref_key='cardset'" );
2070 $result = mysql_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','cardset',".
2071 DB_quote_smart($setpref).")");
2072 echo "Ok, changed you preferences for the cards.\n";
2075 case "emailnonaddict":
2076 $result = mysql_query("SELECT * from User_Prefs".
2077 " WHERE user_id='$myid' AND pref_key='email'" );
2078 if( mysql_fetch_array($result,MYSQL_NUM))
2079 $result = mysql_query("UPDATE User_Prefs SET value=".DB_quote_smart($setpref).
2080 " WHERE user_id='$myid' AND pref_key='email'" );
2082 $result = mysql_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','email',".
2083 DB_quote_smart($setpref).")");
2084 echo "Ok, changed you preferences for sending out emails.\n";
2088 else if(myisset("passwd"))
2090 if( $_REQUEST["passwd"]=="ask" )
2092 /* reset password form*/
2093 output_password_recovery($email,$password);
2095 else if($_REQUEST["passwd"]=="set")
2097 /* reset password */
2100 /* check if old password matches */
2101 $oldpasswd = md5($_REQUEST["password0"]);
2102 if(!( ($password == $oldpasswd) || DB_check_recovery_passwords($oldpasswd,$email) ))
2104 /* check if new passwords are types the same twice */
2105 if($_REQUEST["password1"] != $_REQUEST["password2"] )
2111 echo "The new passwords don't match. <br />";
2114 echo "The old password is not correct. <br />";
2117 echo "Changed the password.<br />";
2118 mysql_query("UPDATE User SET password='".md5($_REQUEST["password1"]).
2119 "' WHERE id=".DB_quote_smart($myid));
2125 else /* output default user page */
2127 /* display links to settings */
2128 output_user_settings();
2130 DB_update_user_timestamp($myid);
2132 display_user_menu();
2134 echo "<div class=\"user\">";
2135 echo "<h4>These are all your games:</h4>\n";
2136 echo "<p>Session: <br />\n";
2137 echo "<span class=\"gamestatuspre\"> p </span> = pre-game phase ";
2138 echo "<span class=\"gamestatusplay\">P </span> = game in progess ";
2139 echo "<span class=\"gamestatusover\">F </span> = game finished <br />";
2143 $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.mod_date,Game.player,Game.status from Hand".
2144 " LEFT JOIN Game ON Game.id=Hand.game_id".
2145 " WHERE user_id='$myid'".
2146 " ORDER BY Game.session,Game.create_date" );
2148 echo "<table>\n <tr><td>\n";
2149 while( $r = mysql_fetch_array($result,MYSQL_NUM))
2151 $game = DB_format_gameid($r[1]);
2152 $gamenr = (int) $game;
2153 if($gamenrold < $gamenr)
2156 echo "</td></tr>\n <tr> <td>$gamenr:</td><td> ";
2158 echo "$gamenr:</td><td> ";
2159 $gamenrold = $gamenr;
2163 echo "\n <span class=\"gamestatuspre\"><a href=\"".$INDEX."?me=".$r[0]."\">p </a></span> ";
2166 else if ($r[4]=='gameover')
2167 echo "\n <span class=\"gamestatusover\"><a href=\"".$INDEX."?me=".$r[0]."\">F </a></span> ";
2170 echo "\n <span class=\"gamestatusplay\"><a href=\"".$INDEX."?me=".$r[0]."\">P </a></span> ";
2172 if($r[4] != 'gameover')
2174 echo "</td><td>\n ";
2178 echo "(it's <strong>your</strong> turn)\n";
2181 $name = DB_get_name_by_userid($r[3]);
2183 if(DB_get_reminder($r[3],$gameid)==0)
2184 if(time()-strtotime($r[2]) > 60*60*24*7)
2186 "<a href=\"$INDEX?remind=1&me=".$r[0]."\">Send a reminder.</a>";
2187 echo "(it's $name's turn)\n";
2190 if(time()-strtotime($r[2]) > 60*60*24*30)
2192 "<a href=\"$INDEX?cancel=1&me=".$r[0]."\">Cancel?</a>".
2193 " (clicking here is final and can't be restored)";
2197 echo "</td></tr>\n</table>\n";
2198 $names = DB_get_all_names();
2199 echo "<h4>Registered players:</h4>\n<p>\n";
2200 echo implode(", ",$names)."\n";
2201 echo "</p>\n</div>";
2206 echo "<div class=\"message\">Sorry email and password don't match. Please <a href=\"$INDEX\">try again</a>. </div>";
2213 /* default login page */
2216 $pre[0]=0;$game[0]=0;$done[0]=0;
2217 $r=mysql_query("SELECT COUNT(id) FROM Game GROUP BY status");
2219 $pre = mysql_fetch_array($r,MYSQL_NUM);
2220 $game = mysql_fetch_array($r,MYSQL_NUM);
2221 $done = mysql_fetch_array($r,MYSQL_NUM);
2224 $r=mysql_query("SELECT AVG(datediff(mod_date,create_date)) FROM Game where status='gameover' ");
2226 $avgage= mysql_fetch_array($r,MYSQL_NUM);
2230 output_home_page($pre[0],$game[0],$done[0],$avgage[0]);