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 /**************************************
47 * get some information from the DB
48 **************************************/
49 $gameid = DB_get_gameid_by_hash($me);
50 $myname = DB_get_name('hash',$me);
51 $mystatus = DB_get_status_by_hash($me);
52 $origmystatus = DB_get_status_by_hash($me); /* to show "it's your turn" menu when game has just finished */
53 $mypos = DB_get_pos_by_hash($me);
54 $myhand = DB_get_handid('hash',$me);
55 $myparty = DB_get_party_by_hash($me);
56 $session = DB_get_session_by_gameid($gameid);
58 /* get prefs and save them in a variable*/
59 $PREF = DB_get_PREF(isset($_SESSION['id'])?$_SESSION['id']:$myid);
61 /* get rule set for this game */
62 $RULES = DB_get_RULES($gameid);
64 /* get some infos about the game */
65 $gametype = DB_get_gametype_by_gameid($gameid);
66 $gamestatus = DB_get_game_status_by_gameid($gameid);
70 $gametype = DB_get_solo_by_gameid($gameid);
71 $GT = $gametype.' '.$GT;
74 /* do we need to worry about Schweinchen?
75 * check gametype and rules
76 * if yes, figure out if someone actually has Schweinchen
77 * save information in $GAME
80 if( $gamestatus == 'pre' )
82 /* always need to use Schweinchen to figure out for example who has poverty */
84 /* unless the gametype is set and we know that we are in poverty were schweinchen is not valid */
85 if( in_array( $gametype,array('poverty','dpoverty') ))
90 /* in a game Schweinchen is not valid in all types of games */
91 if( in_array($gametype,array('normal','wedding','trump','silent') ))
92 if( in_array($RULES['schweinchen'],array('both','second','secondaftercall')) )
96 /* these are the defaults */
97 $GAME['schweinchen-who'] = NULL;
98 $GAME['schweinchen-first'] = NULL;
99 $GAME['schweinchen-second'] = NULL;
103 /* need to check for Schweinchen */
106 $hash = DB_get_hash_from_game_and_pos($gameid,$i);
107 $cards = DB_get_all_hand($hash);
108 if( in_array('19',$cards) && in_array('20',$cards) )
109 $GAME['schweinchen-who']=$hash;
111 $GAME['schweinchen-first'] = 0; /* to keep track if they have been played already */
112 $GAME['schweinchen-second'] = 0;
114 /* end check for Schweinchen */
116 /* set the $CARDS variable, needed for sorting the cards
117 * we set it to normal so that the pre-game phase is handled ok
118 * and later set it to the correct game type that is played
120 set_gametype('normal');
122 /* handle user notes (only possible while game is running)*/
123 if( $mystatus!='gameover' )
126 $note = $_REQUEST['note'];
129 DB_insert_note($note,$gameid,$myid);
132 /* handle calls, output a comment to show when the call was made */
133 /* initialize comments */
136 /* get information needed to submit comment */
137 $playid = DB_get_current_playid($gameid);
141 DB_insert_comment($comment,$playid,$myid);
144 /* end check for calls */
147 /* check for calls, set comment */
148 if(myisset('call') && $_REQUEST['call'] == '120' && can_call(120,$me))
150 $result = DB_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
153 else if($myparty=='contra')
154 $comment .= "Contra";
156 if(myisset('call') && $_REQUEST['call'] == '90' && can_call(90,$me))
158 $result = DB_query("UPDATE Hand SET point_call='90' WHERE hash='$me' ");
161 if(myisset('call') && $_REQUEST['call'] == '60' && can_call(60,$me))
163 $result = DB_query("UPDATE Hand SET point_call='60' WHERE hash='$me' ");
166 if(myisset('call') && $_REQUEST['call'] == '30' && can_call(30,$me))
168 $result = DB_query("UPDATE Hand SET point_call='30' WHERE hash='$me' ");
171 if(myisset('call') && $_REQUEST['call'] == '0' && can_call(0,$me))
173 $result = DB_query("UPDATE Hand SET point_call='0' WHERE hash='$me' ");
178 /*****************************************************************
179 * output extra division in case this game is part of a session
180 *****************************************************************/
183 echo "<div class=\"session\">\n";
185 /* output rule set */
186 echo " <div class=\"sessionrules\">\n "._('Rules').":\n";
187 switch($RULES['dullen'])
190 echo " <img class=\"rulesicon\" alt=\""._('no ten of hearts').
191 "\" src=\"pics/button/no-ten-of-hearts.png\"/>\n"; break;
193 echo " <img class=\"rulesicon\" alt=\""._('ten of hearts').
194 "\" src=\"pics/button/ten-of-hearts.png\"/>\n"; break;
196 echo " <img class=\"rulesicon\" alt=\""._('second ten of hearts').
197 "\" src=\"pics/button/second-ten-of-hearts.png\"/>\n"; break;
199 switch($RULES['schweinchen'])
202 echo " <img class=\"rulesicon\" alt=\""._('no schweinchen')."\" ".
203 "src=\"pics/button/no-schweinchen.png\"/>\n"; break;
205 echo " <img class=\"rulesicon\" alt=\""._('two schweinchen')."\" ".
206 "src=\"pics/button/two-schweinchen.png\"/>\n"; break;
208 echo " <img class=\"rulesicon\" alt=\"".('second schweinchen')."\" ".
209 "src=\"pics/button/second-schweinchen.png\"/>\n"; break;
210 case 'secondaftercall':
211 echo " <img class=\"rulesicon\" alt=\""._('second schweinchen after call')."\" ".
212 "src=\"pics/button/second-schweinchen-after-call.png\"/>\n"; break;
214 switch($RULES['call'])
217 echo " <img class=\"rulesicon\" alt=\""._('1st-own-card')."\" src=\"pics/button/1st-own-card.png\"/>\n"; break;
219 echo " <img class=\"rulesicon\" alt=\""._('5th-card')."\" src=\"pics/button/5th-card.png\"/>\n"; break;
221 echo " <img class=\"rulesicon\" alt=\""._('9-cards')."\" src=\"pics/button/9-cards.png\"/>\n"; break;
224 echo ' '._('10ofhearts').": {$RULES['dullen']} <br />\n";
225 echo ' '._('schweinchen').": {$RULES['schweinchen']} <br />\n";
226 echo ' '._('call').": {$RULES['call']} <br />\n";
227 echo ' '._('lowtrump').": {$RULES['lowtrump']} <br />\n";
228 echo " </div>\n </div>\n";
232 echo " <div class=\"sessionscore\">";
234 $score = generate_score_table($session);
236 /* get the last entry to show on the main page */
238 $finalscore = array_pop($tmpscore);
239 $finalscore = $finalscore['players'];
243 echo _('Score').": \n";
244 foreach($finalscore as $user=>$value)
246 $name = DB_get_name('userid',$user);
247 echo " ".substr($name,0,2).": $value ";
252 /* first game, no score yet */
256 /* output all games for the score table */
257 echo format_score_table_html($score,$myid);
260 /* figure out which game in a session we are in and link to the
261 * previous and next game if possible
263 $hashes = DB_get_hashes_by_session($session,$myid);
266 foreach($hashes as $hash)
276 $previous = $hashes[$j-2];
284 /* check for solo, add game type to session number */
285 echo " Game $session.$j";
288 if(isset($_SESSION['id']) && $_SESSION['id']==$myid)
291 echo " <a href=\"{$INDEX}?action=game&me=$previous\">"._('previous')."</a> \n";
293 echo " <a href=\"{$INDEX}?action=game&me=$next\">"._('next')."</a> \n";
296 echo " <a href=\"{$INDEX}?action=game&me=$lasthash\">last</a> \n";
301 /* the user has done something, update the timestamp. Use $myid in
302 * active games and check for session-id in old games (myid might be wrong in that case)
304 if($mystatus!='gameover')
305 DB_update_user_timestamp($myid);
307 if(isset($_SESSION['id']))
308 DB_update_user_timestamp($_SESSION['id']);
311 /******************************************************************************
312 * Output tricks played, table, messages, and cards (depending on game status)
313 ******************************************************************************/
315 /* put everyting in a form */
316 echo "<form action=\"index.php?action=game&me=$me\" method=\"post\">\n";
318 /* display the table and the names */
321 /* mystatus gets the player through the different stages of a game.
322 * start: does the player want to play?
323 * init: check for sickness
324 * check: check for return values from init
325 * poverty: handle poverty, wait here until all player have reached this state
326 * display sickness and move on to game
327 * play: game in progress
328 * gameover: are we revisiting a game
331 /* Depending on the situation we set
332 * cards_status (see functions.php for possible options)
334 $cards_status = CARDS_EMPTY;
336 /* Also collect message that should be displayed to the user, so that we can show
337 * them after showing the table. This makes the html flow more consistent and easier
338 * tournament change layouts, especially for smaller displays, e.g. mobile phones
345 /****************************************
346 * ask if player wants to join the game *
347 ****************************************/
349 /* don't ask if user has autosetup set to yest */
351 if($PREF['autosetup']=='yes') $skip = 1;
353 if( !myisset('in') && !$skip)
355 /* asks the player, if he wants to join the game */
356 output_check_want_to_play($me);
357 $card_status = CARDS_EMPTY;
362 /* check the result, if player wants to join, got next stage, else cancel game */
363 if(!$skip && $_REQUEST['in'] == 'no' )
365 /* cancel the game */
366 $email_message = "Hello, \n\n".
367 "the game has been canceled due to the request of one of the players.\n\n";
369 $userids = DB_get_all_userid_by_gameid($gameid);
370 foreach($userids as $user)
372 mymail($user,$gameid,GAME_CANCELED,$email_message);
375 /* update game status */
376 cancel_game('noplay',$gameid);
381 /* user wants to join the game */
383 /* move on to the next stage,
384 * no break statement to immediately go to the next stage
387 DB_set_hand_status_by_hash($me,'init');
389 /* check if everyone has reached this stage, set player in game-table to the next player */
390 $userids = DB_get_all_userid_by_gameid($gameid);
391 foreach($userids as $user)
393 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
394 if($userstat!='init')
396 /* whos turn is it? */
397 DB_set_player_by_gameid($gameid,$user);
404 /***************************
405 * check if player is sick *
406 ***************************/
407 if(!myisset('solo','wedding','poverty','nines','lowtrump') )
409 /* output sickness of other playes, in case they already selected and are sitting in front of the current player */
410 echo "\n<ul class=\"tricks\">\n";
411 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
412 " <div class=\"trick\" id=\"trick0\">\n";
414 for($pos=1;$pos<5;$pos++)
416 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
417 $userid = DB_get_userid('gameid-position',$gameid,$pos);
418 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
420 if($userstatus=='start' || $userstatus=='init')
421 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs <br/ > to decide </div>\n"; /* show this to everyone */
423 if($usersick!=NULL && $pos<=$mypos ) /* only show this for people sitting before the player */
424 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
425 else if($usersick==NULL && $pos<=$mypos)
426 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
428 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
429 /* end displaying sickness */
431 $mycards = DB_get_hand($me);
432 output_check_for_sickness($me,$mycards);
434 $card_status = CARDS_SHOW;
440 /* check if someone selected more than one sickness */
442 if($_REQUEST['solo']!='No') $Nsickness++;
443 if($_REQUEST['wedding'] == 'yes') $Nsickness++;
444 if($_REQUEST['poverty'] == 'yes') $Nsickness++;
445 if($_REQUEST['nines'] == 'yes') $Nsickness++;
446 if($_REQUEST['lowtrump'] == 'yes') $Nsickness++;
450 $messages[] = "You selected more than one sickness, please go back ".
451 "and answer the <a href=\"$INDEX?action=game&me=$me&in=yes\">question</a> again.";
453 $card_status = CARDS_SHOW;
459 /* everything is ok, save what user said and proceed */
460 $messages[] = "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 $messages[] = "<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 $messages[] = _("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 $messages[] = _("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 $messages[] = _("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');
505 else if($_REQUEST['lowtrump'] == 'yes')
507 if($RULES['lowtrump']=='cancel')
508 $messages[] = _("What? You just don't want to play a game because you have low trump? Well, if no one".
509 " is playing solo, this game will be canceled.")."<br />\n";
511 $messages[] = _("Don't think you can win with low trumps...? Ok, poverty chosen.")." <br />.<br />\n";
513 DB_set_sickness_by_hash($me,'lowtrump');
516 /* move on to the next stage*/
517 DB_set_hand_status_by_hash($me,'check');
523 /* here we check what all players said and figure out what game we are playing
524 * this can therefore only be handled once all players finished the last stage
527 /* output sickness of other playes, in case they already selected and are sitting in front of the current player */
528 echo "\n<ul class=\"tricks\">\n";
529 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
530 " <div class=\"trick\" id=\"trick0\">\n";
532 for($pos=1;$pos<5;$pos++)
534 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
535 $userid = DB_get_userid('gameid-position',$gameid,$pos);
536 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
538 if($userstatus=='start' || $userstatus=='init')
539 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs <br />to decide </div>\n"; /* show this to everyone */
541 if($usersick!=NULL) /* in the init-phase we only showed players with $pos<$mypos, now we can show all */
542 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
544 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
546 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
547 /* end displaying sickness */
549 $messages[] = _('Checking if someone else selected solo, nines, wedding or poverty.');
551 /* check if everyone has reached this stage */
552 $userids = DB_get_all_userid_by_gameid($gameid);
554 foreach($userids as $user)
556 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
557 if($userstat!='check')
560 DB_set_player_by_gameid($gameid,$user);
567 $messages[] = _('This step can only be handled after everyone finished the last step. '.
568 'Seems like this is not the case, so you need to wait a bit... '.
569 'you will get an email once that is the case, please use the link in '.
570 'that email to continue the game.');
572 /* display cards, if player was just at the init-phase he will still see the cards from there
573 * we can put this one here, since the last player to finish the init state won't get here and
574 * will still see his card anyway from the init-phase
576 if($mystatus=='check')
578 $card_status = CARDS_SHOW;
583 /* Ok, everyone finished the init-phase, time to figure out what game we
584 * are playing, in case there are any solos this already
585 * will have the correct information in it */
587 $messages[] = _('Ok, everyone is done... figuring out what kind of game we are playing.');
589 $gametype = DB_get_gametype_by_gameid($gameid);
590 $startplayer = DB_get_startplayer_by_gameid($gameid);
592 /* check for sickness */
597 foreach($userids as $user)
599 $name = DB_get_name('userid',$user);
600 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
601 if($usersick == 'nines' || ($RULES['lowtrump']=='cancel' && $usersick=='lowtrump') )
604 $cancelsick = $usersick;
605 break; /* no need to check for other poverties, since only solo can win and that is already set */
607 else if($usersick == 'poverty' || ($RULES['lowtrump']=='poverty' && $usersick=='lowtrump'))
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 if($cancelsick == 'nines')
625 $email_message = "The game has been canceled because ".DB_get_name('userid',$cancel).
626 " has five or more nines and nobody is playing solo.\n\n".
627 "To redeal either start a new game or, in case the game was part of a tournament,\n".
628 "go to the last game and use the link at the bottom of the page to redeal.\n\n";
630 /* update game status */
631 cancel_game('nines',$gameid);
633 $messages[] = "The game has been canceled because ".DB_get_name('userid',$cancel).
634 " has five or more nines and nobody is playing solo.";
636 else if ($cancelsick == 'lowtrump')
638 $email_message = "The game has been canceled because ".DB_get_name('userid',$cancel).
639 " has low trump and nobody is playing solo.\n\n".
640 "To redeal either start a new game or, in case the game was part of a tournament,\n".
641 "go to the last game and use the link at the bottom of the page to redeal.\n\n";
643 /* update game status */
644 cancel_game('lowtrump',$gameid);
646 $messages[] = "The game has been canceled because ".DB_get_name('userid',$cancel).
647 " has low trump and nobody is playing solo.";
650 $userids = DB_get_all_userid_by_gameid($gameid);
651 foreach($userids as $user)
653 mymail($user,$gameid, GAME_CANCELED, $email_message);
658 else if($poverty==1) /* one person has poverty */
660 DB_set_gametype_by_gameid($gameid,'poverty');
661 $gametype = 'poverty';
662 $who = DB_get_sickness_by_gameid($gameid);
665 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
666 if($firstsick == 'poverty' || ($RULES['lowtrump']=='poverty' && $firstsick=='lowtrump'))
667 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
669 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
672 else if($poverty==2) /* two people have poverty */
674 DB_set_gametype_by_gameid($gameid,'dpoverty');
675 $gametype = 'dpoverty';
676 $who = DB_get_sickness_by_gameid($gameid);
679 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
680 if($firstsick == 'poverty' || ($RULES['lowtrump']=='poverty' && $firstsick=='lowtrump'))
682 $secondsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
683 if($secondsick == 'poverty' || ($RULES['lowtrump']=='poverty' && $secondsick=='lowtrump'))
684 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
686 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
689 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
694 DB_set_gametype_by_gameid($gameid,'wedding');
695 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
696 $gametype = 'wedding';
698 /* now the gametype is set correctly in the database */
699 $messages[] = _('Got it').' :)';
701 /* loop over all players, set re/contra if possible and start the game if possible */
702 $userids = DB_get_all_userid_by_gameid($gameid);
703 foreach($userids as $userid)
705 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
710 /* are we the solo player? set us to re, else set us to contra */
711 $pos = DB_get_pos_by_hash($userhash);
712 if($pos == $startplayer)
713 DB_set_party_by_hash($userhash,'re');
715 DB_set_party_by_hash($userhash,'contra');
716 DB_set_hand_status_by_hash($userhash,'play');
720 /* set person with the wedding to re, do the rest during the game */
721 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
722 if($usersick == 'wedding')
723 DB_set_party_by_hash($userhash,'re');
725 DB_set_party_by_hash($userhash,'contra');
727 DB_set_hand_status_by_hash($userhash,'play');
731 $hand = DB_get_all_hand($userhash);
733 if(in_array('3',$hand)||in_array('4',$hand))
734 DB_set_party_by_hash($userhash,'re');
736 DB_set_party_by_hash($userhash,'contra');
737 DB_set_hand_status_by_hash($userhash,'play');
741 /* set person with poverty to play status */
742 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
743 if($usersick == 'poverty' || ($RULES['lowtrump']=='poverty' && $usersick=='lowtrump'))
744 DB_set_hand_status_by_hash($userhash,'play');
746 /* set status of first player to be asked to poverty */
747 $who = DB_get_sickness_by_gameid($gameid);
748 if($who > 6) $who= $who/10; /* in case we have dpoverty */
749 $whoid = DB_get_userid('gameid-position',$gameid,$who);
751 DB_set_hand_status_by_hash($userhash,'poverty');
754 /* check for silent solo, set game type to solo in this case */
755 $gametype = DB_get_gametype_by_gameid($gameid);
756 $userids = DB_get_all_userid_by_gameid($gameid);
757 foreach($userids as $userid)
759 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
761 if($gametype=='normal')
763 $userhand = DB_get_all_hand($userhash);
764 if(check_wedding($userhand))
766 /* normal game type and player has both queens -> silent solo */
767 /* keep startplayer, just set gametype to silent solo */
768 DB_set_gametype_by_gameid($gameid,'solo');
769 DB_set_solo_by_gameid($gameid,'silent');
774 /* send out email to first player or poverty person*/
775 if($gametype!='poverty' && $gametype!='dpoverty')
777 $startplayer = DB_get_startplayer_by_gameid($gameid);
778 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
779 $who = DB_get_userid('hash',$hash);
780 DB_set_player_by_gameid($gameid,$who);
784 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
786 /* email startplayer */
787 $email_message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
788 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
789 mymail($who,$gameid,GAME_READY,$email_message);
793 $messages[] = "Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
797 /* set status of first player to be asked to poverty */
798 $who = DB_get_sickness_by_gameid($gameid);
799 if($who > 6) $who= $who/10; /* in case we have dpoverty */
801 $whoid = DB_get_userid('gameid-position',$gameid,$who);
803 $messages[] = "Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br /\n";
806 $whohash = DB_get_hash_from_game_and_pos($gameid,$who);
807 DB_set_player_by_gameid($gameid,$whoid);
809 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
811 /* email player for poverty */
812 $email_message = "Poverty: It's your turn now in game ".DB_format_gameid($gameid).".\n".
813 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$whohash."\n\n" ;
814 mymail($whoid,$gameid,GAME_POVERTY,$email_message);
818 $card_status = CARDS_SHOW;
823 /* user only gets here in a poverty game, several things have to be handled here:
824 * A) ask, if user wants to take trump
826 * poverty: set re/contra
827 * dpoverty: first time: set re, send email to second player
828 * second time: set contra
829 * poverty: set status of other players to 'play'
830 * set status to play in case 0 trump
831 * no -> set status to play,
832 * ask next player or cancle the game if no more players
833 * B) user took trump and has too many cards (e.g. count(cards)>12 and re/contra set)
834 * ask to give cards back, set status to play, once player has 12 cards
836 * it is easier to check B) first
839 set_gametype($gametype); /* this sets the $CARDS variable */
840 $myparty = DB_get_party_by_hash($me);
842 /* the following is part B) of whats needs to be done)
843 /* check if user wants to give cards back */
844 if(myisset('exchange'))
846 $exchange = $_REQUEST['exchange'];
847 $partnerhash = DB_get_partner_hash_by_hash($me);
848 $partnerid = DB_get_userid('hash',$partnerhash);
849 $partnerhand = DB_get_handid('gameid-userid',$gameid,$partnerid);
851 /* if exchange is set to a value>0, exchange that card back to the partner */
854 $result = DB_query("UPDATE Hand_Card SET hand_id='$partnerhand'".
855 " WHERE hand_id='$myhand' AND card_id=".DB_quote_smart($exchange));
856 DB_add_exchanged_card(DB_quote_smart($exchange),$myhand,$partnerhand);
860 /* output pre-game trick in case user reloads,
861 * only needs to be done when a team has been formed */
862 if($myparty=='re' || $myparty=='contra')
864 echo "\n<ul class=\"tricks\">\n";
866 $mygametype = DB_get_gametype_by_gameid($gameid);
868 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
869 " <div class=\"trick\" id=\"trick0\">\n";
871 /* get information so show the cards that have been handed over in a poverty game */
872 output_exchanged_cards();
874 echo " </div>\n </li>\n</ul>\n\n"; /* end div trick, end li trick , end ul tricks */
876 /* end output pre-game trick */
879 $mycards = DB_get_hand($me);
881 /* default: show cards, will be overwritten, if we need to give back cards */
882 $card_status = CARDS_SHOW;
884 /* check if user need to give more cards back */
885 if( ($myparty=='re' || $myparty=='contra') && count($mycards)>12)
887 $card_status = CARDS_EXCHANGE;
889 else if( ($myparty=='re' || $myparty=='contra') && count($mycards)==12)
891 /* user is done, ready to play */
892 DB_set_hand_status_by_hash($me,'play');
894 /* email start player */
895 $startplayer = DB_get_startplayer_by_gameid($gameid);
896 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
897 $who = DB_get_userid('hash',$hash);
898 DB_set_player_by_gameid($gameid,$who);
902 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
904 /* email startplayer */
905 $email_message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
906 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
907 mymail($who,$gameid,GAME_READY,$email_message);
911 $messages[]= "Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.";
914 /* the following is part A) of what needs to be done */
915 if(!myisset('trump'))
919 echo "<div class=\"poverty\">\n";
920 $userids = DB_get_all_userid_by_gameid($gameid);
921 foreach($userids as $user)
923 $name = DB_get_name('userid',$user);
924 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
925 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
926 $userparty = DB_get_party_by_hash($userhash);
928 if(($usersick=='poverty'|| ($RULES['lowtrump']=='poverty' && $usersick=='lowtrump')) && !$userparty)
930 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
931 $cards = DB_get_hand($hash);
934 foreach($cards as $card)
935 if($card<27) $nrtrump++;
937 if($usersick=='lowtrump')
939 echo "Player $name has $nrtrump $low trump. Do you want to take them?".
940 "<a href=\"index.php?action=game&me=$me&trump=$user\">Yes</a> <br />\n";
943 echo "<a href=\"index.php?action=game&me=$me&trump=no\">No way</a> <br />\n";
950 $trump = $_REQUEST['trump'];
954 /* user doesn't want to take trump */
955 DB_set_hand_status_by_hash($me,'play');
957 /* set next player who needs to be asked and email him*/
958 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
959 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
961 /* don't ask people who have poverty */
963 if($firstsick=='poverty' || ($RULES['lowtrump']=='poverty' && $firstsick=='lowtrump'))
965 if($secondsick=='poverty'|| ($RULES['lowtrump']=='poverty' && $secondsick=='lowtrump'))
970 if($gametype=='dpoverty')
972 $next=999; /* need to cancel for sure, since both would need to take the trump */
975 /* no more people to ask, need to cancel the game */
978 $email_message = "Hello, \n\n".
979 "Game ".DB_format_gameid($gameid)." has been canceled since nobody wanted to take the trump.\n\n";
981 $userids = DB_get_all_userid_by_gameid($gameid);
982 foreach($userids as $user)
984 mymail($user, $gameid, GAME_CANCELED_POVERTY, $email_message);
987 /* update game status */
988 cancel_game('trump',$gameid);
990 $messages[] = "Game ".DB_format_gameid($gameid)." has been canceled.";
995 /* email next player, set his status to poverty */
996 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
997 $userid = DB_get_userid('hash',$userhash);
999 DB_set_player_by_gameid($gameid,$userid);
1000 DB_set_hand_status_by_hash($userhash,'poverty');
1002 $email_message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
1003 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
1004 mymail($userid,$gameid, GAME_POVERTY, $email_message);
1007 $cards_status = CARDS_SHOW;
1011 /* player wants to take trump, change cards */
1013 /* user wants to take trump */
1014 $trump = $_REQUEST['trump'];
1015 $userhand = DB_get_handid('gameid-userid',$gameid,$trump);
1016 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
1018 /* remember which cards were handed over*/
1019 $partnerhand = DB_get_all_hand($userhash);
1020 foreach ($partnerhand as $card)
1022 DB_add_exchanged_card($card,$userhand,$myhand);
1024 /* copy trump from player A to B */
1025 $result = DB_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
1028 $mycards = DB_get_hand($me);
1031 if($gametype=='poverty')
1033 $userids = DB_get_all_userid_by_gameid($gameid);
1034 foreach($userids as $user)
1036 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1037 if($hash==$userhash||$hash==$me)
1039 DB_set_party_by_hash($hash,'re');
1043 DB_set_party_by_hash($hash,'contra');
1044 DB_set_hand_status_by_hash($hash,'play'); /* the contra party is ready to play */
1047 /* check if we are done (in case of no trump handed over), if so, go to 'play' phase right away*/
1048 if(count($mycards)==12)
1050 DB_set_hand_status_by_hash($me,'play');
1055 /* has the re party already been set?*/
1057 $userids = DB_get_all_userid_by_gameid($gameid);
1058 foreach($userids as $user)
1060 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1061 $party = DB_get_party_by_hash($hash);
1067 DB_set_party_by_hash($me,'contra');
1068 DB_set_party_by_hash($userhash,'contra');
1072 DB_set_party_by_hash($me,'re');
1073 DB_set_party_by_hash($userhash,'re');
1075 /* send out email to second non-poverty player */
1076 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
1077 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
1080 if($firstsick=='poverty'|| ($RULES['lowtrump']=='poverty' && $firstsick=='lowtrump'))
1081 if($secondsick=='poverty'|| ($RULES['lowtrump']=='poverty' && $secondsick=='lowtrump'))
1087 $messages[] = "Error in poverty, please contact the Admin";
1089 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
1090 $userid = DB_get_userid('hash',$userhash);
1092 DB_set_player_by_gameid($gameid,$userid);
1093 DB_set_hand_status_by_hash($userhash,'poverty');
1095 $email_message = "Two people have poverty, it's your turn to decide, if you want to take the trump. Please visit:".
1096 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
1097 mymail($userid,$gameid, GAME_DPOVERTY, $email_message);
1100 $messages[] = "Please, <a href=\"$INDEX?action=game&me=$me\">continue</a> here";
1107 /* both entries here, so that the tricks are visible for both.
1108 * in case of 'play' there is a break later that skips the last part
1111 /* first check if the game has been canceled and display */
1114 case 'cancel-noplay':
1115 $messages[] = "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.";
1117 case 'cancel-timedout':
1118 $messages[] = "The game has been canceled because one player wasn't responding.<br />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.";
1120 case 'cancel-nines':
1121 $messages[] = "The game has been canceled because one player had too many nines.";
1123 case 'cancel-lowtrump':
1124 $messages[] = "The game has been canceled because one player had low trump.";
1126 case 'cancel-trump':
1127 $messages[] = "The game has been canceled because nobody wanted to take the trump.";
1130 /* for these two types, we shouldn't show the cards, since we might want to restart the game */
1131 if (in_array($gamestatus,array('cancel-noplay','cancel-timedout')))
1134 /* check if all players are ready to play,
1135 * if so, send out email to the startplayer
1136 * only need to do this if the game hasn't started yet
1138 $gamestatus = DB_get_game_status_by_gameid($gameid);
1139 if($gamestatus == 'pre')
1142 $userids = DB_get_all_userid_by_gameid($gameid);
1143 foreach($userids as $user)
1145 $userstatus = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
1146 if($userstatus !='play' && $userstatus!='gameover')
1149 DB_set_player_by_gameid($gameid,$user);
1155 /* only set this after all poverty, etc. are handled*/
1156 DB_set_game_status_by_gameid($gameid,'play');
1158 /* email startplayer */
1159 $startplayer = DB_get_startplayer_by_gameid($gameid);
1160 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
1161 $who = DB_get_userid('hash',$hash);
1162 DB_set_player_by_gameid($gameid,$who);
1164 if($hash!=$me && DB_get_email_pref_by_hash($hash)!='emailaddict')
1166 /* email startplayer) */
1167 $email_message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
1168 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
1169 mymail($who,$gameid, GAME_READY, $email_message);
1173 /* figure out what kind of game we are playing,
1174 * set the global variables $CARDS['trump'],$CARDS['diamonds'],$CARDS['hearts'],
1175 * $CARDS['clubs'],$CARDS['spades'],$CARDS['foxes']
1179 $gametype = DB_get_gametype_by_gameid($gameid);
1181 if($gametype=='solo')
1183 $gametype = DB_get_solo_by_gameid($gameid);
1184 if($gametype=='silent')
1187 $GT = $gametype.' '.$GT;
1190 $gametype = 'normal';
1192 set_gametype($gametype); /* this sets the $CARDS variable */
1194 /* get some infos about the game, need to reset this, since it might have changed */
1195 $gamestatus = DB_get_game_status_by_gameid($gameid);
1197 /* has the game started? No, then just wait here...*/
1198 if($gamestatus == 'pre')
1200 $messages[] = _('You finished the setup, but not everyone else finished it... '.
1201 'You need to wait for the others. Just wait for an email.');
1203 $card_status = CARDS_SHOW;
1205 break; /* not sure this works... the idea is that you can
1206 * only play a card after everyone is ready to play */
1210 /* get time from the last action of the game */
1211 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1212 $gameend = time() - strtotime($r[0]);
1214 /* handle comments in case player didn't play a card, allow comments a week after the end of the game */
1215 if( (!myisset('card') && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
1216 if(myisset('comment'))
1218 $comment = $_REQUEST['comment'];
1219 $playid = DB_get_current_playid($gameid);
1222 DB_insert_comment($comment,$playid,$myid);
1225 /* get everything relevant to display the tricks */
1226 $result = DB_query("SELECT Hand_Card.card_id as card,".
1227 " Hand.position as position,".
1228 " Play.sequence as sequence, ".
1230 " GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
1231 " SEPARATOR '\n' ), ".
1232 " Play.create_date, ".
1235 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1236 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1237 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1238 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1239 "LEFT JOIN User On User.id=Comment.user_id ".
1240 "WHERE Trick.game_id='".$gameid."' ".
1241 "GROUP BY Trick.id, sequence ".
1242 "ORDER BY Trick.id, sequence ASC");
1244 $lasttrick = DB_get_max_trickid($gameid);
1246 $play = array(); /* needed to calculate winner later */
1248 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1249 $firstcard = ''; /* first card in a trick */
1251 echo "\n<ul class=\"tricks\">\n";
1253 /* output vorbehalte */
1254 $mygametype = DB_get_gametype_by_gameid($gameid);
1255 $mygamesolo = DB_get_solo_by_gameid($gameid);
1256 if($mygametype != 'normal') /* only show when needed */
1257 if(!( $mygametype == 'solo' && $mygamesolo == 'silent') )
1259 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
1260 " <div class=\"trick\" id=\"trick0\">\n";
1262 /* get information so show the cards that have been handed over in a poverty game */
1263 output_exchanged_cards();
1265 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1269 while($r = DB_fetch_array($result))
1277 /* count number of tricks */
1281 /* check if first schweinchen has been played */
1282 if( $GAME['schweinchen-who'] && ($r[0] == 19 || $r[0] == 20) )
1283 if(!$GAME['schweinchen-first'])
1284 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1286 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1288 /* save card to be able to find the winner of the trick later */
1289 $play[$seq] = array('card'=>$r[0],'pos'=>$pos);
1293 /* first card in a trick, output some html */
1294 if($trick!=$lasttrick)
1296 /* start of an old trick? */
1297 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">"._('Trick')." $trickNR</a>\n".
1298 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1299 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1301 else if($trick==$lasttrick)
1303 /* start of a last trick? */
1304 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">"._('Trick')." $trickNR</a>\n".
1305 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1306 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1309 /* remember first card, so that we are able to check, what cards can be played */
1314 echo " <div class=\"card".($pos-1)."\">\n";
1316 /* display comments */
1318 echo " <span class=\"comment\">".$comment."</span>\n";
1321 display_card($r[0],$PREF['cardset']);
1323 echo " </div>\n"; /* end div card */
1328 $winner = get_winner($play,$gametype); /* returns the position */
1329 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1333 /* whos turn is it? */
1336 $winner = get_winner($play,$gametype); /* returns the position */
1338 $firstcard = ''; /* new trick, no first card */
1343 if($next==5) $next = 1;
1346 /* my turn?, display cards as links, ask for comments*/
1347 if(DB_get_pos_by_hash($me) == $next)
1352 /* do we want to play a card? */
1353 if(myisset('card') && $myturn)
1355 $card = $_REQUEST['card'];
1356 $handid = DB_get_handid('hash',$me);
1357 $commentSchweinchen =''; /* used to add a comment when Schweinchen is being played */
1359 /* check if we have card and that we haven't played it yet*/
1360 /* set played in hand_card to true where hand_id and card_id*/
1361 $r = DB_query_array("SELECT id FROM Hand_Card WHERE played='false' and ".
1362 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1363 $handcardid = $r[0];
1365 if($handcardid) /* everything ok, play card */
1367 /* update Game timestamp */
1368 DB_update_game_timestamp($gameid);
1370 /* mark card as played */
1371 DB_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1372 DB_quote_smart($card));
1374 /* get trick id or start new trick */
1375 $a = DB_get_current_trickid($gameid);
1380 $playid = DB_play_card($trickid,$handcardid,$sequence);
1382 /* check special output for schweinchen in case in case a fox is being played
1383 * check for correct rules, etc. has already been done
1385 if( $GAME['schweinchen-who'] && ($card == 19 || $card == 20) )
1387 if(!$GAME['schweinchen-first'])
1388 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1390 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1392 if( $RULES['schweinchen']=='both' ||
1393 ($RULES['schweinchen']=='second' && $GAME['schweinchen-second']==1 )||
1394 ($RULES['schweinchen']=='secondaftercall' && $GAME['schweinchen-second']==1 &&
1395 (DB_get_call_by_hash($GAME['schweinchen-who']) || DB_get_partner_call_by_hash($GAME['schweinchen-who']) ))
1398 DB_insert_comment('Schweinchen! ',$playid,$myid);
1399 $commentSchweinchen = 'Schweinchen! ';
1402 echo 'schweinchen = '.$GAME['schweinchen-who'].' ---<br />';
1405 /* if sequence == 4 check who one in case of wedding */
1406 if($sequence == 4 && $GT == 'wedding')
1408 /* is wedding resolve */
1409 $resolved = DB_get_sickness_by_gameid($gameid);
1412 /* who has wedding */
1413 $userids = DB_get_all_userid_by_gameid($gameid);
1414 foreach($userids as $user)
1416 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1417 if($usersick == 'wedding')
1420 /* who won the trick */
1421 $play = DB_get_cards_by_trick($trickid);
1422 $winner = get_winner($play,$gametype); /* returns the position */
1423 $winnerid = DB_get_userid('gameid-position',$gameid,$winner);
1424 /* is tricknr <=3 */
1425 if($tricknr <=3 && $winnerid!=$whosick)
1427 /* set resolved at tricknr*/
1428 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1430 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1431 DB_set_party_by_hash($whash,'re');
1433 if($tricknr == 3 && $winnerid==$whosick)
1435 /* set resolved at tricknr*/
1436 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1441 /* if sequence == 4, set winner of the trick, count points and set the next player */
1444 $play = DB_get_cards_by_trick($trickid);
1445 $winner = get_winner($play,$gametype); /* returns the position */
1448 * check if someone caught a fox
1449 *******************************/
1451 /* first check if we should account for solos at all,
1452 * since it doesn't make sense in some games
1454 $ok = 0; /* fox shouldn't be counted */
1455 if(DB_get_gametype_by_gameid($gameid)=='solo')
1457 $solo = DB_get_solo_by_gameid($gameid);
1458 if($solo == 'trump' || $solo == 'silent')
1459 $ok = 1; /* for trump solos and silent solos, foxes are ok */
1462 $ok = 1; /* for all other games (not solos) foxes are ok too */
1465 foreach($play as $played)
1467 if ( $played['card']==19 || $played['card']==20 )
1468 if ($played['pos']!= $winner )
1470 /* possible caught a fox, check party */
1471 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1472 $uid2 = DB_get_userid('gameid-position',$gameid,$played['pos']);
1474 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1475 $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1477 if($party1 != $party2)
1478 DB_query("INSERT INTO Score".
1479 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1484 * check for karlchen (jack of clubs in the last trick)
1485 ******************************************************/
1487 /* same as for foxes, karlchen doesn't always make sense
1488 * check what kind of game it is and set karlchen accordingly */
1490 if($tricknr == 12 ) /* Karlchen works only in the last trick */
1492 /* check for solo */
1494 if(DB_get_gametype_by_gameid($gameid)=='solo' )
1495 $solo = DB_get_solo_by_gameid($gameid);
1497 /* no Karlchen in these solos */
1498 if($solo != 'trumpless' && $solo != 'jack' && $solo != 'queen' )
1500 foreach($play as $played)
1501 if ( $played['card']==11 || $played['card']==12 )
1502 if ($played['pos'] == $winner )
1505 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1506 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1508 DB_query("INSERT INTO Score".
1509 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1512 }; /* end scoring Karlchen */
1515 * check for doppelopf (>40 points)
1516 ***********************************/
1519 foreach($play as $played)
1521 $points += DB_get_card_value_by_cardid($played['card']);
1525 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1526 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1528 DB_query("INSERT INTO Score".
1529 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
1533 * set winner (for this trick)
1537 DB_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1539 $messages[] = "ERROR during scoring";
1542 echo "DEBUG: position $winner won the trick <br />";
1544 /* who is the next player? */
1549 $next = DB_get_pos_by_hash($me)+1;
1551 if($next==5) $next=1;
1553 /* check for coment */
1554 if(myisset('comment'))
1556 $comment = $_REQUEST['comment'];
1558 DB_insert_comment($comment,$playid,$myid);
1559 if($commentSchweinchen)
1560 $comment = $commentSchweinchen . $comment;
1563 /* display played card */
1564 $pos = DB_get_pos_by_hash($me);
1567 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">"._('Trick').' '.($tricknr)."</a>\n".
1568 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1569 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1572 echo " <div class=\"card".($pos-1)."\">\n ";
1574 /* display comments */
1575 display_card($card,$PREF['cardset']);
1577 echo "\n <span class=\"comment\"> ".$comment."</span>\n";
1580 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1582 /*check if we still have cards left, else set status to gameover */
1583 if(sizeof(DB_get_hand($me))==0)
1585 DB_set_hand_status_by_hash($me,'gameover');
1586 $mystatus = 'gameover';
1589 /* if all players are done, set game status to game over,
1590 * get the points of the last trick and send out an email
1593 $userids = DB_get_all_userid_by_gameid($gameid);
1596 foreach($userids as $user)
1597 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1601 DB_set_game_status_by_gameid($gameid,'gameover');
1603 /* email next player, if game is still running */
1604 if(DB_get_game_status_by_gameid($gameid)=='play')
1606 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1607 $who = DB_get_userid('hash',$next_hash);
1608 DB_set_player_by_gameid($gameid,$who);
1610 $email_message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1611 "It's your turn now.\n".
1612 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$next_hash."\n\n" ;
1613 if( DB_get_email_pref_by_uid($who)!='emailaddict' )
1615 mymail($who,$gameid, GAME_YOUR_TURN, $email_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 $email_message = "The game is over. Thanks for playing :)\n";
1630 $email_message .= "Final score:\n";
1631 while( $r = DB_fetch_array($result) )
1632 $email_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 $email_message .= "\nTotals:\n";
1645 while( $r = DB_fetch_array($result) )
1647 $email_message .= " ".$r[0]." ".$r[1]."\n";
1650 else if($r[0] == 'contra')
1655 * save score in database
1659 /* get calls from re/contra */
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== -1)
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 == -1 && $call_contra == -1)
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 == -1 )
1715 $winning_party='contra';
1718 if($call_contra != -1)
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 == -1 )
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!= -1)
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!= -1)
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 != -1)
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!= -1 && $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 != -1)
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 != -1 && $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 */
1816 $email_message .= "\n";
1818 $email_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 $email_message .= " ".$r[0]."\n";
1827 $email_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 $email_message .= " ".$r[0]."\n";
1836 $email_message .= " Total Points (from the Re point of view): $Tpoint\n";
1837 $email_message .= "\n";
1839 $session = DB_get_session_by_gameid($gameid);
1840 $score = generate_score_table($session);
1842 $email_message .= "Score Table:\n";
1843 $email_message .= format_score_table_ascii($score);
1844 $email_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" ;
1854 $email_message .= $link;
1856 $email_message .= "\n\n (you can use reply all on this email to reach all the players.)\n\n";
1857 mymail($userids,$gameid, GAME_OVER, $email_message);
1862 $messages[] = "can't find that card?!";
1865 else if(myisset('card') && !$myturn )
1867 $messages[] = _("please wait until it's your turn!");
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 if($myturn && !myisset('card') && $mystatus=='play' )
1939 $card_status = CARDS_MYTURN;
1941 else if($mystatus=='play' )
1943 $card_status = CARDS_SHOW;
1945 else if($mystatus=='gameover')
1947 if(isset($_SESSION['id']) && $myid==$_SESSION['id'])
1948 $card_status = CARDS_GAMEOVER_ME;
1950 $card_status = CARDS_GAMEOVER;
1953 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1954 if($mystatus=='play')
1957 /* the following happens only when the gamestatus is 'gameover' */
1958 /* check if game is over, display results */
1959 if(DB_get_game_status_by_gameid($gameid)=='play')
1961 echo _('The game is over for you... other people still need to play though');
1965 myerror("error in testing the status");
1966 } /*end of output: tricks, table, messages, card */
1972 $mycards = DB_get_hand($me);
1973 $mycards = mysort($mycards,$gametype);
1976 echo '<div class="mycards">';
1977 switch ($card_status) {
1979 echo _('Your cards are').": <br />\n";
1980 foreach($mycards as $card)
1981 display_card($card,$PREF['cardset']);
1983 case CARDS_EXCHANGE:
1984 echo '<div class="poverty"> '._('You need to get rid of a few cards')."</div>\n";
1986 echo _('Your cards are').": <br />\n";
1988 foreach($mycards as $card)
1989 display_link_card($card,$PREF['cardset'],$type);
1990 echo ' <input type="submit" class="submitbutton" value="select card to give back" />'."\n";
1993 echo 'Hello '.$myname.", it's your turn! <br />\n";
1994 echo _('Your cards are').": <br />\n";
1996 /* do we have to follow suite? */
1998 if(have_suit($mycards,$firstcard))
2001 /* count how many cards we can play, so that we can pre-select it if there is only one */
2003 foreach($mycards as $card)
2008 /* display only cards that the player is allowed to play as links, the rest just display normal
2009 * also check if we have both schweinchen, in that case only display on of them as playable
2011 if( ($followsuit && !same_type($card,$firstcard)) ||
2012 ( (int)($card)==19 &&
2013 !$GAME['schweinchen-first'] &&
2014 ( $RULES['schweinchen']=='second' ||
2015 ( $RULES['schweinchen']=='secondaftercall' &&
2016 (DB_get_call_by_hash($GAME['schweinchen-who']) ||
2017 DB_get_partner_call_by_hash($GAME['schweinchen-who']) )
2020 $GAME['schweinchen-who']==$me &&
2021 in_array($gametype,array('normal','wedding','trump','silent'))
2029 /* make it boolean, so that we can pass it later to display_link_card */
2030 if($howmanycards!=1)
2033 foreach($mycards as $card)
2035 /* display only cards that the player is allowed to play as links, the rest just display normal
2036 * also check if we have both schweinchen, in that case only display on of them as playable
2038 if( ($followsuit && !same_type($card,$firstcard)) ||
2039 ( (int)($card)==19 &&
2040 !$GAME['schweinchen-first'] &&
2041 ( $RULES['schweinchen']=='second' ||
2042 ( $RULES['schweinchen']=='secondaftercall' &&
2043 (DB_get_call_by_hash($GAME['schweinchen-who']) ||
2044 DB_get_partner_call_by_hash($GAME['schweinchen-who']) )
2047 $GAME['schweinchen-who']==$me &&
2048 in_array($gametype,array('normal','wedding','trump','silent'))
2051 display_card($card,$PREF['cardset']);
2053 display_link_card($card,$PREF['cardset'],$type='card',$selected=$howmanycards);
2056 case CARDS_GAMEOVER_ME:
2057 case CARDS_GAMEOVER:
2058 if($card_status == CARDS_GAMEOVER_ME)
2059 echo _('Your cards were').": <br />\n";
2062 $name = DB_get_name('userid',$myid);
2063 echo "$name's were: <br />\n";
2065 $oldcards = DB_get_all_hand($me);
2066 $oldcards = mysort($oldcards,$gametype);
2068 foreach($oldcards as $card)
2069 display_card($card,$PREF['cardset']);
2071 /* display hands of everyone else */
2072 $userids = DB_get_all_userid_by_gameid($gameid);
2073 foreach($userids as $user)
2075 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
2081 $name = DB_get_name('userid',$user);
2082 $oldcards = DB_get_all_hand($userhash);
2083 $oldcards = mysort($oldcards,$gametype);
2084 echo "$name's cards were: <br />\n";
2085 foreach($oldcards as $card)
2086 display_card($card,$PREF['cardset']);
2100 if( sizeof($messages) )
2102 echo "\n<div class=\"message\">\n";
2103 foreach($messages as $message)
2105 echo " <div>$message <div>close</div> </div>\n";
2110 /***********************************************
2111 * Comments, re/contra calls, user menu
2112 ***********************************************/
2114 /* output other games where it is the users turn
2115 * make sure that the people looking at old games don't see the wrong games here
2117 if( $gamestatus != 'gameover' )
2119 /* game isn't over, only valid user can get here, so show menu */
2120 display_user_menu($myid);
2122 else if( $origmystatus != 'gameover' )
2124 /* user just played the very last card, game is now over, it's still ok to show the menu though */
2125 display_user_menu($myid);
2127 else if( $mystatus == 'gameover'
2128 && isset($_SESSION['id']) )
2130 /* user is looking at someone else's game, show the menu for the correct user */
2131 display_user_menu($_SESSION['id']);
2135 echo "<div class=\"usermenu\">\n";
2136 echo "It's your turn in these games:<br />\n";
2137 echo "Please log in to see this information.\n";
2142 * display gameinfo: re/contra, comment-box, play-card button, games played by others
2145 echo "<div class=\"gameinfo\">\n";
2147 /* get time from the last action of the game */
2148 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
2149 $gameend = time() - strtotime($r[0]);
2152 if($gamestatus == 'play' || $gameend < 60*60*24*7)
2154 echo ' '._('A short comment').":<input name=\"comment\" type=\"text\" size=\"20\" maxlength=\"100\" />\n";
2158 if($gamestatus == 'play' )
2160 $myparty = DB_get_party_by_hash($me);
2161 output_form_calls($me,$myparty);
2164 /* play-card button */
2165 if($gamestatus == 'play' || $gameend < 60*60*24*7)
2167 echo " <input type=\"submit\" value=\""._('submit')."\" />\n";
2170 /* has this hand been played by others? */
2171 $other_game_ids = DB_played_by_others($gameid);
2172 if(sizeof($other_game_ids)>0 && $mystatus=='gameover')
2174 $mypos = DB_get_pos_by_hash($me);
2175 echo " <p>See how other played the same hand: \n";
2176 foreach($other_game_ids as $id)
2178 $otherhash = DB_get_hash_from_game_and_pos($id,$mypos);
2179 $othername = DB_get_name('hash',$otherhash);
2180 echo " <a href=\"$INDEX?action=game&me=$otherhash\">$othername</a> ";
2185 echo "</div>\n\n"; /* end gameinfo */
2187 /* make sure that we don't show the notes to the wrong person
2188 * (e.g. other people looking at an old game)
2190 if( $mystatus != 'gameover' ||
2191 ( $mystatus == 'gameover' &&
2192 isset($_SESSION['id']) &&
2193 $myid == $_SESSION['id']))
2194 output_user_notes($myid,$gameid,$mystatus);
2198 /*********************************
2200 *********************************/
2202 $gamestatus = DB_get_game_status_by_gameid($gameid);
2203 if($mystatus=='gameover' &&
2204 ($gamestatus =='gameover' || $gamestatus =='cancel-nines' || $gamestatus =='cancel-trump') &&
2205 isset($_SESSION['id']) && $_SESSION['id']==$myid)
2207 $session = DB_get_session_by_gameid($gameid);
2208 $result = DB_query("SELECT id,create_date FROM Game".
2209 " WHERE session=$session".
2210 " ORDER BY create_date DESC".
2214 $r = DB_fetch_array($result);
2216 if(!$session || $gameid==$r[0])
2218 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
2219 $names = DB_get_all_names_by_gameid($gameid);
2220 $type = DB_get_gametype_by_gameid($gameid);
2224 $solo = DB_get_solo_by_gameid($gameid);
2226 if($solo!='silent') /* repeat game with same first player */
2227 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
2228 else /* rotate normally */
2229 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
2231 else if($gamestatus == 'cancel-nines' || $gamestatus == 'cancel-trump')
2232 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
2233 else /* rotate normally */
2234 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);