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 $origmystatus = DB_get_status_by_hash($me); /* to show "it's your turn" menu when game has just finished */
51 $mypos = DB_get_pos_by_hash($me);
52 $myhand = DB_get_handid('hash',$me);
53 $myparty = DB_get_party_by_hash($me);
54 $session = DB_get_session_by_gameid($gameid);
56 /* get prefs and save them in a variable*/
57 $PREF = DB_get_PREF(isset($_SESSION['id'])?$_SESSION['id']:$myid);
59 /* get rule set for this game */
60 $RULES = DB_get_RULES($gameid);
62 /* get some infos about the game */
63 $gametype = DB_get_gametype_by_gameid($gameid);
64 $gamestatus = DB_get_game_status_by_gameid($gameid);
68 $gametype = DB_get_solo_by_gameid($gameid);
69 $GT = $gametype.' '.$GT;
72 /* do we need to worry about Schweinchen?
73 * check gametype and rules
74 * if yes, figure out if someone actually has Schweinchen
75 * save information in $GAME
78 if( $gamestatus == 'pre' )
80 /* always need to use Schweinchen to figure out for example who has poverty */
85 /* in a game Schweinchen is not valid in all types of games */
86 if( in_array($gametype,array('normal','wedding','trump','silent') ))
87 if( in_array($RULES['schweinchen'],array('both','second','secondaftercall')) )
91 /* these are the defaults */
92 $GAME['schweinchen-who'] = NULL;
93 $GAME['schweinchen-first'] = NULL;
94 $GAME['schweinchen-second'] = NULL;
98 /* need to check for Schweinchen */
101 $hash = DB_get_hash_from_game_and_pos($gameid,$i);
102 $cards = DB_get_all_hand($hash);
103 if( in_array('19',$cards) && in_array('20',$cards) )
104 $GAME['schweinchen-who']=$hash;
106 $GAME['schweinchen-first'] = 0; /* to keep track if they have been played already */
107 $GAME['schweinchen-second'] = 0;
109 /* end check for Schweinchen */
111 /* set the $CARDS variable, needed for sorting the cards
112 * we set it to normal so that the pre-game phase is handled ok
113 * and later set it to the correct game type that is played
115 set_gametype('normal');
117 /* put everyting in a form */
118 echo "<form action=\"index.php?action=game&me=$me\" method=\"post\">\n";
120 /* handle user notes (only possible while game is running)*/
121 if( $mystatus!='gameover' )
124 $note = $_REQUEST['note'];
127 DB_insert_note($note,$gameid,$myid);
130 /* make sure that we don't show the notes to the wrong person
131 * (e.g. other people looking at an old game)
133 if( $mystatus != 'gameover' ||
134 ( $mystatus == 'gameover' &&
135 isset($_SESSION['id']) &&
136 $myid == $_SESSION['id']))
137 output_user_notes($myid,$gameid,$mystatus);
139 /* handle calls, output a comment to show when the call was made */
140 /* initialize comments */
143 /* check for calls, set comment */
144 if(myisset('call') && $_REQUEST['call'] == '120' && can_call(120,$me))
146 $result = DB_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
149 else if($myparty=='contra')
150 $comment .= "Contra";
152 if(myisset('call') && $_REQUEST['call'] == '90' && can_call(90,$me))
154 $result = DB_query("UPDATE Hand SET point_call='90' WHERE hash='$me' ");
157 if(myisset('call') && $_REQUEST['call'] == '60' && can_call(60,$me))
159 $result = DB_query("UPDATE Hand SET point_call='60' WHERE hash='$me' ");
162 if(myisset('call') && $_REQUEST['call'] == '30' && can_call(30,$me))
164 $result = DB_query("UPDATE Hand SET point_call='30' WHERE hash='$me' ");
167 if(myisset('call') && $_REQUEST['call'] == '0' && can_call(0,$me))
169 $result = DB_query("UPDATE Hand SET point_call='0' WHERE hash='$me' ");
173 /* get information needed to submit comment */
174 $playid = DB_get_current_playid($gameid);
178 DB_insert_comment($comment,$playid,$myid);
181 /* end check for calls */
184 /* output extra division in case this game is part of a session */
187 echo "<div class=\"session\">\n";
188 echo " <div class=\"sessionrules\">Rules: ";
189 switch($RULES['dullen'])
192 echo " <img class=\"rulesicon\" alt=\"not ten of hearts\" src=\"pics/button/no-ten-of-hearts.png\"/>\n"; break;
194 echo " <img class=\"rulesicon\" alt=\"ten of hearts\" src=\"pics/button/ten-of-hearts.png\"/>\n"; break;
196 echo " <img class=\"rulesicon\" alt=\"second ten of hearts\" src=\"pics/button/second-ten-of-hearts.png\"/>\n"; break;
198 switch($RULES['schweinchen'])
201 echo " <img class=\"rulesicon\" alt=\"no schweinchen\" ".
202 "src=\"pics/button/no-schweinchen.png\"/>\n"; break;
204 echo " <img class=\"rulesicon\" alt=\"two schweinchen \" ".
205 "src=\"pics/button/two-schweinchen.png\"/>\n"; break;
207 echo " <img class=\"rulesicon\" alt=\"second schweinchen\" ".
208 "src=\"pics/button/second-schweinchen.png\"/>\n"; break;
209 case 'secondaftercall':
210 echo " <img class=\"rulesicon\" alt=\"second schweinchen after call\" ".
211 "src=\"pics/button/second-schweinchen-after-call.png\"/>\n"; break;
213 switch($RULES['call'])
216 echo " <img class=\"rulesicon\" alt=\"1st-own-card\" src=\"pics/button/1st-own-card.png\"/>\n"; break;
218 echo " <img class=\"rulesicon\" alt=\"5th-card\" src=\"pics/button/5th-card.png\"/>\n"; break;
220 echo " <img class=\"rulesicon\" alt=\"9-cards\" src=\"pics/button/9-cards.png\"/>\n"; break;
223 echo " 10ofhearts : {$RULES['dullen']} <br />\n";
224 echo " schweinchen: {$RULES['schweinchen']} <br />\n";
225 echo " call: {$RULES['call']} <br />\n";
226 echo " </div>\n </div>\n";
230 echo " <div class=\"sessionscore\">";
232 $score = generate_score_table($session);
234 /* get the last entry to show on the main page */
236 $finalscore = array_pop($tmpscore);
237 $finalscore = $finalscore['players'];
242 foreach($finalscore as $user=>$value)
244 $name = DB_get_name('userid',$user);
245 echo " ".substr($name,0,2).": $value ";
250 /* first game, no score yet */
254 /* output all games for the score table */
255 echo format_score_table_html($score,$myid);
258 /* figure out which game in a session we are in and link to the
259 * previous and next game if possible
261 $hashes = DB_get_hashes_by_session($session,$myid);
264 foreach($hashes as $hash)
274 $previous = $hashes[$j-2];
282 if(isset($_SESSION['id']) && $_SESSION['id']==$myid)
285 echo "<a href=\"{$INDEX}?action=game&me=$previous\">previous game</a> \n";
286 echo "This is game number $j of <a href=\"{$INDEX}?action=game&me=$lasthash\">$i</a> in session $session.\n";
288 echo " <a href=\"{$INDEX}?action=game&me=$next\">next game</a> \n";
291 echo "This is game number $j of $i in session $session.";
295 /* display the table and the names */
298 /* mystatus gets the player through the different stages of a game.
299 * start: does the player want to play?
300 * init: check for sickness
301 * check: check for return values from init
302 * poverty: handle poverty, wait here until all player have reached this state
303 * display sickness and move on to game
304 * play: game in progress
305 * gameover: are we revisiting a game
308 /* the user has done something, update the timestamp. Use $myid in
309 * active games and check for session-id in old games (myid might be wrong in that case)
311 if($mystatus!='gameover')
312 DB_update_user_timestamp($myid);
314 if(isset($_SESSION['id']))
315 DB_update_user_timestamp($_SESSION['id']);
320 /* don't ask if user has autosetup set to yest */
322 if($PREF['autosetup']=='yes') $skip = 1;
324 if( !myisset('in') && !$skip)
326 /* asks the player, if he wants to join the game */
327 output_check_want_to_play($me);
332 /* check the result, if player wants to join, got next stage, else cancel game */
333 if(!$skip && $_REQUEST['in'] == 'no' )
335 /* cancel the game */
336 $message = "Hello, \n\n".
337 "the game has been canceled due to the request of one of the players.\n\n";
339 $userids = DB_get_all_userid_by_gameid($gameid);
340 foreach($userids as $user)
342 $subject = 'Game '.DB_format_gameid($gameid).' canceled';
343 mymail($user,$subject,$message);
346 /* update game status */
347 cancel_game('noplay',$gameid);
352 /* user wants to join the game */
354 /* move on to the next stage,
355 * no break statement to immediately go to the next stage
358 DB_set_hand_status_by_hash($me,'init');
360 /* check if everyone has reached this stage, send out email */
361 $userids = DB_get_all_userid_by_gameid($gameid);
363 foreach($userids as $user)
365 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
366 if($userstat!='init')
368 /* whos turn is it? */
369 DB_set_player_by_gameid($gameid,$user);
376 /* all done, send out email unless this player is the startplayer */
377 $startplayer = DB_get_startplayer_by_gameid($gameid);
378 if($mypos == $startplayer)
380 /* do nothing, go to next stage */
384 /* email startplayer */
386 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
387 $who = DB_get_userid('hash',$hash);
388 DB_set_player_by_gameid($gameid,$who);
390 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
391 "Use this link to go the game: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
392 mymail($who,"Ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
399 /* here we ask the player if he is sick */
400 $mycards = DB_get_hand($me);
401 $mycards = mysort($mycards,$gametype);
403 if(!myisset('solo','wedding','poverty','nines') )
405 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
406 echo "\n<ul class=\"tricks\">\n";
407 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
408 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
409 " <div class=\"trick\" id=\"trick0\">\n";
411 for($pos=1;$pos<5;$pos++)
413 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
414 $userid = DB_get_userid('gameid-position',$gameid,$pos);
415 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
417 if($userstatus=='start' || $userstatus=='init')
418 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs <br/ > to decide </div>\n"; /* show this to everyone */
420 if($usersick!=NULL && $pos<=$mypos ) /* only show this for people sitting before the player */
421 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
422 else if($usersick==NULL && $pos<=$mypos)
423 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
425 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
426 /* end displaying sickness */
428 output_check_for_sickness($me,$mycards);
430 echo "<div class=\"mycards\">Your cards are: <br />\n";
431 foreach($mycards as $card)
432 display_card($card,$PREF['cardset']);
439 /* check if someone selected more than one sickness */
441 if($_REQUEST['solo']!='No') $Nsickness++;
442 if($_REQUEST['wedding'] == 'yes') $Nsickness++;
443 if($_REQUEST['poverty'] == 'yes') $Nsickness++;
444 if($_REQUEST['nines'] == 'yes') $Nsickness++;
448 echo "<p class=\"message\"> You selected more than one sickness, please go back ".
449 "and answer the <a href=\"$INDEX?action=game&me=$me&in=yes\">question</a> again.</p>";
451 echo "<div class=\"mycards\">Your cards are: <br />\n";
452 foreach($mycards as $card)
453 display_card($card,$PREF['cardset']);
460 /* everything is ok, save what user said and proceed */
461 echo "<p class=\"message\">Processing what you selected in the last step...";
463 /* check if this sickness needs to be handled first */
464 $gametype = DB_get_gametype_by_gameid($gameid);
465 $startplayer = DB_get_startplayer_by_gameid($gameid); /* need this to check which solo goes first */
467 if( $_REQUEST['solo']!='No' )
469 /* user wants to play a solo */
471 /* store the info in the user's hand info */
472 DB_set_solo_by_hash($me,$_REQUEST['solo']);
473 DB_set_sickness_by_hash($me,'solo');
475 echo "<br />Seems like you want to play a {$_REQUEST['solo']} solo. Got it.<br />\n";
477 if($gametype == 'solo' && $startplayer<$mypos)
478 {}/* do nothing, since someone else already is playing solo */
481 /* this solo comes first
482 * store info in game table
484 DB_set_gametype_by_gameid($gameid,'solo');
485 DB_set_startplayer_by_gameid($gameid,$mypos);
486 DB_set_solo_by_gameid($gameid,$_REQUEST['solo']);
489 else if($_REQUEST['wedding'] == 'yes')
491 /* silent solo is set further down */
492 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
493 DB_set_sickness_by_hash($me,'wedding');
495 else if($_REQUEST['poverty'] == 'yes')
497 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
498 DB_set_sickness_by_hash($me,'poverty');
500 else if($_REQUEST['nines'] == 'yes')
502 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
503 " is playing solo, this game will be canceled.<br />\n";
504 DB_set_sickness_by_hash($me,'nines');
509 /* move on to the next stage*/
510 DB_set_hand_status_by_hash($me,'check');
515 /* here we check what all players said and figure out what game we are playing
516 * this can therefore only be handled once all players finished the last stage
519 $mycards = DB_get_hand($me);
520 $mycards = mysort($mycards,$gametype);
522 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
523 echo "\n<ul class=\"tricks\">\n";
524 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
525 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
526 " <div class=\"trick\" id=\"trick0\">\n";
528 for($pos=1;$pos<5;$pos++)
530 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
531 $userid = DB_get_userid('gameid-position',$gameid,$pos);
532 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
534 if($userstatus=='start' || $userstatus=='init')
535 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs <br />to decide </div>\n"; /* show this to everyone */
537 if($usersick!=NULL) /* in the init-phase we only showed players with $pos<$mypos, now we can show all */
538 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
540 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
542 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
543 /* end displaying sickness */
545 echo "<div class=\"message\">\n";
546 echo "<p> Checking if someone else selected solo, nines, wedding or poverty.</p>";
548 /* check if everyone has reached this stage */
549 $userids = DB_get_all_userid_by_gameid($gameid);
551 foreach($userids as $user)
553 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
554 if($userstat!='check')
557 DB_set_player_by_gameid($gameid,$user);
564 echo "<p>This step can only be handled after everyone finished the last step. ".
565 "Seems like this is not the case, so you need to wait a bit... ".
566 "you will get an email once that is the case, please use the link in ".
567 "that email to continue the game.</p></div>";
569 /* display cards, if player was just at the init-phase he will still see the cards from there
570 * we can put this one here, since the last player to finish the init state won't get here and
571 * will still see his card anyway from the init-phase
573 if($mystatus=='check')
576 echo "<div class=\"mycards\">Your cards are: <br />\n";
577 foreach($mycards as $card)
578 display_card($card,$PREF['cardset']);
585 /* Ok, everyone finished the init-phase, time to figure out what game we
586 * are playing, in case there are any solos this already
587 * will have the correct information in it */
589 echo "<p> Ok, everyone is done... figuring out what kind of game we are playing.</p>";
591 $gametype = DB_get_gametype_by_gameid($gameid);
592 $startplayer = DB_get_startplayer_by_gameid($gameid);
594 /* check for sickness */
599 foreach($userids as $user)
601 $name = DB_get_name('userid',$user);
602 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
603 if($usersick == 'nines')
606 break; /* no need to check for other poverties, since only solo can win and that is already set */
608 else if($usersick == 'poverty')
610 else if($usersick == 'wedding')
612 else if($usersick == 'solo')
616 /* now check which sickness comes first and set the gametype to it */
617 if($gametype == 'solo')
624 $message = "The game has been canceled because ".DB_get_name('userid',$nines).
625 " has five or more nines and nobody is playing solo.\n\n".
626 "To redeal either start a new game or, in case the game was part of a tournament,\n".
627 "go to the last game and use the link at the bottom of the page to redeal.\n\n";
629 $userids = DB_get_all_userid_by_gameid($gameid);
630 foreach($userids as $user)
632 $subject = 'Game '.DB_format_gameid($gameid).' canceled';
633 mymail($user,$subject,$message);
636 /* update game status */
637 cancel_game('nines',$gameid);
639 echo "<p>The game has been canceled because ".DB_get_name('userid',$nines).
640 " has five or more nines and nobody is playing solo.</p>\n";
644 else if($poverty==1) /* one person has poverty */
646 DB_set_gametype_by_gameid($gameid,'poverty');
647 $gametype = 'poverty';
648 $who = DB_get_sickness_by_gameid($gameid);
651 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
652 if($firstsick == 'poverty')
653 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
655 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
658 else if($poverty==2) /* two people have poverty */
660 DB_set_gametype_by_gameid($gameid,'dpoverty');
661 $gametype = 'dpoverty';
662 $who = DB_get_sickness_by_gameid($gameid);
665 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
666 if($firstsick == 'poverty')
668 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
669 if($secondsick == 'poverty')
670 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
672 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
675 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
680 DB_set_gametype_by_gameid($gameid,'wedding');
681 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
682 $gametype = 'wedding';
684 /* now the gametype is set correctly in the database */
685 echo "<p> Got it :)</p>";
687 /* loop over all players, set re/contra if possible and start the game if possible */
688 $userids = DB_get_all_userid_by_gameid($gameid);
689 foreach($userids as $userid)
691 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
696 /* are we the solo player? set us to re, else set us to contra */
697 $pos = DB_get_pos_by_hash($userhash);
698 if($pos == $startplayer)
699 DB_set_party_by_hash($userhash,'re');
701 DB_set_party_by_hash($userhash,'contra');
702 DB_set_hand_status_by_hash($userhash,'play');
706 /* set person with the wedding to re, do the rest during the game */
707 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
708 if($usersick == 'wedding')
709 DB_set_party_by_hash($userhash,'re');
711 DB_set_party_by_hash($userhash,'contra');
713 DB_set_hand_status_by_hash($userhash,'play');
717 $hand = DB_get_all_hand($userhash);
719 if(in_array('3',$hand)||in_array('4',$hand))
720 DB_set_party_by_hash($userhash,'re');
722 DB_set_party_by_hash($userhash,'contra');
723 DB_set_hand_status_by_hash($userhash,'play');
727 /* set person with poverty to play status */
728 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
729 if($usersick == 'poverty')
730 DB_set_hand_status_by_hash($userhash,'play');
732 /* set status of first player to be asked to poverty */
733 $who = DB_get_sickness_by_gameid($gameid);
734 if($who > 6) $who= $who/10; /* in case we have dpoverty */
735 $whoid = DB_get_userid('gameid-position',$gameid,$who);
737 DB_set_hand_status_by_hash($userhash,'poverty');
740 /* check for silent solo, set game type to solo in this case */
741 $gametype = DB_get_gametype_by_gameid($gameid);
742 $userids = DB_get_all_userid_by_gameid($gameid);
743 foreach($userids as $userid)
745 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
747 if($gametype=='normal')
749 $userhand = DB_get_all_hand($userhash);
750 if(check_wedding($userhand))
752 /* normal game type and player has both queens -> silent solo */
753 /* keep startplayer, just set gametype to silent solo */
754 DB_set_gametype_by_gameid($gameid,'solo');
755 DB_set_solo_by_gameid($gameid,'silent');
760 /* send out email to first player or poverty person*/
761 if($gametype!='poverty' && $gametype!='dpoverty')
763 $startplayer = DB_get_startplayer_by_gameid($gameid);
764 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
765 $who = DB_get_userid('hash',$hash);
766 DB_set_player_by_gameid($gameid,$who);
770 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
772 /* email startplayer */
773 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
774 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
775 $subject = 'Ready, set, go... (game '.DB_format_gameid($gameid).')';
776 mymail($who,$subject,$message);
780 echo "<div class=\"message\">Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.</div>\n";
784 /* set status of first player to be asked to poverty */
785 $who = DB_get_sickness_by_gameid($gameid);
786 if($who > 6) $who= $who/10; /* in case we have dpoverty */
788 $whoid = DB_get_userid('gameid-position',$gameid,$who);
790 echo "<div class=\"message\">Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.</div>\n";
793 $whohash = DB_get_hash_from_game_and_pos($gameid,$who);
794 DB_set_player_by_gameid($gameid,$whoid);
796 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
798 /* email player for poverty */
799 $message = "Poverty: It's your turn now in game ".DB_format_gameid($gameid).".\n".
800 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$whohash."\n\n" ;
801 $subject = 'Poverty (game '.DB_format_gameid($gameid).') ';
802 mymail($whoid,$subject,$message);
810 /* user only gets here in a poverty game, several things have to be handled here:
811 * A) ask, if user wants to take trump
813 * poverty: set re/contra
814 * dpoverty: first time: set re, send email to second player
815 * second time: set contra
816 * poverty: set status of other players to 'play'
817 * set status to play in case 0 trump
818 * no -> set status to play,
819 * ask next player or cancle the game if no more players
820 * B) user took trump and has too many cards (e.g. count(cards)>12 and re/contra set)
821 * ask to give cards back, set status to play, once player has 12 cards
823 * it is easier to check B) first
826 set_gametype($gametype); /* this sets the $CARDS variable */
827 $myparty = DB_get_party_by_hash($me);
829 /* the following is part B) of whats needs to be done)
830 /* check if user wants to give cards back */
831 if(myisset('exchange'))
833 $exchange = $_REQUEST['exchange'];
834 $partnerhash = DB_get_partner_hash_by_hash($me);
835 $partnerid = DB_get_userid('hash',$partnerhash);
836 $partnerhand = DB_get_handid('gameid-userid',$gameid,$partnerid);
838 /* if exchange is set to a value>0, exchange that card back to the partner */
841 $result = DB_query("UPDATE Hand_Card SET hand_id='$partnerhand'".
842 " WHERE hand_id='$myhand' AND card_id=".DB_quote_smart($exchange));
843 DB_add_exchanged_card(DB_quote_smart($exchange),$myhand,$partnerhand);
848 $mycards = DB_get_hand($me);
849 $mycards = mysort($mycards,$gametype);
851 /* output pre-game trick in case user reloads,
852 * only needs to be done when a team has been formed */
853 if($myparty=='re' || $myparty=='contra')
855 echo "\n<ul class=\"tricks\">\n";
856 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
858 $mygametype = DB_get_gametype_by_gameid($gameid);
860 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
861 " <div class=\"trick\" id=\"trick0\">\n";
863 /* get information so show the cards that have been handed over in a poverty game */
864 output_exchanged_cards();
866 echo " </div>\n </li>\n"; /* end div trick, end li trick */
868 /* end output pre-game trick */
870 /* check if user need to give more cards back */
871 if( ($myparty=='re' || $myparty=='contra') && count($mycards)>12)
873 echo "<div class=\"poverty\"> you need to get rid of a few cards</div>\n";
876 echo "<div class=\"mycards\">Your cards are: <br />\n";
877 foreach($mycards as $card)
878 display_link_card($card,$PREF['cardset'],$type);
879 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select card to give back\" />\n";
882 else if( ($myparty=='re' || $myparty=='contra') && count($mycards)==12)
884 /* user is done, ready to play */
885 DB_set_hand_status_by_hash($me,'play');
887 /* email start player */
888 $startplayer = DB_get_startplayer_by_gameid($gameid);
889 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
890 $who = DB_get_userid('hash',$hash);
891 DB_set_player_by_gameid($gameid,$who);
895 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
897 /* email startplayer */
898 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
899 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
900 $subject = 'Ready, set, go... (game '.DB_format_gameid($gameid).') ';
901 mymail($who,$subject,$message);
905 echo "<div class=\"message\">Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.</div>\n";
908 /* the following is part A) of what needs to be done */
909 if(!myisset('trump'))
913 echo "<div class=\"poverty\">\n";
914 $userids = DB_get_all_userid_by_gameid($gameid);
915 foreach($userids as $user)
917 $name = DB_get_name('userid',$user);
918 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
919 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
920 $userparty = DB_get_party_by_hash($userhash);
922 if($usersick=='poverty' && !$userparty)
924 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
925 $cards = DB_get_hand($hash);
928 foreach($cards as $card)
929 if($card<27) $nrtrump++;
930 echo "Player $name has $nrtrump trump. Do you want to take them?".
931 "<a href=\"index.php?action=game&me=$me&trump=$user\">Yes</a> <br />\n";
934 echo "<a href=\"index.php?action=game&me=$me&trump=no\">No way</a> <br />\n";
935 echo "</div><div>\n";
937 echo "<div class=\"mycards\">Your cards are: <br />\n";
938 foreach($mycards as $card)
939 display_card($card,$PREF['cardset']);
940 echo "</div></div>\n";
946 $trump = $_REQUEST['trump'];
950 /* user doesn't want to take trump */
951 DB_set_hand_status_by_hash($me,'play');
953 /* set next player who needs to be asked and email him*/
954 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
955 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
957 /* don't ask people who have poverty */
959 if($firstsick=='poverty')
961 if($secondsick=='poverty')
966 if($gametype=='dpoverty')
968 $next=999; /* need to cancel for sure, since both would need to take the trump */
971 /* no more people to ask, need to cancel the game */
974 $message = "Hello, \n\n".
975 "Game ".DB_format_gameid($gameid)." has been canceled since nobody wanted to take the trump.\n\n";
977 $userids = DB_get_all_userid_by_gameid($gameid);
978 foreach($userids as $user)
980 $subject = 'Game '.DB_format_gameid($gameid).' canceled (poverty not resolved)';
981 mymail($user,$subject,$message);
984 /* update game status */
985 cancel_game('trump',$gameid);
987 echo "<p class=\"message\";>Game ".DB_format_gameid($gameid)." has been canceled.<br /><br /></p>";
992 /* email next player, set his status to poverty */
993 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
994 $userid = DB_get_userid('hash',$userhash);
996 DB_set_player_by_gameid($gameid,$userid);
997 DB_set_hand_status_by_hash($userhash,'poverty');
999 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
1000 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
1001 $subject = 'Poverty (game '.DB_format_gameid($gameid).')';
1002 mymail($userid,$subject,$message);
1007 /* player wants to take trump, change cards */
1009 /* user wants to take trump */
1010 $trump = $_REQUEST['trump'];
1011 $userhand = DB_get_handid('gameid-userid',$gameid,$trump);
1012 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
1014 /* remember which cards were handed over*/
1015 $partnerhand = DB_get_all_hand($userhash);
1016 foreach ($partnerhand as $card)
1018 DB_add_exchanged_card($card,$userhand,$myhand);
1020 /* copy trump from player A to B */
1021 $result = DB_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
1024 $mycards = DB_get_hand($me);
1027 if($gametype=='poverty')
1029 $userids = DB_get_all_userid_by_gameid($gameid);
1030 foreach($userids as $user)
1032 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1033 if($hash==$userhash||$hash==$me)
1035 DB_set_party_by_hash($hash,'re');
1039 DB_set_party_by_hash($hash,'contra');
1040 DB_set_hand_status_by_hash($hash,'play'); /* the contra party is ready to play */
1043 /* check if we are done (in case of no trump handed over), if so, go to 'play' phase right away*/
1044 if(count($mycards)==12)
1046 DB_set_hand_status_by_hash($me,'play');
1051 /* has the re party already been set?*/
1053 $userids = DB_get_all_userid_by_gameid($gameid);
1054 foreach($userids as $user)
1056 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1057 $party = DB_get_party_by_hash($hash);
1063 DB_set_party_by_hash($me,'contra');
1064 DB_set_party_by_hash($userhash,'contra');
1068 DB_set_party_by_hash($me,'re');
1069 DB_set_party_by_hash($userhash,'re');
1071 /* send out email to second non-poverty player */
1072 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
1073 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
1076 if($firstsick=='poverty')
1077 if($secondsick=='poverty')
1083 echo "<div class=\"message\">Error in poverty, please contact the Admin</div>\n";
1085 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
1086 $userid = DB_get_userid('hash',$userhash);
1088 DB_set_player_by_gameid($gameid,$userid);
1089 DB_set_hand_status_by_hash($userhash,'poverty');
1091 $message = "Two people have poverty, it's your turn to decide, if you want to take the trump. Please visit:".
1092 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
1093 $subject = 'Double poverty (game '.DB_format_gameid($gameid).')';
1094 mymail($userid,$subject,$message);
1097 echo "<div class=\"message\">Please, <a href=\"$INDEX?action=game&me=$me\">continue</a> here.</div>\n";
1105 /* both entries here, so that the tricks are visible for both.
1106 * in case of 'play' there is a break later that skips the last part
1109 /* first check if the game has been canceled and display */
1112 case 'cancel-noplay':
1113 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>";
1115 case 'cancel-timedout':
1116 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>";
1118 case 'cancel-nines':
1119 case 'cancel-timedout':
1120 echo "<div class=\"message\"><p>The game has been canceled because one player had too many nines.</p></div>";
1122 case 'cancel-trump':
1123 echo "<div class=\"message\"><p>The game has been canceled because nobody wanted to take the trump.</p></div>";
1126 /* for these two types, we shouldn't show the cards, since we might want to restart the game */
1127 if (in_array($gamestatus,array('cancel-noplay','cancel-timedout')))
1130 /* check if all players are ready to play,
1131 * if so, send out email to the startplayer
1132 * only need to do this if the game hasn't started yet
1134 $gamestatus = DB_get_game_status_by_gameid($gameid);
1135 if($gamestatus == 'pre')
1138 $userids = DB_get_all_userid_by_gameid($gameid);
1139 foreach($userids as $user)
1141 $userstatus = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
1142 if($userstatus !='play' && $userstatus!='gameover')
1145 DB_set_player_by_gameid($gameid,$user);
1151 /* only set this after all poverty, etc. are handled*/
1152 DB_set_game_status_by_gameid($gameid,'play');
1154 /* email startplayer */
1155 $startplayer = DB_get_startplayer_by_gameid($gameid);
1156 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
1157 $who = DB_get_userid('hash',$hash);
1158 DB_set_player_by_gameid($gameid,$who);
1160 if($hash!=$me && DB_get_email_pref_by_hash($hash)!='emailaddict')
1162 /* email startplayer) */
1163 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
1164 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
1165 $subject = 'Ready, set, go... (game '.DB_format_gameid($gameid).')';
1166 mymail($who,$subject,$message);
1170 /* figure out what kind of game we are playing,
1171 * set the global variables $CARDS['trump'],$CARDS['diamonds'],$CARDS['hearts'],
1172 * $CARDS['clubs'],$CARDS['spades'],$CARDS['foxes']
1176 $gametype = DB_get_gametype_by_gameid($gameid);
1178 if($gametype=='solo')
1180 $gametype = DB_get_solo_by_gameid($gameid);
1181 if($gametype=='silent')
1184 $GT = $gametype.' '.$GT;
1187 $gametype = 'normal';
1189 set_gametype($gametype); /* this sets the $CARDS variable */
1191 /* get some infos about the game, need to reset this, since it might have changed */
1192 $gamestatus = DB_get_game_status_by_gameid($gameid);
1194 /* has the game started? No, then just wait here...*/
1195 if($gamestatus == 'pre')
1197 echo "<p class=\"message\"> You finished the setup, but not everyone else finished it... ".
1198 "You need to wait for the others. Just wait for an email. </p>";
1200 $mycards = DB_get_hand($me);
1201 $mycards = mysort($mycards,$gametype);
1203 echo "<div class=\"mycards\">Your cards are: <br />\n";
1204 foreach($mycards as $card)
1205 display_card($card,$PREF['cardset']);
1208 break; /* not sure this works... the idea is that you can
1209 * only play a card after everyone is ready to play */
1213 /* get time from the last action of the game */
1214 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1215 $gameend = time() - strtotime($r[0]);
1217 /* handle comments in case player didn't play a card, allow comments a week after the end of the game */
1218 if( (!myisset('card') && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
1219 if(myisset('comment'))
1221 $comment = $_REQUEST['comment'];
1222 $playid = DB_get_current_playid($gameid);
1225 DB_insert_comment($comment,$playid,$myid);
1228 /* get everything relevant to display the tricks */
1229 $result = DB_query("SELECT Hand_Card.card_id as card,".
1230 " Hand.position as position,".
1231 " Play.sequence as sequence, ".
1233 " GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
1234 " SEPARATOR '\n' ), ".
1235 " Play.create_date, ".
1238 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1239 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1240 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1241 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1242 "LEFT JOIN User On User.id=Comment.user_id ".
1243 "WHERE Trick.game_id='".$gameid."' ".
1244 "GROUP BY Trick.id, sequence ".
1245 "ORDER BY Trick.id, sequence ASC");
1247 $lasttrick = DB_get_max_trickid($gameid);
1249 $play = array(); /* needed to calculate winner later */
1251 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1252 $firstcard = ''; /* first card in a trick */
1254 echo "\n<ul class=\"tricks\">\n";
1255 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
1257 /* output vorbehalte */
1258 $mygametype = DB_get_gametype_by_gameid($gameid);
1259 if($mygametype != 'normal' && $mygametype != 'silent') /* only show when needed */
1261 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
1262 " <div class=\"trick\" id=\"trick0\">\n";
1264 /* get information so show the cards that have been handed over in a poverty game */
1265 output_exchanged_cards();
1267 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1271 while($r = DB_fetch_array($result))
1279 /* count number of tricks */
1283 /* check if first schweinchen has been played */
1284 if( $GAME['schweinchen-who'] && ($r[0] == 19 || $r[0] == 20) )
1285 if(!$GAME['schweinchen-first'])
1286 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1288 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1290 /* save card to be able to find the winner of the trick later */
1291 $play[$seq] = array('card'=>$r[0],'pos'=>$pos);
1295 /* first card in a trick, output some html */
1296 if($trick!=$lasttrick)
1298 /* start of an old trick? */
1299 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1300 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1301 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1303 else if($trick==$lasttrick)
1305 /* start of a last trick? */
1306 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1307 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1308 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1311 /* remember first card, so that we are able to check, what cards can be played */
1316 echo " <div class=\"card".($pos-1)."\">\n";
1318 /* display comments */
1320 echo " <span class=\"comment\">".$comment."</span>\n";
1323 display_card($r[0],$PREF['cardset']);
1325 echo " </div>\n"; /* end div card */
1330 $winner = get_winner($play,$gametype); /* returns the position */
1331 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1335 /* whos turn is it? */
1338 $winner = get_winner($play,$gametype); /* returns the position */
1340 $firstcard = ''; /* new trick, no first card */
1345 if($next==5) $next = 1;
1348 /* my turn?, display cards as links, ask for comments*/
1349 if(DB_get_pos_by_hash($me) == $next)
1354 /* do we want to play a card? */
1355 if(myisset('card') && $myturn)
1357 $card = $_REQUEST['card'];
1358 $handid = DB_get_handid('hash',$me);
1359 $commentSchweinchen =''; /* used to add a comment when Schweinchen is being played */
1361 /* check if we have card and that we haven't played it yet*/
1362 /* set played in hand_card to true where hand_id and card_id*/
1363 $r = DB_query_array("SELECT id FROM Hand_Card WHERE played='false' and ".
1364 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1365 $handcardid = $r[0];
1367 if($handcardid) /* everything ok, play card */
1369 /* update Game timestamp */
1370 DB_update_game_timestamp($gameid);
1372 /* mark card as played */
1373 DB_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1374 DB_quote_smart($card));
1376 /* get trick id or start new trick */
1377 $a = DB_get_current_trickid($gameid);
1382 $playid = DB_play_card($trickid,$handcardid,$sequence);
1384 /* check special output for schweinchen in case in case a fox is being played
1385 * check for correct rules, etc. has already been done
1387 if( $GAME['schweinchen-who'] && ($card == 19 || $card == 20) )
1389 if(!$GAME['schweinchen-first'])
1390 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1392 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1394 if( $RULES['schweinchen']=='both' ||
1395 ($RULES['schweinchen']=='second' && $GAME['schweinchen-second']==1 )||
1396 ($RULES['schweinchen']=='secondaftercall' && $GAME['schweinchen-second']==1 &&
1397 (DB_get_call_by_hash($GAME['schweinchen-who']) || DB_get_partner_call_by_hash($GAME['schweinchen-who']) ))
1400 DB_insert_comment('Schweinchen! ',$playid,$myid);
1401 $commentSchweinchen = 'Schweinchen! ';
1404 echo 'schweinchen = '.$GAME['schweinchen-who'].' ---<br />';
1407 /* if sequence == 4 check who one in case of wedding */
1408 if($sequence == 4 && $GT == 'wedding')
1410 /* is wedding resolve */
1411 $resolved = DB_get_sickness_by_gameid($gameid);
1414 /* who has wedding */
1415 $userids = DB_get_all_userid_by_gameid($gameid);
1416 foreach($userids as $user)
1418 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1419 if($usersick == 'wedding')
1422 /* who won the trick */
1423 $play = DB_get_cards_by_trick($trickid);
1424 $winner = get_winner($play,$gametype); /* returns the position */
1425 $winnerid = DB_get_userid('gameid-position',$gameid,$winner);
1426 /* is tricknr <=3 */
1427 if($tricknr <=3 && $winnerid!=$whosick)
1429 /* set resolved at tricknr*/
1430 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1432 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1433 DB_set_party_by_hash($whash,'re');
1435 if($tricknr == 3 && $winnerid==$whosick)
1437 /* set resolved at tricknr*/
1438 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1443 /* if sequence == 4, set winner of the trick, count points and set the next player */
1446 $play = DB_get_cards_by_trick($trickid);
1447 $winner = get_winner($play,$gametype); /* returns the position */
1450 * check if someone caught a fox
1451 *******************************/
1453 /* first check if we should account for solos at all,
1454 * since it doesn't make sense in some games
1456 $ok = 0; /* fox shouldn't be counted */
1457 if(DB_get_gametype_by_gameid($gameid)=='solo')
1459 $solo = DB_get_solo_by_gameid($gameid);
1460 if($solo == 'trump' || $solo == 'silent')
1461 $ok = 1; /* for trump solos and silent solos, foxes are ok */
1464 $ok = 1; /* for all other games (not solos) foxes are ok too */
1467 foreach($play as $played)
1469 if ( $played['card']==19 || $played['card']==20 )
1470 if ($played['pos']!= $winner )
1472 /* possible caught a fox, check party */
1473 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1474 $uid2 = DB_get_userid('gameid-position',$gameid,$played['pos']);
1476 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1477 $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1479 if($party1 != $party2)
1480 DB_query("INSERT INTO Score".
1481 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1486 * check for karlchen (jack of clubs in the last trick)
1487 ******************************************************/
1489 /* same as for foxes, karlchen doesn't always make sense
1490 * check what kind of game it is and set karlchen accordingly */
1491 $ok = 1; /* default: karlchen should be accounted for */
1493 $ok = 0; /* Karlchen works only in the last trick */
1494 if($ok && DB_get_gametype_by_gameid($gameid)=='solo' )
1496 $solo = DB_get_solo_by_gameid($gameid);
1497 if($solo == 'trumpless' || $solo == 'jack' || $solo == 'queen' )
1498 $ok = 0; /* no Karlchen in these solos */
1502 foreach($play as $played)
1503 if ( $played['card']==11 || $played['card']==12 )
1504 if ($played['pos'] == $winner )
1506 /* possible caught a fox, check party */
1507 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1508 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1510 DB_query("INSERT INTO Score".
1511 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1514 * check for doppelopf (>40 points)
1515 ***********************************/
1518 foreach($play as $played)
1520 $points += DB_get_card_value_by_cardid($played['card']);
1524 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1525 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1527 DB_query("INSERT INTO Score".
1528 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
1532 * set winner (for this trick)
1536 DB_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1538 echo "ERROR during scoring";
1541 echo "DEBUG: position $winner won the trick <br />";
1543 /* who is the next player? */
1548 $next = DB_get_pos_by_hash($me)+1;
1550 if($next==5) $next=1;
1552 /* check for coment */
1553 if(myisset('comment'))
1555 $comment = $_REQUEST['comment'];
1557 DB_insert_comment($comment,$playid,$myid);
1558 if($commentSchweinchen)
1559 $comment = $commentSchweinchen . $comment;
1562 /* display played card */
1563 $pos = DB_get_pos_by_hash($me);
1566 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1567 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1568 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1571 echo " <div class=\"card".($pos-1)."\">\n ";
1573 /* display comments */
1574 display_card($card,$PREF['cardset']);
1576 echo "\n <span class=\"comment\"> ".$comment."</span>\n";
1579 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1581 /*check if we still have cards left, else set status to gameover */
1582 if(sizeof(DB_get_hand($me))==0)
1584 DB_set_hand_status_by_hash($me,'gameover');
1585 $mystatus = 'gameover';
1588 /* if all players are done, set game status to game over,
1589 * get the points of the last trick and send out an email
1592 $userids = DB_get_all_userid_by_gameid($gameid);
1595 foreach($userids as $user)
1596 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1600 DB_set_game_status_by_gameid($gameid,'gameover');
1602 /* email next player, if game is still running */
1603 if(DB_get_game_status_by_gameid($gameid)=='play')
1605 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1606 $who = DB_get_userid('hash',$next_hash);
1607 DB_set_player_by_gameid($gameid,$who);
1609 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1610 "It's your turn now.\n".
1611 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$next_hash."\n\n" ;
1612 if( DB_get_email_pref_by_uid($who)!='emailaddict' )
1614 $subject = 'A card has been played in game '.DB_format_gameid($gameid);
1615 mymail($who,$subject,$message);
1618 else /* send out final email */
1620 /* individual score */
1621 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1622 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1623 " LEFT JOIN User ON User.id=Hand.user_id".
1624 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1625 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1626 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1627 " WHERE Hand.game_id='$gameid'".
1628 " GROUP BY User.fullname" );
1629 $message = "The game is over. Thanks for playing :)\n";
1630 $message .= "Final score:\n";
1631 while( $r = DB_fetch_array($result) )
1632 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1634 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1635 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1636 " LEFT JOIN User ON User.id=Hand.user_id".
1637 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1638 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1639 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1640 " WHERE Hand.game_id='$gameid'".
1641 " GROUP BY Hand.party" );
1642 $message .= "\nTotals:\n";
1645 while( $r = DB_fetch_array($result) )
1647 $message .= " ".$r[0]." ".$r[1]."\n";
1650 else if($r[0] == 'contra')
1655 * save score in database
1659 /* get calls from re/contra */
1661 $call_contra = NULL;
1662 foreach($userids as $user)
1664 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1665 $call = DB_get_call_by_hash($hash);
1666 $party = DB_get_party_by_hash($hash);
1670 $call = (int) $call;
1676 else if( $call < $call_re)
1679 else if($party=='contra')
1681 if($call_contra==NULL)
1682 $call_contra = $call;
1683 else if( $call < $call_contra)
1684 $call_contra = $call;
1689 /* figure out who one */
1690 $winning_party = NULL;
1692 if($call_re == NULL && $call_contra==NULL)
1694 /* nobody made a call, so it's easy to figure out who won */
1696 $winning_party='re';
1698 $winning_party='contra';
1702 /* if one party makes a call, they only win, iff they make enough points
1703 * if only one party made a call, the other one wins,
1704 * if the first one didn't make it
1708 $offset = 120 - $call_re;
1710 $offset--; /* since we use a > in the next equation */
1712 if($re > 120+$offset)
1713 $winning_party='re';
1714 else if ($call_contra == NULL )
1715 $winning_party='contra';
1720 $offset = 120 - $call_contra;
1721 if($call_contra == 0)
1722 $offset--; /* since we use a > in the next equation */
1724 if($contra > 120+$offset)
1725 $winning_party='contra';
1726 else if ($call_re == NULL )
1727 $winning_party='re';
1731 /* one point for each call of the other party in case the other party didn't win
1732 * and one point each in case the party made more than points than one of the calls
1734 if($winning_party!='contra' && $call_contra!=NULL)
1736 for( $p=$call_contra;$p<=120; $p+=30 )
1738 DB_query("INSERT INTO Score".
1739 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
1742 for( $p=$call_contra; $p<120; $p+=30)
1745 DB_query("INSERT INTO Score".
1746 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'made$p')");
1749 if($winning_party!='re' and $call_re!=NULL)
1751 for( $p=$call_re;$p<=120; $p+=30 )
1753 DB_query("INSERT INTO Score".
1754 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'against$p')");
1757 for( $p=$call_re; $p<120; $p+=30)
1760 DB_query("INSERT INTO Score".
1761 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
1765 /* point in case contra won */
1766 if($winning_party=='contra')
1768 DB_query("INSERT INTO Score".
1769 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1772 /* one point each for winning and each 30 points + calls */
1773 if($winning_party=='re')
1775 foreach(array(120,150,180,210,240) as $p)
1778 if($p==240 || $call_contra!=NULL)
1782 DB_query("INSERT INTO Score".
1783 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
1785 /* re called something and won */
1786 foreach(array(0,30,60,90,120) as $p)
1788 if($call_re!=NULL && $call_re<$p+1)
1789 DB_query("INSERT INTO Score".
1790 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
1793 else if( $winning_party=='contra')
1795 foreach(array(120,150,180,210,240) as $p)
1798 if($p==240 || $call_re!=NULL)
1801 if($contra>$p-$offset)
1802 DB_query("INSERT INTO Score".
1803 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
1805 /* re called something and won */
1806 foreach(array(0,30,60,90,120) as $p)
1808 if($call_contra!=NULL && $call_contra<$p+1)
1809 DB_query("INSERT INTO Score".
1810 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
1815 /* add score points to email */
1818 $message .= " Points Re: \n";
1819 $queryresult = DB_query("SELECT score FROM Score ".
1820 " WHERE game_id=$gameid AND party='re'".
1822 while($r = DB_fetch_array($queryresult) )
1824 $message .= " ".$r[0]."\n";
1827 $message .= " Points Contra: \n";
1828 $queryresult = DB_query("SELECT score FROM Score ".
1829 " WHERE game_id=$gameid AND party='contra'".
1831 while($r = DB_fetch_array($queryresult) )
1833 $message .= " ".$r[0]."\n";
1836 $message .= " Total Points (from the Re point of view): $Tpoint\n";
1839 $session = DB_get_session_by_gameid($gameid);
1840 $score = generate_score_table($session);
1842 $message .= "Score Table:\n";
1843 $message .= format_score_table_ascii($score);
1844 $message .= "\nUse these links to have a look at game ".DB_format_gameid($gameid).": \n";
1846 /* send out final email */
1847 foreach($userids as $user)
1849 /* add links for all players */
1850 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1851 $name = DB_get_name('userid',$user);
1853 $link = "$name: ".$HOST.$INDEX."?action=game&me=".$hash."\n" ;
1856 $message .= "\n\n (you can use reply all on this email to reach all the players.)\n\n";
1857 $subject = ' Game over (game '.DB_format_gameid($gameid).') ';
1858 mymail($userids,$subject,$message);
1863 echo "can't find that card?! <br />\n";
1866 else if(myisset('card') && !$myturn )
1868 echo "please wait until it's your turn! <br />\n";
1871 if($seq!=4 && $trickNR>=1 && !(myisset('card') && $myturn) )
1872 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1874 /* display points in case game is over */
1875 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1877 echo " <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1878 " <div class=\"trick\" id=\"trick13\">\n";
1879 /* add pic for re/contra
1880 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1882 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1883 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1884 " LEFT JOIN User ON User.id=Hand.user_id".
1885 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1886 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1887 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1888 " WHERE Hand.game_id='$gameid'".
1889 " GROUP BY User.fullname" );
1890 while( $r = DB_fetch_array($result))
1891 echo " <div class=\"card".($r[3]-1)."\">\n".
1892 " <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
1895 /* display totals */
1896 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1897 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1898 " LEFT JOIN User ON User.id=Hand.user_id".
1899 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1900 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1901 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1902 " WHERE Hand.game_id='$gameid'".
1903 " GROUP BY Hand.party" );
1904 echo "<div class=\"total\">\n Totals:<br />\n";
1905 while( $r = DB_fetch_array($result))
1906 echo " ".$r[0]." ".$r[1]."<br />\n";
1908 $queryresult = DB_query("SELECT timediff(mod_date,create_date) ".
1909 " FROM Game WHERE id='$gameid'");
1910 $r = DB_fetch_array($queryresult);
1911 echo " <p>This game took ".$r[0]." hours.</p>\n";
1913 echo " <div class=\"re\">\n Points Re: <br />\n";
1914 $queryresult = DB_query("SELECT score FROM Score ".
1915 " WHERE game_id=$gameid AND party='re'".
1917 while($r = DB_fetch_array($queryresult) )
1918 echo " ".$r[0]."<br />\n";
1921 echo " <div class=\"contra\">\n Points Contra: <br />\n";
1922 $queryresult = DB_query("SELECT score FROM Score ".
1923 " WHERE game_id=$gameid AND party='contra'".
1925 while($r = DB_fetch_array($queryresult) )
1926 echo " ".$r[0]."<br />\n";
1931 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1934 echo " <li onclick=\"hl_prev();\" class=\"old\"><a href=\"#\">prev</a></li>\n";
1935 echo " <li onclick=\"hl_next();\" class=\"old\"><a href=\"#\">next</a></li>\n";
1936 echo "</ul>\n"; /* end ul tricks*/
1938 $mycards = DB_get_hand($me);
1939 $mycards = mysort($mycards,$gametype);
1940 echo "<div class=\"mycards\">\n";
1942 if($myturn && !myisset('card') && $mystatus=='play' )
1944 echo "Hello ".$myname.", it's your turn! <br />\n";
1945 echo "Your cards are: <br />\n";
1947 /* do we have to follow suite? */
1949 if(have_suit($mycards,$firstcard))
1952 foreach($mycards as $card)
1954 /* display only cards that the player is allowed to play as links, the rest just display normal
1955 * also check if we have both schweinchen, in that case only display on of them as playable
1957 if( ($followsuit && !same_type($card,$firstcard)) ||
1958 ( (int)($card)==19 &&
1959 !$GAME['schweinchen-first'] &&
1960 ( $RULES['schweinchen']=='second' ||
1961 ( $RULES['schweinchen']=='secondaftercall' &&
1962 (DB_get_call_by_hash($GAME['schweinchen-who']) ||
1963 DB_get_partner_call_by_hash($GAME['schweinchen-who']) )
1966 $GAME['schweinchen-who']==$me &&
1967 in_array($gametype,array('normal','wedding','trump','silent'))
1970 display_card($card,$PREF['cardset']);
1972 display_link_card($card,$PREF['cardset']);
1975 else if($mystatus=='play' )
1977 echo "Your cards are: <br />\n";
1978 foreach($mycards as $card)
1979 display_card($card,$PREF['cardset']);
1981 else if($mystatus=='gameover')
1983 $oldcards = DB_get_all_hand($me);
1984 $oldcards = mysort($oldcards,$gametype);
1986 if(isset($_SESSION['id']) && $myid==$_SESSION['id'])
1987 echo "Your cards were: <br />\n";
1990 $name = DB_get_name('userid',$myid);
1991 echo "$name's were: <br />\n";
1994 foreach($oldcards as $card)
1995 display_card($card,$PREF['cardset']);
1997 $userids = DB_get_all_userid_by_gameid($gameid);
1998 foreach($userids as $user)
2000 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
2006 $name = DB_get_name('userid',$user);
2007 $oldcards = DB_get_all_hand($userhash);
2008 $oldcards = mysort($oldcards,$gametype);
2009 echo "$name's cards were: <br />\n";
2010 foreach($oldcards as $card)
2011 display_card($card,$PREF['cardset']);
2017 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
2018 if($mystatus=='play')
2021 /* the following happens only when the gamestatus is 'gameover' */
2022 /* check if game is over, display results */
2023 if(DB_get_game_status_by_gameid($gameid)=='play')
2025 echo "The game is over for you.. other people still need to play though";
2029 myerror("error in testing the status");
2032 /* output other games where it is the users turn
2033 * make sure that the people looking at old games don't see the wrong games here
2035 if( $gamestatus != 'gameover' )
2037 /* game isn't over, only valid user can get here, so show menu */
2038 display_user_menu($myid);
2040 else if( $origmystatus != 'gameover' )
2042 /* user just played the very last card, game is now over, it's still ok to show the menu though */
2043 display_user_menu($myid);
2045 else if( $mystatus == 'gameover' &&
2046 isset($_SESSION['id']) )
2048 /* user is looking at someone else's game, show the menu for the correct user */
2049 display_user_menu($_SESSION['id']);
2053 echo "<div class=\"usermenu\">\n";
2054 echo "It's your turn in these games:<br />\n";
2055 echo "Please log in to see this information.\n";
2059 /* display rule set for this game */
2060 echo "<div class=\"gameinfo\">\n";
2062 if($gamestatus == 'play' )
2064 $myparty = DB_get_party_by_hash($me);
2065 output_form_calls($me,$myparty);
2067 /* get time from the last action of the game */
2068 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
2069 $gameend = time() - strtotime($r[0]);
2071 if($gamestatus == 'play' || $gameend < 60*60*24*7)
2073 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
2076 echo "<input type=\"submit\" value=\"submit\" />\n";
2078 /* has this hand been played by others? */
2079 $other_game_ids = DB_played_by_others($gameid);
2080 if(sizeof($other_game_ids)>0 && $mystatus=='gameover')
2082 $mypos = DB_get_pos_by_hash($me);
2083 echo "<p>See how other played the same hand: <br />\n";
2084 foreach($other_game_ids as $id)
2086 $otherhash = DB_get_hash_from_game_and_pos($id,$mypos);
2087 $othername = DB_get_name('hash',$otherhash);
2088 echo "<a href=\"$INDEX?action=game&me=$otherhash\">$othername</a><br />";
2097 $gamestatus = DB_get_game_status_by_gameid($gameid);
2098 if($mystatus=='gameover' &&
2099 ($gamestatus =='gameover' || $gamestatus =='cancel-nines' || $gamestatus =='cancel-trump') &&
2100 isset($_SESSION['id']) && $_SESSION['id']==$myid)
2102 $session = DB_get_session_by_gameid($gameid);
2103 $result = DB_query("SELECT id,create_date FROM Game".
2104 " WHERE session=$session".
2105 " ORDER BY create_date DESC".
2109 $r = DB_fetch_array($result);
2111 if(!$session || $gameid==$r[0])
2113 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
2114 $names = DB_get_all_names_by_gameid($gameid);
2115 $type = DB_get_gametype_by_gameid($gameid);
2119 $solo = DB_get_solo_by_gameid($gameid);
2121 if($solo!='silent') /* repeat game with same first player */
2122 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
2123 else /* rotate normally */
2124 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
2126 else if($gamestatus == 'cancel-nines' || $gamestatus == 'cancel-trump')
2127 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
2128 else /* rotate normally */
2129 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);