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";
16 $me = $_REQUEST["me"];
18 /* Ok, got a hash, but is it valid? */
19 $myid = DB_get_userid('hash',$me);
22 echo "Can't find you in the database, please check the url.<br />\n";
23 echo "perhaps the game has been canceled, check by login in <a href=\"$INDEX\">here</a>.";
29 global $GAME,$RULES,$CARDS;
31 /* user might get here by clicking on the link in an email, so session might not be set */
32 if(isset($_SESSION["name"]))
33 output_status($_SESSION["name"]);
35 /* the user has done something, update the timestamp */
36 DB_update_user_timestamp($myid);
38 /* get some information from the DB */
39 $gameid = DB_get_gameid_by_hash($me);
40 $myname = DB_get_name('hash',$me);
41 $mystatus = DB_get_status_by_hash($me);
42 $mypos = DB_get_pos_by_hash($me);
43 $myhand = DB_get_handid('hash',$me);
44 $session = DB_get_session_by_gameid($gameid);
46 /* get prefs and save them in a variable*/
47 $PREF = DB_get_PREF($myid);
49 /* get rule set for this game */
50 $RULES = DB_get_RULES($gameid);
52 /* get some infos about the game */
53 $gametype = DB_get_gametype_by_gameid($gameid);
54 $gamestatus = DB_get_game_status_by_gameid($gameid);
58 $gametype = DB_get_solo_by_gameid($gameid);
59 $GT = $gametype." ".$GT;
62 /* do we need to worry about Schweinchen?
63 * check gametype and rules
64 * if yes, figure out if someone actually has Schweinchen
65 * save information in $GAME
68 if( $gamestatus == 'pre' )
70 /* always need to use Schweinchen to figure out for example who has poverty */
75 /* in a game Schweinchen is not valid in all types of games */
76 if( $gametype == 'normal' || $gametype == 'silent' || $gametype=='trump' )
77 if( in_array($RULES['schweinchen'],array('both','second','secondaftercall')) )
81 /* these are the defaults */
82 $GAME['schweinchen-who'] = NULL;
83 $GAME['schweinchen-first'] = NULL;
84 $GAME['schweinchen-second'] = NULL;
88 /* need to check for Schweinchen */
91 $hash = DB_get_hash_from_game_and_pos($gameid,$i);
92 $cards = DB_get_all_hand($hash);
93 if( in_array("19",$cards) && in_array("20",$cards) )
94 $GAME['schweinchen-who']=$hash;
96 $GAME['schweinchen-first'] = 0; /* to keep track if they have been played already */
97 $GAME['schweinchen-second'] = 0;
99 /* end check for Schweinchen */
101 /* set the $CARDS variable, needed for sorting the cards
102 * we set it to normal so that the pre-game phase is handled ok
103 * and later set it to the correct game type that is played
105 set_gametype('normal');
107 /* put everyting in a form */
108 echo "<form action=\"index.php?action=game&me=$me\" method=\"post\">\n";
112 /* output extra division in case this game is part of a session */
115 echo "<div class=\"session\">\n";
116 echo " <div class=\"sessionrules\">Rules (+icons fur rules) \n";
118 echo " 10ofhearts : ".$RULES["dullen"] ."<br />\n";
119 echo " schweinchen: ".$RULES["schweinchen"] ."<br />\n";
120 echo " call: ".$RULES["call"] ."<br />\n";
121 echo " </div>\n </div>\n";
122 echo " <div class=\"sessionscore\">Score \n";
123 $score = generate_score_table($session);
124 echo format_score_table_html($score,$myid);
126 $hashes = DB_get_hashes_by_session($session,$myid);
128 foreach($hashes as $hash)
136 echo "This is game number $j of <a href=\"".$INDEX."?action=game&me=$lasthash\">$i</a> in session $session.";
140 /* display the table and the names */
143 /* mystatus gets the player through the different stages of a game.
144 * start: does the player want to play?
145 * init: check for sickness
146 * check: check for return values from init
147 * poverty: handle poverty, wait here until all player have reached this state
148 * display sickness and move on to game
149 * play: game in progress
150 * gameover: are we revisiting a game
155 /* don't ask if user has autosetup set to yest */
157 if($PREF['autosetup']=='yes') $skip = 1;
159 if( !myisset("in") && !$skip)
161 /* asks the player, if he wants to join the game */
162 output_check_want_to_play($me);
167 /* check the result, if player wants to join, got next stage, else cancel game */
168 if(!$skip && $_REQUEST["in"] == "no" )
170 /* cancel the game */
171 $message = "Hello, \n\n".
172 "the game has been canceled due to the request of one of the players.\n";
174 $userids = DB_get_all_userid_by_gameid($gameid);
175 foreach($userids as $user)
177 $To = DB_get_email('userid',$user);
178 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
181 /* delete everything from the dB */
187 /* user wants to join the game */
189 /* move on to the next stage,
190 * no break statement to immediately go to the next stage
193 DB_set_hand_status_by_hash($me,'init');
195 /* check if everyone has reached this stage, send out email */
196 $userids = DB_get_all_userid_by_gameid($gameid);
198 foreach($userids as $user)
200 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
201 if($userstat!='init')
203 /* whos turn is it? */
204 DB_set_player_by_gameid($gameid,$user);
211 /* all done, send out email unless this player is the startplayer */
212 $startplayer = DB_get_startplayer_by_gameid($gameid);
213 if($mypos == $startplayer)
215 /* do nothing, go to next stage */
219 /* email startplayer */
221 $email = DB_get_email('position-gameid',$startplayer,$gameid);
222 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
223 $who = DB_get_userid('email',$email);
224 DB_set_player_by_gameid($gameid,$who);
226 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
227 "Use this link to go the game: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
228 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
235 /* here we ask the player if he is sick */
236 $mycards = DB_get_hand($me);
237 $mycards = mysort($mycards,$gametype);
239 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
240 echo "\n<ul class=\"tricks\">\n";
241 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
242 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
243 " <div class=\"trick\" id=\"trick0\">\n";
245 for($pos=1;$pos<5;$pos++)
247 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
248 $userid = DB_get_userid('gameid-position',$gameid,$pos);
249 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
251 if($userstatus=='start' || $userstatus=='init')
252 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs to decide </div>\n"; /* show this to everyone */
254 if($usersick!=NULL && $pos<=$mypos ) /* only show this for people sitting before the player */
255 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
256 else if($usersick==NULL && $pos<=$mypos)
257 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
259 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
260 /* end displaying sickness */
262 if(!myisset("solo","wedding","poverty","nines") )
264 output_check_for_sickness($me,$mycards);
266 echo "<div class=\"mycards\">Your cards are: <br />\n";
267 foreach($mycards as $card)
268 display_card($card,$PREF["cardset"]);
275 /* check if someone selected more than one sickness */
277 if($_REQUEST["solo"]!="No") $Nsickness++;
278 if($_REQUEST["wedding"] == "yes") $Nsickness++;
279 if($_REQUEST["poverty"] == "yes") $Nsickness++;
280 if($_REQUEST["nines"] == "yes") $Nsickness++;
284 echo "<p class=\"message\"> You selected more than one sickness, please go back ".
285 "and answer the <a href=\"$INDEX?action=game&me=$me&in=yes\">question</a> again.</p>";
287 echo "<div class=\"mycards\">Your cards are: <br />\n";
288 foreach($mycards as $card)
289 display_card($card,$PREF["cardset"]);
296 /* everything is ok, save what user said and proceed */
297 echo "<p class=\"message\">Processing what you selected in the last step...";
299 /* check if this sickness needs to be handled first */
300 $gametype = DB_get_gametype_by_gameid($gameid);
301 $startplayer = DB_get_startplayer_by_gameid($gameid); /* need this to check which solo goes first */
303 if( $_REQUEST["solo"]!="No" )
305 /* user wants to play a solo */
307 /* store the info in the user's hand info */
308 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
309 DB_set_sickness_by_hash($me,"solo");
311 echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
313 if($gametype == "solo" && $startplayer<$mypos)
314 {}/* do nothing, since someone else already is playing solo */
317 /* this solo comes first
318 * store info in game table
320 DB_set_gametype_by_gameid($gameid,"solo");
321 DB_set_startplayer_by_gameid($gameid,$mypos);
322 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
325 else if($_REQUEST["wedding"] == "yes")
327 /* silent solo is set further down */
328 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
329 DB_set_sickness_by_hash($me,"wedding");
331 else if($_REQUEST["poverty"] == "yes")
333 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
334 DB_set_sickness_by_hash($me,"poverty");
336 else if($_REQUEST["nines"] == "yes")
338 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
339 " is playing solo, this game will be canceled.<br />\n";
340 DB_set_sickness_by_hash($me,"nines");
345 /* move on to the next stage*/
346 DB_set_hand_status_by_hash($me,'check');
351 /* here we check what all players said and figure out what game we are playing
352 * this can therefore only be handled once all players finished the last stage
355 /* only need to redisplay the cards when the user reloads the page and lands directly here */
356 if($mystatus=='check')
358 $mycards = DB_get_hand($me);
359 $mycards = mysort($mycards,$gametype);
361 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
362 echo "\n<ul class=\"tricks\">\n";
363 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
364 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
365 " <div class=\"trick\" id=\"trick0\">\n";
367 for($pos=1;$pos<5;$pos++)
369 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
370 $userid = DB_get_userid('gameid-position',$gameid,$pos);
371 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
373 if($userstatus=='start' || $userstatus=='init')
374 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs to decide </div>\n"; /* show this to everyone */
376 if($usersick!=NULL) /* in the init-phase we only showed players with $pos<$mypos, now we can show all */
377 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
379 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
381 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
382 /* end displaying sickness */
385 echo "<div class=\"message\">\n";
386 echo "<p> Checking if someone else selected solo, nines, wedding or poverty.</p>";
388 /* check if everyone has reached this stage */
389 $userids = DB_get_all_userid_by_gameid($gameid);
391 foreach($userids as $user)
393 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
394 if($userstat!='check')
397 DB_set_player_by_gameid($gameid,$user);
404 echo "<p>This step can only be handled after everyone finished the last step. ".
405 "Seems like this is not the case, so you need to wait a bit... ".
406 "you will get an email once that is the case, please use the link in ".
407 "that email to continue the game.</p></div>";
409 /* display cards, if player was just at the init-phase he will still see the cards from there
410 * we can put this one here, since the last player to finish the init state won't get here and
411 * will still see his card anyway from the init-phase
413 if($mystatus=='check')
416 echo "<div class=\"mycards\">Your cards are: <br />\n";
417 foreach($mycards as $card)
418 display_card($card,$PREF["cardset"]);
425 /* Ok, everyone finished the init-phase, time to figure out what game we
426 * are playing, in case there are any solos this already
427 * will have the correct information in it */
429 echo "<p> Ok, everyone is done... figuring out what kind of game we are playing.</p>";
431 $gametype = DB_get_gametype_by_gameid($gameid);
432 $startplayer = DB_get_startplayer_by_gameid($gameid);
434 /* check for sickness */
439 foreach($userids as $user)
441 $name = DB_get_name('userid',$user);
442 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
443 if($usersick == 'nines')
446 break; /* no need to check for other poverties, since only solo can win and that is already set */
448 else if($usersick == 'poverty')
450 else if($usersick == 'wedding')
452 else if($usersick == 'solo')
456 /* now check which sickness comes first and set the gametype to it */
457 if($gametype == "solo")
464 /* TODO: should we keep statistics of this? */
465 $message = "Hello, \n\n".
466 " the game has been canceled because ".DB_get_name('userid',$nines).
467 " has five or more nines and nobody is playing solo.\n\n".
468 " To redeal either start a new game or, in case the game was part of a tournament, \n".
469 " go to the last game and use the link at the bottom of the page to redeal.";
471 $userids = DB_get_all_userid_by_gameid($gameid);
472 foreach($userids as $user)
474 $To = DB_get_email('userid',$user);
475 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
478 /* delete everything from the dB */
481 echo "The game has been canceled because ".DB_get_name('userid',$nines).
482 " has five or more nines and nobody is playing solo.\n";
487 else if($poverty==1) /* one person has poverty */
489 DB_set_gametype_by_gameid($gameid,"poverty");
490 $gametype = "poverty";
491 $who = DB_get_sickness_by_gameid($gameid);
494 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
495 if($firstsick == "poverty")
496 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
498 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
501 else if($poverty==2) /* two people have poverty */
503 DB_set_gametype_by_gameid($gameid,"dpoverty");
504 $gametype = "dpoverty";
505 $who = DB_get_sickness_by_gameid($gameid);
508 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
509 if($firstsick == "poverty")
511 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
512 if($secondsick == "poverty")
513 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
515 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
518 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
523 DB_set_gametype_by_gameid($gameid,"wedding");
524 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
525 $gametype = "wedding";
527 /* now the gametype is set correctly in the database */
528 echo "<p> Got it :)</p>";
530 /* loop over all players, set re/contra if possible and start the game if possible */
531 $userids = DB_get_all_userid_by_gameid($gameid);
532 foreach($userids as $userid)
534 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
539 /* are we the solo player? set us to re, else set us to contra */
540 $pos = DB_get_pos_by_hash($userhash);
541 if($pos == $startplayer)
542 DB_set_party_by_hash($userhash,"re");
544 DB_set_party_by_hash($userhash,"contra");
545 DB_set_hand_status_by_hash($userhash,'play');
549 /* set person with the wedding to re, do the rest during the game */
550 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
551 if($usersick == "wedding")
552 DB_set_party_by_hash($userhash,"re");
554 DB_set_party_by_hash($userhash,"contra");
556 DB_set_hand_status_by_hash($userhash,'play');
560 $hand = DB_get_all_hand($userhash);
562 if(in_array('3',$hand)||in_array('4',$hand))
563 DB_set_party_by_hash($userhash,"re");
565 DB_set_party_by_hash($userhash,"contra");
566 DB_set_hand_status_by_hash($userhash,'play');
570 /* set person with poverty to play status */
571 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
572 if($usersick == "poverty")
573 DB_set_hand_status_by_hash($userhash,'play');
575 /* set status of first player to be asked to poverty */
576 $who = DB_get_sickness_by_gameid($gameid);
577 if($who > 6) $who= $who/10; /* in case we have dpoverty */
578 $whoid = DB_get_userid('gameid-position',$gameid,$who);
580 DB_set_hand_status_by_hash($userhash,'poverty');
583 /* check for silent solo, set game type to solo in this case */
584 $gametype = DB_get_gametype_by_gameid($gameid);
585 $userids = DB_get_all_userid_by_gameid($gameid);
586 foreach($userids as $userid)
588 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
590 if($gametype=='normal')
592 $userhand = DB_get_all_hand($userhash);
593 if(check_wedding($userhand))
595 /* normal game type and player has both queens -> silent solo */
596 /* keep startplayer, just set gametype to silent solo */
597 DB_set_gametype_by_gameid($gameid,"solo");
598 DB_set_solo_by_gameid($gameid,'silent');
603 /* send out email to first player or poverty person*/
604 if($gametype!="poverty" && $gametype!="dpoverty")
606 $startplayer = DB_get_startplayer_by_gameid($gameid);
607 $email = DB_get_email('position-gameid',$startplayer,$gameid);
608 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
609 $who = DB_get_userid('email',$email);
610 DB_set_player_by_gameid($gameid,$who);
614 if(DB_get_email_pref_by_hash($hash)!="emailaddict")
616 /* email startplayer */
617 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
618 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
619 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
623 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
627 /* set status of first player to be asked to poverty */
628 $who = DB_get_sickness_by_gameid($gameid);
629 if($who > 6) $who= $who/10; /* in case we have dpoverty */
631 $whoid = DB_get_userid('gameid-position',$gameid,$who);
633 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
636 $email = DB_get_email('position-gameid',$who,$gameid);
637 $whohash = DB_get_hash_from_game_and_pos($gameid,$who);
638 DB_set_player_by_gameid($gameid,$whoid);
640 if(DB_get_email_pref_by_hash($hash)!="emailaddict")
642 /* email player for poverty */
643 $message = "Poverty: It's your turn now in game ".DB_format_gameid($gameid).".\n".
644 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$whohash."\n\n" ;
645 mymail($email,$EmailName."Poverty (game ".DB_format_gameid($gameid).") ",$message);
653 /* user only gets here in a poverty game, several things have to be handled here:
654 * A) ask, if user wants to take trump
656 * poverty: set re/contra
657 * dpoverty: first time: set re, send email to second player
658 * second time: set contra
659 * poverty: set status of other players to 'play'
660 * set status to play in case 0 trump
661 * no -> set status to play,
662 * ask next player or cancle the game if no more players
663 * B) user took trump and has too many cards (e.g. count(cards)>12 and re/contra set)
664 * ask to give cards back, set status to play, once player has 12 cards
666 * it is easier to check B) first
669 /* output pre game in case user reloads */
672 set_gametype($gametype); /* this sets the $CARDS variable */
673 $myparty = DB_get_party_by_hash($me);
675 /* the following is part B) of whats needs to be done)
676 /* check if user wants to give cards back */
677 if(myisset("exchange"))
679 $exchange = $_REQUEST['exchange'];
680 $partnerhash = DB_get_partner_hash_by_hash($me);
681 $partnerid = DB_get_userid('hash',$partnerhash);
682 $partnerhand = DB_get_handid('gameid-userid',$gameid,$partnerid);
684 /* if exchange is set to a value>0, exchange that card back to the partner */
687 $result = DB_query("UPDATE Hand_Card SET hand_id='$partnerhand'".
688 " WHERE hand_id='$myhand' AND card_id=".DB_quote_smart($exchange));
693 $mycards = DB_get_hand($me);
694 $mycards = mysort($mycards,$gametype);
696 /* check if user need to give more cards back */
697 if( ($myparty=='re' || $myparty=='contra') && count($mycards)>12)
699 echo "<div class=\"poverty\"> you need to get rid of a few cards</div>\n";
702 echo "<div class=\"mycards\">Your cards are: <br />\n";
703 foreach($mycards as $card)
704 display_link_card($card,$PREF["cardset"],$type);
705 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select card to give back\" />\n";
708 else if( ($myparty=='re' || $myparty=='contra') && count($mycards)==12)
710 /* user is done, ready to play */
711 DB_set_hand_status_by_hash($me,'play');
713 /* email start player */
714 $startplayer = DB_get_startplayer_by_gameid($gameid);
715 $email = DB_get_email('position-gameid',$startplayer,$gameid);
716 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
717 $who = DB_get_userid('email',$email);
718 DB_set_player_by_gameid($gameid,$who);
722 if(DB_get_email_pref_by_hash($hash)!="emailaddict")
724 /* email startplayer */
725 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
726 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
727 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
731 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
734 /* the following is part A) of what needs to be done */
735 if(!myisset("trump"))
739 echo "<div class=\"poverty\">\n";
740 $userids = DB_get_all_userid_by_gameid($gameid);
741 foreach($userids as $user)
743 $name = DB_get_name('userid',$user);
744 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
745 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
746 $userparty = DB_get_party_by_hash($userhash);
748 if($usersick=="poverty" && !$userparty)
750 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
751 $cards = DB_get_hand($hash);
754 foreach($cards as $card)
755 if($card<27) $nrtrump++;
756 echo "Player $name has $nrtrump trump. Do you want to take them?".
757 "<a href=\"index.php?action=game&me=$me&trump=$user\">Yes</a> <br />\n";
760 echo "<a href=\"index.php?action=game&me=$me&trump=no\">No way</a> <br />\n";
761 echo "</div><div>\n";
763 echo "<div class=\"mycards\">Your cards are: <br />\n";
764 foreach($mycards as $card)
765 display_card($card,$PREF["cardset"]);
766 echo "</div></div>\n";
772 $trump = $_REQUEST['trump'];
776 /* user doesn't want to take trump */
777 DB_set_hand_status_by_hash($me,'play');
779 /* set next player who needs to be asked and email him*/
780 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
781 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
783 /* don't ask people who have poverty */
785 if($firstsick=="poverty")
787 if($secondsick=="poverty")
792 if($gametype=="dpoverty")
794 $next=999; /* need to cancel for sure, since both would need to take the trump */
797 /* no more people to ask, need to cancel the game */
800 $message = "Hello, \n\n".
801 "Game ".DB_format_gameid($gameid)." has been canceled since nobody wanted to take the trump.\n";
803 $userids = DB_get_all_userid_by_gameid($gameid);
804 foreach($userids as $user)
806 $To = DB_get_email('userid',$user);
807 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled (poverty not resolved)",$message);
810 /* delete everything from the dB */
813 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been canceled.<br /><br /></p>";
820 /* email next player, set his status to poverty */
821 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
822 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
823 $userid = DB_get_userid('email',$To);
825 DB_set_player_by_gameid($gameid,$userid);
826 DB_set_hand_status_by_hash($userhash,'poverty');
828 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
829 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
830 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
835 /* player wants to take trump, change cards */
837 /* user wants to take trump */
838 $trump = $_REQUEST["trump"];
839 $userhand = DB_get_handid('gameid-userid',$gameid,$trump);
840 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
842 /* copy trump from player A to B */
843 $result = DB_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
846 $mycards = DB_get_hand($me);
849 if($gametype=='poverty')
851 $userids = DB_get_all_userid_by_gameid($gameid);
852 foreach($userids as $user)
854 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
855 if($hash==$userhash||$hash==$me)
857 DB_set_party_by_hash($hash,"re");
861 DB_set_party_by_hash($hash,"contra");
862 DB_set_hand_status_by_hash($hash,'play'); /* the contra party is ready to play */
865 /* check if we are done (in case of no trump handed over), if so, go to 'play' phase right away*/
866 if(count($mycards)==12)
868 DB_set_hand_status_by_hash($me,'play');
873 /* has the re party already been set?*/
875 $userids = DB_get_all_userid_by_gameid($gameid);
876 foreach($userids as $user)
878 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
879 $party = DB_get_party_by_hash($hash);
885 DB_set_party_by_hash($me,'contra');
886 DB_set_party_by_hash($userhash,'contra');
890 DB_set_party_by_hash($me,'re');
891 DB_set_party_by_hash($userhash,'re');
893 /* send out email to second non-poverty player */
894 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
895 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
898 if($firstsick=="poverty")
899 if($secondsick=="poverty")
905 echo "<div class=\"message\">Error in poverty, please contact the Admin</div>\n";
907 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
908 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
909 $userid = DB_get_userid('email',$To);
911 DB_set_player_by_gameid($gameid,$userid);
912 DB_set_hand_status_by_hash($userhash,'poverty');
914 $message = "Two people have poverty, it's your turn to decide, if you want to take the trump. Please visit:".
915 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
916 mymail($To,$EmailName." double poverty (game ".DB_format_gameid($gameid).")",$message);
921 echo "<div class=\"message\"> Please, <a href=\"$INDEX?action=game&me=$me\">continue</a> here.</div>\n";
929 /* both entries here, so that the tricks are visible for both.
930 * in case of 'play' there is a break later that skips the last part
933 /* check if all players are ready to play,
934 * if so, send out email to the startplayer
935 * only need to do this if the game hasn't started yet
937 $gamestatus = DB_get_game_status_by_gameid($gameid);
938 if($gamestatus == 'pre')
941 $userids = DB_get_all_userid_by_gameid($gameid);
942 foreach($userids as $user)
944 $userstatus = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
945 if($userstatus !='play' && $userstatus!='gameover')
948 DB_set_player_by_gameid($gameid,$user);
954 /* only set this after all poverty, etc. are handled*/
955 DB_set_game_status_by_gameid($gameid,'play');
957 /* email startplayer */
958 $startplayer = DB_get_startplayer_by_gameid($gameid);
959 $email = DB_get_email('position-gameid',$startplayer,$gameid);
960 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
961 $who = DB_get_userid('email',$email);
962 DB_set_player_by_gameid($gameid,$who);
964 if($hash!=$me && DB_get_email_pref_by_hash($hash)!="emailaddict")
966 /* email startplayer) */
967 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
968 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
969 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
973 /* figure out what kind of game we are playing,
974 * set the global variables $CARDS["trump"],$CARDS["diamonds"],$CARDS["hearts"],
975 * $CARDS["clubs"],$CARDS["spades"],$CARDS["foxes"]
979 $gametype = DB_get_gametype_by_gameid($gameid);
981 if($gametype=="solo")
983 $gametype = DB_get_solo_by_gameid($gameid);
984 if($gametype=='silent')
987 $GT = $gametype." ".$GT;
990 $gametype = "normal";
992 set_gametype($gametype); /* this sets the $CARDS variable */
994 /* get some infos about the game, need to reset this, since it might have changed */
995 $gamestatus = DB_get_game_status_by_gameid($gameid);
997 /* has the game started? No, then just wait here...*/
998 if($gamestatus == 'pre')
1000 echo "<p class=\"message\"> You finished the setup, but not everyone else finished it... ".
1001 "You need to wait for the others. Just wait for an email. </p>";
1003 $mycards = DB_get_hand($me);
1004 $mycards = mysort($mycards,$gametype);
1006 echo "<div class=\"mycards\">Your cards are: <br />\n";
1007 foreach($mycards as $card)
1008 display_card($card,$PREF["cardset"]);
1011 break; /* not sure this works... the idea is that you can
1012 * only play a card after everyone is ready to play */
1015 /* get time from the last action of the game */
1016 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1017 $gameend = time() - strtotime($r[0]);
1019 /* handle comments in case player didn't play a card, allow comments a week after the end of the game */
1020 if( (!myisset("card") && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
1021 if(myisset("comment"))
1023 $comment = $_REQUEST["comment"];
1024 $playid = DB_get_current_playid($gameid);
1027 DB_insert_comment($comment,$playid,$myid);
1030 /* handle notes in case player didn't play a card, allow notes only during a game */
1031 if( (!myisset("card") && $mystatus=='play') )
1034 $note = $_REQUEST["note"];
1037 DB_insert_note($note,$gameid,$myid);
1040 /* get everything relevant to display the tricks */
1041 $result = DB_query("SELECT Hand_Card.card_id as card,".
1042 " Hand.position as position,".
1043 " Play.sequence as sequence, ".
1045 " GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
1046 " SEPARATOR '\n' ), ".
1047 " Play.create_date, ".
1050 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1051 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1052 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1053 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1054 "LEFT JOIN User On User.id=Comment.user_id ".
1055 "WHERE Trick.game_id='".$gameid."' ".
1056 "GROUP BY Trick.id, sequence ".
1057 "ORDER BY Trick.id, sequence ASC");
1059 $lasttrick = DB_get_max_trickid($gameid);
1061 $play = array(); /* needed to calculate winner later */
1063 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1064 $firstcard = ""; /* first card in a trick */
1066 echo "\n<ul class=\"tricks\">\n";
1067 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
1069 /* output vorbehalte */
1070 $mygametype = DB_get_gametype_by_gameid($gameid);
1071 if($mygametype != 'normal' && $mygametype != 'silent') /* only show when needed */
1073 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
1074 " <div class=\"trick\" id=\"trick0\">\n";
1076 for($mypos=1;$mypos<5;$mypos++)
1078 $usersick = DB_get_sickness_by_pos_and_gameid($mypos,$gameid);
1081 echo " <div class=\"vorbehalt".($mypos-1)."\"> Vorbehalt <br />";
1083 echo " $usersick <br />";
1086 if($mygametype == $usersick)
1090 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1094 while($r = DB_fetch_array($result))
1102 /* count number of tricks */
1106 /* check if first schweinchen has been played */
1107 if( $GAME['schweinchen-who'] && ($r[0] == 19 || $r[0] == 20) )
1108 if(!$GAME['schweinchen-first'])
1109 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1111 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1113 /* save card to be able to find the winner of the trick later */
1114 $play[$seq] = array("card"=>$r[0],"pos"=>$pos);
1118 /* first card in a trick, output some html */
1119 if($trick!=$lasttrick)
1121 /* start of an old trick? */
1122 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1123 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1124 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1126 else if($trick==$lasttrick)
1128 /* start of a last trick? */
1129 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1130 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1131 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1134 /* remember first card, so that we are able to check, what cards can be played */
1139 echo " <div class=\"card".($pos-1)."\">\n";
1141 /* display comments */
1143 echo " <span class=\"comment\">".$comment."</span>\n";
1146 display_card($r[0],$PREF["cardset"]);
1148 echo " </div>\n"; /* end div card */
1153 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1157 /* whos turn is it? */
1160 $winner = get_winner($play,$gametype); /* returns the position */
1162 $firstcard = ""; /* new trick, no first card */
1167 if($next==5) $next = 1;
1170 /* my turn?, display cards as links, ask for comments*/
1171 if(DB_get_pos_by_hash($me) == $next)
1176 /* do we want to play a card? */
1177 if(myisset("card") && $myturn)
1179 $card = $_REQUEST["card"];
1180 $handid = DB_get_handid('hash',$me);
1181 $commentSchweinchen =""; /* used to add a comment when Schweinchen is being played */
1183 /* check if we have card and that we haven't played it yet*/
1184 /* set played in hand_card to true where hand_id and card_id*/
1185 $r = DB_query_array("SELECT id FROM Hand_Card WHERE played='false' and ".
1186 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1187 $handcardid = $r[0];
1189 if($handcardid) /* everything ok, play card */
1191 /* update Game timestamp */
1192 DB_update_game_timestamp($gameid);
1194 /* check if a call was made, must do this before we set the card status to played */
1195 if(myisset("call") && $_REQUEST["call"] == "120" && can_call(120,$me))
1196 $result = DB_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
1197 if(myisset("call") && $_REQUEST["call"] == "90" && can_call(90,$me))
1198 $result = DB_query("UPDATE Hand SET point_call='90' WHERE hash='$me' ");
1199 if(myisset("call") && $_REQUEST["call"] == "60" && can_call(60,$me))
1200 $result = DB_query("UPDATE Hand SET point_call='60' WHERE hash='$me' ");
1201 if(myisset("call") && $_REQUEST["call"] == "30" && can_call(30,$me))
1202 $result = DB_query("UPDATE Hand SET point_call='30' WHERE hash='$me' ");
1203 if(myisset("call") && $_REQUEST["call"] == "0" && can_call(0,$me))
1204 $result = DB_query("UPDATE Hand SET point_call='0' WHERE hash='$me' ");
1206 /* mark card as played */
1207 DB_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1208 DB_quote_smart($card));
1210 /* get trick id or start new trick */
1211 $a = DB_get_current_trickid($gameid);
1216 $playid = DB_play_card($trickid,$handcardid,$sequence);
1218 /* check special output for schweinchen in case in case a fox is being played
1219 * check for correct rules, etc. has already been done
1221 if( $GAME["schweinchen-who"] && ($card == 19 || $card == 20) )
1223 if(!$GAME['schweinchen-first'])
1224 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1226 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1228 if( ($GAME['schweinchen-second']==1 && $RULES['schweinchen']=='second') || $RULES['schweinchen']=='both')
1230 DB_insert_comment("Schweinchen! ",$playid,$myid);
1231 $commentSchweinchen = "Schweinchen! ";
1234 echo "schweinchen = ".$GAME["schweinchen-who"]." ---<br />";
1237 /* if sequence == 4 check who one in case of wedding */
1238 if($sequence == 4 && $GT == "wedding")
1240 /* is wedding resolve */
1241 $resolved = DB_get_sickness_by_gameid($gameid);
1244 /* who has wedding */
1245 $userids = DB_get_all_userid_by_gameid($gameid);
1246 foreach($userids as $user)
1248 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1249 if($usersick == "wedding")
1252 /* who won the trick */
1253 $play = DB_get_cards_by_trick($trickid);
1254 $winner = get_winner($play,$gametype); /* returns the position */
1255 $winnerid = DB_get_userid('gameid-position',$gameid,$winner);
1256 /* is tricknr <=3 */
1257 if($tricknr <=3 && $winnerid!=$whosick)
1259 /* set resolved at tricknr*/
1260 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1262 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1263 DB_set_party_by_hash($whash,"re");
1265 if($tricknr == 3 && $winnerid==$whosick)
1267 /* set resolved at tricknr*/
1268 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1273 /* if sequence == 4, set winner of the trick, count points and set the next player */
1276 $play = DB_get_cards_by_trick($trickid);
1277 $winner = get_winner($play,$gametype); /* returns the position */
1279 /* check if someone caught a fox */
1280 /* first check if we should account for solos at all,
1281 * since it doesn't make sense in some games
1283 $ok = 0; /* fox shouldn't be counted */
1284 if(DB_get_gametype_by_gameid($gameid)=="solo")
1286 $solo = DB_get_solo_by_gameid($gameid);
1287 if($solo == 'trump' || $solo == 'silent')
1288 $ok = 1; /* for trump solos and silent solos, foxes are ok */
1291 $ok = 1; /* for all other games (not solos) foxes are ok too */
1294 foreach($play as $played)
1296 if ( $played['card']==19 || $played['card']==20 )
1297 if ($played['pos']!= $winner )
1299 /* possible caught a fox, check party */
1300 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1301 $uid2 = DB_get_userid('gameid-position',$gameid,$played['pos']);
1303 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1304 $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1306 if($party1 != $party2)
1307 DB_query("INSERT INTO Score".
1308 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1312 /* check for karlchen (jack of clubs in the last trick)*/
1313 /* same as for foxes, karlchen doesn't always make sense
1314 * check what kind of game it is and set karlchen accordingly */
1315 $ok = 1; /* default: karlchen should be accounted for */
1317 $ok = 0; /* Karlchen works only in the last trick */
1318 if($ok && DB_get_gametype_by_gameid($gameid)=="solo" )
1320 $solo = DB_get_solo_by_gameid($gameid);
1321 if($solo == "trumpless" || $solo == "jack" || $solo == "queen" )
1322 $ok = 0; /* no Karlchen in these solos */
1326 foreach($play as $played)
1327 if ( $played['card']==11 || $played['card']==12 )
1328 if ($played['pos'] == $winner )
1330 /* possible caught a fox, check party */
1331 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1332 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1334 DB_query("INSERT INTO Score".
1335 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1337 /* check for doppelopf (>40 points)*/
1339 foreach($play as $played)
1341 $points += DB_get_card_value_by_cardid($played['card']);
1345 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1346 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1348 DB_query("INSERT INTO Score".
1349 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
1353 DB_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1355 echo "ERROR during scoring";
1358 echo "DEBUG: position $winner won the trick <br />";
1360 /* who is the next player? */
1365 $next = DB_get_pos_by_hash($me)+1;
1367 if($next==5) $next=1;
1369 /* check for coment */
1370 if(myisset("comment"))
1372 $comment = $_REQUEST["comment"];
1374 DB_insert_comment($comment,$playid,$myid);
1375 if($commentSchweinchen)
1376 $comment = $commentSchweinchen . $comment;
1379 /* check for note */
1382 $note = $_REQUEST["note"];
1384 DB_insert_note($note,$gameid,$myid);
1387 /* display played card */
1388 $pos = DB_get_pos_by_hash($me);
1391 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1392 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1393 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1396 echo " <div class=\"card".($pos-1)."\">\n ";
1398 /* display comments */
1399 display_card($card,$PREF["cardset"]);
1401 echo "\n <span class=\"comment\"> ".$comment."</span>\n";
1404 /*check if we still have cards left, else set status to gameover */
1405 if(sizeof(DB_get_hand($me))==0)
1407 DB_set_hand_status_by_hash($me,'gameover');
1408 $mystatus = 'gameover';
1411 /* if all players are done, set game status to game over,
1412 * get the points of the last trick and send out an email
1415 $userids = DB_get_all_userid_by_gameid($gameid);
1418 foreach($userids as $user)
1419 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1423 DB_set_game_status_by_gameid($gameid,"gameover");
1425 /* email next player, if game is still running */
1426 if(DB_get_game_status_by_gameid($gameid)=='play')
1428 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1429 $email = DB_get_email('hash',$next_hash);
1430 $who = DB_get_userid('email',$email);
1431 DB_set_player_by_gameid($gameid,$who);
1433 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1434 "It's your turn now.\n".
1435 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$next_hash."\n\n" ;
1436 if( DB_get_email_pref_by_uid($who)!="emailaddict" )
1437 mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
1439 else /* send out final email */
1441 /* individual score */
1442 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1443 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1444 " LEFT JOIN User ON User.id=Hand.user_id".
1445 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1446 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1447 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1448 " WHERE Hand.game_id='$gameid'".
1449 " GROUP BY User.fullname" );
1450 $message = "The game is over. Thanks for playing :)\n";
1451 $message .= "Final score:\n";
1452 while( $r = DB_fetch_array($result) )
1453 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1455 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1456 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1457 " LEFT JOIN User ON User.id=Hand.user_id".
1458 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1459 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1460 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1461 " WHERE Hand.game_id='$gameid'".
1462 " GROUP BY Hand.party" );
1463 $message .= "\nTotals:\n";
1466 while( $r = DB_fetch_array($result) )
1468 $message .= " ".$r[0]." ".$r[1]."\n";
1471 else if($r[0] == "contra")
1476 * save score in database
1480 /* get calls from re/contra */
1482 $call_contra = NULL;
1483 foreach($userids as $user)
1485 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1486 $call = DB_get_call_by_hash($hash);
1487 $party = DB_get_party_by_hash($hash);
1491 $call = (int) $call;
1497 else if( $call < $call_re)
1500 else if($party=="contra")
1502 if($call_contra==NULL)
1503 $call_contra = $call;
1504 else if( $call < $call_re)
1505 $call_contra = $call;
1510 /* figure out who one */
1511 $winning_party = NULL;
1513 if($call_re == NULL && $call_contra==NULL)
1515 /* nobody made a call, so it's easy to figure out who won */
1517 $winning_party="re";
1519 $winning_party="contra";
1523 /* if one party makes a call, they only win, iff they make enough points
1524 * if only one party made a call, the other one wins,
1525 * if the first one didn't make it
1529 $offset = 120 - $call_re;
1531 $offset--; /* since we use a > in the next equation */
1533 if($re > 120+$offset)
1534 $winning_party="re";
1535 else if ($call_contra == NULL )
1536 $winning_party="contra";
1541 $offset = 120 - $call_contra;
1542 if($call_contra == 0)
1543 $offset--; /* since we use a > in the next equation */
1545 if($contra > 120+$offset)
1546 $winning_party="contra";
1547 else if ($call_re == NULL )
1548 $winning_party="re";
1552 /* one point for each call of the other party in case the other party didn't win
1553 * and one point each in case the party made more than points than one of the calls
1555 if($winning_party!="contra" && $call_contra!=NULL)
1557 for( $p=$call_contra;$p<=120; $p+=30 )
1559 DB_query("INSERT INTO Score".
1560 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
1563 for( $p=$call_contra; $p<120; $p+=30)
1566 DB_query("INSERT INTO Score".
1567 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'made$p')");
1570 if($winning_party!="re" and $call_re!=NULL)
1572 for( $p=$call_re;$p<=120; $p+=30 )
1574 DB_query("INSERT INTO Score".
1575 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'against$p')");
1578 for( $p=$call_re; $p<120; $p+=30)
1581 DB_query("INSERT INTO Score".
1582 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
1586 /* point in case contra won */
1587 if($winning_party=="contra")
1589 DB_query("INSERT INTO Score".
1590 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1593 /* one point each for winning and each 30 points + calls */
1594 if($winning_party=="re")
1596 foreach(array(120,150,180,210,240) as $p)
1599 if($p==240 || $call_contra!=NULL)
1603 DB_query("INSERT INTO Score".
1604 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
1606 /* re called something and won */
1607 foreach(array(0,30,60,90,120) as $p)
1609 if($call_re!=NULL && $call_re<$p+1)
1610 DB_query("INSERT INTO Score".
1611 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
1614 else if( $winning_party=="contra")
1616 foreach(array(120,150,180,210,240) as $p)
1619 if($p==240 || $call_re!=NULL)
1622 if($contra>$p-$offset)
1623 DB_query("INSERT INTO Score".
1624 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
1626 /* re called something and won */
1627 foreach(array(0,30,60,90,120) as $p)
1629 if($call_contra!=NULL && $call_contra<$p+1)
1630 DB_query("INSERT INTO Score".
1631 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
1636 /* add score points to email */
1639 $message .= " Points Re: \n";
1640 $queryresult = DB_query("SELECT score FROM Score ".
1641 " WHERE game_id=$gameid AND party='re'".
1643 while($r = DB_fetch_array($queryresult) )
1645 $message .= " ".$r[0]."\n";
1648 $message .= " Points Contra: \n";
1649 $queryresult = DB_query("SELECT score FROM Score ".
1650 " WHERE game_id=$gameid AND party='contra'".
1652 while($r = DB_fetch_array($queryresult) )
1654 $message .= " ".$r[0]."\n";
1657 $message .= " Total Points (from the Re point of view): $Tpoint\n";
1660 $session = DB_get_session_by_gameid($gameid);
1661 $score = generate_score_table($session);
1663 $message .= "Score Table:\n";
1664 $message .= format_score_table_ascii($score);
1666 /* send out final email */
1669 foreach($userids as $user)
1670 $all[] = DB_get_email('userid',$user);
1671 $To = implode(",",$all);
1673 $help = "\n\n (you can use reply all on this email to reach all the players.)\n";
1674 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 1(2)",$message.$help);
1676 foreach($userids as $user)
1678 $To = DB_get_email('userid',$user);
1679 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1681 $link = "Use this link to have a look at game ".DB_format_gameid($gameid).": ".
1682 $HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
1683 if( DB_get_email_pref_by_uid($user) != "emailaddict" )
1684 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 2(2)",$link);
1690 echo "can't find that card?! <br />\n";
1693 else if(myisset("card") && !$myturn )
1695 echo "please wait until it's your turn! <br />\n";
1698 if($seq!=4 && $trickNR>=1)
1699 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1701 /* display points in case game is over */
1702 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1704 echo " <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1705 " <div class=\"trick\" id=\"trick13\">\n";
1706 /* add pic for re/contra
1707 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1709 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1710 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1711 " LEFT JOIN User ON User.id=Hand.user_id".
1712 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1713 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1714 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1715 " WHERE Hand.game_id='$gameid'".
1716 " GROUP BY User.fullname" );
1717 while( $r = DB_fetch_array($result))
1718 echo " <div class=\"card".($r[3]-1)."\">\n".
1719 " <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
1722 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1725 echo " <li onclick=\"hl_prev();\" class=\"old\"><a href=\"#\">prev</a></li>\n";
1726 echo " <li onclick=\"hl_next();\" class=\"old\"><a href=\"#\">next</a></li>\n";
1727 echo "</ul>\n"; /* end ul tricks*/
1729 echo "<div class=\"notes\"> Personal notes: <br />\n";
1730 $notes = DB_get_notes_by_userid_and_gameid($myid,$gameid);
1731 foreach($notes as $note)
1732 echo "$note <hr />\n";
1733 echo "<input name=\"note\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1736 $mycards = DB_get_hand($me);
1737 $mycards = mysort($mycards,$gametype);
1738 echo "<div class=\"mycards\">\n";
1740 if($myturn && !myisset("card") && $mystatus=='play' )
1742 echo "Hello ".$myname.", it's your turn! <br />\n";
1743 echo "Your cards are: <br />\n";
1745 /* do we have to follow suite? */
1747 if(have_suit($mycards,$firstcard))
1750 foreach($mycards as $card)
1752 if( ($followsuit && !same_type($card,$firstcard)) ||
1753 ( (int)($card)==19 && ($RULES['schweinchen']=='second'||$RULES['schweinchen']=='secondaftercall')
1754 && $GAME['schweinchen-who']==$me && !$GAME['schweinchen-first'] )
1756 display_card($card,$PREF["cardset"]);
1758 display_link_card($card,$PREF["cardset"]);
1761 else if($mystatus=='play' )
1763 echo "Your cards are: <br />\n";
1764 foreach($mycards as $card)
1765 display_card($card,$PREF["cardset"]);
1767 else if($mystatus=='gameover')
1769 $oldcards = DB_get_all_hand($me);
1770 $oldcards = mysort($oldcards,$gametype);
1771 echo "Your cards were: <br />\n";
1772 foreach($oldcards as $card)
1773 display_card($card,$PREF["cardset"]);
1775 $userids = DB_get_all_userid_by_gameid($gameid);
1776 foreach($userids as $user)
1778 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1784 $name = DB_get_name('userid',$user);
1785 $oldcards = DB_get_all_hand($userhash);
1786 $oldcards = mysort($oldcards,$gametype);
1787 echo "$name's cards were: <br />\n";
1788 foreach($oldcards as $card)
1789 display_card($card,$PREF["cardset"]);
1795 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1796 if($mystatus=='play')
1799 /* the following happens only when the gamestatus is 'gameover' */
1800 /* check if game is over, display results */
1801 if(DB_get_game_status_by_gameid($gameid)=='play')
1803 echo "The game is over for you.. other people still need to play though";
1807 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1808 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1809 " LEFT JOIN User ON User.id=Hand.user_id".
1810 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1811 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1812 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1813 " WHERE Hand.game_id='$gameid'".
1814 " GROUP BY Hand.party" );
1815 echo "<div class=\"total\"> Totals:<br />\n";
1816 while( $r = DB_fetch_array($result))
1817 echo " ".$r[0]." ".$r[1]."<br />\n";
1819 $queryresult = DB_query("SELECT timediff(mod_date,create_date) ".
1820 " FROM Game WHERE id='$gameid'");
1821 $r = DB_fetch_array($queryresult);
1822 echo "<p>This game took ".$r[0]." hours.</p>";
1824 echo "<div class=\"re\">\n Points Re: <br />\n";
1825 $queryresult = DB_query("SELECT score FROM Score ".
1826 " WHERE game_id=$gameid AND party='re'".
1828 while($r = DB_fetch_array($queryresult) )
1829 echo " ".$r[0]."<br />\n";
1832 echo "<div class=\"contra\">\n Points Contra: <br />\n";
1833 $queryresult = DB_query("SELECT score FROM Score ".
1834 " WHERE game_id=$gameid AND party='contra'".
1836 while($r = DB_fetch_array($queryresult) )
1837 echo " ".$r[0]."<br />\n";
1846 myerror("error in testing the status");
1848 /* output left menu */
1849 display_user_menu();
1851 /* output right menu */
1853 /* display rule set for this game */
1854 echo "<div class=\"gameinfo\">\n";
1856 if($gamestatus == 'play' )
1857 output_form_calls($me);
1859 /* get time from the last action of the game */
1860 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1861 $gameend = time() - strtotime($r[0]);
1863 if($gamestatus == 'play' || $gameend < 60*60*24*7)
1865 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1868 echo "<input type=\"submit\" value=\"submit\" />\n";
1874 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1877 $session = DB_get_session_by_gameid($gameid);
1878 $result = DB_query("SELECT id,create_date FROM Game".
1879 " WHERE session=$session".
1880 " ORDER BY create_date DESC".
1884 $r = DB_fetch_array($result);
1886 if(!$session || $gameid==$r[0])
1888 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
1889 $names = DB_get_all_names_by_gameid($gameid);
1890 $type = DB_get_gametype_by_gameid($gameid);
1894 $solo = DB_get_solo_by_gameid($gameid);
1896 if($solo!='silent') /* repeat game with same first player */
1897 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
1898 else /* rotate normally */
1899 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
1901 else /* rotate normally */
1902 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);