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 $session = DB_get_session_by_gameid($gameid);
39 /* get prefs and save them in a variable*/
40 $PREF = DB_get_PREF(isset($_SESSION['id'])?$_SESSION['id']:$myid);
42 /* get rule set for this game */
43 $RULES = DB_get_RULES($gameid);
45 /* get some infos about the game */
46 $gametype = DB_get_gametype_by_gameid($gameid);
47 $gamestatus = DB_get_game_status_by_gameid($gameid);
51 $gametype = DB_get_solo_by_gameid($gameid);
52 $GT = $gametype.' '.$GT;
55 /* do we need to worry about Schweinchen?
56 * check gametype and rules
57 * if yes, figure out if someone actually has Schweinchen
58 * save information in $GAME
61 if( $gamestatus == 'pre' )
63 /* always need to use Schweinchen to figure out for example who has poverty */
68 /* in a game Schweinchen is not valid in all types of games */
69 if( $gametype == 'normal' || $gametype == 'silent' || $gametype=='trump' || $gametype=='wedding')
70 if( in_array($RULES['schweinchen'],array('both','second','secondaftercall')) )
74 /* these are the defaults */
75 $GAME['schweinchen-who'] = NULL;
76 $GAME['schweinchen-first'] = NULL;
77 $GAME['schweinchen-second'] = NULL;
81 /* need to check for Schweinchen */
84 $hash = DB_get_hash_from_game_and_pos($gameid,$i);
85 $cards = DB_get_all_hand($hash);
86 if( in_array('19',$cards) && in_array('20',$cards) )
87 $GAME['schweinchen-who']=$hash;
89 $GAME['schweinchen-first'] = 0; /* to keep track if they have been played already */
90 $GAME['schweinchen-second'] = 0;
92 /* end check for Schweinchen */
94 /* set the $CARDS variable, needed for sorting the cards
95 * we set it to normal so that the pre-game phase is handled ok
96 * and later set it to the correct game type that is played
98 set_gametype('normal');
100 /* put everyting in a form */
101 echo "<form action=\"index.php?action=game&me=$me\" method=\"post\">\n";
103 /* handle user notes (only possible while game is running)*/
104 if( $mystatus!='gameover' )
107 $note = $_REQUEST['note'];
110 DB_insert_note($note,$gameid,$myid);
113 /* make sure that we don't show the notes to the wrong person
114 * (e.g. other people looking at an old game)
116 if( $mystatus != 'gameover' ||
117 ( $mystatus == 'gameover' &&
118 isset($_SESSION['id']) &&
119 $myid == $_SESSION['id']))
120 output_user_notes($myid,$gameid,$mystatus);
123 if(myisset('call') && $_REQUEST['call'] == '120' && can_call(120,$me))
124 $result = DB_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
125 if(myisset('call') && $_REQUEST['call'] == '90' && can_call(90,$me))
126 $result = DB_query("UPDATE Hand SET point_call='90' WHERE hash='$me' ");
127 if(myisset('call') && $_REQUEST['call'] == '60' && can_call(60,$me))
128 $result = DB_query("UPDATE Hand SET point_call='60' WHERE hash='$me' ");
129 if(myisset('call') && $_REQUEST['call'] == '30' && can_call(30,$me))
130 $result = DB_query("UPDATE Hand SET point_call='30' WHERE hash='$me' ");
131 if(myisset('call') && $_REQUEST['call'] == '0' && can_call(0,$me))
132 $result = DB_query("UPDATE Hand SET point_call='0' WHERE hash='$me' ");
134 /* output extra division in case this game is part of a session */
137 echo "<div class=\"session\">\n";
138 echo " <div class=\"sessionrules\">Rules: ";
139 switch($RULES['dullen'])
142 echo " <img class=\"rulesicon\" alt=\"not ten of hearts\" src=\"pics/button/no-ten-of-hearts.png\"/>\n"; break;
144 echo " <img class=\"rulesicon\" alt=\"ten of hearts\" src=\"pics/button/ten-of-hearts.png\"/>\n"; break;
146 echo " <img class=\"rulesicon\" alt=\"second ten of hearts\" src=\"pics/button/second-ten-of-hearts.png\"/>\n"; break;
148 switch($RULES['schweinchen'])
151 echo " <img class=\"rulesicon\" alt=\"no schweinchen\" ".
152 "src=\"pics/button/no-schweinchen.png\"/>\n"; break;
154 echo " <img class=\"rulesicon\" alt=\"two schweinchen \" ".
155 "src=\"pics/button/two-schweinchen.png\"/>\n"; break;
157 echo " <img class=\"rulesicon\" alt=\"second schweinchen\" ".
158 "src=\"pics/button/second-schweinchen.png\"/>\n"; break;
159 case 'secondaftercall':
160 echo " <img class=\"rulesicon\" alt=\"second schweinchen after call\" ".
161 "src=\"pics/button/second-schweinchen-after-call.png\"/>\n"; break;
163 switch($RULES['call'])
166 echo " <img class=\"rulesicon\" alt=\"1st-own-card\" src=\"pics/button/1st-own-card.png\"/>\n"; break;
168 echo " <img class=\"rulesicon\" alt=\"5th-card\" src=\"pics/button/5th-card.png\"/>\n"; break;
170 echo " <img class=\"rulesicon\" alt=\"9-cards\" src=\"pics/button/9-cards.png\"/>\n"; break;
173 echo " 10ofhearts : {$RULES['dullen']} <br />\n";
174 echo " schweinchen: {$RULES['schweinchen']} <br />\n";
175 echo " call: {$RULES['call']} <br />\n";
176 echo " </div>\n </div>\n";
179 $score = generate_score_table($session);
181 $finalscore = array_pop($tmpscore);
182 $finalscore = $finalscore['players'];
184 echo " <div class=\"sessionscore\">Score: \n";
185 foreach($finalscore as $user=>$value)
187 $name = DB_get_name('userid',$user);
188 echo " ".substr($name,0,2).": $value ";
190 echo format_score_table_html($score,$myid);
193 /* figure out which game in a session we are in and link to the previous and next game if possible */
194 $hashes = DB_get_hashes_by_session($session,$myid);
197 foreach($hashes as $hash)
207 $previous = $hashes[$j-2];
215 if(isset($_SESSION['id']) && $_SESSION['id']==$myid)
218 echo "<a href=\"{$INDEX}?action=game&me=$previous\">previous game</a> \n";
219 echo "This is game number $j of <a href=\"{$INDEX}?action=game&me=$lasthash\">$i</a> in session $session.\n";
221 echo " <a href=\"{$INDEX}?action=game&me=$next\">next game</a> \n";
224 echo "This is game number $j of $i in session $session.";
228 /* display the table and the names */
231 /* mystatus gets the player through the different stages of a game.
232 * start: does the player want to play?
233 * init: check for sickness
234 * check: check for return values from init
235 * poverty: handle poverty, wait here until all player have reached this state
236 * display sickness and move on to game
237 * play: game in progress
238 * gameover: are we revisiting a game
243 /* don't ask if user has autosetup set to yest */
245 if($PREF['autosetup']=='yes') $skip = 1;
247 if( !myisset('in') && !$skip)
249 /* asks the player, if he wants to join the game */
250 output_check_want_to_play($me);
255 /* check the result, if player wants to join, got next stage, else cancel game */
256 if(!$skip && $_REQUEST['in'] == 'no' )
258 /* cancel the game */
259 $message = "Hello, \n\n".
260 "the game has been canceled due to the request of one of the players.\n";
262 $userids = DB_get_all_userid_by_gameid($gameid);
263 foreach($userids as $user)
265 $To = DB_get_email('userid',$user);
266 mymail($To,"$EmailName game ".DB_format_gameid($gameid)." canceled",$message);
269 /* delete everything from the dB */
275 /* user wants to join the game */
277 /* move on to the next stage,
278 * no break statement to immediately go to the next stage
281 DB_set_hand_status_by_hash($me,'init');
283 /* check if everyone has reached this stage, send out email */
284 $userids = DB_get_all_userid_by_gameid($gameid);
286 foreach($userids as $user)
288 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
289 if($userstat!='init')
291 /* whos turn is it? */
292 DB_set_player_by_gameid($gameid,$user);
299 /* all done, send out email unless this player is the startplayer */
300 $startplayer = DB_get_startplayer_by_gameid($gameid);
301 if($mypos == $startplayer)
303 /* do nothing, go to next stage */
307 /* email startplayer */
309 $email = DB_get_email('position-gameid',$startplayer,$gameid);
310 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
311 $who = DB_get_userid('email',$email);
312 DB_set_player_by_gameid($gameid,$who);
314 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
315 "Use this link to go the game: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
316 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
323 /* here we ask the player if he is sick */
324 $mycards = DB_get_hand($me);
325 $mycards = mysort($mycards,$gametype);
327 if(!myisset('solo','wedding','poverty','nines') )
329 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
330 echo "\n<ul class=\"tricks\">\n";
331 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
332 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
333 " <div class=\"trick\" id=\"trick0\">\n";
335 for($pos=1;$pos<5;$pos++)
337 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
338 $userid = DB_get_userid('gameid-position',$gameid,$pos);
339 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
341 if($userstatus=='start' || $userstatus=='init')
342 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs <br/ > to decide </div>\n"; /* show this to everyone */
344 if($usersick!=NULL && $pos<=$mypos ) /* only show this for people sitting before the player */
345 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
346 else if($usersick==NULL && $pos<=$mypos)
347 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
349 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
350 /* end displaying sickness */
352 output_check_for_sickness($me,$mycards);
354 echo "<div class=\"mycards\">Your cards are: <br />\n";
355 foreach($mycards as $card)
356 display_card($card,$PREF['cardset']);
363 /* check if someone selected more than one sickness */
365 if($_REQUEST['solo']!='No') $Nsickness++;
366 if($_REQUEST['wedding'] == 'yes') $Nsickness++;
367 if($_REQUEST['poverty'] == 'yes') $Nsickness++;
368 if($_REQUEST['nines'] == 'yes') $Nsickness++;
372 echo "<p class=\"message\"> You selected more than one sickness, please go back ".
373 "and answer the <a href=\"$INDEX?action=game&me=$me&in=yes\">question</a> again.</p>";
375 echo "<div class=\"mycards\">Your cards are: <br />\n";
376 foreach($mycards as $card)
377 display_card($card,$PREF['cardset']);
384 /* everything is ok, save what user said and proceed */
385 echo "<p class=\"message\">Processing what you selected in the last step...";
387 /* check if this sickness needs to be handled first */
388 $gametype = DB_get_gametype_by_gameid($gameid);
389 $startplayer = DB_get_startplayer_by_gameid($gameid); /* need this to check which solo goes first */
391 if( $_REQUEST['solo']!='No' )
393 /* user wants to play a solo */
395 /* store the info in the user's hand info */
396 DB_set_solo_by_hash($me,$_REQUEST['solo']);
397 DB_set_sickness_by_hash($me,'solo');
399 echo "<br />Seems like you want to play a {$_REQUEST['solo']} solo. Got it.<br />\n";
401 if($gametype == 'solo' && $startplayer<$mypos)
402 {}/* do nothing, since someone else already is playing solo */
405 /* this solo comes first
406 * store info in game table
408 DB_set_gametype_by_gameid($gameid,'solo');
409 DB_set_startplayer_by_gameid($gameid,$mypos);
410 DB_set_solo_by_gameid($gameid,$_REQUEST['solo']);
413 else if($_REQUEST['wedding'] == 'yes')
415 /* silent solo is set further down */
416 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
417 DB_set_sickness_by_hash($me,'wedding');
419 else if($_REQUEST['poverty'] == 'yes')
421 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
422 DB_set_sickness_by_hash($me,'poverty');
424 else if($_REQUEST['nines'] == 'yes')
426 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
427 " is playing solo, this game will be canceled.<br />\n";
428 DB_set_sickness_by_hash($me,'nines');
433 /* move on to the next stage*/
434 DB_set_hand_status_by_hash($me,'check');
439 /* here we check what all players said and figure out what game we are playing
440 * this can therefore only be handled once all players finished the last stage
443 $mycards = DB_get_hand($me);
444 $mycards = mysort($mycards,$gametype);
446 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
447 echo "\n<ul class=\"tricks\">\n";
448 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
449 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
450 " <div class=\"trick\" id=\"trick0\">\n";
452 for($pos=1;$pos<5;$pos++)
454 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
455 $userid = DB_get_userid('gameid-position',$gameid,$pos);
456 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
458 if($userstatus=='start' || $userstatus=='init')
459 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs <br />to decide </div>\n"; /* show this to everyone */
461 if($usersick!=NULL) /* in the init-phase we only showed players with $pos<$mypos, now we can show all */
462 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
464 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
466 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
467 /* end displaying sickness */
469 echo "<div class=\"message\">\n";
470 echo "<p> Checking if someone else selected solo, nines, wedding or poverty.</p>";
472 /* check if everyone has reached this stage */
473 $userids = DB_get_all_userid_by_gameid($gameid);
475 foreach($userids as $user)
477 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
478 if($userstat!='check')
481 DB_set_player_by_gameid($gameid,$user);
488 echo "<p>This step can only be handled after everyone finished the last step. ".
489 "Seems like this is not the case, so you need to wait a bit... ".
490 "you will get an email once that is the case, please use the link in ".
491 "that email to continue the game.</p></div>";
493 /* display cards, if player was just at the init-phase he will still see the cards from there
494 * we can put this one here, since the last player to finish the init state won't get here and
495 * will still see his card anyway from the init-phase
497 if($mystatus=='check')
500 echo "<div class=\"mycards\">Your cards are: <br />\n";
501 foreach($mycards as $card)
502 display_card($card,$PREF['cardset']);
509 /* Ok, everyone finished the init-phase, time to figure out what game we
510 * are playing, in case there are any solos this already
511 * will have the correct information in it */
513 echo "<p> Ok, everyone is done... figuring out what kind of game we are playing.</p>";
515 $gametype = DB_get_gametype_by_gameid($gameid);
516 $startplayer = DB_get_startplayer_by_gameid($gameid);
518 /* check for sickness */
523 foreach($userids as $user)
525 $name = DB_get_name('userid',$user);
526 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
527 if($usersick == 'nines')
530 break; /* no need to check for other poverties, since only solo can win and that is already set */
532 else if($usersick == 'poverty')
534 else if($usersick == 'wedding')
536 else if($usersick == 'solo')
540 /* now check which sickness comes first and set the gametype to it */
541 if($gametype == 'solo')
548 /* TODO: should we keep statistics of this? */
549 $message = "Hello, \n\n".
550 " the game has been canceled because ".DB_get_name('userid',$nines).
551 " has five or more nines and nobody is playing solo.\n\n".
552 " To redeal either start a new game or, in case the game was part of a tournament, \n".
553 " go to the last game and use the link at the bottom of the page to redeal.";
555 $userids = DB_get_all_userid_by_gameid($gameid);
556 foreach($userids as $user)
558 $To = DB_get_email('userid',$user);
559 mymail($To,"$EmailName game ".DB_format_gameid($gameid)." canceled",$message);
562 /* delete everything from the dB */
565 echo "The game has been canceled because ".DB_get_name('userid',$nines).
566 " has five or more nines and nobody is playing solo.\n";
569 else if($poverty==1) /* one person has poverty */
571 DB_set_gametype_by_gameid($gameid,'poverty');
572 $gametype = 'poverty';
573 $who = DB_get_sickness_by_gameid($gameid);
576 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
577 if($firstsick == 'poverty')
578 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
580 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
583 else if($poverty==2) /* two people have poverty */
585 DB_set_gametype_by_gameid($gameid,'dpoverty');
586 $gametype = 'dpoverty';
587 $who = DB_get_sickness_by_gameid($gameid);
590 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
591 if($firstsick == 'poverty')
593 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
594 if($secondsick == 'poverty')
595 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
597 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
600 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
605 DB_set_gametype_by_gameid($gameid,'wedding');
606 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
607 $gametype = 'wedding';
609 /* now the gametype is set correctly in the database */
610 echo "<p> Got it :)</p>";
612 /* loop over all players, set re/contra if possible and start the game if possible */
613 $userids = DB_get_all_userid_by_gameid($gameid);
614 foreach($userids as $userid)
616 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
621 /* are we the solo player? set us to re, else set us to contra */
622 $pos = DB_get_pos_by_hash($userhash);
623 if($pos == $startplayer)
624 DB_set_party_by_hash($userhash,'re');
626 DB_set_party_by_hash($userhash,'contra');
627 DB_set_hand_status_by_hash($userhash,'play');
631 /* set person with the wedding to re, do the rest during the game */
632 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
633 if($usersick == 'wedding')
634 DB_set_party_by_hash($userhash,'re');
636 DB_set_party_by_hash($userhash,'contra');
638 DB_set_hand_status_by_hash($userhash,'play');
642 $hand = DB_get_all_hand($userhash);
644 if(in_array('3',$hand)||in_array('4',$hand))
645 DB_set_party_by_hash($userhash,'re');
647 DB_set_party_by_hash($userhash,'contra');
648 DB_set_hand_status_by_hash($userhash,'play');
652 /* set person with poverty to play status */
653 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
654 if($usersick == 'poverty')
655 DB_set_hand_status_by_hash($userhash,'play');
657 /* set status of first player to be asked to poverty */
658 $who = DB_get_sickness_by_gameid($gameid);
659 if($who > 6) $who= $who/10; /* in case we have dpoverty */
660 $whoid = DB_get_userid('gameid-position',$gameid,$who);
662 DB_set_hand_status_by_hash($userhash,'poverty');
665 /* check for silent solo, set game type to solo in this case */
666 $gametype = DB_get_gametype_by_gameid($gameid);
667 $userids = DB_get_all_userid_by_gameid($gameid);
668 foreach($userids as $userid)
670 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
672 if($gametype=='normal')
674 $userhand = DB_get_all_hand($userhash);
675 if(check_wedding($userhand))
677 /* normal game type and player has both queens -> silent solo */
678 /* keep startplayer, just set gametype to silent solo */
679 DB_set_gametype_by_gameid($gameid,'solo');
680 DB_set_solo_by_gameid($gameid,'silent');
685 /* send out email to first player or poverty person*/
686 if($gametype!='poverty' && $gametype!='dpoverty')
688 $startplayer = DB_get_startplayer_by_gameid($gameid);
689 $email = DB_get_email('position-gameid',$startplayer,$gameid);
690 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
691 $who = DB_get_userid('email',$email);
692 DB_set_player_by_gameid($gameid,$who);
696 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
698 /* email startplayer */
699 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
700 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
701 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
705 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
709 /* set status of first player to be asked to poverty */
710 $who = DB_get_sickness_by_gameid($gameid);
711 if($who > 6) $who= $who/10; /* in case we have dpoverty */
713 $whoid = DB_get_userid('gameid-position',$gameid,$who);
715 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
718 $email = DB_get_email('position-gameid',$who,$gameid);
719 $whohash = DB_get_hash_from_game_and_pos($gameid,$who);
720 DB_set_player_by_gameid($gameid,$whoid);
722 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
724 /* email player for poverty */
725 $message = "Poverty: It's your turn now in game ".DB_format_gameid($gameid).".\n".
726 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$whohash."\n\n" ;
727 mymail($email,$EmailName."Poverty (game ".DB_format_gameid($gameid).") ",$message);
735 /* user only gets here in a poverty game, several things have to be handled here:
736 * A) ask, if user wants to take trump
738 * poverty: set re/contra
739 * dpoverty: first time: set re, send email to second player
740 * second time: set contra
741 * poverty: set status of other players to 'play'
742 * set status to play in case 0 trump
743 * no -> set status to play,
744 * ask next player or cancle the game if no more players
745 * B) user took trump and has too many cards (e.g. count(cards)>12 and re/contra set)
746 * ask to give cards back, set status to play, once player has 12 cards
748 * it is easier to check B) first
751 /* output pre game in case user reloads */
754 set_gametype($gametype); /* this sets the $CARDS variable */
755 $myparty = DB_get_party_by_hash($me);
757 /* the following is part B) of whats needs to be done)
758 /* check if user wants to give cards back */
759 if(myisset('exchange'))
761 $exchange = $_REQUEST['exchange'];
762 $partnerhash = DB_get_partner_hash_by_hash($me);
763 $partnerid = DB_get_userid('hash',$partnerhash);
764 $partnerhand = DB_get_handid('gameid-userid',$gameid,$partnerid);
766 /* if exchange is set to a value>0, exchange that card back to the partner */
769 $result = DB_query("UPDATE Hand_Card SET hand_id='$partnerhand'".
770 " WHERE hand_id='$myhand' AND card_id=".DB_quote_smart($exchange));
771 DB_add_exchanged_card(DB_quote_smart($exchange),$myhand,$partnerhand);
776 $mycards = DB_get_hand($me);
777 $mycards = mysort($mycards,$gametype);
779 /* check if user need to give more cards back */
780 if( ($myparty=='re' || $myparty=='contra') && count($mycards)>12)
782 echo "<div class=\"poverty\"> you need to get rid of a few cards</div>\n";
785 echo "<div class=\"mycards\">Your cards are: <br />\n";
786 foreach($mycards as $card)
787 display_link_card($card,$PREF['cardset'],$type);
788 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select card to give back\" />\n";
791 else if( ($myparty=='re' || $myparty=='contra') && count($mycards)==12)
793 /* user is done, ready to play */
794 DB_set_hand_status_by_hash($me,'play');
796 /* email start player */
797 $startplayer = DB_get_startplayer_by_gameid($gameid);
798 $email = DB_get_email('position-gameid',$startplayer,$gameid);
799 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
800 $who = DB_get_userid('email',$email);
801 DB_set_player_by_gameid($gameid,$who);
805 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
807 /* email startplayer */
808 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
809 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
810 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
814 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
817 /* the following is part A) of what needs to be done */
818 if(!myisset('trump'))
822 echo "<div class=\"poverty\">\n";
823 $userids = DB_get_all_userid_by_gameid($gameid);
824 foreach($userids as $user)
826 $name = DB_get_name('userid',$user);
827 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
828 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
829 $userparty = DB_get_party_by_hash($userhash);
831 if($usersick=='poverty' && !$userparty)
833 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
834 $cards = DB_get_hand($hash);
837 foreach($cards as $card)
838 if($card<27) $nrtrump++;
839 echo "Player $name has $nrtrump trump. Do you want to take them?".
840 "<a href=\"index.php?action=game&me=$me&trump=$user\">Yes</a> <br />\n";
843 echo "<a href=\"index.php?action=game&me=$me&trump=no\">No way</a> <br />\n";
844 echo "</div><div>\n";
846 echo "<div class=\"mycards\">Your cards are: <br />\n";
847 foreach($mycards as $card)
848 display_card($card,$PREF['cardset']);
849 echo "</div></div>\n";
855 $trump = $_REQUEST['trump'];
859 /* user doesn't want to take trump */
860 DB_set_hand_status_by_hash($me,'play');
862 /* set next player who needs to be asked and email him*/
863 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
864 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
866 /* don't ask people who have poverty */
868 if($firstsick=='poverty')
870 if($secondsick=='poverty')
875 if($gametype=='dpoverty')
877 $next=999; /* need to cancel for sure, since both would need to take the trump */
880 /* no more people to ask, need to cancel the game */
883 $message = "Hello, \n\n".
884 "Game ".DB_format_gameid($gameid)." has been canceled since nobody wanted to take the trump.\n";
886 $userids = DB_get_all_userid_by_gameid($gameid);
887 foreach($userids as $user)
889 $To = DB_get_email('userid',$user);
890 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled (poverty not resolved)",$message);
893 /* delete everything from the dB */
896 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been canceled.<br /><br /></p>";
901 /* email next player, set his status to poverty */
902 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
903 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
904 $userid = DB_get_userid('email',$To);
906 DB_set_player_by_gameid($gameid,$userid);
907 DB_set_hand_status_by_hash($userhash,'poverty');
909 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
910 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
911 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
916 /* player wants to take trump, change cards */
918 /* user wants to take trump */
919 $trump = $_REQUEST['trump'];
920 $userhand = DB_get_handid('gameid-userid',$gameid,$trump);
921 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
923 /* remember which cards were handed over*/
924 $partnerhand = DB_get_all_hand($userhash);
925 foreach ($partnerhand as $card)
927 DB_add_exchanged_card($card,$userhand,$myhand);
929 /* copy trump from player A to B */
930 $result = DB_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
933 $mycards = DB_get_hand($me);
936 if($gametype=='poverty')
938 $userids = DB_get_all_userid_by_gameid($gameid);
939 foreach($userids as $user)
941 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
942 if($hash==$userhash||$hash==$me)
944 DB_set_party_by_hash($hash,'re');
948 DB_set_party_by_hash($hash,'contra');
949 DB_set_hand_status_by_hash($hash,'play'); /* the contra party is ready to play */
952 /* check if we are done (in case of no trump handed over), if so, go to 'play' phase right away*/
953 if(count($mycards)==12)
955 DB_set_hand_status_by_hash($me,'play');
960 /* has the re party already been set?*/
962 $userids = DB_get_all_userid_by_gameid($gameid);
963 foreach($userids as $user)
965 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
966 $party = DB_get_party_by_hash($hash);
972 DB_set_party_by_hash($me,'contra');
973 DB_set_party_by_hash($userhash,'contra');
977 DB_set_party_by_hash($me,'re');
978 DB_set_party_by_hash($userhash,'re');
980 /* send out email to second non-poverty player */
981 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
982 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
985 if($firstsick=='poverty')
986 if($secondsick=='poverty')
992 echo "<div class=\"message\">Error in poverty, please contact the Admin</div>\n";
994 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
995 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
996 $userid = DB_get_userid('email',$To);
998 DB_set_player_by_gameid($gameid,$userid);
999 DB_set_hand_status_by_hash($userhash,'poverty');
1001 $message = "Two people have poverty, it's your turn to decide, if you want to take the trump. Please visit:".
1002 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
1003 mymail($To,$EmailName." double poverty (game ".DB_format_gameid($gameid).")",$message);
1008 echo "<div class=\"message\"> Please, <a href=\"$INDEX?action=game&me=$me\">continue</a> here.</div>\n";
1016 /* both entries here, so that the tricks are visible for both.
1017 * in case of 'play' there is a break later that skips the last part
1020 /* check if all players are ready to play,
1021 * if so, send out email to the startplayer
1022 * only need to do this if the game hasn't started yet
1024 $gamestatus = DB_get_game_status_by_gameid($gameid);
1025 if($gamestatus == 'pre')
1028 $userids = DB_get_all_userid_by_gameid($gameid);
1029 foreach($userids as $user)
1031 $userstatus = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
1032 if($userstatus !='play' && $userstatus!='gameover')
1035 DB_set_player_by_gameid($gameid,$user);
1041 /* only set this after all poverty, etc. are handled*/
1042 DB_set_game_status_by_gameid($gameid,'play');
1044 /* email startplayer */
1045 $startplayer = DB_get_startplayer_by_gameid($gameid);
1046 $email = DB_get_email('position-gameid',$startplayer,$gameid);
1047 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
1048 $who = DB_get_userid('email',$email);
1049 DB_set_player_by_gameid($gameid,$who);
1051 if($hash!=$me && DB_get_email_pref_by_hash($hash)!='emailaddict')
1053 /* email startplayer) */
1054 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
1055 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
1056 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
1060 /* figure out what kind of game we are playing,
1061 * set the global variables $CARDS['trump'],$CARDS['diamonds'],$CARDS['hearts'],
1062 * $CARDS['clubs'],$CARDS['spades'],$CARDS['foxes']
1066 $gametype = DB_get_gametype_by_gameid($gameid);
1068 if($gametype=='solo')
1070 $gametype = DB_get_solo_by_gameid($gameid);
1071 if($gametype=='silent')
1074 $GT = $gametype.' '.$GT;
1077 $gametype = 'normal';
1079 set_gametype($gametype); /* this sets the $CARDS variable */
1081 /* get some infos about the game, need to reset this, since it might have changed */
1082 $gamestatus = DB_get_game_status_by_gameid($gameid);
1084 /* has the game started? No, then just wait here...*/
1085 if($gamestatus == 'pre')
1087 echo "<p class=\"message\"> You finished the setup, but not everyone else finished it... ".
1088 "You need to wait for the others. Just wait for an email. </p>";
1090 $mycards = DB_get_hand($me);
1091 $mycards = mysort($mycards,$gametype);
1093 echo "<div class=\"mycards\">Your cards are: <br />\n";
1094 foreach($mycards as $card)
1095 display_card($card,$PREF['cardset']);
1098 break; /* not sure this works... the idea is that you can
1099 * only play a card after everyone is ready to play */
1102 /* get time from the last action of the game */
1103 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1104 $gameend = time() - strtotime($r[0]);
1106 /* handle comments in case player didn't play a card, allow comments a week after the end of the game */
1107 if( (!myisset('card') && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
1108 if(myisset('comment'))
1110 $comment = $_REQUEST['comment'];
1111 $playid = DB_get_current_playid($gameid);
1114 DB_insert_comment($comment,$playid,$myid);
1117 /* get everything relevant to display the tricks */
1118 $result = DB_query("SELECT Hand_Card.card_id as card,".
1119 " Hand.position as position,".
1120 " Play.sequence as sequence, ".
1122 " GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
1123 " SEPARATOR '\n' ), ".
1124 " Play.create_date, ".
1127 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1128 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1129 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1130 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1131 "LEFT JOIN User On User.id=Comment.user_id ".
1132 "WHERE Trick.game_id='".$gameid."' ".
1133 "GROUP BY Trick.id, sequence ".
1134 "ORDER BY Trick.id, sequence ASC");
1136 $lasttrick = DB_get_max_trickid($gameid);
1138 $play = array(); /* needed to calculate winner later */
1140 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1141 $firstcard = ''; /* first card in a trick */
1143 echo "\n<ul class=\"tricks\">\n";
1144 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
1146 /* output vorbehalte */
1147 $mygametype = DB_get_gametype_by_gameid($gameid);
1148 if($mygametype != 'normal' && $mygametype != 'silent') /* only show when needed */
1150 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
1151 " <div class=\"trick\" id=\"trick0\">\n";
1152 /* get information so show the cards that have been handed over in a poverty game */
1157 if($mygametype == 'poverty' || $mygametype=='dpoverty')
1159 /* who has poverty */
1160 for($mypos=1;$mypos<5;$mypos++)
1162 $usersick = DB_get_sickness_by_pos_and_gameid($mypos,$gameid);
1163 if($usersick == 'poverty')
1165 $povertypos2 = $mypos;
1167 $povertypos1 = $mypos;
1169 /* get hash and cards for all */
1170 $povertyhash1 = DB_get_hash_from_game_and_pos($gameid,$povertypos1);
1171 $partnerhash1 = DB_get_partner_hash_by_hash($povertyhash1);
1173 $povertycards1 = DB_get_exchanged_cards($povertyhash1);
1174 $partnercards1 = DB_get_exchanged_cards($partnerhash1);
1176 $partnerpos1 = DB_get_pos_by_hash($partnerhash1);
1179 $povertyhash2 = DB_get_hash_from_game_and_pos($gameid,$povertypos2);
1180 $partnerhash2 = DB_get_partner_hash_by_hash($povertyhash2);
1182 $povertycards2 = DB_get_exchanged_cards($povertyhash2);
1183 $partnercards2 = DB_get_exchanged_cards($partnerhash2);
1185 $partnerpos2 = DB_get_pos_by_hash($partnerhash2);
1190 for($mypos=1;$mypos<5;$mypos++)
1192 $usersick = DB_get_sickness_by_pos_and_gameid($mypos,$gameid);
1193 if($usersick!=NULL ||
1194 $mypos==$povertypos1 || $mypos==$partnerpos1 ||
1195 $mypos==$povertypos2 || $mypos==$partnerpos2 )
1197 echo " <div class=\"vorbehalt".($mypos-1)."\"> Vorbehalt <br />";
1199 echo " $usersick <br />";
1200 if($mypos==$partnerpos1)
1202 foreach($partnercards1 as $card)
1203 if($povertyhash1 == $me || $partnerhash1 == $me || $mystatus=='gameover')
1204 display_card($card,$PREF['cardset']);
1206 display_card(0,$PREF['cardset']);
1208 else if($mypos==$povertypos1)
1210 foreach($povertycards1 as $card)
1211 if($povertyhash1 == $me || $partnerhash1 == $me || $mystatus=='gameover')
1212 display_card($card,$PREF['cardset']);
1214 display_card(0,$PREF['cardset']);
1216 else if($mypos==$povertypos2)
1218 foreach($povertycards2 as $card)
1219 if($povertyhash2 == $me || $partnerhash2 == $me || $mystatus=='gameover')
1220 display_card($card,$PREF['cardset']);
1222 display_card(0,$PREF['cardset']);
1224 else if($mypos==$partnerpos2)
1226 foreach($partnercards2 as $card)
1227 if($povertyhash2 == $me || $partnerhash2 == $me || $mystatus=='gameover')
1228 display_card($card,$PREF['cardset']);
1230 display_card(0,$PREF['cardset']);
1235 if($mygametype == $usersick)
1239 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1243 while($r = DB_fetch_array($result))
1251 /* count number of tricks */
1255 /* check if first schweinchen has been played */
1256 if( $GAME['schweinchen-who'] && ($r[0] == 19 || $r[0] == 20) )
1257 if(!$GAME['schweinchen-first'])
1258 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1260 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1262 /* save card to be able to find the winner of the trick later */
1263 $play[$seq] = array('card'=>$r[0],'pos'=>$pos);
1267 /* first card in a trick, output some html */
1268 if($trick!=$lasttrick)
1270 /* start of an old trick? */
1271 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1272 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1273 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1275 else if($trick==$lasttrick)
1277 /* start of a last trick? */
1278 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1279 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1280 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1283 /* remember first card, so that we are able to check, what cards can be played */
1288 echo " <div class=\"card".($pos-1)."\">\n";
1290 /* display comments */
1292 echo " <span class=\"comment\">".$comment."</span>\n";
1295 display_card($r[0],$PREF['cardset']);
1297 echo " </div>\n"; /* end div card */
1302 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1306 /* whos turn is it? */
1309 $winner = get_winner($play,$gametype); /* returns the position */
1311 $firstcard = ''; /* new trick, no first card */
1316 if($next==5) $next = 1;
1319 /* my turn?, display cards as links, ask for comments*/
1320 if(DB_get_pos_by_hash($me) == $next)
1325 /* do we want to play a card? */
1326 if(myisset('card') && $myturn)
1328 $card = $_REQUEST['card'];
1329 $handid = DB_get_handid('hash',$me);
1330 $commentSchweinchen =''; /* used to add a comment when Schweinchen is being played */
1332 /* check if we have card and that we haven't played it yet*/
1333 /* set played in hand_card to true where hand_id and card_id*/
1334 $r = DB_query_array("SELECT id FROM Hand_Card WHERE played='false' and ".
1335 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1336 $handcardid = $r[0];
1338 if($handcardid) /* everything ok, play card */
1340 /* update Game timestamp */
1341 DB_update_game_timestamp($gameid);
1343 /* mark card as played */
1344 DB_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1345 DB_quote_smart($card));
1347 /* get trick id or start new trick */
1348 $a = DB_get_current_trickid($gameid);
1353 $playid = DB_play_card($trickid,$handcardid,$sequence);
1355 /* check special output for schweinchen in case in case a fox is being played
1356 * check for correct rules, etc. has already been done
1358 if( $GAME['schweinchen-who'] && ($card == 19 || $card == 20) )
1360 if(!$GAME['schweinchen-first'])
1361 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1363 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1365 if( $RULES['schweinchen']=='both' ||
1366 ($RULES['schweinchen']=='second' && $GAME['schweinchen-second']==1 )||
1367 ($RULES['schweinchen']=='secondaftercall' && $GAME['schweinchen-second']==1 &&
1368 (DB_get_call_by_hash($GAME['schweinchen-who']) || DB_get_partner_call_by_hash($GAME['schweinchen-who']) ))
1371 DB_insert_comment('Schweinchen! ',$playid,$myid);
1372 $commentSchweinchen = 'Schweinchen! ';
1375 echo 'schweinchen = '.$GAME['schweinchen-who'].' ---<br />';
1378 /* if sequence == 4 check who one in case of wedding */
1379 if($sequence == 4 && $GT == 'wedding')
1381 /* is wedding resolve */
1382 $resolved = DB_get_sickness_by_gameid($gameid);
1385 /* who has wedding */
1386 $userids = DB_get_all_userid_by_gameid($gameid);
1387 foreach($userids as $user)
1389 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1390 if($usersick == 'wedding')
1393 /* who won the trick */
1394 $play = DB_get_cards_by_trick($trickid);
1395 $winner = get_winner($play,$gametype); /* returns the position */
1396 $winnerid = DB_get_userid('gameid-position',$gameid,$winner);
1397 /* is tricknr <=3 */
1398 if($tricknr <=3 && $winnerid!=$whosick)
1400 /* set resolved at tricknr*/
1401 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1403 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1404 DB_set_party_by_hash($whash,'re');
1406 if($tricknr == 3 && $winnerid==$whosick)
1408 /* set resolved at tricknr*/
1409 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1414 /* if sequence == 4, set winner of the trick, count points and set the next player */
1417 $play = DB_get_cards_by_trick($trickid);
1418 $winner = get_winner($play,$gametype); /* returns the position */
1420 /* check if someone caught a fox */
1421 /* first check if we should account for solos at all,
1422 * since it doesn't make sense in some games
1424 $ok = 0; /* fox shouldn't be counted */
1425 if(DB_get_gametype_by_gameid($gameid)=='solo')
1427 $solo = DB_get_solo_by_gameid($gameid);
1428 if($solo == 'trump' || $solo == 'silent')
1429 $ok = 1; /* for trump solos and silent solos, foxes are ok */
1432 $ok = 1; /* for all other games (not solos) foxes are ok too */
1435 foreach($play as $played)
1437 if ( $played['card']==19 || $played['card']==20 )
1438 if ($played['pos']!= $winner )
1440 /* possible caught a fox, check party */
1441 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1442 $uid2 = DB_get_userid('gameid-position',$gameid,$played['pos']);
1444 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1445 $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1447 if($party1 != $party2)
1448 DB_query("INSERT INTO Score".
1449 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1453 /* check for karlchen (jack of clubs in the last trick)*/
1454 /* same as for foxes, karlchen doesn't always make sense
1455 * check what kind of game it is and set karlchen accordingly */
1456 $ok = 1; /* default: karlchen should be accounted for */
1458 $ok = 0; /* Karlchen works only in the last trick */
1459 if($ok && DB_get_gametype_by_gameid($gameid)=='solo' )
1461 $solo = DB_get_solo_by_gameid($gameid);
1462 if($solo == 'trumpless' || $solo == 'jack' || $solo == 'queen' )
1463 $ok = 0; /* no Karlchen in these solos */
1467 foreach($play as $played)
1468 if ( $played['card']==11 || $played['card']==12 )
1469 if ($played['pos'] == $winner )
1471 /* possible caught a fox, check party */
1472 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1473 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1475 DB_query("INSERT INTO Score".
1476 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1478 /* check for doppelopf (>40 points)*/
1480 foreach($play as $played)
1482 $points += DB_get_card_value_by_cardid($played['card']);
1486 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1487 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1489 DB_query("INSERT INTO Score".
1490 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
1494 DB_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1496 echo "ERROR during scoring";
1499 echo "DEBUG: position $winner won the trick <br />";
1501 /* who is the next player? */
1506 $next = DB_get_pos_by_hash($me)+1;
1508 if($next==5) $next=1;
1510 /* check for coment */
1511 if(myisset('comment'))
1513 $comment = $_REQUEST['comment'];
1515 DB_insert_comment($comment,$playid,$myid);
1516 if($commentSchweinchen)
1517 $comment = $commentSchweinchen . $comment;
1520 /* display played card */
1521 $pos = DB_get_pos_by_hash($me);
1524 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1525 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1526 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1529 echo " <div class=\"card".($pos-1)."\">\n ";
1531 /* display comments */
1532 display_card($card,$PREF['cardset']);
1534 echo "\n <span class=\"comment\"> ".$comment."</span>\n";
1537 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1539 /*check if we still have cards left, else set status to gameover */
1540 if(sizeof(DB_get_hand($me))==0)
1542 DB_set_hand_status_by_hash($me,'gameover');
1543 $mystatus = 'gameover';
1546 /* if all players are done, set game status to game over,
1547 * get the points of the last trick and send out an email
1550 $userids = DB_get_all_userid_by_gameid($gameid);
1553 foreach($userids as $user)
1554 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1558 DB_set_game_status_by_gameid($gameid,'gameover');
1560 /* email next player, if game is still running */
1561 if(DB_get_game_status_by_gameid($gameid)=='play')
1563 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1564 $email = DB_get_email('hash',$next_hash);
1565 $who = DB_get_userid('email',$email);
1566 DB_set_player_by_gameid($gameid,$who);
1568 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1569 "It's your turn now.\n".
1570 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$next_hash."\n\n" ;
1571 if( DB_get_email_pref_by_uid($who)!='emailaddict' )
1572 mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
1574 else /* send out final email */
1576 /* individual score */
1577 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1578 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1579 " LEFT JOIN User ON User.id=Hand.user_id".
1580 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1581 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1582 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1583 " WHERE Hand.game_id='$gameid'".
1584 " GROUP BY User.fullname" );
1585 $message = "The game is over. Thanks for playing :)\n";
1586 $message .= "Final score:\n";
1587 while( $r = DB_fetch_array($result) )
1588 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1590 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1591 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1592 " LEFT JOIN User ON User.id=Hand.user_id".
1593 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1594 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1595 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1596 " WHERE Hand.game_id='$gameid'".
1597 " GROUP BY Hand.party" );
1598 $message .= "\nTotals:\n";
1601 while( $r = DB_fetch_array($result) )
1603 $message .= " ".$r[0]." ".$r[1]."\n";
1606 else if($r[0] == 'contra')
1611 * save score in database
1615 /* get calls from re/contra */
1617 $call_contra = NULL;
1618 foreach($userids as $user)
1620 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1621 $call = DB_get_call_by_hash($hash);
1622 $party = DB_get_party_by_hash($hash);
1626 $call = (int) $call;
1632 else if( $call < $call_re)
1635 else if($party=='contra')
1637 if($call_contra==NULL)
1638 $call_contra = $call;
1639 else if( $call < $call_contra)
1640 $call_contra = $call;
1645 /* figure out who one */
1646 $winning_party = NULL;
1648 if($call_re == NULL && $call_contra==NULL)
1650 /* nobody made a call, so it's easy to figure out who won */
1652 $winning_party='re';
1654 $winning_party='contra';
1658 /* if one party makes a call, they only win, iff they make enough points
1659 * if only one party made a call, the other one wins,
1660 * if the first one didn't make it
1664 $offset = 120 - $call_re;
1666 $offset--; /* since we use a > in the next equation */
1668 if($re > 120+$offset)
1669 $winning_party='re';
1670 else if ($call_contra == NULL )
1671 $winning_party='contra';
1676 $offset = 120 - $call_contra;
1677 if($call_contra == 0)
1678 $offset--; /* since we use a > in the next equation */
1680 if($contra > 120+$offset)
1681 $winning_party='contra';
1682 else if ($call_re == NULL )
1683 $winning_party='re';
1687 /* one point for each call of the other party in case the other party didn't win
1688 * and one point each in case the party made more than points than one of the calls
1690 if($winning_party!='contra' && $call_contra!=NULL)
1692 for( $p=$call_contra;$p<=120; $p+=30 )
1694 DB_query("INSERT INTO Score".
1695 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
1698 for( $p=$call_contra; $p<120; $p+=30)
1701 DB_query("INSERT INTO Score".
1702 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'made$p')");
1705 if($winning_party!='re' and $call_re!=NULL)
1707 for( $p=$call_re;$p<=120; $p+=30 )
1709 DB_query("INSERT INTO Score".
1710 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'against$p')");
1713 for( $p=$call_re; $p<120; $p+=30)
1716 DB_query("INSERT INTO Score".
1717 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
1721 /* point in case contra won */
1722 if($winning_party=='contra')
1724 DB_query("INSERT INTO Score".
1725 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1728 /* one point each for winning and each 30 points + calls */
1729 if($winning_party=='re')
1731 foreach(array(120,150,180,210,240) as $p)
1734 if($p==240 || $call_contra!=NULL)
1738 DB_query("INSERT INTO Score".
1739 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
1741 /* re called something and won */
1742 foreach(array(0,30,60,90,120) as $p)
1744 if($call_re!=NULL && $call_re<$p+1)
1745 DB_query("INSERT INTO Score".
1746 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
1749 else if( $winning_party=='contra')
1751 foreach(array(120,150,180,210,240) as $p)
1754 if($p==240 || $call_re!=NULL)
1757 if($contra>$p-$offset)
1758 DB_query("INSERT INTO Score".
1759 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
1761 /* re called something and won */
1762 foreach(array(0,30,60,90,120) as $p)
1764 if($call_contra!=NULL && $call_contra<$p+1)
1765 DB_query("INSERT INTO Score".
1766 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
1771 /* add score points to email */
1774 $message .= " Points Re: \n";
1775 $queryresult = DB_query("SELECT score FROM Score ".
1776 " WHERE game_id=$gameid AND party='re'".
1778 while($r = DB_fetch_array($queryresult) )
1780 $message .= " ".$r[0]."\n";
1783 $message .= " Points Contra: \n";
1784 $queryresult = DB_query("SELECT score FROM Score ".
1785 " WHERE game_id=$gameid AND party='contra'".
1787 while($r = DB_fetch_array($queryresult) )
1789 $message .= " ".$r[0]."\n";
1792 $message .= " Total Points (from the Re point of view): $Tpoint\n";
1795 $session = DB_get_session_by_gameid($gameid);
1796 $score = generate_score_table($session);
1798 $message .= "Score Table:\n";
1799 $message .= format_score_table_ascii($score);
1800 $message .= "\nUse these links to have a look at game ".DB_format_gameid($gameid).": \n";
1802 /* send out final email */
1805 foreach($userids as $user)
1807 $all[] = DB_get_email('userid',$user);
1809 /* add links for all players */
1810 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1811 $name = DB_get_name('userid',$user);
1813 $link = "$name: ".$HOST.$INDEX."?action=game&me=".$hash."\n" ;
1816 $To = implode(",",$all);
1818 $message .= "\n\n (you can use reply all on this email to reach all the players.)\n";
1819 mymail($To,$EmailName."Game over (game ".DB_format_gameid($gameid).") ",$message);
1824 echo "can't find that card?! <br />\n";
1827 else if(myisset('card') && !$myturn )
1829 echo "please wait until it's your turn! <br />\n";
1832 if($seq!=4 && $trickNR>=1 && !(myisset('card') && $myturn) )
1833 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1835 /* display points in case game is over */
1836 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1838 echo " <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1839 " <div class=\"trick\" id=\"trick13\">\n";
1840 /* add pic for re/contra
1841 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1843 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1844 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1845 " LEFT JOIN User ON User.id=Hand.user_id".
1846 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1847 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1848 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1849 " WHERE Hand.game_id='$gameid'".
1850 " GROUP BY User.fullname" );
1851 while( $r = DB_fetch_array($result))
1852 echo " <div class=\"card".($r[3]-1)."\">\n".
1853 " <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
1856 /* display totals */
1857 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1858 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1859 " LEFT JOIN User ON User.id=Hand.user_id".
1860 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1861 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1862 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1863 " WHERE Hand.game_id='$gameid'".
1864 " GROUP BY Hand.party" );
1865 echo "<div class=\"total\">\n Totals:<br />\n";
1866 while( $r = DB_fetch_array($result))
1867 echo " ".$r[0]." ".$r[1]."<br />\n";
1869 $queryresult = DB_query("SELECT timediff(mod_date,create_date) ".
1870 " FROM Game WHERE id='$gameid'");
1871 $r = DB_fetch_array($queryresult);
1872 echo " <p>This game took ".$r[0]." hours.</p>\n";
1874 echo " <div class=\"re\">\n Points Re: <br />\n";
1875 $queryresult = DB_query("SELECT score FROM Score ".
1876 " WHERE game_id=$gameid AND party='re'".
1878 while($r = DB_fetch_array($queryresult) )
1879 echo " ".$r[0]."<br />\n";
1882 echo " <div class=\"contra\">\n Points Contra: <br />\n";
1883 $queryresult = DB_query("SELECT score FROM Score ".
1884 " WHERE game_id=$gameid AND party='contra'".
1886 while($r = DB_fetch_array($queryresult) )
1887 echo " ".$r[0]."<br />\n";
1892 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1895 echo " <li onclick=\"hl_prev();\" class=\"old\"><a href=\"#\">prev</a></li>\n";
1896 echo " <li onclick=\"hl_next();\" class=\"old\"><a href=\"#\">next</a></li>\n";
1897 echo "</ul>\n"; /* end ul tricks*/
1899 $mycards = DB_get_hand($me);
1900 $mycards = mysort($mycards,$gametype);
1901 echo "<div class=\"mycards\">\n";
1903 if($myturn && !myisset('card') && $mystatus=='play' )
1905 echo "Hello ".$myname.", it's your turn! <br />\n";
1906 echo "Your cards are: <br />\n";
1908 /* do we have to follow suite? */
1910 if(have_suit($mycards,$firstcard))
1913 foreach($mycards as $card)
1915 /* display only cards that the player is allowed to play as links, the rest just display normal
1916 * also check if we have both schweinchen, in that case only display on of them as playable
1918 if( ($followsuit && !same_type($card,$firstcard)) ||
1919 ( (int)($card)==19 && !$GAME['schweinchen-first'] &&
1920 ($RULES['schweinchen']=='second'||
1921 ( $RULES['schweinchen']=='secondaftercall' &&
1922 (DB_get_call_by_hash($GAME['schweinchen-who']) || DB_get_partner_call_by_hash($GAME['schweinchen-who']) )))
1923 && $GAME['schweinchen-who']==$me )
1925 display_card($card,$PREF['cardset']);
1927 display_link_card($card,$PREF['cardset']);
1930 else if($mystatus=='play' )
1932 echo "Your cards are: <br />\n";
1933 foreach($mycards as $card)
1934 display_card($card,$PREF['cardset']);
1936 else if($mystatus=='gameover')
1938 $oldcards = DB_get_all_hand($me);
1939 $oldcards = mysort($oldcards,$gametype);
1940 echo "Your cards were: <br />\n";
1941 foreach($oldcards as $card)
1942 display_card($card,$PREF['cardset']);
1944 $userids = DB_get_all_userid_by_gameid($gameid);
1945 foreach($userids as $user)
1947 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1953 $name = DB_get_name('userid',$user);
1954 $oldcards = DB_get_all_hand($userhash);
1955 $oldcards = mysort($oldcards,$gametype);
1956 echo "$name's cards were: <br />\n";
1957 foreach($oldcards as $card)
1958 display_card($card,$PREF['cardset']);
1964 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1965 if($mystatus=='play')
1968 /* the following happens only when the gamestatus is 'gameover' */
1969 /* check if game is over, display results */
1970 if(DB_get_game_status_by_gameid($gameid)=='play')
1972 echo "The game is over for you.. other people still need to play though";
1976 myerror("error in testing the status");
1979 /* output other games where it is the users turn
1980 * make sure that the people looking at old games don't see the wrong games here
1982 if( $mystatus != 'gameover' )
1983 display_user_menu($myid);
1984 else if( $mystatus == 'gameover' &&
1985 isset($_SESSION['id']) )
1987 display_user_menu($_SESSION['id']);
1991 echo "<div class=\"usermenu\">\n";
1992 echo "It's your turn in these games:<br />\n";
1993 echo "Please log in to see this information.\n";
1997 /* display rule set for this game */
1998 echo "<div class=\"gameinfo\">\n";
2000 if($gamestatus == 'play' )
2002 $myparty = DB_get_party_by_hash($me);
2003 output_form_calls($me,$myparty);
2005 /* get time from the last action of the game */
2006 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
2007 $gameend = time() - strtotime($r[0]);
2009 if($gamestatus == 'play' || $gameend < 60*60*24*7)
2011 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
2014 echo "<input type=\"submit\" value=\"submit\" />\n";
2016 /* has this hand been played by others? */
2017 $other_game_ids = DB_played_by_others($gameid);
2018 if(sizeof($other_game_ids)>0 && $mystatus=='gameover')
2020 $mypos = DB_get_pos_by_hash($me);
2021 echo "<p>See how other played the same hand: <br />\n";
2022 foreach($other_game_ids as $id)
2024 $otherhash = DB_get_hash_from_game_and_pos($id,$mypos);
2025 $othername = DB_get_name('hash',$otherhash);
2026 echo "<a href=\"$INDEX?action=game&me=$otherhash\">$othername</a><br />";
2035 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' && isset($_SESSION['id']) && $_SESSION['id']==$myid)
2037 $session = DB_get_session_by_gameid($gameid);
2038 $result = DB_query("SELECT id,create_date FROM Game".
2039 " WHERE session=$session".
2040 " ORDER BY create_date DESC".
2044 $r = DB_fetch_array($result);
2046 if(!$session || $gameid==$r[0])
2048 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
2049 $names = DB_get_all_names_by_gameid($gameid);
2050 $type = DB_get_gametype_by_gameid($gameid);
2054 $solo = DB_get_solo_by_gameid($gameid);
2056 if($solo!='silent') /* repeat game with same first player */
2057 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
2058 else /* rotate normally */
2059 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
2061 else /* rotate normally */
2062 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);