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";
110 /* handle user notes (only possible while game is running)*/
111 if( $mystatus!='gameover' )
114 $note = $_REQUEST['note'];
117 DB_insert_note($note,$gameid,$myid);
119 output_user_notes($myid,$gameid,$mystatus);
121 /* output extra division in case this game is part of a session */
124 echo "<div class=\"session\">\n";
125 echo " <div class=\"sessionrules\">Rules (+icons fur rules) \n";
127 echo " 10ofhearts : ".$RULES["dullen"] ."<br />\n";
128 echo " schweinchen: ".$RULES["schweinchen"] ."<br />\n";
129 echo " call: ".$RULES["call"] ."<br />\n";
130 echo " </div>\n </div>\n";
131 echo " <div class=\"sessionscore\">Score \n";
132 $score = generate_score_table($session);
133 echo format_score_table_html($score,$myid);
135 $hashes = DB_get_hashes_by_session($session,$myid);
137 foreach($hashes as $hash)
145 echo "This is game number $j of <a href=\"".$INDEX."?action=game&me=$lasthash\">$i</a> in session $session.";
149 /* display the table and the names */
152 /* mystatus gets the player through the different stages of a game.
153 * start: does the player want to play?
154 * init: check for sickness
155 * check: check for return values from init
156 * poverty: handle poverty, wait here until all player have reached this state
157 * display sickness and move on to game
158 * play: game in progress
159 * gameover: are we revisiting a game
164 /* don't ask if user has autosetup set to yest */
166 if($PREF['autosetup']=='yes') $skip = 1;
168 if( !myisset("in") && !$skip)
170 /* asks the player, if he wants to join the game */
171 output_check_want_to_play($me);
176 /* check the result, if player wants to join, got next stage, else cancel game */
177 if(!$skip && $_REQUEST["in"] == "no" )
179 /* cancel the game */
180 $message = "Hello, \n\n".
181 "the game has been canceled due to the request of one of the players.\n";
183 $userids = DB_get_all_userid_by_gameid($gameid);
184 foreach($userids as $user)
186 $To = DB_get_email('userid',$user);
187 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
190 /* delete everything from the dB */
196 /* user wants to join the game */
198 /* move on to the next stage,
199 * no break statement to immediately go to the next stage
202 DB_set_hand_status_by_hash($me,'init');
204 /* check if everyone has reached this stage, send out email */
205 $userids = DB_get_all_userid_by_gameid($gameid);
207 foreach($userids as $user)
209 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
210 if($userstat!='init')
212 /* whos turn is it? */
213 DB_set_player_by_gameid($gameid,$user);
220 /* all done, send out email unless this player is the startplayer */
221 $startplayer = DB_get_startplayer_by_gameid($gameid);
222 if($mypos == $startplayer)
224 /* do nothing, go to next stage */
228 /* email startplayer */
230 $email = DB_get_email('position-gameid',$startplayer,$gameid);
231 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
232 $who = DB_get_userid('email',$email);
233 DB_set_player_by_gameid($gameid,$who);
235 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
236 "Use this link to go the game: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
237 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
244 /* here we ask the player if he is sick */
245 $mycards = DB_get_hand($me);
246 $mycards = mysort($mycards,$gametype);
248 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
249 echo "\n<ul class=\"tricks\">\n";
250 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
251 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
252 " <div class=\"trick\" id=\"trick0\">\n";
254 for($pos=1;$pos<5;$pos++)
256 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
257 $userid = DB_get_userid('gameid-position',$gameid,$pos);
258 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
260 if($userstatus=='start' || $userstatus=='init')
261 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs to decide </div>\n"; /* show this to everyone */
263 if($usersick!=NULL && $pos<=$mypos ) /* only show this for people sitting before the player */
264 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
265 else if($usersick==NULL && $pos<=$mypos)
266 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
268 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
269 /* end displaying sickness */
271 if(!myisset("solo","wedding","poverty","nines") )
273 output_check_for_sickness($me,$mycards);
275 echo "<div class=\"mycards\">Your cards are: <br />\n";
276 foreach($mycards as $card)
277 display_card($card,$PREF["cardset"]);
284 /* check if someone selected more than one sickness */
286 if($_REQUEST["solo"]!="No") $Nsickness++;
287 if($_REQUEST["wedding"] == "yes") $Nsickness++;
288 if($_REQUEST["poverty"] == "yes") $Nsickness++;
289 if($_REQUEST["nines"] == "yes") $Nsickness++;
293 echo "<p class=\"message\"> You selected more than one sickness, please go back ".
294 "and answer the <a href=\"$INDEX?action=game&me=$me&in=yes\">question</a> again.</p>";
296 echo "<div class=\"mycards\">Your cards are: <br />\n";
297 foreach($mycards as $card)
298 display_card($card,$PREF["cardset"]);
305 /* everything is ok, save what user said and proceed */
306 echo "<p class=\"message\">Processing what you selected in the last step...";
308 /* check if this sickness needs to be handled first */
309 $gametype = DB_get_gametype_by_gameid($gameid);
310 $startplayer = DB_get_startplayer_by_gameid($gameid); /* need this to check which solo goes first */
312 if( $_REQUEST["solo"]!="No" )
314 /* user wants to play a solo */
316 /* store the info in the user's hand info */
317 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
318 DB_set_sickness_by_hash($me,"solo");
320 echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
322 if($gametype == "solo" && $startplayer<$mypos)
323 {}/* do nothing, since someone else already is playing solo */
326 /* this solo comes first
327 * store info in game table
329 DB_set_gametype_by_gameid($gameid,"solo");
330 DB_set_startplayer_by_gameid($gameid,$mypos);
331 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
334 else if($_REQUEST["wedding"] == "yes")
336 /* silent solo is set further down */
337 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
338 DB_set_sickness_by_hash($me,"wedding");
340 else if($_REQUEST["poverty"] == "yes")
342 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
343 DB_set_sickness_by_hash($me,"poverty");
345 else if($_REQUEST["nines"] == "yes")
347 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
348 " is playing solo, this game will be canceled.<br />\n";
349 DB_set_sickness_by_hash($me,"nines");
354 /* move on to the next stage*/
355 DB_set_hand_status_by_hash($me,'check');
360 /* here we check what all players said and figure out what game we are playing
361 * this can therefore only be handled once all players finished the last stage
364 /* only need to redisplay the cards when the user reloads the page and lands directly here */
365 if($mystatus=='check')
367 $mycards = DB_get_hand($me);
368 $mycards = mysort($mycards,$gametype);
370 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
371 echo "\n<ul class=\"tricks\">\n";
372 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
373 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
374 " <div class=\"trick\" id=\"trick0\">\n";
376 for($pos=1;$pos<5;$pos++)
378 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
379 $userid = DB_get_userid('gameid-position',$gameid,$pos);
380 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
382 if($userstatus=='start' || $userstatus=='init')
383 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs to decide </div>\n"; /* show this to everyone */
385 if($usersick!=NULL) /* in the init-phase we only showed players with $pos<$mypos, now we can show all */
386 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
388 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
390 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
391 /* 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";
496 else if($poverty==1) /* one person has poverty */
498 DB_set_gametype_by_gameid($gameid,"poverty");
499 $gametype = "poverty";
500 $who = DB_get_sickness_by_gameid($gameid);
503 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
504 if($firstsick == "poverty")
505 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
507 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
510 else if($poverty==2) /* two people have poverty */
512 DB_set_gametype_by_gameid($gameid,"dpoverty");
513 $gametype = "dpoverty";
514 $who = DB_get_sickness_by_gameid($gameid);
517 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
518 if($firstsick == "poverty")
520 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
521 if($secondsick == "poverty")
522 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
524 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
527 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
532 DB_set_gametype_by_gameid($gameid,"wedding");
533 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
534 $gametype = "wedding";
536 /* now the gametype is set correctly in the database */
537 echo "<p> Got it :)</p>";
539 /* loop over all players, set re/contra if possible and start the game if possible */
540 $userids = DB_get_all_userid_by_gameid($gameid);
541 foreach($userids as $userid)
543 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
548 /* are we the solo player? set us to re, else set us to contra */
549 $pos = DB_get_pos_by_hash($userhash);
550 if($pos == $startplayer)
551 DB_set_party_by_hash($userhash,"re");
553 DB_set_party_by_hash($userhash,"contra");
554 DB_set_hand_status_by_hash($userhash,'play');
558 /* set person with the wedding to re, do the rest during the game */
559 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
560 if($usersick == "wedding")
561 DB_set_party_by_hash($userhash,"re");
563 DB_set_party_by_hash($userhash,"contra");
565 DB_set_hand_status_by_hash($userhash,'play');
569 $hand = DB_get_all_hand($userhash);
571 if(in_array('3',$hand)||in_array('4',$hand))
572 DB_set_party_by_hash($userhash,"re");
574 DB_set_party_by_hash($userhash,"contra");
575 DB_set_hand_status_by_hash($userhash,'play');
579 /* set person with poverty to play status */
580 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
581 if($usersick == "poverty")
582 DB_set_hand_status_by_hash($userhash,'play');
584 /* set status of first player to be asked to poverty */
585 $who = DB_get_sickness_by_gameid($gameid);
586 if($who > 6) $who= $who/10; /* in case we have dpoverty */
587 $whoid = DB_get_userid('gameid-position',$gameid,$who);
589 DB_set_hand_status_by_hash($userhash,'poverty');
592 /* check for silent solo, set game type to solo in this case */
593 $gametype = DB_get_gametype_by_gameid($gameid);
594 $userids = DB_get_all_userid_by_gameid($gameid);
595 foreach($userids as $userid)
597 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
599 if($gametype=='normal')
601 $userhand = DB_get_all_hand($userhash);
602 if(check_wedding($userhand))
604 /* normal game type and player has both queens -> silent solo */
605 /* keep startplayer, just set gametype to silent solo */
606 DB_set_gametype_by_gameid($gameid,"solo");
607 DB_set_solo_by_gameid($gameid,'silent');
612 /* send out email to first player or poverty person*/
613 if($gametype!="poverty" && $gametype!="dpoverty")
615 $startplayer = DB_get_startplayer_by_gameid($gameid);
616 $email = DB_get_email('position-gameid',$startplayer,$gameid);
617 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
618 $who = DB_get_userid('email',$email);
619 DB_set_player_by_gameid($gameid,$who);
623 if(DB_get_email_pref_by_hash($hash)!="emailaddict")
625 /* email startplayer */
626 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
627 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
628 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
632 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
636 /* set status of first player to be asked to poverty */
637 $who = DB_get_sickness_by_gameid($gameid);
638 if($who > 6) $who= $who/10; /* in case we have dpoverty */
640 $whoid = DB_get_userid('gameid-position',$gameid,$who);
642 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
645 $email = DB_get_email('position-gameid',$who,$gameid);
646 $whohash = DB_get_hash_from_game_and_pos($gameid,$who);
647 DB_set_player_by_gameid($gameid,$whoid);
649 if(DB_get_email_pref_by_hash($hash)!="emailaddict")
651 /* email player for poverty */
652 $message = "Poverty: It's your turn now in game ".DB_format_gameid($gameid).".\n".
653 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$whohash."\n\n" ;
654 mymail($email,$EmailName."Poverty (game ".DB_format_gameid($gameid).") ",$message);
662 /* user only gets here in a poverty game, several things have to be handled here:
663 * A) ask, if user wants to take trump
665 * poverty: set re/contra
666 * dpoverty: first time: set re, send email to second player
667 * second time: set contra
668 * poverty: set status of other players to 'play'
669 * set status to play in case 0 trump
670 * no -> set status to play,
671 * ask next player or cancle the game if no more players
672 * B) user took trump and has too many cards (e.g. count(cards)>12 and re/contra set)
673 * ask to give cards back, set status to play, once player has 12 cards
675 * it is easier to check B) first
678 /* output pre game in case user reloads */
681 set_gametype($gametype); /* this sets the $CARDS variable */
682 $myparty = DB_get_party_by_hash($me);
684 /* the following is part B) of whats needs to be done)
685 /* check if user wants to give cards back */
686 if(myisset("exchange"))
688 $exchange = $_REQUEST['exchange'];
689 $partnerhash = DB_get_partner_hash_by_hash($me);
690 $partnerid = DB_get_userid('hash',$partnerhash);
691 $partnerhand = DB_get_handid('gameid-userid',$gameid,$partnerid);
693 /* if exchange is set to a value>0, exchange that card back to the partner */
696 $result = DB_query("UPDATE Hand_Card SET hand_id='$partnerhand'".
697 " WHERE hand_id='$myhand' AND card_id=".DB_quote_smart($exchange));
702 $mycards = DB_get_hand($me);
703 $mycards = mysort($mycards,$gametype);
705 /* check if user need to give more cards back */
706 if( ($myparty=='re' || $myparty=='contra') && count($mycards)>12)
708 echo "<div class=\"poverty\"> you need to get rid of a few cards</div>\n";
711 echo "<div class=\"mycards\">Your cards are: <br />\n";
712 foreach($mycards as $card)
713 display_link_card($card,$PREF["cardset"],$type);
714 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select card to give back\" />\n";
717 else if( ($myparty=='re' || $myparty=='contra') && count($mycards)==12)
719 /* user is done, ready to play */
720 DB_set_hand_status_by_hash($me,'play');
722 /* email start player */
723 $startplayer = DB_get_startplayer_by_gameid($gameid);
724 $email = DB_get_email('position-gameid',$startplayer,$gameid);
725 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
726 $who = DB_get_userid('email',$email);
727 DB_set_player_by_gameid($gameid,$who);
731 if(DB_get_email_pref_by_hash($hash)!="emailaddict")
733 /* email startplayer */
734 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
735 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
736 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
740 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
743 /* the following is part A) of what needs to be done */
744 if(!myisset("trump"))
748 echo "<div class=\"poverty\">\n";
749 $userids = DB_get_all_userid_by_gameid($gameid);
750 foreach($userids as $user)
752 $name = DB_get_name('userid',$user);
753 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
754 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
755 $userparty = DB_get_party_by_hash($userhash);
757 if($usersick=="poverty" && !$userparty)
759 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
760 $cards = DB_get_hand($hash);
763 foreach($cards as $card)
764 if($card<27) $nrtrump++;
765 echo "Player $name has $nrtrump trump. Do you want to take them?".
766 "<a href=\"index.php?action=game&me=$me&trump=$user\">Yes</a> <br />\n";
769 echo "<a href=\"index.php?action=game&me=$me&trump=no\">No way</a> <br />\n";
770 echo "</div><div>\n";
772 echo "<div class=\"mycards\">Your cards are: <br />\n";
773 foreach($mycards as $card)
774 display_card($card,$PREF["cardset"]);
775 echo "</div></div>\n";
781 $trump = $_REQUEST['trump'];
785 /* user doesn't want to take trump */
786 DB_set_hand_status_by_hash($me,'play');
788 /* set next player who needs to be asked and email him*/
789 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
790 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
792 /* don't ask people who have poverty */
794 if($firstsick=="poverty")
796 if($secondsick=="poverty")
801 if($gametype=="dpoverty")
803 $next=999; /* need to cancel for sure, since both would need to take the trump */
806 /* no more people to ask, need to cancel the game */
809 $message = "Hello, \n\n".
810 "Game ".DB_format_gameid($gameid)." has been canceled since nobody wanted to take the trump.\n";
812 $userids = DB_get_all_userid_by_gameid($gameid);
813 foreach($userids as $user)
815 $To = DB_get_email('userid',$user);
816 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled (poverty not resolved)",$message);
819 /* delete everything from the dB */
822 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been canceled.<br /><br /></p>";
829 /* email next player, set his status to poverty */
830 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
831 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
832 $userid = DB_get_userid('email',$To);
834 DB_set_player_by_gameid($gameid,$userid);
835 DB_set_hand_status_by_hash($userhash,'poverty');
837 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
838 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
839 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
844 /* player wants to take trump, change cards */
846 /* user wants to take trump */
847 $trump = $_REQUEST["trump"];
848 $userhand = DB_get_handid('gameid-userid',$gameid,$trump);
849 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
851 /* copy trump from player A to B */
852 $result = DB_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
855 $mycards = DB_get_hand($me);
858 if($gametype=='poverty')
860 $userids = DB_get_all_userid_by_gameid($gameid);
861 foreach($userids as $user)
863 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
864 if($hash==$userhash||$hash==$me)
866 DB_set_party_by_hash($hash,"re");
870 DB_set_party_by_hash($hash,"contra");
871 DB_set_hand_status_by_hash($hash,'play'); /* the contra party is ready to play */
874 /* check if we are done (in case of no trump handed over), if so, go to 'play' phase right away*/
875 if(count($mycards)==12)
877 DB_set_hand_status_by_hash($me,'play');
882 /* has the re party already been set?*/
884 $userids = DB_get_all_userid_by_gameid($gameid);
885 foreach($userids as $user)
887 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
888 $party = DB_get_party_by_hash($hash);
894 DB_set_party_by_hash($me,'contra');
895 DB_set_party_by_hash($userhash,'contra');
899 DB_set_party_by_hash($me,'re');
900 DB_set_party_by_hash($userhash,'re');
902 /* send out email to second non-poverty player */
903 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
904 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
907 if($firstsick=="poverty")
908 if($secondsick=="poverty")
914 echo "<div class=\"message\">Error in poverty, please contact the Admin</div>\n";
916 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
917 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
918 $userid = DB_get_userid('email',$To);
920 DB_set_player_by_gameid($gameid,$userid);
921 DB_set_hand_status_by_hash($userhash,'poverty');
923 $message = "Two people have poverty, it's your turn to decide, if you want to take the trump. Please visit:".
924 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
925 mymail($To,$EmailName." double poverty (game ".DB_format_gameid($gameid).")",$message);
930 echo "<div class=\"message\"> Please, <a href=\"$INDEX?action=game&me=$me\">continue</a> here.</div>\n";
938 /* both entries here, so that the tricks are visible for both.
939 * in case of 'play' there is a break later that skips the last part
942 /* check if all players are ready to play,
943 * if so, send out email to the startplayer
944 * only need to do this if the game hasn't started yet
946 $gamestatus = DB_get_game_status_by_gameid($gameid);
947 if($gamestatus == 'pre')
950 $userids = DB_get_all_userid_by_gameid($gameid);
951 foreach($userids as $user)
953 $userstatus = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
954 if($userstatus !='play' && $userstatus!='gameover')
957 DB_set_player_by_gameid($gameid,$user);
963 /* only set this after all poverty, etc. are handled*/
964 DB_set_game_status_by_gameid($gameid,'play');
966 /* email startplayer */
967 $startplayer = DB_get_startplayer_by_gameid($gameid);
968 $email = DB_get_email('position-gameid',$startplayer,$gameid);
969 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
970 $who = DB_get_userid('email',$email);
971 DB_set_player_by_gameid($gameid,$who);
973 if($hash!=$me && DB_get_email_pref_by_hash($hash)!="emailaddict")
975 /* email startplayer) */
976 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
977 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
978 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
982 /* figure out what kind of game we are playing,
983 * set the global variables $CARDS["trump"],$CARDS["diamonds"],$CARDS["hearts"],
984 * $CARDS["clubs"],$CARDS["spades"],$CARDS["foxes"]
988 $gametype = DB_get_gametype_by_gameid($gameid);
990 if($gametype=="solo")
992 $gametype = DB_get_solo_by_gameid($gameid);
993 if($gametype=='silent')
996 $GT = $gametype." ".$GT;
999 $gametype = "normal";
1001 set_gametype($gametype); /* this sets the $CARDS variable */
1003 /* get some infos about the game, need to reset this, since it might have changed */
1004 $gamestatus = DB_get_game_status_by_gameid($gameid);
1006 /* has the game started? No, then just wait here...*/
1007 if($gamestatus == 'pre')
1009 echo "<p class=\"message\"> You finished the setup, but not everyone else finished it... ".
1010 "You need to wait for the others. Just wait for an email. </p>";
1012 $mycards = DB_get_hand($me);
1013 $mycards = mysort($mycards,$gametype);
1015 echo "<div class=\"mycards\">Your cards are: <br />\n";
1016 foreach($mycards as $card)
1017 display_card($card,$PREF["cardset"]);
1020 break; /* not sure this works... the idea is that you can
1021 * only play a card after everyone is ready to play */
1024 /* get time from the last action of the game */
1025 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1026 $gameend = time() - strtotime($r[0]);
1028 /* handle comments in case player didn't play a card, allow comments a week after the end of the game */
1029 if( (!myisset("card") && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
1030 if(myisset("comment"))
1032 $comment = $_REQUEST["comment"];
1033 $playid = DB_get_current_playid($gameid);
1036 DB_insert_comment($comment,$playid,$myid);
1039 /* get everything relevant to display the tricks */
1040 $result = DB_query("SELECT Hand_Card.card_id as card,".
1041 " Hand.position as position,".
1042 " Play.sequence as sequence, ".
1044 " GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
1045 " SEPARATOR '\n' ), ".
1046 " Play.create_date, ".
1049 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1050 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1051 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1052 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1053 "LEFT JOIN User On User.id=Comment.user_id ".
1054 "WHERE Trick.game_id='".$gameid."' ".
1055 "GROUP BY Trick.id, sequence ".
1056 "ORDER BY Trick.id, sequence ASC");
1058 $lasttrick = DB_get_max_trickid($gameid);
1060 $play = array(); /* needed to calculate winner later */
1062 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1063 $firstcard = ""; /* first card in a trick */
1065 echo "\n<ul class=\"tricks\">\n";
1066 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
1068 /* output vorbehalte */
1069 $mygametype = DB_get_gametype_by_gameid($gameid);
1070 if($mygametype != 'normal' && $mygametype != 'silent') /* only show when needed */
1072 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
1073 " <div class=\"trick\" id=\"trick0\">\n";
1075 for($mypos=1;$mypos<5;$mypos++)
1077 $usersick = DB_get_sickness_by_pos_and_gameid($mypos,$gameid);
1080 echo " <div class=\"vorbehalt".($mypos-1)."\"> Vorbehalt <br />";
1082 echo " $usersick <br />";
1085 if($mygametype == $usersick)
1089 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1093 while($r = DB_fetch_array($result))
1101 /* count number of tricks */
1105 /* check if first schweinchen has been played */
1106 if( $GAME['schweinchen-who'] && ($r[0] == 19 || $r[0] == 20) )
1107 if(!$GAME['schweinchen-first'])
1108 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1110 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1112 /* save card to be able to find the winner of the trick later */
1113 $play[$seq] = array("card"=>$r[0],"pos"=>$pos);
1117 /* first card in a trick, output some html */
1118 if($trick!=$lasttrick)
1120 /* start of an old trick? */
1121 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1122 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1123 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1125 else if($trick==$lasttrick)
1127 /* start of a last trick? */
1128 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1129 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1130 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1133 /* remember first card, so that we are able to check, what cards can be played */
1138 echo " <div class=\"card".($pos-1)."\">\n";
1140 /* display comments */
1142 echo " <span class=\"comment\">".$comment."</span>\n";
1145 display_card($r[0],$PREF["cardset"]);
1147 echo " </div>\n"; /* end div card */
1152 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1156 /* whos turn is it? */
1159 $winner = get_winner($play,$gametype); /* returns the position */
1161 $firstcard = ""; /* new trick, no first card */
1166 if($next==5) $next = 1;
1169 /* my turn?, display cards as links, ask for comments*/
1170 if(DB_get_pos_by_hash($me) == $next)
1175 /* do we want to play a card? */
1176 if(myisset("card") && $myturn)
1178 $card = $_REQUEST["card"];
1179 $handid = DB_get_handid('hash',$me);
1180 $commentSchweinchen =""; /* used to add a comment when Schweinchen is being played */
1182 /* check if we have card and that we haven't played it yet*/
1183 /* set played in hand_card to true where hand_id and card_id*/
1184 $r = DB_query_array("SELECT id FROM Hand_Card WHERE played='false' and ".
1185 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1186 $handcardid = $r[0];
1188 if($handcardid) /* everything ok, play card */
1190 /* update Game timestamp */
1191 DB_update_game_timestamp($gameid);
1193 /* check if a call was made, must do this before we set the card status to played */
1194 if(myisset("call") && $_REQUEST["call"] == "120" && can_call(120,$me))
1195 $result = DB_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
1196 if(myisset("call") && $_REQUEST["call"] == "90" && can_call(90,$me))
1197 $result = DB_query("UPDATE Hand SET point_call='90' WHERE hash='$me' ");
1198 if(myisset("call") && $_REQUEST["call"] == "60" && can_call(60,$me))
1199 $result = DB_query("UPDATE Hand SET point_call='60' WHERE hash='$me' ");
1200 if(myisset("call") && $_REQUEST["call"] == "30" && can_call(30,$me))
1201 $result = DB_query("UPDATE Hand SET point_call='30' WHERE hash='$me' ");
1202 if(myisset("call") && $_REQUEST["call"] == "0" && can_call(0,$me))
1203 $result = DB_query("UPDATE Hand SET point_call='0' WHERE hash='$me' ");
1205 /* mark card as played */
1206 DB_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1207 DB_quote_smart($card));
1209 /* get trick id or start new trick */
1210 $a = DB_get_current_trickid($gameid);
1215 $playid = DB_play_card($trickid,$handcardid,$sequence);
1217 /* check special output for schweinchen in case in case a fox is being played
1218 * check for correct rules, etc. has already been done
1220 if( $GAME["schweinchen-who"] && ($card == 19 || $card == 20) )
1222 if(!$GAME['schweinchen-first'])
1223 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1225 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1227 if( ($GAME['schweinchen-second']==1 && $RULES['schweinchen']=='second') || $RULES['schweinchen']=='both')
1229 DB_insert_comment("Schweinchen! ",$playid,$myid);
1230 $commentSchweinchen = "Schweinchen! ";
1233 echo "schweinchen = ".$GAME["schweinchen-who"]." ---<br />";
1236 /* if sequence == 4 check who one in case of wedding */
1237 if($sequence == 4 && $GT == "wedding")
1239 /* is wedding resolve */
1240 $resolved = DB_get_sickness_by_gameid($gameid);
1243 /* who has wedding */
1244 $userids = DB_get_all_userid_by_gameid($gameid);
1245 foreach($userids as $user)
1247 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1248 if($usersick == "wedding")
1251 /* who won the trick */
1252 $play = DB_get_cards_by_trick($trickid);
1253 $winner = get_winner($play,$gametype); /* returns the position */
1254 $winnerid = DB_get_userid('gameid-position',$gameid,$winner);
1255 /* is tricknr <=3 */
1256 if($tricknr <=3 && $winnerid!=$whosick)
1258 /* set resolved at tricknr*/
1259 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1261 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1262 DB_set_party_by_hash($whash,"re");
1264 if($tricknr == 3 && $winnerid==$whosick)
1266 /* set resolved at tricknr*/
1267 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1272 /* if sequence == 4, set winner of the trick, count points and set the next player */
1275 $play = DB_get_cards_by_trick($trickid);
1276 $winner = get_winner($play,$gametype); /* returns the position */
1278 /* check if someone caught a fox */
1279 /* first check if we should account for solos at all,
1280 * since it doesn't make sense in some games
1282 $ok = 0; /* fox shouldn't be counted */
1283 if(DB_get_gametype_by_gameid($gameid)=="solo")
1285 $solo = DB_get_solo_by_gameid($gameid);
1286 if($solo == 'trump' || $solo == 'silent')
1287 $ok = 1; /* for trump solos and silent solos, foxes are ok */
1290 $ok = 1; /* for all other games (not solos) foxes are ok too */
1293 foreach($play as $played)
1295 if ( $played['card']==19 || $played['card']==20 )
1296 if ($played['pos']!= $winner )
1298 /* possible caught a fox, check party */
1299 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1300 $uid2 = DB_get_userid('gameid-position',$gameid,$played['pos']);
1302 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1303 $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1305 if($party1 != $party2)
1306 DB_query("INSERT INTO Score".
1307 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1311 /* check for karlchen (jack of clubs in the last trick)*/
1312 /* same as for foxes, karlchen doesn't always make sense
1313 * check what kind of game it is and set karlchen accordingly */
1314 $ok = 1; /* default: karlchen should be accounted for */
1316 $ok = 0; /* Karlchen works only in the last trick */
1317 if($ok && DB_get_gametype_by_gameid($gameid)=="solo" )
1319 $solo = DB_get_solo_by_gameid($gameid);
1320 if($solo == "trumpless" || $solo == "jack" || $solo == "queen" )
1321 $ok = 0; /* no Karlchen in these solos */
1325 foreach($play as $played)
1326 if ( $played['card']==11 || $played['card']==12 )
1327 if ($played['pos'] == $winner )
1329 /* possible caught a fox, check party */
1330 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1331 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1333 DB_query("INSERT INTO Score".
1334 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1336 /* check for doppelopf (>40 points)*/
1338 foreach($play as $played)
1340 $points += DB_get_card_value_by_cardid($played['card']);
1344 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1345 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1347 DB_query("INSERT INTO Score".
1348 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
1352 DB_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1354 echo "ERROR during scoring";
1357 echo "DEBUG: position $winner won the trick <br />";
1359 /* who is the next player? */
1364 $next = DB_get_pos_by_hash($me)+1;
1366 if($next==5) $next=1;
1368 /* check for coment */
1369 if(myisset("comment"))
1371 $comment = $_REQUEST["comment"];
1373 DB_insert_comment($comment,$playid,$myid);
1374 if($commentSchweinchen)
1375 $comment = $commentSchweinchen . $comment;
1378 /* check for note */
1381 $note = $_REQUEST["note"];
1383 DB_insert_note($note,$gameid,$myid);
1386 /* display played card */
1387 $pos = DB_get_pos_by_hash($me);
1390 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1391 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1392 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1395 echo " <div class=\"card".($pos-1)."\">\n ";
1397 /* display comments */
1398 display_card($card,$PREF["cardset"]);
1400 echo "\n <span class=\"comment\"> ".$comment."</span>\n";
1403 /*check if we still have cards left, else set status to gameover */
1404 if(sizeof(DB_get_hand($me))==0)
1406 DB_set_hand_status_by_hash($me,'gameover');
1407 $mystatus = 'gameover';
1410 /* if all players are done, set game status to game over,
1411 * get the points of the last trick and send out an email
1414 $userids = DB_get_all_userid_by_gameid($gameid);
1417 foreach($userids as $user)
1418 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1422 DB_set_game_status_by_gameid($gameid,"gameover");
1424 /* email next player, if game is still running */
1425 if(DB_get_game_status_by_gameid($gameid)=='play')
1427 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1428 $email = DB_get_email('hash',$next_hash);
1429 $who = DB_get_userid('email',$email);
1430 DB_set_player_by_gameid($gameid,$who);
1432 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1433 "It's your turn now.\n".
1434 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$next_hash."\n\n" ;
1435 if( DB_get_email_pref_by_uid($who)!="emailaddict" )
1436 mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
1438 else /* send out final email */
1440 /* individual score */
1441 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1442 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1443 " LEFT JOIN User ON User.id=Hand.user_id".
1444 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1445 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1446 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1447 " WHERE Hand.game_id='$gameid'".
1448 " GROUP BY User.fullname" );
1449 $message = "The game is over. Thanks for playing :)\n";
1450 $message .= "Final score:\n";
1451 while( $r = DB_fetch_array($result) )
1452 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1454 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1455 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1456 " LEFT JOIN User ON User.id=Hand.user_id".
1457 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1458 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1459 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1460 " WHERE Hand.game_id='$gameid'".
1461 " GROUP BY Hand.party" );
1462 $message .= "\nTotals:\n";
1465 while( $r = DB_fetch_array($result) )
1467 $message .= " ".$r[0]." ".$r[1]."\n";
1470 else if($r[0] == "contra")
1475 * save score in database
1479 /* get calls from re/contra */
1481 $call_contra = NULL;
1482 foreach($userids as $user)
1484 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1485 $call = DB_get_call_by_hash($hash);
1486 $party = DB_get_party_by_hash($hash);
1490 $call = (int) $call;
1496 else if( $call < $call_re)
1499 else if($party=="contra")
1501 if($call_contra==NULL)
1502 $call_contra = $call;
1503 else if( $call < $call_re)
1504 $call_contra = $call;
1509 /* figure out who one */
1510 $winning_party = NULL;
1512 if($call_re == NULL && $call_contra==NULL)
1514 /* nobody made a call, so it's easy to figure out who won */
1516 $winning_party="re";
1518 $winning_party="contra";
1522 /* if one party makes a call, they only win, iff they make enough points
1523 * if only one party made a call, the other one wins,
1524 * if the first one didn't make it
1528 $offset = 120 - $call_re;
1530 $offset--; /* since we use a > in the next equation */
1532 if($re > 120+$offset)
1533 $winning_party="re";
1534 else if ($call_contra == NULL )
1535 $winning_party="contra";
1540 $offset = 120 - $call_contra;
1541 if($call_contra == 0)
1542 $offset--; /* since we use a > in the next equation */
1544 if($contra > 120+$offset)
1545 $winning_party="contra";
1546 else if ($call_re == NULL )
1547 $winning_party="re";
1551 /* one point for each call of the other party in case the other party didn't win
1552 * and one point each in case the party made more than points than one of the calls
1554 if($winning_party!="contra" && $call_contra!=NULL)
1556 for( $p=$call_contra;$p<=120; $p+=30 )
1558 DB_query("INSERT INTO Score".
1559 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
1562 for( $p=$call_contra; $p<120; $p+=30)
1565 DB_query("INSERT INTO Score".
1566 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'made$p')");
1569 if($winning_party!="re" and $call_re!=NULL)
1571 for( $p=$call_re;$p<=120; $p+=30 )
1573 DB_query("INSERT INTO Score".
1574 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'against$p')");
1577 for( $p=$call_re; $p<120; $p+=30)
1580 DB_query("INSERT INTO Score".
1581 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
1585 /* point in case contra won */
1586 if($winning_party=="contra")
1588 DB_query("INSERT INTO Score".
1589 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1592 /* one point each for winning and each 30 points + calls */
1593 if($winning_party=="re")
1595 foreach(array(120,150,180,210,240) as $p)
1598 if($p==240 || $call_contra!=NULL)
1602 DB_query("INSERT INTO Score".
1603 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
1605 /* re called something and won */
1606 foreach(array(0,30,60,90,120) as $p)
1608 if($call_re!=NULL && $call_re<$p+1)
1609 DB_query("INSERT INTO Score".
1610 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
1613 else if( $winning_party=="contra")
1615 foreach(array(120,150,180,210,240) as $p)
1618 if($p==240 || $call_re!=NULL)
1621 if($contra>$p-$offset)
1622 DB_query("INSERT INTO Score".
1623 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
1625 /* re called something and won */
1626 foreach(array(0,30,60,90,120) as $p)
1628 if($call_contra!=NULL && $call_contra<$p+1)
1629 DB_query("INSERT INTO Score".
1630 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
1635 /* add score points to email */
1638 $message .= " Points Re: \n";
1639 $queryresult = DB_query("SELECT score FROM Score ".
1640 " WHERE game_id=$gameid AND party='re'".
1642 while($r = DB_fetch_array($queryresult) )
1644 $message .= " ".$r[0]."\n";
1647 $message .= " Points Contra: \n";
1648 $queryresult = DB_query("SELECT score FROM Score ".
1649 " WHERE game_id=$gameid AND party='contra'".
1651 while($r = DB_fetch_array($queryresult) )
1653 $message .= " ".$r[0]."\n";
1656 $message .= " Total Points (from the Re point of view): $Tpoint\n";
1659 $session = DB_get_session_by_gameid($gameid);
1660 $score = generate_score_table($session);
1662 $message .= "Score Table:\n";
1663 $message .= format_score_table_ascii($score);
1665 /* send out final email */
1668 foreach($userids as $user)
1669 $all[] = DB_get_email('userid',$user);
1670 $To = implode(",",$all);
1672 $help = "\n\n (you can use reply all on this email to reach all the players.)\n";
1673 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 1(2)",$message.$help);
1675 foreach($userids as $user)
1677 $To = DB_get_email('userid',$user);
1678 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1680 $link = "Use this link to have a look at game ".DB_format_gameid($gameid).": ".
1681 $HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
1682 if( DB_get_email_pref_by_uid($user) != "emailaddict" )
1683 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 2(2)",$link);
1689 echo "can't find that card?! <br />\n";
1692 else if(myisset("card") && !$myturn )
1694 echo "please wait until it's your turn! <br />\n";
1697 if($seq!=4 && $trickNR>=1)
1698 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1700 /* display points in case game is over */
1701 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1703 echo " <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1704 " <div class=\"trick\" id=\"trick13\">\n";
1705 /* add pic for re/contra
1706 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1708 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1709 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1710 " LEFT JOIN User ON User.id=Hand.user_id".
1711 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1712 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1713 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1714 " WHERE Hand.game_id='$gameid'".
1715 " GROUP BY User.fullname" );
1716 while( $r = DB_fetch_array($result))
1717 echo " <div class=\"card".($r[3]-1)."\">\n".
1718 " <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
1721 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1724 echo " <li onclick=\"hl_prev();\" class=\"old\"><a href=\"#\">prev</a></li>\n";
1725 echo " <li onclick=\"hl_next();\" class=\"old\"><a href=\"#\">next</a></li>\n";
1726 echo "</ul>\n"; /* end ul tricks*/
1728 $mycards = DB_get_hand($me);
1729 $mycards = mysort($mycards,$gametype);
1730 echo "<div class=\"mycards\">\n";
1732 if($myturn && !myisset("card") && $mystatus=='play' )
1734 echo "Hello ".$myname.", it's your turn! <br />\n";
1735 echo "Your cards are: <br />\n";
1737 /* do we have to follow suite? */
1739 if(have_suit($mycards,$firstcard))
1742 foreach($mycards as $card)
1744 if( ($followsuit && !same_type($card,$firstcard)) ||
1745 ( (int)($card)==19 && ($RULES['schweinchen']=='second'||$RULES['schweinchen']=='secondaftercall')
1746 && $GAME['schweinchen-who']==$me && !$GAME['schweinchen-first'] )
1748 display_card($card,$PREF["cardset"]);
1750 display_link_card($card,$PREF["cardset"]);
1753 else if($mystatus=='play' )
1755 echo "Your cards are: <br />\n";
1756 foreach($mycards as $card)
1757 display_card($card,$PREF["cardset"]);
1759 else if($mystatus=='gameover')
1761 $oldcards = DB_get_all_hand($me);
1762 $oldcards = mysort($oldcards,$gametype);
1763 echo "Your cards were: <br />\n";
1764 foreach($oldcards as $card)
1765 display_card($card,$PREF["cardset"]);
1767 $userids = DB_get_all_userid_by_gameid($gameid);
1768 foreach($userids as $user)
1770 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1776 $name = DB_get_name('userid',$user);
1777 $oldcards = DB_get_all_hand($userhash);
1778 $oldcards = mysort($oldcards,$gametype);
1779 echo "$name's cards were: <br />\n";
1780 foreach($oldcards as $card)
1781 display_card($card,$PREF["cardset"]);
1787 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1788 if($mystatus=='play')
1791 /* the following happens only when the gamestatus is 'gameover' */
1792 /* check if game is over, display results */
1793 if(DB_get_game_status_by_gameid($gameid)=='play')
1795 echo "The game is over for you.. other people still need to play though";
1799 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1800 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1801 " LEFT JOIN User ON User.id=Hand.user_id".
1802 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1803 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1804 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1805 " WHERE Hand.game_id='$gameid'".
1806 " GROUP BY Hand.party" );
1807 echo "<div class=\"total\"> Totals:<br />\n";
1808 while( $r = DB_fetch_array($result))
1809 echo " ".$r[0]." ".$r[1]."<br />\n";
1811 $queryresult = DB_query("SELECT timediff(mod_date,create_date) ".
1812 " FROM Game WHERE id='$gameid'");
1813 $r = DB_fetch_array($queryresult);
1814 echo "<p>This game took ".$r[0]." hours.</p>";
1816 echo "<div class=\"re\">\n Points Re: <br />\n";
1817 $queryresult = DB_query("SELECT score FROM Score ".
1818 " WHERE game_id=$gameid AND party='re'".
1820 while($r = DB_fetch_array($queryresult) )
1821 echo " ".$r[0]."<br />\n";
1824 echo "<div class=\"contra\">\n Points Contra: <br />\n";
1825 $queryresult = DB_query("SELECT score FROM Score ".
1826 " WHERE game_id=$gameid AND party='contra'".
1828 while($r = DB_fetch_array($queryresult) )
1829 echo " ".$r[0]."<br />\n";
1838 myerror("error in testing the status");
1840 /* output left menu */
1841 display_user_menu();
1843 /* output right menu */
1845 /* display rule set for this game */
1846 echo "<div class=\"gameinfo\">\n";
1848 if($gamestatus == 'play' )
1849 output_form_calls($me);
1851 /* get time from the last action of the game */
1852 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1853 $gameend = time() - strtotime($r[0]);
1855 if($gamestatus == 'play' || $gameend < 60*60*24*7)
1857 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1860 echo "<input type=\"submit\" value=\"submit\" />\n";
1866 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1869 $session = DB_get_session_by_gameid($gameid);
1870 $result = DB_query("SELECT id,create_date FROM Game".
1871 " WHERE session=$session".
1872 " ORDER BY create_date DESC".
1876 $r = DB_fetch_array($result);
1878 if(!$session || $gameid==$r[0])
1880 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
1881 $names = DB_get_all_names_by_gameid($gameid);
1882 $type = DB_get_gametype_by_gameid($gameid);
1886 $solo = DB_get_solo_by_gameid($gameid);
1888 if($solo!='silent') /* repeat game with same first player */
1889 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
1890 else /* rotate normally */
1891 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
1893 else /* rotate normally */
1894 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);