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\">Score: \n";
219 foreach($finalscore as $user=>$value)
221 $name = DB_get_name('userid',$user);
222 echo " ".substr($name,0,2).": $value ";
224 echo format_score_table_html($score,$myid);
227 /* figure out which game in a session we are in and link to the previous and next game if possible */
228 $hashes = DB_get_hashes_by_session($session,$myid);
231 foreach($hashes as $hash)
241 $previous = $hashes[$j-2];
249 if(isset($_SESSION['id']) && $_SESSION['id']==$myid)
252 echo "<a href=\"{$INDEX}?action=game&me=$previous\">previous game</a> \n";
253 echo "This is game number $j of <a href=\"{$INDEX}?action=game&me=$lasthash\">$i</a> in session $session.\n";
255 echo " <a href=\"{$INDEX}?action=game&me=$next\">next game</a> \n";
258 echo "This is game number $j of $i in session $session.";
262 /* display the table and the names */
265 /* mystatus gets the player through the different stages of a game.
266 * start: does the player want to play?
267 * init: check for sickness
268 * check: check for return values from init
269 * poverty: handle poverty, wait here until all player have reached this state
270 * display sickness and move on to game
271 * play: game in progress
272 * gameover: are we revisiting a game
277 /* don't ask if user has autosetup set to yest */
279 if($PREF['autosetup']=='yes') $skip = 1;
281 if( !myisset('in') && !$skip)
283 /* asks the player, if he wants to join the game */
284 output_check_want_to_play($me);
289 /* check the result, if player wants to join, got next stage, else cancel game */
290 if(!$skip && $_REQUEST['in'] == 'no' )
292 /* cancel the game */
293 $message = "Hello, \n\n".
294 "the game has been canceled due to the request of one of the players.\n";
296 $userids = DB_get_all_userid_by_gameid($gameid);
297 foreach($userids as $user)
299 $To = DB_get_email('userid',$user);
300 mymail($To,"$EmailName game ".DB_format_gameid($gameid)." canceled",$message);
303 /* delete everything from the dB */
309 /* user wants to join the game */
311 /* move on to the next stage,
312 * no break statement to immediately go to the next stage
315 DB_set_hand_status_by_hash($me,'init');
317 /* check if everyone has reached this stage, send out email */
318 $userids = DB_get_all_userid_by_gameid($gameid);
320 foreach($userids as $user)
322 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
323 if($userstat!='init')
325 /* whos turn is it? */
326 DB_set_player_by_gameid($gameid,$user);
333 /* all done, send out email unless this player is the startplayer */
334 $startplayer = DB_get_startplayer_by_gameid($gameid);
335 if($mypos == $startplayer)
337 /* do nothing, go to next stage */
341 /* email startplayer */
343 $email = DB_get_email('position-gameid',$startplayer,$gameid);
344 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
345 $who = DB_get_userid('email',$email);
346 DB_set_player_by_gameid($gameid,$who);
348 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
349 "Use this link to go the game: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
350 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
357 /* here we ask the player if he is sick */
358 $mycards = DB_get_hand($me);
359 $mycards = mysort($mycards,$gametype);
361 if(!myisset('solo','wedding','poverty','nines') )
363 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
364 echo "\n<ul class=\"tricks\">\n";
365 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
366 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
367 " <div class=\"trick\" id=\"trick0\">\n";
369 for($pos=1;$pos<5;$pos++)
371 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
372 $userid = DB_get_userid('gameid-position',$gameid,$pos);
373 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
375 if($userstatus=='start' || $userstatus=='init')
376 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs <br/ > to decide </div>\n"; /* show this to everyone */
378 if($usersick!=NULL && $pos<=$mypos ) /* only show this for people sitting before the player */
379 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
380 else if($usersick==NULL && $pos<=$mypos)
381 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
383 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
384 /* end displaying sickness */
386 output_check_for_sickness($me,$mycards);
388 echo "<div class=\"mycards\">Your cards are: <br />\n";
389 foreach($mycards as $card)
390 display_card($card,$PREF['cardset']);
397 /* check if someone selected more than one sickness */
399 if($_REQUEST['solo']!='No') $Nsickness++;
400 if($_REQUEST['wedding'] == 'yes') $Nsickness++;
401 if($_REQUEST['poverty'] == 'yes') $Nsickness++;
402 if($_REQUEST['nines'] == 'yes') $Nsickness++;
406 echo "<p class=\"message\"> You selected more than one sickness, please go back ".
407 "and answer the <a href=\"$INDEX?action=game&me=$me&in=yes\">question</a> again.</p>";
409 echo "<div class=\"mycards\">Your cards are: <br />\n";
410 foreach($mycards as $card)
411 display_card($card,$PREF['cardset']);
418 /* everything is ok, save what user said and proceed */
419 echo "<p class=\"message\">Processing what you selected in the last step...";
421 /* check if this sickness needs to be handled first */
422 $gametype = DB_get_gametype_by_gameid($gameid);
423 $startplayer = DB_get_startplayer_by_gameid($gameid); /* need this to check which solo goes first */
425 if( $_REQUEST['solo']!='No' )
427 /* user wants to play a solo */
429 /* store the info in the user's hand info */
430 DB_set_solo_by_hash($me,$_REQUEST['solo']);
431 DB_set_sickness_by_hash($me,'solo');
433 echo "<br />Seems like you want to play a {$_REQUEST['solo']} solo. Got it.<br />\n";
435 if($gametype == 'solo' && $startplayer<$mypos)
436 {}/* do nothing, since someone else already is playing solo */
439 /* this solo comes first
440 * store info in game table
442 DB_set_gametype_by_gameid($gameid,'solo');
443 DB_set_startplayer_by_gameid($gameid,$mypos);
444 DB_set_solo_by_gameid($gameid,$_REQUEST['solo']);
447 else if($_REQUEST['wedding'] == 'yes')
449 /* silent solo is set further down */
450 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
451 DB_set_sickness_by_hash($me,'wedding');
453 else if($_REQUEST['poverty'] == 'yes')
455 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
456 DB_set_sickness_by_hash($me,'poverty');
458 else if($_REQUEST['nines'] == 'yes')
460 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
461 " is playing solo, this game will be canceled.<br />\n";
462 DB_set_sickness_by_hash($me,'nines');
467 /* move on to the next stage*/
468 DB_set_hand_status_by_hash($me,'check');
473 /* here we check what all players said and figure out what game we are playing
474 * this can therefore only be handled once all players finished the last stage
477 $mycards = DB_get_hand($me);
478 $mycards = mysort($mycards,$gametype);
480 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
481 echo "\n<ul class=\"tricks\">\n";
482 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
483 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
484 " <div class=\"trick\" id=\"trick0\">\n";
486 for($pos=1;$pos<5;$pos++)
488 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
489 $userid = DB_get_userid('gameid-position',$gameid,$pos);
490 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
492 if($userstatus=='start' || $userstatus=='init')
493 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs <br />to decide </div>\n"; /* show this to everyone */
495 if($usersick!=NULL) /* in the init-phase we only showed players with $pos<$mypos, now we can show all */
496 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
498 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
500 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
501 /* end displaying sickness */
503 echo "<div class=\"message\">\n";
504 echo "<p> Checking if someone else selected solo, nines, wedding or poverty.</p>";
506 /* check if everyone has reached this stage */
507 $userids = DB_get_all_userid_by_gameid($gameid);
509 foreach($userids as $user)
511 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
512 if($userstat!='check')
515 DB_set_player_by_gameid($gameid,$user);
522 echo "<p>This step can only be handled after everyone finished the last step. ".
523 "Seems like this is not the case, so you need to wait a bit... ".
524 "you will get an email once that is the case, please use the link in ".
525 "that email to continue the game.</p></div>";
527 /* display cards, if player was just at the init-phase he will still see the cards from there
528 * we can put this one here, since the last player to finish the init state won't get here and
529 * will still see his card anyway from the init-phase
531 if($mystatus=='check')
534 echo "<div class=\"mycards\">Your cards are: <br />\n";
535 foreach($mycards as $card)
536 display_card($card,$PREF['cardset']);
543 /* Ok, everyone finished the init-phase, time to figure out what game we
544 * are playing, in case there are any solos this already
545 * will have the correct information in it */
547 echo "<p> Ok, everyone is done... figuring out what kind of game we are playing.</p>";
549 $gametype = DB_get_gametype_by_gameid($gameid);
550 $startplayer = DB_get_startplayer_by_gameid($gameid);
552 /* check for sickness */
557 foreach($userids as $user)
559 $name = DB_get_name('userid',$user);
560 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
561 if($usersick == 'nines')
564 break; /* no need to check for other poverties, since only solo can win and that is already set */
566 else if($usersick == 'poverty')
568 else if($usersick == 'wedding')
570 else if($usersick == 'solo')
574 /* now check which sickness comes first and set the gametype to it */
575 if($gametype == 'solo')
582 /* TODO: should we keep statistics of this? */
583 $message = "Hello, \n\n".
584 " the game has been canceled because ".DB_get_name('userid',$nines).
585 " has five or more nines and nobody is playing solo.\n\n".
586 " To redeal either start a new game or, in case the game was part of a tournament, \n".
587 " go to the last game and use the link at the bottom of the page to redeal.";
589 $userids = DB_get_all_userid_by_gameid($gameid);
590 foreach($userids as $user)
592 $To = DB_get_email('userid',$user);
593 mymail($To,"$EmailName game ".DB_format_gameid($gameid)." canceled",$message);
596 /* delete everything from the dB */
599 echo "The game has been canceled because ".DB_get_name('userid',$nines).
600 " has five or more nines and nobody is playing solo.\n";
603 else if($poverty==1) /* one person has poverty */
605 DB_set_gametype_by_gameid($gameid,'poverty');
606 $gametype = 'poverty';
607 $who = DB_get_sickness_by_gameid($gameid);
610 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
611 if($firstsick == 'poverty')
612 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
614 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
617 else if($poverty==2) /* two people have poverty */
619 DB_set_gametype_by_gameid($gameid,'dpoverty');
620 $gametype = 'dpoverty';
621 $who = DB_get_sickness_by_gameid($gameid);
624 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
625 if($firstsick == 'poverty')
627 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
628 if($secondsick == 'poverty')
629 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
631 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
634 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
639 DB_set_gametype_by_gameid($gameid,'wedding');
640 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
641 $gametype = 'wedding';
643 /* now the gametype is set correctly in the database */
644 echo "<p> Got it :)</p>";
646 /* loop over all players, set re/contra if possible and start the game if possible */
647 $userids = DB_get_all_userid_by_gameid($gameid);
648 foreach($userids as $userid)
650 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
655 /* are we the solo player? set us to re, else set us to contra */
656 $pos = DB_get_pos_by_hash($userhash);
657 if($pos == $startplayer)
658 DB_set_party_by_hash($userhash,'re');
660 DB_set_party_by_hash($userhash,'contra');
661 DB_set_hand_status_by_hash($userhash,'play');
665 /* set person with the wedding to re, do the rest during the game */
666 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
667 if($usersick == 'wedding')
668 DB_set_party_by_hash($userhash,'re');
670 DB_set_party_by_hash($userhash,'contra');
672 DB_set_hand_status_by_hash($userhash,'play');
676 $hand = DB_get_all_hand($userhash);
678 if(in_array('3',$hand)||in_array('4',$hand))
679 DB_set_party_by_hash($userhash,'re');
681 DB_set_party_by_hash($userhash,'contra');
682 DB_set_hand_status_by_hash($userhash,'play');
686 /* set person with poverty to play status */
687 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
688 if($usersick == 'poverty')
689 DB_set_hand_status_by_hash($userhash,'play');
691 /* set status of first player to be asked to poverty */
692 $who = DB_get_sickness_by_gameid($gameid);
693 if($who > 6) $who= $who/10; /* in case we have dpoverty */
694 $whoid = DB_get_userid('gameid-position',$gameid,$who);
696 DB_set_hand_status_by_hash($userhash,'poverty');
699 /* check for silent solo, set game type to solo in this case */
700 $gametype = DB_get_gametype_by_gameid($gameid);
701 $userids = DB_get_all_userid_by_gameid($gameid);
702 foreach($userids as $userid)
704 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
706 if($gametype=='normal')
708 $userhand = DB_get_all_hand($userhash);
709 if(check_wedding($userhand))
711 /* normal game type and player has both queens -> silent solo */
712 /* keep startplayer, just set gametype to silent solo */
713 DB_set_gametype_by_gameid($gameid,'solo');
714 DB_set_solo_by_gameid($gameid,'silent');
719 /* send out email to first player or poverty person*/
720 if($gametype!='poverty' && $gametype!='dpoverty')
722 $startplayer = DB_get_startplayer_by_gameid($gameid);
723 $email = DB_get_email('position-gameid',$startplayer,$gameid);
724 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
725 $who = DB_get_userid('email',$email);
726 DB_set_player_by_gameid($gameid,$who);
730 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
732 /* email startplayer */
733 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
734 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
735 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
739 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
743 /* set status of first player to be asked to poverty */
744 $who = DB_get_sickness_by_gameid($gameid);
745 if($who > 6) $who= $who/10; /* in case we have dpoverty */
747 $whoid = DB_get_userid('gameid-position',$gameid,$who);
749 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
752 $email = DB_get_email('position-gameid',$who,$gameid);
753 $whohash = DB_get_hash_from_game_and_pos($gameid,$who);
754 DB_set_player_by_gameid($gameid,$whoid);
756 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
758 /* email player for poverty */
759 $message = "Poverty: It's your turn now in game ".DB_format_gameid($gameid).".\n".
760 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$whohash."\n\n" ;
761 mymail($email,$EmailName."Poverty (game ".DB_format_gameid($gameid).") ",$message);
769 /* user only gets here in a poverty game, several things have to be handled here:
770 * A) ask, if user wants to take trump
772 * poverty: set re/contra
773 * dpoverty: first time: set re, send email to second player
774 * second time: set contra
775 * poverty: set status of other players to 'play'
776 * set status to play in case 0 trump
777 * no -> set status to play,
778 * ask next player or cancle the game if no more players
779 * B) user took trump and has too many cards (e.g. count(cards)>12 and re/contra set)
780 * ask to give cards back, set status to play, once player has 12 cards
782 * it is easier to check B) first
785 /* output pre game in case user reloads */
788 set_gametype($gametype); /* this sets the $CARDS variable */
789 $myparty = DB_get_party_by_hash($me);
791 /* the following is part B) of whats needs to be done)
792 /* check if user wants to give cards back */
793 if(myisset('exchange'))
795 $exchange = $_REQUEST['exchange'];
796 $partnerhash = DB_get_partner_hash_by_hash($me);
797 $partnerid = DB_get_userid('hash',$partnerhash);
798 $partnerhand = DB_get_handid('gameid-userid',$gameid,$partnerid);
800 /* if exchange is set to a value>0, exchange that card back to the partner */
803 $result = DB_query("UPDATE Hand_Card SET hand_id='$partnerhand'".
804 " WHERE hand_id='$myhand' AND card_id=".DB_quote_smart($exchange));
805 DB_add_exchanged_card(DB_quote_smart($exchange),$myhand,$partnerhand);
810 $mycards = DB_get_hand($me);
811 $mycards = mysort($mycards,$gametype);
813 /* check if user need to give more cards back */
814 if( ($myparty=='re' || $myparty=='contra') && count($mycards)>12)
816 echo "<div class=\"poverty\"> you need to get rid of a few cards</div>\n";
819 echo "<div class=\"mycards\">Your cards are: <br />\n";
820 foreach($mycards as $card)
821 display_link_card($card,$PREF['cardset'],$type);
822 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select card to give back\" />\n";
825 else if( ($myparty=='re' || $myparty=='contra') && count($mycards)==12)
827 /* user is done, ready to play */
828 DB_set_hand_status_by_hash($me,'play');
830 /* email start player */
831 $startplayer = DB_get_startplayer_by_gameid($gameid);
832 $email = DB_get_email('position-gameid',$startplayer,$gameid);
833 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
834 $who = DB_get_userid('email',$email);
835 DB_set_player_by_gameid($gameid,$who);
839 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
841 /* email startplayer */
842 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
843 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
844 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
848 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
851 /* the following is part A) of what needs to be done */
852 if(!myisset('trump'))
856 echo "<div class=\"poverty\">\n";
857 $userids = DB_get_all_userid_by_gameid($gameid);
858 foreach($userids as $user)
860 $name = DB_get_name('userid',$user);
861 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
862 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
863 $userparty = DB_get_party_by_hash($userhash);
865 if($usersick=='poverty' && !$userparty)
867 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
868 $cards = DB_get_hand($hash);
871 foreach($cards as $card)
872 if($card<27) $nrtrump++;
873 echo "Player $name has $nrtrump trump. Do you want to take them?".
874 "<a href=\"index.php?action=game&me=$me&trump=$user\">Yes</a> <br />\n";
877 echo "<a href=\"index.php?action=game&me=$me&trump=no\">No way</a> <br />\n";
878 echo "</div><div>\n";
880 echo "<div class=\"mycards\">Your cards are: <br />\n";
881 foreach($mycards as $card)
882 display_card($card,$PREF['cardset']);
883 echo "</div></div>\n";
889 $trump = $_REQUEST['trump'];
893 /* user doesn't want to take trump */
894 DB_set_hand_status_by_hash($me,'play');
896 /* set next player who needs to be asked and email him*/
897 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
898 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
900 /* don't ask people who have poverty */
902 if($firstsick=='poverty')
904 if($secondsick=='poverty')
909 if($gametype=='dpoverty')
911 $next=999; /* need to cancel for sure, since both would need to take the trump */
914 /* no more people to ask, need to cancel the game */
917 $message = "Hello, \n\n".
918 "Game ".DB_format_gameid($gameid)." has been canceled since nobody wanted to take the trump.\n";
920 $userids = DB_get_all_userid_by_gameid($gameid);
921 foreach($userids as $user)
923 $To = DB_get_email('userid',$user);
924 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled (poverty not resolved)",$message);
927 /* delete everything from the dB */
930 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been canceled.<br /><br /></p>";
935 /* email next player, set his status to poverty */
936 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
937 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
938 $userid = DB_get_userid('email',$To);
940 DB_set_player_by_gameid($gameid,$userid);
941 DB_set_hand_status_by_hash($userhash,'poverty');
943 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
944 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
945 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
950 /* player wants to take trump, change cards */
952 /* user wants to take trump */
953 $trump = $_REQUEST['trump'];
954 $userhand = DB_get_handid('gameid-userid',$gameid,$trump);
955 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
957 /* remember which cards were handed over*/
958 $partnerhand = DB_get_all_hand($userhash);
959 foreach ($partnerhand as $card)
961 DB_add_exchanged_card($card,$userhand,$myhand);
963 /* copy trump from player A to B */
964 $result = DB_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
967 $mycards = DB_get_hand($me);
970 if($gametype=='poverty')
972 $userids = DB_get_all_userid_by_gameid($gameid);
973 foreach($userids as $user)
975 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
976 if($hash==$userhash||$hash==$me)
978 DB_set_party_by_hash($hash,'re');
982 DB_set_party_by_hash($hash,'contra');
983 DB_set_hand_status_by_hash($hash,'play'); /* the contra party is ready to play */
986 /* check if we are done (in case of no trump handed over), if so, go to 'play' phase right away*/
987 if(count($mycards)==12)
989 DB_set_hand_status_by_hash($me,'play');
994 /* has the re party already been set?*/
996 $userids = DB_get_all_userid_by_gameid($gameid);
997 foreach($userids as $user)
999 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1000 $party = DB_get_party_by_hash($hash);
1006 DB_set_party_by_hash($me,'contra');
1007 DB_set_party_by_hash($userhash,'contra');
1011 DB_set_party_by_hash($me,'re');
1012 DB_set_party_by_hash($userhash,'re');
1014 /* send out email to second non-poverty player */
1015 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
1016 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
1019 if($firstsick=='poverty')
1020 if($secondsick=='poverty')
1026 echo "<div class=\"message\">Error in poverty, please contact the Admin</div>\n";
1028 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
1029 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
1030 $userid = DB_get_userid('email',$To);
1032 DB_set_player_by_gameid($gameid,$userid);
1033 DB_set_hand_status_by_hash($userhash,'poverty');
1035 $message = "Two people have poverty, it's your turn to decide, if you want to take the trump. Please visit:".
1036 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
1037 mymail($To,$EmailName." double poverty (game ".DB_format_gameid($gameid).")",$message);
1042 echo "<div class=\"message\"> Please, <a href=\"$INDEX?action=game&me=$me\">continue</a> here.</div>\n";
1050 /* both entries here, so that the tricks are visible for both.
1051 * in case of 'play' there is a break later that skips the last part
1054 /* check if all players are ready to play,
1055 * if so, send out email to the startplayer
1056 * only need to do this if the game hasn't started yet
1058 $gamestatus = DB_get_game_status_by_gameid($gameid);
1059 if($gamestatus == 'pre')
1062 $userids = DB_get_all_userid_by_gameid($gameid);
1063 foreach($userids as $user)
1065 $userstatus = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
1066 if($userstatus !='play' && $userstatus!='gameover')
1069 DB_set_player_by_gameid($gameid,$user);
1075 /* only set this after all poverty, etc. are handled*/
1076 DB_set_game_status_by_gameid($gameid,'play');
1078 /* email startplayer */
1079 $startplayer = DB_get_startplayer_by_gameid($gameid);
1080 $email = DB_get_email('position-gameid',$startplayer,$gameid);
1081 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
1082 $who = DB_get_userid('email',$email);
1083 DB_set_player_by_gameid($gameid,$who);
1085 if($hash!=$me && DB_get_email_pref_by_hash($hash)!='emailaddict')
1087 /* email startplayer) */
1088 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
1089 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
1090 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
1094 /* figure out what kind of game we are playing,
1095 * set the global variables $CARDS['trump'],$CARDS['diamonds'],$CARDS['hearts'],
1096 * $CARDS['clubs'],$CARDS['spades'],$CARDS['foxes']
1100 $gametype = DB_get_gametype_by_gameid($gameid);
1102 if($gametype=='solo')
1104 $gametype = DB_get_solo_by_gameid($gameid);
1105 if($gametype=='silent')
1108 $GT = $gametype.' '.$GT;
1111 $gametype = 'normal';
1113 set_gametype($gametype); /* this sets the $CARDS variable */
1115 /* get some infos about the game, need to reset this, since it might have changed */
1116 $gamestatus = DB_get_game_status_by_gameid($gameid);
1118 /* has the game started? No, then just wait here...*/
1119 if($gamestatus == 'pre')
1121 echo "<p class=\"message\"> You finished the setup, but not everyone else finished it... ".
1122 "You need to wait for the others. Just wait for an email. </p>";
1124 $mycards = DB_get_hand($me);
1125 $mycards = mysort($mycards,$gametype);
1127 echo "<div class=\"mycards\">Your cards are: <br />\n";
1128 foreach($mycards as $card)
1129 display_card($card,$PREF['cardset']);
1132 break; /* not sure this works... the idea is that you can
1133 * only play a card after everyone is ready to play */
1137 /* get time from the last action of the game */
1138 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1139 $gameend = time() - strtotime($r[0]);
1141 /* handle comments in case player didn't play a card, allow comments a week after the end of the game */
1142 if( (!myisset('card') && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
1143 if(myisset('comment'))
1145 $comment = $_REQUEST['comment'];
1146 $playid = DB_get_current_playid($gameid);
1149 DB_insert_comment($comment,$playid,$myid);
1152 /* get everything relevant to display the tricks */
1153 $result = DB_query("SELECT Hand_Card.card_id as card,".
1154 " Hand.position as position,".
1155 " Play.sequence as sequence, ".
1157 " GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
1158 " SEPARATOR '\n' ), ".
1159 " Play.create_date, ".
1162 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1163 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1164 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1165 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1166 "LEFT JOIN User On User.id=Comment.user_id ".
1167 "WHERE Trick.game_id='".$gameid."' ".
1168 "GROUP BY Trick.id, sequence ".
1169 "ORDER BY Trick.id, sequence ASC");
1171 $lasttrick = DB_get_max_trickid($gameid);
1173 $play = array(); /* needed to calculate winner later */
1175 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1176 $firstcard = ''; /* first card in a trick */
1178 echo "\n<ul class=\"tricks\">\n";
1179 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
1181 /* output vorbehalte */
1182 $mygametype = DB_get_gametype_by_gameid($gameid);
1183 if($mygametype != 'normal' && $mygametype != 'silent') /* only show when needed */
1185 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
1186 " <div class=\"trick\" id=\"trick0\">\n";
1187 /* get information so show the cards that have been handed over in a poverty game */
1192 if($mygametype == 'poverty' || $mygametype=='dpoverty')
1194 /* who has poverty */
1195 for($mypos=1;$mypos<5;$mypos++)
1197 $usersick = DB_get_sickness_by_pos_and_gameid($mypos,$gameid);
1198 if($usersick == 'poverty')
1200 $povertypos2 = $mypos;
1202 $povertypos1 = $mypos;
1204 /* get hash and cards for all */
1205 $povertyhash1 = DB_get_hash_from_game_and_pos($gameid,$povertypos1);
1206 $partnerhash1 = DB_get_partner_hash_by_hash($povertyhash1);
1208 $povertycards1 = DB_get_exchanged_cards($povertyhash1);
1209 $partnercards1 = DB_get_exchanged_cards($partnerhash1);
1211 $partnerpos1 = DB_get_pos_by_hash($partnerhash1);
1214 $povertyhash2 = DB_get_hash_from_game_and_pos($gameid,$povertypos2);
1215 $partnerhash2 = DB_get_partner_hash_by_hash($povertyhash2);
1217 $povertycards2 = DB_get_exchanged_cards($povertyhash2);
1218 $partnercards2 = DB_get_exchanged_cards($partnerhash2);
1220 $partnerpos2 = DB_get_pos_by_hash($partnerhash2);
1225 for($mypos=1;$mypos<5;$mypos++)
1227 $usersick = DB_get_sickness_by_pos_and_gameid($mypos,$gameid);
1228 if($usersick!=NULL ||
1229 $mypos==$povertypos1 || $mypos==$partnerpos1 ||
1230 $mypos==$povertypos2 || $mypos==$partnerpos2 )
1232 echo " <div class=\"vorbehalt".($mypos-1)."\"> Vorbehalt <br />";
1234 echo " $usersick <br />";
1235 if($mypos==$partnerpos1)
1237 foreach($partnercards1 as $card)
1238 if($povertyhash1 == $me || $partnerhash1 == $me || $mystatus=='gameover')
1239 display_card($card,$PREF['cardset']);
1241 display_card(0,$PREF['cardset']);
1243 else if($mypos==$povertypos1)
1245 foreach($povertycards1 as $card)
1246 if($povertyhash1 == $me || $partnerhash1 == $me || $mystatus=='gameover')
1247 display_card($card,$PREF['cardset']);
1249 display_card(0,$PREF['cardset']);
1251 else if($mypos==$povertypos2)
1253 foreach($povertycards2 as $card)
1254 if($povertyhash2 == $me || $partnerhash2 == $me || $mystatus=='gameover')
1255 display_card($card,$PREF['cardset']);
1257 display_card(0,$PREF['cardset']);
1259 else if($mypos==$partnerpos2)
1261 foreach($partnercards2 as $card)
1262 if($povertyhash2 == $me || $partnerhash2 == $me || $mystatus=='gameover')
1263 display_card($card,$PREF['cardset']);
1265 display_card(0,$PREF['cardset']);
1270 if($mygametype == $usersick)
1274 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1278 while($r = DB_fetch_array($result))
1286 /* count number of tricks */
1290 /* check if first schweinchen has been played */
1291 if( $GAME['schweinchen-who'] && ($r[0] == 19 || $r[0] == 20) )
1292 if(!$GAME['schweinchen-first'])
1293 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1295 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1297 /* save card to be able to find the winner of the trick later */
1298 $play[$seq] = array('card'=>$r[0],'pos'=>$pos);
1302 /* first card in a trick, output some html */
1303 if($trick!=$lasttrick)
1305 /* start of an old trick? */
1306 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1307 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1308 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1310 else if($trick==$lasttrick)
1312 /* start of a last trick? */
1313 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1314 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1315 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1318 /* remember first card, so that we are able to check, what cards can be played */
1323 echo " <div class=\"card".($pos-1)."\">\n";
1325 /* display comments */
1327 echo " <span class=\"comment\">".$comment."</span>\n";
1330 display_card($r[0],$PREF['cardset']);
1332 echo " </div>\n"; /* end div card */
1337 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1341 /* whos turn is it? */
1344 $winner = get_winner($play,$gametype); /* returns the position */
1346 $firstcard = ''; /* new trick, no first card */
1351 if($next==5) $next = 1;
1354 /* my turn?, display cards as links, ask for comments*/
1355 if(DB_get_pos_by_hash($me) == $next)
1360 /* do we want to play a card? */
1361 if(myisset('card') && $myturn)
1363 $card = $_REQUEST['card'];
1364 $handid = DB_get_handid('hash',$me);
1365 $commentSchweinchen =''; /* used to add a comment when Schweinchen is being played */
1367 /* check if we have card and that we haven't played it yet*/
1368 /* set played in hand_card to true where hand_id and card_id*/
1369 $r = DB_query_array("SELECT id FROM Hand_Card WHERE played='false' and ".
1370 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1371 $handcardid = $r[0];
1373 if($handcardid) /* everything ok, play card */
1375 /* update Game timestamp */
1376 DB_update_game_timestamp($gameid);
1378 /* mark card as played */
1379 DB_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1380 DB_quote_smart($card));
1382 /* get trick id or start new trick */
1383 $a = DB_get_current_trickid($gameid);
1388 $playid = DB_play_card($trickid,$handcardid,$sequence);
1390 /* check special output for schweinchen in case in case a fox is being played
1391 * check for correct rules, etc. has already been done
1393 if( $GAME['schweinchen-who'] && ($card == 19 || $card == 20) )
1395 if(!$GAME['schweinchen-first'])
1396 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1398 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1400 if( $RULES['schweinchen']=='both' ||
1401 ($RULES['schweinchen']=='second' && $GAME['schweinchen-second']==1 )||
1402 ($RULES['schweinchen']=='secondaftercall' && $GAME['schweinchen-second']==1 &&
1403 (DB_get_call_by_hash($GAME['schweinchen-who']) || DB_get_partner_call_by_hash($GAME['schweinchen-who']) ))
1406 DB_insert_comment('Schweinchen! ',$playid,$myid);
1407 $commentSchweinchen = 'Schweinchen! ';
1410 echo 'schweinchen = '.$GAME['schweinchen-who'].' ---<br />';
1413 /* if sequence == 4 check who one in case of wedding */
1414 if($sequence == 4 && $GT == 'wedding')
1416 /* is wedding resolve */
1417 $resolved = DB_get_sickness_by_gameid($gameid);
1420 /* who has wedding */
1421 $userids = DB_get_all_userid_by_gameid($gameid);
1422 foreach($userids as $user)
1424 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1425 if($usersick == 'wedding')
1428 /* who won the trick */
1429 $play = DB_get_cards_by_trick($trickid);
1430 $winner = get_winner($play,$gametype); /* returns the position */
1431 $winnerid = DB_get_userid('gameid-position',$gameid,$winner);
1432 /* is tricknr <=3 */
1433 if($tricknr <=3 && $winnerid!=$whosick)
1435 /* set resolved at tricknr*/
1436 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1438 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1439 DB_set_party_by_hash($whash,'re');
1441 if($tricknr == 3 && $winnerid==$whosick)
1443 /* set resolved at tricknr*/
1444 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1449 /* if sequence == 4, set winner of the trick, count points and set the next player */
1452 $play = DB_get_cards_by_trick($trickid);
1453 $winner = get_winner($play,$gametype); /* returns the position */
1455 /* check if someone caught a fox */
1456 /* first check if we should account for solos at all,
1457 * since it doesn't make sense in some games
1459 $ok = 0; /* fox shouldn't be counted */
1460 if(DB_get_gametype_by_gameid($gameid)=='solo')
1462 $solo = DB_get_solo_by_gameid($gameid);
1463 if($solo == 'trump' || $solo == 'silent')
1464 $ok = 1; /* for trump solos and silent solos, foxes are ok */
1467 $ok = 1; /* for all other games (not solos) foxes are ok too */
1470 foreach($play as $played)
1472 if ( $played['card']==19 || $played['card']==20 )
1473 if ($played['pos']!= $winner )
1475 /* possible caught a fox, check party */
1476 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1477 $uid2 = DB_get_userid('gameid-position',$gameid,$played['pos']);
1479 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1480 $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1482 if($party1 != $party2)
1483 DB_query("INSERT INTO Score".
1484 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1488 /* check for karlchen (jack of clubs in the last trick)*/
1489 /* same as for foxes, karlchen doesn't always make sense
1490 * check what kind of game it is and set karlchen accordingly */
1491 $ok = 1; /* default: karlchen should be accounted for */
1493 $ok = 0; /* Karlchen works only in the last trick */
1494 if($ok && DB_get_gametype_by_gameid($gameid)=='solo' )
1496 $solo = DB_get_solo_by_gameid($gameid);
1497 if($solo == 'trumpless' || $solo == 'jack' || $solo == 'queen' )
1498 $ok = 0; /* no Karlchen in these solos */
1502 foreach($play as $played)
1503 if ( $played['card']==11 || $played['card']==12 )
1504 if ($played['pos'] == $winner )
1506 /* possible caught a fox, check party */
1507 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1508 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1510 DB_query("INSERT INTO Score".
1511 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1513 /* check for doppelopf (>40 points)*/
1515 foreach($play as $played)
1517 $points += DB_get_card_value_by_cardid($played['card']);
1521 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1522 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1524 DB_query("INSERT INTO Score".
1525 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
1529 DB_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1531 echo "ERROR during scoring";
1534 echo "DEBUG: position $winner won the trick <br />";
1536 /* who is the next player? */
1541 $next = DB_get_pos_by_hash($me)+1;
1543 if($next==5) $next=1;
1545 /* check for coment */
1546 if(myisset('comment'))
1548 $comment = $_REQUEST['comment'];
1550 DB_insert_comment($comment,$playid,$myid);
1551 if($commentSchweinchen)
1552 $comment = $commentSchweinchen . $comment;
1555 /* display played card */
1556 $pos = DB_get_pos_by_hash($me);
1559 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1560 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1561 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1564 echo " <div class=\"card".($pos-1)."\">\n ";
1566 /* display comments */
1567 display_card($card,$PREF['cardset']);
1569 echo "\n <span class=\"comment\"> ".$comment."</span>\n";
1572 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1574 /*check if we still have cards left, else set status to gameover */
1575 if(sizeof(DB_get_hand($me))==0)
1577 DB_set_hand_status_by_hash($me,'gameover');
1578 $mystatus = 'gameover';
1581 /* if all players are done, set game status to game over,
1582 * get the points of the last trick and send out an email
1585 $userids = DB_get_all_userid_by_gameid($gameid);
1588 foreach($userids as $user)
1589 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1593 DB_set_game_status_by_gameid($gameid,'gameover');
1595 /* email next player, if game is still running */
1596 if(DB_get_game_status_by_gameid($gameid)=='play')
1598 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1599 $email = DB_get_email('hash',$next_hash);
1600 $who = DB_get_userid('email',$email);
1601 DB_set_player_by_gameid($gameid,$who);
1603 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1604 "It's your turn now.\n".
1605 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$next_hash."\n\n" ;
1606 if( DB_get_email_pref_by_uid($who)!='emailaddict' )
1607 mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
1609 else /* send out final email */
1611 /* individual score */
1612 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1613 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1614 " LEFT JOIN User ON User.id=Hand.user_id".
1615 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1616 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1617 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1618 " WHERE Hand.game_id='$gameid'".
1619 " GROUP BY User.fullname" );
1620 $message = "The game is over. Thanks for playing :)\n";
1621 $message .= "Final score:\n";
1622 while( $r = DB_fetch_array($result) )
1623 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1625 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1626 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1627 " LEFT JOIN User ON User.id=Hand.user_id".
1628 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1629 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1630 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1631 " WHERE Hand.game_id='$gameid'".
1632 " GROUP BY Hand.party" );
1633 $message .= "\nTotals:\n";
1636 while( $r = DB_fetch_array($result) )
1638 $message .= " ".$r[0]." ".$r[1]."\n";
1641 else if($r[0] == 'contra')
1646 * save score in database
1650 /* get calls from re/contra */
1652 $call_contra = NULL;
1653 foreach($userids as $user)
1655 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1656 $call = DB_get_call_by_hash($hash);
1657 $party = DB_get_party_by_hash($hash);
1661 $call = (int) $call;
1667 else if( $call < $call_re)
1670 else if($party=='contra')
1672 if($call_contra==NULL)
1673 $call_contra = $call;
1674 else if( $call < $call_contra)
1675 $call_contra = $call;
1680 /* figure out who one */
1681 $winning_party = NULL;
1683 if($call_re == NULL && $call_contra==NULL)
1685 /* nobody made a call, so it's easy to figure out who won */
1687 $winning_party='re';
1689 $winning_party='contra';
1693 /* if one party makes a call, they only win, iff they make enough points
1694 * if only one party made a call, the other one wins,
1695 * if the first one didn't make it
1699 $offset = 120 - $call_re;
1701 $offset--; /* since we use a > in the next equation */
1703 if($re > 120+$offset)
1704 $winning_party='re';
1705 else if ($call_contra == NULL )
1706 $winning_party='contra';
1711 $offset = 120 - $call_contra;
1712 if($call_contra == 0)
1713 $offset--; /* since we use a > in the next equation */
1715 if($contra > 120+$offset)
1716 $winning_party='contra';
1717 else if ($call_re == NULL )
1718 $winning_party='re';
1722 /* one point for each call of the other party in case the other party didn't win
1723 * and one point each in case the party made more than points than one of the calls
1725 if($winning_party!='contra' && $call_contra!=NULL)
1727 for( $p=$call_contra;$p<=120; $p+=30 )
1729 DB_query("INSERT INTO Score".
1730 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
1733 for( $p=$call_contra; $p<120; $p+=30)
1736 DB_query("INSERT INTO Score".
1737 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'made$p')");
1740 if($winning_party!='re' and $call_re!=NULL)
1742 for( $p=$call_re;$p<=120; $p+=30 )
1744 DB_query("INSERT INTO Score".
1745 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'against$p')");
1748 for( $p=$call_re; $p<120; $p+=30)
1751 DB_query("INSERT INTO Score".
1752 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
1756 /* point in case contra won */
1757 if($winning_party=='contra')
1759 DB_query("INSERT INTO Score".
1760 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1763 /* one point each for winning and each 30 points + calls */
1764 if($winning_party=='re')
1766 foreach(array(120,150,180,210,240) as $p)
1769 if($p==240 || $call_contra!=NULL)
1773 DB_query("INSERT INTO Score".
1774 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
1776 /* re called something and won */
1777 foreach(array(0,30,60,90,120) as $p)
1779 if($call_re!=NULL && $call_re<$p+1)
1780 DB_query("INSERT INTO Score".
1781 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
1784 else if( $winning_party=='contra')
1786 foreach(array(120,150,180,210,240) as $p)
1789 if($p==240 || $call_re!=NULL)
1792 if($contra>$p-$offset)
1793 DB_query("INSERT INTO Score".
1794 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
1796 /* re called something and won */
1797 foreach(array(0,30,60,90,120) as $p)
1799 if($call_contra!=NULL && $call_contra<$p+1)
1800 DB_query("INSERT INTO Score".
1801 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
1806 /* add score points to email */
1809 $message .= " Points Re: \n";
1810 $queryresult = DB_query("SELECT score FROM Score ".
1811 " WHERE game_id=$gameid AND party='re'".
1813 while($r = DB_fetch_array($queryresult) )
1815 $message .= " ".$r[0]."\n";
1818 $message .= " Points Contra: \n";
1819 $queryresult = DB_query("SELECT score FROM Score ".
1820 " WHERE game_id=$gameid AND party='contra'".
1822 while($r = DB_fetch_array($queryresult) )
1824 $message .= " ".$r[0]."\n";
1827 $message .= " Total Points (from the Re point of view): $Tpoint\n";
1830 $session = DB_get_session_by_gameid($gameid);
1831 $score = generate_score_table($session);
1833 $message .= "Score Table:\n";
1834 $message .= format_score_table_ascii($score);
1835 $message .= "\nUse these links to have a look at game ".DB_format_gameid($gameid).": \n";
1837 /* send out final email */
1840 foreach($userids as $user)
1842 $all[] = DB_get_email('userid',$user);
1844 /* add links for all players */
1845 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1846 $name = DB_get_name('userid',$user);
1848 $link = "$name: ".$HOST.$INDEX."?action=game&me=".$hash."\n" ;
1851 $To = implode(",",$all);
1853 $message .= "\n\n (you can use reply all on this email to reach all the players.)\n";
1854 mymail($To,$EmailName."Game over (game ".DB_format_gameid($gameid).") ",$message);
1859 echo "can't find that card?! <br />\n";
1862 else if(myisset('card') && !$myturn )
1864 echo "please wait until it's your turn! <br />\n";
1867 if($seq!=4 && $trickNR>=1 && !(myisset('card') && $myturn) )
1868 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1870 /* display points in case game is over */
1871 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1873 echo " <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1874 " <div class=\"trick\" id=\"trick13\">\n";
1875 /* add pic for re/contra
1876 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1878 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1879 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1880 " LEFT JOIN User ON User.id=Hand.user_id".
1881 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1882 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1883 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1884 " WHERE Hand.game_id='$gameid'".
1885 " GROUP BY User.fullname" );
1886 while( $r = DB_fetch_array($result))
1887 echo " <div class=\"card".($r[3]-1)."\">\n".
1888 " <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
1891 /* display totals */
1892 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1893 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1894 " LEFT JOIN User ON User.id=Hand.user_id".
1895 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1896 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1897 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1898 " WHERE Hand.game_id='$gameid'".
1899 " GROUP BY Hand.party" );
1900 echo "<div class=\"total\">\n Totals:<br />\n";
1901 while( $r = DB_fetch_array($result))
1902 echo " ".$r[0]." ".$r[1]."<br />\n";
1904 $queryresult = DB_query("SELECT timediff(mod_date,create_date) ".
1905 " FROM Game WHERE id='$gameid'");
1906 $r = DB_fetch_array($queryresult);
1907 echo " <p>This game took ".$r[0]." hours.</p>\n";
1909 echo " <div class=\"re\">\n Points Re: <br />\n";
1910 $queryresult = DB_query("SELECT score FROM Score ".
1911 " WHERE game_id=$gameid AND party='re'".
1913 while($r = DB_fetch_array($queryresult) )
1914 echo " ".$r[0]."<br />\n";
1917 echo " <div class=\"contra\">\n Points Contra: <br />\n";
1918 $queryresult = DB_query("SELECT score FROM Score ".
1919 " WHERE game_id=$gameid AND party='contra'".
1921 while($r = DB_fetch_array($queryresult) )
1922 echo " ".$r[0]."<br />\n";
1927 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1930 echo " <li onclick=\"hl_prev();\" class=\"old\"><a href=\"#\">prev</a></li>\n";
1931 echo " <li onclick=\"hl_next();\" class=\"old\"><a href=\"#\">next</a></li>\n";
1932 echo "</ul>\n"; /* end ul tricks*/
1934 $mycards = DB_get_hand($me);
1935 $mycards = mysort($mycards,$gametype);
1936 echo "<div class=\"mycards\">\n";
1938 if($myturn && !myisset('card') && $mystatus=='play' )
1940 echo "Hello ".$myname.", it's your turn! <br />\n";
1941 echo "Your cards are: <br />\n";
1943 /* do we have to follow suite? */
1945 if(have_suit($mycards,$firstcard))
1948 foreach($mycards as $card)
1950 /* display only cards that the player is allowed to play as links, the rest just display normal
1951 * also check if we have both schweinchen, in that case only display on of them as playable
1953 if( ($followsuit && !same_type($card,$firstcard)) ||
1954 ( (int)($card)==19 && !$GAME['schweinchen-first'] &&
1955 ($RULES['schweinchen']=='second'||
1956 ( $RULES['schweinchen']=='secondaftercall' &&
1957 (DB_get_call_by_hash($GAME['schweinchen-who']) || DB_get_partner_call_by_hash($GAME['schweinchen-who']) )))
1958 && $GAME['schweinchen-who']==$me )
1960 display_card($card,$PREF['cardset']);
1962 display_link_card($card,$PREF['cardset']);
1965 else if($mystatus=='play' )
1967 echo "Your cards are: <br />\n";
1968 foreach($mycards as $card)
1969 display_card($card,$PREF['cardset']);
1971 else if($mystatus=='gameover')
1973 $oldcards = DB_get_all_hand($me);
1974 $oldcards = mysort($oldcards,$gametype);
1975 echo "Your cards were: <br />\n";
1976 foreach($oldcards as $card)
1977 display_card($card,$PREF['cardset']);
1979 $userids = DB_get_all_userid_by_gameid($gameid);
1980 foreach($userids as $user)
1982 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1988 $name = DB_get_name('userid',$user);
1989 $oldcards = DB_get_all_hand($userhash);
1990 $oldcards = mysort($oldcards,$gametype);
1991 echo "$name's cards were: <br />\n";
1992 foreach($oldcards as $card)
1993 display_card($card,$PREF['cardset']);
1999 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
2000 if($mystatus=='play')
2003 /* the following happens only when the gamestatus is 'gameover' */
2004 /* check if game is over, display results */
2005 if(DB_get_game_status_by_gameid($gameid)=='play')
2007 echo "The game is over for you.. other people still need to play though";
2011 myerror("error in testing the status");
2014 /* output other games where it is the users turn
2015 * make sure that the people looking at old games don't see the wrong games here
2017 if( $mystatus != 'gameover' )
2018 display_user_menu($myid);
2019 else if( $mystatus == 'gameover' &&
2020 isset($_SESSION['id']) )
2022 display_user_menu($_SESSION['id']);
2026 echo "<div class=\"usermenu\">\n";
2027 echo "It's your turn in these games:<br />\n";
2028 echo "Please log in to see this information.\n";
2032 /* display rule set for this game */
2033 echo "<div class=\"gameinfo\">\n";
2035 if($gamestatus == 'play' )
2037 $myparty = DB_get_party_by_hash($me);
2038 output_form_calls($me,$myparty);
2040 /* get time from the last action of the game */
2041 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
2042 $gameend = time() - strtotime($r[0]);
2044 if($gamestatus == 'play' || $gameend < 60*60*24*7)
2046 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
2049 echo "<input type=\"submit\" value=\"submit\" />\n";
2051 /* has this hand been played by others? */
2052 $other_game_ids = DB_played_by_others($gameid);
2053 if(sizeof($other_game_ids)>0 && $mystatus=='gameover')
2055 $mypos = DB_get_pos_by_hash($me);
2056 echo "<p>See how other played the same hand: <br />\n";
2057 foreach($other_game_ids as $id)
2059 $otherhash = DB_get_hash_from_game_and_pos($id,$mypos);
2060 $othername = DB_get_name('hash',$otherhash);
2061 echo "<a href=\"$INDEX?action=game&me=$otherhash\">$othername</a><br />";
2070 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' && isset($_SESSION['id']) && $_SESSION['id']==$myid)
2072 $session = DB_get_session_by_gameid($gameid);
2073 $result = DB_query("SELECT id,create_date FROM Game".
2074 " WHERE session=$session".
2075 " ORDER BY create_date DESC".
2079 $r = DB_fetch_array($result);
2081 if(!$session || $gameid==$r[0])
2083 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
2084 $names = DB_get_all_names_by_gameid($gameid);
2085 $type = DB_get_gametype_by_gameid($gameid);
2089 $solo = DB_get_solo_by_gameid($gameid);
2091 if($solo!='silent') /* repeat game with same first player */
2092 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
2093 else /* rotate normally */
2094 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
2096 else /* rotate normally */
2097 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);