2 /* Copyright 2006, 2007, 2008, 2009, 2010 Arun Persaud <arun@nubati.net>
4 * This file is part of e-DoKo.
6 * e-DoKo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * e-DoKo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with e-DoKo. If not, see <http://www.gnu.org/licenses/>.
21 /* make sure that we are not called from outside the scripts,
22 * use a variable defined in config.php to check this
27 /* calling game.php only makes sense when we give it a hash for a game */
30 echo "Hmm, you really shouldn't mess with the urls.<br />\n";
33 $me = $_REQUEST['me'];
35 /* Ok, got a hash, but is it valid? */
36 $myid = DB_get_userid('hash',$me);
39 echo "Can't find you in the database, please check the url.<br />\n";
40 echo "perhaps the game has been canceled, check by login in <a href=\"$INDEX\">here</a>.";
44 global $GAME,$RULES,$CARDS;
46 /* get some information from the DB */
47 $gameid = DB_get_gameid_by_hash($me);
48 $myname = DB_get_name('hash',$me);
49 $mystatus = DB_get_status_by_hash($me);
50 $mypos = DB_get_pos_by_hash($me);
51 $myhand = DB_get_handid('hash',$me);
52 $myparty = DB_get_party_by_hash($me);
53 $session = DB_get_session_by_gameid($gameid);
55 /* get prefs and save them in a variable*/
56 $PREF = DB_get_PREF(isset($_SESSION['id'])?$_SESSION['id']:$myid);
58 /* get rule set for this game */
59 $RULES = DB_get_RULES($gameid);
61 /* get some infos about the game */
62 $gametype = DB_get_gametype_by_gameid($gameid);
63 $gamestatus = DB_get_game_status_by_gameid($gameid);
67 $gametype = DB_get_solo_by_gameid($gameid);
68 $GT = $gametype.' '.$GT;
71 /* do we need to worry about Schweinchen?
72 * check gametype and rules
73 * if yes, figure out if someone actually has Schweinchen
74 * save information in $GAME
77 if( $gamestatus == 'pre' )
79 /* always need to use Schweinchen to figure out for example who has poverty */
84 /* in a game Schweinchen is not valid in all types of games */
85 if( in_array($gametype,array('normal','wedding','trump','silent') ))
86 if( in_array($RULES['schweinchen'],array('both','second','secondaftercall')) )
90 /* these are the defaults */
91 $GAME['schweinchen-who'] = NULL;
92 $GAME['schweinchen-first'] = NULL;
93 $GAME['schweinchen-second'] = NULL;
97 /* need to check for Schweinchen */
100 $hash = DB_get_hash_from_game_and_pos($gameid,$i);
101 $cards = DB_get_all_hand($hash);
102 if( in_array('19',$cards) && in_array('20',$cards) )
103 $GAME['schweinchen-who']=$hash;
105 $GAME['schweinchen-first'] = 0; /* to keep track if they have been played already */
106 $GAME['schweinchen-second'] = 0;
108 /* end check for Schweinchen */
110 /* set the $CARDS variable, needed for sorting the cards
111 * we set it to normal so that the pre-game phase is handled ok
112 * and later set it to the correct game type that is played
114 set_gametype('normal');
116 /* put everyting in a form */
117 echo "<form action=\"index.php?action=game&me=$me\" method=\"post\">\n";
119 /* handle user notes (only possible while game is running)*/
120 if( $mystatus!='gameover' )
123 $note = $_REQUEST['note'];
126 DB_insert_note($note,$gameid,$myid);
129 /* make sure that we don't show the notes to the wrong person
130 * (e.g. other people looking at an old game)
132 if( $mystatus != 'gameover' ||
133 ( $mystatus == 'gameover' &&
134 isset($_SESSION['id']) &&
135 $myid == $_SESSION['id']))
136 output_user_notes($myid,$gameid,$mystatus);
138 /* handle calls, output a comment to show when the call was made */
139 /* initialize comments */
142 /* check for calls, set comment */
143 if(myisset('call') && $_REQUEST['call'] == '120' && can_call(120,$me))
145 $result = DB_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
148 else if($myparty=='contra')
149 $comment .= "Contra";
151 if(myisset('call') && $_REQUEST['call'] == '90' && can_call(90,$me))
153 $result = DB_query("UPDATE Hand SET point_call='90' WHERE hash='$me' ");
156 if(myisset('call') && $_REQUEST['call'] == '60' && can_call(60,$me))
158 $result = DB_query("UPDATE Hand SET point_call='60' WHERE hash='$me' ");
161 if(myisset('call') && $_REQUEST['call'] == '30' && can_call(30,$me))
163 $result = DB_query("UPDATE Hand SET point_call='30' WHERE hash='$me' ");
166 if(myisset('call') && $_REQUEST['call'] == '0' && can_call(0,$me))
168 $result = DB_query("UPDATE Hand SET point_call='0' WHERE hash='$me' ");
172 /* get information needed to submit comment */
173 $playid = DB_get_current_playid($gameid);
177 DB_insert_comment($comment,$playid,$myid);
180 /* end check for calls */
183 /* output extra division in case this game is part of a session */
186 echo "<div class=\"session\">\n";
187 echo " <div class=\"sessionrules\">Rules: ";
188 switch($RULES['dullen'])
191 echo " <img class=\"rulesicon\" alt=\"not ten of hearts\" src=\"pics/button/no-ten-of-hearts.png\"/>\n"; break;
193 echo " <img class=\"rulesicon\" alt=\"ten of hearts\" src=\"pics/button/ten-of-hearts.png\"/>\n"; break;
195 echo " <img class=\"rulesicon\" alt=\"second ten of hearts\" src=\"pics/button/second-ten-of-hearts.png\"/>\n"; break;
197 switch($RULES['schweinchen'])
200 echo " <img class=\"rulesicon\" alt=\"no schweinchen\" ".
201 "src=\"pics/button/no-schweinchen.png\"/>\n"; break;
203 echo " <img class=\"rulesicon\" alt=\"two schweinchen \" ".
204 "src=\"pics/button/two-schweinchen.png\"/>\n"; break;
206 echo " <img class=\"rulesicon\" alt=\"second schweinchen\" ".
207 "src=\"pics/button/second-schweinchen.png\"/>\n"; break;
208 case 'secondaftercall':
209 echo " <img class=\"rulesicon\" alt=\"second schweinchen after call\" ".
210 "src=\"pics/button/second-schweinchen-after-call.png\"/>\n"; break;
212 switch($RULES['call'])
215 echo " <img class=\"rulesicon\" alt=\"1st-own-card\" src=\"pics/button/1st-own-card.png\"/>\n"; break;
217 echo " <img class=\"rulesicon\" alt=\"5th-card\" src=\"pics/button/5th-card.png\"/>\n"; break;
219 echo " <img class=\"rulesicon\" alt=\"9-cards\" src=\"pics/button/9-cards.png\"/>\n"; break;
222 echo " 10ofhearts : {$RULES['dullen']} <br />\n";
223 echo " schweinchen: {$RULES['schweinchen']} <br />\n";
224 echo " call: {$RULES['call']} <br />\n";
225 echo " </div>\n </div>\n";
229 echo " <div class=\"sessionscore\">";
231 $score = generate_score_table($session);
233 /* get the last entry to show on the main page */
235 $finalscore = array_pop($tmpscore);
236 $finalscore = $finalscore['players'];
241 foreach($finalscore as $user=>$value)
243 $name = DB_get_name('userid',$user);
244 echo " ".substr($name,0,2).": $value ";
249 /* first game, no score yet */
253 /* output all games for the score table */
254 echo format_score_table_html($score,$myid);
257 /* figure out which game in a session we are in and link to the
258 * previous and next game if possible
260 $hashes = DB_get_hashes_by_session($session,$myid);
263 foreach($hashes as $hash)
273 $previous = $hashes[$j-2];
281 if(isset($_SESSION['id']) && $_SESSION['id']==$myid)
284 echo "<a href=\"{$INDEX}?action=game&me=$previous\">previous game</a> \n";
285 echo "This is game number $j of <a href=\"{$INDEX}?action=game&me=$lasthash\">$i</a> in session $session.\n";
287 echo " <a href=\"{$INDEX}?action=game&me=$next\">next game</a> \n";
290 echo "This is game number $j of $i in session $session.";
294 /* display the table and the names */
297 /* mystatus gets the player through the different stages of a game.
298 * start: does the player want to play?
299 * init: check for sickness
300 * check: check for return values from init
301 * poverty: handle poverty, wait here until all player have reached this state
302 * display sickness and move on to game
303 * play: game in progress
304 * gameover: are we revisiting a game
307 /* the user has done something, update the timestamp. Use $myid in
308 * active games and check for session-id in old games (myid might be wrong in that case)
310 if($mystatus!='gameover')
311 DB_update_user_timestamp($myid);
313 if(isset($_SESSION['id']))
314 DB_update_user_timestamp($_SESSION['id']);
319 /* don't ask if user has autosetup set to yest */
321 if($PREF['autosetup']=='yes') $skip = 1;
323 if( !myisset('in') && !$skip)
325 /* asks the player, if he wants to join the game */
326 output_check_want_to_play($me);
331 /* check the result, if player wants to join, got next stage, else cancel game */
332 if(!$skip && $_REQUEST['in'] == 'no' )
334 /* cancel the game */
335 $message = "Hello, \n\n".
336 "the game has been canceled due to the request of one of the players.\n\n";
338 $userids = DB_get_all_userid_by_gameid($gameid);
339 foreach($userids as $user)
341 $subject = 'Game '.DB_format_gameid($gameid).' canceled';
342 mymail($user,$subject,$message);
345 /* update game status */
346 cancel_game('noplay',$gameid);
351 /* user wants to join the game */
353 /* move on to the next stage,
354 * no break statement to immediately go to the next stage
357 DB_set_hand_status_by_hash($me,'init');
359 /* check if everyone has reached this stage, send out email */
360 $userids = DB_get_all_userid_by_gameid($gameid);
362 foreach($userids as $user)
364 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
365 if($userstat!='init')
367 /* whos turn is it? */
368 DB_set_player_by_gameid($gameid,$user);
375 /* all done, send out email unless this player is the startplayer */
376 $startplayer = DB_get_startplayer_by_gameid($gameid);
377 if($mypos == $startplayer)
379 /* do nothing, go to next stage */
383 /* email startplayer */
385 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
386 $who = DB_get_userid('hash',$hash);
387 DB_set_player_by_gameid($gameid,$who);
389 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
390 "Use this link to go the game: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
391 mymail($who,"Ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
398 /* here we ask the player if he is sick */
399 $mycards = DB_get_hand($me);
400 $mycards = mysort($mycards,$gametype);
402 if(!myisset('solo','wedding','poverty','nines') )
404 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
405 echo "\n<ul class=\"tricks\">\n";
406 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
407 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
408 " <div class=\"trick\" id=\"trick0\">\n";
410 for($pos=1;$pos<5;$pos++)
412 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
413 $userid = DB_get_userid('gameid-position',$gameid,$pos);
414 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
416 if($userstatus=='start' || $userstatus=='init')
417 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs <br/ > to decide </div>\n"; /* show this to everyone */
419 if($usersick!=NULL && $pos<=$mypos ) /* only show this for people sitting before the player */
420 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
421 else if($usersick==NULL && $pos<=$mypos)
422 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
424 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
425 /* end displaying sickness */
427 output_check_for_sickness($me,$mycards);
429 echo "<div class=\"mycards\">Your cards are: <br />\n";
430 foreach($mycards as $card)
431 display_card($card,$PREF['cardset']);
438 /* check if someone selected more than one sickness */
440 if($_REQUEST['solo']!='No') $Nsickness++;
441 if($_REQUEST['wedding'] == 'yes') $Nsickness++;
442 if($_REQUEST['poverty'] == 'yes') $Nsickness++;
443 if($_REQUEST['nines'] == 'yes') $Nsickness++;
447 echo "<p class=\"message\"> You selected more than one sickness, please go back ".
448 "and answer the <a href=\"$INDEX?action=game&me=$me&in=yes\">question</a> again.</p>";
450 echo "<div class=\"mycards\">Your cards are: <br />\n";
451 foreach($mycards as $card)
452 display_card($card,$PREF['cardset']);
459 /* everything is ok, save what user said and proceed */
460 echo "<p class=\"message\">Processing what you selected in the last step...";
462 /* check if this sickness needs to be handled first */
463 $gametype = DB_get_gametype_by_gameid($gameid);
464 $startplayer = DB_get_startplayer_by_gameid($gameid); /* need this to check which solo goes first */
466 if( $_REQUEST['solo']!='No' )
468 /* user wants to play a solo */
470 /* store the info in the user's hand info */
471 DB_set_solo_by_hash($me,$_REQUEST['solo']);
472 DB_set_sickness_by_hash($me,'solo');
474 echo "<br />Seems like you want to play a {$_REQUEST['solo']} solo. Got it.<br />\n";
476 if($gametype == 'solo' && $startplayer<$mypos)
477 {}/* do nothing, since someone else already is playing solo */
480 /* this solo comes first
481 * store info in game table
483 DB_set_gametype_by_gameid($gameid,'solo');
484 DB_set_startplayer_by_gameid($gameid,$mypos);
485 DB_set_solo_by_gameid($gameid,$_REQUEST['solo']);
488 else if($_REQUEST['wedding'] == 'yes')
490 /* silent solo is set further down */
491 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
492 DB_set_sickness_by_hash($me,'wedding');
494 else if($_REQUEST['poverty'] == 'yes')
496 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
497 DB_set_sickness_by_hash($me,'poverty');
499 else if($_REQUEST['nines'] == 'yes')
501 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
502 " is playing solo, this game will be canceled.<br />\n";
503 DB_set_sickness_by_hash($me,'nines');
508 /* move on to the next stage*/
509 DB_set_hand_status_by_hash($me,'check');
514 /* here we check what all players said and figure out what game we are playing
515 * this can therefore only be handled once all players finished the last stage
518 $mycards = DB_get_hand($me);
519 $mycards = mysort($mycards,$gametype);
521 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
522 echo "\n<ul class=\"tricks\">\n";
523 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
524 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
525 " <div class=\"trick\" id=\"trick0\">\n";
527 for($pos=1;$pos<5;$pos++)
529 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
530 $userid = DB_get_userid('gameid-position',$gameid,$pos);
531 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
533 if($userstatus=='start' || $userstatus=='init')
534 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs <br />to decide </div>\n"; /* show this to everyone */
536 if($usersick!=NULL) /* in the init-phase we only showed players with $pos<$mypos, now we can show all */
537 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
539 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
541 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
542 /* end displaying sickness */
544 echo "<div class=\"message\">\n";
545 echo "<p> Checking if someone else selected solo, nines, wedding or poverty.</p>";
547 /* check if everyone has reached this stage */
548 $userids = DB_get_all_userid_by_gameid($gameid);
550 foreach($userids as $user)
552 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
553 if($userstat!='check')
556 DB_set_player_by_gameid($gameid,$user);
563 echo "<p>This step can only be handled after everyone finished the last step. ".
564 "Seems like this is not the case, so you need to wait a bit... ".
565 "you will get an email once that is the case, please use the link in ".
566 "that email to continue the game.</p></div>";
568 /* display cards, if player was just at the init-phase he will still see the cards from there
569 * we can put this one here, since the last player to finish the init state won't get here and
570 * will still see his card anyway from the init-phase
572 if($mystatus=='check')
575 echo "<div class=\"mycards\">Your cards are: <br />\n";
576 foreach($mycards as $card)
577 display_card($card,$PREF['cardset']);
584 /* Ok, everyone finished the init-phase, time to figure out what game we
585 * are playing, in case there are any solos this already
586 * will have the correct information in it */
588 echo "<p> Ok, everyone is done... figuring out what kind of game we are playing.</p>";
590 $gametype = DB_get_gametype_by_gameid($gameid);
591 $startplayer = DB_get_startplayer_by_gameid($gameid);
593 /* check for sickness */
598 foreach($userids as $user)
600 $name = DB_get_name('userid',$user);
601 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
602 if($usersick == 'nines')
605 break; /* no need to check for other poverties, since only solo can win and that is already set */
607 else if($usersick == 'poverty')
609 else if($usersick == 'wedding')
611 else if($usersick == 'solo')
615 /* now check which sickness comes first and set the gametype to it */
616 if($gametype == 'solo')
623 $message = "The game has been canceled because ".DB_get_name('userid',$nines).
624 " has five or more nines and nobody is playing solo.\n\n".
625 "To redeal either start a new game or, in case the game was part of a tournament,\n".
626 "go to the last game and use the link at the bottom of the page to redeal.\n\n";
628 $userids = DB_get_all_userid_by_gameid($gameid);
629 foreach($userids as $user)
631 $subject = 'Game '.DB_format_gameid($gameid).' canceled';
632 mymail($user,$subject,$message);
635 /* update game status */
636 cancel_game('nines',$gameid);
638 echo "<p>The game has been canceled because ".DB_get_name('userid',$nines).
639 " has five or more nines and nobody is playing solo.</p>\n";
643 else if($poverty==1) /* one person has poverty */
645 DB_set_gametype_by_gameid($gameid,'poverty');
646 $gametype = 'poverty';
647 $who = DB_get_sickness_by_gameid($gameid);
650 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
651 if($firstsick == 'poverty')
652 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
654 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
657 else if($poverty==2) /* two people have poverty */
659 DB_set_gametype_by_gameid($gameid,'dpoverty');
660 $gametype = 'dpoverty';
661 $who = DB_get_sickness_by_gameid($gameid);
664 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
665 if($firstsick == 'poverty')
667 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
668 if($secondsick == 'poverty')
669 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
671 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
674 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
679 DB_set_gametype_by_gameid($gameid,'wedding');
680 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
681 $gametype = 'wedding';
683 /* now the gametype is set correctly in the database */
684 echo "<p> Got it :)</p>";
686 /* loop over all players, set re/contra if possible and start the game if possible */
687 $userids = DB_get_all_userid_by_gameid($gameid);
688 foreach($userids as $userid)
690 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
695 /* are we the solo player? set us to re, else set us to contra */
696 $pos = DB_get_pos_by_hash($userhash);
697 if($pos == $startplayer)
698 DB_set_party_by_hash($userhash,'re');
700 DB_set_party_by_hash($userhash,'contra');
701 DB_set_hand_status_by_hash($userhash,'play');
705 /* set person with the wedding to re, do the rest during the game */
706 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
707 if($usersick == 'wedding')
708 DB_set_party_by_hash($userhash,'re');
710 DB_set_party_by_hash($userhash,'contra');
712 DB_set_hand_status_by_hash($userhash,'play');
716 $hand = DB_get_all_hand($userhash);
718 if(in_array('3',$hand)||in_array('4',$hand))
719 DB_set_party_by_hash($userhash,'re');
721 DB_set_party_by_hash($userhash,'contra');
722 DB_set_hand_status_by_hash($userhash,'play');
726 /* set person with poverty to play status */
727 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
728 if($usersick == 'poverty')
729 DB_set_hand_status_by_hash($userhash,'play');
731 /* set status of first player to be asked to poverty */
732 $who = DB_get_sickness_by_gameid($gameid);
733 if($who > 6) $who= $who/10; /* in case we have dpoverty */
734 $whoid = DB_get_userid('gameid-position',$gameid,$who);
736 DB_set_hand_status_by_hash($userhash,'poverty');
739 /* check for silent solo, set game type to solo in this case */
740 $gametype = DB_get_gametype_by_gameid($gameid);
741 $userids = DB_get_all_userid_by_gameid($gameid);
742 foreach($userids as $userid)
744 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
746 if($gametype=='normal')
748 $userhand = DB_get_all_hand($userhash);
749 if(check_wedding($userhand))
751 /* normal game type and player has both queens -> silent solo */
752 /* keep startplayer, just set gametype to silent solo */
753 DB_set_gametype_by_gameid($gameid,'solo');
754 DB_set_solo_by_gameid($gameid,'silent');
759 /* send out email to first player or poverty person*/
760 if($gametype!='poverty' && $gametype!='dpoverty')
762 $startplayer = DB_get_startplayer_by_gameid($gameid);
763 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
764 $who = DB_get_userid('hash',$hash);
765 DB_set_player_by_gameid($gameid,$who);
769 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
771 /* email startplayer */
772 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
773 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
774 $subject = 'Ready, set, go... (game '.DB_format_gameid($gameid).')';
775 mymail($who,$subject,$message);
779 echo "<div class=\"message\">Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.</div>\n";
783 /* set status of first player to be asked to poverty */
784 $who = DB_get_sickness_by_gameid($gameid);
785 if($who > 6) $who= $who/10; /* in case we have dpoverty */
787 $whoid = DB_get_userid('gameid-position',$gameid,$who);
789 echo "<div class=\"message\">Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.</div>\n";
792 $whohash = DB_get_hash_from_game_and_pos($gameid,$who);
793 DB_set_player_by_gameid($gameid,$whoid);
795 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
797 /* email player for poverty */
798 $message = "Poverty: It's your turn now in game ".DB_format_gameid($gameid).".\n".
799 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$whohash."\n\n" ;
800 $subject = 'Poverty (game '.DB_format_gameid($gameid).') ';
801 mymail($whoid,$subject,$message);
809 /* user only gets here in a poverty game, several things have to be handled here:
810 * A) ask, if user wants to take trump
812 * poverty: set re/contra
813 * dpoverty: first time: set re, send email to second player
814 * second time: set contra
815 * poverty: set status of other players to 'play'
816 * set status to play in case 0 trump
817 * no -> set status to play,
818 * ask next player or cancle the game if no more players
819 * B) user took trump and has too many cards (e.g. count(cards)>12 and re/contra set)
820 * ask to give cards back, set status to play, once player has 12 cards
822 * it is easier to check B) first
825 set_gametype($gametype); /* this sets the $CARDS variable */
826 $myparty = DB_get_party_by_hash($me);
828 /* the following is part B) of whats needs to be done)
829 /* check if user wants to give cards back */
830 if(myisset('exchange'))
832 $exchange = $_REQUEST['exchange'];
833 $partnerhash = DB_get_partner_hash_by_hash($me);
834 $partnerid = DB_get_userid('hash',$partnerhash);
835 $partnerhand = DB_get_handid('gameid-userid',$gameid,$partnerid);
837 /* if exchange is set to a value>0, exchange that card back to the partner */
840 $result = DB_query("UPDATE Hand_Card SET hand_id='$partnerhand'".
841 " WHERE hand_id='$myhand' AND card_id=".DB_quote_smart($exchange));
842 DB_add_exchanged_card(DB_quote_smart($exchange),$myhand,$partnerhand);
847 $mycards = DB_get_hand($me);
848 $mycards = mysort($mycards,$gametype);
850 /* output pre-game trick in case user reloads,
851 * only needs to be done when a team has been formed */
852 if($myparty=='re' || $myparty=='contra')
854 echo "\n<ul class=\"tricks\">\n";
855 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
857 $mygametype = DB_get_gametype_by_gameid($gameid);
859 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
860 " <div class=\"trick\" id=\"trick0\">\n";
862 /* get information so show the cards that have been handed over in a poverty game */
863 output_exchanged_cards();
865 echo " </div>\n </li>\n"; /* end div trick, end li trick */
867 /* end output pre-game trick */
869 /* check if user need to give more cards back */
870 if( ($myparty=='re' || $myparty=='contra') && count($mycards)>12)
872 echo "<div class=\"poverty\"> you need to get rid of a few cards</div>\n";
875 echo "<div class=\"mycards\">Your cards are: <br />\n";
876 foreach($mycards as $card)
877 display_link_card($card,$PREF['cardset'],$type);
878 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select card to give back\" />\n";
881 else if( ($myparty=='re' || $myparty=='contra') && count($mycards)==12)
883 /* user is done, ready to play */
884 DB_set_hand_status_by_hash($me,'play');
886 /* email start player */
887 $startplayer = DB_get_startplayer_by_gameid($gameid);
888 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
889 $who = DB_get_userid('hash',$hash);
890 DB_set_player_by_gameid($gameid,$who);
894 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
896 /* email startplayer */
897 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
898 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
899 $subject = 'Ready, set, go... (game '.DB_format_gameid($gameid).') ';
900 mymail($who,$subject,$message);
904 echo "<div class=\"message\">Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.</div>\n";
907 /* the following is part A) of what needs to be done */
908 if(!myisset('trump'))
912 echo "<div class=\"poverty\">\n";
913 $userids = DB_get_all_userid_by_gameid($gameid);
914 foreach($userids as $user)
916 $name = DB_get_name('userid',$user);
917 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
918 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
919 $userparty = DB_get_party_by_hash($userhash);
921 if($usersick=='poverty' && !$userparty)
923 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
924 $cards = DB_get_hand($hash);
927 foreach($cards as $card)
928 if($card<27) $nrtrump++;
929 echo "Player $name has $nrtrump trump. Do you want to take them?".
930 "<a href=\"index.php?action=game&me=$me&trump=$user\">Yes</a> <br />\n";
933 echo "<a href=\"index.php?action=game&me=$me&trump=no\">No way</a> <br />\n";
934 echo "</div><div>\n";
936 echo "<div class=\"mycards\">Your cards are: <br />\n";
937 foreach($mycards as $card)
938 display_card($card,$PREF['cardset']);
939 echo "</div></div>\n";
945 $trump = $_REQUEST['trump'];
949 /* user doesn't want to take trump */
950 DB_set_hand_status_by_hash($me,'play');
952 /* set next player who needs to be asked and email him*/
953 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
954 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
956 /* don't ask people who have poverty */
958 if($firstsick=='poverty')
960 if($secondsick=='poverty')
965 if($gametype=='dpoverty')
967 $next=999; /* need to cancel for sure, since both would need to take the trump */
970 /* no more people to ask, need to cancel the game */
973 $message = "Hello, \n\n".
974 "Game ".DB_format_gameid($gameid)." has been canceled since nobody wanted to take the trump.\n\n";
976 $userids = DB_get_all_userid_by_gameid($gameid);
977 foreach($userids as $user)
979 $subject = 'Game '.DB_format_gameid($gameid).' canceled (poverty not resolved)';
980 mymail($user,$subject,$message);
983 /* update game status */
984 cancel_game('trump',$gameid);
986 echo "<p class=\"message\";>Game ".DB_format_gameid($gameid)." has been canceled.<br /><br /></p>";
991 /* email next player, set his status to poverty */
992 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
993 $userid = DB_get_userid('hash',$userhash);
995 DB_set_player_by_gameid($gameid,$userid);
996 DB_set_hand_status_by_hash($userhash,'poverty');
998 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
999 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
1000 $subject = 'Poverty (game '.DB_format_gameid($gameid).')';
1001 mymail($userid,$subject,$message);
1006 /* player wants to take trump, change cards */
1008 /* user wants to take trump */
1009 $trump = $_REQUEST['trump'];
1010 $userhand = DB_get_handid('gameid-userid',$gameid,$trump);
1011 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
1013 /* remember which cards were handed over*/
1014 $partnerhand = DB_get_all_hand($userhash);
1015 foreach ($partnerhand as $card)
1017 DB_add_exchanged_card($card,$userhand,$myhand);
1019 /* copy trump from player A to B */
1020 $result = DB_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
1023 $mycards = DB_get_hand($me);
1026 if($gametype=='poverty')
1028 $userids = DB_get_all_userid_by_gameid($gameid);
1029 foreach($userids as $user)
1031 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1032 if($hash==$userhash||$hash==$me)
1034 DB_set_party_by_hash($hash,'re');
1038 DB_set_party_by_hash($hash,'contra');
1039 DB_set_hand_status_by_hash($hash,'play'); /* the contra party is ready to play */
1042 /* check if we are done (in case of no trump handed over), if so, go to 'play' phase right away*/
1043 if(count($mycards)==12)
1045 DB_set_hand_status_by_hash($me,'play');
1050 /* has the re party already been set?*/
1052 $userids = DB_get_all_userid_by_gameid($gameid);
1053 foreach($userids as $user)
1055 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1056 $party = DB_get_party_by_hash($hash);
1062 DB_set_party_by_hash($me,'contra');
1063 DB_set_party_by_hash($userhash,'contra');
1067 DB_set_party_by_hash($me,'re');
1068 DB_set_party_by_hash($userhash,'re');
1070 /* send out email to second non-poverty player */
1071 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
1072 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
1075 if($firstsick=='poverty')
1076 if($secondsick=='poverty')
1082 echo "<div class=\"message\">Error in poverty, please contact the Admin</div>\n";
1084 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
1085 $userid = DB_get_userid('hash',$userhash);
1087 DB_set_player_by_gameid($gameid,$userid);
1088 DB_set_hand_status_by_hash($userhash,'poverty');
1090 $message = "Two people have poverty, it's your turn to decide, if you want to take the trump. Please visit:".
1091 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
1092 $subject = 'Double poverty (game '.DB_format_gameid($gameid).')';
1093 mymail($userid,$subject,$message);
1096 echo "<div class=\"message\">Please, <a href=\"$INDEX?action=game&me=$me\">continue</a> here.</div>\n";
1104 /* both entries here, so that the tricks are visible for both.
1105 * in case of 'play' there is a break later that skips the last part
1108 /* first check if the game has been canceled and display */
1111 case 'cancel-noplay':
1112 echo "<div class=\"message\"><p>The game has been canceled due to the request of one player.</p><p>If this was a mistake all 4 players need to send an Email to $ADMIN_NAME at $ADMIN_EMAIL requesting that the game should be restarted.</p></div>";
1114 case 'cancel-timedout':
1115 echo "<div class=\"message\"><p>The game has been canceled because one player wasn't responding.</p><p>If this was a mistake all 4 players need to send an Email to $ADMIN_NAME at $ADMIN_EMAIL requesting that the game should be restarted.</p></div>";
1117 case 'cancel-nines':
1118 case 'cancel-timedout':
1119 echo "<div class=\"message\"><p>The game has been canceled because one player had too many nines.</p></div>";
1121 case 'cancel-trump':
1122 echo "<div class=\"message\"><p>The game has been canceled because nobody wanted to take the trump.</p></div>";
1125 /* for these two types, we shouldn't show the cards, since we might want to restart the game */
1126 if (in_array($gamestatus,array('cancel-noplay','cancel-timedout')))
1129 /* check if all players are ready to play,
1130 * if so, send out email to the startplayer
1131 * only need to do this if the game hasn't started yet
1133 $gamestatus = DB_get_game_status_by_gameid($gameid);
1134 if($gamestatus == 'pre')
1137 $userids = DB_get_all_userid_by_gameid($gameid);
1138 foreach($userids as $user)
1140 $userstatus = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
1141 if($userstatus !='play' && $userstatus!='gameover')
1144 DB_set_player_by_gameid($gameid,$user);
1150 /* only set this after all poverty, etc. are handled*/
1151 DB_set_game_status_by_gameid($gameid,'play');
1153 /* email startplayer */
1154 $startplayer = DB_get_startplayer_by_gameid($gameid);
1155 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
1156 $who = DB_get_userid('hash',$hash);
1157 DB_set_player_by_gameid($gameid,$who);
1159 if($hash!=$me && DB_get_email_pref_by_hash($hash)!='emailaddict')
1161 /* email startplayer) */
1162 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
1163 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
1164 $subject = 'Ready, set, go... (game '.DB_format_gameid($gameid).')';
1165 mymail($who,$subject,$message);
1169 /* figure out what kind of game we are playing,
1170 * set the global variables $CARDS['trump'],$CARDS['diamonds'],$CARDS['hearts'],
1171 * $CARDS['clubs'],$CARDS['spades'],$CARDS['foxes']
1175 $gametype = DB_get_gametype_by_gameid($gameid);
1177 if($gametype=='solo')
1179 $gametype = DB_get_solo_by_gameid($gameid);
1180 if($gametype=='silent')
1183 $GT = $gametype.' '.$GT;
1186 $gametype = 'normal';
1188 set_gametype($gametype); /* this sets the $CARDS variable */
1190 /* get some infos about the game, need to reset this, since it might have changed */
1191 $gamestatus = DB_get_game_status_by_gameid($gameid);
1193 /* has the game started? No, then just wait here...*/
1194 if($gamestatus == 'pre')
1196 echo "<p class=\"message\"> You finished the setup, but not everyone else finished it... ".
1197 "You need to wait for the others. Just wait for an email. </p>";
1199 $mycards = DB_get_hand($me);
1200 $mycards = mysort($mycards,$gametype);
1202 echo "<div class=\"mycards\">Your cards are: <br />\n";
1203 foreach($mycards as $card)
1204 display_card($card,$PREF['cardset']);
1207 break; /* not sure this works... the idea is that you can
1208 * only play a card after everyone is ready to play */
1212 /* get time from the last action of the game */
1213 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1214 $gameend = time() - strtotime($r[0]);
1216 /* handle comments in case player didn't play a card, allow comments a week after the end of the game */
1217 if( (!myisset('card') && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
1218 if(myisset('comment'))
1220 $comment = $_REQUEST['comment'];
1221 $playid = DB_get_current_playid($gameid);
1224 DB_insert_comment($comment,$playid,$myid);
1227 /* get everything relevant to display the tricks */
1228 $result = DB_query("SELECT Hand_Card.card_id as card,".
1229 " Hand.position as position,".
1230 " Play.sequence as sequence, ".
1232 " GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
1233 " SEPARATOR '\n' ), ".
1234 " Play.create_date, ".
1237 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1238 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1239 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1240 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1241 "LEFT JOIN User On User.id=Comment.user_id ".
1242 "WHERE Trick.game_id='".$gameid."' ".
1243 "GROUP BY Trick.id, sequence ".
1244 "ORDER BY Trick.id, sequence ASC");
1246 $lasttrick = DB_get_max_trickid($gameid);
1248 $play = array(); /* needed to calculate winner later */
1250 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1251 $firstcard = ''; /* first card in a trick */
1253 echo "\n<ul class=\"tricks\">\n";
1254 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
1256 /* output vorbehalte */
1257 $mygametype = DB_get_gametype_by_gameid($gameid);
1258 if($mygametype != 'normal' && $mygametype != 'silent') /* only show when needed */
1260 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
1261 " <div class=\"trick\" id=\"trick0\">\n";
1263 /* get information so show the cards that have been handed over in a poverty game */
1264 output_exchanged_cards();
1266 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1270 while($r = DB_fetch_array($result))
1278 /* count number of tricks */
1282 /* check if first schweinchen has been played */
1283 if( $GAME['schweinchen-who'] && ($r[0] == 19 || $r[0] == 20) )
1284 if(!$GAME['schweinchen-first'])
1285 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1287 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1289 /* save card to be able to find the winner of the trick later */
1290 $play[$seq] = array('card'=>$r[0],'pos'=>$pos);
1294 /* first card in a trick, output some html */
1295 if($trick!=$lasttrick)
1297 /* start of an old trick? */
1298 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1299 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1300 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1302 else if($trick==$lasttrick)
1304 /* start of a last trick? */
1305 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1306 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1307 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1310 /* remember first card, so that we are able to check, what cards can be played */
1315 echo " <div class=\"card".($pos-1)."\">\n";
1317 /* display comments */
1319 echo " <span class=\"comment\">".$comment."</span>\n";
1322 display_card($r[0],$PREF['cardset']);
1324 echo " </div>\n"; /* end div card */
1329 $winner = get_winner($play,$gametype); /* returns the position */
1330 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1334 /* whos turn is it? */
1337 $winner = get_winner($play,$gametype); /* returns the position */
1339 $firstcard = ''; /* new trick, no first card */
1344 if($next==5) $next = 1;
1347 /* my turn?, display cards as links, ask for comments*/
1348 if(DB_get_pos_by_hash($me) == $next)
1353 /* do we want to play a card? */
1354 if(myisset('card') && $myturn)
1356 $card = $_REQUEST['card'];
1357 $handid = DB_get_handid('hash',$me);
1358 $commentSchweinchen =''; /* used to add a comment when Schweinchen is being played */
1360 /* check if we have card and that we haven't played it yet*/
1361 /* set played in hand_card to true where hand_id and card_id*/
1362 $r = DB_query_array("SELECT id FROM Hand_Card WHERE played='false' and ".
1363 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1364 $handcardid = $r[0];
1366 if($handcardid) /* everything ok, play card */
1368 /* update Game timestamp */
1369 DB_update_game_timestamp($gameid);
1371 /* mark card as played */
1372 DB_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1373 DB_quote_smart($card));
1375 /* get trick id or start new trick */
1376 $a = DB_get_current_trickid($gameid);
1381 $playid = DB_play_card($trickid,$handcardid,$sequence);
1383 /* check special output for schweinchen in case in case a fox is being played
1384 * check for correct rules, etc. has already been done
1386 if( $GAME['schweinchen-who'] && ($card == 19 || $card == 20) )
1388 if(!$GAME['schweinchen-first'])
1389 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1391 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1393 if( $RULES['schweinchen']=='both' ||
1394 ($RULES['schweinchen']=='second' && $GAME['schweinchen-second']==1 )||
1395 ($RULES['schweinchen']=='secondaftercall' && $GAME['schweinchen-second']==1 &&
1396 (DB_get_call_by_hash($GAME['schweinchen-who']) || DB_get_partner_call_by_hash($GAME['schweinchen-who']) ))
1399 DB_insert_comment('Schweinchen! ',$playid,$myid);
1400 $commentSchweinchen = 'Schweinchen! ';
1403 echo 'schweinchen = '.$GAME['schweinchen-who'].' ---<br />';
1406 /* if sequence == 4 check who one in case of wedding */
1407 if($sequence == 4 && $GT == 'wedding')
1409 /* is wedding resolve */
1410 $resolved = DB_get_sickness_by_gameid($gameid);
1413 /* who has wedding */
1414 $userids = DB_get_all_userid_by_gameid($gameid);
1415 foreach($userids as $user)
1417 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1418 if($usersick == 'wedding')
1421 /* who won the trick */
1422 $play = DB_get_cards_by_trick($trickid);
1423 $winner = get_winner($play,$gametype); /* returns the position */
1424 $winnerid = DB_get_userid('gameid-position',$gameid,$winner);
1425 /* is tricknr <=3 */
1426 if($tricknr <=3 && $winnerid!=$whosick)
1428 /* set resolved at tricknr*/
1429 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1431 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1432 DB_set_party_by_hash($whash,'re');
1434 if($tricknr == 3 && $winnerid==$whosick)
1436 /* set resolved at tricknr*/
1437 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1442 /* if sequence == 4, set winner of the trick, count points and set the next player */
1445 $play = DB_get_cards_by_trick($trickid);
1446 $winner = get_winner($play,$gametype); /* returns the position */
1449 * check if someone caught a fox
1450 *******************************/
1452 /* first check if we should account for solos at all,
1453 * since it doesn't make sense in some games
1455 $ok = 0; /* fox shouldn't be counted */
1456 if(DB_get_gametype_by_gameid($gameid)=='solo')
1458 $solo = DB_get_solo_by_gameid($gameid);
1459 if($solo == 'trump' || $solo == 'silent')
1460 $ok = 1; /* for trump solos and silent solos, foxes are ok */
1463 $ok = 1; /* for all other games (not solos) foxes are ok too */
1466 foreach($play as $played)
1468 if ( $played['card']==19 || $played['card']==20 )
1469 if ($played['pos']!= $winner )
1471 /* possible caught a fox, check party */
1472 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1473 $uid2 = DB_get_userid('gameid-position',$gameid,$played['pos']);
1475 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1476 $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1478 if($party1 != $party2)
1479 DB_query("INSERT INTO Score".
1480 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1485 * check for karlchen (jack of clubs in the last trick)
1486 ******************************************************/
1488 /* same as for foxes, karlchen doesn't always make sense
1489 * check what kind of game it is and set karlchen accordingly */
1490 $ok = 1; /* default: karlchen should be accounted for */
1492 $ok = 0; /* Karlchen works only in the last trick */
1493 if($ok && DB_get_gametype_by_gameid($gameid)=='solo' )
1495 $solo = DB_get_solo_by_gameid($gameid);
1496 if($solo == 'trumpless' || $solo == 'jack' || $solo == 'queen' )
1497 $ok = 0; /* no Karlchen in these solos */
1501 foreach($play as $played)
1502 if ( $played['card']==11 || $played['card']==12 )
1503 if ($played['pos'] == $winner )
1505 /* possible caught a fox, check party */
1506 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1507 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1509 DB_query("INSERT INTO Score".
1510 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1513 * check for doppelopf (>40 points)
1514 ***********************************/
1517 foreach($play as $played)
1519 $points += DB_get_card_value_by_cardid($played['card']);
1523 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1524 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1526 DB_query("INSERT INTO Score".
1527 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
1531 * set winner (for this trick)
1535 DB_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1537 echo "ERROR during scoring";
1540 echo "DEBUG: position $winner won the trick <br />";
1542 /* who is the next player? */
1547 $next = DB_get_pos_by_hash($me)+1;
1549 if($next==5) $next=1;
1551 /* check for coment */
1552 if(myisset('comment'))
1554 $comment = $_REQUEST['comment'];
1556 DB_insert_comment($comment,$playid,$myid);
1557 if($commentSchweinchen)
1558 $comment = $commentSchweinchen . $comment;
1561 /* display played card */
1562 $pos = DB_get_pos_by_hash($me);
1565 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1566 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1567 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1570 echo " <div class=\"card".($pos-1)."\">\n ";
1572 /* display comments */
1573 display_card($card,$PREF['cardset']);
1575 echo "\n <span class=\"comment\"> ".$comment."</span>\n";
1578 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1580 /*check if we still have cards left, else set status to gameover */
1581 if(sizeof(DB_get_hand($me))==0)
1583 DB_set_hand_status_by_hash($me,'gameover');
1584 $mystatus = 'gameover';
1587 /* if all players are done, set game status to game over,
1588 * get the points of the last trick and send out an email
1591 $userids = DB_get_all_userid_by_gameid($gameid);
1594 foreach($userids as $user)
1595 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1599 DB_set_game_status_by_gameid($gameid,'gameover');
1601 /* email next player, if game is still running */
1602 if(DB_get_game_status_by_gameid($gameid)=='play')
1604 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1605 $who = DB_get_userid('hash',$next_hash);
1606 DB_set_player_by_gameid($gameid,$who);
1608 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1609 "It's your turn now.\n".
1610 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$next_hash."\n\n" ;
1611 if( DB_get_email_pref_by_uid($who)!='emailaddict' )
1613 $subject = 'A card has been played in game '.DB_format_gameid($gameid);
1614 mymail($who,$subject,$message);
1617 else /* send out final email */
1619 /* individual score */
1620 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1621 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1622 " LEFT JOIN User ON User.id=Hand.user_id".
1623 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1624 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1625 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1626 " WHERE Hand.game_id='$gameid'".
1627 " GROUP BY User.fullname" );
1628 $message = "The game is over. Thanks for playing :)\n";
1629 $message .= "Final score:\n";
1630 while( $r = DB_fetch_array($result) )
1631 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1633 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1634 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1635 " LEFT JOIN User ON User.id=Hand.user_id".
1636 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1637 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1638 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1639 " WHERE Hand.game_id='$gameid'".
1640 " GROUP BY Hand.party" );
1641 $message .= "\nTotals:\n";
1644 while( $r = DB_fetch_array($result) )
1646 $message .= " ".$r[0]." ".$r[1]."\n";
1649 else if($r[0] == 'contra')
1654 * save score in database
1658 /* get calls from re/contra */
1660 $call_contra = NULL;
1661 foreach($userids as $user)
1663 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1664 $call = DB_get_call_by_hash($hash);
1665 $party = DB_get_party_by_hash($hash);
1669 $call = (int) $call;
1675 else if( $call < $call_re)
1678 else if($party=='contra')
1680 if($call_contra==NULL)
1681 $call_contra = $call;
1682 else if( $call < $call_contra)
1683 $call_contra = $call;
1688 /* figure out who one */
1689 $winning_party = NULL;
1691 if($call_re == NULL && $call_contra==NULL)
1693 /* nobody made a call, so it's easy to figure out who won */
1695 $winning_party='re';
1697 $winning_party='contra';
1701 /* if one party makes a call, they only win, iff they make enough points
1702 * if only one party made a call, the other one wins,
1703 * if the first one didn't make it
1707 $offset = 120 - $call_re;
1709 $offset--; /* since we use a > in the next equation */
1711 if($re > 120+$offset)
1712 $winning_party='re';
1713 else if ($call_contra == NULL )
1714 $winning_party='contra';
1719 $offset = 120 - $call_contra;
1720 if($call_contra == 0)
1721 $offset--; /* since we use a > in the next equation */
1723 if($contra > 120+$offset)
1724 $winning_party='contra';
1725 else if ($call_re == NULL )
1726 $winning_party='re';
1730 /* one point for each call of the other party in case the other party didn't win
1731 * and one point each in case the party made more than points than one of the calls
1733 if($winning_party!='contra' && $call_contra!=NULL)
1735 for( $p=$call_contra;$p<=120; $p+=30 )
1737 DB_query("INSERT INTO Score".
1738 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
1741 for( $p=$call_contra; $p<120; $p+=30)
1744 DB_query("INSERT INTO Score".
1745 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'made$p')");
1748 if($winning_party!='re' and $call_re!=NULL)
1750 for( $p=$call_re;$p<=120; $p+=30 )
1752 DB_query("INSERT INTO Score".
1753 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'against$p')");
1756 for( $p=$call_re; $p<120; $p+=30)
1759 DB_query("INSERT INTO Score".
1760 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
1764 /* point in case contra won */
1765 if($winning_party=='contra')
1767 DB_query("INSERT INTO Score".
1768 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1771 /* one point each for winning and each 30 points + calls */
1772 if($winning_party=='re')
1774 foreach(array(120,150,180,210,240) as $p)
1777 if($p==240 || $call_contra!=NULL)
1781 DB_query("INSERT INTO Score".
1782 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
1784 /* re called something and won */
1785 foreach(array(0,30,60,90,120) as $p)
1787 if($call_re!=NULL && $call_re<$p+1)
1788 DB_query("INSERT INTO Score".
1789 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
1792 else if( $winning_party=='contra')
1794 foreach(array(120,150,180,210,240) as $p)
1797 if($p==240 || $call_re!=NULL)
1800 if($contra>$p-$offset)
1801 DB_query("INSERT INTO Score".
1802 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
1804 /* re called something and won */
1805 foreach(array(0,30,60,90,120) as $p)
1807 if($call_contra!=NULL && $call_contra<$p+1)
1808 DB_query("INSERT INTO Score".
1809 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
1814 /* add score points to email */
1817 $message .= " Points Re: \n";
1818 $queryresult = DB_query("SELECT score FROM Score ".
1819 " WHERE game_id=$gameid AND party='re'".
1821 while($r = DB_fetch_array($queryresult) )
1823 $message .= " ".$r[0]."\n";
1826 $message .= " Points Contra: \n";
1827 $queryresult = DB_query("SELECT score FROM Score ".
1828 " WHERE game_id=$gameid AND party='contra'".
1830 while($r = DB_fetch_array($queryresult) )
1832 $message .= " ".$r[0]."\n";
1835 $message .= " Total Points (from the Re point of view): $Tpoint\n";
1838 $session = DB_get_session_by_gameid($gameid);
1839 $score = generate_score_table($session);
1841 $message .= "Score Table:\n";
1842 $message .= format_score_table_ascii($score);
1843 $message .= "\nUse these links to have a look at game ".DB_format_gameid($gameid).": \n";
1845 /* send out final email */
1846 foreach($userids as $user)
1848 /* add links for all players */
1849 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1850 $name = DB_get_name('userid',$user);
1852 $link = "$name: ".$HOST.$INDEX."?action=game&me=".$hash."\n" ;
1855 $message .= "\n\n (you can use reply all on this email to reach all the players.)\n\n";
1856 $subject = ' Game over (game '.DB_format_gameid($gameid).') ';
1857 mymail($userids,$subject,$message);
1862 echo "can't find that card?! <br />\n";
1865 else if(myisset('card') && !$myturn )
1867 echo "please wait until it's your turn! <br />\n";
1870 if($seq!=4 && $trickNR>=1 && !(myisset('card') && $myturn) )
1871 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1873 /* display points in case game is over */
1874 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1876 echo " <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1877 " <div class=\"trick\" id=\"trick13\">\n";
1878 /* add pic for re/contra
1879 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1881 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1882 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1883 " LEFT JOIN User ON User.id=Hand.user_id".
1884 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1885 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1886 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1887 " WHERE Hand.game_id='$gameid'".
1888 " GROUP BY User.fullname" );
1889 while( $r = DB_fetch_array($result))
1890 echo " <div class=\"card".($r[3]-1)."\">\n".
1891 " <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
1894 /* display totals */
1895 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1896 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1897 " LEFT JOIN User ON User.id=Hand.user_id".
1898 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1899 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1900 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1901 " WHERE Hand.game_id='$gameid'".
1902 " GROUP BY Hand.party" );
1903 echo "<div class=\"total\">\n Totals:<br />\n";
1904 while( $r = DB_fetch_array($result))
1905 echo " ".$r[0]." ".$r[1]."<br />\n";
1907 $queryresult = DB_query("SELECT timediff(mod_date,create_date) ".
1908 " FROM Game WHERE id='$gameid'");
1909 $r = DB_fetch_array($queryresult);
1910 echo " <p>This game took ".$r[0]." hours.</p>\n";
1912 echo " <div class=\"re\">\n Points Re: <br />\n";
1913 $queryresult = DB_query("SELECT score FROM Score ".
1914 " WHERE game_id=$gameid AND party='re'".
1916 while($r = DB_fetch_array($queryresult) )
1917 echo " ".$r[0]."<br />\n";
1920 echo " <div class=\"contra\">\n Points Contra: <br />\n";
1921 $queryresult = DB_query("SELECT score FROM Score ".
1922 " WHERE game_id=$gameid AND party='contra'".
1924 while($r = DB_fetch_array($queryresult) )
1925 echo " ".$r[0]."<br />\n";
1930 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1933 echo " <li onclick=\"hl_prev();\" class=\"old\"><a href=\"#\">prev</a></li>\n";
1934 echo " <li onclick=\"hl_next();\" class=\"old\"><a href=\"#\">next</a></li>\n";
1935 echo "</ul>\n"; /* end ul tricks*/
1937 $mycards = DB_get_hand($me);
1938 $mycards = mysort($mycards,$gametype);
1939 echo "<div class=\"mycards\">\n";
1941 if($myturn && !myisset('card') && $mystatus=='play' )
1943 echo "Hello ".$myname.", it's your turn! <br />\n";
1944 echo "Your cards are: <br />\n";
1946 /* do we have to follow suite? */
1948 if(have_suit($mycards,$firstcard))
1951 foreach($mycards as $card)
1953 /* display only cards that the player is allowed to play as links, the rest just display normal
1954 * also check if we have both schweinchen, in that case only display on of them as playable
1956 if( ($followsuit && !same_type($card,$firstcard)) ||
1957 ( (int)($card)==19 &&
1958 !$GAME['schweinchen-first'] &&
1959 ( $RULES['schweinchen']=='second' ||
1960 ( $RULES['schweinchen']=='secondaftercall' &&
1961 (DB_get_call_by_hash($GAME['schweinchen-who']) ||
1962 DB_get_partner_call_by_hash($GAME['schweinchen-who']) )
1965 $GAME['schweinchen-who']==$me &&
1966 in_array($gametype,array('normal','wedding','trump','silent'))
1969 display_card($card,$PREF['cardset']);
1971 display_link_card($card,$PREF['cardset']);
1974 else if($mystatus=='play' )
1976 echo "Your cards are: <br />\n";
1977 foreach($mycards as $card)
1978 display_card($card,$PREF['cardset']);
1980 else if($mystatus=='gameover')
1982 $oldcards = DB_get_all_hand($me);
1983 $oldcards = mysort($oldcards,$gametype);
1985 if(isset($_SESSION['id']) && $myid==$_SESSION['id'])
1986 echo "Your cards were: <br />\n";
1989 $name = DB_get_name('userid',$myid);
1990 echo "$name's were: <br />\n";
1993 foreach($oldcards as $card)
1994 display_card($card,$PREF['cardset']);
1996 $userids = DB_get_all_userid_by_gameid($gameid);
1997 foreach($userids as $user)
1999 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
2005 $name = DB_get_name('userid',$user);
2006 $oldcards = DB_get_all_hand($userhash);
2007 $oldcards = mysort($oldcards,$gametype);
2008 echo "$name's cards were: <br />\n";
2009 foreach($oldcards as $card)
2010 display_card($card,$PREF['cardset']);
2016 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
2017 if($mystatus=='play')
2020 /* the following happens only when the gamestatus is 'gameover' */
2021 /* check if game is over, display results */
2022 if(DB_get_game_status_by_gameid($gameid)=='play')
2024 echo "The game is over for you.. other people still need to play though";
2028 myerror("error in testing the status");
2031 /* output other games where it is the users turn
2032 * make sure that the people looking at old games don't see the wrong games here
2034 if( $mystatus != 'gameover' )
2035 display_user_menu($myid);
2036 else if( $mystatus == 'gameover' &&
2037 isset($_SESSION['id']) )
2039 display_user_menu($_SESSION['id']);
2043 echo "<div class=\"usermenu\">\n";
2044 echo "It's your turn in these games:<br />\n";
2045 echo "Please log in to see this information.\n";
2049 /* display rule set for this game */
2050 echo "<div class=\"gameinfo\">\n";
2052 if($gamestatus == 'play' )
2054 $myparty = DB_get_party_by_hash($me);
2055 output_form_calls($me,$myparty);
2057 /* get time from the last action of the game */
2058 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
2059 $gameend = time() - strtotime($r[0]);
2061 if($gamestatus == 'play' || $gameend < 60*60*24*7)
2063 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
2066 echo "<input type=\"submit\" value=\"submit\" />\n";
2068 /* has this hand been played by others? */
2069 $other_game_ids = DB_played_by_others($gameid);
2070 if(sizeof($other_game_ids)>0 && $mystatus=='gameover')
2072 $mypos = DB_get_pos_by_hash($me);
2073 echo "<p>See how other played the same hand: <br />\n";
2074 foreach($other_game_ids as $id)
2076 $otherhash = DB_get_hash_from_game_and_pos($id,$mypos);
2077 $othername = DB_get_name('hash',$otherhash);
2078 echo "<a href=\"$INDEX?action=game&me=$otherhash\">$othername</a><br />";
2087 $gamestatus = DB_get_game_status_by_gameid($gameid);
2088 if($mystatus=='gameover' &&
2089 ($gamestatus =='gameover' || $gamestatus =='cancel-nines' || $gamestatus =='cancel-trump') &&
2090 isset($_SESSION['id']) && $_SESSION['id']==$myid)
2092 $session = DB_get_session_by_gameid($gameid);
2093 $result = DB_query("SELECT id,create_date FROM Game".
2094 " WHERE session=$session".
2095 " ORDER BY create_date DESC".
2099 $r = DB_fetch_array($result);
2101 if(!$session || $gameid==$r[0])
2103 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
2104 $names = DB_get_all_names_by_gameid($gameid);
2105 $type = DB_get_gametype_by_gameid($gameid);
2109 $solo = DB_get_solo_by_gameid($gameid);
2111 if($solo!='silent') /* repeat game with same first player */
2112 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
2113 else /* rotate normally */
2114 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
2116 else if($gamestatus == 'cancel-nines' || $gamestatus == 'cancel-trump')
2117 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
2118 else /* rotate normally */
2119 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);