2 /* make sure that we are not called from outside the scripts,
3 * use a variable defined in config.php to check this
8 /* calling game.php only makes sense when we give it a hash for a game */
11 echo "Hmm, you really shouldn't mess with the urls.<br />\n";
16 $me = $_REQUEST["me"];
18 /* Ok, got a hash, but is it valid? */
19 $myid = DB_get_userid('hash',$me);
22 echo "Can't find you in the database, please check the url.<br />\n";
23 echo "perhaps the game has been canceled, check by login in <a href=\"$INDEX\">here</a>.";
29 global $GAME,$RULES,$CARDS;
31 /* user might get here by clicking on the link in an email, so session might not be set */
32 if(isset($_SESSION["name"]))
33 output_status($_SESSION["name"]);
35 /* the user has done something, update the timestamp */
36 DB_update_user_timestamp($myid);
38 /* get some information from the DB */
39 $gameid = DB_get_gameid_by_hash($me);
40 $myname = DB_get_name('hash',$me);
41 $mystatus = DB_get_status_by_hash($me);
42 $mypos = DB_get_pos_by_hash($me);
43 $myhand = DB_get_handid('hash',$me);
44 $session = DB_get_session_by_gameid($gameid);
46 /* get prefs and save them in a variable*/
47 $PREF = DB_get_PREF($myid);
49 /* get rule set for this game */
50 $RULES = DB_get_RULES($gameid);
52 /* get some infos about the game */
53 $gametype = DB_get_gametype_by_gameid($gameid);
54 $gamestatus = DB_get_game_status_by_gameid($gameid);
58 $gametype = DB_get_solo_by_gameid($gameid);
59 $GT = $gametype." ".$GT;
62 /* do we need to worry about Schweinchen?
63 * check gametype and rules
64 * if yes, figure out if someone actually has Schweinchen
65 * save information in $GAME
68 if( $gamestatus == 'pre' )
70 /* always need to use Schweinchen to figure out for example who has poverty */
75 /* in a game Schweinchen is not valid in all types of games */
76 if( $gametype == 'normal' || $gametype == 'silent' || $gametype=='trump' )
77 if( in_array($RULES['schweinchen'],array('both','second','secondaftercall')) )
81 /* these are the defaults */
82 $GAME['schweinchen-who'] = NULL;
83 $GAME['schweinchen-first'] = NULL;
84 $GAME['schweinchen-second'] = NULL;
88 /* need to check for Schweinchen */
91 $hash = DB_get_hash_from_game_and_pos($gameid,$i);
92 $cards = DB_get_all_hand($hash);
93 if( in_array("19",$cards) && in_array("20",$cards) )
94 $GAME['schweinchen-who']=$hash;
96 $GAME['schweinchen-first'] = 0; /* to keep track if they have been played already */
97 $GAME['schweinchen-second'] = 0;
99 /* end check for Schweinchen */
101 /* set the $CARDS variable, needed for sorting the cards
102 * we set it to normal so that the pre-game phase is handled ok
103 * and later set it to the correct game type that is played
105 set_gametype('normal');
107 /* put everyting in a form */
108 echo "<form action=\"index.php?action=game&me=$me\" method=\"post\">\n";
112 /* output extra division in case this game is part of a session */
115 echo "<div class=\"session\">\n".
116 "This game is part of session $session: \n";
117 $hashes = DB_get_hashes_by_session($session,$myid);
119 foreach($hashes as $hash)
124 echo "<a href=\"".$INDEX."?action=game&me=".$hash."\">$i</a> \n";
130 /* display the table and the names */
133 /* mystatus gets the player through the different stages of a game.
134 * start: does the player want to play?
135 * init: check for sickness
136 * check: check for return values from init
137 * poverty: handle poverty, wait here until all player have reached this state
138 * display sickness and move on to game
139 * play: game in progress
140 * gameover: are we revisiting a game
145 /* don't ask if user has autosetup set to yest */
147 if($PREF['autosetup']=='yes') $skip = 1;
149 if( !myisset("in") && !$skip)
151 /* asks the player, if he wants to join the game */
152 output_check_want_to_play($me);
157 /* check the result, if player wants to join, got next stage, else cancel game */
158 if(!$skip && $_REQUEST["in"] == "no" )
160 /* cancel the game */
161 $message = "Hello, \n\n".
162 "the game has been canceled due to the request of one of the players.\n";
164 $userids = DB_get_all_userid_by_gameid($gameid);
165 foreach($userids as $user)
167 $To = DB_get_email('userid',$user);
168 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
171 /* delete everything from the dB */
177 /* user wants to join the game */
179 /* move on to the next stage,
180 * no break statement to immediately go to the next stage
183 DB_set_hand_status_by_hash($me,'init');
185 /* check if everyone has reached this stage, send out email */
186 $userids = DB_get_all_userid_by_gameid($gameid);
188 foreach($userids as $user)
190 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
191 if($userstat!='init')
193 /* whos turn is it? */
194 DB_set_player_by_gameid($gameid,$user);
201 /* all done, send out email unless this player is the startplayer */
202 $startplayer = DB_get_startplayer_by_gameid($gameid);
203 if($mypos == $startplayer)
205 /* do nothing, go to next stage */
209 /* email startplayer */
211 $email = DB_get_email('position-gameid',$startplayer,$gameid);
212 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
213 $who = DB_get_userid('email',$email);
214 DB_set_player_by_gameid($gameid,$who);
216 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
217 "Use this link to go the game: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
218 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
225 /* here we ask the player if he is sick */
226 $mycards = DB_get_hand($me);
227 $mycards = mysort($mycards,$gametype);
229 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
230 echo "\n<ul class=\"tricks\">\n";
231 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
232 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
233 " <div class=\"trick\" id=\"trick0\">\n";
235 for($pos=1;$pos<5;$pos++)
237 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
238 $userid = DB_get_userid('gameid-position',$gameid,$pos);
239 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
241 if($userstatus=='start' || $userstatus=='init')
242 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs to decide </div>\n"; /* show this to everyone */
244 if($usersick!=NULL && $pos<=$mypos ) /* only show this for people sitting before the player */
245 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
246 else if($usersick==NULL && $pos<=$mypos)
247 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
249 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
250 /* end displaying sickness */
252 if(!myisset("solo","wedding","poverty","nines") )
254 output_check_for_sickness($me,$mycards);
256 echo "<div class=\"mycards\">Your cards are: <br />\n";
257 foreach($mycards as $card)
258 display_card($card,$PREF["cardset"]);
265 /* check if someone selected more than one sickness */
267 if($_REQUEST["solo"]!="No") $Nsickness++;
268 if($_REQUEST["wedding"] == "yes") $Nsickness++;
269 if($_REQUEST["poverty"] == "yes") $Nsickness++;
270 if($_REQUEST["nines"] == "yes") $Nsickness++;
274 echo "<p class=\"message\"> You selected more than one sickness, please go back ".
275 "and answer the <a href=\"$INDEX?action=game&me=$me&in=yes\">question</a> again.</p>";
277 echo "<div class=\"mycards\">Your cards are: <br />\n";
278 foreach($mycards as $card)
279 display_card($card,$PREF["cardset"]);
286 /* everything is ok, save what user said and proceed */
287 echo "<p class=\"message\">Processing what you selected in the last step...";
289 /* check if this sickness needs to be handled first */
290 $gametype = DB_get_gametype_by_gameid($gameid);
291 $startplayer = DB_get_startplayer_by_gameid($gameid); /* need this to check which solo goes first */
293 if( $_REQUEST["solo"]!="No" )
295 /* user wants to play a solo */
297 /* store the info in the user's hand info */
298 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
299 DB_set_sickness_by_hash($me,"solo");
301 echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
303 if($gametype == "solo" && $startplayer<$mypos)
304 {}/* do nothing, since someone else already is playing solo */
307 /* this solo comes first
308 * store info in game table
310 DB_set_gametype_by_gameid($gameid,"solo");
311 DB_set_startplayer_by_gameid($gameid,$mypos);
312 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
315 else if($_REQUEST["wedding"] == "yes")
317 /* silent solo is set further down */
318 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
319 DB_set_sickness_by_hash($me,"wedding");
321 else if($_REQUEST["poverty"] == "yes")
323 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
324 DB_set_sickness_by_hash($me,"poverty");
326 else if($_REQUEST["nines"] == "yes")
328 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
329 " is playing solo, this game will be canceled.<br />\n";
330 DB_set_sickness_by_hash($me,"nines");
335 /* move on to the next stage*/
336 DB_set_hand_status_by_hash($me,'check');
341 /* here we check what all players said and figure out what game we are playing
342 * this can therefore only be handled once all players finished the last stage
345 /* only need to redisplay the cards when the user reloads the page and lands directly here */
346 if($mystatus=='check')
348 $mycards = DB_get_hand($me);
349 $mycards = mysort($mycards,$gametype);
351 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
352 echo "\n<ul class=\"tricks\">\n";
353 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
354 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
355 " <div class=\"trick\" id=\"trick0\">\n";
357 for($pos=1;$pos<5;$pos++)
359 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
360 $userid = DB_get_userid('gameid-position',$gameid,$pos);
361 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
363 if($userstatus=='start' || $userstatus=='init')
364 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs to decide </div>\n"; /* show this to everyone */
366 if($usersick!=NULL) /* in the init-phase we only showed players with $pos<$mypos, now we can show all */
367 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
369 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
371 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
372 /* end displaying sickness */
375 echo "<div class=\"message\">\n";
376 echo "<p> Checking if someone else selected solo, nines, wedding or poverty.</p>";
378 /* check if everyone has reached this stage */
379 $userids = DB_get_all_userid_by_gameid($gameid);
381 foreach($userids as $user)
383 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
384 if($userstat!='check')
387 DB_set_player_by_gameid($gameid,$user);
394 echo "<p>This step can only be handled after everyone finished the last step. ".
395 "Seems like this is not the case, so you need to wait a bit... ".
396 "you will get an email once that is the case, please use the link in ".
397 "that email to continue the game.</p></div>";
399 /* display cards, if player was just at the init-phase he will still see the cards from there
400 * we can put this one here, since the last player to finish the init state won't get here and
401 * will still see his card anyway from the init-phase
403 if($mystatus=='check')
406 echo "<div class=\"mycards\">Your cards are: <br />\n";
407 foreach($mycards as $card)
408 display_card($card,$PREF["cardset"]);
415 /* Ok, everyone finished the init-phase, time to figure out what game we
416 * are playing, in case there are any solos this already
417 * will have the correct information in it */
419 echo "<p> Ok, everyone is done... figuring out what kind of game we are playing.</p>";
421 $gametype = DB_get_gametype_by_gameid($gameid);
422 $startplayer = DB_get_startplayer_by_gameid($gameid);
424 /* check for sickness */
429 foreach($userids as $user)
431 $name = DB_get_name('userid',$user);
432 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
433 if($usersick == 'nines')
436 break; /* no need to check for other poverties, since only solo can win and that is already set */
438 else if($usersick == 'poverty')
440 else if($usersick == 'wedding')
442 else if($usersick == 'solo')
446 /* now check which sickness comes first and set the gametype to it */
447 if($gametype == "solo")
454 /* TODO: should we keep statistics of this? */
455 $message = "Hello, \n\n".
456 " the game has been canceled because ".DB_get_name('userid',$nines).
457 " has five or more nines and nobody is playing solo.\n\n".
458 " To redeal either start a new game or, in case the game was part of a tournament, \n".
459 " go to the last game and use the link at the bottom of the page to redeal.";
461 $userids = DB_get_all_userid_by_gameid($gameid);
462 foreach($userids as $user)
464 $To = DB_get_email('userid',$user);
465 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
468 /* delete everything from the dB */
471 echo "The game has been canceled because ".DB_get_name('userid',$nines).
472 " has five or more nines and nobody is playing solo.\n";
477 else if($poverty==1) /* one person has poverty */
479 DB_set_gametype_by_gameid($gameid,"poverty");
480 $gametype = "poverty";
481 $who = DB_get_sickness_by_gameid($gameid);
484 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
485 if($firstsick == "poverty")
486 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
488 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
491 else if($poverty==2) /* two people have poverty */
493 DB_set_gametype_by_gameid($gameid,"dpoverty");
494 $gametype = "dpoverty";
495 $who = DB_get_sickness_by_gameid($gameid);
498 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
499 if($firstsick == "poverty")
501 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
502 if($secondsick == "poverty")
503 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
505 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
508 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
513 DB_set_gametype_by_gameid($gameid,"wedding");
514 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
515 $gametype = "wedding";
517 /* now the gametype is set correctly in the database */
518 echo "<p> Got it :)</p>";
520 /* loop over all players, set re/contra if possible and start the game if possible */
521 $userids = DB_get_all_userid_by_gameid($gameid);
522 foreach($userids as $userid)
524 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
529 /* are we the solo player? set us to re, else set us to contra */
530 $pos = DB_get_pos_by_hash($userhash);
531 if($pos == $startplayer)
532 DB_set_party_by_hash($userhash,"re");
534 DB_set_party_by_hash($userhash,"contra");
535 DB_set_hand_status_by_hash($userhash,'play');
539 /* set person with the wedding to re, do the rest during the game */
540 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
541 if($usersick == "wedding")
542 DB_set_party_by_hash($userhash,"re");
544 DB_set_party_by_hash($userhash,"contra");
546 DB_set_hand_status_by_hash($userhash,'play');
550 $hand = DB_get_all_hand($userhash);
552 if(in_array('3',$hand)||in_array('4',$hand))
553 DB_set_party_by_hash($userhash,"re");
555 DB_set_party_by_hash($userhash,"contra");
556 DB_set_hand_status_by_hash($userhash,'play');
560 /* set person with poverty to play status */
561 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
562 if($usersick == "poverty")
563 DB_set_hand_status_by_hash($userhash,'play');
565 /* set status of first player to be asked to poverty */
566 $who = DB_get_sickness_by_gameid($gameid);
567 if($who > 6) $who= $who/10; /* in case we have dpoverty */
568 $whoid = DB_get_userid('gameid-position',$gameid,$who);
570 DB_set_hand_status_by_hash($userhash,'poverty');
573 /* check for silent solo, set game type to solo in this case */
574 $gametype = DB_get_gametype_by_gameid($gameid);
575 $userids = DB_get_all_userid_by_gameid($gameid);
576 foreach($userids as $userid)
578 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
580 if($gametype=='normal')
582 $userhand = DB_get_all_hand($userhash);
583 if(check_wedding($userhand))
585 /* normal game type and player has both queens -> silent solo */
586 /* keep startplayer, just set gametype to silent solo */
587 DB_set_gametype_by_gameid($gameid,"solo");
588 DB_set_solo_by_gameid($gameid,'silent');
593 /* send out email to first player or poverty person*/
594 if($gametype!="poverty" && $gametype!="dpoverty")
596 $startplayer = DB_get_startplayer_by_gameid($gameid);
597 $email = DB_get_email('position-gameid',$startplayer,$gameid);
598 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
599 $who = DB_get_userid('email',$email);
600 DB_set_player_by_gameid($gameid,$who);
604 if(DB_get_email_pref_by_hash($hash)!="emailaddict")
606 /* email startplayer */
607 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
608 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
609 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
613 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
617 /* set status of first player to be asked to poverty */
618 $who = DB_get_sickness_by_gameid($gameid);
619 if($who > 6) $who= $who/10; /* in case we have dpoverty */
621 $whoid = DB_get_userid('gameid-position',$gameid,$who);
623 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
626 $email = DB_get_email('position-gameid',$who,$gameid);
627 $whohash = DB_get_hash_from_game_and_pos($gameid,$who);
628 DB_set_player_by_gameid($gameid,$whoid);
630 if(DB_get_email_pref_by_hash($hash)!="emailaddict")
632 /* email player for poverty */
633 $message = "Poverty: It's your turn now in game ".DB_format_gameid($gameid).".\n".
634 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$whohash."\n\n" ;
635 mymail($email,$EmailName."Poverty (game ".DB_format_gameid($gameid).") ",$message);
643 /* user only gets here in a poverty game, several things have to be handled here:
644 * A) ask, if user wants to take trump
646 * poverty: set re/contra
647 * dpoverty: first time: set re, send email to second player
648 * second time: set contra
649 * poverty: set status of other players to 'play'
650 * set status to play in case 0 trump
651 * no -> set status to play,
652 * ask next player or cancle the game if no more players
653 * B) user took trump and has too many cards (e.g. count(cards)>12 and re/contra set)
654 * ask to give cards back, set status to play, once player has 12 cards
656 * it is easier to check B) first
659 /* output pre game in case user reloads */
662 set_gametype($gametype); /* this sets the $CARDS variable */
663 $myparty = DB_get_party_by_hash($me);
665 /* the following is part B) of whats needs to be done)
666 /* check if user wants to give cards back */
667 if(myisset("exchange"))
669 $exchange = $_REQUEST['exchange'];
670 $partnerhash = DB_get_partner_hash_by_hash($me);
671 $partnerid = DB_get_userid('hash',$partnerhash);
672 $partnerhand = DB_get_handid('gameid-userid',$gameid,$partnerid);
674 /* if exchange is set to a value>0, exchange that card back to the partner */
677 $result = DB_query("UPDATE Hand_Card SET hand_id='$partnerhand'".
678 " WHERE hand_id='$myhand' AND card_id=".DB_quote_smart($exchange));
683 $mycards = DB_get_hand($me);
684 $mycards = mysort($mycards,$gametype);
686 /* check if user need to give more cards back */
687 if( ($myparty=='re' || $myparty=='contra') && count($mycards)>12)
689 echo "<div class=\"poverty\"> you need to get rid of a few cards</div>\n";
692 echo "<div class=\"mycards\">Your cards are: <br />\n";
693 foreach($mycards as $card)
694 display_link_card($card,$PREF["cardset"],$type);
695 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select card to give back\" />\n";
698 else if( ($myparty=='re' || $myparty=='contra') && count($mycards)==12)
700 /* user is done, ready to play */
701 DB_set_hand_status_by_hash($me,'play');
703 /* email start player */
704 $startplayer = DB_get_startplayer_by_gameid($gameid);
705 $email = DB_get_email('position-gameid',$startplayer,$gameid);
706 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
707 $who = DB_get_userid('email',$email);
708 DB_set_player_by_gameid($gameid,$who);
712 if(DB_get_email_pref_by_hash($hash)!="emailaddict")
714 /* email startplayer */
715 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
716 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
717 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
721 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
724 /* the following is part A) of what needs to be done */
725 if(!myisset("trump"))
729 echo "<div class=\"poverty\">\n";
730 $userids = DB_get_all_userid_by_gameid($gameid);
731 foreach($userids as $user)
733 $name = DB_get_name('userid',$user);
734 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
735 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
736 $userparty = DB_get_party_by_hash($userhash);
738 if($usersick=="poverty" && !$userparty)
740 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
741 $cards = DB_get_hand($hash);
744 foreach($cards as $card)
745 if($card<27) $nrtrump++;
746 echo "Player $name has $nrtrump trump. Do you want to take them?".
747 "<a href=\"index.php?action=game&me=$me&trump=$user\">Yes</a> <br />\n";
750 echo "<a href=\"index.php?action=game&me=$me&trump=no\">No way</a> <br />\n";
751 echo "</div><div>\n";
753 echo "<div class=\"mycards\">Your cards are: <br />\n";
754 foreach($mycards as $card)
755 display_card($card,$PREF["cardset"]);
756 echo "</div></div>\n";
762 $trump = $_REQUEST['trump'];
766 /* user doesn't want to take trump */
767 DB_set_hand_status_by_hash($me,'play');
769 /* set next player who needs to be asked and email him*/
770 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
771 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
773 /* don't ask people who have poverty */
775 if($firstsick=="poverty")
777 if($secondsick=="poverty")
782 if($gametype=="dpoverty")
784 $next=999; /* need to cancel for sure, since both would need to take the trump */
787 /* no more people to ask, need to cancel the game */
790 $message = "Hello, \n\n".
791 "Game ".DB_format_gameid($gameid)." has been canceled since nobody wanted to take the trump.\n";
793 $userids = DB_get_all_userid_by_gameid($gameid);
794 foreach($userids as $user)
796 $To = DB_get_email('userid',$user);
797 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled (poverty not resolved)",$message);
800 /* delete everything from the dB */
803 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been canceled.<br /><br /></p>";
810 /* email next player, set his status to poverty */
811 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
812 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
813 $userid = DB_get_userid('email',$To);
815 DB_set_player_by_gameid($gameid,$userid);
816 DB_set_hand_status_by_hash($userhash,'poverty');
818 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
819 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
820 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
825 /* player wants to take trump, change cards */
827 /* user wants to take trump */
828 $trump = $_REQUEST["trump"];
829 $userhand = DB_get_handid('gameid-userid',$gameid,$trump);
830 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
832 /* copy trump from player A to B */
833 $result = DB_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
836 $mycards = DB_get_hand($me);
839 if($gametype=='poverty')
841 $userids = DB_get_all_userid_by_gameid($gameid);
842 foreach($userids as $user)
844 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
845 if($hash==$userhash||$hash==$me)
847 DB_set_party_by_hash($hash,"re");
851 DB_set_party_by_hash($hash,"contra");
852 DB_set_hand_status_by_hash($hash,'play'); /* the contra party is ready to play */
855 /* check if we are done (in case of no trump handed over), if so, go to 'play' phase right away*/
856 if(count($mycards)==12)
858 DB_set_hand_status_by_hash($me,'play');
863 /* has the re party already been set?*/
865 $userids = DB_get_all_userid_by_gameid($gameid);
866 foreach($userids as $user)
868 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
869 $party = DB_get_party_by_hash($hash);
875 DB_set_party_by_hash($me,'contra');
876 DB_set_party_by_hash($userhash,'contra');
880 DB_set_party_by_hash($me,'re');
881 DB_set_party_by_hash($userhash,'re');
883 /* send out email to second non-poverty player */
884 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
885 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
888 if($firstsick=="poverty")
889 if($secondsick=="poverty")
895 echo "<div class=\"message\">Error in poverty, please contact the Admin</div>\n";
897 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
898 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
899 $userid = DB_get_userid('email',$To);
901 DB_set_player_by_gameid($gameid,$userid);
902 DB_set_hand_status_by_hash($userhash,'poverty');
904 $message = "Two people have poverty, it's your turn to decide, if you want to take the trump. Please visit:".
905 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
906 mymail($To,$EmailName." double poverty (game ".DB_format_gameid($gameid).")",$message);
911 echo "<div class=\"message\"> Please, <a href=\"$INDEX?action=game&me=$me\">continue</a> here.</div>\n";
919 /* both entries here, so that the tricks are visible for both.
920 * in case of 'play' there is a break later that skips the last part
923 /* check if all players are ready to play,
924 * if so, send out email to the startplayer
925 * only need to do this if the game hasn't started yet
927 $gamestatus = DB_get_game_status_by_gameid($gameid);
928 if($gamestatus == 'pre')
931 $userids = DB_get_all_userid_by_gameid($gameid);
932 foreach($userids as $user)
934 $userstatus = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
935 if($userstatus !='play' && $userstatus!='gameover')
938 DB_set_player_by_gameid($gameid,$user);
944 /* only set this after all poverty, etc. are handled*/
945 DB_set_game_status_by_gameid($gameid,'play');
947 /* email startplayer */
948 $startplayer = DB_get_startplayer_by_gameid($gameid);
949 $email = DB_get_email('position-gameid',$startplayer,$gameid);
950 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
951 $who = DB_get_userid('email',$email);
952 DB_set_player_by_gameid($gameid,$who);
954 if($hash!=$me && DB_get_email_pref_by_hash($hash)!="emailaddict")
956 /* email startplayer) */
957 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
958 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
959 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
963 /* figure out what kind of game we are playing,
964 * set the global variables $CARDS["trump"],$CARDS["diamonds"],$CARDS["hearts"],
965 * $CARDS["clubs"],$CARDS["spades"],$CARDS["foxes"]
969 $gametype = DB_get_gametype_by_gameid($gameid);
971 if($gametype=="solo")
973 $gametype = DB_get_solo_by_gameid($gameid);
974 if($gametype=='silent')
977 $GT = $gametype." ".$GT;
980 $gametype = "normal";
982 set_gametype($gametype); /* this sets the $CARDS variable */
984 /* get some infos about the game, need to reset this, since it might have changed */
985 $gamestatus = DB_get_game_status_by_gameid($gameid);
987 /* has the game started? No, then just wait here...*/
988 if($gamestatus == 'pre')
990 echo "<p class=\"message\"> You finished the setup, but not everyone else finished it... ".
991 "You need to wait for the others. Just wait for an email. </p>";
993 $mycards = DB_get_hand($me);
994 $mycards = mysort($mycards,$gametype);
996 echo "<div class=\"mycards\">Your cards are: <br />\n";
997 foreach($mycards as $card)
998 display_card($card,$PREF["cardset"]);
1001 break; /* not sure this works... the idea is that you can
1002 * only play a card after everyone is ready to play */
1005 /* get time from the last action of the game */
1006 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1007 $gameend = time() - strtotime($r[0]);
1009 /* handle comments in case player didn't play a card, allow comments a week after the end of the game */
1010 if( (!myisset("card") && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
1011 if(myisset("comment"))
1013 $comment = $_REQUEST["comment"];
1014 $playid = DB_get_current_playid($gameid);
1017 DB_insert_comment($comment,$playid,$myid);
1020 /* handle notes in case player didn't play a card, allow notes only during a game */
1021 if( (!myisset("card") && $mystatus=='play') )
1024 $note = $_REQUEST["note"];
1027 DB_insert_note($note,$gameid,$myid);
1030 /* get everything relevant to display the tricks */
1031 $result = DB_query("SELECT Hand_Card.card_id as card,".
1032 " Hand.position as position,".
1033 " Play.sequence as sequence, ".
1035 " GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
1036 " SEPARATOR '\n' ), ".
1037 " Play.create_date, ".
1040 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1041 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1042 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1043 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1044 "LEFT JOIN User On User.id=Comment.user_id ".
1045 "WHERE Trick.game_id='".$gameid."' ".
1046 "GROUP BY Trick.id, sequence ".
1047 "ORDER BY Trick.id, sequence ASC");
1049 $lasttrick = DB_get_max_trickid($gameid);
1051 $play = array(); /* needed to calculate winner later */
1053 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1054 $firstcard = ""; /* first card in a trick */
1056 echo "\n<ul class=\"tricks\">\n";
1057 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
1059 /* output vorbehalte */
1060 $mygametype = DB_get_gametype_by_gameid($gameid);
1061 if($mygametype != 'normal' && $mygametype != 'silent') /* only show when needed */
1063 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
1064 " <div class=\"trick\" id=\"trick0\">\n";
1066 for($mypos=1;$mypos<5;$mypos++)
1068 $usersick = DB_get_sickness_by_pos_and_gameid($mypos,$gameid);
1071 echo " <div class=\"vorbehalt".($mypos-1)."\"> Vorbehalt <br />";
1073 echo " $usersick <br />";
1076 if($mygametype == $usersick)
1080 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1084 while($r = DB_fetch_array($result))
1092 /* count number of tricks */
1096 /* check if first schweinchen has been played */
1097 if( $GAME['schweinchen-who'] && ($r[0] == 19 || $r[0] == 20) )
1098 if(!$GAME['schweinchen-first'])
1099 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1101 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1103 /* save card to be able to find the winner of the trick later */
1104 $play[$seq] = array("card"=>$r[0],"pos"=>$pos);
1108 /* first card in a trick, output some html */
1109 if($trick!=$lasttrick)
1111 /* start of an old trick? */
1112 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1113 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1114 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1116 else if($trick==$lasttrick)
1118 /* start of a last trick? */
1119 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1120 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1121 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1124 /* remember first card, so that we are able to check, what cards can be played */
1129 echo " <div class=\"card".($pos-1)."\">\n";
1131 /* display comments */
1133 echo " <span class=\"comment\">".$comment."</span>\n";
1136 display_card($r[0],$PREF["cardset"]);
1138 echo " </div>\n"; /* end div card */
1143 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1147 /* whos turn is it? */
1150 $winner = get_winner($play,$gametype); /* returns the position */
1152 $firstcard = ""; /* new trick, no first card */
1157 if($next==5) $next = 1;
1160 /* my turn?, display cards as links, ask for comments*/
1161 if(DB_get_pos_by_hash($me) == $next)
1166 /* do we want to play a card? */
1167 if(myisset("card") && $myturn)
1169 $card = $_REQUEST["card"];
1170 $handid = DB_get_handid('hash',$me);
1171 $commentSchweinchen =""; /* used to add a comment when Schweinchen is being played */
1173 /* check if we have card and that we haven't played it yet*/
1174 /* set played in hand_card to true where hand_id and card_id*/
1175 $r = DB_query_array("SELECT id FROM Hand_Card WHERE played='false' and ".
1176 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1177 $handcardid = $r[0];
1179 if($handcardid) /* everything ok, play card */
1181 /* update Game timestamp */
1182 DB_update_game_timestamp($gameid);
1184 /* check if a call was made, must do this before we set the card status to played */
1185 if(myisset("call") && $_REQUEST["call"] == "120" && can_call(120,$me))
1186 $result = DB_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
1187 if(myisset("call") && $_REQUEST["call"] == "90" && can_call(90,$me))
1188 $result = DB_query("UPDATE Hand SET point_call='90' WHERE hash='$me' ");
1189 if(myisset("call") && $_REQUEST["call"] == "60" && can_call(60,$me))
1190 $result = DB_query("UPDATE Hand SET point_call='60' WHERE hash='$me' ");
1191 if(myisset("call") && $_REQUEST["call"] == "30" && can_call(30,$me))
1192 $result = DB_query("UPDATE Hand SET point_call='30' WHERE hash='$me' ");
1193 if(myisset("call") && $_REQUEST["call"] == "0" && can_call(0,$me))
1194 $result = DB_query("UPDATE Hand SET point_call='0' WHERE hash='$me' ");
1196 /* mark card as played */
1197 DB_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1198 DB_quote_smart($card));
1200 /* get trick id or start new trick */
1201 $a = DB_get_current_trickid($gameid);
1206 $playid = DB_play_card($trickid,$handcardid,$sequence);
1208 /* check special output for schweinchen in case in case a fox is being played
1209 * check for correct rules, etc. has already been done
1211 if( $GAME["schweinchen-who"] && ($card == 19 || $card == 20) )
1213 if(!$GAME['schweinchen-first'])
1214 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1216 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1218 if( ($GAME['schweinchen-second']==1 && $RULES['schweinchen']=='second') || $RULES['schweinchen']=='both')
1220 DB_insert_comment("Schweinchen! ",$playid,$myid);
1221 $commentSchweinchen = "Schweinchen! ";
1224 echo "schweinchen = ".$GAME["schweinchen-who"]." ---<br />";
1227 /* if sequence == 4 check who one in case of wedding */
1228 if($sequence == 4 && $GT == "wedding")
1230 /* is wedding resolve */
1231 $resolved = DB_get_sickness_by_gameid($gameid);
1234 /* who has wedding */
1235 $userids = DB_get_all_userid_by_gameid($gameid);
1236 foreach($userids as $user)
1238 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1239 if($usersick == "wedding")
1242 /* who won the trick */
1243 $play = DB_get_cards_by_trick($trickid);
1244 $winner = get_winner($play,$gametype); /* returns the position */
1245 $winnerid = DB_get_userid('gameid-position',$gameid,$winner);
1246 /* is tricknr <=3 */
1247 if($tricknr <=3 && $winnerid!=$whosick)
1249 /* set resolved at tricknr*/
1250 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1252 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1253 DB_set_party_by_hash($whash,"re");
1255 if($tricknr == 3 && $winnerid==$whosick)
1257 /* set resolved at tricknr*/
1258 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1263 /* if sequence == 4, set winner of the trick, count points and set the next player */
1266 $play = DB_get_cards_by_trick($trickid);
1267 $winner = get_winner($play,$gametype); /* returns the position */
1269 /* check if someone caught a fox */
1270 /* first check if we should account for solos at all,
1271 * since it doesn't make sense in some games
1273 $ok = 0; /* fox shouldn't be counted */
1274 if(DB_get_gametype_by_gameid($gameid)=="solo")
1276 $solo = DB_get_solo_by_gameid($gameid);
1277 if($solo == 'trump' || $solo == 'silent')
1278 $ok = 1; /* for trump solos and silent solos, foxes are ok */
1281 $ok = 1; /* for all other games (not solos) foxes are ok too */
1284 foreach($play as $played)
1286 if ( $played['card']==19 || $played['card']==20 )
1287 if ($played['pos']!= $winner )
1289 /* possible caught a fox, check party */
1290 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1291 $uid2 = DB_get_userid('gameid-position',$gameid,$played['pos']);
1293 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1294 $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1296 if($party1 != $party2)
1297 DB_query("INSERT INTO Score".
1298 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1302 /* check for karlchen (jack of clubs in the last trick)*/
1303 /* same as for foxes, karlchen doesn't always make sense
1304 * check what kind of game it is and set karlchen accordingly */
1305 $ok = 1; /* default: karlchen should be accounted for */
1307 $ok = 0; /* Karlchen works only in the last trick */
1308 if($ok && DB_get_gametype_by_gameid($gameid)=="solo" )
1310 $solo = DB_get_solo_by_gameid($gameid);
1311 if($solo == "trumpless" || $solo == "jack" || $solo == "queen" )
1312 $ok = 0; /* no Karlchen in these solos */
1316 foreach($play as $played)
1317 if ( $played['card']==11 || $played['card']==12 )
1318 if ($played['pos'] == $winner )
1320 /* possible caught a fox, check party */
1321 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1322 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1324 DB_query("INSERT INTO Score".
1325 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1327 /* check for doppelopf (>40 points)*/
1329 foreach($play as $played)
1331 $points += DB_get_card_value_by_cardid($played['card']);
1335 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1336 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1338 DB_query("INSERT INTO Score".
1339 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
1343 DB_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1345 echo "ERROR during scoring";
1348 echo "DEBUG: position $winner won the trick <br />";
1350 /* who is the next player? */
1355 $next = DB_get_pos_by_hash($me)+1;
1357 if($next==5) $next=1;
1359 /* check for coment */
1360 if(myisset("comment"))
1362 $comment = $_REQUEST["comment"];
1364 DB_insert_comment($comment,$playid,$myid);
1365 if($commentSchweinchen)
1366 $comment = $commentSchweinchen . $comment;
1369 /* check for note */
1372 $note = $_REQUEST["note"];
1374 DB_insert_note($note,$gameid,$myid);
1377 /* display played card */
1378 $pos = DB_get_pos_by_hash($me);
1381 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1382 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1383 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1386 echo " <div class=\"card".($pos-1)."\">\n ";
1388 /* display comments */
1389 display_card($card,$PREF["cardset"]);
1391 echo "\n <span class=\"comment\"> ".$comment."</span>\n";
1394 /*check if we still have cards left, else set status to gameover */
1395 if(sizeof(DB_get_hand($me))==0)
1397 DB_set_hand_status_by_hash($me,'gameover');
1398 $mystatus = 'gameover';
1401 /* if all players are done, set game status to game over,
1402 * get the points of the last trick and send out an email
1405 $userids = DB_get_all_userid_by_gameid($gameid);
1408 foreach($userids as $user)
1409 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1413 DB_set_game_status_by_gameid($gameid,"gameover");
1415 /* email next player, if game is still running */
1416 if(DB_get_game_status_by_gameid($gameid)=='play')
1418 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1419 $email = DB_get_email('hash',$next_hash);
1420 $who = DB_get_userid('email',$email);
1421 DB_set_player_by_gameid($gameid,$who);
1423 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1424 "It's your turn now.\n".
1425 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$next_hash."\n\n" ;
1426 if( DB_get_email_pref_by_uid($who)!="emailaddict" )
1427 mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
1429 else /* send out final email */
1431 /* individual score */
1432 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1433 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1434 " LEFT JOIN User ON User.id=Hand.user_id".
1435 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1436 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1437 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1438 " WHERE Hand.game_id='$gameid'".
1439 " GROUP BY User.fullname" );
1440 $message = "The game is over. Thanks for playing :)\n";
1441 $message .= "Final score:\n";
1442 while( $r = DB_fetch_array($result) )
1443 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1445 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1446 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1447 " LEFT JOIN User ON User.id=Hand.user_id".
1448 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1449 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1450 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1451 " WHERE Hand.game_id='$gameid'".
1452 " GROUP BY Hand.party" );
1453 $message .= "\nTotals:\n";
1456 while( $r = DB_fetch_array($result) )
1458 $message .= " ".$r[0]." ".$r[1]."\n";
1461 else if($r[0] == "contra")
1466 * save score in database
1470 /* get calls from re/contra */
1472 $call_contra = NULL;
1473 foreach($userids as $user)
1475 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1476 $call = DB_get_call_by_hash($hash);
1477 $party = DB_get_party_by_hash($hash);
1481 $call = (int) $call;
1487 else if( $call < $call_re)
1490 else if($party=="contra")
1492 if($call_contra==NULL)
1493 $call_contra = $call;
1494 else if( $call < $call_re)
1495 $call_contra = $call;
1500 /* figure out who one */
1501 $winning_party = NULL;
1503 if($call_re == NULL && $call_contra==NULL)
1505 /* nobody made a call, so it's easy to figure out who won */
1507 $winning_party="re";
1509 $winning_party="contra";
1513 /* if one party makes a call, they only win, iff they make enough points
1514 * if only one party made a call, the other one wins,
1515 * if the first one didn't make it
1519 $offset = 120 - $call_re;
1521 $offset--; /* since we use a > in the next equation */
1523 if($re > 120+$offset)
1524 $winning_party="re";
1525 else if ($call_contra == NULL )
1526 $winning_party="contra";
1531 $offset = 120 - $call_contra;
1532 if($call_contra == 0)
1533 $offset--; /* since we use a > in the next equation */
1535 if($contra > 120+$offset)
1536 $winning_party="contra";
1537 else if ($call_re == NULL )
1538 $winning_party="re";
1542 /* one point for each call of the other party in case the other party didn't win
1543 * and one point each in case the party made more than points than one of the calls
1545 if($winning_party!="contra" && $call_contra!=NULL)
1547 for( $p=$call_contra;$p<=120; $p+=30 )
1549 DB_query("INSERT INTO Score".
1550 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
1553 for( $p=$call_contra; $p<120; $p+=30)
1556 DB_query("INSERT INTO Score".
1557 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'made$p')");
1560 if($winning_party!="re" and $call_re!=NULL)
1562 for( $p=$call_re;$p<=120; $p+=30 )
1564 DB_query("INSERT INTO Score".
1565 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'against$p')");
1568 for( $p=$call_re; $p<120; $p+=30)
1571 DB_query("INSERT INTO Score".
1572 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
1576 /* point in case contra won */
1577 if($winning_party=="contra")
1579 DB_query("INSERT INTO Score".
1580 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1583 /* one point each for winning and each 30 points + calls */
1584 if($winning_party=="re")
1586 foreach(array(120,150,180,210,240) as $p)
1589 if($p==240 || $call_contra!=NULL)
1593 DB_query("INSERT INTO Score".
1594 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
1596 /* re called something and won */
1597 foreach(array(0,30,60,90,120) as $p)
1599 if($call_re!=NULL && $call_re<$p+1)
1600 DB_query("INSERT INTO Score".
1601 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
1604 else if( $winning_party=="contra")
1606 foreach(array(120,150,180,210,240) as $p)
1609 if($p==240 || $call_re!=NULL)
1612 if($contra>$p-$offset)
1613 DB_query("INSERT INTO Score".
1614 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
1616 /* re called something and won */
1617 foreach(array(0,30,60,90,120) as $p)
1619 if($call_contra!=NULL && $call_contra<$p+1)
1620 DB_query("INSERT INTO Score".
1621 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
1626 /* add score points to email */
1629 $message .= " Points Re: \n";
1630 $queryresult = DB_query("SELECT score FROM Score ".
1631 " WHERE game_id=$gameid AND party='re'".
1633 while($r = DB_fetch_array($queryresult) )
1635 $message .= " ".$r[0]."\n";
1638 $message .= " Points Contra: \n";
1639 $queryresult = DB_query("SELECT score FROM Score ".
1640 " WHERE game_id=$gameid AND party='contra'".
1642 while($r = DB_fetch_array($queryresult) )
1644 $message .= " ".$r[0]."\n";
1647 $message .= " Total Points (from the Re point of view): $Tpoint\n";
1650 $session = DB_get_session_by_gameid($gameid);
1651 $score = generate_score_table($session);
1652 /* convert html to ascii */
1653 $score = str_replace("<div class=\"scoretable\">\n<table class=\"score\">\n <tr>\n","",$score);
1654 $score = str_replace("</table></div>\n","",$score);
1655 $score = str_replace("\n","",$score);
1656 $score = str_replace(array("<tr>","</tr>","<td>","</td>"),array("","\n","","|"),$score);
1657 $score = explode("\n",$score);
1659 $header = array_slice($score,0,1);
1660 $header = explode("|",$header[0]);
1661 for($i=0;$i<sizeof($header);$i++)
1662 $header[$i]=str_pad($header[$i],6," ",STR_PAD_BOTH);
1663 $header = implode("|",$header);
1664 $header.= "\n------+------+------+------+------+\n";
1665 if(sizeof($score)>5) $header.= " ... \n";
1667 if(sizeof($score)>5) $score = array_slice($score,-5,5);
1668 for($i=0;$i<sizeof($score);$i++)
1670 $line = explode("|",$score[$i]);
1671 for($j=0;$j<sizeof($line);$j++)
1672 $line[$j]=str_pad($line[$j],6," ",STR_PAD_LEFT);
1673 $score[$i] = implode("|",$line);
1676 $score = implode("\n",$score);
1677 $score = $header.$score;
1679 $message .= "Score Table:\n";
1682 /* send out final email */
1685 foreach($userids as $user)
1686 $all[] = DB_get_email('userid',$user);
1687 $To = implode(",",$all);
1689 $help = "\n\n (you can use reply all on this email to reach all the players.)\n";
1690 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 1(2)",$message.$help);
1692 foreach($userids as $user)
1694 $To = DB_get_email('userid',$user);
1695 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1697 $link = "Use this link to have a look at game ".DB_format_gameid($gameid).": ".
1698 $HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
1699 if( DB_get_email_pref_by_uid($user) != "emailaddict" )
1700 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 2(2)",$link);
1706 echo "can't find that card?! <br />\n";
1709 else if(myisset("card") && !$myturn )
1711 echo "please wait until it's your turn! <br />\n";
1714 if($seq!=4 && $trickNR>=1)
1715 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1717 /* display points in case game is over */
1718 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1720 echo " <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1721 " <div class=\"trick\" id=\"trick13\">\n";
1722 /* add pic for re/contra
1723 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1725 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1726 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1727 " LEFT JOIN User ON User.id=Hand.user_id".
1728 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1729 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1730 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1731 " WHERE Hand.game_id='$gameid'".
1732 " GROUP BY User.fullname" );
1733 while( $r = DB_fetch_array($result))
1734 echo " <div class=\"card".($r[3]-1)."\">\n".
1735 " <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
1738 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1741 echo " <li onclick=\"hl_prev();\" class=\"old\"><a href=\"#\">prev</a></li>\n";
1742 echo " <li onclick=\"hl_next();\" class=\"old\"><a href=\"#\">next</a></li>\n";
1743 echo "</ul>\n"; /* end ul tricks*/
1745 echo "<div class=\"notes\"> Personal notes: <br />\n";
1746 $notes = DB_get_notes_by_userid_and_gameid($myid,$gameid);
1747 foreach($notes as $note)
1748 echo "$note <hr />\n";
1749 echo "Insert note:<input name=\"note\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1752 $mycards = DB_get_hand($me);
1753 $mycards = mysort($mycards,$gametype);
1754 echo "<div class=\"mycards\">\n";
1756 if($myturn && !myisset("card") && $mystatus=='play' )
1758 echo "Hello ".$myname.", it's your turn! <br />\n";
1759 echo "Your cards are: <br />\n";
1761 /* do we have to follow suite? */
1763 if(have_suit($mycards,$firstcard))
1766 foreach($mycards as $card)
1768 if( ($followsuit && !same_type($card,$firstcard)) ||
1769 ( (int)($card)==19 && ($RULES['schweinchen']=='second'||$RULES['schweinchen']=='secondaftercall')
1770 && $GAME['schweinchen-who']==$me && !$GAME['schweinchen-first'] )
1772 display_card($card,$PREF["cardset"]);
1774 display_link_card($card,$PREF["cardset"]);
1777 else if($mystatus=='play' )
1779 echo "Your cards are: <br />\n";
1780 foreach($mycards as $card)
1781 display_card($card,$PREF["cardset"]);
1783 else if($mystatus=='gameover')
1785 $oldcards = DB_get_all_hand($me);
1786 $oldcards = mysort($oldcards,$gametype);
1787 echo "Your cards were: <br />\n";
1788 foreach($oldcards as $card)
1789 display_card($card,$PREF["cardset"]);
1791 $userids = DB_get_all_userid_by_gameid($gameid);
1792 foreach($userids as $user)
1794 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1800 $name = DB_get_name('userid',$user);
1801 $oldcards = DB_get_all_hand($userhash);
1802 $oldcards = mysort($oldcards,$gametype);
1803 echo "$name's cards were: <br />\n";
1804 foreach($oldcards as $card)
1805 display_card($card,$PREF["cardset"]);
1811 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1812 if($mystatus=='play')
1815 /* the following happens only when the gamestatus is 'gameover' */
1816 /* check if game is over, display results */
1817 if(DB_get_game_status_by_gameid($gameid)=='play')
1819 echo "The game is over for you.. other people still need to play though";
1823 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1824 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1825 " LEFT JOIN User ON User.id=Hand.user_id".
1826 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1827 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1828 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1829 " WHERE Hand.game_id='$gameid'".
1830 " GROUP BY Hand.party" );
1831 echo "<div class=\"total\"> Totals:<br />\n";
1832 while( $r = DB_fetch_array($result))
1833 echo " ".$r[0]." ".$r[1]."<br />\n";
1835 $queryresult = DB_query("SELECT timediff(mod_date,create_date) ".
1836 " FROM Game WHERE id='$gameid'");
1837 $r = DB_fetch_array($queryresult);
1838 echo "<p>This game took ".$r[0]." hours.</p>";
1840 echo "<div class=\"re\">\n Points Re: <br />\n";
1841 $queryresult = DB_query("SELECT score FROM Score ".
1842 " WHERE game_id=$gameid AND party='re'".
1844 while($r = DB_fetch_array($queryresult) )
1845 echo " ".$r[0]."<br />\n";
1848 echo "<div class=\"contra\">\n Points Contra: <br />\n";
1849 $queryresult = DB_query("SELECT score FROM Score ".
1850 " WHERE game_id=$gameid AND party='contra'".
1852 while($r = DB_fetch_array($queryresult) )
1853 echo " ".$r[0]."<br />\n";
1862 myerror("error in testing the status");
1864 /* output left menu */
1865 display_user_menu();
1867 /* output right menu */
1869 /* display rule set for this game */
1870 echo "<div class=\"gameinfo\">\n";
1872 if($gamestatus != 'pre')
1873 echo " Gametype: $GT <br />\n";
1875 echo "Rules: <br />\n";
1876 echo "10ofhearts : ".$RULES["dullen"] ."<br />\n";
1877 echo "schweinchen: ".$RULES["schweinchen"] ."<br />\n";
1878 echo "call: ".$RULES["call"] ."<br />\n";
1881 if($gamestatus == 'play' )
1882 output_form_calls($me);
1884 /* get time from the last action of the game */
1885 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1886 $gameend = time() - strtotime($r[0]);
1888 if($gamestatus == 'play' || $gameend < 60*60*24*7)
1890 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1894 echo "<input type=\"submit\" value=\"submit\" />\n<hr />\n";
1896 $session = DB_get_session_by_gameid($gameid);
1897 $score = generate_score_table($session);
1905 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1908 $session = DB_get_session_by_gameid($gameid);
1909 $result = DB_query("SELECT id,create_date FROM Game".
1910 " WHERE session=$session".
1911 " ORDER BY create_date DESC".
1915 $r = DB_fetch_array($result);
1917 if(!$session || $gameid==$r[0])
1919 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
1920 $names = DB_get_all_names_by_gameid($gameid);
1921 $type = DB_get_gametype_by_gameid($gameid);
1925 $solo = DB_get_solo_by_gameid($gameid);
1927 if($solo!='silent') /* repeat game with same first player */
1928 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
1929 else /* rotate normally */
1930 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
1932 else /* rotate normally */
1933 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);