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 "This game is part of session $session: \n";
117 $hashes = DB_get_hashes_by_session($session,$myid);
119 foreach($hashes as $hash)
124 echo "<a href=\"".$INDEX."?action=game&me=".$hash."\">$i</a> \n";
130 /* display the table and the names */
133 /* mystatus gets the player through the different stages of a game.
134 * start: does the player want to play?
135 * init: check for sickness
136 * check: check for return values from init
137 * poverty: handle poverty, wait here until all player have reached this state
138 * display sickness and move on to game
139 * play: game in progress
140 * gameover: are we revisiting a game
147 /* asks the player, if he wants to join the game */
148 output_check_want_to_play($me);
153 /* check the result, if player wants to join, got next stage, else cancel game */
154 if($_REQUEST["in"] == "no")
156 /* cancel the game */
157 $message = "Hello, \n\n".
158 "the game has been canceled due to the request of one of the players.\n";
160 $userids = DB_get_all_userid_by_gameid($gameid);
161 foreach($userids as $user)
163 $To = DB_get_email('userid',$user);
164 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
167 /* delete everything from the dB */
173 /* user wants to join the game */
175 /* move on to the next stage,
176 * no break statement to immediately go to the next stage
179 DB_set_hand_status_by_hash($me,'init');
181 /* check if everyone has reached this stage, send out email */
182 $userids = DB_get_all_userid_by_gameid($gameid);
184 foreach($userids as $user)
186 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
187 if($userstat!='init')
189 /* whos turn is it? */
190 DB_set_player_by_gameid($gameid,$user);
197 /* all done, send out email unless this player is the startplayer */
198 $startplayer = DB_get_startplayer_by_gameid($gameid);
199 if($mypos == $startplayer)
201 /* do nothing, go to next stage */
205 /* email startplayer */
207 $email = DB_get_email('position-gameid',$startplayer,$gameid);
208 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
209 $who = DB_get_userid('email',$email);
210 DB_set_player_by_gameid($gameid,$who);
212 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
213 "Use this link to go the game: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
214 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
221 /* here we ask the player if he is sick */
222 $mycards = DB_get_hand($me);
223 $mycards = mysort($mycards,$gametype);
225 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
226 echo "\n<ul class=\"tricks\">\n";
227 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
228 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
229 " <div class=\"trick\" id=\"trick0\">\n";
231 for($pos=1;$pos<5;$pos++)
233 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
234 $userid = DB_get_userid('gameid-position',$gameid,$pos);
235 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
237 if($userstatus=='start' || $userstatus=='init')
238 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs to decide </div>\n"; /* show this to everyone */
240 if($usersick!=NULL && $pos<=$mypos ) /* only show this for people sitting before the player */
241 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
242 else if($usersick==NULL && $pos<=$mypos)
243 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
245 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
246 /* end displaying sickness */
248 if(!myisset("solo","wedding","poverty","nines") )
250 output_check_for_sickness($me,$mycards);
252 echo "<div class=\"mycards\">Your cards are: <br />\n";
253 foreach($mycards as $card)
254 display_card($card,$PREF["cardset"]);
261 /* check if someone selected more than one sickness */
263 if($_REQUEST["solo"]!="No") $Nsickness++;
264 if($_REQUEST["wedding"] == "yes") $Nsickness++;
265 if($_REQUEST["poverty"] == "yes") $Nsickness++;
266 if($_REQUEST["nines"] == "yes") $Nsickness++;
270 echo "<p class=\"message\"> You selected more than one sickness, please go back ".
271 "and answer the <a href=\"$INDEX?action=game&me=$me&in=yes\">question</a> again.</p>";
273 echo "<div class=\"mycards\">Your cards are: <br />\n";
274 foreach($mycards as $card)
275 display_card($card,$PREF["cardset"]);
282 /* everything is ok, save what user said and proceed */
283 echo "<p class=\"message\">Processing what you selected in the last step...";
285 /* check if this sickness needs to be handled first */
286 $gametype = DB_get_gametype_by_gameid($gameid);
287 $startplayer = DB_get_startplayer_by_gameid($gameid); /* need this to check which solo goes first */
289 if( $_REQUEST["solo"]!="No" )
291 /* user wants to play a solo */
293 /* store the info in the user's hand info */
294 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
295 DB_set_sickness_by_hash($me,"solo");
297 echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
299 if($gametype == "solo" && $startplayer<$mypos)
300 {}/* do nothing, since someone else already is playing solo */
303 /* this solo comes first
304 * store info in game table
306 DB_set_gametype_by_gameid($gameid,"solo");
307 DB_set_startplayer_by_gameid($gameid,$mypos);
308 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
311 else if($_REQUEST["wedding"] == "yes")
313 /* silent solo is set further down */
314 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
315 DB_set_sickness_by_hash($me,"wedding");
317 else if($_REQUEST["poverty"] == "yes")
319 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
320 DB_set_sickness_by_hash($me,"poverty");
322 else if($_REQUEST["nines"] == "yes")
324 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
325 " is playing solo, this game will be canceled.<br />\n";
326 DB_set_sickness_by_hash($me,"nines");
331 /* move on to the next stage*/
332 DB_set_hand_status_by_hash($me,'check');
337 /* here we check what all players said and figure out what game we are playing
338 * this can therefore only be handled once all players finished the last stage
341 /* only need to redisplay the cards when the user reloads the page and lands directly here */
342 if($mystatus=='check')
344 $mycards = DB_get_hand($me);
345 $mycards = mysort($mycards,$gametype);
347 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
348 echo "\n<ul class=\"tricks\">\n";
349 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
350 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
351 " <div class=\"trick\" id=\"trick0\">\n";
353 for($pos=1;$pos<5;$pos++)
355 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
356 $userid = DB_get_userid('gameid-position',$gameid,$pos);
357 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
359 if($userstatus=='start' || $userstatus=='init')
360 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs to decide </div>\n"; /* show this to everyone */
362 if($usersick!=NULL) /* in the init-phase we only showed players with $pos<$mypos, now we can show all */
363 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
365 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
367 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
368 /* end displaying sickness */
371 echo "<div class=\"message\">\n";
372 echo "<p> Checking if someone else selected solo, nines, wedding or poverty.</p>";
374 /* check if everyone has reached this stage */
375 $userids = DB_get_all_userid_by_gameid($gameid);
377 foreach($userids as $user)
379 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
380 if($userstat!='check')
383 DB_set_player_by_gameid($gameid,$user);
390 echo "<p>This step can only be handled after everyone finished the last step. ".
391 "Seems like this is not the case, so you need to wait a bit... ".
392 "you will get an email once that is the case, please use the link in ".
393 "that email to continue the game.</p></div>";
395 /* display cards, if player was just at the init-phase he will still see the cards from there
396 * we can put this one here, since the last player to finish the init state won't get here and
397 * will still see his card anyway from the init-phase
399 if($mystatus=='check')
402 echo "<div class=\"mycards\">Your cards are: <br />\n";
403 foreach($mycards as $card)
404 display_card($card,$PREF["cardset"]);
411 /* Ok, everyone finished the init-phase, time to figure out what game we
412 * are playing, in case there are any solos this already
413 * will have the correct information in it */
415 echo "<p> Ok, everyone is done... figuring out what kind of game we are playing.</p>";
417 $gametype = DB_get_gametype_by_gameid($gameid);
418 $startplayer = DB_get_startplayer_by_gameid($gameid);
420 /* check for sickness */
425 foreach($userids as $user)
427 $name = DB_get_name('userid',$user);
428 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
429 if($usersick == 'nines')
432 break; /* no need to check for other poverties, since only solo can win and that is already set */
434 else if($usersick == 'poverty')
436 else if($usersick == 'wedding')
438 else if($usersick == 'solo')
442 /* now check which sickness comes first and set the gametype to it */
443 if($gametype == "solo")
450 /* TODO: should we keep statistics of this? */
451 $message = "Hello, \n\n".
452 " the game has been canceled because ".DB_get_name('userid',$nines).
453 " has five or more nines and nobody is playing solo.\n\n".
454 " To redeal either start a new game or, in case the game was part of a tournament, \n".
455 " go to the last game and use the link at the bottom of the page to redeal.";
457 $userids = DB_get_all_userid_by_gameid($gameid);
458 foreach($userids as $user)
460 $To = DB_get_email('userid',$user);
461 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
464 /* delete everything from the dB */
467 echo "The game has been canceled because ".DB_get_name('userid',$nines).
468 " has five or more nines and nobody is playing solo.\n";
473 else if($poverty==1) /* one person has poverty */
475 DB_set_gametype_by_gameid($gameid,"poverty");
476 $gametype = "poverty";
477 $who = DB_get_sickness_by_gameid($gameid);
480 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
481 if($firstsick == "poverty")
482 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
484 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
487 else if($poverty==2) /* two people have poverty */
489 DB_set_gametype_by_gameid($gameid,"dpoverty");
490 $gametype = "dpoverty";
491 $who = DB_get_sickness_by_gameid($gameid);
494 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
495 if($firstsick == "poverty")
497 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
498 if($secondsick == "poverty")
499 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
501 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
504 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
509 DB_set_gametype_by_gameid($gameid,"wedding");
510 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
511 $gametype = "wedding";
513 /* now the gametype is set correctly in the database */
514 echo "<p> Got it :)</p>";
516 /* loop over all players, set re/contra if possible and start the game if possible */
517 $userids = DB_get_all_userid_by_gameid($gameid);
518 foreach($userids as $userid)
520 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
525 /* are we the solo player? set us to re, else set us to contra */
526 $pos = DB_get_pos_by_hash($userhash);
527 if($pos == $startplayer)
528 DB_set_party_by_hash($userhash,"re");
530 DB_set_party_by_hash($userhash,"contra");
531 DB_set_hand_status_by_hash($userhash,'play');
535 /* set person with the wedding to re, do the rest during the game */
536 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
537 if($usersick == "wedding")
538 DB_set_party_by_hash($userhash,"re");
540 DB_set_party_by_hash($userhash,"contra");
542 DB_set_hand_status_by_hash($userhash,'play');
546 $hand = DB_get_all_hand($userhash);
548 if(in_array('3',$hand)||in_array('4',$hand))
549 DB_set_party_by_hash($userhash,"re");
551 DB_set_party_by_hash($userhash,"contra");
552 DB_set_hand_status_by_hash($userhash,'play');
556 /* set person with poverty to play status */
557 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
558 if($usersick == "poverty")
559 DB_set_hand_status_by_hash($userhash,'play');
561 /* set status of first player to be asked to poverty */
562 $who = DB_get_sickness_by_gameid($gameid);
563 if($who > 6) $who= $who/10; /* in case we have dpoverty */
564 $whoid = DB_get_userid('gameid-position',$gameid,$who);
566 DB_set_hand_status_by_hash($userhash,'poverty');
569 /* check for silent solo, set game type to solo in this case */
570 $gametype = DB_get_gametype_by_gameid($gameid);
571 $userids = DB_get_all_userid_by_gameid($gameid);
572 foreach($userids as $userid)
574 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
576 if($gametype=='normal')
578 $userhand = DB_get_all_hand($userhash);
579 if(check_wedding($userhand))
581 /* normal game type and player has both queens -> silent solo */
582 /* keep startplayer, just set gametype to silent solo */
583 DB_set_gametype_by_gameid($gameid,"solo");
584 DB_set_solo_by_gameid($gameid,'silent');
589 /* send out email to first player or poverty person*/
590 if($gametype!="poverty" && $gametype!="dpoverty")
592 $startplayer = DB_get_startplayer_by_gameid($gameid);
593 $email = DB_get_email('position-gameid',$startplayer,$gameid);
594 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
595 $who = DB_get_userid('email',$email);
596 DB_set_player_by_gameid($gameid,$who);
600 if(DB_get_email_pref_by_hash($hash)!="emailaddict")
602 /* email startplayer */
603 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
604 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
605 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
609 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
613 /* set status of first player to be asked to poverty */
614 $who = DB_get_sickness_by_gameid($gameid);
615 if($who > 6) $who= $who/10; /* in case we have dpoverty */
617 $whoid = DB_get_userid('gameid-position',$gameid,$who);
619 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
622 $email = DB_get_email('position-gameid',$who,$gameid);
623 $whohash = DB_get_hash_from_game_and_pos($gameid,$who);
624 DB_set_player_by_gameid($gameid,$whoid);
626 if(DB_get_email_pref_by_hash($hash)!="emailaddict")
628 /* email player for poverty */
629 $message = "Poverty: It's your turn now in game ".DB_format_gameid($gameid).".\n".
630 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$whohash."\n\n" ;
631 mymail($email,$EmailName."Poverty (game ".DB_format_gameid($gameid).") ",$message);
639 /* user only gets here in a poverty game, several things have to be handled here:
640 * A) ask, if user wants to take trump
642 * poverty: set re/contra
643 * dpoverty: first time: set re, send email to second player
644 * second time: set contra
645 * poverty: set status of other players to 'play'
646 * set status to play in case 0 trump
647 * no -> set status to play,
648 * ask next player or cancle the game if no more players
649 * B) user took trump and has too many cards (e.g. count(cards)>12 and re/contra set)
650 * ask to give cards back, set status to play, once player has 12 cards
652 * it is easier to check B) first
655 /* output pre game in case user reloads */
658 set_gametype($gametype); /* this sets the $CARDS variable */
659 $myparty = DB_get_party_by_hash($me);
661 /* the following is part B) of whats needs to be done)
662 /* check if user wants to give cards back */
663 if(myisset("exchange"))
665 $exchange = $_REQUEST['exchange'];
666 $partnerhash = DB_get_partner_hash_by_hash($me);
667 $partnerid = DB_get_userid('hash',$partnerhash);
668 $partnerhand = DB_get_handid('gameid-userid',$gameid,$partnerid);
670 /* if exchange is set to a value>0, exchange that card back to the partner */
673 $result = DB_query("UPDATE Hand_Card SET hand_id='$partnerhand'".
674 " WHERE hand_id='$myhand' AND card_id=".DB_quote_smart($exchange));
679 $mycards = DB_get_hand($me);
680 $mycards = mysort($mycards,$gametype);
682 /* check if user need to give more cards back */
683 if( ($myparty=='re' || $myparty=='contra') && count($mycards)>12)
685 echo "<div class=\"poverty\"> you need to get rid of a few cards</div>\n";
688 echo "<div class=\"mycards\">Your cards are: <br />\n";
689 foreach($mycards as $card)
690 display_link_card($card,$PREF["cardset"],$type);
691 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select card to give back\" />\n";
694 else if( ($myparty=='re' || $myparty=='contra') && count($mycards)==12)
696 /* user is done, ready to play */
697 DB_set_hand_status_by_hash($me,'play');
699 /* email start player */
700 $startplayer = DB_get_startplayer_by_gameid($gameid);
701 $email = DB_get_email('position-gameid',$startplayer,$gameid);
702 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
703 $who = DB_get_userid('email',$email);
704 DB_set_player_by_gameid($gameid,$who);
708 if(DB_get_email_pref_by_hash($hash)!="emailaddict")
710 /* email startplayer */
711 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
712 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
713 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
717 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
720 /* the following is part A) of what needs to be done */
721 if(!myisset("trump"))
725 echo "<div class=\"poverty\">\n";
726 $userids = DB_get_all_userid_by_gameid($gameid);
727 foreach($userids as $user)
729 $name = DB_get_name('userid',$user);
730 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
731 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
732 $userparty = DB_get_party_by_hash($userhash);
734 if($usersick=="poverty" && !$userparty)
736 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
737 $cards = DB_get_hand($hash);
740 foreach($cards as $card)
741 if($card<27) $nrtrump++;
742 echo "Player $name has $nrtrump trump. Do you want to take them?".
743 "<a href=\"index.php?action=game&me=$me&trump=$user\">yes</a> <br />\n";
746 echo "<a href=\"index.php?action=game&me=$me&trump=no\">No,way I take those trump...</a> <br />\n";
747 echo "</div><div>\n";
749 echo "<div class=\"mycards\">Your cards are: <br />\n";
750 foreach($mycards as $card)
751 display_card($card,$PREF["cardset"]);
752 echo "</div></div>\n";
758 $trump = $_REQUEST['trump'];
762 /* user doesn't want to take trump */
763 DB_set_hand_status_by_hash($me,'play');
765 /* set next player who needs to be asked and email him*/
766 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
767 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
769 /* don't ask people who have poverty */
771 if($firstsick=="poverty")
773 if($secondsick=="poverty")
778 if($gametype=="dpoverty")
780 $next=999; /* need to cancel for sure, since both would need to take the trump */
783 /* no more people to ask, need to cancel the game */
786 $message = "Hello, \n\n".
787 "Game ".DB_format_gameid($gameid)." has been canceled since nobody wanted to take the trump.\n";
789 $userids = DB_get_all_userid_by_gameid($gameid);
790 foreach($userids as $user)
792 $To = DB_get_email('userid',$user);
793 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled (poverty not resolved)",$message);
796 /* delete everything from the dB */
799 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been canceled.<br /><br /></p>";
806 /* email next player, set his status to poverty */
807 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
808 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
809 $userid = DB_get_userid('email',$To);
811 DB_set_player_by_gameid($gameid,$userid);
812 DB_set_hand_status_by_hash($userhash,'poverty');
814 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
815 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
816 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
821 /* player wants to take trump, change cards */
823 /* user wants to take trump */
824 $trump = $_REQUEST["trump"];
825 $userhand = DB_get_handid('gameid-userid',$gameid,$trump);
826 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
828 /* copy trump from player A to B */
829 $result = DB_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
832 $mycards = DB_get_hand($me);
835 if($gametype=='poverty')
837 $userids = DB_get_all_userid_by_gameid($gameid);
838 foreach($userids as $user)
840 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
841 if($hash==$userhash||$hash==$me)
843 DB_set_party_by_hash($hash,"re");
847 DB_set_party_by_hash($hash,"contra");
848 DB_set_hand_status_by_hash($hash,'play'); /* the contra party is ready to play */
851 /* check if we are done (in case of no trump handed over), if so, go to 'play' phase right away*/
852 if(count($mycards)==12)
854 DB_set_hand_status_by_hash($me,'play');
859 /* has the re party already been set?*/
861 $userids = DB_get_all_userid_by_gameid($gameid);
862 foreach($userids as $user)
864 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
865 $party = DB_get_party_by_hash($hash);
871 DB_set_party_by_hash($me,'contra');
872 DB_set_party_by_hash($userhash,'contra');
876 DB_set_party_by_hash($me,'re');
877 DB_set_party_by_hash($userhash,'re');
879 /* send out email to second non-poverty player */
880 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
881 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
884 if($firstsick=="poverty")
885 if($secondsick=="poverty")
891 echo "<div class=\"message\">Error in poverty, please contact the Admin</div>\n";
893 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
894 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
895 $userid = DB_get_userid('email',$To);
897 DB_set_player_by_gameid($gameid,$userid);
898 DB_set_hand_status_by_hash($userhash,'poverty');
900 $message = "Two people have poverty, it's your turn to decide, if you want to take the trump. Please visit:".
901 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
902 mymail($To,$EmailName." double poverty (game ".DB_format_gameid($gameid).")",$message);
907 echo "<div class=\"message\"> Please, <a href=\"$INDEX?action=game&me=$me\">continue</a> here.</div>\n";
915 /* both entries here, so that the tricks are visible for both.
916 * in case of 'play' there is a break later that skips the last part
919 /* check if all players are ready to play,
920 * if so, send out email to the startplayer
921 * only need to do this if the game hasn't started yet
923 $gamestatus = DB_get_game_status_by_gameid($gameid);
924 if($gamestatus == 'pre')
927 $userids = DB_get_all_userid_by_gameid($gameid);
928 foreach($userids as $user)
930 $userstatus = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
931 if($userstatus !='play' && $userstatus!='gameover')
934 DB_set_player_by_gameid($gameid,$user);
940 /* only set this after all poverty, etc. are handled*/
941 DB_set_game_status_by_gameid($gameid,'play');
943 /* email startplayer */
944 $startplayer = DB_get_startplayer_by_gameid($gameid);
945 $email = DB_get_email('position-gameid',$startplayer,$gameid);
946 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
947 $who = DB_get_userid('email',$email);
948 DB_set_player_by_gameid($gameid,$who);
950 if($hash!=$me && DB_get_email_pref_by_hash($hash)!="emailaddict")
952 /* email startplayer) */
953 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
954 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
955 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
959 /* figure out what kind of game we are playing,
960 * set the global variables $CARDS["trump"],$CARDS["diamonds"],$CARDS["hearts"],
961 * $CARDS["clubs"],$CARDS["spades"],$CARDS["foxes"]
965 $gametype = DB_get_gametype_by_gameid($gameid);
967 if($gametype=="solo")
969 $gametype = DB_get_solo_by_gameid($gameid);
970 if($gametype=='silent')
973 $GT = $gametype." ".$GT;
976 $gametype = "normal";
978 set_gametype($gametype); /* this sets the $CARDS variable */
980 /* get some infos about the game, need to reset this, since it might have changed */
981 $gamestatus = DB_get_game_status_by_gameid($gameid);
983 /* has the game started? No, then just wait here...*/
984 if($gamestatus == 'pre')
986 echo "<p class=\"message\"> You finished the setup, but not everyone else finished it... ".
987 "You need to wait for the others. Just wait for an email. </p>";
989 $mycards = DB_get_hand($me);
990 $mycards = mysort($mycards,$gametype);
992 echo "<div class=\"mycards\">Your cards are: <br />\n";
993 foreach($mycards as $card)
994 display_card($card,$PREF["cardset"]);
997 break; /* not sure this works... the idea is that you can
998 * only play a card after everyone is ready to play */
1001 /* get time from the last action of the game */
1002 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1003 $gameend = time() - strtotime($r[0]);
1005 /* handle comments in case player didn't play a card, allow comments a week after the end of the game */
1006 if( (!myisset("card") && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
1007 if(myisset("comment"))
1009 $comment = $_REQUEST["comment"];
1010 $playid = DB_get_current_playid($gameid);
1013 DB_insert_comment($comment,$playid,$myid);
1016 /* handle notes in case player didn't play a card, allow notes only during a game */
1017 if( (!myisset("card") && $mystatus=='play') )
1020 $note = $_REQUEST["note"];
1023 DB_insert_note($note,$gameid,$myid);
1026 /* get everything relevant to display the tricks */
1027 $result = DB_query("SELECT Hand_Card.card_id as card,".
1028 " Hand.position as position,".
1029 " Play.sequence as sequence, ".
1031 " GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
1032 " SEPARATOR '\n' ), ".
1033 " Play.create_date, ".
1036 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1037 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1038 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1039 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1040 "LEFT JOIN User On User.id=Comment.user_id ".
1041 "WHERE Trick.game_id='".$gameid."' ".
1042 "GROUP BY Trick.id, sequence ".
1043 "ORDER BY Trick.id, sequence ASC");
1045 $lasttrick = DB_get_max_trickid($gameid);
1047 $play = array(); /* needed to calculate winner later */
1049 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1050 $firstcard = ""; /* first card in a trick */
1052 echo "\n<ul class=\"tricks\">\n";
1053 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
1055 /* output vorbehalte */
1056 $mygametype = DB_get_gametype_by_gameid($gameid);
1057 if($mygametype != 'normal' && $mygametype != 'silent') /* only show when needed */
1059 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
1060 " <div class=\"trick\" id=\"trick0\">\n";
1062 for($mypos=1;$mypos<5;$mypos++)
1064 $usersick = DB_get_sickness_by_pos_and_gameid($mypos,$gameid);
1067 echo " <div class=\"vorbehalt".($mypos-1)."\"> Vorbehalt <br />";
1069 echo " $usersick <br />";
1072 if($mygametype == $usersick)
1076 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1080 while($r = DB_fetch_array($result))
1088 /* check if first schweinchen has been played */
1089 if( $GAME['schweinchen-who'] && ($r[0] == 19 || $r[0] == 20) )
1090 if(!$GAME['schweinchen-first'])
1091 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1093 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1095 /* save card to be able to find the winner of the trick later */
1096 $play[$seq] = array("card"=>$r[0],"pos"=>$pos);
1100 /* first card in a trick, output some html */
1101 if($trick!=$lasttrick)
1103 /* start of an old trick? */
1104 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1105 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1106 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1108 else if($trick==$lasttrick)
1110 /* start of a last trick? */
1111 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1112 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1113 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1116 /* remember first card, so that we are able to check, what cards can be played */
1121 echo " <div class=\"card".($pos-1)."\">\n";
1123 /* display comments */
1125 echo " <span class=\"comment\">".$comment."</span>\n";
1128 display_card($r[0],$PREF["cardset"]);
1130 echo " </div>\n"; /* end div card */
1136 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1140 /* whos turn is it? */
1143 $winner = get_winner($play,$gametype); /* returns the position */
1145 $firstcard = ""; /* new trick, no first card */
1150 if($next==5) $next = 1;
1153 /* my turn?, display cards as links, ask for comments*/
1154 if(DB_get_pos_by_hash($me) == $next)
1159 /* do we want to play a card? */
1160 if(myisset("card") && $myturn)
1162 $card = $_REQUEST["card"];
1163 $handid = DB_get_handid('hash',$me);
1164 $commentSchweinchen =""; /* used to add a comment when Schweinchen is being played */
1166 /* check if we have card and that we haven't played it yet*/
1167 /* set played in hand_card to true where hand_id and card_id*/
1168 $r = DB_query_array("SELECT id FROM Hand_Card WHERE played='false' and ".
1169 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1170 $handcardid = $r[0];
1172 if($handcardid) /* everything ok, play card */
1174 /* update Game timestamp */
1175 DB_update_game_timestamp($gameid);
1177 /* check if a call was made, must do this before we set the card status to played */
1178 if(myisset("call") && $_REQUEST["call"] == "120" && can_call(120,$me))
1179 $result = DB_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
1180 if(myisset("call") && $_REQUEST["call"] == "90" && can_call(90,$me))
1181 $result = DB_query("UPDATE Hand SET point_call='90' WHERE hash='$me' ");
1182 if(myisset("call") && $_REQUEST["call"] == "60" && can_call(60,$me))
1183 $result = DB_query("UPDATE Hand SET point_call='60' WHERE hash='$me' ");
1184 if(myisset("call") && $_REQUEST["call"] == "30" && can_call(30,$me))
1185 $result = DB_query("UPDATE Hand SET point_call='30' WHERE hash='$me' ");
1186 if(myisset("call") && $_REQUEST["call"] == "0" && can_call(0,$me))
1187 $result = DB_query("UPDATE Hand SET point_call='0' WHERE hash='$me' ");
1189 /* mark card as played */
1190 DB_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1191 DB_quote_smart($card));
1193 /* get trick id or start new trick */
1194 $a = DB_get_current_trickid($gameid);
1199 $playid = DB_play_card($trickid,$handcardid,$sequence);
1201 /* check special output for schweinchen in case in case a fox is being played
1202 * check for correct rules, etc. has already been done
1204 if( $GAME["schweinchen-who"] && ($card == 19 || $card == 20) )
1206 if(!$GAME['schweinchen-first'])
1207 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1209 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1211 if( ($GAME['schweinchen-second']==1 && $RULES['schweinchen']=='second') || $RULES['schweinchen']=='both')
1213 DB_insert_comment("Schweinchen! ",$playid,$myid);
1214 $commentSchweinchen = "Schweinchen! ";
1217 echo "schweinchen = ".$GAME["schweinchen-who"]." ---<br />";
1220 /* if sequence == 4 check who one in case of wedding */
1221 if($sequence == 4 && $GT == "wedding")
1223 /* is wedding resolve */
1224 $resolved = DB_get_sickness_by_gameid($gameid);
1227 /* who has wedding */
1228 $userids = DB_get_all_userid_by_gameid($gameid);
1229 foreach($userids as $user)
1231 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1232 if($usersick == "wedding")
1235 /* who won the trick */
1236 $play = DB_get_cards_by_trick($trickid);
1237 $winner = get_winner($play,$gametype); /* returns the position */
1238 $winnerid = DB_get_userid('gameid-position',$gameid,$winner);
1239 /* is tricknr <=3 */
1240 if($tricknr <=3 && $winnerid!=$whosick)
1242 /* set resolved at tricknr*/
1243 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1245 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1246 DB_set_party_by_hash($whash,"re");
1248 if($tricknr == 3 && $winnerid==$whosick)
1250 /* set resolved at tricknr*/
1251 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1256 /* if sequence == 4, set winner of the trick, count points and set the next player */
1259 $play = DB_get_cards_by_trick($trickid);
1260 $winner = get_winner($play,$gametype); /* returns the position */
1262 /* check if someone caught a fox */
1263 /* first check if we should account for solos at all,
1264 * since it doesn't make sense in some games
1266 $ok = 0; /* fox shouldn't be counted */
1267 if(DB_get_gametype_by_gameid($gameid)=="solo")
1269 $solo = DB_get_solo_by_gameid($gameid);
1270 if($solo == 'trump' || $solo == 'silent')
1271 $ok = 1; /* for trump solos and silent solos, foxes are ok */
1274 $ok = 1; /* for all other games (not solos) foxes are ok too */
1277 foreach($play as $played)
1279 if ( $played['card']==19 || $played['card']==20 )
1280 if ($played['pos']!= $winner )
1282 /* possible caught a fox, check party */
1283 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1284 $uid2 = DB_get_userid('gameid-position',$gameid,$played['pos']);
1286 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1287 $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1289 if($party1 != $party2)
1290 DB_query("INSERT INTO Score".
1291 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1295 /* check for karlchen (jack of clubs in the last trick)*/
1296 /* same as for foxes, karlchen doesn't always make sense
1297 * check what kind of game it is and set karlchen accordingly */
1298 $ok = 1; /* default: karlchen should be accounted for */
1300 $ok = 0; /* Karlchen works only in the last trick */
1301 if($ok && DB_get_gametype_by_gameid($gameid)=="solo" )
1303 $solo = DB_get_solo_by_gameid($gameid);
1304 if($solo == "trumpless" || $solo == "jack" || $solo == "queen" )
1305 $ok = 0; /* no Karlchen in these solos */
1309 foreach($play as $played)
1310 if ( $played['card']==11 || $played['card']==12 )
1311 if ($played['pos'] == $winner )
1313 /* possible caught a fox, check party */
1314 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1315 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1317 DB_query("INSERT INTO Score".
1318 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1320 /* check for doppelopf (>40 points)*/
1322 foreach($play as $played)
1324 $points += DB_get_card_value_by_cardid($played['card']);
1328 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1329 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1331 DB_query("INSERT INTO Score".
1332 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
1336 DB_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1338 echo "ERROR during scoring";
1341 echo "DEBUG: position $winner won the trick <br />";
1343 /* who is the next player? */
1348 $next = DB_get_pos_by_hash($me)+1;
1350 if($next==5) $next=1;
1352 /* check for coment */
1353 if(myisset("comment"))
1355 $comment = $_REQUEST["comment"];
1357 DB_insert_comment($comment,$playid,$myid);
1358 if($commentSchweinchen)
1359 $comment = $commentSchweinchen . $comment;
1362 /* check for note */
1365 $note = $_REQUEST["note"];
1367 DB_insert_note($note,$gameid,$myid);
1370 /* display played card */
1371 $pos = DB_get_pos_by_hash($me);
1374 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1375 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1376 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1379 echo " <div class=\"card".($pos-1)."\">\n ";
1381 /* display comments */
1382 display_card($card,$PREF["cardset"]);
1384 echo "\n <span class=\"comment\"> ".$comment."</span>\n";
1387 /*check if we still have cards left, else set status to gameover */
1388 if(sizeof(DB_get_hand($me))==0)
1390 DB_set_hand_status_by_hash($me,'gameover');
1391 $mystatus = 'gameover';
1394 /* if all players are done, set game status to game over,
1395 * get the points of the last trick and send out an email
1398 $userids = DB_get_all_userid_by_gameid($gameid);
1401 foreach($userids as $user)
1402 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1406 DB_set_game_status_by_gameid($gameid,"gameover");
1408 /* email next player, if game is still running */
1409 if(DB_get_game_status_by_gameid($gameid)=='play')
1411 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1412 $email = DB_get_email('hash',$next_hash);
1413 $who = DB_get_userid('email',$email);
1414 DB_set_player_by_gameid($gameid,$who);
1416 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1417 "It's your turn now.\n".
1418 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$next_hash."\n\n" ;
1419 if( DB_get_email_pref_by_uid($who)!="emailaddict" )
1420 mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
1422 else /* send out final email */
1424 /* individual score */
1425 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1426 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1427 " LEFT JOIN User ON User.id=Hand.user_id".
1428 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1429 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1430 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1431 " WHERE Hand.game_id='$gameid'".
1432 " GROUP BY User.fullname" );
1433 $message = "The game is over. Thanks for playing :)\n";
1434 $message .= "Final score:\n";
1435 while( $r = DB_fetch_array($result) )
1436 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1438 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1439 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1440 " LEFT JOIN User ON User.id=Hand.user_id".
1441 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1442 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1443 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1444 " WHERE Hand.game_id='$gameid'".
1445 " GROUP BY Hand.party" );
1446 $message .= "\nTotals:\n";
1449 while( $r = DB_fetch_array($result) )
1451 $message .= " ".$r[0]." ".$r[1]."\n";
1454 else if($r[0] == "contra")
1459 * save score in database
1463 /* get calls from re/contra */
1465 $call_contra = NULL;
1466 foreach($userids as $user)
1468 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1469 $call = DB_get_call_by_hash($hash);
1470 $party = DB_get_party_by_hash($hash);
1474 $call = (int) $call;
1480 else if( $call < $call_re)
1483 else if($party=="contra")
1485 if($call_contra==NULL)
1486 $call_contra = $call;
1487 else if( $call < $call_re)
1488 $call_contra = $call;
1493 /* figure out who one */
1494 $winning_party = NULL;
1496 if($call_re == NULL && $call_contra==NULL)
1498 /* nobody made a call, so it's easy to figure out who won */
1500 $winning_party="re";
1502 $winning_party="contra";
1506 /* if one party makes a call, they only win, iff they make enough points
1507 * if only one party made a call, the other one wins,
1508 * if the first one didn't make it
1512 $offset = 120 - $call_re;
1514 $offset--; /* since we use a > in the next equation */
1516 if($re > 120+$offset)
1517 $winning_party="re";
1518 else if ($call_contra == NULL )
1519 $winning_party="contra";
1524 $offset = 120 - $call_contra;
1525 if($call_contra == 0)
1526 $offset--; /* since we use a > in the next equation */
1528 if($contra > 120+$offset)
1529 $winning_party="contra";
1530 else if ($call_re == NULL )
1531 $winning_party="re";
1535 /* one point for each call of the other party in case the other party didn't win
1536 * and one point each in case the party made more than points than one of the calls
1538 if($winning_party!="contra" && $call_contra!=NULL)
1540 for( $p=$call_contra;$p<=120; $p+=30 )
1542 DB_query("INSERT INTO Score".
1543 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
1546 for( $p=$call_contra; $p<120; $p+=30)
1549 DB_query("INSERT INTO Score".
1550 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'made$p')");
1553 if($winning_party!="re" and $call_re!=NULL)
1555 for( $p=$call_re;$p<=120; $p+=30 )
1557 DB_query("INSERT INTO Score".
1558 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'against$p')");
1561 for( $p=$call_re; $p<120; $p+=30)
1564 DB_query("INSERT INTO Score".
1565 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
1569 /* point in case contra won */
1570 if($winning_party=="contra")
1572 DB_query("INSERT INTO Score".
1573 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1576 /* one point each for winning and each 30 points + calls */
1577 if($winning_party=="re")
1579 foreach(array(120,150,180,210,240) as $p)
1582 if($p==240 || $call_contra!=NULL)
1586 DB_query("INSERT INTO Score".
1587 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
1589 /* re called something and won */
1590 foreach(array(0,30,60,90,120) as $p)
1592 if($call_re!=NULL && $call_re<$p+1)
1593 DB_query("INSERT INTO Score".
1594 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
1597 else if( $winning_party=="contra")
1599 foreach(array(120,150,180,210,240) as $p)
1602 if($p==240 || $call_re!=NULL)
1605 if($contra>$p-$offset)
1606 DB_query("INSERT INTO Score".
1607 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
1609 /* re called something and won */
1610 foreach(array(0,30,60,90,120) as $p)
1612 if($call_contra!=NULL && $call_contra<$p+1)
1613 DB_query("INSERT INTO Score".
1614 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
1619 /* add score points to email */
1622 $message .= " Points Re: \n";
1623 $queryresult = DB_query("SELECT score FROM Score ".
1624 " WHERE game_id=$gameid AND party='re'".
1626 while($r = DB_fetch_array($queryresult) )
1628 $message .= " ".$r[0]."\n";
1631 $message .= " Points Contra: \n";
1632 $queryresult = DB_query("SELECT score FROM Score ".
1633 " WHERE game_id=$gameid AND party='contra'".
1635 while($r = DB_fetch_array($queryresult) )
1637 $message .= " ".$r[0]."\n";
1640 $message .= " Total Points (from the Re point of view): $Tpoint\n";
1643 $session = DB_get_session_by_gameid($gameid);
1644 $score = generate_score_table($session);
1645 /* convert html to ascii */
1646 $score = str_replace("<div class=\"scoretable\">\n<table class=\"score\">\n <tr>\n","",$score);
1647 $score = str_replace("</table></div>\n","",$score);
1648 $score = str_replace("\n","",$score);
1649 $score = str_replace(array("<tr>","</tr>","<td>","</td>"),array("","\n","","|"),$score);
1650 $score = explode("\n",$score);
1652 $header = array_slice($score,0,1);
1653 $header = explode("|",$header[0]);
1654 for($i=0;$i<sizeof($header);$i++)
1655 $header[$i]=str_pad($header[$i],6," ",STR_PAD_BOTH);
1656 $header = implode("|",$header);
1657 $header.= "\n------+------+------+------+------+\n";
1658 if(sizeof($score)>5) $header.= " ... \n";
1660 if(sizeof($score)>5) $score = array_slice($score,-5,5);
1661 for($i=0;$i<sizeof($score);$i++)
1663 $line = explode("|",$score[$i]);
1664 for($j=0;$j<sizeof($line);$j++)
1665 $line[$j]=str_pad($line[$j],6," ",STR_PAD_LEFT);
1666 $score[$i] = implode("|",$line);
1669 $score = implode("\n",$score);
1670 $score = $header.$score;
1672 $message .= "Score Table:\n";
1675 /* send out final email */
1678 foreach($userids as $user)
1679 $all[] = DB_get_email('userid',$user);
1680 $To = implode(",",$all);
1682 $help = "\n\n (you can use reply all on this email to reach all the players.)\n";
1683 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 1(2)",$message.$help);
1685 foreach($userids as $user)
1687 $To = DB_get_email('userid',$user);
1688 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1690 $link = "Use this link to have a look at game ".DB_format_gameid($gameid).": ".
1691 $HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
1692 if( DB_get_email_pref_by_uid($user) != "emailaddict" )
1693 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 2(2)",$link);
1699 echo "can't find that card?! <br />\n";
1702 else if(myisset("card") && !$myturn )
1704 echo "please wait until it's your turn! <br />\n";
1707 if($seq!=4 && $trickNR>1)
1708 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1710 /* display points in case game is over */
1711 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1713 echo " <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1714 " <div class=\"trick\" id=\"trick13\">\n";
1715 /* add pic for re/contra
1716 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1718 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1719 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1720 " LEFT JOIN User ON User.id=Hand.user_id".
1721 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1722 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1723 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1724 " WHERE Hand.game_id='$gameid'".
1725 " GROUP BY User.fullname" );
1726 while( $r = DB_fetch_array($result))
1727 echo " <div class=\"card".($r[3]-1)."\">\n".
1728 " <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
1731 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1735 echo "</ul>\n"; /* end ul tricks*/
1737 echo "<div class=\"notes\"> Personal notes: <br />\n";
1738 $notes = DB_get_notes_by_userid_and_gameid($myid,$gameid);
1739 foreach($notes as $note)
1740 echo "$note <hr \>\n";
1741 echo "Insert note:<input name=\"note\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1744 $mycards = DB_get_hand($me);
1745 $mycards = mysort($mycards,$gametype);
1746 echo "<div class=\"mycards\">\n";
1748 if($myturn && !myisset("card") && $mystatus=='play' )
1750 echo "Hello ".$myname.", it's your turn! <br />\n";
1751 echo "Your cards are: <br />\n";
1753 /* do we have to follow suite? */
1755 if(have_suit($mycards,$firstcard))
1758 foreach($mycards as $card)
1760 if( ($followsuit && !same_type($card,$firstcard)) ||
1761 ( (int)($card)==19 && ($RULES['schweinchen']=='second'||$RULES['schweinchen']=='secondaftercall')
1762 && $GAME['schweinchen-who']==$me && !$GAME['schweinchen-first'] )
1764 display_card($card,$PREF["cardset"]);
1766 display_link_card($card,$PREF["cardset"]);
1769 else if($mystatus=='play' )
1771 echo "Your cards are: <br />\n";
1772 foreach($mycards as $card)
1773 display_card($card,$PREF["cardset"]);
1775 else if($mystatus=='gameover')
1777 $oldcards = DB_get_all_hand($me);
1778 $oldcards = mysort($oldcards,$gametype);
1779 echo "Your cards were: <br />\n";
1780 foreach($oldcards as $card)
1781 display_card($card,$PREF["cardset"]);
1783 $userids = DB_get_all_userid_by_gameid($gameid);
1784 foreach($userids as $user)
1786 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1792 $name = DB_get_name('userid',$user);
1793 $oldcards = DB_get_all_hand($userhash);
1794 $oldcards = mysort($oldcards,$gametype);
1795 echo "$name's cards were: <br />\n";
1796 foreach($oldcards as $card)
1797 display_card($card,$PREF["cardset"]);
1803 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1804 if($mystatus=='play')
1807 /* the following happens only when the gamestatus is 'gameover' */
1808 /* check if game is over, display results */
1809 if(DB_get_game_status_by_gameid($gameid)=='play')
1811 echo "The game is over for you.. other people still need to play though";
1815 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1816 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1817 " LEFT JOIN User ON User.id=Hand.user_id".
1818 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1819 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1820 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1821 " WHERE Hand.game_id='$gameid'".
1822 " GROUP BY Hand.party" );
1823 echo "<div class=\"total\"> Totals:<br />\n";
1824 while( $r = DB_fetch_array($result))
1825 echo " ".$r[0]." ".$r[1]."<br />\n";
1827 $queryresult = DB_query("SELECT timediff(mod_date,create_date) ".
1828 " FROM Game WHERE id='$gameid'");
1829 $r = DB_fetch_array($queryresult);
1830 echo "<p>This game took ".$r[0]." hours.</p>";
1832 echo "<div class=\"re\">\n Points Re: <br />\n";
1833 $queryresult = DB_query("SELECT score FROM Score ".
1834 " WHERE game_id=$gameid AND party='re'".
1836 while($r = DB_fetch_array($queryresult) )
1837 echo " ".$r[0]."<br />\n";
1840 echo "<div class=\"contra\">\n Points Contra: <br />\n";
1841 $queryresult = DB_query("SELECT score FROM Score ".
1842 " WHERE game_id=$gameid AND party='contra'".
1844 while($r = DB_fetch_array($queryresult) )
1845 echo " ".$r[0]."<br />\n";
1854 myerror("error in testing the status");
1856 /* output left menu */
1857 display_user_menu();
1859 /* output right menu */
1861 /* display rule set for this game */
1862 echo "<div class=\"gameinfo\">\n";
1864 if($gamestatus != 'pre')
1865 echo " Gametype: $GT <br />\n";
1867 echo "Rules: <br />\n";
1868 echo "10ofhearts : ".$RULES["dullen"] ."<br />\n";
1869 echo "schweinchen: ".$RULES["schweinchen"] ."<br />\n";
1870 echo "call: ".$RULES["call"] ."<br />\n";
1873 if($gamestatus == 'play' )
1874 output_form_calls($me);
1876 /* get time from the last action of the game */
1877 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1878 $gameend = time() - strtotime($r[0]);
1880 if($gamestatus == 'play' || $gameend < 60*60*24*7)
1882 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1886 echo "<input type=\"submit\" value=\"submit\" />\n<hr />\n";
1888 $session = DB_get_session_by_gameid($gameid);
1889 $score = generate_score_table($session);
1897 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1900 $session = DB_get_session_by_gameid($gameid);
1901 $result = DB_query("SELECT id,create_date FROM Game".
1902 " WHERE session=$session".
1903 " ORDER BY create_date DESC".
1907 $r = DB_fetch_array($result);
1909 if(!$session || $gameid==$r[0])
1911 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
1912 $names = DB_get_all_names_by_gameid($gameid);
1913 $type = DB_get_gametype_by_gameid($gameid);
1917 $solo = DB_get_solo_by_gameid($gameid);
1919 if($solo!='silent') /* repeat game with same first player */
1920 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
1921 else /* rotate normally */
1922 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
1924 else /* rotate normally */
1925 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);