+ foreach($userids as $user)
+ {
+ $To = DB_get_email_by_userid($user);
+ $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
+ if($userhash != $me)
+ {
+ $message = "Everyone finish the questionary in game ".DB_format_gameid($gameid).", ".
+ "please visit this link now to continue: \n".
+ " ".$host."?me=".$userhash."\n\n" ;
+ mymail($To,$EmailName." finished setup in game ".DB_format_gameid($gameid),$message);
+ }
+ };
+ };
+ };
+ };
+ break;
+
+ case 'poverty':
+ /* here we need to check if there is a solo or some other form of sickness.
+ * If so, which one is the most important one
+ * set that one in the Game table
+ * tell people about it.
+ */
+ echo "<div class=\"message\">\n";
+ echo "<p> Checking if someone else selected solo, nines, wedding or poverty.</p>";
+
+ /* check if everyone has reached this stage */
+ $userids = DB_get_all_userid_by_gameid($gameid);
+ $ok = 1;
+ foreach($userids as $user)
+ {
+ $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
+ if($userstat!='poverty' && $userstat!='play')
+ $ok = 0;
+ };
+
+ if(!$ok)
+ {
+ echo "This step can only be handled after everyone finished the last step. ".
+ "Seems like this is not the case, so you need to wait a bit... ".
+ "you will get an email once that is the case, please use the link in ".
+ "that email to continue the game.<br />";
+ }
+ else
+ {
+ echo "Everyone has finished checking their cards, let's see what they said...<br />";
+
+ /* check what kind of game we are playing, in case there are any solos this already
+ *will have the correct information in it */
+ $gametype = DB_get_gametype_by_gameid($gameid);
+ $startplayer = DB_get_startplayer_by_gameid($gameid);
+
+ /* check for different sickness and just output a general info */
+ $nines = 0;
+ $poverty = 0;
+ $wedding = 0;
+ $solo = 0;
+ foreach($userids as $user)
+ {
+ $name = DB_get_name_by_userid($user);
+ $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
+ if($usersick == 'nines')
+ {
+ $nines = $user;
+ echo "$name has a Vorbehalt. <br />";
+ break;
+ }
+ else if($usersick == 'poverty')
+ {
+ $poverty++;
+ echo "$name has a Vorbehalt. <br />";
+ }
+ else if($usersick == 'wedding')
+ {
+ $wedding=$user;
+ echo "$name has a Vorbehalt. <br />" ;
+ }
+ else if($usersick == 'solo')
+ {
+ $solo++;
+ echo "$name has a Vorbehalt. <br />" ;
+ }
+ }
+
+ /* now check which sickness comes first and set the gametype to it */
+
+ if($gametype == "solo")
+ {
+ /* do nothing */
+ }
+ else if($nines)
+ {
+ /* cancel game */
+ /* TODO: should we keep statistics of this? */
+ $message = "Hello, \n\n".
+ " the game has been canceled because ".DB_get_name_by_userid($nines).
+ " has five or more nines and nobody is playing solo.\n\n".
+ " To redeal either start a new game or, in case the game was part of a tournament, \n".
+ " go to the last game and use the link at the bottom of the page to redeal.";
+
+ $userids = DB_get_all_userid_by_gameid($gameid);
+ foreach($userids as $user)
+ {
+ $To = DB_get_email_by_userid($user);
+ mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
+ }
+
+ /* delete everything from the dB */
+ DB_cancel_game($me);
+
+ echo "The game has been canceled because ".DB_get_name_by_userid($nines).
+ " has five or more nines and nobody is playing solo.\n";
+ output_footer();
+ DB_close();
+ exit();
+ }
+ else if($poverty==1) /* one person has poverty */
+ {
+ DB_set_gametype_by_gameid($gameid,"poverty");
+ $gametype = "poverty";
+ $who = DB_get_sickness_by_gameid($gameid);
+ if(!$who)
+ {
+ $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
+ if($firstsick == "poverty")
+ DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
+ else
+ DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
+ }
+ }
+ else if($poverty==2) /* two people have poverty */
+ {
+ DB_set_gametype_by_gameid($gameid,"dpoverty");
+ $gametype = "dpoverty";
+ $who = DB_get_sickness_by_gameid($gameid);
+ if(!$who)
+ {
+ $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
+ if($firstsick == "poverty")
+ {
+ $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
+ if($secondsick == "poverty")
+ DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
+ else
+ DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
+ }
+ else
+ DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
+ }
+ }
+ else if($wedding> 0)
+ {
+ DB_set_gametype_by_gameid($gameid,"wedding");
+ DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
+ $gametype = "wedding";
+ };
+
+ echo "<br />\n";
+
+ /* now the gametype is set correctly (shouldn't matter that this is calculated for every user)
+ * output what kind of game we have */
+
+ $poverty = 0;
+ foreach($userids as $user)
+ {
+ /* userids are sorted by position...
+ * so output whatever the first one has, then whatever the next one has
+ * stop when the sickness is the same as the gametype
+ */
+
+ $name = DB_get_name_by_userid($user);
+ $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
+
+ if($usersick)
+ echo "$name has $usersick. <br />"; /*TODO: perhaps save this in a string and store in Game? */
+
+ if($usersick=="poverty")
+ $poverty++;
+ if($usersick == "wedding" && $gametype =="wedding")
+ break;
+ if($usersick == "poverty" && $gametype =="poverty")
+ break;
+ if($usersick == "poverty" && $gametype =="dpoverty" && $poverty==2)
+ break;
+ if($usersick == "solo" && $gametype =="solo")
+ break;
+ };
+
+ /* output Schweinchen in case the rules need it */
+ if( $gametype != "solo")
+ if($GAME["schweinchen"] && $RULES["schweinchen"]=="both" )
+ echo DB_get_name_by_hash($GAME["schweinchen-who"])." has Schweinchen. <br />";
+
+ echo "<br />\n";
+
+ /* finished the setup, set re/contra parties if possible, go to next stage unless there is a case of poverty*/
+ switch($gametype)
+ {
+ case "solo":
+ /* are we the solo player? set us to re, else set us to contra */
+ $pos = DB_get_pos_by_hash($me);
+ if($pos == $startplayer)
+ DB_set_party_by_hash($me,"re");
+ else
+ DB_set_party_by_hash($me,"contra");
+ DB_set_hand_status_by_hash($me,'play');
+ break;
+
+ case "wedding":
+ /* set person with the wedding to re, do the rest during the game */
+ $usersick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
+ if($usersick == "wedding")
+ DB_set_party_by_hash($me,"re");
+ else
+ DB_set_party_by_hash($me,"contra");
+
+ echo "Whoever will make the first trick will be on the re team. <br />\n";
+ echo " Ok, the game can start now, please finish <a href=\"$host?me=$me\">the setup</a>.<br />";
+ DB_set_hand_status_by_hash($me,'play');
+ break;
+
+ case "normal":
+ $hand = DB_get_all_hand($me);
+
+ if(in_array('3',$hand)||in_array('4',$hand))
+ DB_set_party_by_hash($me,"re");
+ else
+ DB_set_party_by_hash($me,"contra");
+ DB_set_hand_status_by_hash($me,'play');
+ break;
+ case "poverty":
+ case "dpoverty":
+ /* check if poverty resolved (e.g. DB.Game who set to NULL)
+ * yes? =>trump was taken, start game; break;
+ */
+ $who = DB_get_sickness_by_gameid($gameid);
+ if($who<0)
+ { /* trump has been taken */
+ DB_set_hand_status_by_hash($me,'play');
+ break;
+ };
+
+ if($who>9) /*= two people still have trump on the table*/
+ $add = 10;
+ else
+ $add = 1;
+
+ /* check if we are being asked now
+ * no? display wait message, e.g. player X is asked at the moment
+ */
+ $usersick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
+ if(myisset("trump") && $_REQUEST["trump"]=="no" && ($who==$mypos || $who==$mypos*10))
+ {
+ /* user doesn't want to take trump */
+ /* set next player who needs to be asked */
+ $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
+ $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
+
+ if($firstsick=="poverty")
+ {
+ if($secondsick=="poverty")
+ DB_set_sickness_by_gameid($gameid,$who+$add*3);
+ else
+ DB_set_sickness_by_gameid($gameid,$who+$add*2);
+ }
+ else
+ DB_set_sickness_by_gameid($gameid,$who+$add);
+
+ /* email next player */
+ $who = DB_get_sickness_by_gameid($gameid);
+ if($who>9) $who = $who/10;
+
+ if($who<=4)
+ {
+ $To = DB_get_email_by_pos_and_gameid($who,$gameid);
+ $userhash = DB_get_hash_from_game_and_pos($gameid,$who);
+ DB_set_player_by_gameid($gameid,$who);
+
+ $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
+ " ".$host."?me=".$userhash."\n\n" ;
+ mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
+ }
+
+ /* this user is done */
+ DB_set_hand_status_by_hash($me,'play');
+ break;
+ }
+ else if(myisset("trump") && !myisset("exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
+ {
+ /* user wants to take trump */
+ $trump = $_REQUEST["trump"];
+
+ /* get hand id for user $trump */
+ $userhand = DB_get_handid_by_gameid_and_userid($gameid,$trump);
+ /* copy trump from player A to B */
+ $result = mysql_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
+
+ /* add hidden button with trump in it to get to the next point */
+ echo "</div><div class=\"poverty\">\n";
+ echo " <input type=\"hidden\" name=\"exchange\" value=\"-1\" />\n";
+ echo " <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
+ echo " <input type=\"submit\" class=\"submitbutton\" value=\"select cards to give back\" />\n";
+ echo "</div><div>\n";
+ }
+ else if(myisset("trump","exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
+ {
+ $trump = $_REQUEST["trump"];
+ $exchange = $_REQUEST["exchange"];
+ $userhand = DB_get_handid_by_gameid_and_userid($gameid,$trump);
+
+ /* if exchange is set to a value>0, exchange that card back to user $trump */
+ if($exchange >0)
+ {
+ $result = mysql_query("UPDATE Hand_Card SET hand_id='$userhand'".
+ " WHERE hand_id='$myhand' AND card_id='$exchange'" );
+ };
+
+ /* if number of cards == 12, set status to play for both users */
+ $result = mysql_query("SELECT COUNT(*) FROM Hand_Card WHERE hand_id='$myhand'" );
+ $r = mysql_fetch_array($result,MYSQL_NUM);
+ if(!$r)
+ {
+ myerror("error in poverty");
+ die();
+ };
+ if($r[0]==12)
+ {
+ if($gametype=="poverty" || $who<9)
+ {
+ DB_set_sickness_by_gameid($gameid,-1); /* done with poverty */
+ }
+ else /* reduce poverty count by one, that is go to single digits $who */
+ {
+ $add = 1;
+ $who = $who/10;
+
+ /* whom to ask next */
+ $firstsick = DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
+ $secondsick = DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
+
+ if($firstsick!="poverty")
+ DB_set_sickness_by_gameid($gameid,$who+$add);
+ else
+ {
+ if($secondsick!="poverty")
+ DB_set_sickness_by_gameid($gameid,$who+$add*2);
+ else
+ DB_set_sickness_by_gameid($gameid,$who+$add*3);
+ };
+
+ /* email next player */
+ $who = DB_get_sickness_by_gameid($gameid);
+ if($who<=4)
+ {
+ $To = DB_get_email_by_pos_and_gameid($who,$gameid);
+ $userhash = DB_get_hash_from_game_and_pos($gameid,$who);
+ DB_set_player_by_gameid($gameid,$who);