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);
1133 /* check if we have card and that we haven't played it yet*/
1134 /* set played in hand_card to true where hand_id and card_id*/
1135 $r = DB_query_array("SELECT id FROM Hand_Card WHERE played='false' and ".
1136 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1137 $handcardid = $r[0];
1139 if($handcardid) /* everything ok, play card */
1141 /* update Game timestamp */
1142 DB_update_game_timestamp($gameid);
1144 /* check if a call was made, must do this before we set the card status to played */
1145 if(myisset("call") && $_REQUEST["call"] == "120" && can_call(120,$me))
1146 $result = DB_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
1147 if(myisset("call") && $_REQUEST["call"] == "90" && can_call(90,$me))
1148 $result = DB_query("UPDATE Hand SET point_call='90' WHERE hash='$me' ");
1149 if(myisset("call") && $_REQUEST["call"] == "60" && can_call(60,$me))
1150 $result = DB_query("UPDATE Hand SET point_call='60' WHERE hash='$me' ");
1151 if(myisset("call") && $_REQUEST["call"] == "30" && can_call(30,$me))
1152 $result = DB_query("UPDATE Hand SET point_call='30' WHERE hash='$me' ");
1153 if(myisset("call") && $_REQUEST["call"] == "0" && can_call(0,$me))
1154 $result = DB_query("UPDATE Hand SET point_call='0' WHERE hash='$me' ");
1156 /* mark card as played */
1157 DB_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1158 DB_quote_smart($card));
1160 /* get trick id or start new trick */
1161 $a = DB_get_current_trickid($gameid);
1166 $playid = DB_play_card($trickid,$handcardid,$sequence);
1168 /* check special output for schweinchen in case in case a fox is being played
1169 * check for correct rules, etc. has already been done
1171 if( $GAME["schweinchen-who"] && ($card == 19 || $card == 20) )
1173 if(!$GAME['schweinchen-first'])
1174 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1176 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1178 if( ($GAME['schweinchen-second']==1 && $RULES['schweinchen']=='second') || $RULES['schweinchen']=='both')
1180 DB_insert_comment("Schweinchen! ",$playid,$myid);
1181 $commentSchweinchen = "Schweinchen! ";
1184 echo "schweinchen = ".$GAME["schweinchen-who"]." ---<br />";
1187 /* if sequence == 4 check who one in case of wedding */
1188 if($sequence == 4 && $GT == "wedding")
1190 /* is wedding resolve */
1191 $resolved = DB_get_sickness_by_gameid($gameid);
1194 /* who has wedding */
1195 $userids = DB_get_all_userid_by_gameid($gameid);
1196 foreach($userids as $user)
1198 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1199 if($usersick == "wedding")
1202 /* who won the trick */
1203 $play = DB_get_cards_by_trick($trickid);
1204 $winner = get_winner($play,$gametype); /* returns the position */
1205 $winnerid = DB_get_userid('gameid-position',$gameid,$winner);
1206 /* is tricknr <=3 */
1207 if($tricknr <=3 && $winnerid!=$whosick)
1209 /* set resolved at tricknr*/
1210 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1212 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1213 DB_set_party_by_hash($whash,"re");
1215 if($tricknr == 3 && $winnerid==$whosick)
1217 /* set resolved at tricknr*/
1218 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1223 /* if sequence == 4, set winner of the trick, count points and set the next player */
1226 $play = DB_get_cards_by_trick($trickid);
1227 $winner = get_winner($play,$gametype); /* returns the position */
1229 /* check if someone caught a fox */
1230 /* first check if we should account for solos at all,
1231 * since it doesn't make sense in some games
1233 $ok = 0; /* fox shouldn't be counted */
1234 if(DB_get_gametype_by_gameid($gameid)=="solo")
1236 $solo = DB_get_solo_by_gameid($gameid);
1237 if($solo == 'trump' || $solo == 'silent')
1238 $ok = 1; /* for trump solos and silent solos, foxes are ok */
1241 $ok = 1; /* for all other games (not solos) foxes are ok too */
1244 foreach($play as $played)
1246 if ( $played['card']==19 || $played['card']==20 )
1247 if ($played['pos']!= $winner )
1249 /* possible caught a fox, check party */
1250 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1251 $uid2 = DB_get_userid('gameid-position',$gameid,$played['pos']);
1253 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1254 $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1256 if($party1 != $party2)
1257 DB_query("INSERT INTO Score".
1258 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1262 /* check for karlchen (jack of clubs in the last trick)*/
1263 /* same as for foxes, karlchen doesn't always make sense
1264 * check what kind of game it is and set karlchen accordingly */
1265 $ok = 1; /* default: karlchen should be accounted for */
1267 $ok = 0; /* Karlchen works only in the last trick */
1268 if($ok && DB_get_gametype_by_gameid($gameid)=="solo" )
1270 $solo = DB_get_solo_by_gameid($gameid);
1271 if($solo == "trumpless" || $solo == "jack" || $solo == "queen" )
1272 $ok = 0; /* no Karlchen in these solos */
1276 foreach($play as $played)
1277 if ( $played['card']==11 || $played['card']==12 )
1278 if ($played['pos'] == $winner )
1280 /* possible caught a fox, check party */
1281 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1282 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1284 DB_query("INSERT INTO Score".
1285 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1287 /* check for doppelopf (>40 points)*/
1289 foreach($play as $played)
1291 $points += DB_get_card_value_by_cardid($played['card']);
1295 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1296 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1298 DB_query("INSERT INTO Score".
1299 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
1303 DB_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1305 echo "ERROR during scoring";
1308 echo "DEBUG: position $winner won the trick <br />";
1310 /* who is the next player? */
1315 $next = DB_get_pos_by_hash($me)+1;
1317 if($next==5) $next=1;
1319 /* check for coment */
1320 if(myisset("comment"))
1322 $comment = $_REQUEST["comment"];
1324 DB_insert_comment($comment,$playid,$myid);
1325 if($commentSchweinchen)
1326 $comment = $commentSchweinchen . $comment;
1329 /* check for note */
1332 $note = $_REQUEST["note"];
1334 DB_insert_note($note,$gameid,$myid);
1337 /* display played card */
1338 $pos = DB_get_pos_by_hash($me);
1341 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1342 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1343 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1346 echo " <div class=\"card".($pos-1)."\">\n ";
1348 /* display comments */
1349 display_card($card,$PREF["cardset"]);
1351 echo "\n <span class=\"comment\"> ".$comment."</span>\n";
1354 /*check if we still have cards left, else set status to gameover */
1355 if(sizeof(DB_get_hand($me))==0)
1357 DB_set_hand_status_by_hash($me,'gameover');
1358 $mystatus = 'gameover';
1361 /* if all players are done, set game status to game over,
1362 * get the points of the last trick and send out an email
1365 $userids = DB_get_all_userid_by_gameid($gameid);
1368 foreach($userids as $user)
1369 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1373 DB_set_game_status_by_gameid($gameid,"gameover");
1375 /* email next player, if game is still running */
1376 if(DB_get_game_status_by_gameid($gameid)=='play')
1378 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1379 $email = DB_get_email('hash',$next_hash);
1380 $who = DB_get_userid('email',$email);
1381 DB_set_player_by_gameid($gameid,$who);
1383 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1384 "It's your turn now.\n".
1385 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$next_hash."\n\n" ;
1386 if( DB_get_email_pref_by_uid($who)!="emailaddict" )
1387 mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
1389 else /* send out final email */
1391 /* individual score */
1392 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1393 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1394 " LEFT JOIN User ON User.id=Hand.user_id".
1395 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1396 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1397 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1398 " WHERE Hand.game_id='$gameid'".
1399 " GROUP BY User.fullname" );
1400 $message = "The game is over. Thanks for playing :)\n";
1401 $message .= "Final score:\n";
1402 while( $r = DB_fetch_array($result) )
1403 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1405 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1406 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1407 " LEFT JOIN User ON User.id=Hand.user_id".
1408 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1409 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1410 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1411 " WHERE Hand.game_id='$gameid'".
1412 " GROUP BY Hand.party" );
1413 $message .= "\nTotals:\n";
1416 while( $r = DB_fetch_array($result) )
1418 $message .= " ".$r[0]." ".$r[1]."\n";
1421 else if($r[0] == "contra")
1426 * save score in database
1430 /* get calls from re/contra */
1432 $call_contra = NULL;
1433 foreach($userids as $user)
1435 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1436 $call = DB_get_call_by_hash($hash);
1437 $party = DB_get_party_by_hash($hash);
1441 $call = (int) $call;
1447 else if( $call < $call_re)
1450 else if($party=="contra")
1452 if($call_contra==NULL)
1453 $call_contra = $call;
1454 else if( $call < $call_re)
1455 $call_contra = $call;
1460 /* figure out who one */
1461 $winning_party = NULL;
1463 if($call_re == NULL && $call_contra==NULL)
1465 /* nobody made a call, so it's easy to figure out who won */
1467 $winning_party="re";
1469 $winning_party="contra";
1473 /* if one party makes a call, they only win, iff they make enough points
1474 * if only one party made a call, the other one wins,
1475 * if the first one didn't make it
1479 $offset = 120 - $call_re;
1481 $offset--; /* since we use a > in the next equation */
1483 if($re > 120+$offset)
1484 $winning_party="re";
1485 else if ($call_contra == NULL )
1486 $winning_party="contra";
1491 $offset = 120 - $call_contra;
1492 if($call_contra == 0)
1493 $offset--; /* since we use a > in the next equation */
1495 if($contra > 120+$offset)
1496 $winning_party="contra";
1497 else if ($call_re == NULL )
1498 $winning_party="re";
1502 /* one point for each call of the other party in case the other party didn't win
1503 * and one point each in case the party made more than points than one of the calls
1505 if($winning_party!="contra" && $call_contra!=NULL)
1507 for( $p=$call_contra;$p<=120; $p+=30 )
1509 DB_query("INSERT INTO Score".
1510 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
1513 for( $p=$call_contra; $p<120; $p+=30)
1516 DB_query("INSERT INTO Score".
1517 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'made$p')");
1520 if($winning_party!="re" and $call_re!=NULL)
1522 for( $p=$call_re;$p<=120; $p+=30 )
1524 DB_query("INSERT INTO Score".
1525 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'against$p')");
1528 for( $p=$call_re; $p<120; $p+=30)
1531 DB_query("INSERT INTO Score".
1532 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
1536 /* point in case contra won */
1537 if($winning_party=="contra")
1539 DB_query("INSERT INTO Score".
1540 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1543 /* one point each for winning and each 30 points + calls */
1544 if($winning_party=="re")
1546 foreach(array(120,150,180,210,240) as $p)
1549 if($p==240 || $call_contra!=NULL)
1553 DB_query("INSERT INTO Score".
1554 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
1556 /* re called something and won */
1557 foreach(array(0,30,60,90,120) as $p)
1559 if($call_re!=NULL && $call_re<$p+1)
1560 DB_query("INSERT INTO Score".
1561 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
1564 else if( $winning_party=="contra")
1566 foreach(array(120,150,180,210,240) as $p)
1569 if($p==240 || $call_re!=NULL)
1572 if($contra>$p-$offset)
1573 DB_query("INSERT INTO Score".
1574 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
1576 /* re called something and won */
1577 foreach(array(0,30,60,90,120) as $p)
1579 if($call_contra!=NULL && $call_contra<$p+1)
1580 DB_query("INSERT INTO Score".
1581 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
1586 /* add score points to email */
1589 $message .= " Points Re: \n";
1590 $queryresult = DB_query("SELECT score FROM Score ".
1591 " WHERE game_id=$gameid AND party='re'".
1593 while($r = DB_fetch_array($queryresult) )
1595 $message .= " ".$r[0]."\n";
1598 $message .= " Points Contra: \n";
1599 $queryresult = DB_query("SELECT score FROM Score ".
1600 " WHERE game_id=$gameid AND party='contra'".
1602 while($r = DB_fetch_array($queryresult) )
1604 $message .= " ".$r[0]."\n";
1607 $message .= " Total Points (from the Re point of view): $Tpoint\n";
1610 $session = DB_get_session_by_gameid($gameid);
1611 $score = generate_score_table($session);
1612 /* convert html to ascii */
1613 $score = str_replace("<div class=\"scoretable\">\n<table class=\"score\">\n <tr>\n","",$score);
1614 $score = str_replace("</table></div>\n","",$score);
1615 $score = str_replace("\n","",$score);
1616 $score = str_replace(array("<tr>","</tr>","<td>","</td>"),array("","\n","","|"),$score);
1617 $score = explode("\n",$score);
1619 $header = array_slice($score,0,1);
1620 $header = explode("|",$header[0]);
1621 for($i=0;$i<sizeof($header);$i++)
1622 $header[$i]=str_pad($header[$i],6," ",STR_PAD_BOTH);
1623 $header = implode("|",$header);
1624 $header.= "\n------+------+------+------+------+\n";
1625 if(sizeof($score)>5) $header.= " ... \n";
1627 if(sizeof($score)>5) $score = array_slice($score,-5,5);
1628 for($i=0;$i<sizeof($score);$i++)
1630 $line = explode("|",$score[$i]);
1631 for($j=0;$j<sizeof($line);$j++)
1632 $line[$j]=str_pad($line[$j],6," ",STR_PAD_LEFT);
1633 $score[$i] = implode("|",$line);
1636 $score = implode("\n",$score);
1637 $score = $header.$score;
1639 $message .= "Score Table:\n";
1642 /* send out final email */
1645 foreach($userids as $user)
1646 $all[] = DB_get_email('userid',$user);
1647 $To = implode(",",$all);
1649 $help = "\n\n (you can use reply all on this email to reach all the players.)\n";
1650 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 1(2)",$message.$help);
1652 foreach($userids as $user)
1654 $To = DB_get_email('userid',$user);
1655 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1657 $link = "Use this link to have a look at game ".DB_format_gameid($gameid).": ".
1658 $HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
1659 if( DB_get_email_pref_by_uid($user) != "emailaddict" )
1660 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 2(2)",$link);
1666 echo "can't find that card?! <br />\n";
1669 else if(myisset("card") && !$myturn )
1671 echo "please wait until it's your turn! <br />\n";
1674 if($seq!=4 && $trickNR>1)
1675 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1677 /* display points in case game is over */
1678 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1680 echo " <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1681 " <div class=\"trick\" id=\"trick13\">\n";
1682 /* add pic for re/contra
1683 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1685 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1686 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1687 " LEFT JOIN User ON User.id=Hand.user_id".
1688 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1689 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1690 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1691 " WHERE Hand.game_id='$gameid'".
1692 " GROUP BY User.fullname" );
1693 while( $r = DB_fetch_array($result))
1694 echo " <div class=\"card".($r[3]-1)."\">\n".
1695 " <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
1698 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1702 echo "</ul>\n"; /* end ul tricks*/
1704 echo "<div class=\"notes\"> Personal notes: <br />\n";
1705 $notes = DB_get_notes_by_userid_and_gameid($myid,$gameid);
1706 foreach($notes as $note)
1707 echo "$note <hr \>\n";
1708 echo "Insert note:<input name=\"note\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1711 $mycards = DB_get_hand($me);
1712 $mycards = mysort($mycards,$gametype);
1713 echo "<div class=\"mycards\">\n";
1715 if($myturn && !myisset("card") && $mystatus=='play' )
1717 echo "Hello ".$myname.", it's your turn! <br />\n";
1718 echo "Your cards are: <br />\n";
1720 /* do we have to follow suite? */
1722 if(have_suit($mycards,$firstcard))
1725 foreach($mycards as $card)
1727 if( ($followsuit && !same_type($card,$firstcard)) ||
1728 ( (int)($card)==19 && ($RULES['schweinchen']=='second'||$RULES['schweinchen']=='secondaftercall')
1729 && $GAME['schweinchen-who']==$me && !$GAME['schweinchen-first'] )
1731 display_card($card,$PREF["cardset"]);
1733 display_link_card($card,$PREF["cardset"]);
1736 else if($mystatus=='play' )
1738 echo "Your cards are: <br />\n";
1739 foreach($mycards as $card)
1740 display_card($card,$PREF["cardset"]);
1742 else if($mystatus=='gameover')
1744 $oldcards = DB_get_all_hand($me);
1745 $oldcards = mysort($oldcards,$gametype);
1746 echo "Your cards were: <br />\n";
1747 foreach($oldcards as $card)
1748 display_card($card,$PREF["cardset"]);
1750 $userids = DB_get_all_userid_by_gameid($gameid);
1751 foreach($userids as $user)
1753 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1759 $name = DB_get_name('userid',$user);
1760 $oldcards = DB_get_all_hand($userhash);
1761 $oldcards = mysort($oldcards,$gametype);
1762 echo "$name's cards were: <br />\n";
1763 foreach($oldcards as $card)
1764 display_card($card,$PREF["cardset"]);
1770 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1771 if($mystatus=='play')
1774 /* the following happens only when the gamestatus is 'gameover' */
1775 /* check if game is over, display results */
1776 if(DB_get_game_status_by_gameid($gameid)=='play')
1778 echo "The game is over for you.. other people still need to play though";
1782 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1783 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1784 " LEFT JOIN User ON User.id=Hand.user_id".
1785 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1786 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1787 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1788 " WHERE Hand.game_id='$gameid'".
1789 " GROUP BY Hand.party" );
1790 echo "<div class=\"total\"> Totals:<br />\n";
1791 while( $r = DB_fetch_array($result))
1792 echo " ".$r[0]." ".$r[1]."<br />\n";
1794 $queryresult = DB_query("SELECT timediff(mod_date,create_date) ".
1795 " FROM Game WHERE id='$gameid'");
1796 $r = DB_fetch_array($queryresult);
1797 echo "<p>This game took ".$r[0]." hours.</p>";
1799 echo "<div class=\"re\">\n Points Re: <br />\n";
1800 $queryresult = DB_query("SELECT score FROM Score ".
1801 " WHERE game_id=$gameid AND party='re'".
1803 while($r = DB_fetch_array($queryresult) )
1804 echo " ".$r[0]."<br />\n";
1807 echo "<div class=\"contra\">\n Points Contra: <br />\n";
1808 $queryresult = DB_query("SELECT score FROM Score ".
1809 " WHERE game_id=$gameid AND party='contra'".
1811 while($r = DB_fetch_array($queryresult) )
1812 echo " ".$r[0]."<br />\n";
1821 myerror("error in testing the status");
1823 /* output left menu */
1824 display_user_menu();
1826 /* output right menu */
1828 /* display rule set for this game */
1829 echo "<div class=\"gameinfo\">\n";
1831 if($gamestatus != 'pre')
1832 echo " Gametype: $GT <br />\n";
1834 echo "Rules: <br />\n";
1835 echo "10ofhearts : ".$RULES["dullen"] ."<br />\n";
1836 echo "schweinchen: ".$RULES["schweinchen"] ."<br />\n";
1837 echo "call: ".$RULES["call"] ."<br />\n";
1840 if($gamestatus == 'play' )
1841 output_form_calls($me);
1843 /* get time from the last action of the game */
1844 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1845 $gameend = time() - strtotime($r[0]);
1847 if($gamestatus == 'play' || $gameend < 60*60*24*7)
1849 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1853 echo "<input type=\"submit\" value=\"submit\" />\n<hr />\n";
1855 $session = DB_get_session_by_gameid($gameid);
1856 $score = generate_score_table($session);
1864 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1867 $session = DB_get_session_by_gameid($gameid);
1868 $result = DB_query("SELECT id,create_date FROM Game".
1869 " WHERE session=$session".
1870 " ORDER BY create_date DESC".
1874 $r = DB_fetch_array($result);
1876 if(!$session || $gameid==$r[0])
1878 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
1879 $names = DB_get_all_names_by_gameid($gameid);
1880 $type = DB_get_gametype_by_gameid($gameid);
1884 $solo = DB_get_solo_by_gameid($gameid);
1886 if($solo!='silent') /* repeat game with same first player */
1887 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
1888 else /* rotate normally */
1889 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
1891 else /* rotate normally */
1892 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);