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";
213 $score = generate_score_table($session);
215 $finalscore = array_pop($tmpscore);
216 $finalscore = $finalscore['players'];
218 echo " <div class=\"sessionscore\">";
222 /* output the final score on the front page */
223 foreach($finalscore as $user=>$value)
225 $name = DB_get_name('userid',$user);
226 echo " ".substr($name,0,2).": $value ";
231 /* first game, no score yet */
235 echo format_score_table_html($score,$myid);
238 /* figure out which game in a session we are in and link to the previous and next game if possible */
239 $hashes = DB_get_hashes_by_session($session,$myid);
242 foreach($hashes as $hash)
252 $previous = $hashes[$j-2];
260 if(isset($_SESSION['id']) && $_SESSION['id']==$myid)
263 echo "<a href=\"{$INDEX}?action=game&me=$previous\">previous game</a> \n";
264 echo "This is game number $j of <a href=\"{$INDEX}?action=game&me=$lasthash\">$i</a> in session $session.\n";
266 echo " <a href=\"{$INDEX}?action=game&me=$next\">next game</a> \n";
269 echo "This is game number $j of $i in session $session.";
273 /* display the table and the names */
276 /* mystatus gets the player through the different stages of a game.
277 * start: does the player want to play?
278 * init: check for sickness
279 * check: check for return values from init
280 * poverty: handle poverty, wait here until all player have reached this state
281 * display sickness and move on to game
282 * play: game in progress
283 * gameover: are we revisiting a game
288 /* don't ask if user has autosetup set to yest */
290 if($PREF['autosetup']=='yes') $skip = 1;
292 if( !myisset('in') && !$skip)
294 /* asks the player, if he wants to join the game */
295 output_check_want_to_play($me);
300 /* check the result, if player wants to join, got next stage, else cancel game */
301 if(!$skip && $_REQUEST['in'] == 'no' )
303 /* cancel the game */
304 $message = "Hello, \n\n".
305 "the game has been canceled due to the request of one of the players.\n";
307 $userids = DB_get_all_userid_by_gameid($gameid);
308 foreach($userids as $user)
310 $subject = 'Game '.DB_format_gameid($gameid).' canceled';
311 mymail($user,$subject,$message);
314 /* delete everything from the dB */
320 /* user wants to join the game */
322 /* move on to the next stage,
323 * no break statement to immediately go to the next stage
326 DB_set_hand_status_by_hash($me,'init');
328 /* check if everyone has reached this stage, send out email */
329 $userids = DB_get_all_userid_by_gameid($gameid);
331 foreach($userids as $user)
333 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
334 if($userstat!='init')
336 /* whos turn is it? */
337 DB_set_player_by_gameid($gameid,$user);
344 /* all done, send out email unless this player is the startplayer */
345 $startplayer = DB_get_startplayer_by_gameid($gameid);
346 if($mypos == $startplayer)
348 /* do nothing, go to next stage */
352 /* email startplayer */
354 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
355 $who = DB_get_userid('hash',$hash);
356 DB_set_player_by_gameid($gameid,$who);
358 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
359 "Use this link to go the game: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
360 mymail($who,"Ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
367 /* here we ask the player if he is sick */
368 $mycards = DB_get_hand($me);
369 $mycards = mysort($mycards,$gametype);
371 if(!myisset('solo','wedding','poverty','nines') )
373 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
374 echo "\n<ul class=\"tricks\">\n";
375 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
376 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
377 " <div class=\"trick\" id=\"trick0\">\n";
379 for($pos=1;$pos<5;$pos++)
381 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
382 $userid = DB_get_userid('gameid-position',$gameid,$pos);
383 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
385 if($userstatus=='start' || $userstatus=='init')
386 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs <br/ > to decide </div>\n"; /* show this to everyone */
388 if($usersick!=NULL && $pos<=$mypos ) /* only show this for people sitting before the player */
389 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
390 else if($usersick==NULL && $pos<=$mypos)
391 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
393 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
394 /* end displaying sickness */
396 output_check_for_sickness($me,$mycards);
398 echo "<div class=\"mycards\">Your cards are: <br />\n";
399 foreach($mycards as $card)
400 display_card($card,$PREF['cardset']);
407 /* check if someone selected more than one sickness */
409 if($_REQUEST['solo']!='No') $Nsickness++;
410 if($_REQUEST['wedding'] == 'yes') $Nsickness++;
411 if($_REQUEST['poverty'] == 'yes') $Nsickness++;
412 if($_REQUEST['nines'] == 'yes') $Nsickness++;
416 echo "<p class=\"message\"> You selected more than one sickness, please go back ".
417 "and answer the <a href=\"$INDEX?action=game&me=$me&in=yes\">question</a> again.</p>";
419 echo "<div class=\"mycards\">Your cards are: <br />\n";
420 foreach($mycards as $card)
421 display_card($card,$PREF['cardset']);
428 /* everything is ok, save what user said and proceed */
429 echo "<p class=\"message\">Processing what you selected in the last step...";
431 /* check if this sickness needs to be handled first */
432 $gametype = DB_get_gametype_by_gameid($gameid);
433 $startplayer = DB_get_startplayer_by_gameid($gameid); /* need this to check which solo goes first */
435 if( $_REQUEST['solo']!='No' )
437 /* user wants to play a solo */
439 /* store the info in the user's hand info */
440 DB_set_solo_by_hash($me,$_REQUEST['solo']);
441 DB_set_sickness_by_hash($me,'solo');
443 echo "<br />Seems like you want to play a {$_REQUEST['solo']} solo. Got it.<br />\n";
445 if($gametype == 'solo' && $startplayer<$mypos)
446 {}/* do nothing, since someone else already is playing solo */
449 /* this solo comes first
450 * store info in game table
452 DB_set_gametype_by_gameid($gameid,'solo');
453 DB_set_startplayer_by_gameid($gameid,$mypos);
454 DB_set_solo_by_gameid($gameid,$_REQUEST['solo']);
457 else if($_REQUEST['wedding'] == 'yes')
459 /* silent solo is set further down */
460 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
461 DB_set_sickness_by_hash($me,'wedding');
463 else if($_REQUEST['poverty'] == 'yes')
465 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
466 DB_set_sickness_by_hash($me,'poverty');
468 else if($_REQUEST['nines'] == 'yes')
470 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
471 " is playing solo, this game will be canceled.<br />\n";
472 DB_set_sickness_by_hash($me,'nines');
477 /* move on to the next stage*/
478 DB_set_hand_status_by_hash($me,'check');
483 /* here we check what all players said and figure out what game we are playing
484 * this can therefore only be handled once all players finished the last stage
487 $mycards = DB_get_hand($me);
488 $mycards = mysort($mycards,$gametype);
490 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
491 echo "\n<ul class=\"tricks\">\n";
492 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
493 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
494 " <div class=\"trick\" id=\"trick0\">\n";
496 for($pos=1;$pos<5;$pos++)
498 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
499 $userid = DB_get_userid('gameid-position',$gameid,$pos);
500 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
502 if($userstatus=='start' || $userstatus=='init')
503 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs <br />to decide </div>\n"; /* show this to everyone */
505 if($usersick!=NULL) /* in the init-phase we only showed players with $pos<$mypos, now we can show all */
506 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
508 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
510 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
511 /* end displaying sickness */
513 echo "<div class=\"message\">\n";
514 echo "<p> Checking if someone else selected solo, nines, wedding or poverty.</p>";
516 /* check if everyone has reached this stage */
517 $userids = DB_get_all_userid_by_gameid($gameid);
519 foreach($userids as $user)
521 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
522 if($userstat!='check')
525 DB_set_player_by_gameid($gameid,$user);
532 echo "<p>This step can only be handled after everyone finished the last step. ".
533 "Seems like this is not the case, so you need to wait a bit... ".
534 "you will get an email once that is the case, please use the link in ".
535 "that email to continue the game.</p></div>";
537 /* display cards, if player was just at the init-phase he will still see the cards from there
538 * we can put this one here, since the last player to finish the init state won't get here and
539 * will still see his card anyway from the init-phase
541 if($mystatus=='check')
544 echo "<div class=\"mycards\">Your cards are: <br />\n";
545 foreach($mycards as $card)
546 display_card($card,$PREF['cardset']);
553 /* Ok, everyone finished the init-phase, time to figure out what game we
554 * are playing, in case there are any solos this already
555 * will have the correct information in it */
557 echo "<p> Ok, everyone is done... figuring out what kind of game we are playing.</p>";
559 $gametype = DB_get_gametype_by_gameid($gameid);
560 $startplayer = DB_get_startplayer_by_gameid($gameid);
562 /* check for sickness */
567 foreach($userids as $user)
569 $name = DB_get_name('userid',$user);
570 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
571 if($usersick == 'nines')
574 break; /* no need to check for other poverties, since only solo can win and that is already set */
576 else if($usersick == 'poverty')
578 else if($usersick == 'wedding')
580 else if($usersick == 'solo')
584 /* now check which sickness comes first and set the gametype to it */
585 if($gametype == 'solo')
592 /* TODO: should we keep statistics of this? */
593 $message = "Hello, \n\n".
594 " the game has been canceled because ".DB_get_name('userid',$nines).
595 " has five or more nines and nobody is playing solo.\n\n".
596 " To redeal either start a new game or, in case the game was part of a tournament, \n".
597 " go to the last game and use the link at the bottom of the page to redeal.";
599 $userids = DB_get_all_userid_by_gameid($gameid);
600 foreach($userids as $user)
602 $subject = 'Game '.DB_format_gameid($gameid).' canceled';
603 mymail($user,$subject,$message);
606 /* delete everything from the dB */
609 echo "The game has been canceled because ".DB_get_name('userid',$nines).
610 " has five or more nines and nobody is playing solo.\n";
613 else if($poverty==1) /* one person has poverty */
615 DB_set_gametype_by_gameid($gameid,'poverty');
616 $gametype = 'poverty';
617 $who = DB_get_sickness_by_gameid($gameid);
620 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
621 if($firstsick == 'poverty')
622 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
624 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
627 else if($poverty==2) /* two people have poverty */
629 DB_set_gametype_by_gameid($gameid,'dpoverty');
630 $gametype = 'dpoverty';
631 $who = DB_get_sickness_by_gameid($gameid);
634 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
635 if($firstsick == 'poverty')
637 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
638 if($secondsick == 'poverty')
639 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
641 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
644 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
649 DB_set_gametype_by_gameid($gameid,'wedding');
650 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
651 $gametype = 'wedding';
653 /* now the gametype is set correctly in the database */
654 echo "<p> Got it :)</p>";
656 /* loop over all players, set re/contra if possible and start the game if possible */
657 $userids = DB_get_all_userid_by_gameid($gameid);
658 foreach($userids as $userid)
660 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
665 /* are we the solo player? set us to re, else set us to contra */
666 $pos = DB_get_pos_by_hash($userhash);
667 if($pos == $startplayer)
668 DB_set_party_by_hash($userhash,'re');
670 DB_set_party_by_hash($userhash,'contra');
671 DB_set_hand_status_by_hash($userhash,'play');
675 /* set person with the wedding to re, do the rest during the game */
676 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
677 if($usersick == 'wedding')
678 DB_set_party_by_hash($userhash,'re');
680 DB_set_party_by_hash($userhash,'contra');
682 DB_set_hand_status_by_hash($userhash,'play');
686 $hand = DB_get_all_hand($userhash);
688 if(in_array('3',$hand)||in_array('4',$hand))
689 DB_set_party_by_hash($userhash,'re');
691 DB_set_party_by_hash($userhash,'contra');
692 DB_set_hand_status_by_hash($userhash,'play');
696 /* set person with poverty to play status */
697 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
698 if($usersick == 'poverty')
699 DB_set_hand_status_by_hash($userhash,'play');
701 /* set status of first player to be asked to poverty */
702 $who = DB_get_sickness_by_gameid($gameid);
703 if($who > 6) $who= $who/10; /* in case we have dpoverty */
704 $whoid = DB_get_userid('gameid-position',$gameid,$who);
706 DB_set_hand_status_by_hash($userhash,'poverty');
709 /* check for silent solo, set game type to solo in this case */
710 $gametype = DB_get_gametype_by_gameid($gameid);
711 $userids = DB_get_all_userid_by_gameid($gameid);
712 foreach($userids as $userid)
714 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
716 if($gametype=='normal')
718 $userhand = DB_get_all_hand($userhash);
719 if(check_wedding($userhand))
721 /* normal game type and player has both queens -> silent solo */
722 /* keep startplayer, just set gametype to silent solo */
723 DB_set_gametype_by_gameid($gameid,'solo');
724 DB_set_solo_by_gameid($gameid,'silent');
729 /* send out email to first player or poverty person*/
730 if($gametype!='poverty' && $gametype!='dpoverty')
732 $startplayer = DB_get_startplayer_by_gameid($gameid);
733 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
734 $who = DB_get_userid('hash',$hash);
735 DB_set_player_by_gameid($gameid,$who);
739 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
741 /* email startplayer */
742 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
743 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
744 $subject = 'Ready, set, go... (game '.DB_format_gameid($gameid).')';
745 mymail($who,$subject,$message);
749 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
753 /* set status of first player to be asked to poverty */
754 $who = DB_get_sickness_by_gameid($gameid);
755 if($who > 6) $who= $who/10; /* in case we have dpoverty */
757 $whoid = DB_get_userid('gameid-position',$gameid,$who);
759 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
762 $whohash = DB_get_hash_from_game_and_pos($gameid,$who);
763 DB_set_player_by_gameid($gameid,$whoid);
765 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
767 /* email player for poverty */
768 $message = "Poverty: It's your turn now in game ".DB_format_gameid($gameid).".\n".
769 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$whohash."\n\n" ;
770 $subject = 'Poverty (game '.DB_format_gameid($gameid).') ';
771 mymail($whoid,$subject,$message);
779 /* user only gets here in a poverty game, several things have to be handled here:
780 * A) ask, if user wants to take trump
782 * poverty: set re/contra
783 * dpoverty: first time: set re, send email to second player
784 * second time: set contra
785 * poverty: set status of other players to 'play'
786 * set status to play in case 0 trump
787 * no -> set status to play,
788 * ask next player or cancle the game if no more players
789 * B) user took trump and has too many cards (e.g. count(cards)>12 and re/contra set)
790 * ask to give cards back, set status to play, once player has 12 cards
792 * it is easier to check B) first
795 set_gametype($gametype); /* this sets the $CARDS variable */
796 $myparty = DB_get_party_by_hash($me);
798 /* the following is part B) of whats needs to be done)
799 /* check if user wants to give cards back */
800 if(myisset('exchange'))
802 $exchange = $_REQUEST['exchange'];
803 $partnerhash = DB_get_partner_hash_by_hash($me);
804 $partnerid = DB_get_userid('hash',$partnerhash);
805 $partnerhand = DB_get_handid('gameid-userid',$gameid,$partnerid);
807 /* if exchange is set to a value>0, exchange that card back to the partner */
810 $result = DB_query("UPDATE Hand_Card SET hand_id='$partnerhand'".
811 " WHERE hand_id='$myhand' AND card_id=".DB_quote_smart($exchange));
812 DB_add_exchanged_card(DB_quote_smart($exchange),$myhand,$partnerhand);
817 $mycards = DB_get_hand($me);
818 $mycards = mysort($mycards,$gametype);
820 /* output pre-game trick in case user reloads,
821 * only needs to be done when a team has been formed */
822 if($myparty=='re' || $myparty=='contra')
824 echo "\n<ul class=\"tricks\">\n";
825 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
827 $mygametype = DB_get_gametype_by_gameid($gameid);
829 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
830 " <div class=\"trick\" id=\"trick0\">\n";
832 /* get information so show the cards that have been handed over in a poverty game */
833 output_exchanged_cards();
835 echo " </div>\n </li>\n"; /* end div trick, end li trick */
837 /* end output pre-game trick */
839 /* check if user need to give more cards back */
840 if( ($myparty=='re' || $myparty=='contra') && count($mycards)>12)
842 echo "<div class=\"poverty\"> you need to get rid of a few cards</div>\n";
845 echo "<div class=\"mycards\">Your cards are: <br />\n";
846 foreach($mycards as $card)
847 display_link_card($card,$PREF['cardset'],$type);
848 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select card to give back\" />\n";
851 else if( ($myparty=='re' || $myparty=='contra') && count($mycards)==12)
853 /* user is done, ready to play */
854 DB_set_hand_status_by_hash($me,'play');
856 /* email start player */
857 $startplayer = DB_get_startplayer_by_gameid($gameid);
858 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
859 $who = DB_get_userid('hash',$hash);
860 DB_set_player_by_gameid($gameid,$who);
864 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
866 /* email startplayer */
867 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
868 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
869 $subject = 'Ready, set, go... (game '.DB_format_gameid($gameid).') ';
870 mymail($who,$subject,$message);
874 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
877 /* the following is part A) of what needs to be done */
878 if(!myisset('trump'))
882 echo "<div class=\"poverty\">\n";
883 $userids = DB_get_all_userid_by_gameid($gameid);
884 foreach($userids as $user)
886 $name = DB_get_name('userid',$user);
887 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
888 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
889 $userparty = DB_get_party_by_hash($userhash);
891 if($usersick=='poverty' && !$userparty)
893 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
894 $cards = DB_get_hand($hash);
897 foreach($cards as $card)
898 if($card<27) $nrtrump++;
899 echo "Player $name has $nrtrump trump. Do you want to take them?".
900 "<a href=\"index.php?action=game&me=$me&trump=$user\">Yes</a> <br />\n";
903 echo "<a href=\"index.php?action=game&me=$me&trump=no\">No way</a> <br />\n";
904 echo "</div><div>\n";
906 echo "<div class=\"mycards\">Your cards are: <br />\n";
907 foreach($mycards as $card)
908 display_card($card,$PREF['cardset']);
909 echo "</div></div>\n";
915 $trump = $_REQUEST['trump'];
919 /* user doesn't want to take trump */
920 DB_set_hand_status_by_hash($me,'play');
922 /* set next player who needs to be asked and email him*/
923 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
924 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
926 /* don't ask people who have poverty */
928 if($firstsick=='poverty')
930 if($secondsick=='poverty')
935 if($gametype=='dpoverty')
937 $next=999; /* need to cancel for sure, since both would need to take the trump */
940 /* no more people to ask, need to cancel the game */
943 $message = "Hello, \n\n".
944 "Game ".DB_format_gameid($gameid)." has been canceled since nobody wanted to take the trump.\n";
946 $userids = DB_get_all_userid_by_gameid($gameid);
947 foreach($userids as $user)
949 $subject = 'Game '.DB_format_gameid($gameid).' canceled (poverty not resolved)';
950 mymail($user,$subject,$message);
953 /* delete everything from the dB */
956 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been canceled.<br /><br /></p>";
961 /* email next player, set his status to poverty */
962 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
963 $userid = DB_get_userid('hash',$userhash);
965 DB_set_player_by_gameid($gameid,$userid);
966 DB_set_hand_status_by_hash($userhash,'poverty');
968 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
969 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
970 $subject = 'Poverty (game '.DB_format_gameid($gameid).')';
971 mymail($userid,$subject,$message);
976 /* player wants to take trump, change cards */
978 /* user wants to take trump */
979 $trump = $_REQUEST['trump'];
980 $userhand = DB_get_handid('gameid-userid',$gameid,$trump);
981 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
983 /* remember which cards were handed over*/
984 $partnerhand = DB_get_all_hand($userhash);
985 foreach ($partnerhand as $card)
987 DB_add_exchanged_card($card,$userhand,$myhand);
989 /* copy trump from player A to B */
990 $result = DB_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
993 $mycards = DB_get_hand($me);
996 if($gametype=='poverty')
998 $userids = DB_get_all_userid_by_gameid($gameid);
999 foreach($userids as $user)
1001 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1002 if($hash==$userhash||$hash==$me)
1004 DB_set_party_by_hash($hash,'re');
1008 DB_set_party_by_hash($hash,'contra');
1009 DB_set_hand_status_by_hash($hash,'play'); /* the contra party is ready to play */
1012 /* check if we are done (in case of no trump handed over), if so, go to 'play' phase right away*/
1013 if(count($mycards)==12)
1015 DB_set_hand_status_by_hash($me,'play');
1020 /* has the re party already been set?*/
1022 $userids = DB_get_all_userid_by_gameid($gameid);
1023 foreach($userids as $user)
1025 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1026 $party = DB_get_party_by_hash($hash);
1032 DB_set_party_by_hash($me,'contra');
1033 DB_set_party_by_hash($userhash,'contra');
1037 DB_set_party_by_hash($me,'re');
1038 DB_set_party_by_hash($userhash,'re');
1040 /* send out email to second non-poverty player */
1041 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
1042 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
1045 if($firstsick=='poverty')
1046 if($secondsick=='poverty')
1052 echo "<div class=\"message\">Error in poverty, please contact the Admin</div>\n";
1054 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
1055 $userid = DB_get_userid('hash',$userhash);
1057 DB_set_player_by_gameid($gameid,$userid);
1058 DB_set_hand_status_by_hash($userhash,'poverty');
1060 $message = "Two people have poverty, it's your turn to decide, if you want to take the trump. Please visit:".
1061 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
1062 $subject = 'Double poverty (game '.DB_format_gameid($gameid).')';
1063 mymail($userid,$subject,$message);
1066 echo "<div class=\"message\"> Please, <a href=\"$INDEX?action=game&me=$me\">continue</a> here.</div>\n";
1074 /* both entries here, so that the tricks are visible for both.
1075 * in case of 'play' there is a break later that skips the last part
1078 /* check if all players are ready to play,
1079 * if so, send out email to the startplayer
1080 * only need to do this if the game hasn't started yet
1082 $gamestatus = DB_get_game_status_by_gameid($gameid);
1083 if($gamestatus == 'pre')
1086 $userids = DB_get_all_userid_by_gameid($gameid);
1087 foreach($userids as $user)
1089 $userstatus = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
1090 if($userstatus !='play' && $userstatus!='gameover')
1093 DB_set_player_by_gameid($gameid,$user);
1099 /* only set this after all poverty, etc. are handled*/
1100 DB_set_game_status_by_gameid($gameid,'play');
1102 /* email startplayer */
1103 $startplayer = DB_get_startplayer_by_gameid($gameid);
1104 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
1105 $who = DB_get_userid('hash',$hash);
1106 DB_set_player_by_gameid($gameid,$who);
1108 if($hash!=$me && DB_get_email_pref_by_hash($hash)!='emailaddict')
1110 /* email startplayer) */
1111 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
1112 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
1113 $subject = 'Ready, set, go... (game '.DB_format_gameid($gameid).')';
1114 mymail($who,$subject,$message);
1118 /* figure out what kind of game we are playing,
1119 * set the global variables $CARDS['trump'],$CARDS['diamonds'],$CARDS['hearts'],
1120 * $CARDS['clubs'],$CARDS['spades'],$CARDS['foxes']
1124 $gametype = DB_get_gametype_by_gameid($gameid);
1126 if($gametype=='solo')
1128 $gametype = DB_get_solo_by_gameid($gameid);
1129 if($gametype=='silent')
1132 $GT = $gametype.' '.$GT;
1135 $gametype = 'normal';
1137 set_gametype($gametype); /* this sets the $CARDS variable */
1139 /* get some infos about the game, need to reset this, since it might have changed */
1140 $gamestatus = DB_get_game_status_by_gameid($gameid);
1142 /* has the game started? No, then just wait here...*/
1143 if($gamestatus == 'pre')
1145 echo "<p class=\"message\"> You finished the setup, but not everyone else finished it... ".
1146 "You need to wait for the others. Just wait for an email. </p>";
1148 $mycards = DB_get_hand($me);
1149 $mycards = mysort($mycards,$gametype);
1151 echo "<div class=\"mycards\">Your cards are: <br />\n";
1152 foreach($mycards as $card)
1153 display_card($card,$PREF['cardset']);
1156 break; /* not sure this works... the idea is that you can
1157 * only play a card after everyone is ready to play */
1161 /* get time from the last action of the game */
1162 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1163 $gameend = time() - strtotime($r[0]);
1165 /* handle comments in case player didn't play a card, allow comments a week after the end of the game */
1166 if( (!myisset('card') && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
1167 if(myisset('comment'))
1169 $comment = $_REQUEST['comment'];
1170 $playid = DB_get_current_playid($gameid);
1173 DB_insert_comment($comment,$playid,$myid);
1176 /* get everything relevant to display the tricks */
1177 $result = DB_query("SELECT Hand_Card.card_id as card,".
1178 " Hand.position as position,".
1179 " Play.sequence as sequence, ".
1181 " GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
1182 " SEPARATOR '\n' ), ".
1183 " Play.create_date, ".
1186 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1187 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1188 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1189 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1190 "LEFT JOIN User On User.id=Comment.user_id ".
1191 "WHERE Trick.game_id='".$gameid."' ".
1192 "GROUP BY Trick.id, sequence ".
1193 "ORDER BY Trick.id, sequence ASC");
1195 $lasttrick = DB_get_max_trickid($gameid);
1197 $play = array(); /* needed to calculate winner later */
1199 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1200 $firstcard = ''; /* first card in a trick */
1202 echo "\n<ul class=\"tricks\">\n";
1203 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
1205 /* output vorbehalte */
1206 $mygametype = DB_get_gametype_by_gameid($gameid);
1207 if($mygametype != 'normal' && $mygametype != 'silent') /* only show when needed */
1209 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
1210 " <div class=\"trick\" id=\"trick0\">\n";
1212 /* get information so show the cards that have been handed over in a poverty game */
1213 output_exchanged_cards();
1215 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1219 while($r = DB_fetch_array($result))
1227 /* count number of tricks */
1231 /* check if first schweinchen has been played */
1232 if( $GAME['schweinchen-who'] && ($r[0] == 19 || $r[0] == 20) )
1233 if(!$GAME['schweinchen-first'])
1234 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1236 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1238 /* save card to be able to find the winner of the trick later */
1239 $play[$seq] = array('card'=>$r[0],'pos'=>$pos);
1243 /* first card in a trick, output some html */
1244 if($trick!=$lasttrick)
1246 /* start of an old trick? */
1247 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1248 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1249 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1251 else if($trick==$lasttrick)
1253 /* start of a last trick? */
1254 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1255 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1256 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1259 /* remember first card, so that we are able to check, what cards can be played */
1264 echo " <div class=\"card".($pos-1)."\">\n";
1266 /* display comments */
1268 echo " <span class=\"comment\">".$comment."</span>\n";
1271 display_card($r[0],$PREF['cardset']);
1273 echo " </div>\n"; /* end div card */
1278 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1282 /* whos turn is it? */
1285 $winner = get_winner($play,$gametype); /* returns the position */
1287 $firstcard = ''; /* new trick, no first card */
1292 if($next==5) $next = 1;
1295 /* my turn?, display cards as links, ask for comments*/
1296 if(DB_get_pos_by_hash($me) == $next)
1301 /* do we want to play a card? */
1302 if(myisset('card') && $myturn)
1304 $card = $_REQUEST['card'];
1305 $handid = DB_get_handid('hash',$me);
1306 $commentSchweinchen =''; /* used to add a comment when Schweinchen is being played */
1308 /* check if we have card and that we haven't played it yet*/
1309 /* set played in hand_card to true where hand_id and card_id*/
1310 $r = DB_query_array("SELECT id FROM Hand_Card WHERE played='false' and ".
1311 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1312 $handcardid = $r[0];
1314 if($handcardid) /* everything ok, play card */
1316 /* update Game timestamp */
1317 DB_update_game_timestamp($gameid);
1319 /* mark card as played */
1320 DB_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1321 DB_quote_smart($card));
1323 /* get trick id or start new trick */
1324 $a = DB_get_current_trickid($gameid);
1329 $playid = DB_play_card($trickid,$handcardid,$sequence);
1331 /* check special output for schweinchen in case in case a fox is being played
1332 * check for correct rules, etc. has already been done
1334 if( $GAME['schweinchen-who'] && ($card == 19 || $card == 20) )
1336 if(!$GAME['schweinchen-first'])
1337 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1339 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1341 if( $RULES['schweinchen']=='both' ||
1342 ($RULES['schweinchen']=='second' && $GAME['schweinchen-second']==1 )||
1343 ($RULES['schweinchen']=='secondaftercall' && $GAME['schweinchen-second']==1 &&
1344 (DB_get_call_by_hash($GAME['schweinchen-who']) || DB_get_partner_call_by_hash($GAME['schweinchen-who']) ))
1347 DB_insert_comment('Schweinchen! ',$playid,$myid);
1348 $commentSchweinchen = 'Schweinchen! ';
1351 echo 'schweinchen = '.$GAME['schweinchen-who'].' ---<br />';
1354 /* if sequence == 4 check who one in case of wedding */
1355 if($sequence == 4 && $GT == 'wedding')
1357 /* is wedding resolve */
1358 $resolved = DB_get_sickness_by_gameid($gameid);
1361 /* who has wedding */
1362 $userids = DB_get_all_userid_by_gameid($gameid);
1363 foreach($userids as $user)
1365 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1366 if($usersick == 'wedding')
1369 /* who won the trick */
1370 $play = DB_get_cards_by_trick($trickid);
1371 $winner = get_winner($play,$gametype); /* returns the position */
1372 $winnerid = DB_get_userid('gameid-position',$gameid,$winner);
1373 /* is tricknr <=3 */
1374 if($tricknr <=3 && $winnerid!=$whosick)
1376 /* set resolved at tricknr*/
1377 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1379 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1380 DB_set_party_by_hash($whash,'re');
1382 if($tricknr == 3 && $winnerid==$whosick)
1384 /* set resolved at tricknr*/
1385 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1390 /* if sequence == 4, set winner of the trick, count points and set the next player */
1393 $play = DB_get_cards_by_trick($trickid);
1394 $winner = get_winner($play,$gametype); /* returns the position */
1396 /* check if someone caught a fox */
1397 /* first check if we should account for solos at all,
1398 * since it doesn't make sense in some games
1400 $ok = 0; /* fox shouldn't be counted */
1401 if(DB_get_gametype_by_gameid($gameid)=='solo')
1403 $solo = DB_get_solo_by_gameid($gameid);
1404 if($solo == 'trump' || $solo == 'silent')
1405 $ok = 1; /* for trump solos and silent solos, foxes are ok */
1408 $ok = 1; /* for all other games (not solos) foxes are ok too */
1411 foreach($play as $played)
1413 if ( $played['card']==19 || $played['card']==20 )
1414 if ($played['pos']!= $winner )
1416 /* possible caught a fox, check party */
1417 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1418 $uid2 = DB_get_userid('gameid-position',$gameid,$played['pos']);
1420 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1421 $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1423 if($party1 != $party2)
1424 DB_query("INSERT INTO Score".
1425 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1429 /* check for karlchen (jack of clubs in the last trick)*/
1430 /* same as for foxes, karlchen doesn't always make sense
1431 * check what kind of game it is and set karlchen accordingly */
1432 $ok = 1; /* default: karlchen should be accounted for */
1434 $ok = 0; /* Karlchen works only in the last trick */
1435 if($ok && DB_get_gametype_by_gameid($gameid)=='solo' )
1437 $solo = DB_get_solo_by_gameid($gameid);
1438 if($solo == 'trumpless' || $solo == 'jack' || $solo == 'queen' )
1439 $ok = 0; /* no Karlchen in these solos */
1443 foreach($play as $played)
1444 if ( $played['card']==11 || $played['card']==12 )
1445 if ($played['pos'] == $winner )
1447 /* possible caught a fox, check party */
1448 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1449 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1451 DB_query("INSERT INTO Score".
1452 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1454 /* check for doppelopf (>40 points)*/
1456 foreach($play as $played)
1458 $points += DB_get_card_value_by_cardid($played['card']);
1462 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1463 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1465 DB_query("INSERT INTO Score".
1466 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
1470 DB_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1472 echo "ERROR during scoring";
1475 echo "DEBUG: position $winner won the trick <br />";
1477 /* who is the next player? */
1482 $next = DB_get_pos_by_hash($me)+1;
1484 if($next==5) $next=1;
1486 /* check for coment */
1487 if(myisset('comment'))
1489 $comment = $_REQUEST['comment'];
1491 DB_insert_comment($comment,$playid,$myid);
1492 if($commentSchweinchen)
1493 $comment = $commentSchweinchen . $comment;
1496 /* display played card */
1497 $pos = DB_get_pos_by_hash($me);
1500 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1501 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1502 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1505 echo " <div class=\"card".($pos-1)."\">\n ";
1507 /* display comments */
1508 display_card($card,$PREF['cardset']);
1510 echo "\n <span class=\"comment\"> ".$comment."</span>\n";
1513 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1515 /*check if we still have cards left, else set status to gameover */
1516 if(sizeof(DB_get_hand($me))==0)
1518 DB_set_hand_status_by_hash($me,'gameover');
1519 $mystatus = 'gameover';
1522 /* if all players are done, set game status to game over,
1523 * get the points of the last trick and send out an email
1526 $userids = DB_get_all_userid_by_gameid($gameid);
1529 foreach($userids as $user)
1530 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1534 DB_set_game_status_by_gameid($gameid,'gameover');
1536 /* email next player, if game is still running */
1537 if(DB_get_game_status_by_gameid($gameid)=='play')
1539 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1540 $who = DB_get_userid('hash',$next_hash);
1541 DB_set_player_by_gameid($gameid,$who);
1543 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1544 "It's your turn now.\n".
1545 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$next_hash."\n\n" ;
1546 if( DB_get_email_pref_by_uid($who)!='emailaddict' )
1548 $subject = 'A card has been played in game '.DB_format_gameid($gameid);
1549 mymail($who,$subject,$message);
1552 else /* send out final email */
1554 /* individual score */
1555 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1556 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1557 " LEFT JOIN User ON User.id=Hand.user_id".
1558 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1559 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1560 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1561 " WHERE Hand.game_id='$gameid'".
1562 " GROUP BY User.fullname" );
1563 $message = "The game is over. Thanks for playing :)\n";
1564 $message .= "Final score:\n";
1565 while( $r = DB_fetch_array($result) )
1566 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1568 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1569 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1570 " LEFT JOIN User ON User.id=Hand.user_id".
1571 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1572 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1573 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1574 " WHERE Hand.game_id='$gameid'".
1575 " GROUP BY Hand.party" );
1576 $message .= "\nTotals:\n";
1579 while( $r = DB_fetch_array($result) )
1581 $message .= " ".$r[0]." ".$r[1]."\n";
1584 else if($r[0] == 'contra')
1589 * save score in database
1593 /* get calls from re/contra */
1595 $call_contra = NULL;
1596 foreach($userids as $user)
1598 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1599 $call = DB_get_call_by_hash($hash);
1600 $party = DB_get_party_by_hash($hash);
1604 $call = (int) $call;
1610 else if( $call < $call_re)
1613 else if($party=='contra')
1615 if($call_contra==NULL)
1616 $call_contra = $call;
1617 else if( $call < $call_contra)
1618 $call_contra = $call;
1623 /* figure out who one */
1624 $winning_party = NULL;
1626 if($call_re == NULL && $call_contra==NULL)
1628 /* nobody made a call, so it's easy to figure out who won */
1630 $winning_party='re';
1632 $winning_party='contra';
1636 /* if one party makes a call, they only win, iff they make enough points
1637 * if only one party made a call, the other one wins,
1638 * if the first one didn't make it
1642 $offset = 120 - $call_re;
1644 $offset--; /* since we use a > in the next equation */
1646 if($re > 120+$offset)
1647 $winning_party='re';
1648 else if ($call_contra == NULL )
1649 $winning_party='contra';
1654 $offset = 120 - $call_contra;
1655 if($call_contra == 0)
1656 $offset--; /* since we use a > in the next equation */
1658 if($contra > 120+$offset)
1659 $winning_party='contra';
1660 else if ($call_re == NULL )
1661 $winning_party='re';
1665 /* one point for each call of the other party in case the other party didn't win
1666 * and one point each in case the party made more than points than one of the calls
1668 if($winning_party!='contra' && $call_contra!=NULL)
1670 for( $p=$call_contra;$p<=120; $p+=30 )
1672 DB_query("INSERT INTO Score".
1673 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
1676 for( $p=$call_contra; $p<120; $p+=30)
1679 DB_query("INSERT INTO Score".
1680 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'made$p')");
1683 if($winning_party!='re' and $call_re!=NULL)
1685 for( $p=$call_re;$p<=120; $p+=30 )
1687 DB_query("INSERT INTO Score".
1688 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'against$p')");
1691 for( $p=$call_re; $p<120; $p+=30)
1694 DB_query("INSERT INTO Score".
1695 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
1699 /* point in case contra won */
1700 if($winning_party=='contra')
1702 DB_query("INSERT INTO Score".
1703 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1706 /* one point each for winning and each 30 points + calls */
1707 if($winning_party=='re')
1709 foreach(array(120,150,180,210,240) as $p)
1712 if($p==240 || $call_contra!=NULL)
1716 DB_query("INSERT INTO Score".
1717 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
1719 /* re called something and won */
1720 foreach(array(0,30,60,90,120) as $p)
1722 if($call_re!=NULL && $call_re<$p+1)
1723 DB_query("INSERT INTO Score".
1724 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
1727 else if( $winning_party=='contra')
1729 foreach(array(120,150,180,210,240) as $p)
1732 if($p==240 || $call_re!=NULL)
1735 if($contra>$p-$offset)
1736 DB_query("INSERT INTO Score".
1737 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
1739 /* re called something and won */
1740 foreach(array(0,30,60,90,120) as $p)
1742 if($call_contra!=NULL && $call_contra<$p+1)
1743 DB_query("INSERT INTO Score".
1744 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
1749 /* add score points to email */
1752 $message .= " Points Re: \n";
1753 $queryresult = DB_query("SELECT score FROM Score ".
1754 " WHERE game_id=$gameid AND party='re'".
1756 while($r = DB_fetch_array($queryresult) )
1758 $message .= " ".$r[0]."\n";
1761 $message .= " Points Contra: \n";
1762 $queryresult = DB_query("SELECT score FROM Score ".
1763 " WHERE game_id=$gameid AND party='contra'".
1765 while($r = DB_fetch_array($queryresult) )
1767 $message .= " ".$r[0]."\n";
1770 $message .= " Total Points (from the Re point of view): $Tpoint\n";
1773 $session = DB_get_session_by_gameid($gameid);
1774 $score = generate_score_table($session);
1776 $message .= "Score Table:\n";
1777 $message .= format_score_table_ascii($score);
1778 $message .= "\nUse these links to have a look at game ".DB_format_gameid($gameid).": \n";
1780 /* send out final email */
1783 foreach($userids as $user)
1785 $all[] = DB_get_email('userid',$user);
1787 /* add links for all players */
1788 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1789 $name = DB_get_name('userid',$user);
1791 $link = "$name: ".$HOST.$INDEX."?action=game&me=".$hash."\n" ;
1794 $To = implode(",",$all);
1796 $message .= "\n\n (you can use reply all on this email to reach all the players.)\n";
1797 $subject = $EmailName.' Game over (game '.DB_format_gameid($gameid).') ';
1798 sendmail($To,$subject,$message);
1803 echo "can't find that card?! <br />\n";
1806 else if(myisset('card') && !$myturn )
1808 echo "please wait until it's your turn! <br />\n";
1811 if($seq!=4 && $trickNR>=1 && !(myisset('card') && $myturn) )
1812 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1814 /* display points in case game is over */
1815 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1817 echo " <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1818 " <div class=\"trick\" id=\"trick13\">\n";
1819 /* add pic for re/contra
1820 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1822 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1823 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1824 " LEFT JOIN User ON User.id=Hand.user_id".
1825 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1826 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1827 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1828 " WHERE Hand.game_id='$gameid'".
1829 " GROUP BY User.fullname" );
1830 while( $r = DB_fetch_array($result))
1831 echo " <div class=\"card".($r[3]-1)."\">\n".
1832 " <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
1835 /* display totals */
1836 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1837 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1838 " LEFT JOIN User ON User.id=Hand.user_id".
1839 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1840 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1841 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1842 " WHERE Hand.game_id='$gameid'".
1843 " GROUP BY Hand.party" );
1844 echo "<div class=\"total\">\n Totals:<br />\n";
1845 while( $r = DB_fetch_array($result))
1846 echo " ".$r[0]." ".$r[1]."<br />\n";
1848 $queryresult = DB_query("SELECT timediff(mod_date,create_date) ".
1849 " FROM Game WHERE id='$gameid'");
1850 $r = DB_fetch_array($queryresult);
1851 echo " <p>This game took ".$r[0]." hours.</p>\n";
1853 echo " <div class=\"re\">\n Points Re: <br />\n";
1854 $queryresult = DB_query("SELECT score FROM Score ".
1855 " WHERE game_id=$gameid AND party='re'".
1857 while($r = DB_fetch_array($queryresult) )
1858 echo " ".$r[0]."<br />\n";
1861 echo " <div class=\"contra\">\n Points Contra: <br />\n";
1862 $queryresult = DB_query("SELECT score FROM Score ".
1863 " WHERE game_id=$gameid AND party='contra'".
1865 while($r = DB_fetch_array($queryresult) )
1866 echo " ".$r[0]."<br />\n";
1871 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1874 echo " <li onclick=\"hl_prev();\" class=\"old\"><a href=\"#\">prev</a></li>\n";
1875 echo " <li onclick=\"hl_next();\" class=\"old\"><a href=\"#\">next</a></li>\n";
1876 echo "</ul>\n"; /* end ul tricks*/
1878 $mycards = DB_get_hand($me);
1879 $mycards = mysort($mycards,$gametype);
1880 echo "<div class=\"mycards\">\n";
1882 if($myturn && !myisset('card') && $mystatus=='play' )
1884 echo "Hello ".$myname.", it's your turn! <br />\n";
1885 echo "Your cards are: <br />\n";
1887 /* do we have to follow suite? */
1889 if(have_suit($mycards,$firstcard))
1892 foreach($mycards as $card)
1894 /* display only cards that the player is allowed to play as links, the rest just display normal
1895 * also check if we have both schweinchen, in that case only display on of them as playable
1897 if( ($followsuit && !same_type($card,$firstcard)) ||
1898 ( (int)($card)==19 && !$GAME['schweinchen-first'] &&
1899 ($RULES['schweinchen']=='second'||
1900 ( $RULES['schweinchen']=='secondaftercall' &&
1901 (DB_get_call_by_hash($GAME['schweinchen-who']) || DB_get_partner_call_by_hash($GAME['schweinchen-who']) )))
1902 && $GAME['schweinchen-who']==$me )
1904 display_card($card,$PREF['cardset']);
1906 display_link_card($card,$PREF['cardset']);
1909 else if($mystatus=='play' )
1911 echo "Your cards are: <br />\n";
1912 foreach($mycards as $card)
1913 display_card($card,$PREF['cardset']);
1915 else if($mystatus=='gameover')
1917 $oldcards = DB_get_all_hand($me);
1918 $oldcards = mysort($oldcards,$gametype);
1919 echo "Your cards were: <br />\n";
1920 foreach($oldcards as $card)
1921 display_card($card,$PREF['cardset']);
1923 $userids = DB_get_all_userid_by_gameid($gameid);
1924 foreach($userids as $user)
1926 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1932 $name = DB_get_name('userid',$user);
1933 $oldcards = DB_get_all_hand($userhash);
1934 $oldcards = mysort($oldcards,$gametype);
1935 echo "$name's cards were: <br />\n";
1936 foreach($oldcards as $card)
1937 display_card($card,$PREF['cardset']);
1943 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1944 if($mystatus=='play')
1947 /* the following happens only when the gamestatus is 'gameover' */
1948 /* check if game is over, display results */
1949 if(DB_get_game_status_by_gameid($gameid)=='play')
1951 echo "The game is over for you.. other people still need to play though";
1955 myerror("error in testing the status");
1958 /* output other games where it is the users turn
1959 * make sure that the people looking at old games don't see the wrong games here
1961 if( $mystatus != 'gameover' )
1962 display_user_menu($myid);
1963 else if( $mystatus == 'gameover' &&
1964 isset($_SESSION['id']) )
1966 display_user_menu($_SESSION['id']);
1970 echo "<div class=\"usermenu\">\n";
1971 echo "It's your turn in these games:<br />\n";
1972 echo "Please log in to see this information.\n";
1976 /* display rule set for this game */
1977 echo "<div class=\"gameinfo\">\n";
1979 if($gamestatus == 'play' )
1981 $myparty = DB_get_party_by_hash($me);
1982 output_form_calls($me,$myparty);
1984 /* get time from the last action of the game */
1985 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1986 $gameend = time() - strtotime($r[0]);
1988 if($gamestatus == 'play' || $gameend < 60*60*24*7)
1990 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1993 echo "<input type=\"submit\" value=\"submit\" />\n";
1995 /* has this hand been played by others? */
1996 $other_game_ids = DB_played_by_others($gameid);
1997 if(sizeof($other_game_ids)>0 && $mystatus=='gameover')
1999 $mypos = DB_get_pos_by_hash($me);
2000 echo "<p>See how other played the same hand: <br />\n";
2001 foreach($other_game_ids as $id)
2003 $otherhash = DB_get_hash_from_game_and_pos($id,$mypos);
2004 $othername = DB_get_name('hash',$otherhash);
2005 echo "<a href=\"$INDEX?action=game&me=$otherhash\">$othername</a><br />";
2014 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' && isset($_SESSION['id']) && $_SESSION['id']==$myid)
2016 $session = DB_get_session_by_gameid($gameid);
2017 $result = DB_query("SELECT id,create_date FROM Game".
2018 " WHERE session=$session".
2019 " ORDER BY create_date DESC".
2023 $r = DB_fetch_array($result);
2025 if(!$session || $gameid==$r[0])
2027 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
2028 $names = DB_get_all_names_by_gameid($gameid);
2029 $type = DB_get_gametype_by_gameid($gameid);
2033 $solo = DB_get_solo_by_gameid($gameid);
2035 if($solo!='silent') /* repeat game with same first player */
2036 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
2037 else /* rotate normally */
2038 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
2040 else /* rotate normally */
2041 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);