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 DB_update_user_timestamp($myid);
30 /* get some information from the DB */
31 $gameid = DB_get_gameid_by_hash($me);
32 $myname = DB_get_name('hash',$me);
33 $mystatus = DB_get_status_by_hash($me);
34 $mypos = DB_get_pos_by_hash($me);
35 $myhand = DB_get_handid('hash',$me);
36 $session = DB_get_session_by_gameid($gameid);
38 /* get prefs and save them in a variable*/
39 $PREF = DB_get_PREF($myid);
41 /* get rule set for this game */
42 $RULES = DB_get_RULES($gameid);
44 /* get some infos about the game */
45 $gametype = DB_get_gametype_by_gameid($gameid);
46 $gamestatus = DB_get_game_status_by_gameid($gameid);
50 $gametype = DB_get_solo_by_gameid($gameid);
51 $GT = $gametype.' '.$GT;
54 /* do we need to worry about Schweinchen?
55 * check gametype and rules
56 * if yes, figure out if someone actually has Schweinchen
57 * save information in $GAME
60 if( $gamestatus == 'pre' )
62 /* always need to use Schweinchen to figure out for example who has poverty */
67 /* in a game Schweinchen is not valid in all types of games */
68 if( $gametype == 'normal' || $gametype == 'silent' || $gametype=='trump' || $gametype=='wedding')
69 if( in_array($RULES['schweinchen'],array('both','second','secondaftercall')) )
73 /* these are the defaults */
74 $GAME['schweinchen-who'] = NULL;
75 $GAME['schweinchen-first'] = NULL;
76 $GAME['schweinchen-second'] = NULL;
80 /* need to check for Schweinchen */
83 $hash = DB_get_hash_from_game_and_pos($gameid,$i);
84 $cards = DB_get_all_hand($hash);
85 if( in_array('19',$cards) && in_array('20',$cards) )
86 $GAME['schweinchen-who']=$hash;
88 $GAME['schweinchen-first'] = 0; /* to keep track if they have been played already */
89 $GAME['schweinchen-second'] = 0;
91 /* end check for Schweinchen */
93 /* set the $CARDS variable, needed for sorting the cards
94 * we set it to normal so that the pre-game phase is handled ok
95 * and later set it to the correct game type that is played
97 set_gametype('normal');
99 /* put everyting in a form */
100 echo "<form action=\"index.php?action=game&me=$me\" method=\"post\">\n";
102 /* handle user notes (only possible while game is running)*/
103 if( $mystatus!='gameover' )
106 $note = $_REQUEST['note'];
109 DB_insert_note($note,$gameid,$myid);
111 output_user_notes($myid,$gameid,$mystatus);
114 if(myisset('call') && $_REQUEST['call'] == '120' && can_call(120,$me))
115 $result = DB_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
116 if(myisset('call') && $_REQUEST['call'] == '90' && can_call(90,$me))
117 $result = DB_query("UPDATE Hand SET point_call='90' WHERE hash='$me' ");
118 if(myisset('call') && $_REQUEST['call'] == '60' && can_call(60,$me))
119 $result = DB_query("UPDATE Hand SET point_call='60' WHERE hash='$me' ");
120 if(myisset('call') && $_REQUEST['call'] == '30' && can_call(30,$me))
121 $result = DB_query("UPDATE Hand SET point_call='30' WHERE hash='$me' ");
122 if(myisset('call') && $_REQUEST['call'] == '0' && can_call(0,$me))
123 $result = DB_query("UPDATE Hand SET point_call='0' WHERE hash='$me' ");
125 /* output extra division in case this game is part of a session */
128 echo "<div class=\"session\">\n";
129 echo " <div class=\"sessionrules\">Rules: ";
130 switch($RULES['dullen'])
133 echo "<img class=\"rulesicon\" alt=\"not ten of hearts\" src=\"pics/button/no-ten-of-hearts.png\"/>\n"; break;
135 echo "<img class=\"rulesicon\" alt=\"ten of hearts\" src=\"pics/button/ten-of-hearts.png\"/>\n"; break;
137 echo "<img class=\"rulesicon\" alt=\"second ten of hearts\" src=\"pics/button/second-ten-of-hearts.png\"/>\n"; break;
139 switch($RULES['schweinchen'])
142 echo "<img class=\"rulesicon\" alt=\"no schweinchen\" ".
143 "src=\"pics/button/no-schweinchen.png\"/>\n"; break;
145 echo "<img class=\"rulesicon\" alt=\"two schweinchen \" ".
146 "src=\"pics/button/two-schweinchen.png\"/>\n"; break;
148 echo "<img class=\"rulesicon\" alt=\"second schweinchen\" ".
149 "src=\"pics/button/second-schweinchen.png\"/>\n"; break;
150 case 'secondaftercall':
151 echo "<img class=\"rulesicon\" alt=\"second schweinchen after call\" ".
152 "src=\"pics/button/second-schweinchen-after-call.png\"/>\n"; break;
154 switch($RULES['call'])
157 echo "<img class=\"rulesicon\" alt=\"1st-own-card\" src=\"pics/button/1st-own-card.png\"/>\n"; break;
159 echo "<img class=\"rulesicon\" alt=\"5th-card\" src=\"pics/button/5th-card.png\"/>\n"; break;
161 echo "<img class=\"rulesicon\" alt=\"9-cards\" src=\"pics/button/9-cards.png\"/>\n"; break;
164 echo " 10ofhearts : {$RULES['dullen']} <br />\n";
165 echo " schweinchen: {$RULES['schweinchen']} <br />\n";
166 echo " call: {$RULES['call']} <br />\n";
167 echo " </div>\n </div>\n";
168 echo " <div class=\"sessionscore\">Score \n";
169 $score = generate_score_table($session);
170 echo format_score_table_html($score,$myid);
172 $hashes = DB_get_hashes_by_session($session,$myid);
174 foreach($hashes as $hash)
182 echo "This is game number $j of <a href=\"{$INDEX}?action=game&me=$lasthash\">$i</a> in session $session.";
186 /* display the table and the names */
189 /* mystatus gets the player through the different stages of a game.
190 * start: does the player want to play?
191 * init: check for sickness
192 * check: check for return values from init
193 * poverty: handle poverty, wait here until all player have reached this state
194 * display sickness and move on to game
195 * play: game in progress
196 * gameover: are we revisiting a game
201 /* don't ask if user has autosetup set to yest */
203 if($PREF['autosetup']=='yes') $skip = 1;
205 if( !myisset('in') && !$skip)
207 /* asks the player, if he wants to join the game */
208 output_check_want_to_play($me);
213 /* check the result, if player wants to join, got next stage, else cancel game */
214 if(!$skip && $_REQUEST['in'] == 'no' )
216 /* cancel the game */
217 $message = "Hello, \n\n".
218 "the game has been canceled due to the request of one of the players.\n";
220 $userids = DB_get_all_userid_by_gameid($gameid);
221 foreach($userids as $user)
223 $To = DB_get_email('userid',$user);
224 mymail($To,"$EmailName game ".DB_format_gameid($gameid)." canceled",$message);
227 /* delete everything from the dB */
233 /* user wants to join the game */
235 /* move on to the next stage,
236 * no break statement to immediately go to the next stage
239 DB_set_hand_status_by_hash($me,'init');
241 /* check if everyone has reached this stage, send out email */
242 $userids = DB_get_all_userid_by_gameid($gameid);
244 foreach($userids as $user)
246 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
247 if($userstat!='init')
249 /* whos turn is it? */
250 DB_set_player_by_gameid($gameid,$user);
257 /* all done, send out email unless this player is the startplayer */
258 $startplayer = DB_get_startplayer_by_gameid($gameid);
259 if($mypos == $startplayer)
261 /* do nothing, go to next stage */
265 /* email startplayer */
267 $email = DB_get_email('position-gameid',$startplayer,$gameid);
268 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
269 $who = DB_get_userid('email',$email);
270 DB_set_player_by_gameid($gameid,$who);
272 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
273 "Use this link to go the game: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
274 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
281 /* here we ask the player if he is sick */
282 $mycards = DB_get_hand($me);
283 $mycards = mysort($mycards,$gametype);
285 if(!myisset('solo','wedding','poverty','nines') )
287 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
288 echo "\n<ul class=\"tricks\">\n";
289 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
290 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
291 " <div class=\"trick\" id=\"trick0\">\n";
293 for($pos=1;$pos<5;$pos++)
295 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
296 $userid = DB_get_userid('gameid-position',$gameid,$pos);
297 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
299 if($userstatus=='start' || $userstatus=='init')
300 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs <br/ > to decide </div>\n"; /* show this to everyone */
302 if($usersick!=NULL && $pos<=$mypos ) /* only show this for people sitting before the player */
303 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
304 else if($usersick==NULL && $pos<=$mypos)
305 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
307 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
308 /* end displaying sickness */
310 output_check_for_sickness($me,$mycards);
312 echo "<div class=\"mycards\">Your cards are: <br />\n";
313 foreach($mycards as $card)
314 display_card($card,$PREF['cardset']);
321 /* check if someone selected more than one sickness */
323 if($_REQUEST['solo']!='No') $Nsickness++;
324 if($_REQUEST['wedding'] == 'yes') $Nsickness++;
325 if($_REQUEST['poverty'] == 'yes') $Nsickness++;
326 if($_REQUEST['nines'] == 'yes') $Nsickness++;
330 echo "<p class=\"message\"> You selected more than one sickness, please go back ".
331 "and answer the <a href=\"$INDEX?action=game&me=$me&in=yes\">question</a> again.</p>";
333 echo "<div class=\"mycards\">Your cards are: <br />\n";
334 foreach($mycards as $card)
335 display_card($card,$PREF['cardset']);
342 /* everything is ok, save what user said and proceed */
343 echo "<p class=\"message\">Processing what you selected in the last step...";
345 /* check if this sickness needs to be handled first */
346 $gametype = DB_get_gametype_by_gameid($gameid);
347 $startplayer = DB_get_startplayer_by_gameid($gameid); /* need this to check which solo goes first */
349 if( $_REQUEST['solo']!='No' )
351 /* user wants to play a solo */
353 /* store the info in the user's hand info */
354 DB_set_solo_by_hash($me,$_REQUEST['solo']);
355 DB_set_sickness_by_hash($me,'solo');
357 echo "<br />Seems like you want to play a {$_REQUEST['solo']} solo. Got it.<br />\n";
359 if($gametype == 'solo' && $startplayer<$mypos)
360 {}/* do nothing, since someone else already is playing solo */
363 /* this solo comes first
364 * store info in game table
366 DB_set_gametype_by_gameid($gameid,'solo');
367 DB_set_startplayer_by_gameid($gameid,$mypos);
368 DB_set_solo_by_gameid($gameid,$_REQUEST['solo']);
371 else if($_REQUEST['wedding'] == 'yes')
373 /* silent solo is set further down */
374 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
375 DB_set_sickness_by_hash($me,'wedding');
377 else if($_REQUEST['poverty'] == 'yes')
379 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
380 DB_set_sickness_by_hash($me,'poverty');
382 else if($_REQUEST['nines'] == 'yes')
384 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
385 " is playing solo, this game will be canceled.<br />\n";
386 DB_set_sickness_by_hash($me,'nines');
391 /* move on to the next stage*/
392 DB_set_hand_status_by_hash($me,'check');
397 /* here we check what all players said and figure out what game we are playing
398 * this can therefore only be handled once all players finished the last stage
401 $mycards = DB_get_hand($me);
402 $mycards = mysort($mycards,$gametype);
404 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
405 echo "\n<ul class=\"tricks\">\n";
406 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
407 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
408 " <div class=\"trick\" id=\"trick0\">\n";
410 for($pos=1;$pos<5;$pos++)
412 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
413 $userid = DB_get_userid('gameid-position',$gameid,$pos);
414 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
416 if($userstatus=='start' || $userstatus=='init')
417 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs <br />to decide </div>\n"; /* show this to everyone */
419 if($usersick!=NULL) /* in the init-phase we only showed players with $pos<$mypos, now we can show all */
420 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
422 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
424 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
425 /* end displaying sickness */
427 echo "<div class=\"message\">\n";
428 echo "<p> Checking if someone else selected solo, nines, wedding or poverty.</p>";
430 /* check if everyone has reached this stage */
431 $userids = DB_get_all_userid_by_gameid($gameid);
433 foreach($userids as $user)
435 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
436 if($userstat!='check')
439 DB_set_player_by_gameid($gameid,$user);
446 echo "<p>This step can only be handled after everyone finished the last step. ".
447 "Seems like this is not the case, so you need to wait a bit... ".
448 "you will get an email once that is the case, please use the link in ".
449 "that email to continue the game.</p></div>";
451 /* display cards, if player was just at the init-phase he will still see the cards from there
452 * we can put this one here, since the last player to finish the init state won't get here and
453 * will still see his card anyway from the init-phase
455 if($mystatus=='check')
458 echo "<div class=\"mycards\">Your cards are: <br />\n";
459 foreach($mycards as $card)
460 display_card($card,$PREF['cardset']);
467 /* Ok, everyone finished the init-phase, time to figure out what game we
468 * are playing, in case there are any solos this already
469 * will have the correct information in it */
471 echo "<p> Ok, everyone is done... figuring out what kind of game we are playing.</p>";
473 $gametype = DB_get_gametype_by_gameid($gameid);
474 $startplayer = DB_get_startplayer_by_gameid($gameid);
476 /* check for sickness */
481 foreach($userids as $user)
483 $name = DB_get_name('userid',$user);
484 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
485 if($usersick == 'nines')
488 break; /* no need to check for other poverties, since only solo can win and that is already set */
490 else if($usersick == 'poverty')
492 else if($usersick == 'wedding')
494 else if($usersick == 'solo')
498 /* now check which sickness comes first and set the gametype to it */
499 if($gametype == 'solo')
506 /* TODO: should we keep statistics of this? */
507 $message = "Hello, \n\n".
508 " the game has been canceled because ".DB_get_name('userid',$nines).
509 " has five or more nines and nobody is playing solo.\n\n".
510 " To redeal either start a new game or, in case the game was part of a tournament, \n".
511 " go to the last game and use the link at the bottom of the page to redeal.";
513 $userids = DB_get_all_userid_by_gameid($gameid);
514 foreach($userids as $user)
516 $To = DB_get_email('userid',$user);
517 mymail($To,"$EmailName game ".DB_format_gameid($gameid)." canceled",$message);
520 /* delete everything from the dB */
523 echo "The game has been canceled because ".DB_get_name('userid',$nines).
524 " has five or more nines and nobody is playing solo.\n";
527 else if($poverty==1) /* one person has poverty */
529 DB_set_gametype_by_gameid($gameid,'poverty');
530 $gametype = 'poverty';
531 $who = DB_get_sickness_by_gameid($gameid);
534 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
535 if($firstsick == 'poverty')
536 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
538 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
541 else if($poverty==2) /* two people have poverty */
543 DB_set_gametype_by_gameid($gameid,'dpoverty');
544 $gametype = 'dpoverty';
545 $who = DB_get_sickness_by_gameid($gameid);
548 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
549 if($firstsick == 'poverty')
551 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
552 if($secondsick == 'poverty')
553 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
555 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
558 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
563 DB_set_gametype_by_gameid($gameid,'wedding');
564 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
565 $gametype = 'wedding';
567 /* now the gametype is set correctly in the database */
568 echo "<p> Got it :)</p>";
570 /* loop over all players, set re/contra if possible and start the game if possible */
571 $userids = DB_get_all_userid_by_gameid($gameid);
572 foreach($userids as $userid)
574 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
579 /* are we the solo player? set us to re, else set us to contra */
580 $pos = DB_get_pos_by_hash($userhash);
581 if($pos == $startplayer)
582 DB_set_party_by_hash($userhash,'re');
584 DB_set_party_by_hash($userhash,'contra');
585 DB_set_hand_status_by_hash($userhash,'play');
589 /* set person with the wedding to re, do the rest during the game */
590 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
591 if($usersick == 'wedding')
592 DB_set_party_by_hash($userhash,'re');
594 DB_set_party_by_hash($userhash,'contra');
596 DB_set_hand_status_by_hash($userhash,'play');
600 $hand = DB_get_all_hand($userhash);
602 if(in_array('3',$hand)||in_array('4',$hand))
603 DB_set_party_by_hash($userhash,'re');
605 DB_set_party_by_hash($userhash,'contra');
606 DB_set_hand_status_by_hash($userhash,'play');
610 /* set person with poverty to play status */
611 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
612 if($usersick == 'poverty')
613 DB_set_hand_status_by_hash($userhash,'play');
615 /* set status of first player to be asked to poverty */
616 $who = DB_get_sickness_by_gameid($gameid);
617 if($who > 6) $who= $who/10; /* in case we have dpoverty */
618 $whoid = DB_get_userid('gameid-position',$gameid,$who);
620 DB_set_hand_status_by_hash($userhash,'poverty');
623 /* check for silent solo, set game type to solo in this case */
624 $gametype = DB_get_gametype_by_gameid($gameid);
625 $userids = DB_get_all_userid_by_gameid($gameid);
626 foreach($userids as $userid)
628 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
630 if($gametype=='normal')
632 $userhand = DB_get_all_hand($userhash);
633 if(check_wedding($userhand))
635 /* normal game type and player has both queens -> silent solo */
636 /* keep startplayer, just set gametype to silent solo */
637 DB_set_gametype_by_gameid($gameid,'solo');
638 DB_set_solo_by_gameid($gameid,'silent');
643 /* send out email to first player or poverty person*/
644 if($gametype!='poverty' && $gametype!='dpoverty')
646 $startplayer = DB_get_startplayer_by_gameid($gameid);
647 $email = DB_get_email('position-gameid',$startplayer,$gameid);
648 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
649 $who = DB_get_userid('email',$email);
650 DB_set_player_by_gameid($gameid,$who);
654 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
656 /* email startplayer */
657 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
658 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
659 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
663 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
667 /* set status of first player to be asked to poverty */
668 $who = DB_get_sickness_by_gameid($gameid);
669 if($who > 6) $who= $who/10; /* in case we have dpoverty */
671 $whoid = DB_get_userid('gameid-position',$gameid,$who);
673 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
676 $email = DB_get_email('position-gameid',$who,$gameid);
677 $whohash = DB_get_hash_from_game_and_pos($gameid,$who);
678 DB_set_player_by_gameid($gameid,$whoid);
680 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
682 /* email player for poverty */
683 $message = "Poverty: It's your turn now in game ".DB_format_gameid($gameid).".\n".
684 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$whohash."\n\n" ;
685 mymail($email,$EmailName."Poverty (game ".DB_format_gameid($gameid).") ",$message);
693 /* user only gets here in a poverty game, several things have to be handled here:
694 * A) ask, if user wants to take trump
696 * poverty: set re/contra
697 * dpoverty: first time: set re, send email to second player
698 * second time: set contra
699 * poverty: set status of other players to 'play'
700 * set status to play in case 0 trump
701 * no -> set status to play,
702 * ask next player or cancle the game if no more players
703 * B) user took trump and has too many cards (e.g. count(cards)>12 and re/contra set)
704 * ask to give cards back, set status to play, once player has 12 cards
706 * it is easier to check B) first
709 /* output pre game in case user reloads */
712 set_gametype($gametype); /* this sets the $CARDS variable */
713 $myparty = DB_get_party_by_hash($me);
715 /* the following is part B) of whats needs to be done)
716 /* check if user wants to give cards back */
717 if(myisset('exchange'))
719 $exchange = $_REQUEST['exchange'];
720 $partnerhash = DB_get_partner_hash_by_hash($me);
721 $partnerid = DB_get_userid('hash',$partnerhash);
722 $partnerhand = DB_get_handid('gameid-userid',$gameid,$partnerid);
724 /* if exchange is set to a value>0, exchange that card back to the partner */
727 $result = DB_query("UPDATE Hand_Card SET hand_id='$partnerhand'".
728 " WHERE hand_id='$myhand' AND card_id=".DB_quote_smart($exchange));
729 DB_add_exchanged_card(DB_quote_smart($exchange),$myhand,$partnerhand);
734 $mycards = DB_get_hand($me);
735 $mycards = mysort($mycards,$gametype);
737 /* check if user need to give more cards back */
738 if( ($myparty=='re' || $myparty=='contra') && count($mycards)>12)
740 echo "<div class=\"poverty\"> you need to get rid of a few cards</div>\n";
743 echo "<div class=\"mycards\">Your cards are: <br />\n";
744 foreach($mycards as $card)
745 display_link_card($card,$PREF['cardset'],$type);
746 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select card to give back\" />\n";
749 else if( ($myparty=='re' || $myparty=='contra') && count($mycards)==12)
751 /* user is done, ready to play */
752 DB_set_hand_status_by_hash($me,'play');
754 /* email start player */
755 $startplayer = DB_get_startplayer_by_gameid($gameid);
756 $email = DB_get_email('position-gameid',$startplayer,$gameid);
757 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
758 $who = DB_get_userid('email',$email);
759 DB_set_player_by_gameid($gameid,$who);
763 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
765 /* email startplayer */
766 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
767 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
768 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
772 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
775 /* the following is part A) of what needs to be done */
776 if(!myisset('trump'))
780 echo "<div class=\"poverty\">\n";
781 $userids = DB_get_all_userid_by_gameid($gameid);
782 foreach($userids as $user)
784 $name = DB_get_name('userid',$user);
785 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
786 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
787 $userparty = DB_get_party_by_hash($userhash);
789 if($usersick=='poverty' && !$userparty)
791 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
792 $cards = DB_get_hand($hash);
795 foreach($cards as $card)
796 if($card<27) $nrtrump++;
797 echo "Player $name has $nrtrump trump. Do you want to take them?".
798 "<a href=\"index.php?action=game&me=$me&trump=$user\">Yes</a> <br />\n";
801 echo "<a href=\"index.php?action=game&me=$me&trump=no\">No way</a> <br />\n";
802 echo "</div><div>\n";
804 echo "<div class=\"mycards\">Your cards are: <br />\n";
805 foreach($mycards as $card)
806 display_card($card,$PREF['cardset']);
807 echo "</div></div>\n";
813 $trump = $_REQUEST['trump'];
817 /* user doesn't want to take trump */
818 DB_set_hand_status_by_hash($me,'play');
820 /* set next player who needs to be asked and email him*/
821 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
822 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
824 /* don't ask people who have poverty */
826 if($firstsick=='poverty')
828 if($secondsick=='poverty')
833 if($gametype=='dpoverty')
835 $next=999; /* need to cancel for sure, since both would need to take the trump */
838 /* no more people to ask, need to cancel the game */
841 $message = "Hello, \n\n".
842 "Game ".DB_format_gameid($gameid)." has been canceled since nobody wanted to take the trump.\n";
844 $userids = DB_get_all_userid_by_gameid($gameid);
845 foreach($userids as $user)
847 $To = DB_get_email('userid',$user);
848 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled (poverty not resolved)",$message);
851 /* delete everything from the dB */
854 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been canceled.<br /><br /></p>";
859 /* email next player, set his status to poverty */
860 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
861 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
862 $userid = DB_get_userid('email',$To);
864 DB_set_player_by_gameid($gameid,$userid);
865 DB_set_hand_status_by_hash($userhash,'poverty');
867 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
868 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
869 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
874 /* player wants to take trump, change cards */
876 /* user wants to take trump */
877 $trump = $_REQUEST['trump'];
878 $userhand = DB_get_handid('gameid-userid',$gameid,$trump);
879 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
881 /* remember which cards were handed over*/
882 $partnerhand = DB_get_all_hand($userhash);
883 foreach ($partnerhand as $card)
885 DB_add_exchanged_card($card,$userhand,$myhand);
887 /* copy trump from player A to B */
888 $result = DB_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
891 $mycards = DB_get_hand($me);
894 if($gametype=='poverty')
896 $userids = DB_get_all_userid_by_gameid($gameid);
897 foreach($userids as $user)
899 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
900 if($hash==$userhash||$hash==$me)
902 DB_set_party_by_hash($hash,'re');
906 DB_set_party_by_hash($hash,'contra');
907 DB_set_hand_status_by_hash($hash,'play'); /* the contra party is ready to play */
910 /* check if we are done (in case of no trump handed over), if so, go to 'play' phase right away*/
911 if(count($mycards)==12)
913 DB_set_hand_status_by_hash($me,'play');
918 /* has the re party already been set?*/
920 $userids = DB_get_all_userid_by_gameid($gameid);
921 foreach($userids as $user)
923 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
924 $party = DB_get_party_by_hash($hash);
930 DB_set_party_by_hash($me,'contra');
931 DB_set_party_by_hash($userhash,'contra');
935 DB_set_party_by_hash($me,'re');
936 DB_set_party_by_hash($userhash,'re');
938 /* send out email to second non-poverty player */
939 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
940 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
943 if($firstsick=='poverty')
944 if($secondsick=='poverty')
950 echo "<div class=\"message\">Error in poverty, please contact the Admin</div>\n";
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 = "Two people have 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." double poverty (game ".DB_format_gameid($gameid).")",$message);
966 echo "<div class=\"message\"> Please, <a href=\"$INDEX?action=game&me=$me\">continue</a> here.</div>\n";
974 /* both entries here, so that the tricks are visible for both.
975 * in case of 'play' there is a break later that skips the last part
978 /* check if all players are ready to play,
979 * if so, send out email to the startplayer
980 * only need to do this if the game hasn't started yet
982 $gamestatus = DB_get_game_status_by_gameid($gameid);
983 if($gamestatus == 'pre')
986 $userids = DB_get_all_userid_by_gameid($gameid);
987 foreach($userids as $user)
989 $userstatus = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
990 if($userstatus !='play' && $userstatus!='gameover')
993 DB_set_player_by_gameid($gameid,$user);
999 /* only set this after all poverty, etc. are handled*/
1000 DB_set_game_status_by_gameid($gameid,'play');
1002 /* email startplayer */
1003 $startplayer = DB_get_startplayer_by_gameid($gameid);
1004 $email = DB_get_email('position-gameid',$startplayer,$gameid);
1005 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
1006 $who = DB_get_userid('email',$email);
1007 DB_set_player_by_gameid($gameid,$who);
1009 if($hash!=$me && DB_get_email_pref_by_hash($hash)!='emailaddict')
1011 /* email startplayer) */
1012 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
1013 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
1014 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
1018 /* figure out what kind of game we are playing,
1019 * set the global variables $CARDS['trump'],$CARDS['diamonds'],$CARDS['hearts'],
1020 * $CARDS['clubs'],$CARDS['spades'],$CARDS['foxes']
1024 $gametype = DB_get_gametype_by_gameid($gameid);
1026 if($gametype=='solo')
1028 $gametype = DB_get_solo_by_gameid($gameid);
1029 if($gametype=='silent')
1032 $GT = $gametype.' '.$GT;
1035 $gametype = 'normal';
1037 set_gametype($gametype); /* this sets the $CARDS variable */
1039 /* get some infos about the game, need to reset this, since it might have changed */
1040 $gamestatus = DB_get_game_status_by_gameid($gameid);
1042 /* has the game started? No, then just wait here...*/
1043 if($gamestatus == 'pre')
1045 echo "<p class=\"message\"> You finished the setup, but not everyone else finished it... ".
1046 "You need to wait for the others. Just wait for an email. </p>";
1048 $mycards = DB_get_hand($me);
1049 $mycards = mysort($mycards,$gametype);
1051 echo "<div class=\"mycards\">Your cards are: <br />\n";
1052 foreach($mycards as $card)
1053 display_card($card,$PREF['cardset']);
1056 break; /* not sure this works... the idea is that you can
1057 * only play a card after everyone is ready to play */
1060 /* get time from the last action of the game */
1061 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1062 $gameend = time() - strtotime($r[0]);
1064 /* handle comments in case player didn't play a card, allow comments a week after the end of the game */
1065 if( (!myisset('card') && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
1066 if(myisset('comment'))
1068 $comment = $_REQUEST['comment'];
1069 $playid = DB_get_current_playid($gameid);
1072 DB_insert_comment($comment,$playid,$myid);
1075 /* get everything relevant to display the tricks */
1076 $result = DB_query("SELECT Hand_Card.card_id as card,".
1077 " Hand.position as position,".
1078 " Play.sequence as sequence, ".
1080 " GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
1081 " SEPARATOR '\n' ), ".
1082 " Play.create_date, ".
1085 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1086 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1087 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1088 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1089 "LEFT JOIN User On User.id=Comment.user_id ".
1090 "WHERE Trick.game_id='".$gameid."' ".
1091 "GROUP BY Trick.id, sequence ".
1092 "ORDER BY Trick.id, sequence ASC");
1094 $lasttrick = DB_get_max_trickid($gameid);
1096 $play = array(); /* needed to calculate winner later */
1098 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1099 $firstcard = ''; /* first card in a trick */
1101 echo "\n<ul class=\"tricks\">\n";
1102 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
1104 /* output vorbehalte */
1105 $mygametype = DB_get_gametype_by_gameid($gameid);
1106 if($mygametype != 'normal' && $mygametype != 'silent') /* only show when needed */
1108 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
1109 " <div class=\"trick\" id=\"trick0\">\n";
1110 /* get information so show the cards that have been handed over in a poverty game */
1115 if($mygametype == 'poverty' || $mygametype=='dpoverty')
1117 /* who has poverty */
1118 for($mypos=1;$mypos<5;$mypos++)
1120 $usersick = DB_get_sickness_by_pos_and_gameid($mypos,$gameid);
1121 if($usersick == 'poverty')
1123 $povertypos2 = $mypos;
1125 $povertypos1 = $mypos;
1127 /* get hash and cards for all */
1128 $povertyhash1 = DB_get_hash_from_game_and_pos($gameid,$povertypos1);
1129 $partnerhash1 = DB_get_partner_hash_by_hash($povertyhash1);
1131 $povertycards1 = DB_get_exchanged_cards($povertyhash1);
1132 $partnercards1 = DB_get_exchanged_cards($partnerhash1);
1134 $partnerpos1 = DB_get_pos_by_hash($partnerhash1);
1137 $povertyhash2 = DB_get_hash_from_game_and_pos($gameid,$povertypos2);
1138 $partnerhash2 = DB_get_partner_hash_by_hash($povertyhash2);
1140 $povertycards2 = DB_get_exchanged_cards($povertyhash2);
1141 $partnercards2 = DB_get_exchanged_cards($partnerhash2);
1143 $partnerpos2 = DB_get_pos_by_hash($partnerhash2);
1148 for($mypos=1;$mypos<5;$mypos++)
1150 $usersick = DB_get_sickness_by_pos_and_gameid($mypos,$gameid);
1151 if($usersick!=NULL ||
1152 $mypos==$povertypos1 || $mypos==$partnerpos1 ||
1153 $mypos==$povertypos2 || $mypos==$partnerpos2 )
1155 echo " <div class=\"vorbehalt".($mypos-1)."\"> Vorbehalt <br />";
1157 echo " $usersick <br />";
1158 if($mypos==$partnerpos1)
1160 foreach($partnercards1 as $card)
1161 if($povertyhash1 == $me || $partnerhash1 == $me || $mystatus=='gameover')
1162 display_card($card,$PREF['cardset']);
1164 display_card(0,$PREF['cardset']);
1166 else if($mypos==$povertypos1)
1168 foreach($povertycards1 as $card)
1169 if($povertyhash1 == $me || $partnerhash1 == $me || $mystatus=='gameover')
1170 display_card($card,$PREF['cardset']);
1172 display_card(0,$PREF['cardset']);
1174 else if($mypos==$povertypos2)
1176 foreach($povertycards2 as $card)
1177 if($povertyhash2 == $me || $partnerhash2 == $me || $mystatus=='gameover')
1178 display_card($card,$PREF['cardset']);
1180 display_card(0,$PREF['cardset']);
1182 else if($mypos==$partnerpos2)
1184 foreach($partnercards2 as $card)
1185 if($povertyhash2 == $me || $partnerhash2 == $me || $mystatus=='gameover')
1186 display_card($card,$PREF['cardset']);
1188 display_card(0,$PREF['cardset']);
1193 if($mygametype == $usersick)
1197 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1201 while($r = DB_fetch_array($result))
1209 /* count number of tricks */
1213 /* check if first schweinchen has been played */
1214 if( $GAME['schweinchen-who'] && ($r[0] == 19 || $r[0] == 20) )
1215 if(!$GAME['schweinchen-first'])
1216 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1218 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1220 /* save card to be able to find the winner of the trick later */
1221 $play[$seq] = array('card'=>$r[0],'pos'=>$pos);
1225 /* first card in a trick, output some html */
1226 if($trick!=$lasttrick)
1228 /* start of an old trick? */
1229 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1230 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1231 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1233 else if($trick==$lasttrick)
1235 /* start of a last trick? */
1236 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1237 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1238 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1241 /* remember first card, so that we are able to check, what cards can be played */
1246 echo " <div class=\"card".($pos-1)."\">\n";
1248 /* display comments */
1250 echo " <span class=\"comment\">".$comment."</span>\n";
1253 display_card($r[0],$PREF['cardset']);
1255 echo " </div>\n"; /* end div card */
1260 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1264 /* whos turn is it? */
1267 $winner = get_winner($play,$gametype); /* returns the position */
1269 $firstcard = ''; /* new trick, no first card */
1274 if($next==5) $next = 1;
1277 /* my turn?, display cards as links, ask for comments*/
1278 if(DB_get_pos_by_hash($me) == $next)
1283 /* do we want to play a card? */
1284 if(myisset('card') && $myturn)
1286 $card = $_REQUEST['card'];
1287 $handid = DB_get_handid('hash',$me);
1288 $commentSchweinchen =''; /* used to add a comment when Schweinchen is being played */
1290 /* check if we have card and that we haven't played it yet*/
1291 /* set played in hand_card to true where hand_id and card_id*/
1292 $r = DB_query_array("SELECT id FROM Hand_Card WHERE played='false' and ".
1293 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1294 $handcardid = $r[0];
1296 if($handcardid) /* everything ok, play card */
1298 /* update Game timestamp */
1299 DB_update_game_timestamp($gameid);
1301 /* mark card as played */
1302 DB_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1303 DB_quote_smart($card));
1305 /* get trick id or start new trick */
1306 $a = DB_get_current_trickid($gameid);
1311 $playid = DB_play_card($trickid,$handcardid,$sequence);
1313 /* check special output for schweinchen in case in case a fox is being played
1314 * check for correct rules, etc. has already been done
1316 if( $GAME['schweinchen-who'] && ($card == 19 || $card == 20) )
1318 if(!$GAME['schweinchen-first'])
1319 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1321 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1323 if( ($GAME['schweinchen-second']==1 && $RULES['schweinchen']=='second') || $RULES['schweinchen']=='both')
1325 DB_insert_comment('Schweinchen! ',$playid,$myid);
1326 $commentSchweinchen = 'Schweinchen! ';
1329 echo 'schweinchen = '.$GAME['schweinchen-who'].' ---<br />';
1332 /* if sequence == 4 check who one in case of wedding */
1333 if($sequence == 4 && $GT == 'wedding')
1335 /* is wedding resolve */
1336 $resolved = DB_get_sickness_by_gameid($gameid);
1339 /* who has wedding */
1340 $userids = DB_get_all_userid_by_gameid($gameid);
1341 foreach($userids as $user)
1343 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1344 if($usersick == 'wedding')
1347 /* who won the trick */
1348 $play = DB_get_cards_by_trick($trickid);
1349 $winner = get_winner($play,$gametype); /* returns the position */
1350 $winnerid = DB_get_userid('gameid-position',$gameid,$winner);
1351 /* is tricknr <=3 */
1352 if($tricknr <=3 && $winnerid!=$whosick)
1354 /* set resolved at tricknr*/
1355 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1357 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1358 DB_set_party_by_hash($whash,'re');
1360 if($tricknr == 3 && $winnerid==$whosick)
1362 /* set resolved at tricknr*/
1363 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1368 /* if sequence == 4, set winner of the trick, count points and set the next player */
1371 $play = DB_get_cards_by_trick($trickid);
1372 $winner = get_winner($play,$gametype); /* returns the position */
1374 /* check if someone caught a fox */
1375 /* first check if we should account for solos at all,
1376 * since it doesn't make sense in some games
1378 $ok = 0; /* fox shouldn't be counted */
1379 if(DB_get_gametype_by_gameid($gameid)=='solo')
1381 $solo = DB_get_solo_by_gameid($gameid);
1382 if($solo == 'trump' || $solo == 'silent')
1383 $ok = 1; /* for trump solos and silent solos, foxes are ok */
1386 $ok = 1; /* for all other games (not solos) foxes are ok too */
1389 foreach($play as $played)
1391 if ( $played['card']==19 || $played['card']==20 )
1392 if ($played['pos']!= $winner )
1394 /* possible caught a fox, check party */
1395 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1396 $uid2 = DB_get_userid('gameid-position',$gameid,$played['pos']);
1398 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1399 $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1401 if($party1 != $party2)
1402 DB_query("INSERT INTO Score".
1403 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1407 /* check for karlchen (jack of clubs in the last trick)*/
1408 /* same as for foxes, karlchen doesn't always make sense
1409 * check what kind of game it is and set karlchen accordingly */
1410 $ok = 1; /* default: karlchen should be accounted for */
1412 $ok = 0; /* Karlchen works only in the last trick */
1413 if($ok && DB_get_gametype_by_gameid($gameid)=='solo' )
1415 $solo = DB_get_solo_by_gameid($gameid);
1416 if($solo == 'trumpless' || $solo == 'jack' || $solo == 'queen' )
1417 $ok = 0; /* no Karlchen in these solos */
1421 foreach($play as $played)
1422 if ( $played['card']==11 || $played['card']==12 )
1423 if ($played['pos'] == $winner )
1425 /* possible caught a fox, check party */
1426 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1427 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1429 DB_query("INSERT INTO Score".
1430 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1432 /* check for doppelopf (>40 points)*/
1434 foreach($play as $played)
1436 $points += DB_get_card_value_by_cardid($played['card']);
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,'doko')");
1448 DB_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1450 echo "ERROR during scoring";
1453 echo "DEBUG: position $winner won the trick <br />";
1455 /* who is the next player? */
1460 $next = DB_get_pos_by_hash($me)+1;
1462 if($next==5) $next=1;
1464 /* check for coment */
1465 if(myisset('comment'))
1467 $comment = $_REQUEST['comment'];
1469 DB_insert_comment($comment,$playid,$myid);
1470 if($commentSchweinchen)
1471 $comment = $commentSchweinchen . $comment;
1474 /* display played card */
1475 $pos = DB_get_pos_by_hash($me);
1478 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1479 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1480 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1483 echo " <div class=\"card".($pos-1)."\">\n ";
1485 /* display comments */
1486 display_card($card,$PREF['cardset']);
1488 echo "\n <span class=\"comment\"> ".$comment."</span>\n";
1491 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1493 /*check if we still have cards left, else set status to gameover */
1494 if(sizeof(DB_get_hand($me))==0)
1496 DB_set_hand_status_by_hash($me,'gameover');
1497 $mystatus = 'gameover';
1500 /* if all players are done, set game status to game over,
1501 * get the points of the last trick and send out an email
1504 $userids = DB_get_all_userid_by_gameid($gameid);
1507 foreach($userids as $user)
1508 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1512 DB_set_game_status_by_gameid($gameid,'gameover');
1514 /* email next player, if game is still running */
1515 if(DB_get_game_status_by_gameid($gameid)=='play')
1517 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1518 $email = DB_get_email('hash',$next_hash);
1519 $who = DB_get_userid('email',$email);
1520 DB_set_player_by_gameid($gameid,$who);
1522 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1523 "It's your turn now.\n".
1524 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$next_hash."\n\n" ;
1525 if( DB_get_email_pref_by_uid($who)!='emailaddict' )
1526 mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
1528 else /* send out final email */
1530 /* individual score */
1531 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1532 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1533 " LEFT JOIN User ON User.id=Hand.user_id".
1534 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1535 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1536 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1537 " WHERE Hand.game_id='$gameid'".
1538 " GROUP BY User.fullname" );
1539 $message = "The game is over. Thanks for playing :)\n";
1540 $message .= "Final score:\n";
1541 while( $r = DB_fetch_array($result) )
1542 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1544 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1545 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1546 " LEFT JOIN User ON User.id=Hand.user_id".
1547 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1548 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1549 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1550 " WHERE Hand.game_id='$gameid'".
1551 " GROUP BY Hand.party" );
1552 $message .= "\nTotals:\n";
1555 while( $r = DB_fetch_array($result) )
1557 $message .= " ".$r[0]." ".$r[1]."\n";
1560 else if($r[0] == 'contra')
1565 * save score in database
1569 /* get calls from re/contra */
1571 $call_contra = NULL;
1572 foreach($userids as $user)
1574 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1575 $call = DB_get_call_by_hash($hash);
1576 $party = DB_get_party_by_hash($hash);
1580 $call = (int) $call;
1586 else if( $call < $call_re)
1589 else if($party=='contra')
1591 if($call_contra==NULL)
1592 $call_contra = $call;
1593 else if( $call < $call_contra)
1594 $call_contra = $call;
1599 /* figure out who one */
1600 $winning_party = NULL;
1602 if($call_re == NULL && $call_contra==NULL)
1604 /* nobody made a call, so it's easy to figure out who won */
1606 $winning_party='re';
1608 $winning_party='contra';
1612 /* if one party makes a call, they only win, iff they make enough points
1613 * if only one party made a call, the other one wins,
1614 * if the first one didn't make it
1618 $offset = 120 - $call_re;
1620 $offset--; /* since we use a > in the next equation */
1622 if($re > 120+$offset)
1623 $winning_party='re';
1624 else if ($call_contra == NULL )
1625 $winning_party='contra';
1630 $offset = 120 - $call_contra;
1631 if($call_contra == 0)
1632 $offset--; /* since we use a > in the next equation */
1634 if($contra > 120+$offset)
1635 $winning_party='contra';
1636 else if ($call_re == NULL )
1637 $winning_party='re';
1641 /* one point for each call of the other party in case the other party didn't win
1642 * and one point each in case the party made more than points than one of the calls
1644 if($winning_party!='contra' && $call_contra!=NULL)
1646 for( $p=$call_contra;$p<=120; $p+=30 )
1648 DB_query("INSERT INTO Score".
1649 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
1652 for( $p=$call_contra; $p<120; $p+=30)
1655 DB_query("INSERT INTO Score".
1656 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'made$p')");
1659 if($winning_party!='re' and $call_re!=NULL)
1661 for( $p=$call_re;$p<=120; $p+=30 )
1663 DB_query("INSERT INTO Score".
1664 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'against$p')");
1667 for( $p=$call_re; $p<120; $p+=30)
1670 DB_query("INSERT INTO Score".
1671 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
1675 /* point in case contra won */
1676 if($winning_party=='contra')
1678 DB_query("INSERT INTO Score".
1679 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1682 /* one point each for winning and each 30 points + calls */
1683 if($winning_party=='re')
1685 foreach(array(120,150,180,210,240) as $p)
1688 if($p==240 || $call_contra!=NULL)
1692 DB_query("INSERT INTO Score".
1693 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
1695 /* re called something and won */
1696 foreach(array(0,30,60,90,120) as $p)
1698 if($call_re!=NULL && $call_re<$p+1)
1699 DB_query("INSERT INTO Score".
1700 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
1703 else if( $winning_party=='contra')
1705 foreach(array(120,150,180,210,240) as $p)
1708 if($p==240 || $call_re!=NULL)
1711 if($contra>$p-$offset)
1712 DB_query("INSERT INTO Score".
1713 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
1715 /* re called something and won */
1716 foreach(array(0,30,60,90,120) as $p)
1718 if($call_contra!=NULL && $call_contra<$p+1)
1719 DB_query("INSERT INTO Score".
1720 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
1725 /* add score points to email */
1728 $message .= " Points Re: \n";
1729 $queryresult = DB_query("SELECT score FROM Score ".
1730 " WHERE game_id=$gameid AND party='re'".
1732 while($r = DB_fetch_array($queryresult) )
1734 $message .= " ".$r[0]."\n";
1737 $message .= " Points Contra: \n";
1738 $queryresult = DB_query("SELECT score FROM Score ".
1739 " WHERE game_id=$gameid AND party='contra'".
1741 while($r = DB_fetch_array($queryresult) )
1743 $message .= " ".$r[0]."\n";
1746 $message .= " Total Points (from the Re point of view): $Tpoint\n";
1749 $session = DB_get_session_by_gameid($gameid);
1750 $score = generate_score_table($session);
1752 $message .= "Score Table:\n";
1753 $message .= format_score_table_ascii($score);
1755 /* send out final email */
1758 foreach($userids as $user)
1759 $all[] = DB_get_email('userid',$user);
1760 $To = implode(",",$all);
1762 $help = "\n\n (you can use reply all on this email to reach all the players.)\n";
1763 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 1(2)",$message.$help);
1765 foreach($userids as $user)
1767 $To = DB_get_email('userid',$user);
1768 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1770 $link = "Use this link to have a look at game ".DB_format_gameid($gameid).": ".
1771 $HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
1772 if( DB_get_email_pref_by_uid($user) != 'emailaddict' )
1773 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 2(2)",$link);
1779 echo "can't find that card?! <br />\n";
1782 else if(myisset('card') && !$myturn )
1784 echo "please wait until it's your turn! <br />\n";
1787 if($seq!=4 && $trickNR>=1 && !(myisset('card') && $myturn) )
1788 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1790 /* display points in case game is over */
1791 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1793 echo " <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1794 " <div class=\"trick\" id=\"trick13\">\n";
1795 /* add pic for re/contra
1796 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1798 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1799 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1800 " LEFT JOIN User ON User.id=Hand.user_id".
1801 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1802 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1803 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1804 " WHERE Hand.game_id='$gameid'".
1805 " GROUP BY User.fullname" );
1806 while( $r = DB_fetch_array($result))
1807 echo " <div class=\"card".($r[3]-1)."\">\n".
1808 " <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
1811 /* display totals */
1812 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1813 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1814 " LEFT JOIN User ON User.id=Hand.user_id".
1815 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1816 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1817 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1818 " WHERE Hand.game_id='$gameid'".
1819 " GROUP BY Hand.party" );
1820 echo "<div class=\"total\">\n Totals:<br />\n";
1821 while( $r = DB_fetch_array($result))
1822 echo " ".$r[0]." ".$r[1]."<br />\n";
1824 $queryresult = DB_query("SELECT timediff(mod_date,create_date) ".
1825 " FROM Game WHERE id='$gameid'");
1826 $r = DB_fetch_array($queryresult);
1827 echo " <p>This game took ".$r[0]." hours.</p>\n";
1829 echo " <div class=\"re\">\n Points Re: <br />\n";
1830 $queryresult = DB_query("SELECT score FROM Score ".
1831 " WHERE game_id=$gameid AND party='re'".
1833 while($r = DB_fetch_array($queryresult) )
1834 echo " ".$r[0]."<br />\n";
1837 echo " <div class=\"contra\">\n Points Contra: <br />\n";
1838 $queryresult = DB_query("SELECT score FROM Score ".
1839 " WHERE game_id=$gameid AND party='contra'".
1841 while($r = DB_fetch_array($queryresult) )
1842 echo " ".$r[0]."<br />\n";
1847 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1850 echo " <li onclick=\"hl_prev();\" class=\"old\"><a href=\"#\">prev</a></li>\n";
1851 echo " <li onclick=\"hl_next();\" class=\"old\"><a href=\"#\">next</a></li>\n";
1852 echo "</ul>\n"; /* end ul tricks*/
1854 $mycards = DB_get_hand($me);
1855 $mycards = mysort($mycards,$gametype);
1856 echo "<div class=\"mycards\">\n";
1858 if($myturn && !myisset('card') && $mystatus=='play' )
1860 echo "Hello ".$myname.", it's your turn! <br />\n";
1861 echo "Your cards are: <br />\n";
1863 /* do we have to follow suite? */
1865 if(have_suit($mycards,$firstcard))
1868 foreach($mycards as $card)
1870 if( ($followsuit && !same_type($card,$firstcard)) ||
1871 ( (int)($card)==19 && ($RULES['schweinchen']=='second'||$RULES['schweinchen']=='secondaftercall')
1872 && $GAME['schweinchen-who']==$me && !$GAME['schweinchen-first'] )
1874 display_card($card,$PREF['cardset']);
1876 display_link_card($card,$PREF['cardset']);
1879 else if($mystatus=='play' )
1881 echo "Your cards are: <br />\n";
1882 foreach($mycards as $card)
1883 display_card($card,$PREF['cardset']);
1885 else if($mystatus=='gameover')
1887 $oldcards = DB_get_all_hand($me);
1888 $oldcards = mysort($oldcards,$gametype);
1889 echo "Your cards were: <br />\n";
1890 foreach($oldcards as $card)
1891 display_card($card,$PREF['cardset']);
1893 $userids = DB_get_all_userid_by_gameid($gameid);
1894 foreach($userids as $user)
1896 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1902 $name = DB_get_name('userid',$user);
1903 $oldcards = DB_get_all_hand($userhash);
1904 $oldcards = mysort($oldcards,$gametype);
1905 echo "$name's cards were: <br />\n";
1906 foreach($oldcards as $card)
1907 display_card($card,$PREF['cardset']);
1913 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1914 if($mystatus=='play')
1917 /* the following happens only when the gamestatus is 'gameover' */
1918 /* check if game is over, display results */
1919 if(DB_get_game_status_by_gameid($gameid)=='play')
1921 echo "The game is over for you.. other people still need to play though";
1925 myerror("error in testing the status");
1927 /* output left menu */
1928 display_user_menu();
1930 /* output right menu */
1932 /* display rule set for this game */
1933 echo "<div class=\"gameinfo\">\n";
1935 if($gamestatus == 'play' )
1936 output_form_calls($me);
1938 /* get time from the last action of the game */
1939 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1940 $gameend = time() - strtotime($r[0]);
1942 if($gamestatus == 'play' || $gameend < 60*60*24*7)
1944 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1947 echo "<input type=\"submit\" value=\"submit\" />\n";
1953 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1955 $session = DB_get_session_by_gameid($gameid);
1956 $result = DB_query("SELECT id,create_date FROM Game".
1957 " WHERE session=$session".
1958 " ORDER BY create_date DESC".
1962 $r = DB_fetch_array($result);
1964 if(!$session || $gameid==$r[0])
1966 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
1967 $names = DB_get_all_names_by_gameid($gameid);
1968 $type = DB_get_gametype_by_gameid($gameid);
1972 $solo = DB_get_solo_by_gameid($gameid);
1974 if($solo!='silent') /* repeat game with same first player */
1975 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
1976 else /* rotate normally */
1977 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
1979 else /* rotate normally */
1980 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);