2 /* make sure that we are not called from outside the scripts,
3 * use a variable defined in config.php to check this
8 /* calling game.php only makes sense when we give it a hash for a game */
11 echo "Hmm, you really shouldn't mess with the urls.<br />\n";
14 $me = $_REQUEST['me'];
16 /* Ok, got a hash, but is it valid? */
17 $myid = DB_get_userid('hash',$me);
20 echo "Can't find you in the database, please check the url.<br />\n";
21 echo "perhaps the game has been canceled, check by login in <a href=\"$INDEX\">here</a>.";
25 global $GAME,$RULES,$CARDS;
27 /* the user has done something, update the timestamp */
28 if(isset($_SESSION['id']))
29 DB_update_user_timestamp($_SESSION['id']);
31 /* get some information from the DB */
32 $gameid = DB_get_gameid_by_hash($me);
33 $myname = DB_get_name('hash',$me);
34 $mystatus = DB_get_status_by_hash($me);
35 $mypos = DB_get_pos_by_hash($me);
36 $myhand = DB_get_handid('hash',$me);
37 $myparty = DB_get_party_by_hash($me);
38 $session = DB_get_session_by_gameid($gameid);
40 /* get prefs and save them in a variable*/
41 $PREF = DB_get_PREF(isset($_SESSION['id'])?$_SESSION['id']:$myid);
43 /* get rule set for this game */
44 $RULES = DB_get_RULES($gameid);
46 /* get some infos about the game */
47 $gametype = DB_get_gametype_by_gameid($gameid);
48 $gamestatus = DB_get_game_status_by_gameid($gameid);
52 $gametype = DB_get_solo_by_gameid($gameid);
53 $GT = $gametype.' '.$GT;
56 /* do we need to worry about Schweinchen?
57 * check gametype and rules
58 * if yes, figure out if someone actually has Schweinchen
59 * save information in $GAME
62 if( $gamestatus == 'pre' )
64 /* always need to use Schweinchen to figure out for example who has poverty */
69 /* in a game Schweinchen is not valid in all types of games */
70 if( $gametype == 'normal' || $gametype == 'silent' || $gametype=='trump' || $gametype=='wedding')
71 if( in_array($RULES['schweinchen'],array('both','second','secondaftercall')) )
75 /* these are the defaults */
76 $GAME['schweinchen-who'] = NULL;
77 $GAME['schweinchen-first'] = NULL;
78 $GAME['schweinchen-second'] = NULL;
82 /* need to check for Schweinchen */
85 $hash = DB_get_hash_from_game_and_pos($gameid,$i);
86 $cards = DB_get_all_hand($hash);
87 if( in_array('19',$cards) && in_array('20',$cards) )
88 $GAME['schweinchen-who']=$hash;
90 $GAME['schweinchen-first'] = 0; /* to keep track if they have been played already */
91 $GAME['schweinchen-second'] = 0;
93 /* end check for Schweinchen */
95 /* set the $CARDS variable, needed for sorting the cards
96 * we set it to normal so that the pre-game phase is handled ok
97 * and later set it to the correct game type that is played
99 set_gametype('normal');
101 /* put everyting in a form */
102 echo "<form action=\"index.php?action=game&me=$me\" method=\"post\">\n";
104 /* handle user notes (only possible while game is running)*/
105 if( $mystatus!='gameover' )
108 $note = $_REQUEST['note'];
111 DB_insert_note($note,$gameid,$myid);
114 /* make sure that we don't show the notes to the wrong person
115 * (e.g. other people looking at an old game)
117 if( $mystatus != 'gameover' ||
118 ( $mystatus == 'gameover' &&
119 isset($_SESSION['id']) &&
120 $myid == $_SESSION['id']))
121 output_user_notes($myid,$gameid,$mystatus);
123 /* handle calls, output a comment to show when the call was made */
124 /* initialize comments */
127 /* check for calls, set comment */
128 if(myisset('call') && $_REQUEST['call'] == '120' && can_call(120,$me))
130 $result = DB_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
133 else if($myparty=='contra')
134 $comment .= "Contra";
136 if(myisset('call') && $_REQUEST['call'] == '90' && can_call(90,$me))
138 $result = DB_query("UPDATE Hand SET point_call='90' WHERE hash='$me' ");
141 if(myisset('call') && $_REQUEST['call'] == '60' && can_call(60,$me))
143 $result = DB_query("UPDATE Hand SET point_call='60' WHERE hash='$me' ");
146 if(myisset('call') && $_REQUEST['call'] == '30' && can_call(30,$me))
148 $result = DB_query("UPDATE Hand SET point_call='30' WHERE hash='$me' ");
151 if(myisset('call') && $_REQUEST['call'] == '0' && can_call(0,$me))
153 $result = DB_query("UPDATE Hand SET point_call='0' WHERE hash='$me' ");
157 /* get information needed to submit comment */
158 $playid = DB_get_current_playid($gameid);
162 DB_insert_comment($comment,$playid,$myid);
165 /* end check for calls */
168 /* output extra division in case this game is part of a session */
171 echo "<div class=\"session\">\n";
172 echo " <div class=\"sessionrules\">Rules: ";
173 switch($RULES['dullen'])
176 echo " <img class=\"rulesicon\" alt=\"not ten of hearts\" src=\"pics/button/no-ten-of-hearts.png\"/>\n"; break;
178 echo " <img class=\"rulesicon\" alt=\"ten of hearts\" src=\"pics/button/ten-of-hearts.png\"/>\n"; break;
180 echo " <img class=\"rulesicon\" alt=\"second ten of hearts\" src=\"pics/button/second-ten-of-hearts.png\"/>\n"; break;
182 switch($RULES['schweinchen'])
185 echo " <img class=\"rulesicon\" alt=\"no schweinchen\" ".
186 "src=\"pics/button/no-schweinchen.png\"/>\n"; break;
188 echo " <img class=\"rulesicon\" alt=\"two schweinchen \" ".
189 "src=\"pics/button/two-schweinchen.png\"/>\n"; break;
191 echo " <img class=\"rulesicon\" alt=\"second schweinchen\" ".
192 "src=\"pics/button/second-schweinchen.png\"/>\n"; break;
193 case 'secondaftercall':
194 echo " <img class=\"rulesicon\" alt=\"second schweinchen after call\" ".
195 "src=\"pics/button/second-schweinchen-after-call.png\"/>\n"; break;
197 switch($RULES['call'])
200 echo " <img class=\"rulesicon\" alt=\"1st-own-card\" src=\"pics/button/1st-own-card.png\"/>\n"; break;
202 echo " <img class=\"rulesicon\" alt=\"5th-card\" src=\"pics/button/5th-card.png\"/>\n"; break;
204 echo " <img class=\"rulesicon\" alt=\"9-cards\" src=\"pics/button/9-cards.png\"/>\n"; break;
207 echo " 10ofhearts : {$RULES['dullen']} <br />\n";
208 echo " schweinchen: {$RULES['schweinchen']} <br />\n";
209 echo " call: {$RULES['call']} <br />\n";
210 echo " </div>\n </div>\n";
214 echo " <div class=\"sessionscore\">";
216 $score = generate_score_table($session);
218 /* get the last entry to show on the main page */
220 $finalscore = array_pop($tmpscore);
221 $finalscore = $finalscore['players'];
226 foreach($finalscore as $user=>$value)
228 $name = DB_get_name('userid',$user);
229 echo " ".substr($name,0,2).": $value ";
234 /* first game, no score yet */
238 /* output all games for the score table */
239 echo format_score_table_html($score,$myid);
242 /* figure out which game in a session we are in and link to the
243 * previous and next game if possible
245 $hashes = DB_get_hashes_by_session($session,$myid);
248 foreach($hashes as $hash)
258 $previous = $hashes[$j-2];
266 if(isset($_SESSION['id']) && $_SESSION['id']==$myid)
269 echo "<a href=\"{$INDEX}?action=game&me=$previous\">previous game</a> \n";
270 echo "This is game number $j of <a href=\"{$INDEX}?action=game&me=$lasthash\">$i</a> in session $session.\n";
272 echo " <a href=\"{$INDEX}?action=game&me=$next\">next game</a> \n";
275 echo "This is game number $j of $i in session $session.";
279 /* display the table and the names */
282 /* mystatus gets the player through the different stages of a game.
283 * start: does the player want to play?
284 * init: check for sickness
285 * check: check for return values from init
286 * poverty: handle poverty, wait here until all player have reached this state
287 * display sickness and move on to game
288 * play: game in progress
289 * gameover: are we revisiting a game
294 /* don't ask if user has autosetup set to yest */
296 if($PREF['autosetup']=='yes') $skip = 1;
298 if( !myisset('in') && !$skip)
300 /* asks the player, if he wants to join the game */
301 output_check_want_to_play($me);
306 /* check the result, if player wants to join, got next stage, else cancel game */
307 if(!$skip && $_REQUEST['in'] == 'no' )
309 /* cancel the game */
310 $message = "Hello, \n\n".
311 "the game has been canceled due to the request of one of the players.\n\n";
313 $userids = DB_get_all_userid_by_gameid($gameid);
314 foreach($userids as $user)
316 $subject = 'Game '.DB_format_gameid($gameid).' canceled';
317 mymail($user,$subject,$message);
320 /* update game status */
321 cancel_game('noplay',$gameid);
326 /* user wants to join the game */
328 /* move on to the next stage,
329 * no break statement to immediately go to the next stage
332 DB_set_hand_status_by_hash($me,'init');
334 /* check if everyone has reached this stage, send out email */
335 $userids = DB_get_all_userid_by_gameid($gameid);
337 foreach($userids as $user)
339 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
340 if($userstat!='init')
342 /* whos turn is it? */
343 DB_set_player_by_gameid($gameid,$user);
350 /* all done, send out email unless this player is the startplayer */
351 $startplayer = DB_get_startplayer_by_gameid($gameid);
352 if($mypos == $startplayer)
354 /* do nothing, go to next stage */
358 /* email startplayer */
360 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
361 $who = DB_get_userid('hash',$hash);
362 DB_set_player_by_gameid($gameid,$who);
364 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
365 "Use this link to go the game: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
366 mymail($who,"Ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
373 /* here we ask the player if he is sick */
374 $mycards = DB_get_hand($me);
375 $mycards = mysort($mycards,$gametype);
377 if(!myisset('solo','wedding','poverty','nines') )
379 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
380 echo "\n<ul class=\"tricks\">\n";
381 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
382 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
383 " <div class=\"trick\" id=\"trick0\">\n";
385 for($pos=1;$pos<5;$pos++)
387 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
388 $userid = DB_get_userid('gameid-position',$gameid,$pos);
389 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
391 if($userstatus=='start' || $userstatus=='init')
392 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs <br/ > to decide </div>\n"; /* show this to everyone */
394 if($usersick!=NULL && $pos<=$mypos ) /* only show this for people sitting before the player */
395 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
396 else if($usersick==NULL && $pos<=$mypos)
397 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
399 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
400 /* end displaying sickness */
402 output_check_for_sickness($me,$mycards);
404 echo "<div class=\"mycards\">Your cards are: <br />\n";
405 foreach($mycards as $card)
406 display_card($card,$PREF['cardset']);
413 /* check if someone selected more than one sickness */
415 if($_REQUEST['solo']!='No') $Nsickness++;
416 if($_REQUEST['wedding'] == 'yes') $Nsickness++;
417 if($_REQUEST['poverty'] == 'yes') $Nsickness++;
418 if($_REQUEST['nines'] == 'yes') $Nsickness++;
422 echo "<p class=\"message\"> You selected more than one sickness, please go back ".
423 "and answer the <a href=\"$INDEX?action=game&me=$me&in=yes\">question</a> again.</p>";
425 echo "<div class=\"mycards\">Your cards are: <br />\n";
426 foreach($mycards as $card)
427 display_card($card,$PREF['cardset']);
434 /* everything is ok, save what user said and proceed */
435 echo "<p class=\"message\">Processing what you selected in the last step...";
437 /* check if this sickness needs to be handled first */
438 $gametype = DB_get_gametype_by_gameid($gameid);
439 $startplayer = DB_get_startplayer_by_gameid($gameid); /* need this to check which solo goes first */
441 if( $_REQUEST['solo']!='No' )
443 /* user wants to play a solo */
445 /* store the info in the user's hand info */
446 DB_set_solo_by_hash($me,$_REQUEST['solo']);
447 DB_set_sickness_by_hash($me,'solo');
449 echo "<br />Seems like you want to play a {$_REQUEST['solo']} solo. Got it.<br />\n";
451 if($gametype == 'solo' && $startplayer<$mypos)
452 {}/* do nothing, since someone else already is playing solo */
455 /* this solo comes first
456 * store info in game table
458 DB_set_gametype_by_gameid($gameid,'solo');
459 DB_set_startplayer_by_gameid($gameid,$mypos);
460 DB_set_solo_by_gameid($gameid,$_REQUEST['solo']);
463 else if($_REQUEST['wedding'] == 'yes')
465 /* silent solo is set further down */
466 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
467 DB_set_sickness_by_hash($me,'wedding');
469 else if($_REQUEST['poverty'] == 'yes')
471 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
472 DB_set_sickness_by_hash($me,'poverty');
474 else if($_REQUEST['nines'] == 'yes')
476 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
477 " is playing solo, this game will be canceled.<br />\n";
478 DB_set_sickness_by_hash($me,'nines');
483 /* move on to the next stage*/
484 DB_set_hand_status_by_hash($me,'check');
489 /* here we check what all players said and figure out what game we are playing
490 * this can therefore only be handled once all players finished the last stage
493 $mycards = DB_get_hand($me);
494 $mycards = mysort($mycards,$gametype);
496 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
497 echo "\n<ul class=\"tricks\">\n";
498 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
499 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
500 " <div class=\"trick\" id=\"trick0\">\n";
502 for($pos=1;$pos<5;$pos++)
504 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
505 $userid = DB_get_userid('gameid-position',$gameid,$pos);
506 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
508 if($userstatus=='start' || $userstatus=='init')
509 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs <br />to decide </div>\n"; /* show this to everyone */
511 if($usersick!=NULL) /* in the init-phase we only showed players with $pos<$mypos, now we can show all */
512 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
514 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
516 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
517 /* end displaying sickness */
519 echo "<div class=\"message\">\n";
520 echo "<p> Checking if someone else selected solo, nines, wedding or poverty.</p>";
522 /* check if everyone has reached this stage */
523 $userids = DB_get_all_userid_by_gameid($gameid);
525 foreach($userids as $user)
527 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
528 if($userstat!='check')
531 DB_set_player_by_gameid($gameid,$user);
538 echo "<p>This step can only be handled after everyone finished the last step. ".
539 "Seems like this is not the case, so you need to wait a bit... ".
540 "you will get an email once that is the case, please use the link in ".
541 "that email to continue the game.</p></div>";
543 /* display cards, if player was just at the init-phase he will still see the cards from there
544 * we can put this one here, since the last player to finish the init state won't get here and
545 * will still see his card anyway from the init-phase
547 if($mystatus=='check')
550 echo "<div class=\"mycards\">Your cards are: <br />\n";
551 foreach($mycards as $card)
552 display_card($card,$PREF['cardset']);
559 /* Ok, everyone finished the init-phase, time to figure out what game we
560 * are playing, in case there are any solos this already
561 * will have the correct information in it */
563 echo "<p> Ok, everyone is done... figuring out what kind of game we are playing.</p>";
565 $gametype = DB_get_gametype_by_gameid($gameid);
566 $startplayer = DB_get_startplayer_by_gameid($gameid);
568 /* check for sickness */
573 foreach($userids as $user)
575 $name = DB_get_name('userid',$user);
576 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
577 if($usersick == 'nines')
580 break; /* no need to check for other poverties, since only solo can win and that is already set */
582 else if($usersick == 'poverty')
584 else if($usersick == 'wedding')
586 else if($usersick == 'solo')
590 /* now check which sickness comes first and set the gametype to it */
591 if($gametype == 'solo')
598 $message = "the game has been canceled because ".DB_get_name('userid',$nines).
599 "has five or more nines and nobody is playing solo.\n\n".
600 "To redeal either start a new game or, in case the game was part of a tournament,\n".
601 "go to the last game and use the link at the bottom of the page to redeal.\n\n";
603 $userids = DB_get_all_userid_by_gameid($gameid);
604 foreach($userids as $user)
606 $subject = 'Game '.DB_format_gameid($gameid).' canceled';
607 mymail($user,$subject,$message);
610 /* update game status */
611 cancel_game('nines',$gameid);
613 echo "The game has been canceled because ".DB_get_name('userid',$nines).
614 " has five or more nines and nobody is playing solo.\n";
617 else if($poverty==1) /* one person has poverty */
619 DB_set_gametype_by_gameid($gameid,'poverty');
620 $gametype = 'poverty';
621 $who = DB_get_sickness_by_gameid($gameid);
624 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
625 if($firstsick == 'poverty')
626 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
628 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
631 else if($poverty==2) /* two people have poverty */
633 DB_set_gametype_by_gameid($gameid,'dpoverty');
634 $gametype = 'dpoverty';
635 $who = DB_get_sickness_by_gameid($gameid);
638 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
639 if($firstsick == 'poverty')
641 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
642 if($secondsick == 'poverty')
643 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
645 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
648 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
653 DB_set_gametype_by_gameid($gameid,'wedding');
654 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
655 $gametype = 'wedding';
657 /* now the gametype is set correctly in the database */
658 echo "<p> Got it :)</p>";
660 /* loop over all players, set re/contra if possible and start the game if possible */
661 $userids = DB_get_all_userid_by_gameid($gameid);
662 foreach($userids as $userid)
664 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
669 /* are we the solo player? set us to re, else set us to contra */
670 $pos = DB_get_pos_by_hash($userhash);
671 if($pos == $startplayer)
672 DB_set_party_by_hash($userhash,'re');
674 DB_set_party_by_hash($userhash,'contra');
675 DB_set_hand_status_by_hash($userhash,'play');
679 /* set person with the wedding to re, do the rest during the game */
680 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
681 if($usersick == 'wedding')
682 DB_set_party_by_hash($userhash,'re');
684 DB_set_party_by_hash($userhash,'contra');
686 DB_set_hand_status_by_hash($userhash,'play');
690 $hand = DB_get_all_hand($userhash);
692 if(in_array('3',$hand)||in_array('4',$hand))
693 DB_set_party_by_hash($userhash,'re');
695 DB_set_party_by_hash($userhash,'contra');
696 DB_set_hand_status_by_hash($userhash,'play');
700 /* set person with poverty to play status */
701 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
702 if($usersick == 'poverty')
703 DB_set_hand_status_by_hash($userhash,'play');
705 /* set status of first player to be asked to poverty */
706 $who = DB_get_sickness_by_gameid($gameid);
707 if($who > 6) $who= $who/10; /* in case we have dpoverty */
708 $whoid = DB_get_userid('gameid-position',$gameid,$who);
710 DB_set_hand_status_by_hash($userhash,'poverty');
713 /* check for silent solo, set game type to solo in this case */
714 $gametype = DB_get_gametype_by_gameid($gameid);
715 $userids = DB_get_all_userid_by_gameid($gameid);
716 foreach($userids as $userid)
718 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
720 if($gametype=='normal')
722 $userhand = DB_get_all_hand($userhash);
723 if(check_wedding($userhand))
725 /* normal game type and player has both queens -> silent solo */
726 /* keep startplayer, just set gametype to silent solo */
727 DB_set_gametype_by_gameid($gameid,'solo');
728 DB_set_solo_by_gameid($gameid,'silent');
733 /* send out email to first player or poverty person*/
734 if($gametype!='poverty' && $gametype!='dpoverty')
736 $startplayer = DB_get_startplayer_by_gameid($gameid);
737 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
738 $who = DB_get_userid('hash',$hash);
739 DB_set_player_by_gameid($gameid,$who);
743 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
745 /* email startplayer */
746 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
747 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
748 $subject = 'Ready, set, go... (game '.DB_format_gameid($gameid).')';
749 mymail($who,$subject,$message);
753 echo "<div class=\"message\">Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.</div>\n";
757 /* set status of first player to be asked to poverty */
758 $who = DB_get_sickness_by_gameid($gameid);
759 if($who > 6) $who= $who/10; /* in case we have dpoverty */
761 $whoid = DB_get_userid('gameid-position',$gameid,$who);
763 echo "<div class=\"message\">Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.</div>\n";
766 $whohash = DB_get_hash_from_game_and_pos($gameid,$who);
767 DB_set_player_by_gameid($gameid,$whoid);
769 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
771 /* email player for poverty */
772 $message = "Poverty: It's your turn now in game ".DB_format_gameid($gameid).".\n".
773 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$whohash."\n\n" ;
774 $subject = 'Poverty (game '.DB_format_gameid($gameid).') ';
775 mymail($whoid,$subject,$message);
783 /* user only gets here in a poverty game, several things have to be handled here:
784 * A) ask, if user wants to take trump
786 * poverty: set re/contra
787 * dpoverty: first time: set re, send email to second player
788 * second time: set contra
789 * poverty: set status of other players to 'play'
790 * set status to play in case 0 trump
791 * no -> set status to play,
792 * ask next player or cancle the game if no more players
793 * B) user took trump and has too many cards (e.g. count(cards)>12 and re/contra set)
794 * ask to give cards back, set status to play, once player has 12 cards
796 * it is easier to check B) first
799 set_gametype($gametype); /* this sets the $CARDS variable */
800 $myparty = DB_get_party_by_hash($me);
802 /* the following is part B) of whats needs to be done)
803 /* check if user wants to give cards back */
804 if(myisset('exchange'))
806 $exchange = $_REQUEST['exchange'];
807 $partnerhash = DB_get_partner_hash_by_hash($me);
808 $partnerid = DB_get_userid('hash',$partnerhash);
809 $partnerhand = DB_get_handid('gameid-userid',$gameid,$partnerid);
811 /* if exchange is set to a value>0, exchange that card back to the partner */
814 $result = DB_query("UPDATE Hand_Card SET hand_id='$partnerhand'".
815 " WHERE hand_id='$myhand' AND card_id=".DB_quote_smart($exchange));
816 DB_add_exchanged_card(DB_quote_smart($exchange),$myhand,$partnerhand);
821 $mycards = DB_get_hand($me);
822 $mycards = mysort($mycards,$gametype);
824 /* output pre-game trick in case user reloads,
825 * only needs to be done when a team has been formed */
826 if($myparty=='re' || $myparty=='contra')
828 echo "\n<ul class=\"tricks\">\n";
829 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
831 $mygametype = DB_get_gametype_by_gameid($gameid);
833 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
834 " <div class=\"trick\" id=\"trick0\">\n";
836 /* get information so show the cards that have been handed over in a poverty game */
837 output_exchanged_cards();
839 echo " </div>\n </li>\n"; /* end div trick, end li trick */
841 /* end output pre-game trick */
843 /* check if user need to give more cards back */
844 if( ($myparty=='re' || $myparty=='contra') && count($mycards)>12)
846 echo "<div class=\"poverty\"> you need to get rid of a few cards</div>\n";
849 echo "<div class=\"mycards\">Your cards are: <br />\n";
850 foreach($mycards as $card)
851 display_link_card($card,$PREF['cardset'],$type);
852 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select card to give back\" />\n";
855 else if( ($myparty=='re' || $myparty=='contra') && count($mycards)==12)
857 /* user is done, ready to play */
858 DB_set_hand_status_by_hash($me,'play');
860 /* email start player */
861 $startplayer = DB_get_startplayer_by_gameid($gameid);
862 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
863 $who = DB_get_userid('hash',$hash);
864 DB_set_player_by_gameid($gameid,$who);
868 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
870 /* email startplayer */
871 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
872 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
873 $subject = 'Ready, set, go... (game '.DB_format_gameid($gameid).') ';
874 mymail($who,$subject,$message);
878 echo "<div class=\"message\">Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.</div>\n";
881 /* the following is part A) of what needs to be done */
882 if(!myisset('trump'))
886 echo "<div class=\"poverty\">\n";
887 $userids = DB_get_all_userid_by_gameid($gameid);
888 foreach($userids as $user)
890 $name = DB_get_name('userid',$user);
891 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
892 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
893 $userparty = DB_get_party_by_hash($userhash);
895 if($usersick=='poverty' && !$userparty)
897 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
898 $cards = DB_get_hand($hash);
901 foreach($cards as $card)
902 if($card<27) $nrtrump++;
903 echo "Player $name has $nrtrump trump. Do you want to take them?".
904 "<a href=\"index.php?action=game&me=$me&trump=$user\">Yes</a> <br />\n";
907 echo "<a href=\"index.php?action=game&me=$me&trump=no\">No way</a> <br />\n";
908 echo "</div><div>\n";
910 echo "<div class=\"mycards\">Your cards are: <br />\n";
911 foreach($mycards as $card)
912 display_card($card,$PREF['cardset']);
913 echo "</div></div>\n";
919 $trump = $_REQUEST['trump'];
923 /* user doesn't want to take trump */
924 DB_set_hand_status_by_hash($me,'play');
926 /* set next player who needs to be asked and email him*/
927 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
928 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
930 /* don't ask people who have poverty */
932 if($firstsick=='poverty')
934 if($secondsick=='poverty')
939 if($gametype=='dpoverty')
941 $next=999; /* need to cancel for sure, since both would need to take the trump */
944 /* no more people to ask, need to cancel the game */
947 $message = "Hello, \n\n".
948 "Game ".DB_format_gameid($gameid)." has been canceled since nobody wanted to take the trump.\n\n";
950 $userids = DB_get_all_userid_by_gameid($gameid);
951 foreach($userids as $user)
953 $subject = 'Game '.DB_format_gameid($gameid).' canceled (poverty not resolved)';
954 mymail($user,$subject,$message);
957 /* update game status */
958 cancel_game('trump',$gameid);
960 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been canceled.<br /><br /></p>";
965 /* email next player, set his status to poverty */
966 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
967 $userid = DB_get_userid('hash',$userhash);
969 DB_set_player_by_gameid($gameid,$userid);
970 DB_set_hand_status_by_hash($userhash,'poverty');
972 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
973 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
974 $subject = 'Poverty (game '.DB_format_gameid($gameid).')';
975 mymail($userid,$subject,$message);
980 /* player wants to take trump, change cards */
982 /* user wants to take trump */
983 $trump = $_REQUEST['trump'];
984 $userhand = DB_get_handid('gameid-userid',$gameid,$trump);
985 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
987 /* remember which cards were handed over*/
988 $partnerhand = DB_get_all_hand($userhash);
989 foreach ($partnerhand as $card)
991 DB_add_exchanged_card($card,$userhand,$myhand);
993 /* copy trump from player A to B */
994 $result = DB_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
997 $mycards = DB_get_hand($me);
1000 if($gametype=='poverty')
1002 $userids = DB_get_all_userid_by_gameid($gameid);
1003 foreach($userids as $user)
1005 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1006 if($hash==$userhash||$hash==$me)
1008 DB_set_party_by_hash($hash,'re');
1012 DB_set_party_by_hash($hash,'contra');
1013 DB_set_hand_status_by_hash($hash,'play'); /* the contra party is ready to play */
1016 /* check if we are done (in case of no trump handed over), if so, go to 'play' phase right away*/
1017 if(count($mycards)==12)
1019 DB_set_hand_status_by_hash($me,'play');
1024 /* has the re party already been set?*/
1026 $userids = DB_get_all_userid_by_gameid($gameid);
1027 foreach($userids as $user)
1029 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1030 $party = DB_get_party_by_hash($hash);
1036 DB_set_party_by_hash($me,'contra');
1037 DB_set_party_by_hash($userhash,'contra');
1041 DB_set_party_by_hash($me,'re');
1042 DB_set_party_by_hash($userhash,'re');
1044 /* send out email to second non-poverty player */
1045 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
1046 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
1049 if($firstsick=='poverty')
1050 if($secondsick=='poverty')
1056 echo "<div class=\"message\">Error in poverty, please contact the Admin</div>\n";
1058 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
1059 $userid = DB_get_userid('hash',$userhash);
1061 DB_set_player_by_gameid($gameid,$userid);
1062 DB_set_hand_status_by_hash($userhash,'poverty');
1064 $message = "Two people have poverty, it's your turn to decide, if you want to take the trump. Please visit:".
1065 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
1066 $subject = 'Double poverty (game '.DB_format_gameid($gameid).')';
1067 mymail($userid,$subject,$message);
1070 echo "<div class=\"message\">Please, <a href=\"$INDEX?action=game&me=$me\">continue</a> here.</div>\n";
1078 /* both entries here, so that the tricks are visible for both.
1079 * in case of 'play' there is a break later that skips the last part
1082 /* first check if the game has been canceled and display */
1085 case 'cancel-noplay':
1086 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>";
1088 case 'cancel-timedout':
1089 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>";
1091 case 'cancel-nines':
1092 case 'cancel-timedout':
1093 echo "<div class=\"message\"><p>The game has been canceled because one player had too many nines.</p></div>";
1095 case 'cancel-trump':
1096 echo "<div class=\"message\"><p>The game has been canceled because nobody wanted to take the trump.</p></div>";
1099 /* for these two types, we shouldn't show the cards, since we might want to restart the game */
1100 if (in_array($gamestatus,array('cancel-noplay','cancel-timedout')))
1103 /* check if all players are ready to play,
1104 * if so, send out email to the startplayer
1105 * only need to do this if the game hasn't started yet
1107 $gamestatus = DB_get_game_status_by_gameid($gameid);
1108 if($gamestatus == 'pre')
1111 $userids = DB_get_all_userid_by_gameid($gameid);
1112 foreach($userids as $user)
1114 $userstatus = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
1115 if($userstatus !='play' && $userstatus!='gameover')
1118 DB_set_player_by_gameid($gameid,$user);
1124 /* only set this after all poverty, etc. are handled*/
1125 DB_set_game_status_by_gameid($gameid,'play');
1127 /* email startplayer */
1128 $startplayer = DB_get_startplayer_by_gameid($gameid);
1129 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
1130 $who = DB_get_userid('hash',$hash);
1131 DB_set_player_by_gameid($gameid,$who);
1133 if($hash!=$me && DB_get_email_pref_by_hash($hash)!='emailaddict')
1135 /* email startplayer) */
1136 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
1137 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
1138 $subject = 'Ready, set, go... (game '.DB_format_gameid($gameid).')';
1139 mymail($who,$subject,$message);
1143 /* figure out what kind of game we are playing,
1144 * set the global variables $CARDS['trump'],$CARDS['diamonds'],$CARDS['hearts'],
1145 * $CARDS['clubs'],$CARDS['spades'],$CARDS['foxes']
1149 $gametype = DB_get_gametype_by_gameid($gameid);
1151 if($gametype=='solo')
1153 $gametype = DB_get_solo_by_gameid($gameid);
1154 if($gametype=='silent')
1157 $GT = $gametype.' '.$GT;
1160 $gametype = 'normal';
1162 set_gametype($gametype); /* this sets the $CARDS variable */
1164 /* get some infos about the game, need to reset this, since it might have changed */
1165 $gamestatus = DB_get_game_status_by_gameid($gameid);
1167 /* has the game started? No, then just wait here...*/
1168 if($gamestatus == 'pre')
1170 echo "<p class=\"message\"> You finished the setup, but not everyone else finished it... ".
1171 "You need to wait for the others. Just wait for an email. </p>";
1173 $mycards = DB_get_hand($me);
1174 $mycards = mysort($mycards,$gametype);
1176 echo "<div class=\"mycards\">Your cards are: <br />\n";
1177 foreach($mycards as $card)
1178 display_card($card,$PREF['cardset']);
1181 break; /* not sure this works... the idea is that you can
1182 * only play a card after everyone is ready to play */
1186 /* get time from the last action of the game */
1187 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1188 $gameend = time() - strtotime($r[0]);
1190 /* handle comments in case player didn't play a card, allow comments a week after the end of the game */
1191 if( (!myisset('card') && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
1192 if(myisset('comment'))
1194 $comment = $_REQUEST['comment'];
1195 $playid = DB_get_current_playid($gameid);
1198 DB_insert_comment($comment,$playid,$myid);
1201 /* get everything relevant to display the tricks */
1202 $result = DB_query("SELECT Hand_Card.card_id as card,".
1203 " Hand.position as position,".
1204 " Play.sequence as sequence, ".
1206 " GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
1207 " SEPARATOR '\n' ), ".
1208 " Play.create_date, ".
1211 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1212 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1213 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1214 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1215 "LEFT JOIN User On User.id=Comment.user_id ".
1216 "WHERE Trick.game_id='".$gameid."' ".
1217 "GROUP BY Trick.id, sequence ".
1218 "ORDER BY Trick.id, sequence ASC");
1220 $lasttrick = DB_get_max_trickid($gameid);
1222 $play = array(); /* needed to calculate winner later */
1224 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1225 $firstcard = ''; /* first card in a trick */
1227 echo "\n<ul class=\"tricks\">\n";
1228 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
1230 /* output vorbehalte */
1231 $mygametype = DB_get_gametype_by_gameid($gameid);
1232 if($mygametype != 'normal' && $mygametype != 'silent') /* only show when needed */
1234 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
1235 " <div class=\"trick\" id=\"trick0\">\n";
1237 /* get information so show the cards that have been handed over in a poverty game */
1238 output_exchanged_cards();
1240 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1244 while($r = DB_fetch_array($result))
1252 /* count number of tricks */
1256 /* check if first schweinchen has been played */
1257 if( $GAME['schweinchen-who'] && ($r[0] == 19 || $r[0] == 20) )
1258 if(!$GAME['schweinchen-first'])
1259 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1261 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1263 /* save card to be able to find the winner of the trick later */
1264 $play[$seq] = array('card'=>$r[0],'pos'=>$pos);
1268 /* first card in a trick, output some html */
1269 if($trick!=$lasttrick)
1271 /* start of an old trick? */
1272 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1273 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1274 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1276 else if($trick==$lasttrick)
1278 /* start of a last trick? */
1279 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1280 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1281 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1284 /* remember first card, so that we are able to check, what cards can be played */
1289 echo " <div class=\"card".($pos-1)."\">\n";
1291 /* display comments */
1293 echo " <span class=\"comment\">".$comment."</span>\n";
1296 display_card($r[0],$PREF['cardset']);
1298 echo " </div>\n"; /* end div card */
1303 $winner = get_winner($play,$gametype); /* returns the position */
1304 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1308 /* whos turn is it? */
1311 $winner = get_winner($play,$gametype); /* returns the position */
1313 $firstcard = ''; /* new trick, no first card */
1318 if($next==5) $next = 1;
1321 /* my turn?, display cards as links, ask for comments*/
1322 if(DB_get_pos_by_hash($me) == $next)
1327 /* do we want to play a card? */
1328 if(myisset('card') && $myturn)
1330 $card = $_REQUEST['card'];
1331 $handid = DB_get_handid('hash',$me);
1332 $commentSchweinchen =''; /* used to add a comment when Schweinchen is being played */
1334 /* check if we have card and that we haven't played it yet*/
1335 /* set played in hand_card to true where hand_id and card_id*/
1336 $r = DB_query_array("SELECT id FROM Hand_Card WHERE played='false' and ".
1337 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1338 $handcardid = $r[0];
1340 if($handcardid) /* everything ok, play card */
1342 /* update Game timestamp */
1343 DB_update_game_timestamp($gameid);
1345 /* mark card as played */
1346 DB_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1347 DB_quote_smart($card));
1349 /* get trick id or start new trick */
1350 $a = DB_get_current_trickid($gameid);
1355 $playid = DB_play_card($trickid,$handcardid,$sequence);
1357 /* check special output for schweinchen in case in case a fox is being played
1358 * check for correct rules, etc. has already been done
1360 if( $GAME['schweinchen-who'] && ($card == 19 || $card == 20) )
1362 if(!$GAME['schweinchen-first'])
1363 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1365 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1367 if( $RULES['schweinchen']=='both' ||
1368 ($RULES['schweinchen']=='second' && $GAME['schweinchen-second']==1 )||
1369 ($RULES['schweinchen']=='secondaftercall' && $GAME['schweinchen-second']==1 &&
1370 (DB_get_call_by_hash($GAME['schweinchen-who']) || DB_get_partner_call_by_hash($GAME['schweinchen-who']) ))
1373 DB_insert_comment('Schweinchen! ',$playid,$myid);
1374 $commentSchweinchen = 'Schweinchen! ';
1377 echo 'schweinchen = '.$GAME['schweinchen-who'].' ---<br />';
1380 /* if sequence == 4 check who one in case of wedding */
1381 if($sequence == 4 && $GT == 'wedding')
1383 /* is wedding resolve */
1384 $resolved = DB_get_sickness_by_gameid($gameid);
1387 /* who has wedding */
1388 $userids = DB_get_all_userid_by_gameid($gameid);
1389 foreach($userids as $user)
1391 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1392 if($usersick == 'wedding')
1395 /* who won the trick */
1396 $play = DB_get_cards_by_trick($trickid);
1397 $winner = get_winner($play,$gametype); /* returns the position */
1398 $winnerid = DB_get_userid('gameid-position',$gameid,$winner);
1399 /* is tricknr <=3 */
1400 if($tricknr <=3 && $winnerid!=$whosick)
1402 /* set resolved at tricknr*/
1403 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1405 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1406 DB_set_party_by_hash($whash,'re');
1408 if($tricknr == 3 && $winnerid==$whosick)
1410 /* set resolved at tricknr*/
1411 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1416 /* if sequence == 4, set winner of the trick, count points and set the next player */
1419 $play = DB_get_cards_by_trick($trickid);
1420 $winner = get_winner($play,$gametype); /* returns the position */
1422 /* check if someone caught a fox */
1423 /* first check if we should account for solos at all,
1424 * since it doesn't make sense in some games
1426 $ok = 0; /* fox shouldn't be counted */
1427 if(DB_get_gametype_by_gameid($gameid)=='solo')
1429 $solo = DB_get_solo_by_gameid($gameid);
1430 if($solo == 'trump' || $solo == 'silent')
1431 $ok = 1; /* for trump solos and silent solos, foxes are ok */
1434 $ok = 1; /* for all other games (not solos) foxes are ok too */
1437 foreach($play as $played)
1439 if ( $played['card']==19 || $played['card']==20 )
1440 if ($played['pos']!= $winner )
1442 /* possible caught a fox, check party */
1443 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1444 $uid2 = DB_get_userid('gameid-position',$gameid,$played['pos']);
1446 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1447 $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1449 if($party1 != $party2)
1450 DB_query("INSERT INTO Score".
1451 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1455 /* check for karlchen (jack of clubs in the last trick)*/
1456 /* same as for foxes, karlchen doesn't always make sense
1457 * check what kind of game it is and set karlchen accordingly */
1458 $ok = 1; /* default: karlchen should be accounted for */
1460 $ok = 0; /* Karlchen works only in the last trick */
1461 if($ok && DB_get_gametype_by_gameid($gameid)=='solo' )
1463 $solo = DB_get_solo_by_gameid($gameid);
1464 if($solo == 'trumpless' || $solo == 'jack' || $solo == 'queen' )
1465 $ok = 0; /* no Karlchen in these solos */
1469 foreach($play as $played)
1470 if ( $played['card']==11 || $played['card']==12 )
1471 if ($played['pos'] == $winner )
1473 /* possible caught a fox, check party */
1474 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1475 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1477 DB_query("INSERT INTO Score".
1478 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1480 /* check for doppelopf (>40 points)*/
1482 foreach($play as $played)
1484 $points += DB_get_card_value_by_cardid($played['card']);
1488 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1489 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1491 DB_query("INSERT INTO Score".
1492 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
1496 DB_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1498 echo "ERROR during scoring";
1501 echo "DEBUG: position $winner won the trick <br />";
1503 /* who is the next player? */
1508 $next = DB_get_pos_by_hash($me)+1;
1510 if($next==5) $next=1;
1512 /* check for coment */
1513 if(myisset('comment'))
1515 $comment = $_REQUEST['comment'];
1517 DB_insert_comment($comment,$playid,$myid);
1518 if($commentSchweinchen)
1519 $comment = $commentSchweinchen . $comment;
1522 /* display played card */
1523 $pos = DB_get_pos_by_hash($me);
1526 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1527 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1528 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1531 echo " <div class=\"card".($pos-1)."\">\n ";
1533 /* display comments */
1534 display_card($card,$PREF['cardset']);
1536 echo "\n <span class=\"comment\"> ".$comment."</span>\n";
1539 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1541 /*check if we still have cards left, else set status to gameover */
1542 if(sizeof(DB_get_hand($me))==0)
1544 DB_set_hand_status_by_hash($me,'gameover');
1545 $mystatus = 'gameover';
1548 /* if all players are done, set game status to game over,
1549 * get the points of the last trick and send out an email
1552 $userids = DB_get_all_userid_by_gameid($gameid);
1555 foreach($userids as $user)
1556 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1560 DB_set_game_status_by_gameid($gameid,'gameover');
1562 /* email next player, if game is still running */
1563 if(DB_get_game_status_by_gameid($gameid)=='play')
1565 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1566 $who = DB_get_userid('hash',$next_hash);
1567 DB_set_player_by_gameid($gameid,$who);
1569 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1570 "It's your turn now.\n".
1571 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$next_hash."\n\n" ;
1572 if( DB_get_email_pref_by_uid($who)!='emailaddict' )
1574 $subject = 'A card has been played in game '.DB_format_gameid($gameid);
1575 mymail($who,$subject,$message);
1578 else /* send out final email */
1580 /* individual score */
1581 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1582 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1583 " LEFT JOIN User ON User.id=Hand.user_id".
1584 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1585 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1586 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1587 " WHERE Hand.game_id='$gameid'".
1588 " GROUP BY User.fullname" );
1589 $message = "The game is over. Thanks for playing :)\n";
1590 $message .= "Final score:\n";
1591 while( $r = DB_fetch_array($result) )
1592 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1594 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1595 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1596 " LEFT JOIN User ON User.id=Hand.user_id".
1597 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1598 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1599 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1600 " WHERE Hand.game_id='$gameid'".
1601 " GROUP BY Hand.party" );
1602 $message .= "\nTotals:\n";
1605 while( $r = DB_fetch_array($result) )
1607 $message .= " ".$r[0]." ".$r[1]."\n";
1610 else if($r[0] == 'contra')
1615 * save score in database
1619 /* get calls from re/contra */
1621 $call_contra = NULL;
1622 foreach($userids as $user)
1624 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1625 $call = DB_get_call_by_hash($hash);
1626 $party = DB_get_party_by_hash($hash);
1630 $call = (int) $call;
1636 else if( $call < $call_re)
1639 else if($party=='contra')
1641 if($call_contra==NULL)
1642 $call_contra = $call;
1643 else if( $call < $call_contra)
1644 $call_contra = $call;
1649 /* figure out who one */
1650 $winning_party = NULL;
1652 if($call_re == NULL && $call_contra==NULL)
1654 /* nobody made a call, so it's easy to figure out who won */
1656 $winning_party='re';
1658 $winning_party='contra';
1662 /* if one party makes a call, they only win, iff they make enough points
1663 * if only one party made a call, the other one wins,
1664 * if the first one didn't make it
1668 $offset = 120 - $call_re;
1670 $offset--; /* since we use a > in the next equation */
1672 if($re > 120+$offset)
1673 $winning_party='re';
1674 else if ($call_contra == NULL )
1675 $winning_party='contra';
1680 $offset = 120 - $call_contra;
1681 if($call_contra == 0)
1682 $offset--; /* since we use a > in the next equation */
1684 if($contra > 120+$offset)
1685 $winning_party='contra';
1686 else if ($call_re == NULL )
1687 $winning_party='re';
1691 /* one point for each call of the other party in case the other party didn't win
1692 * and one point each in case the party made more than points than one of the calls
1694 if($winning_party!='contra' && $call_contra!=NULL)
1696 for( $p=$call_contra;$p<=120; $p+=30 )
1698 DB_query("INSERT INTO Score".
1699 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
1702 for( $p=$call_contra; $p<120; $p+=30)
1705 DB_query("INSERT INTO Score".
1706 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'made$p')");
1709 if($winning_party!='re' and $call_re!=NULL)
1711 for( $p=$call_re;$p<=120; $p+=30 )
1713 DB_query("INSERT INTO Score".
1714 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'against$p')");
1717 for( $p=$call_re; $p<120; $p+=30)
1720 DB_query("INSERT INTO Score".
1721 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
1725 /* point in case contra won */
1726 if($winning_party=='contra')
1728 DB_query("INSERT INTO Score".
1729 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1732 /* one point each for winning and each 30 points + calls */
1733 if($winning_party=='re')
1735 foreach(array(120,150,180,210,240) as $p)
1738 if($p==240 || $call_contra!=NULL)
1742 DB_query("INSERT INTO Score".
1743 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
1745 /* re called something and won */
1746 foreach(array(0,30,60,90,120) as $p)
1748 if($call_re!=NULL && $call_re<$p+1)
1749 DB_query("INSERT INTO Score".
1750 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
1753 else if( $winning_party=='contra')
1755 foreach(array(120,150,180,210,240) as $p)
1758 if($p==240 || $call_re!=NULL)
1761 if($contra>$p-$offset)
1762 DB_query("INSERT INTO Score".
1763 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
1765 /* re called something and won */
1766 foreach(array(0,30,60,90,120) as $p)
1768 if($call_contra!=NULL && $call_contra<$p+1)
1769 DB_query("INSERT INTO Score".
1770 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
1775 /* add score points to email */
1778 $message .= " Points Re: \n";
1779 $queryresult = DB_query("SELECT score FROM Score ".
1780 " WHERE game_id=$gameid AND party='re'".
1782 while($r = DB_fetch_array($queryresult) )
1784 $message .= " ".$r[0]."\n";
1787 $message .= " Points Contra: \n";
1788 $queryresult = DB_query("SELECT score FROM Score ".
1789 " WHERE game_id=$gameid AND party='contra'".
1791 while($r = DB_fetch_array($queryresult) )
1793 $message .= " ".$r[0]."\n";
1796 $message .= " Total Points (from the Re point of view): $Tpoint\n";
1799 $session = DB_get_session_by_gameid($gameid);
1800 $score = generate_score_table($session);
1802 $message .= "Score Table:\n";
1803 $message .= format_score_table_ascii($score);
1804 $message .= "\nUse these links to have a look at game ".DB_format_gameid($gameid).": \n";
1806 /* send out final email */
1807 foreach($userids as $user)
1809 /* add links for all players */
1810 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1811 $name = DB_get_name('userid',$user);
1813 $link = "$name: ".$HOST.$INDEX."?action=game&me=".$hash."\n" ;
1816 $message .= "\n\n (you can use reply all on this email to reach all the players.)\n\n";
1817 $subject = ' Game over (game '.DB_format_gameid($gameid).') ';
1818 mymail($userids,$subject,$message);
1823 echo "can't find that card?! <br />\n";
1826 else if(myisset('card') && !$myturn )
1828 echo "please wait until it's your turn! <br />\n";
1831 if($seq!=4 && $trickNR>=1 && !(myisset('card') && $myturn) )
1832 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1834 /* display points in case game is over */
1835 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1837 echo " <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1838 " <div class=\"trick\" id=\"trick13\">\n";
1839 /* add pic for re/contra
1840 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1842 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1843 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1844 " LEFT JOIN User ON User.id=Hand.user_id".
1845 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1846 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1847 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1848 " WHERE Hand.game_id='$gameid'".
1849 " GROUP BY User.fullname" );
1850 while( $r = DB_fetch_array($result))
1851 echo " <div class=\"card".($r[3]-1)."\">\n".
1852 " <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
1855 /* display totals */
1856 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1857 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1858 " LEFT JOIN User ON User.id=Hand.user_id".
1859 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1860 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1861 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1862 " WHERE Hand.game_id='$gameid'".
1863 " GROUP BY Hand.party" );
1864 echo "<div class=\"total\">\n Totals:<br />\n";
1865 while( $r = DB_fetch_array($result))
1866 echo " ".$r[0]." ".$r[1]."<br />\n";
1868 $queryresult = DB_query("SELECT timediff(mod_date,create_date) ".
1869 " FROM Game WHERE id='$gameid'");
1870 $r = DB_fetch_array($queryresult);
1871 echo " <p>This game took ".$r[0]." hours.</p>\n";
1873 echo " <div class=\"re\">\n Points Re: <br />\n";
1874 $queryresult = DB_query("SELECT score FROM Score ".
1875 " WHERE game_id=$gameid AND party='re'".
1877 while($r = DB_fetch_array($queryresult) )
1878 echo " ".$r[0]."<br />\n";
1881 echo " <div class=\"contra\">\n Points Contra: <br />\n";
1882 $queryresult = DB_query("SELECT score FROM Score ".
1883 " WHERE game_id=$gameid AND party='contra'".
1885 while($r = DB_fetch_array($queryresult) )
1886 echo " ".$r[0]."<br />\n";
1891 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1894 echo " <li onclick=\"hl_prev();\" class=\"old\"><a href=\"#\">prev</a></li>\n";
1895 echo " <li onclick=\"hl_next();\" class=\"old\"><a href=\"#\">next</a></li>\n";
1896 echo "</ul>\n"; /* end ul tricks*/
1898 $mycards = DB_get_hand($me);
1899 $mycards = mysort($mycards,$gametype);
1900 echo "<div class=\"mycards\">\n";
1902 if($myturn && !myisset('card') && $mystatus=='play' )
1904 echo "Hello ".$myname.", it's your turn! <br />\n";
1905 echo "Your cards are: <br />\n";
1907 /* do we have to follow suite? */
1909 if(have_suit($mycards,$firstcard))
1912 foreach($mycards as $card)
1914 /* display only cards that the player is allowed to play as links, the rest just display normal
1915 * also check if we have both schweinchen, in that case only display on of them as playable
1917 if( ($followsuit && !same_type($card,$firstcard)) ||
1918 ( (int)($card)==19 && !$GAME['schweinchen-first'] &&
1919 ($RULES['schweinchen']=='second'||
1920 ( $RULES['schweinchen']=='secondaftercall' &&
1921 (DB_get_call_by_hash($GAME['schweinchen-who']) || DB_get_partner_call_by_hash($GAME['schweinchen-who']) )))
1922 && $GAME['schweinchen-who']==$me )
1924 display_card($card,$PREF['cardset']);
1926 display_link_card($card,$PREF['cardset']);
1929 else if($mystatus=='play' )
1931 echo "Your cards are: <br />\n";
1932 foreach($mycards as $card)
1933 display_card($card,$PREF['cardset']);
1935 else if($mystatus=='gameover')
1937 $oldcards = DB_get_all_hand($me);
1938 $oldcards = mysort($oldcards,$gametype);
1939 echo "Your cards were: <br />\n";
1940 foreach($oldcards as $card)
1941 display_card($card,$PREF['cardset']);
1943 $userids = DB_get_all_userid_by_gameid($gameid);
1944 foreach($userids as $user)
1946 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1952 $name = DB_get_name('userid',$user);
1953 $oldcards = DB_get_all_hand($userhash);
1954 $oldcards = mysort($oldcards,$gametype);
1955 echo "$name's cards were: <br />\n";
1956 foreach($oldcards as $card)
1957 display_card($card,$PREF['cardset']);
1963 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1964 if($mystatus=='play')
1967 /* the following happens only when the gamestatus is 'gameover' */
1968 /* check if game is over, display results */
1969 if(DB_get_game_status_by_gameid($gameid)=='play')
1971 echo "The game is over for you.. other people still need to play though";
1975 myerror("error in testing the status");
1978 /* output other games where it is the users turn
1979 * make sure that the people looking at old games don't see the wrong games here
1981 if( $mystatus != 'gameover' )
1982 display_user_menu($myid);
1983 else if( $mystatus == 'gameover' &&
1984 isset($_SESSION['id']) )
1986 display_user_menu($_SESSION['id']);
1990 echo "<div class=\"usermenu\">\n";
1991 echo "It's your turn in these games:<br />\n";
1992 echo "Please log in to see this information.\n";
1996 /* display rule set for this game */
1997 echo "<div class=\"gameinfo\">\n";
1999 if($gamestatus == 'play' )
2001 $myparty = DB_get_party_by_hash($me);
2002 output_form_calls($me,$myparty);
2004 /* get time from the last action of the game */
2005 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
2006 $gameend = time() - strtotime($r[0]);
2008 if($gamestatus == 'play' || $gameend < 60*60*24*7)
2010 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
2013 echo "<input type=\"submit\" value=\"submit\" />\n";
2015 /* has this hand been played by others? */
2016 $other_game_ids = DB_played_by_others($gameid);
2017 if(sizeof($other_game_ids)>0 && $mystatus=='gameover')
2019 $mypos = DB_get_pos_by_hash($me);
2020 echo "<p>See how other played the same hand: <br />\n";
2021 foreach($other_game_ids as $id)
2023 $otherhash = DB_get_hash_from_game_and_pos($id,$mypos);
2024 $othername = DB_get_name('hash',$otherhash);
2025 echo "<a href=\"$INDEX?action=game&me=$otherhash\">$othername</a><br />";
2034 $gamestatus = DB_get_game_status_by_gameid($gameid);
2035 if($mystatus=='gameover' &&
2036 ($gamestatus =='gameover' || $gamestatus =='cancel-nines' || $gamestatus =='cancel-trump') &&
2037 isset($_SESSION['id']) && $_SESSION['id']==$myid)
2039 $session = DB_get_session_by_gameid($gameid);
2040 $result = DB_query("SELECT id,create_date FROM Game".
2041 " WHERE session=$session".
2042 " ORDER BY create_date DESC".
2046 $r = DB_fetch_array($result);
2048 if(!$session || $gameid==$r[0])
2050 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
2051 $names = DB_get_all_names_by_gameid($gameid);
2052 $type = DB_get_gametype_by_gameid($gameid);
2056 $solo = DB_get_solo_by_gameid($gameid);
2058 if($solo!='silent') /* repeat game with same first player */
2059 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
2060 else /* rotate normally */
2061 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
2063 else if($gamestatus == 'cancel-nines' || $gamestatus == 'cancel-trump')
2064 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
2065 else /* rotate normally */
2066 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);