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' )
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 (+icons for rules) \n";
131 echo " 10ofhearts : {$RULES['dullen']} <br />\n";
132 echo " schweinchen: {$RULES['schweinchen']} <br />\n";
133 echo " call: {$RULES['call']} <br />\n";
134 echo " </div>\n </div>\n";
135 echo " <div class=\"sessionscore\">Score \n";
136 $score = generate_score_table($session);
137 echo format_score_table_html($score,$myid);
139 $hashes = DB_get_hashes_by_session($session,$myid);
141 foreach($hashes as $hash)
149 echo "This is game number $j of <a href=\"{$INDEX}?action=game&me=$lasthash\">$i</a> in session $session.";
153 /* display the table and the names */
156 /* mystatus gets the player through the different stages of a game.
157 * start: does the player want to play?
158 * init: check for sickness
159 * check: check for return values from init
160 * poverty: handle poverty, wait here until all player have reached this state
161 * display sickness and move on to game
162 * play: game in progress
163 * gameover: are we revisiting a game
168 /* don't ask if user has autosetup set to yest */
170 if($PREF['autosetup']=='yes') $skip = 1;
172 if( !myisset('in') && !$skip)
174 /* asks the player, if he wants to join the game */
175 output_check_want_to_play($me);
180 /* check the result, if player wants to join, got next stage, else cancel game */
181 if(!$skip && $_REQUEST['in'] == 'no' )
183 /* cancel the game */
184 $message = "Hello, \n\n".
185 "the game has been canceled due to the request of one of the players.\n";
187 $userids = DB_get_all_userid_by_gameid($gameid);
188 foreach($userids as $user)
190 $To = DB_get_email('userid',$user);
191 mymail($To,"$EmailName game ".DB_format_gameid($gameid)." canceled",$message);
194 /* delete everything from the dB */
200 /* user wants to join the game */
202 /* move on to the next stage,
203 * no break statement to immediately go to the next stage
206 DB_set_hand_status_by_hash($me,'init');
208 /* check if everyone has reached this stage, send out email */
209 $userids = DB_get_all_userid_by_gameid($gameid);
211 foreach($userids as $user)
213 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
214 if($userstat!='init')
216 /* whos turn is it? */
217 DB_set_player_by_gameid($gameid,$user);
224 /* all done, send out email unless this player is the startplayer */
225 $startplayer = DB_get_startplayer_by_gameid($gameid);
226 if($mypos == $startplayer)
228 /* do nothing, go to next stage */
232 /* email startplayer */
234 $email = DB_get_email('position-gameid',$startplayer,$gameid);
235 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
236 $who = DB_get_userid('email',$email);
237 DB_set_player_by_gameid($gameid,$who);
239 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
240 "Use this link to go the game: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
241 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
248 /* here we ask the player if he is sick */
249 $mycards = DB_get_hand($me);
250 $mycards = mysort($mycards,$gametype);
252 if(!myisset('solo','wedding','poverty','nines') )
254 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
255 echo "\n<ul class=\"tricks\">\n";
256 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
257 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
258 " <div class=\"trick\" id=\"trick0\">\n";
260 for($pos=1;$pos<5;$pos++)
262 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
263 $userid = DB_get_userid('gameid-position',$gameid,$pos);
264 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
266 if($userstatus=='start' || $userstatus=='init')
267 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs to decide </div>\n"; /* show this to everyone */
269 if($usersick!=NULL && $pos<=$mypos ) /* only show this for people sitting before the player */
270 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
271 else if($usersick==NULL && $pos<=$mypos)
272 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
274 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
275 /* end displaying sickness */
277 output_check_for_sickness($me,$mycards);
279 echo "<div class=\"mycards\">Your cards are: <br />\n";
280 foreach($mycards as $card)
281 display_card($card,$PREF['cardset']);
288 /* check if someone selected more than one sickness */
290 if($_REQUEST['solo']!='No') $Nsickness++;
291 if($_REQUEST['wedding'] == 'yes') $Nsickness++;
292 if($_REQUEST['poverty'] == 'yes') $Nsickness++;
293 if($_REQUEST['nines'] == 'yes') $Nsickness++;
297 echo "<p class=\"message\"> You selected more than one sickness, please go back ".
298 "and answer the <a href=\"$INDEX?action=game&me=$me&in=yes\">question</a> again.</p>";
300 echo "<div class=\"mycards\">Your cards are: <br />\n";
301 foreach($mycards as $card)
302 display_card($card,$PREF['cardset']);
309 /* everything is ok, save what user said and proceed */
310 echo "<p class=\"message\">Processing what you selected in the last step...";
312 /* check if this sickness needs to be handled first */
313 $gametype = DB_get_gametype_by_gameid($gameid);
314 $startplayer = DB_get_startplayer_by_gameid($gameid); /* need this to check which solo goes first */
316 if( $_REQUEST['solo']!='No' )
318 /* user wants to play a solo */
320 /* store the info in the user's hand info */
321 DB_set_solo_by_hash($me,$_REQUEST['solo']);
322 DB_set_sickness_by_hash($me,'solo');
324 echo "<br />Seems like you want to play a {$_REQUEST['solo']} solo. Got it.<br />\n";
326 if($gametype == 'solo' && $startplayer<$mypos)
327 {}/* do nothing, since someone else already is playing solo */
330 /* this solo comes first
331 * store info in game table
333 DB_set_gametype_by_gameid($gameid,'solo');
334 DB_set_startplayer_by_gameid($gameid,$mypos);
335 DB_set_solo_by_gameid($gameid,$_REQUEST['solo']);
338 else if($_REQUEST['wedding'] == 'yes')
340 /* silent solo is set further down */
341 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
342 DB_set_sickness_by_hash($me,'wedding');
344 else if($_REQUEST['poverty'] == 'yes')
346 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
347 DB_set_sickness_by_hash($me,'poverty');
349 else if($_REQUEST['nines'] == 'yes')
351 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
352 " is playing solo, this game will be canceled.<br />\n";
353 DB_set_sickness_by_hash($me,'nines');
358 /* move on to the next stage*/
359 DB_set_hand_status_by_hash($me,'check');
364 /* here we check what all players said and figure out what game we are playing
365 * this can therefore only be handled once all players finished the last stage
368 $mycards = DB_get_hand($me);
369 $mycards = mysort($mycards,$gametype);
371 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
372 echo "\n<ul class=\"tricks\">\n";
373 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
374 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
375 " <div class=\"trick\" id=\"trick0\">\n";
377 for($pos=1;$pos<5;$pos++)
379 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
380 $userid = DB_get_userid('gameid-position',$gameid,$pos);
381 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
383 if($userstatus=='start' || $userstatus=='init')
384 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs to decide </div>\n"; /* show this to everyone */
386 if($usersick!=NULL) /* in the init-phase we only showed players with $pos<$mypos, now we can show all */
387 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
389 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
391 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
392 /* end displaying sickness */
394 echo "<div class=\"message\">\n";
395 echo "<p> Checking if someone else selected solo, nines, wedding or poverty.</p>";
397 /* check if everyone has reached this stage */
398 $userids = DB_get_all_userid_by_gameid($gameid);
400 foreach($userids as $user)
402 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
403 if($userstat!='check')
406 DB_set_player_by_gameid($gameid,$user);
413 echo "<p>This step can only be handled after everyone finished the last step. ".
414 "Seems like this is not the case, so you need to wait a bit... ".
415 "you will get an email once that is the case, please use the link in ".
416 "that email to continue the game.</p></div>";
418 /* display cards, if player was just at the init-phase he will still see the cards from there
419 * we can put this one here, since the last player to finish the init state won't get here and
420 * will still see his card anyway from the init-phase
422 if($mystatus=='check')
425 echo "<div class=\"mycards\">Your cards are: <br />\n";
426 foreach($mycards as $card)
427 display_card($card,$PREF['cardset']);
434 /* Ok, everyone finished the init-phase, time to figure out what game we
435 * are playing, in case there are any solos this already
436 * will have the correct information in it */
438 echo "<p> Ok, everyone is done... figuring out what kind of game we are playing.</p>";
440 $gametype = DB_get_gametype_by_gameid($gameid);
441 $startplayer = DB_get_startplayer_by_gameid($gameid);
443 /* check for sickness */
448 foreach($userids as $user)
450 $name = DB_get_name('userid',$user);
451 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
452 if($usersick == 'nines')
455 break; /* no need to check for other poverties, since only solo can win and that is already set */
457 else if($usersick == 'poverty')
459 else if($usersick == 'wedding')
461 else if($usersick == 'solo')
465 /* now check which sickness comes first and set the gametype to it */
466 if($gametype == 'solo')
473 /* TODO: should we keep statistics of this? */
474 $message = "Hello, \n\n".
475 " the game has been canceled because ".DB_get_name('userid',$nines).
476 " has five or more nines and nobody is playing solo.\n\n".
477 " To redeal either start a new game or, in case the game was part of a tournament, \n".
478 " go to the last game and use the link at the bottom of the page to redeal.";
480 $userids = DB_get_all_userid_by_gameid($gameid);
481 foreach($userids as $user)
483 $To = DB_get_email('userid',$user);
484 mymail($To,"$EmailName game ".DB_format_gameid($gameid)." canceled",$message);
487 /* delete everything from the dB */
490 echo "The game has been canceled because ".DB_get_name('userid',$nines).
491 " has five or more nines and nobody is playing solo.\n";
494 else if($poverty==1) /* one person has poverty */
496 DB_set_gametype_by_gameid($gameid,'poverty');
497 $gametype = 'poverty';
498 $who = DB_get_sickness_by_gameid($gameid);
501 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
502 if($firstsick == 'poverty')
503 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
505 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
508 else if($poverty==2) /* two people have poverty */
510 DB_set_gametype_by_gameid($gameid,'dpoverty');
511 $gametype = 'dpoverty';
512 $who = DB_get_sickness_by_gameid($gameid);
515 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
516 if($firstsick == 'poverty')
518 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
519 if($secondsick == 'poverty')
520 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
522 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
525 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
530 DB_set_gametype_by_gameid($gameid,'wedding');
531 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
532 $gametype = 'wedding';
534 /* now the gametype is set correctly in the database */
535 echo "<p> Got it :)</p>";
537 /* loop over all players, set re/contra if possible and start the game if possible */
538 $userids = DB_get_all_userid_by_gameid($gameid);
539 foreach($userids as $userid)
541 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
546 /* are we the solo player? set us to re, else set us to contra */
547 $pos = DB_get_pos_by_hash($userhash);
548 if($pos == $startplayer)
549 DB_set_party_by_hash($userhash,'re');
551 DB_set_party_by_hash($userhash,'contra');
552 DB_set_hand_status_by_hash($userhash,'play');
556 /* set person with the wedding to re, do the rest during the game */
557 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
558 if($usersick == 'wedding')
559 DB_set_party_by_hash($userhash,'re');
561 DB_set_party_by_hash($userhash,'contra');
563 DB_set_hand_status_by_hash($userhash,'play');
567 $hand = DB_get_all_hand($userhash);
569 if(in_array('3',$hand)||in_array('4',$hand))
570 DB_set_party_by_hash($userhash,'re');
572 DB_set_party_by_hash($userhash,'contra');
573 DB_set_hand_status_by_hash($userhash,'play');
577 /* set person with poverty to play status */
578 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
579 if($usersick == 'poverty')
580 DB_set_hand_status_by_hash($userhash,'play');
582 /* set status of first player to be asked to poverty */
583 $who = DB_get_sickness_by_gameid($gameid);
584 if($who > 6) $who= $who/10; /* in case we have dpoverty */
585 $whoid = DB_get_userid('gameid-position',$gameid,$who);
587 DB_set_hand_status_by_hash($userhash,'poverty');
590 /* check for silent solo, set game type to solo in this case */
591 $gametype = DB_get_gametype_by_gameid($gameid);
592 $userids = DB_get_all_userid_by_gameid($gameid);
593 foreach($userids as $userid)
595 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
597 if($gametype=='normal')
599 $userhand = DB_get_all_hand($userhash);
600 if(check_wedding($userhand))
602 /* normal game type and player has both queens -> silent solo */
603 /* keep startplayer, just set gametype to silent solo */
604 DB_set_gametype_by_gameid($gameid,'solo');
605 DB_set_solo_by_gameid($gameid,'silent');
610 /* send out email to first player or poverty person*/
611 if($gametype!='poverty' && $gametype!='dpoverty')
613 $startplayer = DB_get_startplayer_by_gameid($gameid);
614 $email = DB_get_email('position-gameid',$startplayer,$gameid);
615 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
616 $who = DB_get_userid('email',$email);
617 DB_set_player_by_gameid($gameid,$who);
621 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
623 /* email startplayer */
624 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
625 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
626 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
630 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
634 /* set status of first player to be asked to poverty */
635 $who = DB_get_sickness_by_gameid($gameid);
636 if($who > 6) $who= $who/10; /* in case we have dpoverty */
638 $whoid = DB_get_userid('gameid-position',$gameid,$who);
640 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
643 $email = DB_get_email('position-gameid',$who,$gameid);
644 $whohash = DB_get_hash_from_game_and_pos($gameid,$who);
645 DB_set_player_by_gameid($gameid,$whoid);
647 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
649 /* email player for poverty */
650 $message = "Poverty: It's your turn now in game ".DB_format_gameid($gameid).".\n".
651 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$whohash."\n\n" ;
652 mymail($email,$EmailName."Poverty (game ".DB_format_gameid($gameid).") ",$message);
660 /* user only gets here in a poverty game, several things have to be handled here:
661 * A) ask, if user wants to take trump
663 * poverty: set re/contra
664 * dpoverty: first time: set re, send email to second player
665 * second time: set contra
666 * poverty: set status of other players to 'play'
667 * set status to play in case 0 trump
668 * no -> set status to play,
669 * ask next player or cancle the game if no more players
670 * B) user took trump and has too many cards (e.g. count(cards)>12 and re/contra set)
671 * ask to give cards back, set status to play, once player has 12 cards
673 * it is easier to check B) first
676 /* output pre game in case user reloads */
679 set_gametype($gametype); /* this sets the $CARDS variable */
680 $myparty = DB_get_party_by_hash($me);
682 /* the following is part B) of whats needs to be done)
683 /* check if user wants to give cards back */
684 if(myisset('exchange'))
686 $exchange = $_REQUEST['exchange'];
687 $partnerhash = DB_get_partner_hash_by_hash($me);
688 $partnerid = DB_get_userid('hash',$partnerhash);
689 $partnerhand = DB_get_handid('gameid-userid',$gameid,$partnerid);
691 /* if exchange is set to a value>0, exchange that card back to the partner */
694 $result = DB_query("UPDATE Hand_Card SET hand_id='$partnerhand'".
695 " WHERE hand_id='$myhand' AND card_id=".DB_quote_smart($exchange));
696 DB_add_exchanged_card(DB_quote_smart($exchange),$myhand,$partnerhand);
701 $mycards = DB_get_hand($me);
702 $mycards = mysort($mycards,$gametype);
704 /* check if user need to give more cards back */
705 if( ($myparty=='re' || $myparty=='contra') && count($mycards)>12)
707 echo "<div class=\"poverty\"> you need to get rid of a few cards</div>\n";
710 echo "<div class=\"mycards\">Your cards are: <br />\n";
711 foreach($mycards as $card)
712 display_link_card($card,$PREF['cardset'],$type);
713 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select card to give back\" />\n";
716 else if( ($myparty=='re' || $myparty=='contra') && count($mycards)==12)
718 /* user is done, ready to play */
719 DB_set_hand_status_by_hash($me,'play');
721 /* email start player */
722 $startplayer = DB_get_startplayer_by_gameid($gameid);
723 $email = DB_get_email('position-gameid',$startplayer,$gameid);
724 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
725 $who = DB_get_userid('email',$email);
726 DB_set_player_by_gameid($gameid,$who);
730 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
732 /* email startplayer */
733 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
734 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
735 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
739 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
742 /* the following is part A) of what needs to be done */
743 if(!myisset('trump'))
747 echo "<div class=\"poverty\">\n";
748 $userids = DB_get_all_userid_by_gameid($gameid);
749 foreach($userids as $user)
751 $name = DB_get_name('userid',$user);
752 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
753 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
754 $userparty = DB_get_party_by_hash($userhash);
756 if($usersick=='poverty' && !$userparty)
758 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
759 $cards = DB_get_hand($hash);
762 foreach($cards as $card)
763 if($card<27) $nrtrump++;
764 echo "Player $name has $nrtrump trump. Do you want to take them?".
765 "<a href=\"index.php?action=game&me=$me&trump=$user\">Yes</a> <br />\n";
768 echo "<a href=\"index.php?action=game&me=$me&trump=no\">No way</a> <br />\n";
769 echo "</div><div>\n";
771 echo "<div class=\"mycards\">Your cards are: <br />\n";
772 foreach($mycards as $card)
773 display_card($card,$PREF['cardset']);
774 echo "</div></div>\n";
780 $trump = $_REQUEST['trump'];
784 /* user doesn't want to take trump */
785 DB_set_hand_status_by_hash($me,'play');
787 /* set next player who needs to be asked and email him*/
788 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
789 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
791 /* don't ask people who have poverty */
793 if($firstsick=='poverty')
795 if($secondsick=='poverty')
800 if($gametype=='dpoverty')
802 $next=999; /* need to cancel for sure, since both would need to take the trump */
805 /* no more people to ask, need to cancel the game */
808 $message = "Hello, \n\n".
809 "Game ".DB_format_gameid($gameid)." has been canceled since nobody wanted to take the trump.\n";
811 $userids = DB_get_all_userid_by_gameid($gameid);
812 foreach($userids as $user)
814 $To = DB_get_email('userid',$user);
815 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled (poverty not resolved)",$message);
818 /* delete everything from the dB */
821 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been canceled.<br /><br /></p>";
826 /* email next player, set his status to poverty */
827 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
828 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
829 $userid = DB_get_userid('email',$To);
831 DB_set_player_by_gameid($gameid,$userid);
832 DB_set_hand_status_by_hash($userhash,'poverty');
834 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
835 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
836 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
841 /* player wants to take trump, change cards */
843 /* user wants to take trump */
844 $trump = $_REQUEST['trump'];
845 $userhand = DB_get_handid('gameid-userid',$gameid,$trump);
846 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
848 /* remember which cards were handed over*/
849 $partnerhand = DB_get_all_hand($userhash);
850 foreach ($partnerhand as $card)
852 DB_add_exchanged_card($card,$userhand,$myhand);
854 /* copy trump from player A to B */
855 $result = DB_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
858 $mycards = DB_get_hand($me);
861 if($gametype=='poverty')
863 $userids = DB_get_all_userid_by_gameid($gameid);
864 foreach($userids as $user)
866 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
867 if($hash==$userhash||$hash==$me)
869 DB_set_party_by_hash($hash,'re');
873 DB_set_party_by_hash($hash,'contra');
874 DB_set_hand_status_by_hash($hash,'play'); /* the contra party is ready to play */
877 /* check if we are done (in case of no trump handed over), if so, go to 'play' phase right away*/
878 if(count($mycards)==12)
880 DB_set_hand_status_by_hash($me,'play');
885 /* has the re party already been set?*/
887 $userids = DB_get_all_userid_by_gameid($gameid);
888 foreach($userids as $user)
890 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
891 $party = DB_get_party_by_hash($hash);
897 DB_set_party_by_hash($me,'contra');
898 DB_set_party_by_hash($userhash,'contra');
902 DB_set_party_by_hash($me,'re');
903 DB_set_party_by_hash($userhash,'re');
905 /* send out email to second non-poverty player */
906 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
907 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
910 if($firstsick=='poverty')
911 if($secondsick=='poverty')
917 echo "<div class=\"message\">Error in poverty, please contact the Admin</div>\n";
919 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
920 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
921 $userid = DB_get_userid('email',$To);
923 DB_set_player_by_gameid($gameid,$userid);
924 DB_set_hand_status_by_hash($userhash,'poverty');
926 $message = "Two people have poverty, it's your turn to decide, if you want to take the trump. Please visit:".
927 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
928 mymail($To,$EmailName." double poverty (game ".DB_format_gameid($gameid).")",$message);
933 echo "<div class=\"message\"> Please, <a href=\"$INDEX?action=game&me=$me\">continue</a> here.</div>\n";
941 /* both entries here, so that the tricks are visible for both.
942 * in case of 'play' there is a break later that skips the last part
945 /* check if all players are ready to play,
946 * if so, send out email to the startplayer
947 * only need to do this if the game hasn't started yet
949 $gamestatus = DB_get_game_status_by_gameid($gameid);
950 if($gamestatus == 'pre')
953 $userids = DB_get_all_userid_by_gameid($gameid);
954 foreach($userids as $user)
956 $userstatus = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
957 if($userstatus !='play' && $userstatus!='gameover')
960 DB_set_player_by_gameid($gameid,$user);
966 /* only set this after all poverty, etc. are handled*/
967 DB_set_game_status_by_gameid($gameid,'play');
969 /* email startplayer */
970 $startplayer = DB_get_startplayer_by_gameid($gameid);
971 $email = DB_get_email('position-gameid',$startplayer,$gameid);
972 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
973 $who = DB_get_userid('email',$email);
974 DB_set_player_by_gameid($gameid,$who);
976 if($hash!=$me && DB_get_email_pref_by_hash($hash)!='emailaddict')
978 /* email startplayer) */
979 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
980 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
981 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
985 /* figure out what kind of game we are playing,
986 * set the global variables $CARDS['trump'],$CARDS['diamonds'],$CARDS['hearts'],
987 * $CARDS['clubs'],$CARDS['spades'],$CARDS['foxes']
991 $gametype = DB_get_gametype_by_gameid($gameid);
993 if($gametype=='solo')
995 $gametype = DB_get_solo_by_gameid($gameid);
996 if($gametype=='silent')
999 $GT = $gametype.' '.$GT;
1002 $gametype = 'normal';
1004 set_gametype($gametype); /* this sets the $CARDS variable */
1006 /* get some infos about the game, need to reset this, since it might have changed */
1007 $gamestatus = DB_get_game_status_by_gameid($gameid);
1009 /* has the game started? No, then just wait here...*/
1010 if($gamestatus == 'pre')
1012 echo "<p class=\"message\"> You finished the setup, but not everyone else finished it... ".
1013 "You need to wait for the others. Just wait for an email. </p>";
1015 $mycards = DB_get_hand($me);
1016 $mycards = mysort($mycards,$gametype);
1018 echo "<div class=\"mycards\">Your cards are: <br />\n";
1019 foreach($mycards as $card)
1020 display_card($card,$PREF['cardset']);
1023 break; /* not sure this works... the idea is that you can
1024 * only play a card after everyone is ready to play */
1027 /* get time from the last action of the game */
1028 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1029 $gameend = time() - strtotime($r[0]);
1031 /* handle comments in case player didn't play a card, allow comments a week after the end of the game */
1032 if( (!myisset('card') && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
1033 if(myisset('comment'))
1035 $comment = $_REQUEST['comment'];
1036 $playid = DB_get_current_playid($gameid);
1039 DB_insert_comment($comment,$playid,$myid);
1042 /* get everything relevant to display the tricks */
1043 $result = DB_query("SELECT Hand_Card.card_id as card,".
1044 " Hand.position as position,".
1045 " Play.sequence as sequence, ".
1047 " GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
1048 " SEPARATOR '\n' ), ".
1049 " Play.create_date, ".
1052 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1053 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1054 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1055 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1056 "LEFT JOIN User On User.id=Comment.user_id ".
1057 "WHERE Trick.game_id='".$gameid."' ".
1058 "GROUP BY Trick.id, sequence ".
1059 "ORDER BY Trick.id, sequence ASC");
1061 $lasttrick = DB_get_max_trickid($gameid);
1063 $play = array(); /* needed to calculate winner later */
1065 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1066 $firstcard = ''; /* first card in a trick */
1068 echo "\n<ul class=\"tricks\">\n";
1069 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
1071 /* output vorbehalte */
1072 $mygametype = DB_get_gametype_by_gameid($gameid);
1073 if($mygametype != 'normal' && $mygametype != 'silent') /* only show when needed */
1075 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
1076 " <div class=\"trick\" id=\"trick0\">\n";
1077 /* get information so show the cards that have been handed over in a poverty game */
1082 if($mygametype == 'poverty' || $mygametype=='dpoverty')
1084 /* who has poverty */
1085 for($mypos=1;$mypos<5;$mypos++)
1087 $usersick = DB_get_sickness_by_pos_and_gameid($mypos,$gameid);
1088 if($usersick == 'poverty')
1090 $povertypos2 = $mypos;
1092 $povertypos1 = $mypos;
1094 /* get hash and cards for all */
1095 $povertyhash1 = DB_get_hash_from_game_and_pos($gameid,$povertypos1);
1096 $partnerhash1 = DB_get_partner_hash_by_hash($povertyhash1);
1098 $povertycards1 = DB_get_exchanged_cards($povertyhash1);
1099 $partnercards1 = DB_get_exchanged_cards($partnerhash1);
1101 $partnerpos1 = DB_get_pos_by_hash($partnerhash1);
1104 $povertyhash2 = DB_get_hash_from_game_and_pos($gameid,$povertypos2);
1105 $partnerhash2 = DB_get_partner_hash_by_hash($povertyhash2);
1107 $povertycards2 = DB_get_exchanged_cards($povertyhash2);
1108 $partnercards2 = DB_get_exchanged_cards($partnerhash2);
1110 $partnerpos2 = DB_get_pos_by_hash($partnerhash2);
1115 for($mypos=1;$mypos<5;$mypos++)
1117 $usersick = DB_get_sickness_by_pos_and_gameid($mypos,$gameid);
1118 if($usersick!=NULL ||
1119 $mypos==$povertypos1 || $mypos==$partnerpos1 ||
1120 $mypos==$povertypos2 || $mypos==$partnerpos2 )
1122 echo " <div class=\"vorbehalt".($mypos-1)."\"> Vorbehalt <br />";
1124 echo " $usersick <br />";
1125 if($mypos==$partnerpos1)
1127 foreach($partnercards1 as $card)
1128 if($povertyhash1 == $me || $partnerhash1 == $me || $mystatus=='gameover')
1129 display_card($card,$PREF['cardset']);
1131 display_card(0,$PREF['cardset']);
1133 else if($mypos==$povertypos1)
1135 foreach($povertycards1 as $card)
1136 if($povertyhash1 == $me || $partnerhash1 == $me || $mystatus=='gameover')
1137 display_card($card,$PREF['cardset']);
1139 display_card(0,$PREF['cardset']);
1141 else if($mypos==$povertypos2)
1143 foreach($povertycards2 as $card)
1144 if($povertyhash2 == $me || $partnerhash2 == $me || $mystatus=='gameover')
1145 display_card($card,$PREF['cardset']);
1147 display_card(0,$PREF['cardset']);
1149 else if($mypos==$partnerpos2)
1151 foreach($partnercards2 as $card)
1152 if($povertyhash2 == $me || $partnerhash2 == $me || $mystatus=='gameover')
1153 display_card($card,$PREF['cardset']);
1155 display_card(0,$PREF['cardset']);
1160 if($mygametype == $usersick)
1164 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1168 while($r = DB_fetch_array($result))
1176 /* count number of tricks */
1180 /* check if first schweinchen has been played */
1181 if( $GAME['schweinchen-who'] && ($r[0] == 19 || $r[0] == 20) )
1182 if(!$GAME['schweinchen-first'])
1183 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1185 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1187 /* save card to be able to find the winner of the trick later */
1188 $play[$seq] = array('card'=>$r[0],'pos'=>$pos);
1192 /* first card in a trick, output some html */
1193 if($trick!=$lasttrick)
1195 /* start of an old trick? */
1196 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1197 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1198 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1200 else if($trick==$lasttrick)
1202 /* start of a last trick? */
1203 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1204 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1205 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1208 /* remember first card, so that we are able to check, what cards can be played */
1213 echo " <div class=\"card".($pos-1)."\">\n";
1215 /* display comments */
1217 echo " <span class=\"comment\">".$comment."</span>\n";
1220 display_card($r[0],$PREF['cardset']);
1222 echo " </div>\n"; /* end div card */
1227 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1231 /* whos turn is it? */
1234 $winner = get_winner($play,$gametype); /* returns the position */
1236 $firstcard = ''; /* new trick, no first card */
1241 if($next==5) $next = 1;
1244 /* my turn?, display cards as links, ask for comments*/
1245 if(DB_get_pos_by_hash($me) == $next)
1250 /* do we want to play a card? */
1251 if(myisset('card') && $myturn)
1253 $card = $_REQUEST['card'];
1254 $handid = DB_get_handid('hash',$me);
1255 $commentSchweinchen =''; /* used to add a comment when Schweinchen is being played */
1257 /* check if we have card and that we haven't played it yet*/
1258 /* set played in hand_card to true where hand_id and card_id*/
1259 $r = DB_query_array("SELECT id FROM Hand_Card WHERE played='false' and ".
1260 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1261 $handcardid = $r[0];
1263 if($handcardid) /* everything ok, play card */
1265 /* update Game timestamp */
1266 DB_update_game_timestamp($gameid);
1268 /* mark card as played */
1269 DB_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1270 DB_quote_smart($card));
1272 /* get trick id or start new trick */
1273 $a = DB_get_current_trickid($gameid);
1278 $playid = DB_play_card($trickid,$handcardid,$sequence);
1280 /* check special output for schweinchen in case in case a fox is being played
1281 * check for correct rules, etc. has already been done
1283 if( $GAME['schweinchen-who'] && ($card == 19 || $card == 20) )
1285 if(!$GAME['schweinchen-first'])
1286 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1288 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1290 if( ($GAME['schweinchen-second']==1 && $RULES['schweinchen']=='second') || $RULES['schweinchen']=='both')
1292 DB_insert_comment('Schweinchen! ',$playid,$myid);
1293 $commentSchweinchen = 'Schweinchen! ';
1296 echo 'schweinchen = '.$GAME['schweinchen-who'].' ---<br />';
1299 /* if sequence == 4 check who one in case of wedding */
1300 if($sequence == 4 && $GT == 'wedding')
1302 /* is wedding resolve */
1303 $resolved = DB_get_sickness_by_gameid($gameid);
1306 /* who has wedding */
1307 $userids = DB_get_all_userid_by_gameid($gameid);
1308 foreach($userids as $user)
1310 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1311 if($usersick == 'wedding')
1314 /* who won the trick */
1315 $play = DB_get_cards_by_trick($trickid);
1316 $winner = get_winner($play,$gametype); /* returns the position */
1317 $winnerid = DB_get_userid('gameid-position',$gameid,$winner);
1318 /* is tricknr <=3 */
1319 if($tricknr <=3 && $winnerid!=$whosick)
1321 /* set resolved at tricknr*/
1322 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1324 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1325 DB_set_party_by_hash($whash,'re');
1327 if($tricknr == 3 && $winnerid==$whosick)
1329 /* set resolved at tricknr*/
1330 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1335 /* if sequence == 4, set winner of the trick, count points and set the next player */
1338 $play = DB_get_cards_by_trick($trickid);
1339 $winner = get_winner($play,$gametype); /* returns the position */
1341 /* check if someone caught a fox */
1342 /* first check if we should account for solos at all,
1343 * since it doesn't make sense in some games
1345 $ok = 0; /* fox shouldn't be counted */
1346 if(DB_get_gametype_by_gameid($gameid)=='solo')
1348 $solo = DB_get_solo_by_gameid($gameid);
1349 if($solo == 'trump' || $solo == 'silent')
1350 $ok = 1; /* for trump solos and silent solos, foxes are ok */
1353 $ok = 1; /* for all other games (not solos) foxes are ok too */
1356 foreach($play as $played)
1358 if ( $played['card']==19 || $played['card']==20 )
1359 if ($played['pos']!= $winner )
1361 /* possible caught a fox, check party */
1362 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1363 $uid2 = DB_get_userid('gameid-position',$gameid,$played['pos']);
1365 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1366 $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1368 if($party1 != $party2)
1369 DB_query("INSERT INTO Score".
1370 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1374 /* check for karlchen (jack of clubs in the last trick)*/
1375 /* same as for foxes, karlchen doesn't always make sense
1376 * check what kind of game it is and set karlchen accordingly */
1377 $ok = 1; /* default: karlchen should be accounted for */
1379 $ok = 0; /* Karlchen works only in the last trick */
1380 if($ok && DB_get_gametype_by_gameid($gameid)=='solo' )
1382 $solo = DB_get_solo_by_gameid($gameid);
1383 if($solo == 'trumpless' || $solo == 'jack' || $solo == 'queen' )
1384 $ok = 0; /* no Karlchen in these solos */
1388 foreach($play as $played)
1389 if ( $played['card']==11 || $played['card']==12 )
1390 if ($played['pos'] == $winner )
1392 /* possible caught a fox, check party */
1393 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1394 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1396 DB_query("INSERT INTO Score".
1397 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1399 /* check for doppelopf (>40 points)*/
1401 foreach($play as $played)
1403 $points += DB_get_card_value_by_cardid($played['card']);
1407 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1408 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1410 DB_query("INSERT INTO Score".
1411 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
1415 DB_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1417 echo "ERROR during scoring";
1420 echo "DEBUG: position $winner won the trick <br />";
1422 /* who is the next player? */
1427 $next = DB_get_pos_by_hash($me)+1;
1429 if($next==5) $next=1;
1431 /* check for coment */
1432 if(myisset('comment'))
1434 $comment = $_REQUEST['comment'];
1436 DB_insert_comment($comment,$playid,$myid);
1437 if($commentSchweinchen)
1438 $comment = $commentSchweinchen . $comment;
1441 /* display played card */
1442 $pos = DB_get_pos_by_hash($me);
1445 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1446 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1447 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1450 echo " <div class=\"card".($pos-1)."\">\n ";
1452 /* display comments */
1453 display_card($card,$PREF['cardset']);
1455 echo "\n <span class=\"comment\"> ".$comment."</span>\n";
1458 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1460 /*check if we still have cards left, else set status to gameover */
1461 if(sizeof(DB_get_hand($me))==0)
1463 DB_set_hand_status_by_hash($me,'gameover');
1464 $mystatus = 'gameover';
1467 /* if all players are done, set game status to game over,
1468 * get the points of the last trick and send out an email
1471 $userids = DB_get_all_userid_by_gameid($gameid);
1474 foreach($userids as $user)
1475 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1479 DB_set_game_status_by_gameid($gameid,'gameover');
1481 /* email next player, if game is still running */
1482 if(DB_get_game_status_by_gameid($gameid)=='play')
1484 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1485 $email = DB_get_email('hash',$next_hash);
1486 $who = DB_get_userid('email',$email);
1487 DB_set_player_by_gameid($gameid,$who);
1489 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1490 "It's your turn now.\n".
1491 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$next_hash."\n\n" ;
1492 if( DB_get_email_pref_by_uid($who)!='emailaddict' )
1493 mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
1495 else /* send out final email */
1497 /* individual score */
1498 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1499 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1500 " LEFT JOIN User ON User.id=Hand.user_id".
1501 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1502 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1503 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1504 " WHERE Hand.game_id='$gameid'".
1505 " GROUP BY User.fullname" );
1506 $message = "The game is over. Thanks for playing :)\n";
1507 $message .= "Final score:\n";
1508 while( $r = DB_fetch_array($result) )
1509 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1511 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1512 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1513 " LEFT JOIN User ON User.id=Hand.user_id".
1514 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1515 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1516 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1517 " WHERE Hand.game_id='$gameid'".
1518 " GROUP BY Hand.party" );
1519 $message .= "\nTotals:\n";
1522 while( $r = DB_fetch_array($result) )
1524 $message .= " ".$r[0]." ".$r[1]."\n";
1527 else if($r[0] == 'contra')
1532 * save score in database
1536 /* get calls from re/contra */
1538 $call_contra = NULL;
1539 foreach($userids as $user)
1541 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1542 $call = DB_get_call_by_hash($hash);
1543 $party = DB_get_party_by_hash($hash);
1547 $call = (int) $call;
1553 else if( $call < $call_re)
1556 else if($party=='contra')
1558 if($call_contra==NULL)
1559 $call_contra = $call;
1560 else if( $call < $call_contra)
1561 $call_contra = $call;
1566 /* figure out who one */
1567 $winning_party = NULL;
1569 if($call_re == NULL && $call_contra==NULL)
1571 /* nobody made a call, so it's easy to figure out who won */
1573 $winning_party='re';
1575 $winning_party='contra';
1579 /* if one party makes a call, they only win, iff they make enough points
1580 * if only one party made a call, the other one wins,
1581 * if the first one didn't make it
1585 $offset = 120 - $call_re;
1587 $offset--; /* since we use a > in the next equation */
1589 if($re > 120+$offset)
1590 $winning_party='re';
1591 else if ($call_contra == NULL )
1592 $winning_party='contra';
1597 $offset = 120 - $call_contra;
1598 if($call_contra == 0)
1599 $offset--; /* since we use a > in the next equation */
1601 if($contra > 120+$offset)
1602 $winning_party='contra';
1603 else if ($call_re == NULL )
1604 $winning_party='re';
1608 /* one point for each call of the other party in case the other party didn't win
1609 * and one point each in case the party made more than points than one of the calls
1611 if($winning_party!='contra' && $call_contra!=NULL)
1613 for( $p=$call_contra;$p<=120; $p+=30 )
1615 DB_query("INSERT INTO Score".
1616 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
1619 for( $p=$call_contra; $p<120; $p+=30)
1622 DB_query("INSERT INTO Score".
1623 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'made$p')");
1626 if($winning_party!='re' and $call_re!=NULL)
1628 for( $p=$call_re;$p<=120; $p+=30 )
1630 DB_query("INSERT INTO Score".
1631 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'against$p')");
1634 for( $p=$call_re; $p<120; $p+=30)
1637 DB_query("INSERT INTO Score".
1638 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
1642 /* point in case contra won */
1643 if($winning_party=='contra')
1645 DB_query("INSERT INTO Score".
1646 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1649 /* one point each for winning and each 30 points + calls */
1650 if($winning_party=='re')
1652 foreach(array(120,150,180,210,240) as $p)
1655 if($p==240 || $call_contra!=NULL)
1659 DB_query("INSERT INTO Score".
1660 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
1662 /* re called something and won */
1663 foreach(array(0,30,60,90,120) as $p)
1665 if($call_re!=NULL && $call_re<$p+1)
1666 DB_query("INSERT INTO Score".
1667 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
1670 else if( $winning_party=='contra')
1672 foreach(array(120,150,180,210,240) as $p)
1675 if($p==240 || $call_re!=NULL)
1678 if($contra>$p-$offset)
1679 DB_query("INSERT INTO Score".
1680 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
1682 /* re called something and won */
1683 foreach(array(0,30,60,90,120) as $p)
1685 if($call_contra!=NULL && $call_contra<$p+1)
1686 DB_query("INSERT INTO Score".
1687 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
1692 /* add score points to email */
1695 $message .= " Points Re: \n";
1696 $queryresult = DB_query("SELECT score FROM Score ".
1697 " WHERE game_id=$gameid AND party='re'".
1699 while($r = DB_fetch_array($queryresult) )
1701 $message .= " ".$r[0]."\n";
1704 $message .= " Points Contra: \n";
1705 $queryresult = DB_query("SELECT score FROM Score ".
1706 " WHERE game_id=$gameid AND party='contra'".
1708 while($r = DB_fetch_array($queryresult) )
1710 $message .= " ".$r[0]."\n";
1713 $message .= " Total Points (from the Re point of view): $Tpoint\n";
1716 $session = DB_get_session_by_gameid($gameid);
1717 $score = generate_score_table($session);
1719 $message .= "Score Table:\n";
1720 $message .= format_score_table_ascii($score);
1722 /* send out final email */
1725 foreach($userids as $user)
1726 $all[] = DB_get_email('userid',$user);
1727 $To = implode(",",$all);
1729 $help = "\n\n (you can use reply all on this email to reach all the players.)\n";
1730 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 1(2)",$message.$help);
1732 foreach($userids as $user)
1734 $To = DB_get_email('userid',$user);
1735 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1737 $link = "Use this link to have a look at game ".DB_format_gameid($gameid).": ".
1738 $HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
1739 if( DB_get_email_pref_by_uid($user) != 'emailaddict' )
1740 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 2(2)",$link);
1746 echo "can't find that card?! <br />\n";
1749 else if(myisset('card') && !$myturn )
1751 echo "please wait until it's your turn! <br />\n";
1754 if($seq!=4 && $trickNR>=1 && !(myisset('card') && $myturn) )
1755 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1757 /* display points in case game is over */
1758 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1760 echo " <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1761 " <div class=\"trick\" id=\"trick13\">\n";
1762 /* add pic for re/contra
1763 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1765 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1766 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1767 " LEFT JOIN User ON User.id=Hand.user_id".
1768 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1769 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1770 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1771 " WHERE Hand.game_id='$gameid'".
1772 " GROUP BY User.fullname" );
1773 while( $r = DB_fetch_array($result))
1774 echo " <div class=\"card".($r[3]-1)."\">\n".
1775 " <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
1778 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1781 echo " <li onclick=\"hl_prev();\" class=\"old\"><a href=\"#\">prev</a></li>\n";
1782 echo " <li onclick=\"hl_next();\" class=\"old\"><a href=\"#\">next</a></li>\n";
1783 echo "</ul>\n"; /* end ul tricks*/
1785 $mycards = DB_get_hand($me);
1786 $mycards = mysort($mycards,$gametype);
1787 echo "<div class=\"mycards\">\n";
1789 if($myturn && !myisset('card') && $mystatus=='play' )
1791 echo "Hello ".$myname.", it's your turn! <br />\n";
1792 echo "Your cards are: <br />\n";
1794 /* do we have to follow suite? */
1796 if(have_suit($mycards,$firstcard))
1799 foreach($mycards as $card)
1801 if( ($followsuit && !same_type($card,$firstcard)) ||
1802 ( (int)($card)==19 && ($RULES['schweinchen']=='second'||$RULES['schweinchen']=='secondaftercall')
1803 && $GAME['schweinchen-who']==$me && !$GAME['schweinchen-first'] )
1805 display_card($card,$PREF['cardset']);
1807 display_link_card($card,$PREF['cardset']);
1810 else if($mystatus=='play' )
1812 echo "Your cards are: <br />\n";
1813 foreach($mycards as $card)
1814 display_card($card,$PREF['cardset']);
1816 else if($mystatus=='gameover')
1818 $oldcards = DB_get_all_hand($me);
1819 $oldcards = mysort($oldcards,$gametype);
1820 echo "Your cards were: <br />\n";
1821 foreach($oldcards as $card)
1822 display_card($card,$PREF['cardset']);
1824 $userids = DB_get_all_userid_by_gameid($gameid);
1825 foreach($userids as $user)
1827 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1833 $name = DB_get_name('userid',$user);
1834 $oldcards = DB_get_all_hand($userhash);
1835 $oldcards = mysort($oldcards,$gametype);
1836 echo "$name's cards were: <br />\n";
1837 foreach($oldcards as $card)
1838 display_card($card,$PREF['cardset']);
1844 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1845 if($mystatus=='play')
1848 /* the following happens only when the gamestatus is 'gameover' */
1849 /* check if game is over, display results */
1850 if(DB_get_game_status_by_gameid($gameid)=='play')
1852 echo "The game is over for you.. other people still need to play though";
1856 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1857 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1858 " LEFT JOIN User ON User.id=Hand.user_id".
1859 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1860 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1861 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1862 " WHERE Hand.game_id='$gameid'".
1863 " GROUP BY Hand.party" );
1864 echo "<div class=\"total\"> Totals:<br />\n";
1865 while( $r = DB_fetch_array($result))
1866 echo " ".$r[0]." ".$r[1]."<br />\n";
1868 $queryresult = DB_query("SELECT timediff(mod_date,create_date) ".
1869 " FROM Game WHERE id='$gameid'");
1870 $r = DB_fetch_array($queryresult);
1871 echo "<p>This game took ".$r[0]." hours.</p>";
1873 echo "<div class=\"re\">\n Points Re: <br />\n";
1874 $queryresult = DB_query("SELECT score FROM Score ".
1875 " WHERE game_id=$gameid AND party='re'".
1877 while($r = DB_fetch_array($queryresult) )
1878 echo " ".$r[0]."<br />\n";
1881 echo "<div class=\"contra\">\n Points Contra: <br />\n";
1882 $queryresult = DB_query("SELECT score FROM Score ".
1883 " WHERE game_id=$gameid AND party='contra'".
1885 while($r = DB_fetch_array($queryresult) )
1886 echo " ".$r[0]."<br />\n";
1895 myerror("error in testing the status");
1897 /* output left menu */
1898 display_user_menu();
1900 /* output right menu */
1902 /* display rule set for this game */
1903 echo "<div class=\"gameinfo\">\n";
1905 if($gamestatus == 'play' )
1906 output_form_calls($me);
1908 /* get time from the last action of the game */
1909 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1910 $gameend = time() - strtotime($r[0]);
1912 if($gamestatus == 'play' || $gameend < 60*60*24*7)
1914 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1917 echo "<input type=\"submit\" value=\"submit\" />\n";
1923 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1925 $session = DB_get_session_by_gameid($gameid);
1926 $result = DB_query("SELECT id,create_date FROM Game".
1927 " WHERE session=$session".
1928 " ORDER BY create_date DESC".
1932 $r = DB_fetch_array($result);
1934 if(!$session || $gameid==$r[0])
1936 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
1937 $names = DB_get_all_names_by_gameid($gameid);
1938 $type = DB_get_gametype_by_gameid($gameid);
1942 $solo = DB_get_solo_by_gameid($gameid);
1944 if($solo!='silent') /* repeat game with same first player */
1945 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
1946 else /* rotate normally */
1947 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
1949 else /* rotate normally */
1950 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);