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 set_gametype($gametype); /* this sets the $CARDS variable */
786 $myparty = DB_get_party_by_hash($me);
788 /* the following is part B) of whats needs to be done)
789 /* check if user wants to give cards back */
790 if(myisset('exchange'))
792 $exchange = $_REQUEST['exchange'];
793 $partnerhash = DB_get_partner_hash_by_hash($me);
794 $partnerid = DB_get_userid('hash',$partnerhash);
795 $partnerhand = DB_get_handid('gameid-userid',$gameid,$partnerid);
797 /* if exchange is set to a value>0, exchange that card back to the partner */
800 $result = DB_query("UPDATE Hand_Card SET hand_id='$partnerhand'".
801 " WHERE hand_id='$myhand' AND card_id=".DB_quote_smart($exchange));
802 DB_add_exchanged_card(DB_quote_smart($exchange),$myhand,$partnerhand);
807 $mycards = DB_get_hand($me);
808 $mycards = mysort($mycards,$gametype);
810 /* output pre-game trick in case user reloads,
811 * only needs to be done when a team has been formed */
812 if($myparty=='re' || $myparty=='contra')
814 echo "\n<ul class=\"tricks\">\n";
815 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
817 $mygametype = DB_get_gametype_by_gameid($gameid);
819 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
820 " <div class=\"trick\" id=\"trick0\">\n";
822 /* get information so show the cards that have been handed over in a poverty game */
823 output_exchanged_cards();
825 echo " </div>\n </li>\n"; /* end div trick, end li trick */
827 /* end output pre-game trick */
829 /* check if user need to give more cards back */
830 if( ($myparty=='re' || $myparty=='contra') && count($mycards)>12)
832 echo "<div class=\"poverty\"> you need to get rid of a few cards</div>\n";
835 echo "<div class=\"mycards\">Your cards are: <br />\n";
836 foreach($mycards as $card)
837 display_link_card($card,$PREF['cardset'],$type);
838 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select card to give back\" />\n";
841 else if( ($myparty=='re' || $myparty=='contra') && count($mycards)==12)
843 /* user is done, ready to play */
844 DB_set_hand_status_by_hash($me,'play');
846 /* email start player */
847 $startplayer = DB_get_startplayer_by_gameid($gameid);
848 $email = DB_get_email('position-gameid',$startplayer,$gameid);
849 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
850 $who = DB_get_userid('email',$email);
851 DB_set_player_by_gameid($gameid,$who);
855 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
857 /* email startplayer */
858 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
859 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
860 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
864 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
867 /* the following is part A) of what needs to be done */
868 if(!myisset('trump'))
872 echo "<div class=\"poverty\">\n";
873 $userids = DB_get_all_userid_by_gameid($gameid);
874 foreach($userids as $user)
876 $name = DB_get_name('userid',$user);
877 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
878 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
879 $userparty = DB_get_party_by_hash($userhash);
881 if($usersick=='poverty' && !$userparty)
883 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
884 $cards = DB_get_hand($hash);
887 foreach($cards as $card)
888 if($card<27) $nrtrump++;
889 echo "Player $name has $nrtrump trump. Do you want to take them?".
890 "<a href=\"index.php?action=game&me=$me&trump=$user\">Yes</a> <br />\n";
893 echo "<a href=\"index.php?action=game&me=$me&trump=no\">No way</a> <br />\n";
894 echo "</div><div>\n";
896 echo "<div class=\"mycards\">Your cards are: <br />\n";
897 foreach($mycards as $card)
898 display_card($card,$PREF['cardset']);
899 echo "</div></div>\n";
905 $trump = $_REQUEST['trump'];
909 /* user doesn't want to take trump */
910 DB_set_hand_status_by_hash($me,'play');
912 /* set next player who needs to be asked and email him*/
913 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
914 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
916 /* don't ask people who have poverty */
918 if($firstsick=='poverty')
920 if($secondsick=='poverty')
925 if($gametype=='dpoverty')
927 $next=999; /* need to cancel for sure, since both would need to take the trump */
930 /* no more people to ask, need to cancel the game */
933 $message = "Hello, \n\n".
934 "Game ".DB_format_gameid($gameid)." has been canceled since nobody wanted to take the trump.\n";
936 $userids = DB_get_all_userid_by_gameid($gameid);
937 foreach($userids as $user)
939 $To = DB_get_email('userid',$user);
940 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled (poverty not resolved)",$message);
943 /* delete everything from the dB */
946 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been canceled.<br /><br /></p>";
951 /* email next player, set his status to poverty */
952 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
953 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
954 $userid = DB_get_userid('email',$To);
956 DB_set_player_by_gameid($gameid,$userid);
957 DB_set_hand_status_by_hash($userhash,'poverty');
959 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
960 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
961 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
966 /* player wants to take trump, change cards */
968 /* user wants to take trump */
969 $trump = $_REQUEST['trump'];
970 $userhand = DB_get_handid('gameid-userid',$gameid,$trump);
971 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
973 /* remember which cards were handed over*/
974 $partnerhand = DB_get_all_hand($userhash);
975 foreach ($partnerhand as $card)
977 DB_add_exchanged_card($card,$userhand,$myhand);
979 /* copy trump from player A to B */
980 $result = DB_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
983 $mycards = DB_get_hand($me);
986 if($gametype=='poverty')
988 $userids = DB_get_all_userid_by_gameid($gameid);
989 foreach($userids as $user)
991 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
992 if($hash==$userhash||$hash==$me)
994 DB_set_party_by_hash($hash,'re');
998 DB_set_party_by_hash($hash,'contra');
999 DB_set_hand_status_by_hash($hash,'play'); /* the contra party is ready to play */
1002 /* check if we are done (in case of no trump handed over), if so, go to 'play' phase right away*/
1003 if(count($mycards)==12)
1005 DB_set_hand_status_by_hash($me,'play');
1010 /* has the re party already been set?*/
1012 $userids = DB_get_all_userid_by_gameid($gameid);
1013 foreach($userids as $user)
1015 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1016 $party = DB_get_party_by_hash($hash);
1022 DB_set_party_by_hash($me,'contra');
1023 DB_set_party_by_hash($userhash,'contra');
1027 DB_set_party_by_hash($me,'re');
1028 DB_set_party_by_hash($userhash,'re');
1030 /* send out email to second non-poverty player */
1031 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
1032 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
1035 if($firstsick=='poverty')
1036 if($secondsick=='poverty')
1042 echo "<div class=\"message\">Error in poverty, please contact the Admin</div>\n";
1044 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
1045 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
1046 $userid = DB_get_userid('email',$To);
1048 DB_set_player_by_gameid($gameid,$userid);
1049 DB_set_hand_status_by_hash($userhash,'poverty');
1051 $message = "Two people have poverty, it's your turn to decide, if you want to take the trump. Please visit:".
1052 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
1053 mymail($To,$EmailName." double poverty (game ".DB_format_gameid($gameid).")",$message);
1058 echo "<div class=\"message\"> Please, <a href=\"$INDEX?action=game&me=$me\">continue</a> here.</div>\n";
1066 /* both entries here, so that the tricks are visible for both.
1067 * in case of 'play' there is a break later that skips the last part
1070 /* check if all players are ready to play,
1071 * if so, send out email to the startplayer
1072 * only need to do this if the game hasn't started yet
1074 $gamestatus = DB_get_game_status_by_gameid($gameid);
1075 if($gamestatus == 'pre')
1078 $userids = DB_get_all_userid_by_gameid($gameid);
1079 foreach($userids as $user)
1081 $userstatus = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
1082 if($userstatus !='play' && $userstatus!='gameover')
1085 DB_set_player_by_gameid($gameid,$user);
1091 /* only set this after all poverty, etc. are handled*/
1092 DB_set_game_status_by_gameid($gameid,'play');
1094 /* email startplayer */
1095 $startplayer = DB_get_startplayer_by_gameid($gameid);
1096 $email = DB_get_email('position-gameid',$startplayer,$gameid);
1097 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
1098 $who = DB_get_userid('email',$email);
1099 DB_set_player_by_gameid($gameid,$who);
1101 if($hash!=$me && DB_get_email_pref_by_hash($hash)!='emailaddict')
1103 /* email startplayer) */
1104 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
1105 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
1106 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
1110 /* figure out what kind of game we are playing,
1111 * set the global variables $CARDS['trump'],$CARDS['diamonds'],$CARDS['hearts'],
1112 * $CARDS['clubs'],$CARDS['spades'],$CARDS['foxes']
1116 $gametype = DB_get_gametype_by_gameid($gameid);
1118 if($gametype=='solo')
1120 $gametype = DB_get_solo_by_gameid($gameid);
1121 if($gametype=='silent')
1124 $GT = $gametype.' '.$GT;
1127 $gametype = 'normal';
1129 set_gametype($gametype); /* this sets the $CARDS variable */
1131 /* get some infos about the game, need to reset this, since it might have changed */
1132 $gamestatus = DB_get_game_status_by_gameid($gameid);
1134 /* has the game started? No, then just wait here...*/
1135 if($gamestatus == 'pre')
1137 echo "<p class=\"message\"> You finished the setup, but not everyone else finished it... ".
1138 "You need to wait for the others. Just wait for an email. </p>";
1140 $mycards = DB_get_hand($me);
1141 $mycards = mysort($mycards,$gametype);
1143 echo "<div class=\"mycards\">Your cards are: <br />\n";
1144 foreach($mycards as $card)
1145 display_card($card,$PREF['cardset']);
1148 break; /* not sure this works... the idea is that you can
1149 * only play a card after everyone is ready to play */
1153 /* get time from the last action of the game */
1154 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1155 $gameend = time() - strtotime($r[0]);
1157 /* handle comments in case player didn't play a card, allow comments a week after the end of the game */
1158 if( (!myisset('card') && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
1159 if(myisset('comment'))
1161 $comment = $_REQUEST['comment'];
1162 $playid = DB_get_current_playid($gameid);
1165 DB_insert_comment($comment,$playid,$myid);
1168 /* get everything relevant to display the tricks */
1169 $result = DB_query("SELECT Hand_Card.card_id as card,".
1170 " Hand.position as position,".
1171 " Play.sequence as sequence, ".
1173 " GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
1174 " SEPARATOR '\n' ), ".
1175 " Play.create_date, ".
1178 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1179 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1180 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1181 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1182 "LEFT JOIN User On User.id=Comment.user_id ".
1183 "WHERE Trick.game_id='".$gameid."' ".
1184 "GROUP BY Trick.id, sequence ".
1185 "ORDER BY Trick.id, sequence ASC");
1187 $lasttrick = DB_get_max_trickid($gameid);
1189 $play = array(); /* needed to calculate winner later */
1191 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1192 $firstcard = ''; /* first card in a trick */
1194 echo "\n<ul class=\"tricks\">\n";
1195 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
1197 /* output vorbehalte */
1198 $mygametype = DB_get_gametype_by_gameid($gameid);
1199 if($mygametype != 'normal' && $mygametype != 'silent') /* only show when needed */
1201 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
1202 " <div class=\"trick\" id=\"trick0\">\n";
1204 /* get information so show the cards that have been handed over in a poverty game */
1205 output_exchanged_cards();
1207 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1211 while($r = DB_fetch_array($result))
1219 /* count number of tricks */
1223 /* check if first schweinchen has been played */
1224 if( $GAME['schweinchen-who'] && ($r[0] == 19 || $r[0] == 20) )
1225 if(!$GAME['schweinchen-first'])
1226 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1228 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1230 /* save card to be able to find the winner of the trick later */
1231 $play[$seq] = array('card'=>$r[0],'pos'=>$pos);
1235 /* first card in a trick, output some html */
1236 if($trick!=$lasttrick)
1238 /* start of an old trick? */
1239 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1240 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1241 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1243 else if($trick==$lasttrick)
1245 /* start of a last trick? */
1246 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1247 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1248 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1251 /* remember first card, so that we are able to check, what cards can be played */
1256 echo " <div class=\"card".($pos-1)."\">\n";
1258 /* display comments */
1260 echo " <span class=\"comment\">".$comment."</span>\n";
1263 display_card($r[0],$PREF['cardset']);
1265 echo " </div>\n"; /* end div card */
1270 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1274 /* whos turn is it? */
1277 $winner = get_winner($play,$gametype); /* returns the position */
1279 $firstcard = ''; /* new trick, no first card */
1284 if($next==5) $next = 1;
1287 /* my turn?, display cards as links, ask for comments*/
1288 if(DB_get_pos_by_hash($me) == $next)
1293 /* do we want to play a card? */
1294 if(myisset('card') && $myturn)
1296 $card = $_REQUEST['card'];
1297 $handid = DB_get_handid('hash',$me);
1298 $commentSchweinchen =''; /* used to add a comment when Schweinchen is being played */
1300 /* check if we have card and that we haven't played it yet*/
1301 /* set played in hand_card to true where hand_id and card_id*/
1302 $r = DB_query_array("SELECT id FROM Hand_Card WHERE played='false' and ".
1303 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1304 $handcardid = $r[0];
1306 if($handcardid) /* everything ok, play card */
1308 /* update Game timestamp */
1309 DB_update_game_timestamp($gameid);
1311 /* mark card as played */
1312 DB_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1313 DB_quote_smart($card));
1315 /* get trick id or start new trick */
1316 $a = DB_get_current_trickid($gameid);
1321 $playid = DB_play_card($trickid,$handcardid,$sequence);
1323 /* check special output for schweinchen in case in case a fox is being played
1324 * check for correct rules, etc. has already been done
1326 if( $GAME['schweinchen-who'] && ($card == 19 || $card == 20) )
1328 if(!$GAME['schweinchen-first'])
1329 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1331 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1333 if( $RULES['schweinchen']=='both' ||
1334 ($RULES['schweinchen']=='second' && $GAME['schweinchen-second']==1 )||
1335 ($RULES['schweinchen']=='secondaftercall' && $GAME['schweinchen-second']==1 &&
1336 (DB_get_call_by_hash($GAME['schweinchen-who']) || DB_get_partner_call_by_hash($GAME['schweinchen-who']) ))
1339 DB_insert_comment('Schweinchen! ',$playid,$myid);
1340 $commentSchweinchen = 'Schweinchen! ';
1343 echo 'schweinchen = '.$GAME['schweinchen-who'].' ---<br />';
1346 /* if sequence == 4 check who one in case of wedding */
1347 if($sequence == 4 && $GT == 'wedding')
1349 /* is wedding resolve */
1350 $resolved = DB_get_sickness_by_gameid($gameid);
1353 /* who has wedding */
1354 $userids = DB_get_all_userid_by_gameid($gameid);
1355 foreach($userids as $user)
1357 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1358 if($usersick == 'wedding')
1361 /* who won the trick */
1362 $play = DB_get_cards_by_trick($trickid);
1363 $winner = get_winner($play,$gametype); /* returns the position */
1364 $winnerid = DB_get_userid('gameid-position',$gameid,$winner);
1365 /* is tricknr <=3 */
1366 if($tricknr <=3 && $winnerid!=$whosick)
1368 /* set resolved at tricknr*/
1369 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1371 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1372 DB_set_party_by_hash($whash,'re');
1374 if($tricknr == 3 && $winnerid==$whosick)
1376 /* set resolved at tricknr*/
1377 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1382 /* if sequence == 4, set winner of the trick, count points and set the next player */
1385 $play = DB_get_cards_by_trick($trickid);
1386 $winner = get_winner($play,$gametype); /* returns the position */
1388 /* check if someone caught a fox */
1389 /* first check if we should account for solos at all,
1390 * since it doesn't make sense in some games
1392 $ok = 0; /* fox shouldn't be counted */
1393 if(DB_get_gametype_by_gameid($gameid)=='solo')
1395 $solo = DB_get_solo_by_gameid($gameid);
1396 if($solo == 'trump' || $solo == 'silent')
1397 $ok = 1; /* for trump solos and silent solos, foxes are ok */
1400 $ok = 1; /* for all other games (not solos) foxes are ok too */
1403 foreach($play as $played)
1405 if ( $played['card']==19 || $played['card']==20 )
1406 if ($played['pos']!= $winner )
1408 /* possible caught a fox, check party */
1409 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1410 $uid2 = DB_get_userid('gameid-position',$gameid,$played['pos']);
1412 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1413 $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1415 if($party1 != $party2)
1416 DB_query("INSERT INTO Score".
1417 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1421 /* check for karlchen (jack of clubs in the last trick)*/
1422 /* same as for foxes, karlchen doesn't always make sense
1423 * check what kind of game it is and set karlchen accordingly */
1424 $ok = 1; /* default: karlchen should be accounted for */
1426 $ok = 0; /* Karlchen works only in the last trick */
1427 if($ok && DB_get_gametype_by_gameid($gameid)=='solo' )
1429 $solo = DB_get_solo_by_gameid($gameid);
1430 if($solo == 'trumpless' || $solo == 'jack' || $solo == 'queen' )
1431 $ok = 0; /* no Karlchen in these solos */
1435 foreach($play as $played)
1436 if ( $played['card']==11 || $played['card']==12 )
1437 if ($played['pos'] == $winner )
1439 /* possible caught a fox, check party */
1440 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1441 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1443 DB_query("INSERT INTO Score".
1444 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1446 /* check for doppelopf (>40 points)*/
1448 foreach($play as $played)
1450 $points += DB_get_card_value_by_cardid($played['card']);
1454 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1455 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1457 DB_query("INSERT INTO Score".
1458 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
1462 DB_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1464 echo "ERROR during scoring";
1467 echo "DEBUG: position $winner won the trick <br />";
1469 /* who is the next player? */
1474 $next = DB_get_pos_by_hash($me)+1;
1476 if($next==5) $next=1;
1478 /* check for coment */
1479 if(myisset('comment'))
1481 $comment = $_REQUEST['comment'];
1483 DB_insert_comment($comment,$playid,$myid);
1484 if($commentSchweinchen)
1485 $comment = $commentSchweinchen . $comment;
1488 /* display played card */
1489 $pos = DB_get_pos_by_hash($me);
1492 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1493 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1494 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1497 echo " <div class=\"card".($pos-1)."\">\n ";
1499 /* display comments */
1500 display_card($card,$PREF['cardset']);
1502 echo "\n <span class=\"comment\"> ".$comment."</span>\n";
1505 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1507 /*check if we still have cards left, else set status to gameover */
1508 if(sizeof(DB_get_hand($me))==0)
1510 DB_set_hand_status_by_hash($me,'gameover');
1511 $mystatus = 'gameover';
1514 /* if all players are done, set game status to game over,
1515 * get the points of the last trick and send out an email
1518 $userids = DB_get_all_userid_by_gameid($gameid);
1521 foreach($userids as $user)
1522 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1526 DB_set_game_status_by_gameid($gameid,'gameover');
1528 /* email next player, if game is still running */
1529 if(DB_get_game_status_by_gameid($gameid)=='play')
1531 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1532 $email = DB_get_email('hash',$next_hash);
1533 $who = DB_get_userid('email',$email);
1534 DB_set_player_by_gameid($gameid,$who);
1536 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1537 "It's your turn now.\n".
1538 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$next_hash."\n\n" ;
1539 if( DB_get_email_pref_by_uid($who)!='emailaddict' )
1540 mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
1542 else /* send out final email */
1544 /* individual score */
1545 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1546 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1547 " LEFT JOIN User ON User.id=Hand.user_id".
1548 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1549 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1550 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1551 " WHERE Hand.game_id='$gameid'".
1552 " GROUP BY User.fullname" );
1553 $message = "The game is over. Thanks for playing :)\n";
1554 $message .= "Final score:\n";
1555 while( $r = DB_fetch_array($result) )
1556 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1558 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1559 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1560 " LEFT JOIN User ON User.id=Hand.user_id".
1561 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1562 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1563 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1564 " WHERE Hand.game_id='$gameid'".
1565 " GROUP BY Hand.party" );
1566 $message .= "\nTotals:\n";
1569 while( $r = DB_fetch_array($result) )
1571 $message .= " ".$r[0]." ".$r[1]."\n";
1574 else if($r[0] == 'contra')
1579 * save score in database
1583 /* get calls from re/contra */
1585 $call_contra = NULL;
1586 foreach($userids as $user)
1588 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1589 $call = DB_get_call_by_hash($hash);
1590 $party = DB_get_party_by_hash($hash);
1594 $call = (int) $call;
1600 else if( $call < $call_re)
1603 else if($party=='contra')
1605 if($call_contra==NULL)
1606 $call_contra = $call;
1607 else if( $call < $call_contra)
1608 $call_contra = $call;
1613 /* figure out who one */
1614 $winning_party = NULL;
1616 if($call_re == NULL && $call_contra==NULL)
1618 /* nobody made a call, so it's easy to figure out who won */
1620 $winning_party='re';
1622 $winning_party='contra';
1626 /* if one party makes a call, they only win, iff they make enough points
1627 * if only one party made a call, the other one wins,
1628 * if the first one didn't make it
1632 $offset = 120 - $call_re;
1634 $offset--; /* since we use a > in the next equation */
1636 if($re > 120+$offset)
1637 $winning_party='re';
1638 else if ($call_contra == NULL )
1639 $winning_party='contra';
1644 $offset = 120 - $call_contra;
1645 if($call_contra == 0)
1646 $offset--; /* since we use a > in the next equation */
1648 if($contra > 120+$offset)
1649 $winning_party='contra';
1650 else if ($call_re == NULL )
1651 $winning_party='re';
1655 /* one point for each call of the other party in case the other party didn't win
1656 * and one point each in case the party made more than points than one of the calls
1658 if($winning_party!='contra' && $call_contra!=NULL)
1660 for( $p=$call_contra;$p<=120; $p+=30 )
1662 DB_query("INSERT INTO Score".
1663 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
1666 for( $p=$call_contra; $p<120; $p+=30)
1669 DB_query("INSERT INTO Score".
1670 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'made$p')");
1673 if($winning_party!='re' and $call_re!=NULL)
1675 for( $p=$call_re;$p<=120; $p+=30 )
1677 DB_query("INSERT INTO Score".
1678 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'against$p')");
1681 for( $p=$call_re; $p<120; $p+=30)
1684 DB_query("INSERT INTO Score".
1685 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
1689 /* point in case contra won */
1690 if($winning_party=='contra')
1692 DB_query("INSERT INTO Score".
1693 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1696 /* one point each for winning and each 30 points + calls */
1697 if($winning_party=='re')
1699 foreach(array(120,150,180,210,240) as $p)
1702 if($p==240 || $call_contra!=NULL)
1706 DB_query("INSERT INTO Score".
1707 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
1709 /* re called something and won */
1710 foreach(array(0,30,60,90,120) as $p)
1712 if($call_re!=NULL && $call_re<$p+1)
1713 DB_query("INSERT INTO Score".
1714 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
1717 else if( $winning_party=='contra')
1719 foreach(array(120,150,180,210,240) as $p)
1722 if($p==240 || $call_re!=NULL)
1725 if($contra>$p-$offset)
1726 DB_query("INSERT INTO Score".
1727 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
1729 /* re called something and won */
1730 foreach(array(0,30,60,90,120) as $p)
1732 if($call_contra!=NULL && $call_contra<$p+1)
1733 DB_query("INSERT INTO Score".
1734 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
1739 /* add score points to email */
1742 $message .= " Points Re: \n";
1743 $queryresult = DB_query("SELECT score FROM Score ".
1744 " WHERE game_id=$gameid AND party='re'".
1746 while($r = DB_fetch_array($queryresult) )
1748 $message .= " ".$r[0]."\n";
1751 $message .= " Points Contra: \n";
1752 $queryresult = DB_query("SELECT score FROM Score ".
1753 " WHERE game_id=$gameid AND party='contra'".
1755 while($r = DB_fetch_array($queryresult) )
1757 $message .= " ".$r[0]."\n";
1760 $message .= " Total Points (from the Re point of view): $Tpoint\n";
1763 $session = DB_get_session_by_gameid($gameid);
1764 $score = generate_score_table($session);
1766 $message .= "Score Table:\n";
1767 $message .= format_score_table_ascii($score);
1768 $message .= "\nUse these links to have a look at game ".DB_format_gameid($gameid).": \n";
1770 /* send out final email */
1773 foreach($userids as $user)
1775 $all[] = DB_get_email('userid',$user);
1777 /* add links for all players */
1778 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1779 $name = DB_get_name('userid',$user);
1781 $link = "$name: ".$HOST.$INDEX."?action=game&me=".$hash."\n" ;
1784 $To = implode(",",$all);
1786 $message .= "\n\n (you can use reply all on this email to reach all the players.)\n";
1787 mymail($To,$EmailName."Game over (game ".DB_format_gameid($gameid).") ",$message);
1792 echo "can't find that card?! <br />\n";
1795 else if(myisset('card') && !$myturn )
1797 echo "please wait until it's your turn! <br />\n";
1800 if($seq!=4 && $trickNR>=1 && !(myisset('card') && $myturn) )
1801 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1803 /* display points in case game is over */
1804 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1806 echo " <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1807 " <div class=\"trick\" id=\"trick13\">\n";
1808 /* add pic for re/contra
1809 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1811 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1812 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1813 " LEFT JOIN User ON User.id=Hand.user_id".
1814 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1815 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1816 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1817 " WHERE Hand.game_id='$gameid'".
1818 " GROUP BY User.fullname" );
1819 while( $r = DB_fetch_array($result))
1820 echo " <div class=\"card".($r[3]-1)."\">\n".
1821 " <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
1824 /* display totals */
1825 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1826 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1827 " LEFT JOIN User ON User.id=Hand.user_id".
1828 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1829 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1830 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1831 " WHERE Hand.game_id='$gameid'".
1832 " GROUP BY Hand.party" );
1833 echo "<div class=\"total\">\n Totals:<br />\n";
1834 while( $r = DB_fetch_array($result))
1835 echo " ".$r[0]." ".$r[1]."<br />\n";
1837 $queryresult = DB_query("SELECT timediff(mod_date,create_date) ".
1838 " FROM Game WHERE id='$gameid'");
1839 $r = DB_fetch_array($queryresult);
1840 echo " <p>This game took ".$r[0]." hours.</p>\n";
1842 echo " <div class=\"re\">\n Points Re: <br />\n";
1843 $queryresult = DB_query("SELECT score FROM Score ".
1844 " WHERE game_id=$gameid AND party='re'".
1846 while($r = DB_fetch_array($queryresult) )
1847 echo " ".$r[0]."<br />\n";
1850 echo " <div class=\"contra\">\n Points Contra: <br />\n";
1851 $queryresult = DB_query("SELECT score FROM Score ".
1852 " WHERE game_id=$gameid AND party='contra'".
1854 while($r = DB_fetch_array($queryresult) )
1855 echo " ".$r[0]."<br />\n";
1860 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1863 echo " <li onclick=\"hl_prev();\" class=\"old\"><a href=\"#\">prev</a></li>\n";
1864 echo " <li onclick=\"hl_next();\" class=\"old\"><a href=\"#\">next</a></li>\n";
1865 echo "</ul>\n"; /* end ul tricks*/
1867 $mycards = DB_get_hand($me);
1868 $mycards = mysort($mycards,$gametype);
1869 echo "<div class=\"mycards\">\n";
1871 if($myturn && !myisset('card') && $mystatus=='play' )
1873 echo "Hello ".$myname.", it's your turn! <br />\n";
1874 echo "Your cards are: <br />\n";
1876 /* do we have to follow suite? */
1878 if(have_suit($mycards,$firstcard))
1881 foreach($mycards as $card)
1883 /* display only cards that the player is allowed to play as links, the rest just display normal
1884 * also check if we have both schweinchen, in that case only display on of them as playable
1886 if( ($followsuit && !same_type($card,$firstcard)) ||
1887 ( (int)($card)==19 && !$GAME['schweinchen-first'] &&
1888 ($RULES['schweinchen']=='second'||
1889 ( $RULES['schweinchen']=='secondaftercall' &&
1890 (DB_get_call_by_hash($GAME['schweinchen-who']) || DB_get_partner_call_by_hash($GAME['schweinchen-who']) )))
1891 && $GAME['schweinchen-who']==$me )
1893 display_card($card,$PREF['cardset']);
1895 display_link_card($card,$PREF['cardset']);
1898 else if($mystatus=='play' )
1900 echo "Your cards are: <br />\n";
1901 foreach($mycards as $card)
1902 display_card($card,$PREF['cardset']);
1904 else if($mystatus=='gameover')
1906 $oldcards = DB_get_all_hand($me);
1907 $oldcards = mysort($oldcards,$gametype);
1908 echo "Your cards were: <br />\n";
1909 foreach($oldcards as $card)
1910 display_card($card,$PREF['cardset']);
1912 $userids = DB_get_all_userid_by_gameid($gameid);
1913 foreach($userids as $user)
1915 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1921 $name = DB_get_name('userid',$user);
1922 $oldcards = DB_get_all_hand($userhash);
1923 $oldcards = mysort($oldcards,$gametype);
1924 echo "$name's cards were: <br />\n";
1925 foreach($oldcards as $card)
1926 display_card($card,$PREF['cardset']);
1932 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1933 if($mystatus=='play')
1936 /* the following happens only when the gamestatus is 'gameover' */
1937 /* check if game is over, display results */
1938 if(DB_get_game_status_by_gameid($gameid)=='play')
1940 echo "The game is over for you.. other people still need to play though";
1944 myerror("error in testing the status");
1947 /* output other games where it is the users turn
1948 * make sure that the people looking at old games don't see the wrong games here
1950 if( $mystatus != 'gameover' )
1951 display_user_menu($myid);
1952 else if( $mystatus == 'gameover' &&
1953 isset($_SESSION['id']) )
1955 display_user_menu($_SESSION['id']);
1959 echo "<div class=\"usermenu\">\n";
1960 echo "It's your turn in these games:<br />\n";
1961 echo "Please log in to see this information.\n";
1965 /* display rule set for this game */
1966 echo "<div class=\"gameinfo\">\n";
1968 if($gamestatus == 'play' )
1970 $myparty = DB_get_party_by_hash($me);
1971 output_form_calls($me,$myparty);
1973 /* get time from the last action of the game */
1974 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1975 $gameend = time() - strtotime($r[0]);
1977 if($gamestatus == 'play' || $gameend < 60*60*24*7)
1979 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1982 echo "<input type=\"submit\" value=\"submit\" />\n";
1984 /* has this hand been played by others? */
1985 $other_game_ids = DB_played_by_others($gameid);
1986 if(sizeof($other_game_ids)>0 && $mystatus=='gameover')
1988 $mypos = DB_get_pos_by_hash($me);
1989 echo "<p>See how other played the same hand: <br />\n";
1990 foreach($other_game_ids as $id)
1992 $otherhash = DB_get_hash_from_game_and_pos($id,$mypos);
1993 $othername = DB_get_name('hash',$otherhash);
1994 echo "<a href=\"$INDEX?action=game&me=$otherhash\">$othername</a><br />";
2003 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' && isset($_SESSION['id']) && $_SESSION['id']==$myid)
2005 $session = DB_get_session_by_gameid($gameid);
2006 $result = DB_query("SELECT id,create_date FROM Game".
2007 " WHERE session=$session".
2008 " ORDER BY create_date DESC".
2012 $r = DB_fetch_array($result);
2014 if(!$session || $gameid==$r[0])
2016 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
2017 $names = DB_get_all_names_by_gameid($gameid);
2018 $type = DB_get_gametype_by_gameid($gameid);
2022 $solo = DB_get_solo_by_gameid($gameid);
2024 if($solo!='silent') /* repeat game with same first player */
2025 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
2026 else /* rotate normally */
2027 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
2029 else /* rotate normally */
2030 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);