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($_REQUEST["in"] == "no" && !$skip)
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 /* check if first schweinchen has been played */
1093 if( $GAME['schweinchen-who'] && ($r[0] == 19 || $r[0] == 20) )
1094 if(!$GAME['schweinchen-first'])
1095 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1097 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1099 /* save card to be able to find the winner of the trick later */
1100 $play[$seq] = array("card"=>$r[0],"pos"=>$pos);
1104 /* first card in a trick, output some html */
1105 if($trick!=$lasttrick)
1107 /* start of an old trick? */
1108 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1109 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1110 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1112 else if($trick==$lasttrick)
1114 /* start of a last trick? */
1115 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1116 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1117 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1120 /* remember first card, so that we are able to check, what cards can be played */
1125 echo " <div class=\"card".($pos-1)."\">\n";
1127 /* display comments */
1129 echo " <span class=\"comment\">".$comment."</span>\n";
1132 display_card($r[0],$PREF["cardset"]);
1134 echo " </div>\n"; /* end div card */
1140 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1144 /* whos turn is it? */
1147 $winner = get_winner($play,$gametype); /* returns the position */
1149 $firstcard = ""; /* new trick, no first card */
1154 if($next==5) $next = 1;
1157 /* my turn?, display cards as links, ask for comments*/
1158 if(DB_get_pos_by_hash($me) == $next)
1163 /* do we want to play a card? */
1164 if(myisset("card") && $myturn)
1166 $card = $_REQUEST["card"];
1167 $handid = DB_get_handid('hash',$me);
1168 $commentSchweinchen =""; /* used to add a comment when Schweinchen is being played */
1170 /* check if we have card and that we haven't played it yet*/
1171 /* set played in hand_card to true where hand_id and card_id*/
1172 $r = DB_query_array("SELECT id FROM Hand_Card WHERE played='false' and ".
1173 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1174 $handcardid = $r[0];
1176 if($handcardid) /* everything ok, play card */
1178 /* update Game timestamp */
1179 DB_update_game_timestamp($gameid);
1181 /* check if a call was made, must do this before we set the card status to played */
1182 if(myisset("call") && $_REQUEST["call"] == "120" && can_call(120,$me))
1183 $result = DB_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
1184 if(myisset("call") && $_REQUEST["call"] == "90" && can_call(90,$me))
1185 $result = DB_query("UPDATE Hand SET point_call='90' WHERE hash='$me' ");
1186 if(myisset("call") && $_REQUEST["call"] == "60" && can_call(60,$me))
1187 $result = DB_query("UPDATE Hand SET point_call='60' WHERE hash='$me' ");
1188 if(myisset("call") && $_REQUEST["call"] == "30" && can_call(30,$me))
1189 $result = DB_query("UPDATE Hand SET point_call='30' WHERE hash='$me' ");
1190 if(myisset("call") && $_REQUEST["call"] == "0" && can_call(0,$me))
1191 $result = DB_query("UPDATE Hand SET point_call='0' WHERE hash='$me' ");
1193 /* mark card as played */
1194 DB_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1195 DB_quote_smart($card));
1197 /* get trick id or start new trick */
1198 $a = DB_get_current_trickid($gameid);
1203 $playid = DB_play_card($trickid,$handcardid,$sequence);
1205 /* check special output for schweinchen in case in case a fox is being played
1206 * check for correct rules, etc. has already been done
1208 if( $GAME["schweinchen-who"] && ($card == 19 || $card == 20) )
1210 if(!$GAME['schweinchen-first'])
1211 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1213 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1215 if( ($GAME['schweinchen-second']==1 && $RULES['schweinchen']=='second') || $RULES['schweinchen']=='both')
1217 DB_insert_comment("Schweinchen! ",$playid,$myid);
1218 $commentSchweinchen = "Schweinchen! ";
1221 echo "schweinchen = ".$GAME["schweinchen-who"]." ---<br />";
1224 /* if sequence == 4 check who one in case of wedding */
1225 if($sequence == 4 && $GT == "wedding")
1227 /* is wedding resolve */
1228 $resolved = DB_get_sickness_by_gameid($gameid);
1231 /* who has wedding */
1232 $userids = DB_get_all_userid_by_gameid($gameid);
1233 foreach($userids as $user)
1235 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1236 if($usersick == "wedding")
1239 /* who won the trick */
1240 $play = DB_get_cards_by_trick($trickid);
1241 $winner = get_winner($play,$gametype); /* returns the position */
1242 $winnerid = DB_get_userid('gameid-position',$gameid,$winner);
1243 /* is tricknr <=3 */
1244 if($tricknr <=3 && $winnerid!=$whosick)
1246 /* set resolved at tricknr*/
1247 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1249 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1250 DB_set_party_by_hash($whash,"re");
1252 if($tricknr == 3 && $winnerid==$whosick)
1254 /* set resolved at tricknr*/
1255 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1260 /* if sequence == 4, set winner of the trick, count points and set the next player */
1263 $play = DB_get_cards_by_trick($trickid);
1264 $winner = get_winner($play,$gametype); /* returns the position */
1266 /* check if someone caught a fox */
1267 /* first check if we should account for solos at all,
1268 * since it doesn't make sense in some games
1270 $ok = 0; /* fox shouldn't be counted */
1271 if(DB_get_gametype_by_gameid($gameid)=="solo")
1273 $solo = DB_get_solo_by_gameid($gameid);
1274 if($solo == 'trump' || $solo == 'silent')
1275 $ok = 1; /* for trump solos and silent solos, foxes are ok */
1278 $ok = 1; /* for all other games (not solos) foxes are ok too */
1281 foreach($play as $played)
1283 if ( $played['card']==19 || $played['card']==20 )
1284 if ($played['pos']!= $winner )
1286 /* possible caught a fox, check party */
1287 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1288 $uid2 = DB_get_userid('gameid-position',$gameid,$played['pos']);
1290 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1291 $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1293 if($party1 != $party2)
1294 DB_query("INSERT INTO Score".
1295 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1299 /* check for karlchen (jack of clubs in the last trick)*/
1300 /* same as for foxes, karlchen doesn't always make sense
1301 * check what kind of game it is and set karlchen accordingly */
1302 $ok = 1; /* default: karlchen should be accounted for */
1304 $ok = 0; /* Karlchen works only in the last trick */
1305 if($ok && DB_get_gametype_by_gameid($gameid)=="solo" )
1307 $solo = DB_get_solo_by_gameid($gameid);
1308 if($solo == "trumpless" || $solo == "jack" || $solo == "queen" )
1309 $ok = 0; /* no Karlchen in these solos */
1313 foreach($play as $played)
1314 if ( $played['card']==11 || $played['card']==12 )
1315 if ($played['pos'] == $winner )
1317 /* possible caught a fox, check party */
1318 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1319 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1321 DB_query("INSERT INTO Score".
1322 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1324 /* check for doppelopf (>40 points)*/
1326 foreach($play as $played)
1328 $points += DB_get_card_value_by_cardid($played['card']);
1332 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1333 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1335 DB_query("INSERT INTO Score".
1336 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
1340 DB_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1342 echo "ERROR during scoring";
1345 echo "DEBUG: position $winner won the trick <br />";
1347 /* who is the next player? */
1352 $next = DB_get_pos_by_hash($me)+1;
1354 if($next==5) $next=1;
1356 /* check for coment */
1357 if(myisset("comment"))
1359 $comment = $_REQUEST["comment"];
1361 DB_insert_comment($comment,$playid,$myid);
1362 if($commentSchweinchen)
1363 $comment = $commentSchweinchen . $comment;
1366 /* check for note */
1369 $note = $_REQUEST["note"];
1371 DB_insert_note($note,$gameid,$myid);
1374 /* display played card */
1375 $pos = DB_get_pos_by_hash($me);
1378 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1379 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1380 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1383 echo " <div class=\"card".($pos-1)."\">\n ";
1385 /* display comments */
1386 display_card($card,$PREF["cardset"]);
1388 echo "\n <span class=\"comment\"> ".$comment."</span>\n";
1391 /*check if we still have cards left, else set status to gameover */
1392 if(sizeof(DB_get_hand($me))==0)
1394 DB_set_hand_status_by_hash($me,'gameover');
1395 $mystatus = 'gameover';
1398 /* if all players are done, set game status to game over,
1399 * get the points of the last trick and send out an email
1402 $userids = DB_get_all_userid_by_gameid($gameid);
1405 foreach($userids as $user)
1406 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1410 DB_set_game_status_by_gameid($gameid,"gameover");
1412 /* email next player, if game is still running */
1413 if(DB_get_game_status_by_gameid($gameid)=='play')
1415 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1416 $email = DB_get_email('hash',$next_hash);
1417 $who = DB_get_userid('email',$email);
1418 DB_set_player_by_gameid($gameid,$who);
1420 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1421 "It's your turn now.\n".
1422 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$next_hash."\n\n" ;
1423 if( DB_get_email_pref_by_uid($who)!="emailaddict" )
1424 mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
1426 else /* send out final email */
1428 /* individual score */
1429 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1430 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1431 " LEFT JOIN User ON User.id=Hand.user_id".
1432 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1433 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1434 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1435 " WHERE Hand.game_id='$gameid'".
1436 " GROUP BY User.fullname" );
1437 $message = "The game is over. Thanks for playing :)\n";
1438 $message .= "Final score:\n";
1439 while( $r = DB_fetch_array($result) )
1440 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1442 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1443 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1444 " LEFT JOIN User ON User.id=Hand.user_id".
1445 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1446 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1447 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1448 " WHERE Hand.game_id='$gameid'".
1449 " GROUP BY Hand.party" );
1450 $message .= "\nTotals:\n";
1453 while( $r = DB_fetch_array($result) )
1455 $message .= " ".$r[0]." ".$r[1]."\n";
1458 else if($r[0] == "contra")
1463 * save score in database
1467 /* get calls from re/contra */
1469 $call_contra = NULL;
1470 foreach($userids as $user)
1472 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1473 $call = DB_get_call_by_hash($hash);
1474 $party = DB_get_party_by_hash($hash);
1478 $call = (int) $call;
1484 else if( $call < $call_re)
1487 else if($party=="contra")
1489 if($call_contra==NULL)
1490 $call_contra = $call;
1491 else if( $call < $call_re)
1492 $call_contra = $call;
1497 /* figure out who one */
1498 $winning_party = NULL;
1500 if($call_re == NULL && $call_contra==NULL)
1502 /* nobody made a call, so it's easy to figure out who won */
1504 $winning_party="re";
1506 $winning_party="contra";
1510 /* if one party makes a call, they only win, iff they make enough points
1511 * if only one party made a call, the other one wins,
1512 * if the first one didn't make it
1516 $offset = 120 - $call_re;
1518 $offset--; /* since we use a > in the next equation */
1520 if($re > 120+$offset)
1521 $winning_party="re";
1522 else if ($call_contra == NULL )
1523 $winning_party="contra";
1528 $offset = 120 - $call_contra;
1529 if($call_contra == 0)
1530 $offset--; /* since we use a > in the next equation */
1532 if($contra > 120+$offset)
1533 $winning_party="contra";
1534 else if ($call_re == NULL )
1535 $winning_party="re";
1539 /* one point for each call of the other party in case the other party didn't win
1540 * and one point each in case the party made more than points than one of the calls
1542 if($winning_party!="contra" && $call_contra!=NULL)
1544 for( $p=$call_contra;$p<=120; $p+=30 )
1546 DB_query("INSERT INTO Score".
1547 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
1550 for( $p=$call_contra; $p<120; $p+=30)
1553 DB_query("INSERT INTO Score".
1554 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'made$p')");
1557 if($winning_party!="re" and $call_re!=NULL)
1559 for( $p=$call_re;$p<=120; $p+=30 )
1561 DB_query("INSERT INTO Score".
1562 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'against$p')");
1565 for( $p=$call_re; $p<120; $p+=30)
1568 DB_query("INSERT INTO Score".
1569 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
1573 /* point in case contra won */
1574 if($winning_party=="contra")
1576 DB_query("INSERT INTO Score".
1577 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1580 /* one point each for winning and each 30 points + calls */
1581 if($winning_party=="re")
1583 foreach(array(120,150,180,210,240) as $p)
1586 if($p==240 || $call_contra!=NULL)
1590 DB_query("INSERT INTO Score".
1591 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
1593 /* re called something and won */
1594 foreach(array(0,30,60,90,120) as $p)
1596 if($call_re!=NULL && $call_re<$p+1)
1597 DB_query("INSERT INTO Score".
1598 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
1601 else if( $winning_party=="contra")
1603 foreach(array(120,150,180,210,240) as $p)
1606 if($p==240 || $call_re!=NULL)
1609 if($contra>$p-$offset)
1610 DB_query("INSERT INTO Score".
1611 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
1613 /* re called something and won */
1614 foreach(array(0,30,60,90,120) as $p)
1616 if($call_contra!=NULL && $call_contra<$p+1)
1617 DB_query("INSERT INTO Score".
1618 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
1623 /* add score points to email */
1626 $message .= " Points Re: \n";
1627 $queryresult = DB_query("SELECT score FROM Score ".
1628 " WHERE game_id=$gameid AND party='re'".
1630 while($r = DB_fetch_array($queryresult) )
1632 $message .= " ".$r[0]."\n";
1635 $message .= " Points Contra: \n";
1636 $queryresult = DB_query("SELECT score FROM Score ".
1637 " WHERE game_id=$gameid AND party='contra'".
1639 while($r = DB_fetch_array($queryresult) )
1641 $message .= " ".$r[0]."\n";
1644 $message .= " Total Points (from the Re point of view): $Tpoint\n";
1647 $session = DB_get_session_by_gameid($gameid);
1648 $score = generate_score_table($session);
1649 /* convert html to ascii */
1650 $score = str_replace("<div class=\"scoretable\">\n<table class=\"score\">\n <tr>\n","",$score);
1651 $score = str_replace("</table></div>\n","",$score);
1652 $score = str_replace("\n","",$score);
1653 $score = str_replace(array("<tr>","</tr>","<td>","</td>"),array("","\n","","|"),$score);
1654 $score = explode("\n",$score);
1656 $header = array_slice($score,0,1);
1657 $header = explode("|",$header[0]);
1658 for($i=0;$i<sizeof($header);$i++)
1659 $header[$i]=str_pad($header[$i],6," ",STR_PAD_BOTH);
1660 $header = implode("|",$header);
1661 $header.= "\n------+------+------+------+------+\n";
1662 if(sizeof($score)>5) $header.= " ... \n";
1664 if(sizeof($score)>5) $score = array_slice($score,-5,5);
1665 for($i=0;$i<sizeof($score);$i++)
1667 $line = explode("|",$score[$i]);
1668 for($j=0;$j<sizeof($line);$j++)
1669 $line[$j]=str_pad($line[$j],6," ",STR_PAD_LEFT);
1670 $score[$i] = implode("|",$line);
1673 $score = implode("\n",$score);
1674 $score = $header.$score;
1676 $message .= "Score Table:\n";
1679 /* send out final email */
1682 foreach($userids as $user)
1683 $all[] = DB_get_email('userid',$user);
1684 $To = implode(",",$all);
1686 $help = "\n\n (you can use reply all on this email to reach all the players.)\n";
1687 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 1(2)",$message.$help);
1689 foreach($userids as $user)
1691 $To = DB_get_email('userid',$user);
1692 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1694 $link = "Use this link to have a look at game ".DB_format_gameid($gameid).": ".
1695 $HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
1696 if( DB_get_email_pref_by_uid($user) != "emailaddict" )
1697 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 2(2)",$link);
1703 echo "can't find that card?! <br />\n";
1706 else if(myisset("card") && !$myturn )
1708 echo "please wait until it's your turn! <br />\n";
1711 if($seq!=4 && $trickNR>=1)
1712 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1714 /* display points in case game is over */
1715 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1717 echo " <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1718 " <div class=\"trick\" id=\"trick13\">\n";
1719 /* add pic for re/contra
1720 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1722 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1723 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1724 " LEFT JOIN User ON User.id=Hand.user_id".
1725 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1726 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1727 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1728 " WHERE Hand.game_id='$gameid'".
1729 " GROUP BY User.fullname" );
1730 while( $r = DB_fetch_array($result))
1731 echo " <div class=\"card".($r[3]-1)."\">\n".
1732 " <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
1735 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1738 echo " <li onclick=\"hl_prev();\" class=\"old\"><a href=\"#\">prev</a></li>\n";
1739 echo " <li onclick=\"hl_next();\" class=\"old\"><a href=\"#\">next</a></li>\n</ul>\n"; /* end ul tricks*/
1741 echo "<div class=\"notes\"> Personal notes: <br />\n";
1742 $notes = DB_get_notes_by_userid_and_gameid($myid,$gameid);
1743 foreach($notes as $note)
1744 echo "$note <hr />\n";
1745 echo "Insert note:<input name=\"note\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1748 $mycards = DB_get_hand($me);
1749 $mycards = mysort($mycards,$gametype);
1750 echo "<div class=\"mycards\">\n";
1752 if($myturn && !myisset("card") && $mystatus=='play' )
1754 echo "Hello ".$myname.", it's your turn! <br />\n";
1755 echo "Your cards are: <br />\n";
1757 /* do we have to follow suite? */
1759 if(have_suit($mycards,$firstcard))
1762 foreach($mycards as $card)
1764 if( ($followsuit && !same_type($card,$firstcard)) ||
1765 ( (int)($card)==19 && ($RULES['schweinchen']=='second'||$RULES['schweinchen']=='secondaftercall')
1766 && $GAME['schweinchen-who']==$me && !$GAME['schweinchen-first'] )
1768 display_card($card,$PREF["cardset"]);
1770 display_link_card($card,$PREF["cardset"]);
1773 else if($mystatus=='play' )
1775 echo "Your cards are: <br />\n";
1776 foreach($mycards as $card)
1777 display_card($card,$PREF["cardset"]);
1779 else if($mystatus=='gameover')
1781 $oldcards = DB_get_all_hand($me);
1782 $oldcards = mysort($oldcards,$gametype);
1783 echo "Your cards were: <br />\n";
1784 foreach($oldcards as $card)
1785 display_card($card,$PREF["cardset"]);
1787 $userids = DB_get_all_userid_by_gameid($gameid);
1788 foreach($userids as $user)
1790 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1796 $name = DB_get_name('userid',$user);
1797 $oldcards = DB_get_all_hand($userhash);
1798 $oldcards = mysort($oldcards,$gametype);
1799 echo "$name's cards were: <br />\n";
1800 foreach($oldcards as $card)
1801 display_card($card,$PREF["cardset"]);
1807 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1808 if($mystatus=='play')
1811 /* the following happens only when the gamestatus is 'gameover' */
1812 /* check if game is over, display results */
1813 if(DB_get_game_status_by_gameid($gameid)=='play')
1815 echo "The game is over for you.. other people still need to play though";
1819 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1820 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1821 " LEFT JOIN User ON User.id=Hand.user_id".
1822 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1823 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1824 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1825 " WHERE Hand.game_id='$gameid'".
1826 " GROUP BY Hand.party" );
1827 echo "<div class=\"total\"> Totals:<br />\n";
1828 while( $r = DB_fetch_array($result))
1829 echo " ".$r[0]." ".$r[1]."<br />\n";
1831 $queryresult = DB_query("SELECT timediff(mod_date,create_date) ".
1832 " FROM Game WHERE id='$gameid'");
1833 $r = DB_fetch_array($queryresult);
1834 echo "<p>This game took ".$r[0]." hours.</p>";
1836 echo "<div class=\"re\">\n Points Re: <br />\n";
1837 $queryresult = DB_query("SELECT score FROM Score ".
1838 " WHERE game_id=$gameid AND party='re'".
1840 while($r = DB_fetch_array($queryresult) )
1841 echo " ".$r[0]."<br />\n";
1844 echo "<div class=\"contra\">\n Points Contra: <br />\n";
1845 $queryresult = DB_query("SELECT score FROM Score ".
1846 " WHERE game_id=$gameid AND party='contra'".
1848 while($r = DB_fetch_array($queryresult) )
1849 echo " ".$r[0]."<br />\n";
1858 myerror("error in testing the status");
1860 /* output left menu */
1861 display_user_menu();
1863 /* output right menu */
1865 /* display rule set for this game */
1866 echo "<div class=\"gameinfo\">\n";
1868 if($gamestatus != 'pre')
1869 echo " Gametype: $GT <br />\n";
1871 echo "Rules: <br />\n";
1872 echo "10ofhearts : ".$RULES["dullen"] ."<br />\n";
1873 echo "schweinchen: ".$RULES["schweinchen"] ."<br />\n";
1874 echo "call: ".$RULES["call"] ."<br />\n";
1877 if($gamestatus == 'play' )
1878 output_form_calls($me);
1880 /* get time from the last action of the game */
1881 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1882 $gameend = time() - strtotime($r[0]);
1884 if($gamestatus == 'play' || $gameend < 60*60*24*7)
1886 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1890 echo "<input type=\"submit\" value=\"submit\" />\n<hr />\n";
1892 $session = DB_get_session_by_gameid($gameid);
1893 $score = generate_score_table($session);
1901 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1904 $session = DB_get_session_by_gameid($gameid);
1905 $result = DB_query("SELECT id,create_date FROM Game".
1906 " WHERE session=$session".
1907 " ORDER BY create_date DESC".
1911 $r = DB_fetch_array($result);
1913 if(!$session || $gameid==$r[0])
1915 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
1916 $names = DB_get_all_names_by_gameid($gameid);
1917 $type = DB_get_gametype_by_gameid($gameid);
1921 $solo = DB_get_solo_by_gameid($gameid);
1923 if($solo!='silent') /* repeat game with same first player */
1924 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
1925 else /* rotate normally */
1926 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
1928 else /* rotate normally */
1929 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);