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";
177 echo " <div class=\"sessionscore\">Score \n";
178 $score = generate_score_table($session);
179 echo format_score_table_html($score,$myid);
181 $hashes = DB_get_hashes_by_session($session,$myid);
184 foreach($hashes as $hash)
194 $previous = $hashes[$j-2];
202 if(isset($_SESSION['id']) && $_SESSION['id']==$myid)
205 echo "<a href=\"{$INDEX}?action=game&me=$previous\">previous game</a> \n";
206 echo "This is game number $j of <a href=\"{$INDEX}?action=game&me=$lasthash\">$i</a> in session $session.\n";
208 echo " <a href=\"{$INDEX}?action=game&me=$next\">next game</a> \n";
211 echo "This is game number $j of $i in session $session.";
215 /* display the table and the names */
218 /* mystatus gets the player through the different stages of a game.
219 * start: does the player want to play?
220 * init: check for sickness
221 * check: check for return values from init
222 * poverty: handle poverty, wait here until all player have reached this state
223 * display sickness and move on to game
224 * play: game in progress
225 * gameover: are we revisiting a game
230 /* don't ask if user has autosetup set to yest */
232 if($PREF['autosetup']=='yes') $skip = 1;
234 if( !myisset('in') && !$skip)
236 /* asks the player, if he wants to join the game */
237 output_check_want_to_play($me);
242 /* check the result, if player wants to join, got next stage, else cancel game */
243 if(!$skip && $_REQUEST['in'] == 'no' )
245 /* cancel the game */
246 $message = "Hello, \n\n".
247 "the game has been canceled due to the request of one of the players.\n";
249 $userids = DB_get_all_userid_by_gameid($gameid);
250 foreach($userids as $user)
252 $To = DB_get_email('userid',$user);
253 mymail($To,"$EmailName game ".DB_format_gameid($gameid)." canceled",$message);
256 /* delete everything from the dB */
262 /* user wants to join the game */
264 /* move on to the next stage,
265 * no break statement to immediately go to the next stage
268 DB_set_hand_status_by_hash($me,'init');
270 /* check if everyone has reached this stage, send out email */
271 $userids = DB_get_all_userid_by_gameid($gameid);
273 foreach($userids as $user)
275 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
276 if($userstat!='init')
278 /* whos turn is it? */
279 DB_set_player_by_gameid($gameid,$user);
286 /* all done, send out email unless this player is the startplayer */
287 $startplayer = DB_get_startplayer_by_gameid($gameid);
288 if($mypos == $startplayer)
290 /* do nothing, go to next stage */
294 /* email startplayer */
296 $email = DB_get_email('position-gameid',$startplayer,$gameid);
297 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
298 $who = DB_get_userid('email',$email);
299 DB_set_player_by_gameid($gameid,$who);
301 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
302 "Use this link to go the game: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
303 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
310 /* here we ask the player if he is sick */
311 $mycards = DB_get_hand($me);
312 $mycards = mysort($mycards,$gametype);
314 if(!myisset('solo','wedding','poverty','nines') )
316 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
317 echo "\n<ul class=\"tricks\">\n";
318 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
319 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
320 " <div class=\"trick\" id=\"trick0\">\n";
322 for($pos=1;$pos<5;$pos++)
324 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
325 $userid = DB_get_userid('gameid-position',$gameid,$pos);
326 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
328 if($userstatus=='start' || $userstatus=='init')
329 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs <br/ > to decide </div>\n"; /* show this to everyone */
331 if($usersick!=NULL && $pos<=$mypos ) /* only show this for people sitting before the player */
332 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
333 else if($usersick==NULL && $pos<=$mypos)
334 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
336 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
337 /* end displaying sickness */
339 output_check_for_sickness($me,$mycards);
341 echo "<div class=\"mycards\">Your cards are: <br />\n";
342 foreach($mycards as $card)
343 display_card($card,$PREF['cardset']);
350 /* check if someone selected more than one sickness */
352 if($_REQUEST['solo']!='No') $Nsickness++;
353 if($_REQUEST['wedding'] == 'yes') $Nsickness++;
354 if($_REQUEST['poverty'] == 'yes') $Nsickness++;
355 if($_REQUEST['nines'] == 'yes') $Nsickness++;
359 echo "<p class=\"message\"> You selected more than one sickness, please go back ".
360 "and answer the <a href=\"$INDEX?action=game&me=$me&in=yes\">question</a> again.</p>";
362 echo "<div class=\"mycards\">Your cards are: <br />\n";
363 foreach($mycards as $card)
364 display_card($card,$PREF['cardset']);
371 /* everything is ok, save what user said and proceed */
372 echo "<p class=\"message\">Processing what you selected in the last step...";
374 /* check if this sickness needs to be handled first */
375 $gametype = DB_get_gametype_by_gameid($gameid);
376 $startplayer = DB_get_startplayer_by_gameid($gameid); /* need this to check which solo goes first */
378 if( $_REQUEST['solo']!='No' )
380 /* user wants to play a solo */
382 /* store the info in the user's hand info */
383 DB_set_solo_by_hash($me,$_REQUEST['solo']);
384 DB_set_sickness_by_hash($me,'solo');
386 echo "<br />Seems like you want to play a {$_REQUEST['solo']} solo. Got it.<br />\n";
388 if($gametype == 'solo' && $startplayer<$mypos)
389 {}/* do nothing, since someone else already is playing solo */
392 /* this solo comes first
393 * store info in game table
395 DB_set_gametype_by_gameid($gameid,'solo');
396 DB_set_startplayer_by_gameid($gameid,$mypos);
397 DB_set_solo_by_gameid($gameid,$_REQUEST['solo']);
400 else if($_REQUEST['wedding'] == 'yes')
402 /* silent solo is set further down */
403 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
404 DB_set_sickness_by_hash($me,'wedding');
406 else if($_REQUEST['poverty'] == 'yes')
408 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
409 DB_set_sickness_by_hash($me,'poverty');
411 else if($_REQUEST['nines'] == 'yes')
413 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
414 " is playing solo, this game will be canceled.<br />\n";
415 DB_set_sickness_by_hash($me,'nines');
420 /* move on to the next stage*/
421 DB_set_hand_status_by_hash($me,'check');
426 /* here we check what all players said and figure out what game we are playing
427 * this can therefore only be handled once all players finished the last stage
430 $mycards = DB_get_hand($me);
431 $mycards = mysort($mycards,$gametype);
433 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
434 echo "\n<ul class=\"tricks\">\n";
435 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
436 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
437 " <div class=\"trick\" id=\"trick0\">\n";
439 for($pos=1;$pos<5;$pos++)
441 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
442 $userid = DB_get_userid('gameid-position',$gameid,$pos);
443 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
445 if($userstatus=='start' || $userstatus=='init')
446 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs <br />to decide </div>\n"; /* show this to everyone */
448 if($usersick!=NULL) /* in the init-phase we only showed players with $pos<$mypos, now we can show all */
449 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
451 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
453 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
454 /* end displaying sickness */
456 echo "<div class=\"message\">\n";
457 echo "<p> Checking if someone else selected solo, nines, wedding or poverty.</p>";
459 /* check if everyone has reached this stage */
460 $userids = DB_get_all_userid_by_gameid($gameid);
462 foreach($userids as $user)
464 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
465 if($userstat!='check')
468 DB_set_player_by_gameid($gameid,$user);
475 echo "<p>This step can only be handled after everyone finished the last step. ".
476 "Seems like this is not the case, so you need to wait a bit... ".
477 "you will get an email once that is the case, please use the link in ".
478 "that email to continue the game.</p></div>";
480 /* display cards, if player was just at the init-phase he will still see the cards from there
481 * we can put this one here, since the last player to finish the init state won't get here and
482 * will still see his card anyway from the init-phase
484 if($mystatus=='check')
487 echo "<div class=\"mycards\">Your cards are: <br />\n";
488 foreach($mycards as $card)
489 display_card($card,$PREF['cardset']);
496 /* Ok, everyone finished the init-phase, time to figure out what game we
497 * are playing, in case there are any solos this already
498 * will have the correct information in it */
500 echo "<p> Ok, everyone is done... figuring out what kind of game we are playing.</p>";
502 $gametype = DB_get_gametype_by_gameid($gameid);
503 $startplayer = DB_get_startplayer_by_gameid($gameid);
505 /* check for sickness */
510 foreach($userids as $user)
512 $name = DB_get_name('userid',$user);
513 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
514 if($usersick == 'nines')
517 break; /* no need to check for other poverties, since only solo can win and that is already set */
519 else if($usersick == 'poverty')
521 else if($usersick == 'wedding')
523 else if($usersick == 'solo')
527 /* now check which sickness comes first and set the gametype to it */
528 if($gametype == 'solo')
535 /* TODO: should we keep statistics of this? */
536 $message = "Hello, \n\n".
537 " the game has been canceled because ".DB_get_name('userid',$nines).
538 " has five or more nines and nobody is playing solo.\n\n".
539 " To redeal either start a new game or, in case the game was part of a tournament, \n".
540 " go to the last game and use the link at the bottom of the page to redeal.";
542 $userids = DB_get_all_userid_by_gameid($gameid);
543 foreach($userids as $user)
545 $To = DB_get_email('userid',$user);
546 mymail($To,"$EmailName game ".DB_format_gameid($gameid)." canceled",$message);
549 /* delete everything from the dB */
552 echo "The game has been canceled because ".DB_get_name('userid',$nines).
553 " has five or more nines and nobody is playing solo.\n";
556 else if($poverty==1) /* one person has poverty */
558 DB_set_gametype_by_gameid($gameid,'poverty');
559 $gametype = 'poverty';
560 $who = DB_get_sickness_by_gameid($gameid);
563 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
564 if($firstsick == 'poverty')
565 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
567 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
570 else if($poverty==2) /* two people have poverty */
572 DB_set_gametype_by_gameid($gameid,'dpoverty');
573 $gametype = 'dpoverty';
574 $who = DB_get_sickness_by_gameid($gameid);
577 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
578 if($firstsick == 'poverty')
580 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
581 if($secondsick == 'poverty')
582 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
584 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
587 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
592 DB_set_gametype_by_gameid($gameid,'wedding');
593 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
594 $gametype = 'wedding';
596 /* now the gametype is set correctly in the database */
597 echo "<p> Got it :)</p>";
599 /* loop over all players, set re/contra if possible and start the game if possible */
600 $userids = DB_get_all_userid_by_gameid($gameid);
601 foreach($userids as $userid)
603 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
608 /* are we the solo player? set us to re, else set us to contra */
609 $pos = DB_get_pos_by_hash($userhash);
610 if($pos == $startplayer)
611 DB_set_party_by_hash($userhash,'re');
613 DB_set_party_by_hash($userhash,'contra');
614 DB_set_hand_status_by_hash($userhash,'play');
618 /* set person with the wedding to re, do the rest during the game */
619 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
620 if($usersick == 'wedding')
621 DB_set_party_by_hash($userhash,'re');
623 DB_set_party_by_hash($userhash,'contra');
625 DB_set_hand_status_by_hash($userhash,'play');
629 $hand = DB_get_all_hand($userhash);
631 if(in_array('3',$hand)||in_array('4',$hand))
632 DB_set_party_by_hash($userhash,'re');
634 DB_set_party_by_hash($userhash,'contra');
635 DB_set_hand_status_by_hash($userhash,'play');
639 /* set person with poverty to play status */
640 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
641 if($usersick == 'poverty')
642 DB_set_hand_status_by_hash($userhash,'play');
644 /* set status of first player to be asked to poverty */
645 $who = DB_get_sickness_by_gameid($gameid);
646 if($who > 6) $who= $who/10; /* in case we have dpoverty */
647 $whoid = DB_get_userid('gameid-position',$gameid,$who);
649 DB_set_hand_status_by_hash($userhash,'poverty');
652 /* check for silent solo, set game type to solo in this case */
653 $gametype = DB_get_gametype_by_gameid($gameid);
654 $userids = DB_get_all_userid_by_gameid($gameid);
655 foreach($userids as $userid)
657 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
659 if($gametype=='normal')
661 $userhand = DB_get_all_hand($userhash);
662 if(check_wedding($userhand))
664 /* normal game type and player has both queens -> silent solo */
665 /* keep startplayer, just set gametype to silent solo */
666 DB_set_gametype_by_gameid($gameid,'solo');
667 DB_set_solo_by_gameid($gameid,'silent');
672 /* send out email to first player or poverty person*/
673 if($gametype!='poverty' && $gametype!='dpoverty')
675 $startplayer = DB_get_startplayer_by_gameid($gameid);
676 $email = DB_get_email('position-gameid',$startplayer,$gameid);
677 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
678 $who = DB_get_userid('email',$email);
679 DB_set_player_by_gameid($gameid,$who);
683 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
685 /* email startplayer */
686 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
687 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
688 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
692 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
696 /* set status of first player to be asked to poverty */
697 $who = DB_get_sickness_by_gameid($gameid);
698 if($who > 6) $who= $who/10; /* in case we have dpoverty */
700 $whoid = DB_get_userid('gameid-position',$gameid,$who);
702 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
705 $email = DB_get_email('position-gameid',$who,$gameid);
706 $whohash = DB_get_hash_from_game_and_pos($gameid,$who);
707 DB_set_player_by_gameid($gameid,$whoid);
709 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
711 /* email player for poverty */
712 $message = "Poverty: It's your turn now in game ".DB_format_gameid($gameid).".\n".
713 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$whohash."\n\n" ;
714 mymail($email,$EmailName."Poverty (game ".DB_format_gameid($gameid).") ",$message);
722 /* user only gets here in a poverty game, several things have to be handled here:
723 * A) ask, if user wants to take trump
725 * poverty: set re/contra
726 * dpoverty: first time: set re, send email to second player
727 * second time: set contra
728 * poverty: set status of other players to 'play'
729 * set status to play in case 0 trump
730 * no -> set status to play,
731 * ask next player or cancle the game if no more players
732 * B) user took trump and has too many cards (e.g. count(cards)>12 and re/contra set)
733 * ask to give cards back, set status to play, once player has 12 cards
735 * it is easier to check B) first
738 /* output pre game in case user reloads */
741 set_gametype($gametype); /* this sets the $CARDS variable */
742 $myparty = DB_get_party_by_hash($me);
744 /* the following is part B) of whats needs to be done)
745 /* check if user wants to give cards back */
746 if(myisset('exchange'))
748 $exchange = $_REQUEST['exchange'];
749 $partnerhash = DB_get_partner_hash_by_hash($me);
750 $partnerid = DB_get_userid('hash',$partnerhash);
751 $partnerhand = DB_get_handid('gameid-userid',$gameid,$partnerid);
753 /* if exchange is set to a value>0, exchange that card back to the partner */
756 $result = DB_query("UPDATE Hand_Card SET hand_id='$partnerhand'".
757 " WHERE hand_id='$myhand' AND card_id=".DB_quote_smart($exchange));
758 DB_add_exchanged_card(DB_quote_smart($exchange),$myhand,$partnerhand);
763 $mycards = DB_get_hand($me);
764 $mycards = mysort($mycards,$gametype);
766 /* check if user need to give more cards back */
767 if( ($myparty=='re' || $myparty=='contra') && count($mycards)>12)
769 echo "<div class=\"poverty\"> you need to get rid of a few cards</div>\n";
772 echo "<div class=\"mycards\">Your cards are: <br />\n";
773 foreach($mycards as $card)
774 display_link_card($card,$PREF['cardset'],$type);
775 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select card to give back\" />\n";
778 else if( ($myparty=='re' || $myparty=='contra') && count($mycards)==12)
780 /* user is done, ready to play */
781 DB_set_hand_status_by_hash($me,'play');
783 /* email start player */
784 $startplayer = DB_get_startplayer_by_gameid($gameid);
785 $email = DB_get_email('position-gameid',$startplayer,$gameid);
786 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
787 $who = DB_get_userid('email',$email);
788 DB_set_player_by_gameid($gameid,$who);
792 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
794 /* email startplayer */
795 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
796 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
797 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
801 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
804 /* the following is part A) of what needs to be done */
805 if(!myisset('trump'))
809 echo "<div class=\"poverty\">\n";
810 $userids = DB_get_all_userid_by_gameid($gameid);
811 foreach($userids as $user)
813 $name = DB_get_name('userid',$user);
814 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
815 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
816 $userparty = DB_get_party_by_hash($userhash);
818 if($usersick=='poverty' && !$userparty)
820 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
821 $cards = DB_get_hand($hash);
824 foreach($cards as $card)
825 if($card<27) $nrtrump++;
826 echo "Player $name has $nrtrump trump. Do you want to take them?".
827 "<a href=\"index.php?action=game&me=$me&trump=$user\">Yes</a> <br />\n";
830 echo "<a href=\"index.php?action=game&me=$me&trump=no\">No way</a> <br />\n";
831 echo "</div><div>\n";
833 echo "<div class=\"mycards\">Your cards are: <br />\n";
834 foreach($mycards as $card)
835 display_card($card,$PREF['cardset']);
836 echo "</div></div>\n";
842 $trump = $_REQUEST['trump'];
846 /* user doesn't want to take trump */
847 DB_set_hand_status_by_hash($me,'play');
849 /* set next player who needs to be asked and email him*/
850 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
851 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
853 /* don't ask people who have poverty */
855 if($firstsick=='poverty')
857 if($secondsick=='poverty')
862 if($gametype=='dpoverty')
864 $next=999; /* need to cancel for sure, since both would need to take the trump */
867 /* no more people to ask, need to cancel the game */
870 $message = "Hello, \n\n".
871 "Game ".DB_format_gameid($gameid)." has been canceled since nobody wanted to take the trump.\n";
873 $userids = DB_get_all_userid_by_gameid($gameid);
874 foreach($userids as $user)
876 $To = DB_get_email('userid',$user);
877 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled (poverty not resolved)",$message);
880 /* delete everything from the dB */
883 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been canceled.<br /><br /></p>";
888 /* email next player, set his status to poverty */
889 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
890 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
891 $userid = DB_get_userid('email',$To);
893 DB_set_player_by_gameid($gameid,$userid);
894 DB_set_hand_status_by_hash($userhash,'poverty');
896 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
897 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
898 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
903 /* player wants to take trump, change cards */
905 /* user wants to take trump */
906 $trump = $_REQUEST['trump'];
907 $userhand = DB_get_handid('gameid-userid',$gameid,$trump);
908 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
910 /* remember which cards were handed over*/
911 $partnerhand = DB_get_all_hand($userhash);
912 foreach ($partnerhand as $card)
914 DB_add_exchanged_card($card,$userhand,$myhand);
916 /* copy trump from player A to B */
917 $result = DB_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
920 $mycards = DB_get_hand($me);
923 if($gametype=='poverty')
925 $userids = DB_get_all_userid_by_gameid($gameid);
926 foreach($userids as $user)
928 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
929 if($hash==$userhash||$hash==$me)
931 DB_set_party_by_hash($hash,'re');
935 DB_set_party_by_hash($hash,'contra');
936 DB_set_hand_status_by_hash($hash,'play'); /* the contra party is ready to play */
939 /* check if we are done (in case of no trump handed over), if so, go to 'play' phase right away*/
940 if(count($mycards)==12)
942 DB_set_hand_status_by_hash($me,'play');
947 /* has the re party already been set?*/
949 $userids = DB_get_all_userid_by_gameid($gameid);
950 foreach($userids as $user)
952 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
953 $party = DB_get_party_by_hash($hash);
959 DB_set_party_by_hash($me,'contra');
960 DB_set_party_by_hash($userhash,'contra');
964 DB_set_party_by_hash($me,'re');
965 DB_set_party_by_hash($userhash,'re');
967 /* send out email to second non-poverty player */
968 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
969 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
972 if($firstsick=='poverty')
973 if($secondsick=='poverty')
979 echo "<div class=\"message\">Error in poverty, please contact the Admin</div>\n";
981 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
982 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
983 $userid = DB_get_userid('email',$To);
985 DB_set_player_by_gameid($gameid,$userid);
986 DB_set_hand_status_by_hash($userhash,'poverty');
988 $message = "Two people have poverty, it's your turn to decide, if you want to take the trump. Please visit:".
989 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
990 mymail($To,$EmailName." double poverty (game ".DB_format_gameid($gameid).")",$message);
995 echo "<div class=\"message\"> Please, <a href=\"$INDEX?action=game&me=$me\">continue</a> here.</div>\n";
1003 /* both entries here, so that the tricks are visible for both.
1004 * in case of 'play' there is a break later that skips the last part
1007 /* check if all players are ready to play,
1008 * if so, send out email to the startplayer
1009 * only need to do this if the game hasn't started yet
1011 $gamestatus = DB_get_game_status_by_gameid($gameid);
1012 if($gamestatus == 'pre')
1015 $userids = DB_get_all_userid_by_gameid($gameid);
1016 foreach($userids as $user)
1018 $userstatus = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
1019 if($userstatus !='play' && $userstatus!='gameover')
1022 DB_set_player_by_gameid($gameid,$user);
1028 /* only set this after all poverty, etc. are handled*/
1029 DB_set_game_status_by_gameid($gameid,'play');
1031 /* email startplayer */
1032 $startplayer = DB_get_startplayer_by_gameid($gameid);
1033 $email = DB_get_email('position-gameid',$startplayer,$gameid);
1034 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
1035 $who = DB_get_userid('email',$email);
1036 DB_set_player_by_gameid($gameid,$who);
1038 if($hash!=$me && DB_get_email_pref_by_hash($hash)!='emailaddict')
1040 /* email startplayer) */
1041 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
1042 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
1043 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
1047 /* figure out what kind of game we are playing,
1048 * set the global variables $CARDS['trump'],$CARDS['diamonds'],$CARDS['hearts'],
1049 * $CARDS['clubs'],$CARDS['spades'],$CARDS['foxes']
1053 $gametype = DB_get_gametype_by_gameid($gameid);
1055 if($gametype=='solo')
1057 $gametype = DB_get_solo_by_gameid($gameid);
1058 if($gametype=='silent')
1061 $GT = $gametype.' '.$GT;
1064 $gametype = 'normal';
1066 set_gametype($gametype); /* this sets the $CARDS variable */
1068 /* get some infos about the game, need to reset this, since it might have changed */
1069 $gamestatus = DB_get_game_status_by_gameid($gameid);
1071 /* has the game started? No, then just wait here...*/
1072 if($gamestatus == 'pre')
1074 echo "<p class=\"message\"> You finished the setup, but not everyone else finished it... ".
1075 "You need to wait for the others. Just wait for an email. </p>";
1077 $mycards = DB_get_hand($me);
1078 $mycards = mysort($mycards,$gametype);
1080 echo "<div class=\"mycards\">Your cards are: <br />\n";
1081 foreach($mycards as $card)
1082 display_card($card,$PREF['cardset']);
1085 break; /* not sure this works... the idea is that you can
1086 * only play a card after everyone is ready to play */
1089 /* get time from the last action of the game */
1090 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1091 $gameend = time() - strtotime($r[0]);
1093 /* handle comments in case player didn't play a card, allow comments a week after the end of the game */
1094 if( (!myisset('card') && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
1095 if(myisset('comment'))
1097 $comment = $_REQUEST['comment'];
1098 $playid = DB_get_current_playid($gameid);
1101 DB_insert_comment($comment,$playid,$myid);
1104 /* get everything relevant to display the tricks */
1105 $result = DB_query("SELECT Hand_Card.card_id as card,".
1106 " Hand.position as position,".
1107 " Play.sequence as sequence, ".
1109 " GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
1110 " SEPARATOR '\n' ), ".
1111 " Play.create_date, ".
1114 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1115 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1116 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1117 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1118 "LEFT JOIN User On User.id=Comment.user_id ".
1119 "WHERE Trick.game_id='".$gameid."' ".
1120 "GROUP BY Trick.id, sequence ".
1121 "ORDER BY Trick.id, sequence ASC");
1123 $lasttrick = DB_get_max_trickid($gameid);
1125 $play = array(); /* needed to calculate winner later */
1127 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1128 $firstcard = ''; /* first card in a trick */
1130 echo "\n<ul class=\"tricks\">\n";
1131 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
1133 /* output vorbehalte */
1134 $mygametype = DB_get_gametype_by_gameid($gameid);
1135 if($mygametype != 'normal' && $mygametype != 'silent') /* only show when needed */
1137 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
1138 " <div class=\"trick\" id=\"trick0\">\n";
1139 /* get information so show the cards that have been handed over in a poverty game */
1144 if($mygametype == 'poverty' || $mygametype=='dpoverty')
1146 /* who has poverty */
1147 for($mypos=1;$mypos<5;$mypos++)
1149 $usersick = DB_get_sickness_by_pos_and_gameid($mypos,$gameid);
1150 if($usersick == 'poverty')
1152 $povertypos2 = $mypos;
1154 $povertypos1 = $mypos;
1156 /* get hash and cards for all */
1157 $povertyhash1 = DB_get_hash_from_game_and_pos($gameid,$povertypos1);
1158 $partnerhash1 = DB_get_partner_hash_by_hash($povertyhash1);
1160 $povertycards1 = DB_get_exchanged_cards($povertyhash1);
1161 $partnercards1 = DB_get_exchanged_cards($partnerhash1);
1163 $partnerpos1 = DB_get_pos_by_hash($partnerhash1);
1166 $povertyhash2 = DB_get_hash_from_game_and_pos($gameid,$povertypos2);
1167 $partnerhash2 = DB_get_partner_hash_by_hash($povertyhash2);
1169 $povertycards2 = DB_get_exchanged_cards($povertyhash2);
1170 $partnercards2 = DB_get_exchanged_cards($partnerhash2);
1172 $partnerpos2 = DB_get_pos_by_hash($partnerhash2);
1177 for($mypos=1;$mypos<5;$mypos++)
1179 $usersick = DB_get_sickness_by_pos_and_gameid($mypos,$gameid);
1180 if($usersick!=NULL ||
1181 $mypos==$povertypos1 || $mypos==$partnerpos1 ||
1182 $mypos==$povertypos2 || $mypos==$partnerpos2 )
1184 echo " <div class=\"vorbehalt".($mypos-1)."\"> Vorbehalt <br />";
1186 echo " $usersick <br />";
1187 if($mypos==$partnerpos1)
1189 foreach($partnercards1 as $card)
1190 if($povertyhash1 == $me || $partnerhash1 == $me || $mystatus=='gameover')
1191 display_card($card,$PREF['cardset']);
1193 display_card(0,$PREF['cardset']);
1195 else if($mypos==$povertypos1)
1197 foreach($povertycards1 as $card)
1198 if($povertyhash1 == $me || $partnerhash1 == $me || $mystatus=='gameover')
1199 display_card($card,$PREF['cardset']);
1201 display_card(0,$PREF['cardset']);
1203 else if($mypos==$povertypos2)
1205 foreach($povertycards2 as $card)
1206 if($povertyhash2 == $me || $partnerhash2 == $me || $mystatus=='gameover')
1207 display_card($card,$PREF['cardset']);
1209 display_card(0,$PREF['cardset']);
1211 else if($mypos==$partnerpos2)
1213 foreach($partnercards2 as $card)
1214 if($povertyhash2 == $me || $partnerhash2 == $me || $mystatus=='gameover')
1215 display_card($card,$PREF['cardset']);
1217 display_card(0,$PREF['cardset']);
1222 if($mygametype == $usersick)
1226 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1230 while($r = DB_fetch_array($result))
1238 /* count number of tricks */
1242 /* check if first schweinchen has been played */
1243 if( $GAME['schweinchen-who'] && ($r[0] == 19 || $r[0] == 20) )
1244 if(!$GAME['schweinchen-first'])
1245 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1247 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1249 /* save card to be able to find the winner of the trick later */
1250 $play[$seq] = array('card'=>$r[0],'pos'=>$pos);
1254 /* first card in a trick, output some html */
1255 if($trick!=$lasttrick)
1257 /* start of an old trick? */
1258 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1259 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1260 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1262 else if($trick==$lasttrick)
1264 /* start of a last trick? */
1265 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1266 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1267 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1270 /* remember first card, so that we are able to check, what cards can be played */
1275 echo " <div class=\"card".($pos-1)."\">\n";
1277 /* display comments */
1279 echo " <span class=\"comment\">".$comment."</span>\n";
1282 display_card($r[0],$PREF['cardset']);
1284 echo " </div>\n"; /* end div card */
1289 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1293 /* whos turn is it? */
1296 $winner = get_winner($play,$gametype); /* returns the position */
1298 $firstcard = ''; /* new trick, no first card */
1303 if($next==5) $next = 1;
1306 /* my turn?, display cards as links, ask for comments*/
1307 if(DB_get_pos_by_hash($me) == $next)
1312 /* do we want to play a card? */
1313 if(myisset('card') && $myturn)
1315 $card = $_REQUEST['card'];
1316 $handid = DB_get_handid('hash',$me);
1317 $commentSchweinchen =''; /* used to add a comment when Schweinchen is being played */
1319 /* check if we have card and that we haven't played it yet*/
1320 /* set played in hand_card to true where hand_id and card_id*/
1321 $r = DB_query_array("SELECT id FROM Hand_Card WHERE played='false' and ".
1322 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1323 $handcardid = $r[0];
1325 if($handcardid) /* everything ok, play card */
1327 /* update Game timestamp */
1328 DB_update_game_timestamp($gameid);
1330 /* mark card as played */
1331 DB_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1332 DB_quote_smart($card));
1334 /* get trick id or start new trick */
1335 $a = DB_get_current_trickid($gameid);
1340 $playid = DB_play_card($trickid,$handcardid,$sequence);
1342 /* check special output for schweinchen in case in case a fox is being played
1343 * check for correct rules, etc. has already been done
1345 if( $GAME['schweinchen-who'] && ($card == 19 || $card == 20) )
1347 if(!$GAME['schweinchen-first'])
1348 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1350 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1352 if( $RULES['schweinchen']=='both' ||
1353 ($RULES['schweinchen']=='second' && $GAME['schweinchen-second']==1 )||
1354 ($RULES['schweinchen']=='secondaftercall' && $GAME['schweinchen-second']==1 &&
1355 (DB_get_call_by_hash($GAME['schweinchen-who']) || DB_get_partner_call_by_hash($GAME['schweinchen-who']) ))
1358 DB_insert_comment('Schweinchen! ',$playid,$myid);
1359 $commentSchweinchen = 'Schweinchen! ';
1362 echo 'schweinchen = '.$GAME['schweinchen-who'].' ---<br />';
1365 /* if sequence == 4 check who one in case of wedding */
1366 if($sequence == 4 && $GT == 'wedding')
1368 /* is wedding resolve */
1369 $resolved = DB_get_sickness_by_gameid($gameid);
1372 /* who has wedding */
1373 $userids = DB_get_all_userid_by_gameid($gameid);
1374 foreach($userids as $user)
1376 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1377 if($usersick == 'wedding')
1380 /* who won the trick */
1381 $play = DB_get_cards_by_trick($trickid);
1382 $winner = get_winner($play,$gametype); /* returns the position */
1383 $winnerid = DB_get_userid('gameid-position',$gameid,$winner);
1384 /* is tricknr <=3 */
1385 if($tricknr <=3 && $winnerid!=$whosick)
1387 /* set resolved at tricknr*/
1388 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1390 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1391 DB_set_party_by_hash($whash,'re');
1393 if($tricknr == 3 && $winnerid==$whosick)
1395 /* set resolved at tricknr*/
1396 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1401 /* if sequence == 4, set winner of the trick, count points and set the next player */
1404 $play = DB_get_cards_by_trick($trickid);
1405 $winner = get_winner($play,$gametype); /* returns the position */
1407 /* check if someone caught a fox */
1408 /* first check if we should account for solos at all,
1409 * since it doesn't make sense in some games
1411 $ok = 0; /* fox shouldn't be counted */
1412 if(DB_get_gametype_by_gameid($gameid)=='solo')
1414 $solo = DB_get_solo_by_gameid($gameid);
1415 if($solo == 'trump' || $solo == 'silent')
1416 $ok = 1; /* for trump solos and silent solos, foxes are ok */
1419 $ok = 1; /* for all other games (not solos) foxes are ok too */
1422 foreach($play as $played)
1424 if ( $played['card']==19 || $played['card']==20 )
1425 if ($played['pos']!= $winner )
1427 /* possible caught a fox, check party */
1428 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1429 $uid2 = DB_get_userid('gameid-position',$gameid,$played['pos']);
1431 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1432 $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1434 if($party1 != $party2)
1435 DB_query("INSERT INTO Score".
1436 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1440 /* check for karlchen (jack of clubs in the last trick)*/
1441 /* same as for foxes, karlchen doesn't always make sense
1442 * check what kind of game it is and set karlchen accordingly */
1443 $ok = 1; /* default: karlchen should be accounted for */
1445 $ok = 0; /* Karlchen works only in the last trick */
1446 if($ok && DB_get_gametype_by_gameid($gameid)=='solo' )
1448 $solo = DB_get_solo_by_gameid($gameid);
1449 if($solo == 'trumpless' || $solo == 'jack' || $solo == 'queen' )
1450 $ok = 0; /* no Karlchen in these solos */
1454 foreach($play as $played)
1455 if ( $played['card']==11 || $played['card']==12 )
1456 if ($played['pos'] == $winner )
1458 /* possible caught a fox, check party */
1459 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1460 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1462 DB_query("INSERT INTO Score".
1463 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1465 /* check for doppelopf (>40 points)*/
1467 foreach($play as $played)
1469 $points += DB_get_card_value_by_cardid($played['card']);
1473 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1474 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1476 DB_query("INSERT INTO Score".
1477 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
1481 DB_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1483 echo "ERROR during scoring";
1486 echo "DEBUG: position $winner won the trick <br />";
1488 /* who is the next player? */
1493 $next = DB_get_pos_by_hash($me)+1;
1495 if($next==5) $next=1;
1497 /* check for coment */
1498 if(myisset('comment'))
1500 $comment = $_REQUEST['comment'];
1502 DB_insert_comment($comment,$playid,$myid);
1503 if($commentSchweinchen)
1504 $comment = $commentSchweinchen . $comment;
1507 /* display played card */
1508 $pos = DB_get_pos_by_hash($me);
1511 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1512 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1513 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1516 echo " <div class=\"card".($pos-1)."\">\n ";
1518 /* display comments */
1519 display_card($card,$PREF['cardset']);
1521 echo "\n <span class=\"comment\"> ".$comment."</span>\n";
1524 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1526 /*check if we still have cards left, else set status to gameover */
1527 if(sizeof(DB_get_hand($me))==0)
1529 DB_set_hand_status_by_hash($me,'gameover');
1530 $mystatus = 'gameover';
1533 /* if all players are done, set game status to game over,
1534 * get the points of the last trick and send out an email
1537 $userids = DB_get_all_userid_by_gameid($gameid);
1540 foreach($userids as $user)
1541 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1545 DB_set_game_status_by_gameid($gameid,'gameover');
1547 /* email next player, if game is still running */
1548 if(DB_get_game_status_by_gameid($gameid)=='play')
1550 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1551 $email = DB_get_email('hash',$next_hash);
1552 $who = DB_get_userid('email',$email);
1553 DB_set_player_by_gameid($gameid,$who);
1555 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1556 "It's your turn now.\n".
1557 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$next_hash."\n\n" ;
1558 if( DB_get_email_pref_by_uid($who)!='emailaddict' )
1559 mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
1561 else /* send out final email */
1563 /* individual score */
1564 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1565 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1566 " LEFT JOIN User ON User.id=Hand.user_id".
1567 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1568 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1569 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1570 " WHERE Hand.game_id='$gameid'".
1571 " GROUP BY User.fullname" );
1572 $message = "The game is over. Thanks for playing :)\n";
1573 $message .= "Final score:\n";
1574 while( $r = DB_fetch_array($result) )
1575 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1577 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) 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 Hand.party" );
1585 $message .= "\nTotals:\n";
1588 while( $r = DB_fetch_array($result) )
1590 $message .= " ".$r[0]." ".$r[1]."\n";
1593 else if($r[0] == 'contra')
1598 * save score in database
1602 /* get calls from re/contra */
1604 $call_contra = NULL;
1605 foreach($userids as $user)
1607 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1608 $call = DB_get_call_by_hash($hash);
1609 $party = DB_get_party_by_hash($hash);
1613 $call = (int) $call;
1619 else if( $call < $call_re)
1622 else if($party=='contra')
1624 if($call_contra==NULL)
1625 $call_contra = $call;
1626 else if( $call < $call_contra)
1627 $call_contra = $call;
1632 /* figure out who one */
1633 $winning_party = NULL;
1635 if($call_re == NULL && $call_contra==NULL)
1637 /* nobody made a call, so it's easy to figure out who won */
1639 $winning_party='re';
1641 $winning_party='contra';
1645 /* if one party makes a call, they only win, iff they make enough points
1646 * if only one party made a call, the other one wins,
1647 * if the first one didn't make it
1651 $offset = 120 - $call_re;
1653 $offset--; /* since we use a > in the next equation */
1655 if($re > 120+$offset)
1656 $winning_party='re';
1657 else if ($call_contra == NULL )
1658 $winning_party='contra';
1663 $offset = 120 - $call_contra;
1664 if($call_contra == 0)
1665 $offset--; /* since we use a > in the next equation */
1667 if($contra > 120+$offset)
1668 $winning_party='contra';
1669 else if ($call_re == NULL )
1670 $winning_party='re';
1674 /* one point for each call of the other party in case the other party didn't win
1675 * and one point each in case the party made more than points than one of the calls
1677 if($winning_party!='contra' && $call_contra!=NULL)
1679 for( $p=$call_contra;$p<=120; $p+=30 )
1681 DB_query("INSERT INTO Score".
1682 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
1685 for( $p=$call_contra; $p<120; $p+=30)
1688 DB_query("INSERT INTO Score".
1689 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'made$p')");
1692 if($winning_party!='re' and $call_re!=NULL)
1694 for( $p=$call_re;$p<=120; $p+=30 )
1696 DB_query("INSERT INTO Score".
1697 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'against$p')");
1700 for( $p=$call_re; $p<120; $p+=30)
1703 DB_query("INSERT INTO Score".
1704 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
1708 /* point in case contra won */
1709 if($winning_party=='contra')
1711 DB_query("INSERT INTO Score".
1712 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1715 /* one point each for winning and each 30 points + calls */
1716 if($winning_party=='re')
1718 foreach(array(120,150,180,210,240) as $p)
1721 if($p==240 || $call_contra!=NULL)
1725 DB_query("INSERT INTO Score".
1726 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
1728 /* re called something and won */
1729 foreach(array(0,30,60,90,120) as $p)
1731 if($call_re!=NULL && $call_re<$p+1)
1732 DB_query("INSERT INTO Score".
1733 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
1736 else if( $winning_party=='contra')
1738 foreach(array(120,150,180,210,240) as $p)
1741 if($p==240 || $call_re!=NULL)
1744 if($contra>$p-$offset)
1745 DB_query("INSERT INTO Score".
1746 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
1748 /* re called something and won */
1749 foreach(array(0,30,60,90,120) as $p)
1751 if($call_contra!=NULL && $call_contra<$p+1)
1752 DB_query("INSERT INTO Score".
1753 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
1758 /* add score points to email */
1761 $message .= " Points Re: \n";
1762 $queryresult = DB_query("SELECT score FROM Score ".
1763 " WHERE game_id=$gameid AND party='re'".
1765 while($r = DB_fetch_array($queryresult) )
1767 $message .= " ".$r[0]."\n";
1770 $message .= " Points Contra: \n";
1771 $queryresult = DB_query("SELECT score FROM Score ".
1772 " WHERE game_id=$gameid AND party='contra'".
1774 while($r = DB_fetch_array($queryresult) )
1776 $message .= " ".$r[0]."\n";
1779 $message .= " Total Points (from the Re point of view): $Tpoint\n";
1782 $session = DB_get_session_by_gameid($gameid);
1783 $score = generate_score_table($session);
1785 $message .= "Score Table:\n";
1786 $message .= format_score_table_ascii($score);
1787 $message .= "\nUse these links to have a look at game ".DB_format_gameid($gameid).": \n";
1789 /* send out final email */
1792 foreach($userids as $user)
1794 $all[] = DB_get_email('userid',$user);
1796 /* add links for all players */
1797 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1798 $name = DB_get_name('userid',$user);
1800 $link = "$name: ".$HOST.$INDEX."?action=game&me=".$hash."\n" ;
1803 $To = implode(",",$all);
1805 $message .= "\n\n (you can use reply all on this email to reach all the players.)\n";
1806 mymail($To,$EmailName."Game over (game ".DB_format_gameid($gameid).") ",$message);
1811 echo "can't find that card?! <br />\n";
1814 else if(myisset('card') && !$myturn )
1816 echo "please wait until it's your turn! <br />\n";
1819 if($seq!=4 && $trickNR>=1 && !(myisset('card') && $myturn) )
1820 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1822 /* display points in case game is over */
1823 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1825 echo " <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1826 " <div class=\"trick\" id=\"trick13\">\n";
1827 /* add pic for re/contra
1828 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1830 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1831 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1832 " LEFT JOIN User ON User.id=Hand.user_id".
1833 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1834 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1835 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1836 " WHERE Hand.game_id='$gameid'".
1837 " GROUP BY User.fullname" );
1838 while( $r = DB_fetch_array($result))
1839 echo " <div class=\"card".($r[3]-1)."\">\n".
1840 " <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
1843 /* display totals */
1844 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1845 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1846 " LEFT JOIN User ON User.id=Hand.user_id".
1847 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1848 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1849 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1850 " WHERE Hand.game_id='$gameid'".
1851 " GROUP BY Hand.party" );
1852 echo "<div class=\"total\">\n Totals:<br />\n";
1853 while( $r = DB_fetch_array($result))
1854 echo " ".$r[0]." ".$r[1]."<br />\n";
1856 $queryresult = DB_query("SELECT timediff(mod_date,create_date) ".
1857 " FROM Game WHERE id='$gameid'");
1858 $r = DB_fetch_array($queryresult);
1859 echo " <p>This game took ".$r[0]." hours.</p>\n";
1861 echo " <div class=\"re\">\n Points Re: <br />\n";
1862 $queryresult = DB_query("SELECT score FROM Score ".
1863 " WHERE game_id=$gameid AND party='re'".
1865 while($r = DB_fetch_array($queryresult) )
1866 echo " ".$r[0]."<br />\n";
1869 echo " <div class=\"contra\">\n Points Contra: <br />\n";
1870 $queryresult = DB_query("SELECT score FROM Score ".
1871 " WHERE game_id=$gameid AND party='contra'".
1873 while($r = DB_fetch_array($queryresult) )
1874 echo " ".$r[0]."<br />\n";
1879 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1882 echo " <li onclick=\"hl_prev();\" class=\"old\"><a href=\"#\">prev</a></li>\n";
1883 echo " <li onclick=\"hl_next();\" class=\"old\"><a href=\"#\">next</a></li>\n";
1884 echo "</ul>\n"; /* end ul tricks*/
1886 $mycards = DB_get_hand($me);
1887 $mycards = mysort($mycards,$gametype);
1888 echo "<div class=\"mycards\">\n";
1890 if($myturn && !myisset('card') && $mystatus=='play' )
1892 echo "Hello ".$myname.", it's your turn! <br />\n";
1893 echo "Your cards are: <br />\n";
1895 /* do we have to follow suite? */
1897 if(have_suit($mycards,$firstcard))
1900 foreach($mycards as $card)
1902 /* display only cards that the player is allowed to play as links, the rest just display normal
1903 * also check if we have both schweinchen, in that case only display on of them as playable
1905 if( ($followsuit && !same_type($card,$firstcard)) ||
1906 ( (int)($card)==19 && !$GAME['schweinchen-first'] &&
1907 ($RULES['schweinchen']=='second'||
1908 ( $RULES['schweinchen']=='secondaftercall' &&
1909 (DB_get_call_by_hash($GAME['schweinchen-who']) || DB_get_partner_call_by_hash($GAME['schweinchen-who']) )))
1910 && $GAME['schweinchen-who']==$me )
1912 display_card($card,$PREF['cardset']);
1914 display_link_card($card,$PREF['cardset']);
1917 else if($mystatus=='play' )
1919 echo "Your cards are: <br />\n";
1920 foreach($mycards as $card)
1921 display_card($card,$PREF['cardset']);
1923 else if($mystatus=='gameover')
1925 $oldcards = DB_get_all_hand($me);
1926 $oldcards = mysort($oldcards,$gametype);
1927 echo "Your cards were: <br />\n";
1928 foreach($oldcards as $card)
1929 display_card($card,$PREF['cardset']);
1931 $userids = DB_get_all_userid_by_gameid($gameid);
1932 foreach($userids as $user)
1934 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1940 $name = DB_get_name('userid',$user);
1941 $oldcards = DB_get_all_hand($userhash);
1942 $oldcards = mysort($oldcards,$gametype);
1943 echo "$name's cards were: <br />\n";
1944 foreach($oldcards as $card)
1945 display_card($card,$PREF['cardset']);
1951 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1952 if($mystatus=='play')
1955 /* the following happens only when the gamestatus is 'gameover' */
1956 /* check if game is over, display results */
1957 if(DB_get_game_status_by_gameid($gameid)=='play')
1959 echo "The game is over for you.. other people still need to play though";
1963 myerror("error in testing the status");
1966 /* output other games where it is the users turn
1967 * make sure that the people looking at old games don't see the wrong games here
1969 if( $mystatus != 'gameover' )
1970 display_user_menu($myid);
1971 else if( $mystatus == 'gameover' &&
1972 isset($_SESSION['id']) )
1974 display_user_menu($_SESSION['id']);
1978 echo "<div class=\"usermenu\">\n";
1979 echo "It's your turn in these games:<br />\n";
1980 echo "Please log in to see this information.\n";
1984 /* display rule set for this game */
1985 echo "<div class=\"gameinfo\">\n";
1987 if($gamestatus == 'play' )
1989 $myparty = DB_get_party_by_hash($me);
1990 output_form_calls($me,$myparty);
1992 /* get time from the last action of the game */
1993 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1994 $gameend = time() - strtotime($r[0]);
1996 if($gamestatus == 'play' || $gameend < 60*60*24*7)
1998 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
2001 echo "<input type=\"submit\" value=\"submit\" />\n";
2003 /* has this hand been played by others? */
2004 $other_game_ids = DB_played_by_others($gameid);
2005 if(sizeof($other_game_ids)>0 && $mystatus=='gameover')
2007 $mypos = DB_get_pos_by_hash($me);
2008 echo "<p>See how other played the same hand: <br />\n";
2009 foreach($other_game_ids as $id)
2011 $otherhash = DB_get_hash_from_game_and_pos($id,$mypos);
2012 $othername = DB_get_name('hash',$otherhash);
2013 echo "<a href=\"$INDEX?action=game&me=$otherhash\">$othername</a><br />";
2022 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' && isset($_SESSION['id']) && $_SESSION['id']==$myid)
2024 $session = DB_get_session_by_gameid($gameid);
2025 $result = DB_query("SELECT id,create_date FROM Game".
2026 " WHERE session=$session".
2027 " ORDER BY create_date DESC".
2031 $r = DB_fetch_array($result);
2033 if(!$session || $gameid==$r[0])
2035 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
2036 $names = DB_get_all_names_by_gameid($gameid);
2037 $type = DB_get_gametype_by_gameid($gameid);
2041 $solo = DB_get_solo_by_gameid($gameid);
2043 if($solo!='silent') /* repeat game with same first player */
2044 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
2045 else /* rotate normally */
2046 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
2048 else /* rotate normally */
2049 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);