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" ))
55 if( !isset($_SESSION["name"]) )
57 echo "<div class=\"message\">Please <a href=\"$INDEX\">log in</a>.</div>";
62 $name = $_SESSION["name"];
64 $PlayerA = $_REQUEST["PlayerA"];
65 $PlayerB = $_REQUEST["PlayerB"];
66 $PlayerC = $_REQUEST["PlayerC"];
67 $PlayerD = $_REQUEST["PlayerD"];
69 /* check if user is in the game */
70 if(!in_array($name,array($PlayerA,$PlayerB,$PlayerC,$PlayerD)))
72 echo "<div class=\"message\">You need to be one of the players to start a <a href=\"$INDEX?new\">new game</a>.</div>";
78 $dullen = $_REQUEST["dullen"];
79 $schweinchen = $_REQUEST["schweinchen"];
80 $call = $_REQUEST["call"];
82 $EmailA = DB_get_email_by_name($PlayerA);
83 $EmailB = DB_get_email_by_name($PlayerB);
84 $EmailC = DB_get_email_by_name($PlayerC);
85 $EmailD = DB_get_email_by_name($PlayerD);
87 if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
89 echo "couldn't find one of the names, please start a new game";
95 $useridA = DB_get_userid_by_name($PlayerA);
96 $useridB = DB_get_userid_by_name($PlayerB);
97 $useridC = DB_get_userid_by_name($PlayerC);
98 $useridD = DB_get_userid_by_name($PlayerD);
100 /* create random numbers */
101 $randomNR = create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD);
102 $randomNRstring = join(":",$randomNR);
106 if(myisset("followup") )
108 $followup= $_REQUEST["followup"];
109 $session = DB_get_session_by_gameid($followup);
110 $ruleset = DB_get_ruleset_by_gameid($followup); /* just copy ruleset from old game,
111 this way no manipulation is possible */
113 /* check if there is a game in pre or play mode, in that case do nothing */
114 if( DB_is_session_active($session) > 0 )
116 echo "<p class=\"message\"> There is already a game going on in session $session, you can't start a new one</p>";
121 else if ( DB_is_session_active($session) < 0 )
123 echo "<p class=\"message\"> ERROR: status of session $session couldn't be determined.</p>";
130 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
131 "'$ruleset','$session' ,NULL)");
134 /* get max session */
135 $max = DB_get_max_session();
137 mysql_query("UPDATE Game SET session='".$max."' WHERE id=".DB_quote_smart($followup));
138 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
139 "'$ruleset','$max' ,NULL)");
144 /* get ruleset information or create new one */
145 $ruleset = DB_get_ruleset($dullen,$schweinchen,$call);
148 myerror("Error defining ruleset: $ruleset");
153 /* get max session */
154 $max = DB_get_max_session();
157 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre', ".
158 "'$ruleset','$max' ,NULL)");
160 $game_id = mysql_insert_id();
163 $TIME = (string) time(); /* to avoid collisions */
164 $hashA = md5("AGameOfDoko".$game_id.$PlayerA.$EmailA.$TIME);
165 $hashB = md5("AGameOfDoko".$game_id.$PlayerB.$EmailB.$TIME);
166 $hashC = md5("AGameOfDoko".$game_id.$PlayerC.$EmailC.$TIME);
167 $hashD = md5("AGameOfDoko".$game_id.$PlayerD.$EmailD.$TIME);
170 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridA).
171 ", ".DB_quote_smart($hashA).", 'start','1',NULL,NULL,NULL,NULL)");
172 $hand_idA = mysql_insert_id();
173 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridB).
174 ", ".DB_quote_smart($hashB).", 'start','2',NULL,NULL,NULL,NULL)");
175 $hand_idB = mysql_insert_id();
176 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridC).
177 ", ".DB_quote_smart($hashC).", 'start','3',NULL,NULL,NULL,NULL)");
178 $hand_idC = mysql_insert_id();
179 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridD).
180 ", ".DB_quote_smart($hashD).", 'start','4',NULL,NULL,NULL,NULL)");
181 $hand_idD = mysql_insert_id();
185 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idA', '".$randomNR[$i]."', 'false')");
186 for($i=12;$i<24;$i++)
187 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idB', '".$randomNR[$i]."', 'false')");
188 for($i=24;$i<36;$i++)
189 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idC', '".$randomNR[$i]."', 'false')");
190 for($i=36;$i<48;$i++)
191 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idD', '".$randomNR[$i]."', 'false')");
193 /* send out email, TODO: check for error with email */
195 "you are invited to play a game of DoKo (that is to debug the program ;).\n".
196 "Place comments and bug reports here:\n".
197 "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
198 "The whole round would consist of the following players:\n".
203 "If you want to join this game, please follow this link:\n\n".
204 "".$HOST.$INDEX."?me=";
206 mymail($EmailA,"You are invited to a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
207 mymail($EmailB,"You are invited to a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
208 mymail($EmailC,"You are invited to a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
209 mymail($EmailD,"You are invited to a game of DoKo","Hello $PlayerD,\n".$message.$hashD);
211 echo "<div class=\"message\">You started a new game. The emails have been sent out!</div>\n";
213 } /* end set up a new game */
214 /* cancel a game, if nothing has happend in the last N minutes */
215 else if(myisset("cancel","me"))
219 $me = $_REQUEST["me"];
221 /* test for valid ID */
222 $myid = DB_get_userid_by_hash($me);
225 echo "Can't find you in the database, please check the url.<br />\n";
226 echo "perhaps the game has been canceled, check by login in <a href=\"$INDEX\">here</a>.";
232 DB_update_user_timestamp($myid);
234 /* get some information from the DB */
235 $gameid = DB_get_gameid_by_hash($me);
236 $myname = DB_get_name_by_hash($me);
238 /* check if game really is old enough */
239 $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
240 $r = mysql_fetch_array($result,MYSQL_NUM);
241 if(time()-strtotime($r[0]) > 60*60*24*30) /* = 1 month */
243 $message = "Hello, \n\n".
244 "Game ".DB_format_gameid($gameid).
245 " has been canceled since nothing happend for a while and $myname requested it.\n";
247 $userids = DB_get_all_userid_by_gameid($gameid);
248 foreach($userids as $user)
250 $To = DB_get_email_by_userid($user);
251 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled (timed out)",$message);
254 /* delete everything from the dB */
257 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid).
258 " has been canceled.<br /><br /></p>";
261 echo "<p>You need to wait longer before you can cancel a game...</p>\n";
263 /* send out a reminder */
264 else if(myisset("remind","me"))
268 $me = $_REQUEST["me"];
270 /* test for valid ID */
271 $myid = DB_get_userid_by_hash($me);
274 echo "Can't find you in the database, please check the url.<br />\n";
275 echo "perhaps the game has been canceled, check by login in <a href=\"$INDEX\">here</a>.";
281 DB_update_user_timestamp($myid);
283 /* get some information from the DB */
284 $gameid = DB_get_gameid_by_hash($me);
285 $myname = DB_get_name_by_hash($me);
287 /* check if game really is old enough */
288 $result = mysql_query("SELECT mod_date,player,status from Game WHERE id='$gameid' " );
289 $r = mysql_fetch_array($result,MYSQL_NUM);
290 if( (time()-strtotime($r[0]) > 60*60*24*7) && ($r[2]!='gameover') ) /* = 1 week */
292 $name = DB_get_name_by_userid($r[1]);
293 $To = DB_get_email_by_userid($r[1]);
294 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$r[1]);
296 $message = "Hello $name, \n\n".
297 "It's your turn in game ".DB_format_gameid($gameid)." \n".
298 "Actually everyone else is waiting for you for more than a week now ;)\n\n".
299 "Please visit this link now to continue: \n".
300 " ".$HOST.$INDEX."?me=".$userhash."\n\n" ;
302 if(DB_get_reminder($r[1],$gameid)>0)
304 echo "<p>An email has already been sent out.</p>\n";
308 DB_set_reminder($r[1],$gameid);
309 mymail($To,$EmailName."Reminder: game ".DB_format_gameid($gameid)." it's your turn",$message);
311 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid).
312 ": an email has been sent out.<br /><br /></p>";
316 echo "<p>You need to wait longer before you can send out a reminder...</p>\n";
318 /* handle request from one specific player for one game,
319 * (the hash is set on a per game base) */
320 else if(myisset("me"))
322 $me = $_REQUEST["me"];
324 /* test for valid ID */
325 $myid = DB_get_userid_by_hash($me);
328 echo "Can't find you in the database, please check the url.<br />\n";
329 echo "perhaps the game has been canceled, check by login in <a href=\"$INDEX\">here</a>.";
335 if(isset($_SESSION["name"]))
336 output_status($_SESSION["name"]);
338 /* the user had done something, update the timestamp */
339 DB_update_user_timestamp($myid);
341 /* get some information from the DB */
342 $gameid = DB_get_gameid_by_hash($me);
343 $myname = DB_get_name_by_hash($me);
344 $mystatus = DB_get_status_by_hash($me);
345 $mypos = DB_get_pos_by_hash($me);
346 $myhand = DB_get_handid_by_hash($me);
347 $session = DB_get_session_by_gameid($gameid);
349 /* get prefs and save them */
353 /* get rule set for this game */
354 $result = mysql_query("SELECT * FROM Rulesets".
355 " LEFT JOIN Game ON Game.ruleset=Rulesets.id ".
356 " WHERE Game.id='$gameid'" );
357 $r = mysql_fetch_array($result,MYSQL_NUM);
359 $RULES["dullen"] = $r[2];
360 $RULES["schweinchen"] = $r[3];
361 $RULES["call"] = $r[4];
363 /* get some infos about the game */
364 $gametype = DB_get_gametype_by_gameid($gameid);
365 $gamestatus = DB_get_game_status_by_gameid($gameid);
367 if($gametype=="solo")
369 $gametype = DB_get_solo_by_gameid($gameid);
370 $GT = $gametype." ".$GT;
373 /* does anyone have both foxes */
374 $GAME["schweinchen"]=0;
377 $hash = DB_get_hash_from_game_and_pos($gameid,$i);
378 $cards = DB_get_all_hand($hash);
379 if( in_array("19",$cards) && in_array("20",$cards) )
381 $GAME["schweinchen"]=1;
382 $GAME["schweinchen-who"]=$hash;
386 /* put everyting in a form */
387 echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
391 /* output extra division in case this game is part of a session */
394 echo "<div class=\"session\">\n".
395 "This game is part of session $session: \n";
396 $hashes = DB_get_hashes_by_session($session,$myid);
398 foreach($hashes as $hash)
403 echo "<a href=\"".$INDEX."?me=".$hash."\">$i</a> \n";
409 /* display the table and the names */
412 /* mystatus gets the player through the different stages of a game.
414 * init: check values from start,
416 * check: check for return values from init
417 * poverty: handle poverty, wait here until all player have reached this state
418 * display sickness and move on to game
419 * play: game in progress
420 * gameover: are we revisiting a game
427 output_check_want_to_play($me);
432 if($_REQUEST["in"] == "no")
434 /* cancel the game */
435 $message = "Hello, \n\n".
436 "the game has been canceled due to the request of one of the players.\n";
438 $userids = DB_get_all_userid_by_gameid($gameid);
439 foreach($userids as $user)
441 $To = DB_get_email_by_userid($user);
442 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
445 /* delete everything from the dB */
451 /* user wants to join the game */
453 /* move on to the next stage,
454 * no break statement to immediately go to the next stage
457 DB_set_hand_status_by_hash($me,'init');
459 /* check if everyone has reached this stage, send out email */
460 $userids = DB_get_all_userid_by_gameid($gameid);
462 foreach($userids as $user)
464 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
465 if($userstat!='init')
467 /* whos turn is it? */
468 DB_set_player_by_gameid($gameid,$user);
474 /* all done, send out email unless this player is the startplayer */
475 $startplayer = DB_get_startplayer_by_gameid($gameid);
476 if($mypos == $startplayer)
478 /* do nothing, go to next stage */
482 /* email startplayer */
484 $email = DB_get_email_by_pos_and_gameid($startplayer,$gameid);
485 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
486 $who = DB_get_userid_by_email($email);
487 DB_set_player_by_gameid($gameid,$who);
489 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
490 "Use this link to play a card: ".$HOST.$INDEX."?me=".$hash."\n\n" ;
491 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
499 $mycards = DB_get_hand($me);
502 output_check_for_sickness($me,$mycards);
504 echo "<p class=\"mycards\">Your cards are: <br />\n";
505 foreach($mycards as $card)
506 display_card($card,$PREF["cardset"]);
509 /* move on to the next stage*/
510 DB_set_hand_status_by_hash($me,'check');
514 /* ok, user is in the game, saw his cards and selected his vorbehalt
515 * so first we check what he selected
517 if(!myisset("solo","wedding","poverty","nines") )
519 /* all these variables have a pre-selected default,
520 * so we should never get here,
521 * unless a user tries to cheat ;)
522 * can also happen if user reloads the page!
524 echo "<p class=\"message\"> You need to answer the <a href=\"$INDEX?me=$me&in=yes\">questions</a>.</p>";
525 DB_set_hand_status_by_hash($me,'init');
529 /* check if someone selected more than one vorbehalt */
531 if($_REQUEST["solo"]!="No") $Nvorbehalt++;
532 if($_REQUEST["wedding"] == "yes") $Nvorbehalt++;
533 if($_REQUEST["poverty"] == "yes") $Nvorbehalt++;
534 if($_REQUEST["nines"] == "yes") $Nvorbehalt++;
538 echo "<p class=\"message\"> You selected more than one vorbehalt, please go back ".
539 "and answer the <a href=\"$INDEX?me=$me&in=yes\">question</a> again.</p>";
540 DB_set_hand_status_by_hash($me,'init');
544 echo "<p class=\"message\">Processing what you selected in the last step...";
546 /* check if this sickness needs to be handled first */
547 $gametype = DB_get_gametype_by_gameid($gameid);
548 $startplayer = DB_get_startplayer_by_gameid($gameid);
550 if( $_REQUEST["solo"]!="No")
552 /* user wants to play a solo */
554 /* store the info in the user's hand info */
555 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
556 DB_set_sickness_by_hash($me,"solo");
558 echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
560 if($gametype == "solo" && $startplayer<$mypos)
561 {}/* do nothing, since someone else already is playing solo */
564 /* this solo comes first
565 * store info in game table
567 DB_set_gametype_by_gameid($gameid,"solo");
568 DB_set_startplayer_by_gameid($gameid,$mypos);
569 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
572 else if($_REQUEST["wedding"] == "yes")
574 /* TODO: add silent solo somewhere*/
575 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
576 DB_set_sickness_by_hash($me,"wedding");
578 else if($_REQUEST["poverty"] == "yes")
580 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
581 DB_set_sickness_by_hash($me,"poverty");
583 else if($_REQUEST["nines"] == "yes")
585 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
586 " is playing solo, this game will be canceled.<br />\n";
587 DB_set_sickness_by_hash($me,"nines");
590 echo " Ok, done with checking, please go to the <a href=\"$INDEX?me=$me\">next step of the setup</a>.</p>";
592 /* move on to the next stage*/
593 DB_set_hand_status_by_hash($me,'poverty');
595 /* check if everyone has reached this stage, send out email */
596 $userids = DB_get_all_userid_by_gameid($gameid);
598 foreach($userids as $user)
600 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
601 if($userstat!='poverty' && $userstat!='play')
604 DB_set_player_by_gameid($gameid,$user);
609 /* reset player = everyone has to do something now */
610 DB_set_player_by_gameid($gameid,NULL);
612 foreach($userids as $user)
614 $To = DB_get_email_by_userid($user);
615 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
618 $message = "Everyone finish the questionary in game ".DB_format_gameid($gameid).", ".
619 "please visit this link now to continue: \n".
620 " ".$HOST.$INDEX."?me=".$userhash."\n\n" ;
621 mymail($To,$EmailName." finished setup in game ".DB_format_gameid($gameid),$message);
630 /* here we need to check if there is a solo or some other form of sickness.
631 * If so, which one is the most important one
632 * set that one in the Game table
633 * tell people about it.
635 echo "<div class=\"message\">\n";
636 echo "<p> Checking if someone else selected solo, nines, wedding or poverty.</p>";
638 /* check if everyone has reached this stage */
639 $userids = DB_get_all_userid_by_gameid($gameid);
641 foreach($userids as $user)
643 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
644 if($userstat!='poverty' && $userstat!='play')
650 echo "This step can only be handled after everyone finished the last step. ".
651 "Seems like this is not the case, so you need to wait a bit... ".
652 "you will get an email once that is the case, please use the link in ".
653 "that email to continue the game.<br />";
657 echo "Everyone has finished checking their cards, let's see what they said...<br />";
659 /* check what kind of game we are playing, in case there are any solos this already
660 *will have the correct information in it */
661 $gametype = DB_get_gametype_by_gameid($gameid);
662 $startplayer = DB_get_startplayer_by_gameid($gameid);
664 /* check for different sickness and just output a general info */
669 foreach($userids as $user)
671 $name = DB_get_name_by_userid($user);
672 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
673 if($usersick == 'nines')
676 echo "$name has a Vorbehalt. <br />";
679 else if($usersick == 'poverty')
682 echo "$name has a Vorbehalt. <br />";
684 else if($usersick == 'wedding')
687 echo "$name has a Vorbehalt. <br />" ;
689 else if($usersick == 'solo')
692 echo "$name has a Vorbehalt. <br />" ;
696 /* now check which sickness comes first and set the gametype to it */
698 if($gametype == "solo")
705 /* TODO: should we keep statistics of this? */
706 $message = "Hello, \n\n".
707 " the game has been canceled because ".DB_get_name_by_userid($nines).
708 " has five or more nines and nobody is playing solo.\n\n".
709 " To redeal either start a new game or, in case the game was part of a tournament, \n".
710 " go to the last game and use the link at the bottom of the page to redeal.";
712 $userids = DB_get_all_userid_by_gameid($gameid);
713 foreach($userids as $user)
715 $To = DB_get_email_by_userid($user);
716 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
719 /* delete everything from the dB */
722 echo "The game has been canceled because ".DB_get_name_by_userid($nines).
723 " has five or more nines and nobody is playing solo.\n";
728 else if($poverty==1) /* one person has poverty */
730 DB_set_gametype_by_gameid($gameid,"poverty");
731 $gametype = "poverty";
732 $who = DB_get_sickness_by_gameid($gameid);
735 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
736 if($firstsick == "poverty")
737 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
739 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
742 else if($poverty==2) /* two people have poverty */
744 DB_set_gametype_by_gameid($gameid,"dpoverty");
745 $gametype = "dpoverty";
746 $who = DB_get_sickness_by_gameid($gameid);
749 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
750 if($firstsick == "poverty")
752 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
753 if($secondsick == "poverty")
754 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
756 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
759 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
764 DB_set_gametype_by_gameid($gameid,"wedding");
765 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
766 $gametype = "wedding";
771 /* now the gametype is set correctly (shouldn't matter that this is calculated for every user)
772 * output what kind of game we have */
775 foreach($userids as $user)
777 /* userids are sorted by position...
778 * so output whatever the first one has, then whatever the next one has
779 * stop when the sickness is the same as the gametype
782 $name = DB_get_name_by_userid($user);
783 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
786 echo "$name has $usersick. <br />"; /*TODO: perhaps save this in a string and store in Game? */
788 if($usersick=="poverty")
790 if($usersick == "wedding" && $gametype =="wedding")
792 if($usersick == "poverty" && $gametype =="poverty")
794 if($usersick == "poverty" && $gametype =="dpoverty" && $poverty==2)
796 if($usersick == "solo" && $gametype =="solo")
800 /* output Schweinchen in case the rules need it */
801 if( $gametype != "solo")
802 if($GAME["schweinchen"] && $RULES["schweinchen"]=="both" )
803 echo DB_get_name_by_hash($GAME["schweinchen-who"])." has Schweinchen. <br />";
807 /* finished the setup, set re/contra parties if possible, go to next stage unless there is a case of poverty*/
811 /* are we the solo player? set us to re, else set us to contra */
812 $pos = DB_get_pos_by_hash($me);
813 if($pos == $startplayer)
814 DB_set_party_by_hash($me,"re");
816 DB_set_party_by_hash($me,"contra");
817 DB_set_hand_status_by_hash($me,'play');
821 /* set person with the wedding to re, do the rest during the game */
822 $usersick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
823 if($usersick == "wedding")
824 DB_set_party_by_hash($me,"re");
826 DB_set_party_by_hash($me,"contra");
828 echo "Whoever will make the first trick will be on the re team. <br />\n";
829 echo " Ok, the game can start now, please finish <a href=\"$INDEX?me=$me\">the setup</a>.<br />";
830 DB_set_hand_status_by_hash($me,'play');
834 $hand = DB_get_all_hand($me);
836 if(in_array('3',$hand)||in_array('4',$hand))
837 DB_set_party_by_hash($me,"re");
839 DB_set_party_by_hash($me,"contra");
840 DB_set_hand_status_by_hash($me,'play');
844 /* check if poverty resolved (e.g. DB.Game who set to NULL)
845 * yes? =>trump was taken, start game; break;
847 $who = DB_get_sickness_by_gameid($gameid);
849 { /* trump has been taken */
850 DB_set_hand_status_by_hash($me,'play');
854 if($who>9) /*= two people still have trump on the table*/
859 /* check if we are being asked now
860 * no? display wait message, e.g. player X is asked at the moment
862 $usersick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
863 if(myisset("trump") && $_REQUEST["trump"]=="no" && ($who==$mypos || $who==$mypos*10))
865 /* user doesn't want to take trump */
866 /* set next player who needs to be asked */
867 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
868 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
870 if($firstsick=="poverty")
872 if($secondsick=="poverty")
873 DB_set_sickness_by_gameid($gameid,$who+$add*3);
875 DB_set_sickness_by_gameid($gameid,$who+$add*2);
878 DB_set_sickness_by_gameid($gameid,$who+$add);
880 /* email next player */
881 $who = DB_get_sickness_by_gameid($gameid);
882 if($who>9) $who = $who/10;
886 $To = DB_get_email_by_pos_and_gameid($who,$gameid);
887 $userhash = DB_get_hash_from_game_and_pos($gameid,$who);
888 $userid = DB_get_userid_by_email($To);
889 DB_set_player_by_gameid($gameid,$userid);
891 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
892 " ".$HOST.$INDEX."?me=".$userhash."\n\n" ;
893 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
896 /* this user is done */
897 DB_set_hand_status_by_hash($me,'play');
900 else if(myisset("trump") && !myisset("exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
902 /* user wants to take trump */
903 $trump = $_REQUEST["trump"];
905 /* get hand id for user $trump */
906 $userhand = DB_get_handid_by_gameid_and_userid($gameid,$trump);
907 /* copy trump from player A to B */
908 $result = mysql_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
910 /* add hidden button with trump in it to get to the next point */
911 echo "</div><div class=\"poverty\">\n";
912 echo " <input type=\"hidden\" name=\"exchange\" value=\"-1\" />\n";
913 echo " <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
914 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select cards to give back\" />\n";
915 echo "</div><div>\n";
917 else if(myisset("trump","exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
919 $trump = $_REQUEST["trump"];
920 $exchange = $_REQUEST["exchange"];
921 $userhand = DB_get_handid_by_gameid_and_userid($gameid,$trump);
923 /* if exchange is set to a value>0, exchange that card back to user $trump */
926 $result = mysql_query("UPDATE Hand_Card SET hand_id='$userhand'".
927 " WHERE hand_id='$myhand' AND card_id='$exchange'" );
930 /* if number of cards == 12, set status to play for both users */
931 $result = mysql_query("SELECT COUNT(*) FROM Hand_Card WHERE hand_id='$myhand'" );
932 $r = mysql_fetch_array($result,MYSQL_NUM);
935 myerror("error in poverty");
940 if($gametype=="poverty" || $who<9)
942 DB_set_sickness_by_gameid($gameid,-1); /* done with poverty */
944 else /* reduce poverty count by one, that is go to single digits $who */
949 /* whom to ask next */
950 $firstsick = DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
951 $secondsick = DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
953 if($firstsick!="poverty")
954 DB_set_sickness_by_gameid($gameid,$who+$add);
957 if($secondsick!="poverty")
958 DB_set_sickness_by_gameid($gameid,$who+$add*2);
960 DB_set_sickness_by_gameid($gameid,$who+$add*3);
963 /* email next player */
964 $who = DB_get_sickness_by_gameid($gameid);
967 $To = DB_get_email_by_pos_and_gameid($who,$gameid);
968 $userhash = DB_get_hash_from_game_and_pos($gameid,$who);
969 $userid = DB_get_userid_by_email($To);
970 DB_set_player_by_gameid($gameid,$userid);
972 $message = "Someone has poverty, it's your turn to decide, ".
973 "if you want to take the trump. Please visit:".
974 " ".$HOST.$INDEX."?me=".$userhash."\n\n" ;
975 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
979 /* this user is done */
980 DB_set_hand_status_by_hash($me,'play');
981 /* and so is his partner */
982 $hash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
983 DB_set_hand_status_by_hash($hash,'play');
985 /* set party to re, unless we had dpoverty, in that case check if we need to set re/contra*/
987 foreach($userids as $user)
989 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
990 $party = DB_get_party_by_hash($userhash);
996 DB_set_party_by_hash($me,"contra");
997 DB_set_party_by_hash($hash,"contra");
1001 foreach($userids as $user)
1003 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1004 if($userhash==$hash||$userhash==$me)
1005 DB_set_party_by_hash($userhash,"re");
1007 DB_set_party_by_hash($userhash,"contra");
1016 /* else show all trump, have lowest card pre-selected, have hidden setting for */
1017 echo "</div><div class=\"poverty\"> you need to get rid of a few cards</div>\n";
1019 set_gametype($gametype); /* this sets the $CARDS variable */
1020 $mycards = DB_get_hand($me);
1021 $mycards = mysort($mycards,$gametype);
1024 echo "<div class=\"mycards\">Your cards are: <br />\n";
1025 foreach($mycards as $card)
1026 display_link_card($card,$PREF["cardset"],$type);
1027 echo " <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
1028 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select one card to give back\" />\n";
1029 echo "</div><div>\n";
1032 else if($who == $mypos || $who == $mypos*10)
1034 echo "</div><div class=\"poverty\">\n";
1035 foreach($userids as $user)
1037 $name = DB_get_name_by_userid($user);
1038 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1040 if($usersick=="poverty")
1042 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1043 $cards = DB_get_hand($hash);
1044 $nrtrump = count_trump($cards);
1047 echo "Player $name has $nrtrump trump. Do you want to take them?".
1048 "<a href=\"index.php?me=$me&trump=$user\">yes</a> <br />\n";
1051 echo "<a href=\"index.php?me=$me&trump=no\">No,way I take those trump...</a> <br />\n";
1052 echo "</div><div>\n";
1054 echo "Your cards are: <br />\n";
1055 $mycards = DB_get_hand($me);
1057 echo "<p class=\"mycards\">Your cards are: <br />\n";
1058 foreach($mycards as $card)
1059 display_card($card,$PREF["cardset"]);
1064 $mysick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
1065 if($mysick=="poverty")
1066 echo "The others are asked if they want to take your trump, you have to wait (you'll get an email).";
1068 echo "it's not your turn yet to decide if you want to take the trump or not.";
1071 /* check if no one wanted to take trump, in that case the gamesickness would be set to 5 or 50 */
1072 $who = DB_get_sickness_by_gameid($gameid);
1073 if($who==5 || $who==50)
1075 $message = "Hello, \n\n".
1076 "Game ".DB_format_gameid($gameid)." has been canceled since nobody wanted to take the trump.\n";
1078 $userids = DB_get_all_userid_by_gameid($gameid);
1079 foreach($userids as $user)
1081 $To = DB_get_email_by_userid($user);
1082 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled (poverty not resolved)",$message);
1085 /* delete everything from the dB */
1086 DB_cancel_game($me);
1088 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been canceled.<br /><br /></p>";
1094 /* check if all players are ready to play */
1096 foreach($userids as $user)
1097 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='play')
1100 DB_set_player_by_gameid($gameid,$user);
1105 /* only set this after all poverty, etc. are handled*/
1106 DB_set_game_status_by_gameid($gameid,'play');
1108 /* email startplayer */
1109 $startplayer = DB_get_startplayer_by_gameid($gameid);
1110 $email = DB_get_email_by_pos_and_gameid($startplayer,$gameid);
1111 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
1112 $who = DB_get_userid_by_email($email);
1113 DB_set_player_by_gameid($gameid,$who);
1115 if($hash!=$me && DB_get_email_pref_by_hash($hash)!="emailaddict")
1117 /* email startplayer) */
1118 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
1119 "Use this link to play a card: ".$HOST.$INDEX."?me=".$hash."\n\n" ;
1120 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
1123 echo " Please, <a href=\"$INDEX?me=$me\">start</a> the game.<br />";
1132 /* both entries here, so that the tricks are visible for both.
1133 * in case of 'play' there is a break later that skips the last part
1136 /* figure out what kind of game we are playing,
1137 * set the global variables $CARDS["trump"],$CARDS["diamonds"],$CARDS["hearts"],
1138 * $CARDS["clubs"],$CARDS["spades"],$CARDS["foxes"]
1142 $gametype = DB_get_gametype_by_gameid($gameid);
1144 if($gametype=="solo")
1146 $gametype = DB_get_solo_by_gameid($gameid);
1147 $GT = $gametype." ".$GT;
1150 $gametype = "normal";
1152 set_gametype($gametype); /* this sets the $CARDS variable */
1154 /* get some infos about the game */
1155 $gamestatus = DB_get_game_status_by_gameid($gameid);
1157 /* has the game started? No, then just wait here...*/
1158 if($gamestatus == 'pre')
1160 echo "<p class=\"message\"> You finished the setup, but not everyone else finished it... ".
1161 "so you need to wait for the others. Just wait for the an email... </p>";
1162 break; /* not sure this works... the idea is that you can
1163 * only play a card after everyone is ready to play */
1166 /* get time from the last action of the game */
1167 $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
1168 $r = mysql_fetch_array($result,MYSQL_NUM);
1169 $gameend = time() - strtotime($r[0]);
1171 /* handel comments in case player didn't play a card, allow comments a week after the end of the game */
1172 if( (!myisset("card") && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
1173 if(myisset("comment"))
1175 $comment = $_REQUEST["comment"];
1176 $playid = DB_get_current_playid($gameid);
1179 DB_insert_comment($comment,$playid,$myid);
1182 /* get everything relevant to display the tricks */
1183 $result = mysql_query("SELECT Hand_Card.card_id as card,".
1184 " Hand.position as position,".
1185 " Play.sequence as sequence, ".
1187 " GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
1188 " SEPARATOR '\n' ), ".
1189 " Play.create_date, ".
1192 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1193 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1194 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1195 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1196 "LEFT JOIN User On User.id=Comment.user_id ".
1197 "WHERE Trick.game_id='".$gameid."' ".
1198 "GROUP BY Trick.id, sequence ".
1199 "ORDER BY Trick.id, sequence ASC");
1201 $lasttrick = DB_get_max_trickid($gameid);
1203 $play = array(); /* needed to calculate winner later */
1205 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1206 $firstcard = ""; /* first card in a trick */
1208 echo "\n<ul class=\"tricks\">\n";
1209 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
1211 /* output vorbehalte */
1212 $mygametype = DB_get_gametype_by_gameid($gameid);
1213 if($mygametype != "normal") /* only show when needed */
1215 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
1216 " <div class=\"trick\" id=\"trick0\">\n";
1218 for($mypos=1;$mypos<5;$mypos++)
1220 $usersick = DB_get_sickness_by_pos_and_gameid($mypos,$gameid);
1223 echo " <div class=\"vorbehalt".($mypos-1)."\"> Vorbehalt <br />";
1225 echo " $usersick <br />";
1228 if($mygametype == $usersick)
1232 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1236 while($r = mysql_fetch_array($result,MYSQL_NUM))
1244 /* check if first schweinchen has been played */
1245 if( $GAME["schweinchen"] && ($r[0] == 19 || $r[0] == 20) )
1246 $GAME["schweinchen"]++;
1248 /* save card to be able to find the winner of the trick later */
1249 $play[$seq] = array("card"=>$r[0],"pos"=>$pos);
1253 /* first card in a trick, output some html */
1254 if($trick!=$lasttrick)
1256 /* start of an old trick? */
1257 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1258 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1259 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1261 else if($trick==$lasttrick)
1263 /* start of a last trick? */
1264 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1265 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1266 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1269 /* remember first card, so that we are able to check, what cards can be played */
1274 echo " <div class=\"card".($pos-1)."\">\n";
1276 /* display comments */
1278 echo " <span class=\"comment\">".$comment."</span>\n";
1281 display_card($r[0],$PREF["cardset"]);
1283 echo " </div>\n"; /* end div card */
1289 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1293 /* whos turn is it? */
1296 $winner = get_winner($play,$gametype); /* returns the position */
1298 $firstcard = ""; /* new trick, no first card */
1303 if($next==5) $next = 1;
1306 /* my turn?, display cards as links, ask for comments*/
1307 if(DB_get_pos_by_hash($me) == $next)
1312 /* do we want to play a card? */
1313 if(myisset("card") && $myturn)
1315 $card = $_REQUEST["card"];
1316 $handid = DB_get_handid_by_hash($me);
1318 /* check if we have card and that we haven't played it yet*/
1319 /* set played in hand_card to true where hand_id and card_id*/
1320 $result = mysql_query("SELECT id FROM Hand_Card WHERE played='false' and ".
1321 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1322 $r = mysql_fetch_array($result,MYSQL_NUM);
1323 $handcardid = $r[0];
1325 if($handcardid) /* everything ok, play card */
1327 /* update Game timestamp */
1328 DB_update_game_timestamp($gameid);
1330 /* check if a call was made, must do this before we set the card status to played */
1331 if(myisset("call120") && $_REQUEST["call120"] == "yes" && can_call(120,$me))
1332 $result = mysql_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
1333 if(myisset("call90") && $_REQUEST["call90"] == "yes" && can_call(90,$me))
1334 $result = mysql_query("UPDATE Hand SET point_call='90' WHERE hash='$me' ");
1335 if(myisset("call60") && $_REQUEST["call60"] == "yes" && can_call(60,$me))
1336 $result = mysql_query("UPDATE Hand SET point_call='60' WHERE hash='$me' ");
1337 if(myisset("call30") && $_REQUEST["call30"] == "yes" && can_call(30,$me))
1338 $result = mysql_query("UPDATE Hand SET point_call='30' WHERE hash='$me' ");
1339 if(myisset("call0") && $_REQUEST["call0"] == "yes" && can_call(0,$me))
1340 $result = mysql_query("UPDATE Hand SET point_call='0' WHERE hash='$me' ");
1342 /* mark card as played */
1343 mysql_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1344 DB_quote_smart($card));
1346 /* get trick id or start new trick */
1347 $a = DB_get_current_trickid($gameid);
1352 $playid = DB_play_card($trickid,$handcardid,$sequence);
1354 /* check for schweinchen */
1355 if($GAME["schweinchen"] && ($card == 19 || $card == 20) )
1357 $GAME["schweinchen"]++; // count how many have been played including this one
1358 if($GAME["schweinchen"]==3 && $RULES["schweinchen"]=="second" )
1359 DB_insert_comment("Schweinchen! ",$playid,$myid);
1360 if($RULES["schweinchen"]=="both" )
1361 DB_insert_comment("Schweinchen! ",$playid,$myid);
1363 echo "schweinchen = ".$GAME["schweinchen"]." ---<br />";
1366 /* if sequence == 4 check who one in case of wedding */
1367 if($sequence == 4 && $GT == "wedding")
1369 /* is wedding resolve */
1370 $resolved = DB_get_sickness_by_gameid($gameid);
1373 /* who has wedding */
1374 $userids = DB_get_all_userid_by_gameid($gameid);
1375 foreach($userids as $user)
1377 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1378 if($usersick == "wedding")
1381 /* who won the trick */
1382 $play = DB_get_cards_by_trick($trickid);
1383 $winner = get_winner($play,$gametype); /* returns the position */
1384 $winnerid = DB_get_userid_by_gameid_and_position($gameid,$winner);
1385 /* is tricknr <=3 */
1386 if($tricknr <=3 && $winnerid!=$whosick)
1388 /* set resolved at tricknr*/
1389 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1391 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1392 DB_set_party_by_hash($whash,"re");
1394 if($tricknr == 3 && $winnerid==$whosick)
1396 /* set resolved at tricknr*/
1397 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1402 /* if sequence == 4, set winner of the trick, count points and set the next player */
1405 $play = DB_get_cards_by_trick($trickid);
1406 $winner = get_winner($play,$gametype); /* returns the position */
1408 /* check if someone caught a fox */
1409 if(DB_get_gametype_by_gameid($gameid)!="solo")
1410 foreach($play as $played)
1412 if ( $played['card']==19 || $played['card']==20 )
1413 if ($played['pos']!= $winner )
1415 /* possible caught a fox, check party */
1416 $uid1 = DB_get_userid_by_gameid_and_position($gameid,$winner);
1417 $uid2 = DB_get_userid_by_gameid_and_position($gameid,$played['pos']);
1419 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1420 $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1422 if($party1 != $party2)
1423 mysql_query("INSERT INTO Score".
1424 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1427 /* check for karlchen (jack of clubs in the last trick)*/
1428 if(DB_get_gametype_by_gameid($gameid)!="solo" && $tricknr == 12)
1429 foreach($play as $played)
1430 if ( $played['card']==11 || $played['card']==12 )
1431 if ($played['pos'] == $winner )
1433 /* possible caught a fox, check party */
1434 $uid1 = DB_get_userid_by_gameid_and_position($gameid,$winner);
1435 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1437 mysql_query("INSERT INTO Score".
1438 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1440 /* check for doppelopf (>40 points)*/
1442 foreach($play as $played)
1444 $points += DB_get_card_value_by_cardid($played['card']);
1448 $uid1 = DB_get_userid_by_gameid_and_position($gameid,$winner);
1449 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1451 mysql_query("INSERT INTO Score".
1452 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
1456 mysql_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1458 echo "ERROR during scoring";
1461 echo "DEBUG: position $winner won the trick <br />";
1463 /* who is the next player? */
1468 $next = DB_get_pos_by_hash($me)+1;
1470 if($next==5) $next=1;
1472 /* check for coment */
1473 if(myisset("comment"))
1475 $comment = $_REQUEST["comment"];
1477 DB_insert_comment($comment,$playid,$myid);
1480 /* display played card */
1481 $pos = DB_get_pos_by_hash($me);
1484 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1485 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1486 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1489 echo " <div class=\"card".($pos-1)."\">\n ";
1491 /* display comments */
1492 display_card($card,$PREF["cardset"]);
1494 echo "\n <span class=\"comment\"> ".$comment."</span>\n";
1497 /*check if we still have cards left, else set status to gameover */
1498 if(sizeof(DB_get_hand($me))==0)
1500 DB_set_hand_status_by_hash($me,'gameover');
1501 $mystatus = 'gameover';
1504 /* if all players are done, set game status to game over,
1505 * get the points of the last trick and send out an email
1508 $userids = DB_get_all_userid_by_gameid($gameid);
1511 foreach($userids as $user)
1512 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1516 DB_set_game_status_by_gameid($gameid,"gameover");
1518 /* email next player, if game is still running */
1519 if(DB_get_game_status_by_gameid($gameid)=='play')
1521 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1522 $email = DB_get_email_by_hash($next_hash);
1523 $who = DB_get_userid_by_email($email);
1524 DB_set_player_by_gameid($gameid,$who);
1526 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1527 "It's your turn now.\n".
1528 "Use this link to play a card: ".$HOST.$INDEX."?me=".$next_hash."\n\n" ;
1529 if( DB_get_email_pref_by_uid($who)!="emailaddict" )
1530 mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
1532 else /* send out final email */
1534 /* individual score */
1535 $result = mysql_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1536 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1537 " LEFT JOIN User ON User.id=Hand.user_id".
1538 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1539 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1540 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1541 " WHERE Hand.game_id='$gameid'".
1542 " GROUP BY User.fullname" );
1543 $message = "The game is over. Thanks for playing :)\n";
1544 $message .= "Final score:\n";
1545 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1546 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1548 $result = mysql_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1549 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1550 " LEFT JOIN User ON User.id=Hand.user_id".
1551 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1552 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1553 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1554 " WHERE Hand.game_id='$gameid'".
1555 " GROUP BY Hand.party" );
1556 $message .= "\nTotals:\n";
1559 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1561 $message .= " ".$r[0]." ".$r[1]."\n";
1564 else if($r[0] == "contra")
1569 * save score in database
1573 /* get calls from re/contra */
1575 $call_contra = NULL;
1576 foreach($userids as $user)
1578 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1579 $call = DB_get_call_by_hash($hash);
1580 $party = DB_get_party_by_hash($hash);
1584 $call = (int) $call;
1590 else if( $call < $call_re)
1593 else if($party=="contra")
1595 if($call_contra==NULL)
1596 $call_contra = $call;
1597 else if( $call < $call_re)
1598 $call_contra = $call;
1603 /* figure out who one */
1604 $winning_party = NULL;
1606 if($call_re == NULL && $call_contra==NULL)
1608 $winning_party="re";
1610 $winning_party="contra";
1615 $offset = 120 - $call_re;
1617 $offset--; /* since we use a > in the next equation */
1619 if($re > 120+$offset)
1620 $winning_party="re";
1621 else if ( $call_contra == NULL )
1622 $winning_party="contra";
1627 $offset = 120 - $call_contra;
1628 if($call_contra == 0)
1629 $offset--; /* since we use a > in the next equation */
1631 if($contra > 120+$offset)
1632 $winning_party="contra";
1633 else if ( $call_contra == NULL )
1634 $winning_party="re";
1638 /* one point for each call of the other party in case the other party didn't win
1639 * and one point each in case the party made more than points than one of the calls
1641 if($winning_party!="contra" && $call_contra!=NULL)
1643 for( $p=$call_contra;$p<=120; $p+=30 )
1645 mysql_query("INSERT INTO Score".
1646 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
1649 for( $p=$call_contra; $p<120; $p+=30)
1652 mysql_query("INSERT INTO Score".
1653 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'made$p')");
1656 if($winning_party!="re" and $call_re!=NULL)
1658 for( $p=$call_re;$p<=120; $p+=30 )
1660 mysql_query("INSERT INTO Score".
1661 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'against$p')");
1664 for( $p=$call_re; $p<120; $p+=30)
1667 mysql_query("INSERT INTO Score".
1668 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
1672 /* point in case contra won */
1673 if($winning_party=="contra")
1675 mysql_query("INSERT INTO Score".
1676 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1679 /* one point each for winning and each 30 points + calls */
1680 if($winning_party=="re")
1682 foreach(array(120,150,180,210,240) as $p)
1685 if($p==240 || $call_contra!=NULL)
1689 mysql_query("INSERT INTO Score".
1690 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
1692 /* re called something and won */
1693 foreach(array(0,30,60,90,120) as $p)
1695 if($call_re!=NULL && $call_re<$p+1)
1696 mysql_query("INSERT INTO Score".
1697 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
1700 else if( $winning_party=="contra")
1702 foreach(array(120,150,180,210,240) as $p)
1705 if($p==240 || $call_re!=NULL)
1708 if($contra>$p-$offset)
1709 mysql_query("INSERT INTO Score".
1710 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
1712 /* re called something and won */
1713 foreach(array(0,30,60,90,120) as $p)
1715 if($call_contra!=NULL && $call_contra<$p+1)
1716 mysql_query("INSERT INTO Score".
1717 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
1722 /* add score points to email */
1725 $message .= " Points Re: \n";
1726 $queryresult = mysql_query("SELECT score FROM Score ".
1727 " WHERE game_id=$gameid AND party='re'".
1729 while($r = mysql_fetch_array($queryresult,MYSQL_NUM) )
1731 $message .= " ".$r[0]."\n";
1734 $message .= " Points Contra: \n";
1735 $queryresult = mysql_query("SELECT score FROM Score ".
1736 " WHERE game_id=$gameid AND party='contra'".
1738 while($r = mysql_fetch_array($queryresult,MYSQL_NUM) )
1740 $message .= " ".$r[0]."\n";
1743 $message .= " Total Points (from the Re point of view): $Tpoint\n";
1746 /* send out final email */
1749 foreach($userids as $user)
1750 $all[] = DB_get_email_by_userid($user);
1751 $To = implode(",",$all);
1753 $help = "\n\n (you can use reply all on this email to reach all the players.)\n";
1754 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 1(2)",$message.$help);
1756 foreach($userids as $user)
1758 $To = DB_get_email_by_userid($user);
1759 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1761 $link = "Use this link to have a look at game ".DB_format_gameid($gameid).": ".
1762 $HOST.$INDEX."?me=".$hash."\n\n" ;
1763 if( DB_get_email_pref_by_uid($user) != "emailaddict" )
1764 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 2(2)",$link);
1770 echo "can't find that card?! <br />\n";
1773 else if(myisset("card") && !$myturn )
1775 echo "please wait until it's your turn! <br />\n";
1778 if($seq!=4 && $trickNR>1)
1779 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1781 /* display points in case game is over */
1782 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1784 echo " <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1785 " <div class=\"trick\" id=\"trick13\">\n";
1786 /* add pic for re/contra
1787 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1789 $result = mysql_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1790 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1791 " LEFT JOIN User ON User.id=Hand.user_id".
1792 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1793 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1794 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1795 " WHERE Hand.game_id='$gameid'".
1796 " GROUP BY User.fullname" );
1797 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1798 echo " <div class=\"card".($r[3]-1)."\">\n".
1799 " <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
1802 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1806 echo "</ul>\n"; /* end ul tricks*/
1808 $mycards = DB_get_hand($me);
1809 $mycards = mysort($mycards,$gametype);
1810 echo "<div class=\"mycards\">\n";
1812 if($myturn && !myisset("card") && $mystatus=='play' )
1814 echo "Hello ".$myname.", it's your turn! <br />\n";
1815 echo "Your cards are: <br />\n";
1817 /* do we have to follow suite? */
1819 if(have_suit($mycards,$firstcard))
1822 foreach($mycards as $card)
1824 if($followsuit && !same_type($card,$firstcard))
1825 display_card($card,$PREF["cardset"]);
1827 display_link_card($card,$PREF["cardset"]);
1830 else if($mystatus=='play' )
1832 echo "Your cards are: <br />\n";
1833 foreach($mycards as $card)
1834 display_card($card,$PREF["cardset"]);
1836 else if($mystatus=='gameover')
1838 $oldcards = DB_get_all_hand($me);
1839 $oldcards = mysort($oldcards,$gametype);
1840 echo "Your cards were: <br />\n";
1841 foreach($oldcards as $card)
1842 display_card($card,$PREF["cardset"]);
1844 $userids = DB_get_all_userid_by_gameid($gameid);
1845 foreach($userids as $user)
1847 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1853 $name = DB_get_name_by_userid($user);
1854 $oldcards = DB_get_all_hand($userhash);
1855 $oldcards = mysort($oldcards,$gametype);
1856 echo "$name's cards were: <br />\n";
1857 foreach($oldcards as $card)
1858 display_card($card,$PREF["cardset"]);
1864 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1865 if($mystatus=='play')
1868 /* the following happens only when the gamestatus is 'gameover' */
1869 /* check if game is over, display results */
1870 if(DB_get_game_status_by_gameid($gameid)=='play')
1872 echo "The game is over for you.. other people still need to play though";
1876 $result = mysql_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1877 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1878 " LEFT JOIN User ON User.id=Hand.user_id".
1879 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1880 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1881 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1882 " WHERE Hand.game_id='$gameid'".
1883 " GROUP BY Hand.party" );
1884 echo "<div class=\"total\"> Totals:<br />\n";
1885 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1886 echo " ".$r[0]." ".$r[1]."<br />\n";
1888 $queryresult = mysql_query("SELECT timediff(mod_date,create_date) ".
1889 " FROM Game WHERE id='$gameid'");
1890 $r = mysql_fetch_array($queryresult,MYSQL_NUM);
1891 echo "<p>This game took ".$r[0]." hours.</p>";
1893 echo "<div class=\"re\">\n Points Re: <br />\n";
1894 $queryresult = mysql_query("SELECT score FROM Score ".
1895 " WHERE game_id=$gameid AND party='re'".
1897 while($r = mysql_fetch_array($queryresult,MYSQL_NUM) )
1898 echo " ".$r[0]."<br />\n";
1901 echo "<div class=\"contra\">\n Points Contra: <br />\n";
1902 $queryresult = mysql_query("SELECT score FROM Score ".
1903 " WHERE game_id=$gameid AND party='contra'".
1905 while($r = mysql_fetch_array($queryresult,MYSQL_NUM) )
1906 echo " ".$r[0]."<br />\n";
1915 myerror("error in testing the status");
1917 /* output left menu */
1918 display_user_menu();
1920 /* output right menu */
1922 /* display rule set for this game */
1923 echo "<div class=\"gameinfo\">\n";
1925 if($gamestatus != 'pre')
1926 echo " Gametype: $GT <br />\n";
1928 echo "Rules: <br />\n";
1929 echo "10ofhearts : ".$RULES["dullen"] ."<br />\n";
1930 echo "schweinchen: ".$RULES["schweinchen"] ."<br />\n";
1931 echo "call: ".$RULES["call"] ."<br />\n";
1934 if($gamestatus == 'play' )
1935 output_form_calls($me);
1937 /* get time from the last action of the game */
1938 $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
1939 $r = mysql_fetch_array($result,MYSQL_NUM);
1940 $gameend = time() - strtotime($r[0]);
1942 if($gamestatus == 'play' || $gameend < 60*60*24*7)
1944 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1948 echo "<input type=\"submit\" value=\"submit\" />\n";
1951 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1955 $session = DB_get_session_by_gameid($gameid);
1956 $result = mysql_query("SELECT id,create_date FROM Game".
1957 " WHERE session=$session".
1958 " ORDER BY create_date DESC".
1962 $r = mysql_fetch_array($result,MYSQL_NUM);
1964 if(!$session || $gameid==$r[0])
1966 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
1967 $names = DB_get_all_names_by_gameid($gameid);
1968 $type = DB_get_gametype_by_gameid($gameid);
1971 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
1973 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
1977 $session = DB_get_session_by_gameid($gameid);
1978 $score = generate_score_table($session);
1980 // if(size_of($score)>30)
1991 /* user status page */
1992 else if( myisset("email","password") || isset($_SESSION["name"]) )
1994 /* test id and password, should really be done in one step */
1995 if(!isset($_SESSION["name"]))
1997 $email = $_REQUEST["email"];
1998 $password = $_REQUEST["password"];
2002 $name = $_SESSION["name"];
2003 $email = DB_get_email_by_name($name);
2004 $password = DB_get_passwd_by_name($name);
2007 if(myisset("forgot"))
2011 $myid = DB_get_userid_by_email($email);
2017 /* check how many entries in recovery table */
2018 $number = DB_get_number_of_passwords_recovery($myid);
2020 /* if less than N recent ones, add a new one and send out email */
2023 echo "Ok, I send you a new password. <br />";
2025 echo "N.B. You tried this already $number times during the last day and it will only work ".
2026 " 5 times during a day.<br />";
2027 echo "The new password will be valid for one day, make sure you reset it to something else.<br />";
2028 echo "Back to the <a href=\"$INDEX\">main page</a>.";
2030 $TIME = (string) time(); /* to avoid collisions */
2031 $hash = md5("Anewpassword".$email.$TIME);
2032 $newpw = substr($hash,1,8);
2034 $message = "Someone (hopefully you) requested a new password. \n".
2035 "You can use this email and the following password: \n".
2037 "to log into the server. The new password is valid for 24h, so make\n".
2038 "sure you reset your password to something new. Your old password will\n".
2039 " also still be valid until you set a new one\n";
2040 mymail($email,$EmailName."recovery ",$message);
2042 DB_set_recovery_password($myid,md5($newpw));
2046 echo "Sorry you already tried 5 times during the last 24h.<br />".
2047 "You need to use one of those passwords or wait to get a new one.<br />";
2048 echo "Back to the <a href=\"$INDEX\">main page</a>.";
2054 echo "You need to give me an email address! <br />".
2055 "Please try <a href=\"$INDEX\">again</a>.";
2057 echo "Couldn't find a player with this email! <br />".
2058 "Please contact Arun, if you think this is a mistake <br />".
2059 "or else try <a href=\"$INDEX\">again</a>.";
2064 /* verify password and email */
2065 if(strlen($password)!=32)
2066 $password = md5($password);
2069 $myid = DB_get_userid_by_email_and_password($email,$password);
2075 $myname = DB_get_name_by_email($email);
2076 $_SESSION["name"] = $myname;
2081 if(myisset("setpref"))
2083 $setpref=$_REQUEST["setpref"];
2087 case "englishcards":
2088 $result = mysql_query("SELECT * from User_Prefs".
2089 " WHERE user_id='$myid' AND pref_key='cardset'" );
2090 if( mysql_fetch_array($result,MYSQL_NUM))
2091 $result = mysql_query("UPDATE User_Prefs SET value=".DB_quote_smart($setpref).
2092 " WHERE user_id='$myid' AND pref_key='cardset'" );
2094 $result = mysql_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','cardset',".
2095 DB_quote_smart($setpref).")");
2096 echo "Ok, changed you preferences for the cards.\n";
2099 case "emailnonaddict":
2100 $result = mysql_query("SELECT * from User_Prefs".
2101 " WHERE user_id='$myid' AND pref_key='email'" );
2102 if( mysql_fetch_array($result,MYSQL_NUM))
2103 $result = mysql_query("UPDATE User_Prefs SET value=".DB_quote_smart($setpref).
2104 " WHERE user_id='$myid' AND pref_key='email'" );
2106 $result = mysql_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','email',".
2107 DB_quote_smart($setpref).")");
2108 echo "Ok, changed you preferences for sending out emails.\n";
2112 else if(myisset("passwd"))
2114 if( $_REQUEST["passwd"]=="ask" )
2116 /* reset password form*/
2117 output_password_recovery($email,$password);
2119 else if($_REQUEST["passwd"]=="set")
2121 /* reset password */
2124 /* check if old password matches */
2125 $oldpasswd = md5($_REQUEST["password0"]);
2126 if(!( ($password == $oldpasswd) || DB_check_recovery_passwords($oldpasswd,$email) ))
2128 /* check if new passwords are types the same twice */
2129 if($_REQUEST["password1"] != $_REQUEST["password2"] )
2135 echo "The new passwords don't match. <br />";
2138 echo "The old password is not correct. <br />";
2141 echo "Changed the password.<br />";
2142 mysql_query("UPDATE User SET password='".md5($_REQUEST["password1"]).
2143 "' WHERE id=".DB_quote_smart($myid));
2149 else /* output default user page */
2151 /* display links to settings */
2152 output_user_settings();
2154 DB_update_user_timestamp($myid);
2156 display_user_menu();
2158 echo "<div class=\"user\">";
2159 echo "<h4>These are all your games:</h4>\n";
2160 echo "<p>Session: <br />\n";
2161 echo "<span class=\"gamestatuspre\"> p </span> = pre-game phase ";
2162 echo "<span class=\"gamestatusplay\">P </span> = game in progess ";
2163 echo "<span class=\"gamestatusover\">F </span> = game finished <br />";
2167 $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.mod_date,Game.player,Game.status from Hand".
2168 " LEFT JOIN Game ON Game.id=Hand.game_id".
2169 " WHERE user_id='$myid'".
2170 " ORDER BY Game.session,Game.create_date" );
2172 echo "<table>\n <tr><td>\n";
2173 while( $r = mysql_fetch_array($result,MYSQL_NUM))
2175 $game = DB_format_gameid($r[1]);
2176 $gamenr = (int) $game;
2177 if($gamenrold < $gamenr)
2180 echo "</td></tr>\n <tr> <td>$gamenr:</td><td> ";
2182 echo "$gamenr:</td><td> ";
2183 $gamenrold = $gamenr;
2187 echo "\n <span class=\"gamestatuspre\"><a href=\"".$INDEX."?me=".$r[0]."\">p </a></span> ";
2190 else if ($r[4]=='gameover')
2191 echo "\n <span class=\"gamestatusover\"><a href=\"".$INDEX."?me=".$r[0]."\">F </a></span> ";
2194 echo "\n <span class=\"gamestatusplay\"><a href=\"".$INDEX."?me=".$r[0]."\">P </a></span> ";
2196 if($r[4] != 'gameover')
2198 echo "</td><td>\n ";
2202 echo "(it's <strong>your</strong> turn)\n";
2205 $name = DB_get_name_by_userid($r[3]);
2207 if(DB_get_reminder($r[3],$gameid)==0)
2208 if(time()-strtotime($r[2]) > 60*60*24*7)
2210 "<a href=\"$INDEX?remind=1&me=".$r[0]."\">Send a reminder.</a>";
2211 echo "(it's $name's turn)\n";
2214 if(time()-strtotime($r[2]) > 60*60*24*30)
2216 "<a href=\"$INDEX?cancel=1&me=".$r[0]."\">Cancel?</a>".
2217 " (clicking here is final and can't be restored)";
2221 echo "</td></tr>\n</table>\n";
2222 $names = DB_get_all_names();
2223 echo "<h4>Registered players:</h4>\n<p>\n";
2224 echo implode(", ",$names)."\n";
2225 echo "</p>\n</div>";
2230 echo "<div class=\"message\">Sorry email and password don't match. Please <a href=\"$INDEX\">try again</a>. </div>";
2237 /* default login page */
2240 $pre[0]=0;$game[0]=0;$done[0]=0;
2241 $r=mysql_query("SELECT COUNT(id) FROM Game GROUP BY status");
2243 $pre = mysql_fetch_array($r,MYSQL_NUM);
2244 $game = mysql_fetch_array($r,MYSQL_NUM);
2245 $done = mysql_fetch_array($r,MYSQL_NUM);
2248 $r=mysql_query("SELECT AVG(datediff(mod_date,create_date)) FROM Game where status='gameover' ");
2250 $avgage= mysql_fetch_array($r,MYSQL_NUM);
2254 output_home_page($pre[0],$game[0],$done[0],$avgage[0]);