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 /* user might get here by clicking on the link in an email, so session might not be set */
30 if(isset($_SESSION["name"]))
31 output_status($_SESSION["name"]);
33 /* the user has done something, update the timestamp */
34 DB_update_user_timestamp($myid);
36 /* get some information from the DB */
37 $gameid = DB_get_gameid_by_hash($me);
38 $myname = DB_get_name('hash',$me);
39 $mystatus = DB_get_status_by_hash($me);
40 $mypos = DB_get_pos_by_hash($me);
41 $myhand = DB_get_handid('hash',$me);
42 $session = DB_get_session_by_gameid($gameid);
44 /* get prefs and save them in a variable*/
45 $PREF = DB_get_PREF($myid);
47 /* get rule set for this game */
48 $RULES = DB_get_RULES($gameid);
50 /* get some infos about the game */
51 $gametype = DB_get_gametype_by_gameid($gameid);
52 $gamestatus = DB_get_game_status_by_gameid($gameid);
56 $gametype = DB_get_solo_by_gameid($gameid);
57 $GT = $gametype." ".$GT;
60 /* do we need to worry about Schweinchen?
61 * check gametype and rules
62 * if yes, figure out if someone actually has Schweinchen
63 * save information in $GAME
66 if( $gamestatus == 'pre' )
68 /* always need to use Schweinchen to figure out for example who has poverty */
73 /* in a game Schweinchen is not valid in all types of games */
74 if( $gametype == 'normal' || $gametype == 'silent' || $gametype=='trump' )
75 if( in_array($RULES['schweinchen'],array('both','second','secondaftercall')) )
81 /* need to check for Schweinchen */
84 $hash = DB_get_hash_from_game_and_pos($gameid,$i);
85 $cards = DB_get_all_hand($hash);
86 if( in_array("19",$cards) && in_array("20",$cards) )
87 $GAME['schweinchen-who']=$hash;
89 $GAME['schweinchen-first'] = 0; /* to keep track if they have been played already */
90 $GAME['schweinchen-second'] = 0;
94 /* no need to check for Schweinchen */
95 $GAME['schweinchen-who']=NULL;
97 /* end check for Schweinchen */
99 set_gametype($gametype); /* this sets the $CARDS variable */
101 /* put everyting in a form */
102 echo "<form action=\"index.php?action=game&me=$me\" method=\"post\">\n";
106 /* output extra division in case this game is part of a session */
109 echo "<div class=\"session\">\n".
110 "This game is part of session $session: \n";
111 $hashes = DB_get_hashes_by_session($session,$myid);
113 foreach($hashes as $hash)
118 echo "<a href=\"".$INDEX."?action=game&me=".$hash."\">$i</a> \n";
124 /* display the table and the names */
127 /* mystatus gets the player through the different stages of a game.
128 * start: does the player want to play?
129 * init: check for sickness
130 * check: check for return values from init
131 * poverty: handle poverty, wait here until all player have reached this state
132 * display sickness and move on to game
133 * play: game in progress
134 * gameover: are we revisiting a game
141 /* asks the player, if he wants to join the game */
142 output_check_want_to_play($me);
147 /* check the result, if player wants to join, got next stage, else cancel game */
148 if($_REQUEST["in"] == "no")
150 /* cancel the game */
151 $message = "Hello, \n\n".
152 "the game has been canceled due to the request of one of the players.\n";
154 $userids = DB_get_all_userid_by_gameid($gameid);
155 foreach($userids as $user)
157 $To = DB_get_email('userid',$user);
158 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
161 /* delete everything from the dB */
167 /* user wants to join the game */
169 /* move on to the next stage,
170 * no break statement to immediately go to the next stage
173 DB_set_hand_status_by_hash($me,'init');
175 /* check if everyone has reached this stage, send out email */
176 $userids = DB_get_all_userid_by_gameid($gameid);
178 foreach($userids as $user)
180 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
181 if($userstat!='init')
183 /* whos turn is it? */
184 DB_set_player_by_gameid($gameid,$user);
190 /* all done, send out email unless this player is the startplayer */
191 $startplayer = DB_get_startplayer_by_gameid($gameid);
192 if($mypos == $startplayer)
194 /* do nothing, go to next stage */
198 /* email startplayer */
200 $email = DB_get_email('position-gameid',$startplayer,$gameid);
201 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
202 $who = DB_get_userid('email',$email);
203 DB_set_player_by_gameid($gameid,$who);
205 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
206 "Use this link to go the game: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
207 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
214 /* here we ask the player if he is sick */
215 $mycards = DB_get_hand($me);
216 $mycards = mysort($mycards,$gametype);
218 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
219 echo "\n<ul class=\"tricks\">\n";
220 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
221 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
222 " <div class=\"trick\" id=\"trick0\">\n";
224 for($pos=1;$pos<5;$pos++)
226 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
227 $userid = DB_get_userid('gameid-position',$gameid,$pos);
228 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
230 if($userstatus=='start' || $userstatus=='init')
231 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs to decide </div>\n"; /* show this to everyone */
233 if($usersick!=NULL && $pos<=$mypos ) /* only show this for people sitting before the player */
234 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
235 else if($usersick==NULL && $pos<=$mypos)
236 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
238 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
239 /* end displaying sickness */
241 if(!myisset("solo","wedding","poverty","nines") )
243 output_check_for_sickness($me,$mycards);
245 echo "<div class=\"mycards\">Your cards are: <br />\n";
246 foreach($mycards as $card)
247 display_card($card,$PREF["cardset"]);
254 /* check if someone selected more than one sickness */
256 if($_REQUEST["solo"]!="No") $Nsickness++;
257 if($_REQUEST["wedding"] == "yes") $Nsickness++;
258 if($_REQUEST["poverty"] == "yes") $Nsickness++;
259 if($_REQUEST["nines"] == "yes") $Nsickness++;
263 echo "<p class=\"message\"> You selected more than one sickness, please go back ".
264 "and answer the <a href=\"$INDEX?action=game&me=$me&in=yes\">question</a> again.</p>";
266 echo "<div class=\"mycards\">Your cards are: <br />\n";
267 foreach($mycards as $card)
268 display_card($card,$PREF["cardset"]);
275 /* everything is ok, save what user said and proceed */
276 echo "<p class=\"message\">Processing what you selected in the last step...";
278 /* check if this sickness needs to be handled first */
279 $gametype = DB_get_gametype_by_gameid($gameid);
280 $startplayer = DB_get_startplayer_by_gameid($gameid); /* need this to check which solo goes first */
282 if( $_REQUEST["solo"]!="No" )
284 /* user wants to play a solo */
286 /* store the info in the user's hand info */
287 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
288 DB_set_sickness_by_hash($me,"solo");
290 echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
292 if($gametype == "solo" && $startplayer<$mypos)
293 {}/* do nothing, since someone else already is playing solo */
296 /* this solo comes first
297 * store info in game table
299 DB_set_gametype_by_gameid($gameid,"solo");
300 DB_set_startplayer_by_gameid($gameid,$mypos);
301 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
304 else if($_REQUEST["wedding"] == "yes")
306 /* TODO: add silent solo somewhere*/
307 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
308 DB_set_sickness_by_hash($me,"wedding");
310 else if($_REQUEST["poverty"] == "yes")
312 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
313 DB_set_sickness_by_hash($me,"poverty");
315 else if($_REQUEST["nines"] == "yes")
317 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
318 " is playing solo, this game will be canceled.<br />\n";
319 DB_set_sickness_by_hash($me,"nines");
324 /* move on to the next stage*/
325 DB_set_hand_status_by_hash($me,'check');
330 /* here we check what all players said and figure out what game we are playing
331 * this can therefore only be handled once all players finished the last stage
334 /* only need to redisplay the cards when the user reloads the page and lands directly here */
335 if($mystatus=='check')
337 $mycards = DB_get_hand($me);
338 $mycards = mysort($mycards,$gametype);
340 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
341 echo "\n<ul class=\"tricks\">\n";
342 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
343 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
344 " <div class=\"trick\" id=\"trick0\">\n";
346 for($pos=1;$pos<5;$pos++)
348 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
349 $userid = DB_get_userid('gameid-position',$gameid,$pos);
350 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
352 if($userstatus=='start' || $userstatus=='init')
353 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs to decide </div>\n"; /* show this to everyone */
355 if($usersick!=NULL) /* in the init-phase we only showed players with $pos<$mypos, now we can show all */
356 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
358 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
360 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
361 /* end displaying sickness */
364 echo "<div class=\"message\">\n";
365 echo "<p> Checking if someone else selected solo, nines, wedding or poverty.</p>";
367 /* check if everyone has reached this stage */
368 $userids = DB_get_all_userid_by_gameid($gameid);
370 foreach($userids as $user)
372 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
373 if($userstat!='check')
376 DB_set_player_by_gameid($gameid,$user);
382 echo "<p>This step can only be handled after everyone finished the last step. ".
383 "Seems like this is not the case, so you need to wait a bit... ".
384 "you will get an email once that is the case, please use the link in ".
385 "that email to continue the game.</p></div>";
387 /* display cards, if player was just at the init-phase he will still see the cards from there
388 * we can put this one here, since the last player to finish the init state won't get here and
389 * will still see his card anyway from the init-phase
391 if($mystatus=='check')
394 echo "<div class=\"mycards\">Your cards are: <br />\n";
395 foreach($mycards as $card)
396 display_card($card,$PREF["cardset"]);
403 /* Ok, everyone finished the init-phase, time to figure out what game we
404 * are playing, in case there are any solos this already
405 * will have the correct information in it */
407 echo "<p> Ok, everyone is done... figuring out what kind of game we are playing.</p>";
409 $gametype = DB_get_gametype_by_gameid($gameid);
410 $startplayer = DB_get_startplayer_by_gameid($gameid);
412 /* check for sickness */
417 foreach($userids as $user)
419 $name = DB_get_name('userid',$user);
420 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
421 if($usersick == 'nines')
424 break; /* no need to check for other poverties, since only solo can win and that is already set */
426 else if($usersick == 'poverty')
428 else if($usersick == 'wedding')
430 else if($usersick == 'solo')
434 /* now check which sickness comes first and set the gametype to it */
435 if($gametype == "solo")
442 /* TODO: should we keep statistics of this? */
443 $message = "Hello, \n\n".
444 " the game has been canceled because ".DB_get_name('userid',$nines).
445 " has five or more nines and nobody is playing solo.\n\n".
446 " To redeal either start a new game or, in case the game was part of a tournament, \n".
447 " go to the last game and use the link at the bottom of the page to redeal.";
449 $userids = DB_get_all_userid_by_gameid($gameid);
450 foreach($userids as $user)
452 $To = DB_get_email('userid',$user);
453 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
456 /* delete everything from the dB */
459 echo "The game has been canceled because ".DB_get_name('userid',$nines).
460 " has five or more nines and nobody is playing solo.\n";
465 else if($poverty==1) /* one person has poverty */
467 DB_set_gametype_by_gameid($gameid,"poverty");
468 $gametype = "poverty";
469 $who = DB_get_sickness_by_gameid($gameid);
472 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
473 if($firstsick == "poverty")
474 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
476 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
479 else if($poverty==2) /* two people have poverty */
481 DB_set_gametype_by_gameid($gameid,"dpoverty");
482 $gametype = "dpoverty";
483 $who = DB_get_sickness_by_gameid($gameid);
486 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
487 if($firstsick == "poverty")
489 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
490 if($secondsick == "poverty")
491 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
493 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
496 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
501 DB_set_gametype_by_gameid($gameid,"wedding");
502 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
503 $gametype = "wedding";
505 /* now the gametype is set correctly in the database */
506 echo "<p> Got it :)</p>";
508 /* loop over all players, set re/contra if possible and start the game if possible */
509 $userids = DB_get_all_userid_by_gameid($gameid);
510 foreach($userids as $userid)
512 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
517 /* are we the solo player? set us to re, else set us to contra */
518 $pos = DB_get_pos_by_hash($userhash);
519 if($pos == $startplayer)
520 DB_set_party_by_hash($userhash,"re");
522 DB_set_party_by_hash($userhash,"contra");
523 DB_set_hand_status_by_hash($userhash,'play');
527 /* set person with the wedding to re, do the rest during the game */
528 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
529 if($usersick == "wedding")
530 DB_set_party_by_hash($userhash,"re");
532 DB_set_party_by_hash($userhash,"contra");
534 DB_set_hand_status_by_hash($userhash,'play');
538 $hand = DB_get_all_hand($userhash);
540 if(in_array('3',$hand)||in_array('4',$hand))
541 DB_set_party_by_hash($userhash,"re");
543 DB_set_party_by_hash($userhash,"contra");
544 DB_set_hand_status_by_hash($userhash,'play');
548 /* set person with poverty to play status */
549 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
550 if($usersick == "poverty")
551 DB_set_hand_status_by_hash($userhash,'play');
553 /* set status of first player to be asked to poverty */
554 $who = DB_get_sickness_by_gameid($gameid);
555 if($who > 6) $who= $who/10; /* in case we have dpoverty */
556 $whoid = DB_get_userid('gameid-position',$gameid,$who);
558 DB_set_hand_status_by_hash($userhash,'poverty');
561 /* check for silent solo, set game type to solo in this case */
562 $gametype = DB_get_gametype_by_gameid($gameid);
563 $userids = DB_get_all_userid_by_gameid($gameid);
564 foreach($userids as $userid)
566 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
568 if($gametype=='normal')
570 $userhand = DB_get_all_hand($userhash);
571 if(check_wedding($userhand))
573 /* normal game type and player has both queens -> silent solo */
574 /* keep startplayer, just set gametype to silent solo */
575 DB_set_gametype_by_gameid($gameid,"solo");
576 DB_set_solo_by_gameid($gameid,'silent');
581 /* send out email to first player or poverty person*/
582 if($gametype!="poverty" && $gametype!="dpoverty")
584 $startplayer = DB_get_startplayer_by_gameid($gameid);
585 $email = DB_get_email('position-gameid',$startplayer,$gameid);
586 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
587 $who = DB_get_userid('email',$email);
588 DB_set_player_by_gameid($gameid,$who);
592 if(DB_get_email_pref_by_hash($hash)!="emailaddict")
594 /* email startplayer */
595 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
596 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
597 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
601 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
605 /* set status of first player to be asked to poverty */
606 $who = DB_get_sickness_by_gameid($gameid);
607 if($who > 6) $who= $who/10; /* in case we have dpoverty */
609 $whoid = DB_get_userid('gameid-position',$gameid,$who);
611 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
614 $email = DB_get_email('position-gameid',$who,$gameid);
615 $whohash = DB_get_hash_from_game_and_pos($gameid,$who);
616 DB_set_player_by_gameid($gameid,$whoid);
618 if(DB_get_email_pref_by_hash($hash)!="emailaddict")
620 /* email player for poverty */
621 $message = "Poverty: It's your turn now in game ".DB_format_gameid($gameid).".\n".
622 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$whohash."\n\n" ;
623 mymail($email,$EmailName."Poverty (game ".DB_format_gameid($gameid).") ",$message);
631 /* user only gets here in a poverty game, several things have to be handled here:
632 * A) ask, if user wants to take trump
634 * poverty: set re/contra
635 * dpoverty: first time: set re, send email to second player
636 * second time: set contra
637 * set status to play in case 0 trump
638 * no -> set status to play,
639 * ask next player or cancle the game if no more players
640 * B) user took trump and has too many cards (e.g. count(cards)>12 and re/contra set)
641 * ask to give cards back, set status to play, once player has 12 cards
643 * it is easier to check B) first
646 /* output pre game in case user reloads */
649 set_gametype($gametype); /* this sets the $CARDS variable */
650 $myparty = DB_get_party_by_hash($me);
652 /* the following is part B) of whats needs to be done)
653 /* check if user wants to give cards back */
654 if(myisset("exchange"))
656 $exchange = $_REQUEST['exchange'];
657 $partnerhash = DB_get_partner_hash_by_hash($me);
658 $partnerid = DB_get_userid('hash',$partnerhash);
659 $partnerhand = DB_get_handid('gameid-userid',$gameid,$partnerid);
661 /* if exchange is set to a value>0, exchange that card back to the partner */
664 $result = DB_query("UPDATE Hand_Card SET hand_id='$partnerhand'".
665 " WHERE hand_id='$myhand' AND card_id=".DB_quote_smart($exchange));
669 $mycards = DB_get_hand($me);
670 $mycards = mysort($mycards,$gametype);
672 /* check if user need to give more cards back */
673 if( ($myparty=='re' || $myparty=='contra') && count($mycards)>12)
675 echo "<div class=\"poverty\"> you need to get rid of a few cards</div>\n";
678 echo "<div class=\"mycards\">Your cards are: <br />\n";
679 foreach($mycards as $card)
680 display_link_card($card,$PREF["cardset"],$type);
681 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select card to give back\" />\n";
684 else if( ($myparty=='re' || $myparty=='contra') && count($mycards)==12)
686 /* user is done, ready to play */
687 DB_set_hand_status_by_hash($me,'play');
688 /* TODO if resolved; email start player, set startplayer */
691 /* the following is part A) of what needs to be done */
692 if(!myisset("trump"))
696 echo "<div class=\"poverty\">\n";
697 $userids = DB_get_all_userid_by_gameid($gameid);
698 foreach($userids as $user)
700 $name = DB_get_name('userid',$user);
701 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
702 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
703 $userparty = DB_get_party_by_hash($userhash);
705 if($usersick=="poverty" && !$userparty)
707 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
708 $cards = DB_get_hand($hash);
711 foreach($cards as $card)
712 if($card<27) $nrtrump++;
713 echo "Player $name has $nrtrump trump. Do you want to take them?".
714 "<a href=\"index.php?action=game&me=$me&trump=$user\">yes</a> <br />\n";
717 echo "<a href=\"index.php?action=game&me=$me&trump=no\">No,way I take those trump...</a> <br />\n";
718 echo "</div><div>\n";
720 echo "<div class=\"mycards\">Your cards are: <br />\n";
721 foreach($mycards as $card)
722 display_card($card,$PREF["cardset"]);
723 echo "</div></div>\n";
729 $trump = $_REQUEST['trump'];
733 /* user doesn't want to take trump */
734 DB_set_hand_status_by_hash($me,'play');
736 /* set next player who needs to be asked and email him*/
737 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
738 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
740 /* don't ask people who have poverty */
742 if($firstsick=="poverty")
744 if($secondsick=="poverty")
749 if($gametype=="dpoverty")
751 $next=999; /* need to cancel for sure, since both would need to take the trump */
754 /* no more people to ask, need to cancel the game */
757 $message = "Hello, \n\n".
758 "Game ".DB_format_gameid($gameid)." has been canceled since nobody wanted to take the trump.\n";
760 $userids = DB_get_all_userid_by_gameid($gameid);
761 foreach($userids as $user)
763 $To = DB_get_email('userid',$user);
764 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled (poverty not resolved)",$message);
767 /* delete everything from the dB */
770 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been canceled.<br /><br /></p>";
777 /* email next player, set his status to poverty */
778 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
779 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
780 $userid = DB_get_userid('email',$To);
782 DB_set_player_by_gameid($gameid,$userid);
783 DB_set_hand_status_by_hash($userhash,'poverty');
785 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
786 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
787 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
792 /* player wants to take trump, change cards */
794 /* user wants to take trump */
795 $trump = $_REQUEST["trump"];
796 $userhand = DB_get_handid('gameid-userid',$gameid,$trump);
797 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
799 /* copy trump from player A to B */
800 $result = DB_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
802 $mycards = DB_get_hand($me);
805 if($gametype=='poverty')
807 $userids = DB_get_all_userid_by_gameid($gameid);
808 foreach($userids as $user)
810 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
811 if($hash==$userhash||$hash==$me)
812 DB_set_party_by_hash($hash,"re");
814 DB_set_party_by_hash($hash,"contra");
816 /* check if we are done, if so, send everyone into the 'play' phase */
817 if(count($mycards)==12)
819 foreach($userids as $user)
821 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
822 DB_set_hand_status_by_hash($hash,'play');
828 /* has the re party already been set?*/
830 $userids = DB_get_all_userid_by_gameid($gameid);
831 foreach($userids as $user)
833 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
834 $party = DB_get_party_by_hash($hash);
840 DB_set_party_by_hash($me,'contra');
841 DB_set_party_by_hash($userhash,'contra');
845 DB_set_party_by_hash($me,'re');
846 DB_set_party_by_hash($userhash,'re');
848 /* send out email to second non-poverty player */
849 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
850 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
853 if($firstsick=="poverty")
854 if($secondsick=="poverty")
860 echo "<div class=\"message\">Error in poverty, please contact the Admin</div>\n";
862 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
863 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
864 $userid = DB_get_userid('email',$To);
866 DB_set_player_by_gameid($gameid,$userid);
867 DB_set_hand_status_by_hash($userhash,'poverty');
869 $message = "Two people have poverty, it's your turn to decide, if you want to take the trump. Please visit:".
870 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
871 mymail($To,$EmailName." double poverty (game ".DB_format_gameid($gameid).")",$message);
876 echo "<div class=\"message\"> Please, <a href=\"$INDEX?action=game&me=$me\">continue</a> here.</div>\n";
884 /* both entries here, so that the tricks are visible for both.
885 * in case of 'play' there is a break later that skips the last part
888 /* check if all players are ready to play,
889 * if so, send out email to the startplayer
890 * only need to do this if the game hasn't started yet
892 $gamestatus = DB_get_game_status_by_gameid($gameid);
893 if($gamestatus == 'pre')
896 $userids = DB_get_all_userid_by_gameid($gameid);
897 foreach($userids as $user)
899 $userstatus = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
900 if($userstatus !='play' && $userstatus!='gameover')
903 DB_set_player_by_gameid($gameid,$user);
908 /* only set this after all poverty, etc. are handled*/
909 DB_set_game_status_by_gameid($gameid,'play');
911 /* email startplayer */
912 $startplayer = DB_get_startplayer_by_gameid($gameid);
913 $email = DB_get_email('position-gameid',$startplayer,$gameid);
914 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
915 $who = DB_get_userid('email',$email);
916 DB_set_player_by_gameid($gameid,$who);
918 if($hash!=$me && DB_get_email_pref_by_hash($hash)!="emailaddict")
920 /* email startplayer) */
921 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
922 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
923 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
927 /* figure out what kind of game we are playing,
928 * set the global variables $CARDS["trump"],$CARDS["diamonds"],$CARDS["hearts"],
929 * $CARDS["clubs"],$CARDS["spades"],$CARDS["foxes"]
933 $gametype = DB_get_gametype_by_gameid($gameid);
935 if($gametype=="solo")
937 $gametype = DB_get_solo_by_gameid($gameid);
938 if($gametype=='silent')
941 $GT = $gametype." ".$GT;
944 $gametype = "normal";
946 set_gametype($gametype); /* this sets the $CARDS variable */
948 /* get some infos about the game, need to reset this, since it might have changed */
949 $gamestatus = DB_get_game_status_by_gameid($gameid);
951 /* has the game started? No, then just wait here...*/
952 if($gamestatus == 'pre')
954 echo "<p class=\"message\"> You finished the setup, but not everyone else finished it... ".
955 "You need to wait for the others. Just wait for an email. </p>";
957 $mycards = DB_get_hand($me);
958 $mycards = mysort($mycards,$gametype);
960 echo "<div class=\"mycards\">Your cards are: <br />\n";
961 foreach($mycards as $card)
962 display_card($card,$PREF["cardset"]);
965 break; /* not sure this works... the idea is that you can
966 * only play a card after everyone is ready to play */
969 /* get time from the last action of the game */
970 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
971 $gameend = time() - strtotime($r[0]);
973 /* handle comments in case player didn't play a card, allow comments a week after the end of the game */
974 if( (!myisset("card") && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
975 if(myisset("comment"))
977 $comment = $_REQUEST["comment"];
978 $playid = DB_get_current_playid($gameid);
981 DB_insert_comment($comment,$playid,$myid);
984 /* handle notes in case player didn't play a card, allow notes only during a game */
985 if( (!myisset("card") && $mystatus=='play') )
988 $note = $_REQUEST["note"];
991 DB_insert_note($note,$gameid,$myid);
994 /* get everything relevant to display the tricks */
995 $result = DB_query("SELECT Hand_Card.card_id as card,".
996 " Hand.position as position,".
997 " Play.sequence as sequence, ".
999 " GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
1000 " SEPARATOR '\n' ), ".
1001 " Play.create_date, ".
1004 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1005 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1006 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1007 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1008 "LEFT JOIN User On User.id=Comment.user_id ".
1009 "WHERE Trick.game_id='".$gameid."' ".
1010 "GROUP BY Trick.id, sequence ".
1011 "ORDER BY Trick.id, sequence ASC");
1013 $lasttrick = DB_get_max_trickid($gameid);
1015 $play = array(); /* needed to calculate winner later */
1017 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1018 $firstcard = ""; /* first card in a trick */
1020 echo "\n<ul class=\"tricks\">\n";
1021 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
1023 /* output vorbehalte */
1024 $mygametype = DB_get_gametype_by_gameid($gameid);
1025 if($mygametype != 'normal' && $mygametype != 'silent') /* only show when needed */
1027 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
1028 " <div class=\"trick\" id=\"trick0\">\n";
1030 for($mypos=1;$mypos<5;$mypos++)
1032 $usersick = DB_get_sickness_by_pos_and_gameid($mypos,$gameid);
1035 echo " <div class=\"vorbehalt".($mypos-1)."\"> Vorbehalt <br />";
1037 echo " $usersick <br />";
1040 if($mygametype == $usersick)
1044 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1048 while($r = DB_fetch_array($result))
1056 /* check if first schweinchen has been played */
1057 if( $GAME['schweinchen-who'] && ($r[0] == 19 || $r[0] == 20) )
1058 if(!$GAME['schweinchen-first'])
1059 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1061 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1063 /* save card to be able to find the winner of the trick later */
1064 $play[$seq] = array("card"=>$r[0],"pos"=>$pos);
1068 /* first card in a trick, output some html */
1069 if($trick!=$lasttrick)
1071 /* start of an old trick? */
1072 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1073 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1074 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1076 else if($trick==$lasttrick)
1078 /* start of a last trick? */
1079 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1080 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1081 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1084 /* remember first card, so that we are able to check, what cards can be played */
1089 echo " <div class=\"card".($pos-1)."\">\n";
1091 /* display comments */
1093 echo " <span class=\"comment\">".$comment."</span>\n";
1096 display_card($r[0],$PREF["cardset"]);
1098 echo " </div>\n"; /* end div card */
1104 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1108 /* whos turn is it? */
1111 $winner = get_winner($play,$gametype); /* returns the position */
1113 $firstcard = ""; /* new trick, no first card */
1118 if($next==5) $next = 1;
1121 /* my turn?, display cards as links, ask for comments*/
1122 if(DB_get_pos_by_hash($me) == $next)
1127 /* do we want to play a card? */
1128 if(myisset("card") && $myturn)
1130 $card = $_REQUEST["card"];
1131 $handid = DB_get_handid('hash',$me);
1132 $commentSchweinchen =""; /* used to add a comment when Schweinchen is being played */
1134 /* check if we have card and that we haven't played it yet*/
1135 /* set played in hand_card to true where hand_id and card_id*/
1136 $r = DB_query_array("SELECT id FROM Hand_Card WHERE played='false' and ".
1137 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1138 $handcardid = $r[0];
1140 if($handcardid) /* everything ok, play card */
1142 /* update Game timestamp */
1143 DB_update_game_timestamp($gameid);
1145 /* check if a call was made, must do this before we set the card status to played */
1146 if(myisset("call") && $_REQUEST["call"] == "120" && can_call(120,$me))
1147 $result = DB_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
1148 if(myisset("call") && $_REQUEST["call"] == "90" && can_call(90,$me))
1149 $result = DB_query("UPDATE Hand SET point_call='90' WHERE hash='$me' ");
1150 if(myisset("call") && $_REQUEST["call"] == "60" && can_call(60,$me))
1151 $result = DB_query("UPDATE Hand SET point_call='60' WHERE hash='$me' ");
1152 if(myisset("call") && $_REQUEST["call"] == "30" && can_call(30,$me))
1153 $result = DB_query("UPDATE Hand SET point_call='30' WHERE hash='$me' ");
1154 if(myisset("call") && $_REQUEST["call"] == "0" && can_call(0,$me))
1155 $result = DB_query("UPDATE Hand SET point_call='0' WHERE hash='$me' ");
1157 /* mark card as played */
1158 DB_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1159 DB_quote_smart($card));
1161 /* get trick id or start new trick */
1162 $a = DB_get_current_trickid($gameid);
1167 $playid = DB_play_card($trickid,$handcardid,$sequence);
1169 /* check special output for schweinchen in case in case a fox is being played
1170 * check for correct rules, etc. has already been done
1172 if( $GAME["schweinchen-who"] && ($card == 19 || $card == 20) )
1174 if(!$GAME['schweinchen-first'])
1175 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1177 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1179 if( ($GAME['schweinchen-second']==1 && $RULES['schweinchen']=='second') || $RULES['schweinchen']=='both')
1181 DB_insert_comment("Schweinchen! ",$playid,$myid);
1182 $commentSchweinchen = "Schweinchen! ";
1185 echo "schweinchen = ".$GAME["schweinchen-who"]." ---<br />";
1188 /* if sequence == 4 check who one in case of wedding */
1189 if($sequence == 4 && $GT == "wedding")
1191 /* is wedding resolve */
1192 $resolved = DB_get_sickness_by_gameid($gameid);
1195 /* who has wedding */
1196 $userids = DB_get_all_userid_by_gameid($gameid);
1197 foreach($userids as $user)
1199 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1200 if($usersick == "wedding")
1203 /* who won the trick */
1204 $play = DB_get_cards_by_trick($trickid);
1205 $winner = get_winner($play,$gametype); /* returns the position */
1206 $winnerid = DB_get_userid('gameid-position',$gameid,$winner);
1207 /* is tricknr <=3 */
1208 if($tricknr <=3 && $winnerid!=$whosick)
1210 /* set resolved at tricknr*/
1211 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1213 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1214 DB_set_party_by_hash($whash,"re");
1216 if($tricknr == 3 && $winnerid==$whosick)
1218 /* set resolved at tricknr*/
1219 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1224 /* if sequence == 4, set winner of the trick, count points and set the next player */
1227 $play = DB_get_cards_by_trick($trickid);
1228 $winner = get_winner($play,$gametype); /* returns the position */
1230 /* check if someone caught a fox */
1231 /* first check if we should account for solos at all,
1232 * since it doesn't make sense in some games
1234 $ok = 0; /* fox shouldn't be counted */
1235 if(DB_get_gametype_by_gameid($gameid)=="solo")
1237 $solo = DB_get_solo_by_gameid($gameid);
1238 if($solo == 'trump' || $solo == 'silent')
1239 $ok = 1; /* for trump solos and silent solos, foxes are ok */
1242 $ok = 1; /* for all other games (not solos) foxes are ok too */
1245 foreach($play as $played)
1247 if ( $played['card']==19 || $played['card']==20 )
1248 if ($played['pos']!= $winner )
1250 /* possible caught a fox, check party */
1251 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1252 $uid2 = DB_get_userid('gameid-position',$gameid,$played['pos']);
1254 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1255 $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1257 if($party1 != $party2)
1258 DB_query("INSERT INTO Score".
1259 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1263 /* check for karlchen (jack of clubs in the last trick)*/
1264 /* same as for foxes, karlchen doesn't always make sense
1265 * check what kind of game it is and set karlchen accordingly */
1266 $ok = 1; /* default: karlchen should be accounted for */
1268 $ok = 0; /* Karlchen works only in the last trick */
1269 if($ok && DB_get_gametype_by_gameid($gameid)=="solo" )
1271 $solo = DB_get_solo_by_gameid($gameid);
1272 if($solo == "trumpless" || $solo == "jack" || $solo == "queen" )
1273 $ok = 0; /* no Karlchen in these solos */
1277 foreach($play as $played)
1278 if ( $played['card']==11 || $played['card']==12 )
1279 if ($played['pos'] == $winner )
1281 /* possible caught a fox, check party */
1282 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1283 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1285 DB_query("INSERT INTO Score".
1286 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1288 /* check for doppelopf (>40 points)*/
1290 foreach($play as $played)
1292 $points += DB_get_card_value_by_cardid($played['card']);
1296 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1297 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1299 DB_query("INSERT INTO Score".
1300 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
1304 DB_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1306 echo "ERROR during scoring";
1309 echo "DEBUG: position $winner won the trick <br />";
1311 /* who is the next player? */
1316 $next = DB_get_pos_by_hash($me)+1;
1318 if($next==5) $next=1;
1320 /* check for coment */
1321 if(myisset("comment"))
1323 $comment = $_REQUEST["comment"];
1325 DB_insert_comment($comment,$playid,$myid);
1326 if($commentSchweinchen)
1327 $comment = $commentSchweinchen . $comment;
1330 /* check for note */
1333 $note = $_REQUEST["note"];
1335 DB_insert_note($note,$gameid,$myid);
1338 /* display played card */
1339 $pos = DB_get_pos_by_hash($me);
1342 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1343 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1344 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1347 echo " <div class=\"card".($pos-1)."\">\n ";
1349 /* display comments */
1350 display_card($card,$PREF["cardset"]);
1352 echo "\n <span class=\"comment\"> ".$comment."</span>\n";
1355 /*check if we still have cards left, else set status to gameover */
1356 if(sizeof(DB_get_hand($me))==0)
1358 DB_set_hand_status_by_hash($me,'gameover');
1359 $mystatus = 'gameover';
1362 /* if all players are done, set game status to game over,
1363 * get the points of the last trick and send out an email
1366 $userids = DB_get_all_userid_by_gameid($gameid);
1369 foreach($userids as $user)
1370 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1374 DB_set_game_status_by_gameid($gameid,"gameover");
1376 /* email next player, if game is still running */
1377 if(DB_get_game_status_by_gameid($gameid)=='play')
1379 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1380 $email = DB_get_email('hash',$next_hash);
1381 $who = DB_get_userid('email',$email);
1382 DB_set_player_by_gameid($gameid,$who);
1384 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1385 "It's your turn now.\n".
1386 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$next_hash."\n\n" ;
1387 if( DB_get_email_pref_by_uid($who)!="emailaddict" )
1388 mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
1390 else /* send out final email */
1392 /* individual score */
1393 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1394 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1395 " LEFT JOIN User ON User.id=Hand.user_id".
1396 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1397 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1398 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1399 " WHERE Hand.game_id='$gameid'".
1400 " GROUP BY User.fullname" );
1401 $message = "The game is over. Thanks for playing :)\n";
1402 $message .= "Final score:\n";
1403 while( $r = DB_fetch_array($result) )
1404 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1406 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1407 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1408 " LEFT JOIN User ON User.id=Hand.user_id".
1409 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1410 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1411 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1412 " WHERE Hand.game_id='$gameid'".
1413 " GROUP BY Hand.party" );
1414 $message .= "\nTotals:\n";
1417 while( $r = DB_fetch_array($result) )
1419 $message .= " ".$r[0]." ".$r[1]."\n";
1422 else if($r[0] == "contra")
1427 * save score in database
1431 /* get calls from re/contra */
1433 $call_contra = NULL;
1434 foreach($userids as $user)
1436 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1437 $call = DB_get_call_by_hash($hash);
1438 $party = DB_get_party_by_hash($hash);
1442 $call = (int) $call;
1448 else if( $call < $call_re)
1451 else if($party=="contra")
1453 if($call_contra==NULL)
1454 $call_contra = $call;
1455 else if( $call < $call_re)
1456 $call_contra = $call;
1461 /* figure out who one */
1462 $winning_party = NULL;
1464 if($call_re == NULL && $call_contra==NULL)
1466 /* nobody made a call, so it's easy to figure out who won */
1468 $winning_party="re";
1470 $winning_party="contra";
1474 /* if one party makes a call, they only win, iff they make enough points
1475 * if only one party made a call, the other one wins,
1476 * if the first one didn't make it
1480 $offset = 120 - $call_re;
1482 $offset--; /* since we use a > in the next equation */
1484 if($re > 120+$offset)
1485 $winning_party="re";
1486 else if ($call_contra == NULL )
1487 $winning_party="contra";
1492 $offset = 120 - $call_contra;
1493 if($call_contra == 0)
1494 $offset--; /* since we use a > in the next equation */
1496 if($contra > 120+$offset)
1497 $winning_party="contra";
1498 else if ($call_re == NULL )
1499 $winning_party="re";
1503 /* one point for each call of the other party in case the other party didn't win
1504 * and one point each in case the party made more than points than one of the calls
1506 if($winning_party!="contra" && $call_contra!=NULL)
1508 for( $p=$call_contra;$p<=120; $p+=30 )
1510 DB_query("INSERT INTO Score".
1511 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
1514 for( $p=$call_contra; $p<120; $p+=30)
1517 DB_query("INSERT INTO Score".
1518 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'made$p')");
1521 if($winning_party!="re" and $call_re!=NULL)
1523 for( $p=$call_re;$p<=120; $p+=30 )
1525 DB_query("INSERT INTO Score".
1526 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'against$p')");
1529 for( $p=$call_re; $p<120; $p+=30)
1532 DB_query("INSERT INTO Score".
1533 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
1537 /* point in case contra won */
1538 if($winning_party=="contra")
1540 DB_query("INSERT INTO Score".
1541 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1544 /* one point each for winning and each 30 points + calls */
1545 if($winning_party=="re")
1547 foreach(array(120,150,180,210,240) as $p)
1550 if($p==240 || $call_contra!=NULL)
1554 DB_query("INSERT INTO Score".
1555 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
1557 /* re called something and won */
1558 foreach(array(0,30,60,90,120) as $p)
1560 if($call_re!=NULL && $call_re<$p+1)
1561 DB_query("INSERT INTO Score".
1562 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
1565 else if( $winning_party=="contra")
1567 foreach(array(120,150,180,210,240) as $p)
1570 if($p==240 || $call_re!=NULL)
1573 if($contra>$p-$offset)
1574 DB_query("INSERT INTO Score".
1575 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
1577 /* re called something and won */
1578 foreach(array(0,30,60,90,120) as $p)
1580 if($call_contra!=NULL && $call_contra<$p+1)
1581 DB_query("INSERT INTO Score".
1582 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
1587 /* add score points to email */
1590 $message .= " Points Re: \n";
1591 $queryresult = DB_query("SELECT score FROM Score ".
1592 " WHERE game_id=$gameid AND party='re'".
1594 while($r = DB_fetch_array($queryresult) )
1596 $message .= " ".$r[0]."\n";
1599 $message .= " Points Contra: \n";
1600 $queryresult = DB_query("SELECT score FROM Score ".
1601 " WHERE game_id=$gameid AND party='contra'".
1603 while($r = DB_fetch_array($queryresult) )
1605 $message .= " ".$r[0]."\n";
1608 $message .= " Total Points (from the Re point of view): $Tpoint\n";
1611 $session = DB_get_session_by_gameid($gameid);
1612 $score = generate_score_table($session);
1613 /* convert html to ascii */
1614 $score = str_replace("<div class=\"scoretable\">\n<table class=\"score\">\n <tr>\n","",$score);
1615 $score = str_replace("</table></div>\n","",$score);
1616 $score = str_replace("\n","",$score);
1617 $score = str_replace(array("<tr>","</tr>","<td>","</td>"),array("","\n","","|"),$score);
1618 $score = explode("\n",$score);
1620 $header = array_slice($score,0,1);
1621 $header = explode("|",$header[0]);
1622 for($i=0;$i<sizeof($header);$i++)
1623 $header[$i]=str_pad($header[$i],6," ",STR_PAD_BOTH);
1624 $header = implode("|",$header);
1625 $header.= "\n------+------+------+------+------+\n";
1626 if(sizeof($score)>5) $header.= " ... \n";
1628 if(sizeof($score)>5) $score = array_slice($score,-5,5);
1629 for($i=0;$i<sizeof($score);$i++)
1631 $line = explode("|",$score[$i]);
1632 for($j=0;$j<sizeof($line);$j++)
1633 $line[$j]=str_pad($line[$j],6," ",STR_PAD_LEFT);
1634 $score[$i] = implode("|",$line);
1637 $score = implode("\n",$score);
1638 $score = $header.$score;
1640 $message .= "Score Table:\n";
1643 /* send out final email */
1646 foreach($userids as $user)
1647 $all[] = DB_get_email('userid',$user);
1648 $To = implode(",",$all);
1650 $help = "\n\n (you can use reply all on this email to reach all the players.)\n";
1651 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 1(2)",$message.$help);
1653 foreach($userids as $user)
1655 $To = DB_get_email('userid',$user);
1656 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1658 $link = "Use this link to have a look at game ".DB_format_gameid($gameid).": ".
1659 $HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
1660 if( DB_get_email_pref_by_uid($user) != "emailaddict" )
1661 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 2(2)",$link);
1667 echo "can't find that card?! <br />\n";
1670 else if(myisset("card") && !$myturn )
1672 echo "please wait until it's your turn! <br />\n";
1675 if($seq!=4 && $trickNR>1)
1676 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1678 /* display points in case game is over */
1679 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1681 echo " <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1682 " <div class=\"trick\" id=\"trick13\">\n";
1683 /* add pic for re/contra
1684 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1686 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1687 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1688 " LEFT JOIN User ON User.id=Hand.user_id".
1689 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1690 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1691 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1692 " WHERE Hand.game_id='$gameid'".
1693 " GROUP BY User.fullname" );
1694 while( $r = DB_fetch_array($result))
1695 echo " <div class=\"card".($r[3]-1)."\">\n".
1696 " <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
1699 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1703 echo "</ul>\n"; /* end ul tricks*/
1705 echo "<div class=\"notes\"> Personal notes: <br />\n";
1706 $notes = DB_get_notes_by_userid_and_gameid($myid,$gameid);
1707 foreach($notes as $note)
1708 echo "$note <hr \>\n";
1709 echo "Insert note:<input name=\"note\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1712 $mycards = DB_get_hand($me);
1713 $mycards = mysort($mycards,$gametype);
1714 echo "<div class=\"mycards\">\n";
1716 if($myturn && !myisset("card") && $mystatus=='play' )
1718 echo "Hello ".$myname.", it's your turn! <br />\n";
1719 echo "Your cards are: <br />\n";
1721 /* do we have to follow suite? */
1723 if(have_suit($mycards,$firstcard))
1726 foreach($mycards as $card)
1728 if( ($followsuit && !same_type($card,$firstcard)) ||
1729 ( (int)($card)==19 && ($RULES['schweinchen']=='second'||$RULES['schweinchen']=='secondaftercall')
1730 && $GAME['schweinchen-who']==$me && !$GAME['schweinchen-first'] )
1732 display_card($card,$PREF["cardset"]);
1734 display_link_card($card,$PREF["cardset"]);
1737 else if($mystatus=='play' )
1739 echo "Your cards are: <br />\n";
1740 foreach($mycards as $card)
1741 display_card($card,$PREF["cardset"]);
1743 else if($mystatus=='gameover')
1745 $oldcards = DB_get_all_hand($me);
1746 $oldcards = mysort($oldcards,$gametype);
1747 echo "Your cards were: <br />\n";
1748 foreach($oldcards as $card)
1749 display_card($card,$PREF["cardset"]);
1751 $userids = DB_get_all_userid_by_gameid($gameid);
1752 foreach($userids as $user)
1754 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1760 $name = DB_get_name('userid',$user);
1761 $oldcards = DB_get_all_hand($userhash);
1762 $oldcards = mysort($oldcards,$gametype);
1763 echo "$name's cards were: <br />\n";
1764 foreach($oldcards as $card)
1765 display_card($card,$PREF["cardset"]);
1771 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1772 if($mystatus=='play')
1775 /* the following happens only when the gamestatus is 'gameover' */
1776 /* check if game is over, display results */
1777 if(DB_get_game_status_by_gameid($gameid)=='play')
1779 echo "The game is over for you.. other people still need to play though";
1783 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1784 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1785 " LEFT JOIN User ON User.id=Hand.user_id".
1786 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1787 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1788 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1789 " WHERE Hand.game_id='$gameid'".
1790 " GROUP BY Hand.party" );
1791 echo "<div class=\"total\"> Totals:<br />\n";
1792 while( $r = DB_fetch_array($result))
1793 echo " ".$r[0]." ".$r[1]."<br />\n";
1795 $queryresult = DB_query("SELECT timediff(mod_date,create_date) ".
1796 " FROM Game WHERE id='$gameid'");
1797 $r = DB_fetch_array($queryresult);
1798 echo "<p>This game took ".$r[0]." hours.</p>";
1800 echo "<div class=\"re\">\n Points Re: <br />\n";
1801 $queryresult = DB_query("SELECT score FROM Score ".
1802 " WHERE game_id=$gameid AND party='re'".
1804 while($r = DB_fetch_array($queryresult) )
1805 echo " ".$r[0]."<br />\n";
1808 echo "<div class=\"contra\">\n Points Contra: <br />\n";
1809 $queryresult = DB_query("SELECT score FROM Score ".
1810 " WHERE game_id=$gameid AND party='contra'".
1812 while($r = DB_fetch_array($queryresult) )
1813 echo " ".$r[0]."<br />\n";
1822 myerror("error in testing the status");
1824 /* output left menu */
1825 display_user_menu();
1827 /* output right menu */
1829 /* display rule set for this game */
1830 echo "<div class=\"gameinfo\">\n";
1832 if($gamestatus != 'pre')
1833 echo " Gametype: $GT <br />\n";
1835 echo "Rules: <br />\n";
1836 echo "10ofhearts : ".$RULES["dullen"] ."<br />\n";
1837 echo "schweinchen: ".$RULES["schweinchen"] ."<br />\n";
1838 echo "call: ".$RULES["call"] ."<br />\n";
1841 if($gamestatus == 'play' )
1842 output_form_calls($me);
1844 /* get time from the last action of the game */
1845 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1846 $gameend = time() - strtotime($r[0]);
1848 if($gamestatus == 'play' || $gameend < 60*60*24*7)
1850 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1854 echo "<input type=\"submit\" value=\"submit\" />\n<hr />\n";
1856 $session = DB_get_session_by_gameid($gameid);
1857 $score = generate_score_table($session);
1865 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1868 $session = DB_get_session_by_gameid($gameid);
1869 $result = DB_query("SELECT id,create_date FROM Game".
1870 " WHERE session=$session".
1871 " ORDER BY create_date DESC".
1875 $r = DB_fetch_array($result);
1877 if(!$session || $gameid==$r[0])
1879 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
1880 $names = DB_get_all_names_by_gameid($gameid);
1881 $type = DB_get_gametype_by_gameid($gameid);
1885 $solo = DB_get_solo_by_gameid($gameid);
1887 if($solo!='silent') /* repeat game with same first player */
1888 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
1889 else /* rotate normally */
1890 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
1892 else /* rotate normally */
1893 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);