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 /* user might get here by clicking on the link in an email, so session might not be set */
30 if(isset($_SESSION["name"]))
31 output_status($_SESSION["name"]);
33 /* the user has done something, update the timestamp */
34 DB_update_user_timestamp($myid);
36 /* get some information from the DB */
37 $gameid = DB_get_gameid_by_hash($me);
38 $myname = DB_get_name('hash',$me);
39 $mystatus = DB_get_status_by_hash($me);
40 $mypos = DB_get_pos_by_hash($me);
41 $myhand = DB_get_handid('hash',$me);
42 $session = DB_get_session_by_gameid($gameid);
44 /* get prefs and save them in a variable*/
45 $PREF = DB_get_PREF($myid);
47 /* get rule set for this game */
48 $RULES = DB_get_RULES($gameid);
50 /* get some infos about the game */
51 $gametype = DB_get_gametype_by_gameid($gameid);
52 $gamestatus = DB_get_game_status_by_gameid($gameid);
56 $gametype = DB_get_solo_by_gameid($gameid);
57 $GT = $gametype." ".$GT;
60 /* do we need to worry about Schweinchen?
61 * check gametype and rules
62 * if yes, figure out if someone actually has Schweinchen
63 * save information in $GAME
66 if( $gametype == 'normal' || $gametype == 'silent' || $gametype=='trump' )
67 if( in_array($RULES['schweinchen'],array('both','second','secondaftercall')) )
72 /* need to check for Schweinchen */
75 $hash = DB_get_hash_from_game_and_pos($gameid,$i);
76 $cards = DB_get_all_hand($hash);
77 if( in_array("19",$cards) && in_array("20",$cards) )
78 $GAME['schweinchen-who']=$hash;
80 $GAME['schweinchen-first'] = 0; /* to keep track if they have been played already */
81 $GAME['schweinchen-second'] = 0;
85 /* no need to check for Schweinchen */
86 $GAME['schweinchen-who']=NULL;
88 /* end check for Schweinchen */
90 /* put everyting in a form */
91 echo "<form action=\"index.php?action=game&me=$me\" method=\"post\">\n";
95 /* output extra division in case this game is part of a session */
98 echo "<div class=\"session\">\n".
99 "This game is part of session $session: \n";
100 $hashes = DB_get_hashes_by_session($session,$myid);
102 foreach($hashes as $hash)
107 echo "<a href=\"".$INDEX."?action=game&me=".$hash."\">$i</a> \n";
113 /* display the table and the names */
116 /* mystatus gets the player through the different stages of a game.
117 * start: does the player want to play?
118 * init: check for sickness
119 * check: check for return values from init
120 * poverty: handle poverty, wait here until all player have reached this state
121 * display sickness and move on to game
122 * play: game in progress
123 * gameover: are we revisiting a game
130 /* asks the player, if he wants to join the game */
131 output_check_want_to_play($me);
136 /* check the result, if player wants to join, got next stage, else cancel game */
137 if($_REQUEST["in"] == "no")
139 /* cancel the game */
140 $message = "Hello, \n\n".
141 "the game has been canceled due to the request of one of the players.\n";
143 $userids = DB_get_all_userid_by_gameid($gameid);
144 foreach($userids as $user)
146 $To = DB_get_email('userid',$user);
147 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
150 /* delete everything from the dB */
156 /* user wants to join the game */
158 /* move on to the next stage,
159 * no break statement to immediately go to the next stage
162 DB_set_hand_status_by_hash($me,'init');
164 /* check if everyone has reached this stage, send out email */
165 $userids = DB_get_all_userid_by_gameid($gameid);
167 foreach($userids as $user)
169 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
170 if($userstat!='init')
172 /* whos turn is it? */
173 DB_set_player_by_gameid($gameid,$user);
179 /* all done, send out email unless this player is the startplayer */
180 $startplayer = DB_get_startplayer_by_gameid($gameid);
181 if($mypos == $startplayer)
183 /* do nothing, go to next stage */
187 /* email startplayer */
189 $email = DB_get_email('position-gameid',$startplayer,$gameid);
190 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
191 $who = DB_get_userid('email',$email);
192 DB_set_player_by_gameid($gameid,$who);
194 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
195 "Use this link to go the game: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
196 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
203 /* here we ask the player if he is sick */
204 $mycards = DB_get_hand($me);
207 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
208 echo "\n<ul class=\"tricks\">\n";
209 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
210 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
211 " <div class=\"trick\" id=\"trick0\">\n";
213 for($pos=1;$pos<5;$pos++)
215 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
216 $userid = DB_get_userid('gameid-position',$gameid,$pos);
217 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
219 if($userstatus=='start' || $userstatus=='init')
220 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs to decide </div>\n"; /* show this to everyone */
222 if($usersick!=NULL && $pos<=$mypos ) /* only show this for people sitting before the player */
223 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
224 else if($usersick==NULL && $pos<=$mypos)
225 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
227 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
228 /* end displaying sickness */
230 if(!myisset("solo","wedding","poverty","nines") )
232 output_check_for_sickness($me,$mycards);
234 echo "<div class=\"mycards\">Your cards are: <br />\n";
235 foreach($mycards as $card)
236 display_card($card,$PREF["cardset"]);
243 /* check if someone selected more than one sickness */
245 if($_REQUEST["solo"]!="No") $Nsickness++;
246 if($_REQUEST["wedding"] == "yes") $Nsickness++;
247 if($_REQUEST["poverty"] == "yes") $Nsickness++;
248 if($_REQUEST["nines"] == "yes") $Nsickness++;
252 echo "<p class=\"message\"> You selected more than one sickness, please go back ".
253 "and answer the <a href=\"$INDEX?action=game&me=$me&in=yes\">question</a> again.</p>";
255 echo "<div class=\"mycards\">Your cards are: <br />\n";
256 foreach($mycards as $card)
257 display_card($card,$PREF["cardset"]);
264 /* everything is ok, save what user said and proceed */
265 echo "<p class=\"message\">Processing what you selected in the last step...";
267 /* check if this sickness needs to be handled first */
268 $gametype = DB_get_gametype_by_gameid($gameid);
269 $startplayer = DB_get_startplayer_by_gameid($gameid); /* need this to check which solo goes first */
271 if( $_REQUEST["solo"]!="No" )
273 /* user wants to play a solo */
275 /* store the info in the user's hand info */
276 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
277 DB_set_sickness_by_hash($me,"solo");
279 echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
281 if($gametype == "solo" && $startplayer<$mypos)
282 {}/* do nothing, since someone else already is playing solo */
285 /* this solo comes first
286 * store info in game table
288 DB_set_gametype_by_gameid($gameid,"solo");
289 DB_set_startplayer_by_gameid($gameid,$mypos);
290 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
293 else if($_REQUEST["wedding"] == "yes")
295 /* TODO: add silent solo somewhere*/
296 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
297 DB_set_sickness_by_hash($me,"wedding");
299 else if($_REQUEST["poverty"] == "yes")
301 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
302 DB_set_sickness_by_hash($me,"poverty");
304 else if($_REQUEST["nines"] == "yes")
306 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
307 " is playing solo, this game will be canceled.<br />\n";
308 DB_set_sickness_by_hash($me,"nines");
313 /* move on to the next stage*/
314 DB_set_hand_status_by_hash($me,'check');
319 /* here we check what all players said and figure out what game we are playing
320 * this can therefore only be handled once all players finished the last stage
323 /* only need to redisplay the cards when the user reloads the page and lands directly here */
324 if($mystatus=='check')
326 $mycards = DB_get_hand($me);
329 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
330 echo "\n<ul class=\"tricks\">\n";
331 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
332 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
333 " <div class=\"trick\" id=\"trick0\">\n";
335 for($pos=1;$pos<5;$pos++)
337 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
338 $userid = DB_get_userid('gameid-position',$gameid,$pos);
339 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
341 if($userstatus=='start' || $userstatus=='init')
342 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs to decide </div>\n"; /* show this to everyone */
344 if($usersick!=NULL) /* in the init-phase we only showed players with $pos<$mypos, now we can show all */
345 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
347 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
349 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
350 /* end displaying sickness */
353 echo "<div class=\"message\">\n";
354 echo "<p> Checking if someone else selected solo, nines, wedding or poverty.</p>";
356 /* check if everyone has reached this stage */
357 $userids = DB_get_all_userid_by_gameid($gameid);
359 foreach($userids as $user)
361 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
362 if($userstat!='check')
365 DB_set_player_by_gameid($gameid,$user);
371 echo "<p>This step can only be handled after everyone finished the last step. ".
372 "Seems like this is not the case, so you need to wait a bit... ".
373 "you will get an email once that is the case, please use the link in ".
374 "that email to continue the game.</p></div>";
376 /* display cards, if player was just at the init-phase he will still see the cards from there
377 * we can put this one here, since the last player to finish the init state won't get here and
378 * will still see his card anyway from the init-phase
380 if($mystatus=='check')
383 echo "<div class=\"mycards\">Your cards are: <br />\n";
384 foreach($mycards as $card)
385 display_card($card,$PREF["cardset"]);
392 /* Ok, everyone finished the init-phase, time to figure out what game we
393 * are playing, in case there are any solos this already
394 * will have the correct information in it */
396 echo "<p> Ok, everyone is done... figuring out what kind of game we are playing.</p>";
398 $gametype = DB_get_gametype_by_gameid($gameid);
399 $startplayer = DB_get_startplayer_by_gameid($gameid);
401 /* check for sickness */
406 foreach($userids as $user)
408 $name = DB_get_name('userid',$user);
409 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
410 if($usersick == 'nines')
413 break; /* no need to check for other poverties, since only solo can win and that is already set */
415 else if($usersick == 'poverty')
417 else if($usersick == 'wedding')
419 else if($usersick == 'solo')
423 /* now check which sickness comes first and set the gametype to it */
424 if($gametype == "solo")
431 /* TODO: should we keep statistics of this? */
432 $message = "Hello, \n\n".
433 " the game has been canceled because ".DB_get_name('userid',$nines).
434 " has five or more nines and nobody is playing solo.\n\n".
435 " To redeal either start a new game or, in case the game was part of a tournament, \n".
436 " go to the last game and use the link at the bottom of the page to redeal.";
438 $userids = DB_get_all_userid_by_gameid($gameid);
439 foreach($userids as $user)
441 $To = DB_get_email('userid',$user);
442 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
445 /* delete everything from the dB */
448 echo "The game has been canceled because ".DB_get_name('userid',$nines).
449 " has five or more nines and nobody is playing solo.\n";
454 else if($poverty==1) /* one person has poverty */
456 DB_set_gametype_by_gameid($gameid,"poverty");
457 $gametype = "poverty";
458 $who = DB_get_sickness_by_gameid($gameid);
461 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
462 if($firstsick == "poverty")
463 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
465 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
468 else if($poverty==2) /* two people have poverty */
470 DB_set_gametype_by_gameid($gameid,"dpoverty");
471 $gametype = "dpoverty";
472 $who = DB_get_sickness_by_gameid($gameid);
475 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
476 if($firstsick == "poverty")
478 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
479 if($secondsick == "poverty")
480 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
482 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
485 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
490 DB_set_gametype_by_gameid($gameid,"wedding");
491 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
492 $gametype = "wedding";
494 /* now the gametype is set correctly in the database */
495 echo "<p> Got it :)</p>";
497 /* loop over all players, set re/contra if possible and start the game if possible */
498 $userids = DB_get_all_userid_by_gameid($gameid);
499 foreach($userids as $userid)
501 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
506 /* are we the solo player? set us to re, else set us to contra */
507 $pos = DB_get_pos_by_hash($userhash);
508 if($pos == $startplayer)
509 DB_set_party_by_hash($userhash,"re");
511 DB_set_party_by_hash($userhash,"contra");
512 DB_set_hand_status_by_hash($userhash,'play');
516 /* set person with the wedding to re, do the rest during the game */
517 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
518 if($usersick == "wedding")
519 DB_set_party_by_hash($userhash,"re");
521 DB_set_party_by_hash($userhash,"contra");
523 DB_set_hand_status_by_hash($userhash,'play');
527 $hand = DB_get_all_hand($userhash);
529 if(in_array('3',$hand)||in_array('4',$hand))
530 DB_set_party_by_hash($userhash,"re");
532 DB_set_party_by_hash($userhash,"contra");
533 DB_set_hand_status_by_hash($userhash,'play');
537 /* set person with poverty to play status */
538 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
539 if($usersick == "poverty")
540 DB_set_hand_status_by_hash($userhash,'play');
542 /* set status of first player to be asked to poverty */
543 $who = DB_get_sickness_by_gameid($gameid);
544 if($who > 6) $who= $who/10; /* in case we have dpoverty */
545 $whoid = DB_get_userid('gameid-position',$gameid,$who);
547 DB_set_hand_status_by_hash($userhash,'poverty');
550 /* check for silent solo, set game type to solo in this case */
551 $gametype = DB_get_gametype_by_gameid($gameid);
552 $userids = DB_get_all_userid_by_gameid($gameid);
553 foreach($userids as $userid)
555 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
557 if($gametype=='normal')
559 $userhand = DB_get_all_hand($userhash);
560 if(check_wedding($userhand))
562 /* normal game type and player has both queens -> silent solo */
563 /* keep startplayer, just set gametype to silent solo */
564 DB_set_gametype_by_gameid($gameid,"solo");
565 DB_set_solo_by_gameid($gameid,'silent');
570 /* send out email to first player or poverty person*/
571 if($gametype!="poverty" && $gametype!="dpoverty")
573 $startplayer = DB_get_startplayer_by_gameid($gameid);
574 $email = DB_get_email('position-gameid',$startplayer,$gameid);
575 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
576 $who = DB_get_userid('email',$email);
577 DB_set_player_by_gameid($gameid,$who);
581 if(DB_get_email_pref_by_hash($hash)!="emailaddict")
583 /* email startplayer */
584 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
585 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
586 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
590 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
594 /* set status of first player to be asked to poverty */
595 $who = DB_get_sickness_by_gameid($gameid);
596 if($who > 6) $who= $who/10; /* in case we have dpoverty */
598 $whoid = DB_get_userid('gameid-position',$gameid,$who);
600 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
603 $email = DB_get_email('position-gameid',$who,$gameid);
604 $whohash = DB_get_hash_from_game_and_pos($gameid,$who);
605 DB_set_player_by_gameid($gameid,$whoid);
607 if(DB_get_email_pref_by_hash($hash)!="emailaddict")
609 /* email player for poverty */
610 $message = "Poverty: It's your turn now in game ".DB_format_gameid($gameid).".\n".
611 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$whohash."\n\n" ;
612 mymail($email,$EmailName."Poverty (game ".DB_format_gameid($gameid).") ",$message);
620 /* user only gets here in a poverty game, several things have to be handled here:
621 * A) ask, if user wants to take trump
623 * poverty: set re/contra
624 * dpoverty: first time: set re, send email to second player
625 * second time: set contra
626 * set status to play in case 0 trump
627 * no -> set status to play,
628 * ask next player or cancle the game if no more players
629 * B) user took trump and has too many cards (e.g. count(cards)>12 and re/contra set)
630 * ask to give cards back, set status to play, once player has 12 cards
632 * it is easier to check B) first
635 /* output pre game in case user reloads */
638 set_gametype($gametype); /* this sets the $CARDS variable */
639 $myparty = DB_get_party_by_hash($me);
641 /* the following is part B) of whats needs to be done)
642 /* check if user wants to give cards back */
643 if(myisset("exchange"))
645 $exchange = $_REQUEST['exchange'];
646 $partnerhash = DB_get_partner_hash_by_hash($me);
647 $partnerid = DB_get_userid('hash',$partnerhash);
648 $partnerhand = DB_get_handid('gameid-userid',$gameid,$partnerid);
650 /* if exchange is set to a value>0, exchange that card back to the partner */
653 $result = DB_query("UPDATE Hand_Card SET hand_id='$partnerhand'".
654 " WHERE hand_id='$myhand' AND card_id=".DB_quote_smart($exchange));
658 $mycards = DB_get_hand($me);
659 $mycards = mysort($mycards,$gametype);
661 /* check if user need to give more cards back */
662 if( ($myparty=='re' || $myparty=='contra') && count($mycards)>12)
664 echo "<div class=\"poverty\"> you need to get rid of a few cards</div>\n";
667 echo "<div class=\"mycards\">Your cards are: <br />\n";
668 foreach($mycards as $card)
669 display_link_card($card,$PREF["cardset"],$type);
670 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select card to give back\" />\n";
673 else if( ($myparty=='re' || $myparty=='contra') && count($mycards)==12)
675 /* user is done, ready to play */
676 DB_set_hand_status_by_hash($me,'play');
677 /* TODO if resolved; email start player, set startplayer */
680 /* the following is part A) of what needs to be done */
681 if(!myisset("trump"))
685 echo "<div class=\"poverty\">\n";
686 $userids = DB_get_all_userid_by_gameid($gameid);
687 foreach($userids as $user)
689 $name = DB_get_name('userid',$user);
690 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
691 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
692 $userparty = DB_get_party_by_hash($userhash);
694 if($usersick=="poverty" && !$userparty)
696 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
697 $cards = DB_get_hand($hash);
700 foreach($cards as $card)
701 if($card<27) $nrtrump++;
702 echo "Player $name has $nrtrump trump. Do you want to take them?".
703 "<a href=\"index.php?action=game&me=$me&trump=$user\">yes</a> <br />\n";
706 echo "<a href=\"index.php?action=game&me=$me&trump=no\">No,way I take those trump...</a> <br />\n";
707 echo "</div><div>\n";
709 echo "<div class=\"mycards\">Your cards are: <br />\n";
710 foreach($mycards as $card)
711 display_card($card,$PREF["cardset"]);
712 echo "</div></div>\n";
718 $trump = $_REQUEST['trump'];
722 /* user doesn't want to take trump */
723 DB_set_hand_status_by_hash($me,'play');
725 /* set next player who needs to be asked and email him*/
726 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
727 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
729 /* don't ask people who have poverty */
731 if($firstsick=="poverty")
733 if($secondsick=="poverty")
738 if($gametype=="dpoverty")
740 $next=999; /* need to cancel for sure, since both would need to take the trump */
743 /* no more people to ask, need to cancel the game */
746 $message = "Hello, \n\n".
747 "Game ".DB_format_gameid($gameid)." has been canceled since nobody wanted to take the trump.\n";
749 $userids = DB_get_all_userid_by_gameid($gameid);
750 foreach($userids as $user)
752 $To = DB_get_email('userid',$user);
753 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled (poverty not resolved)",$message);
756 /* delete everything from the dB */
759 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been canceled.<br /><br /></p>";
766 /* email next player, set his status to poverty */
767 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
768 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
769 $userid = DB_get_userid('email',$To);
771 DB_set_player_by_gameid($gameid,$userid);
772 DB_set_hand_status_by_hash($userhash,'poverty');
774 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
775 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
776 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
781 /* player wants to take trump, change cards */
783 /* user wants to take trump */
784 $trump = $_REQUEST["trump"];
785 $userhand = DB_get_handid('gameid-userid',$gameid,$trump);
786 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
788 /* copy trump from player A to B */
789 $result = DB_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
791 $mycards = DB_get_hand($me);
794 if($gametype=='poverty')
796 $userids = DB_get_all_userid_by_gameid($gameid);
797 foreach($userids as $user)
799 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
800 if($hash==$userhash||$hash==$me)
801 DB_set_party_by_hash($hash,"re");
803 DB_set_party_by_hash($hash,"contra");
805 /* check if we are done, if so, send everyone into the 'play' phase */
806 if(count($mycards)==12)
808 foreach($userids as $user)
810 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
811 DB_set_hand_status_by_hash($hash,'play');
817 /* has the re party already been set?*/
819 $userids = DB_get_all_userid_by_gameid($gameid);
820 foreach($userids as $user)
822 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
823 $party = DB_get_party_by_hash($hash);
829 DB_set_party_by_hash($me,'contra');
830 DB_set_party_by_hash($userhash,'contra');
834 DB_set_party_by_hash($me,'re');
835 DB_set_party_by_hash($userhash,'re');
837 /* send out email to second non-poverty player */
838 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
839 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
842 if($firstsick=="poverty")
843 if($secondsick=="poverty")
849 echo "<div class=\"message\">Error in poverty, please contact the Admin</div>\n";
851 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
852 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
853 $userid = DB_get_userid('email',$To);
855 DB_set_player_by_gameid($gameid,$userid);
856 DB_set_hand_status_by_hash($userhash,'poverty');
858 $message = "Two people have poverty, it's your turn to decide, if you want to take the trump. Please visit:".
859 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
860 mymail($To,$EmailName." double poverty (game ".DB_format_gameid($gameid).")",$message);
865 echo "<div class=\"message\"> Please, <a href=\"$INDEX?action=game&me=$me\">continue</a> here.</div>\n";
873 /* both entries here, so that the tricks are visible for both.
874 * in case of 'play' there is a break later that skips the last part
877 /* check if all players are ready to play,
878 * if so, send out email to the startplayer
879 * only need to do this if the game hasn't started yet
881 $gamestatus = DB_get_game_status_by_gameid($gameid);
882 if($gamestatus == 'pre')
885 $userids = DB_get_all_userid_by_gameid($gameid);
886 foreach($userids as $user)
888 $userstatus = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
889 if($userstatus !='play' && $userstatus!='gameover')
892 DB_set_player_by_gameid($gameid,$user);
897 /* only set this after all poverty, etc. are handled*/
898 DB_set_game_status_by_gameid($gameid,'play');
900 /* email startplayer */
901 $startplayer = DB_get_startplayer_by_gameid($gameid);
902 $email = DB_get_email('position-gameid',$startplayer,$gameid);
903 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
904 $who = DB_get_userid('email',$email);
905 DB_set_player_by_gameid($gameid,$who);
907 if($hash!=$me && DB_get_email_pref_by_hash($hash)!="emailaddict")
909 /* email startplayer) */
910 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
911 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
912 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
916 /* figure out what kind of game we are playing,
917 * set the global variables $CARDS["trump"],$CARDS["diamonds"],$CARDS["hearts"],
918 * $CARDS["clubs"],$CARDS["spades"],$CARDS["foxes"]
922 $gametype = DB_get_gametype_by_gameid($gameid);
924 if($gametype=="solo")
926 $gametype = DB_get_solo_by_gameid($gameid);
927 if($gametype=='silent')
930 $GT = $gametype." ".$GT;
933 $gametype = "normal";
935 set_gametype($gametype); /* this sets the $CARDS variable */
937 /* get some infos about the game, need to reset this, since it might have changed */
938 $gamestatus = DB_get_game_status_by_gameid($gameid);
940 /* has the game started? No, then just wait here...*/
941 if($gamestatus == 'pre')
943 echo "<p class=\"message\"> You finished the setup, but not everyone else finished it... ".
944 "You need to wait for the others. Just wait for an email. </p>";
946 $mycards = DB_get_hand($me);
947 $mycards = mysort($mycards,$gametype);
949 echo "<div class=\"mycards\">Your cards are: <br />\n";
950 foreach($mycards as $card)
951 display_card($card,$PREF["cardset"]);
954 break; /* not sure this works... the idea is that you can
955 * only play a card after everyone is ready to play */
958 /* get time from the last action of the game */
959 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
960 $gameend = time() - strtotime($r[0]);
962 /* handle comments in case player didn't play a card, allow comments a week after the end of the game */
963 if( (!myisset("card") && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
964 if(myisset("comment"))
966 $comment = $_REQUEST["comment"];
967 $playid = DB_get_current_playid($gameid);
970 DB_insert_comment($comment,$playid,$myid);
973 /* handle notes in case player didn't play a card, allow notes only during a game */
974 if( (!myisset("card") && $mystatus=='play') )
977 $note = $_REQUEST["note"];
980 DB_insert_note($note,$gameid,$myid);
983 /* get everything relevant to display the tricks */
984 $result = DB_query("SELECT Hand_Card.card_id as card,".
985 " Hand.position as position,".
986 " Play.sequence as sequence, ".
988 " GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
989 " SEPARATOR '\n' ), ".
990 " Play.create_date, ".
993 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
994 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
995 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
996 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
997 "LEFT JOIN User On User.id=Comment.user_id ".
998 "WHERE Trick.game_id='".$gameid."' ".
999 "GROUP BY Trick.id, sequence ".
1000 "ORDER BY Trick.id, sequence ASC");
1002 $lasttrick = DB_get_max_trickid($gameid);
1004 $play = array(); /* needed to calculate winner later */
1006 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1007 $firstcard = ""; /* first card in a trick */
1009 echo "\n<ul class=\"tricks\">\n";
1010 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
1012 /* output vorbehalte */
1013 $mygametype = DB_get_gametype_by_gameid($gameid);
1014 if($mygametype != 'normal' && $mygametype != 'silent') /* only show when needed */
1016 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
1017 " <div class=\"trick\" id=\"trick0\">\n";
1019 for($mypos=1;$mypos<5;$mypos++)
1021 $usersick = DB_get_sickness_by_pos_and_gameid($mypos,$gameid);
1024 echo " <div class=\"vorbehalt".($mypos-1)."\"> Vorbehalt <br />";
1026 echo " $usersick <br />";
1029 if($mygametype == $usersick)
1033 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1037 while($r = DB_fetch_array($result))
1045 /* check if first schweinchen has been played */
1046 if( $GAME['schweinchen-who'] && ($r[0] == 19 || $r[0] == 20) )
1047 if(!$GAME['schweinchen-first'])
1048 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1050 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1052 /* save card to be able to find the winner of the trick later */
1053 $play[$seq] = array("card"=>$r[0],"pos"=>$pos);
1057 /* first card in a trick, output some html */
1058 if($trick!=$lasttrick)
1060 /* start of an old trick? */
1061 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1062 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1063 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1065 else if($trick==$lasttrick)
1067 /* start of a last trick? */
1068 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1069 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1070 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1073 /* remember first card, so that we are able to check, what cards can be played */
1078 echo " <div class=\"card".($pos-1)."\">\n";
1080 /* display comments */
1082 echo " <span class=\"comment\">".$comment."</span>\n";
1085 display_card($r[0],$PREF["cardset"]);
1087 echo " </div>\n"; /* end div card */
1093 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1097 /* whos turn is it? */
1100 $winner = get_winner($play,$gametype); /* returns the position */
1102 $firstcard = ""; /* new trick, no first card */
1107 if($next==5) $next = 1;
1110 /* my turn?, display cards as links, ask for comments*/
1111 if(DB_get_pos_by_hash($me) == $next)
1116 /* do we want to play a card? */
1117 if(myisset("card") && $myturn)
1119 $card = $_REQUEST["card"];
1120 $handid = DB_get_handid('hash',$me);
1122 /* check if we have card and that we haven't played it yet*/
1123 /* set played in hand_card to true where hand_id and card_id*/
1124 $r = DB_query_array("SELECT id FROM Hand_Card WHERE played='false' and ".
1125 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1126 $handcardid = $r[0];
1128 if($handcardid) /* everything ok, play card */
1130 /* update Game timestamp */
1131 DB_update_game_timestamp($gameid);
1133 /* check if a call was made, must do this before we set the card status to played */
1134 if(myisset("call") && $_REQUEST["call"] == "120" && can_call(120,$me))
1135 $result = DB_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
1136 if(myisset("call") && $_REQUEST["call"] == "90" && can_call(90,$me))
1137 $result = DB_query("UPDATE Hand SET point_call='90' WHERE hash='$me' ");
1138 if(myisset("call") && $_REQUEST["call"] == "60" && can_call(60,$me))
1139 $result = DB_query("UPDATE Hand SET point_call='60' WHERE hash='$me' ");
1140 if(myisset("call") && $_REQUEST["call"] == "30" && can_call(30,$me))
1141 $result = DB_query("UPDATE Hand SET point_call='30' WHERE hash='$me' ");
1142 if(myisset("call") && $_REQUEST["call"] == "0" && can_call(0,$me))
1143 $result = DB_query("UPDATE Hand SET point_call='0' WHERE hash='$me' ");
1145 /* mark card as played */
1146 DB_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1147 DB_quote_smart($card));
1149 /* get trick id or start new trick */
1150 $a = DB_get_current_trickid($gameid);
1155 $playid = DB_play_card($trickid,$handcardid,$sequence);
1157 /* check special output for schweinchen in case in case a fox is being played
1158 * check for correct rules, etc. has already been done
1160 if( $GAME["schweinchen-who"] && ($card == 19 || $card == 20) )
1162 if(!$GAME['schweinchen-first'])
1163 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1165 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1167 if( ($GAME['schweinchen-second']==1 && $RULES['schweinchen']=='second') || $RULES['schweinchen']=='both')
1169 DB_insert_comment("Schweinchen! ",$playid,$myid);
1170 $commentSchweinchen = "Schweinchen! ";
1173 echo "schweinchen = ".$GAME["schweinchen-who"]." ---<br />";
1176 /* if sequence == 4 check who one in case of wedding */
1177 if($sequence == 4 && $GT == "wedding")
1179 /* is wedding resolve */
1180 $resolved = DB_get_sickness_by_gameid($gameid);
1183 /* who has wedding */
1184 $userids = DB_get_all_userid_by_gameid($gameid);
1185 foreach($userids as $user)
1187 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1188 if($usersick == "wedding")
1191 /* who won the trick */
1192 $play = DB_get_cards_by_trick($trickid);
1193 $winner = get_winner($play,$gametype); /* returns the position */
1194 $winnerid = DB_get_userid('gameid-position',$gameid,$winner);
1195 /* is tricknr <=3 */
1196 if($tricknr <=3 && $winnerid!=$whosick)
1198 /* set resolved at tricknr*/
1199 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1201 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1202 DB_set_party_by_hash($whash,"re");
1204 if($tricknr == 3 && $winnerid==$whosick)
1206 /* set resolved at tricknr*/
1207 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1212 /* if sequence == 4, set winner of the trick, count points and set the next player */
1215 $play = DB_get_cards_by_trick($trickid);
1216 $winner = get_winner($play,$gametype); /* returns the position */
1218 /* check if someone caught a fox */
1219 /* first check if we should account for solos at all,
1220 * since it doesn't make sense in some games
1222 $ok = 0; /* fox shouldn't be counted */
1223 if(DB_get_gametype_by_gameid($gameid)=="solo")
1225 $solo = DB_get_solo_by_gameid($gameid);
1226 if($solo == 'trump' || $solo == 'silent')
1227 $ok = 1; /* for trump solos and silent solos, foxes are ok */
1230 $ok = 1; /* for all other games (not solos) foxes are ok too */
1233 foreach($play as $played)
1235 if ( $played['card']==19 || $played['card']==20 )
1236 if ($played['pos']!= $winner )
1238 /* possible caught a fox, check party */
1239 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1240 $uid2 = DB_get_userid('gameid-position',$gameid,$played['pos']);
1242 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1243 $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1245 if($party1 != $party2)
1246 DB_query("INSERT INTO Score".
1247 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1251 /* check for karlchen (jack of clubs in the last trick)*/
1252 /* same as for foxes, karlchen doesn't always make sense
1253 * check what kind of game it is and set karlchen accordingly */
1254 $ok = 1; /* default: karlchen should be accounted for */
1256 $ok = 0; /* Karlchen works only in the last trick */
1257 if($ok && DB_get_gametype_by_gameid($gameid)=="solo" )
1259 $solo = DB_get_solo_by_gameid($gameid);
1260 if($solo == "trumpless" || $solo == "jack" || $solo == "queen" )
1261 $ok = 0; /* no Karlchen in these solos */
1265 foreach($play as $played)
1266 if ( $played['card']==11 || $played['card']==12 )
1267 if ($played['pos'] == $winner )
1269 /* possible caught a fox, check party */
1270 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1271 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1273 DB_query("INSERT INTO Score".
1274 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1276 /* check for doppelopf (>40 points)*/
1278 foreach($play as $played)
1280 $points += DB_get_card_value_by_cardid($played['card']);
1284 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1285 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1287 DB_query("INSERT INTO Score".
1288 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
1292 DB_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1294 echo "ERROR during scoring";
1297 echo "DEBUG: position $winner won the trick <br />";
1299 /* who is the next player? */
1304 $next = DB_get_pos_by_hash($me)+1;
1306 if($next==5) $next=1;
1308 /* check for coment */
1309 if(myisset("comment"))
1311 $comment = $_REQUEST["comment"];
1313 DB_insert_comment($comment,$playid,$myid);
1314 if($commentSchweinchen)
1315 $comment = $commentSchweinchen . $comment;
1318 /* check for note */
1321 $note = $_REQUEST["note"];
1323 DB_insert_note($note,$gameid,$myid);
1326 /* display played card */
1327 $pos = DB_get_pos_by_hash($me);
1330 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1331 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1332 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1335 echo " <div class=\"card".($pos-1)."\">\n ";
1337 /* display comments */
1338 display_card($card,$PREF["cardset"]);
1340 echo "\n <span class=\"comment\"> ".$comment."</span>\n";
1343 /*check if we still have cards left, else set status to gameover */
1344 if(sizeof(DB_get_hand($me))==0)
1346 DB_set_hand_status_by_hash($me,'gameover');
1347 $mystatus = 'gameover';
1350 /* if all players are done, set game status to game over,
1351 * get the points of the last trick and send out an email
1354 $userids = DB_get_all_userid_by_gameid($gameid);
1357 foreach($userids as $user)
1358 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1362 DB_set_game_status_by_gameid($gameid,"gameover");
1364 /* email next player, if game is still running */
1365 if(DB_get_game_status_by_gameid($gameid)=='play')
1367 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1368 $email = DB_get_email('hash',$next_hash);
1369 $who = DB_get_userid('email',$email);
1370 DB_set_player_by_gameid($gameid,$who);
1372 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1373 "It's your turn now.\n".
1374 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$next_hash."\n\n" ;
1375 if( DB_get_email_pref_by_uid($who)!="emailaddict" )
1376 mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
1378 else /* send out final email */
1380 /* individual score */
1381 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1382 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1383 " LEFT JOIN User ON User.id=Hand.user_id".
1384 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1385 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1386 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1387 " WHERE Hand.game_id='$gameid'".
1388 " GROUP BY User.fullname" );
1389 $message = "The game is over. Thanks for playing :)\n";
1390 $message .= "Final score:\n";
1391 while( $r = DB_fetch_array($result) )
1392 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1394 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1395 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1396 " LEFT JOIN User ON User.id=Hand.user_id".
1397 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1398 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1399 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1400 " WHERE Hand.game_id='$gameid'".
1401 " GROUP BY Hand.party" );
1402 $message .= "\nTotals:\n";
1405 while( $r = DB_fetch_array($result) )
1407 $message .= " ".$r[0]." ".$r[1]."\n";
1410 else if($r[0] == "contra")
1415 * save score in database
1419 /* get calls from re/contra */
1421 $call_contra = NULL;
1422 foreach($userids as $user)
1424 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1425 $call = DB_get_call_by_hash($hash);
1426 $party = DB_get_party_by_hash($hash);
1430 $call = (int) $call;
1436 else if( $call < $call_re)
1439 else if($party=="contra")
1441 if($call_contra==NULL)
1442 $call_contra = $call;
1443 else if( $call < $call_re)
1444 $call_contra = $call;
1449 /* figure out who one */
1450 $winning_party = NULL;
1452 if($call_re == NULL && $call_contra==NULL)
1454 /* nobody made a call, so it's easy to figure out who won */
1456 $winning_party="re";
1458 $winning_party="contra";
1462 /* if one party makes a call, they only win, iff they make enough points
1463 * if only one party made a call, the other one wins,
1464 * if the first one didn't make it
1468 $offset = 120 - $call_re;
1470 $offset--; /* since we use a > in the next equation */
1472 if($re > 120+$offset)
1473 $winning_party="re";
1474 else if ($call_contra == NULL )
1475 $winning_party="contra";
1480 $offset = 120 - $call_contra;
1481 if($call_contra == 0)
1482 $offset--; /* since we use a > in the next equation */
1484 if($contra > 120+$offset)
1485 $winning_party="contra";
1486 else if ($call_re == NULL )
1487 $winning_party="re";
1491 /* one point for each call of the other party in case the other party didn't win
1492 * and one point each in case the party made more than points than one of the calls
1494 if($winning_party!="contra" && $call_contra!=NULL)
1496 for( $p=$call_contra;$p<=120; $p+=30 )
1498 DB_query("INSERT INTO Score".
1499 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
1502 for( $p=$call_contra; $p<120; $p+=30)
1505 DB_query("INSERT INTO Score".
1506 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'made$p')");
1509 if($winning_party!="re" and $call_re!=NULL)
1511 for( $p=$call_re;$p<=120; $p+=30 )
1513 DB_query("INSERT INTO Score".
1514 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'against$p')");
1517 for( $p=$call_re; $p<120; $p+=30)
1520 DB_query("INSERT INTO Score".
1521 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
1525 /* point in case contra won */
1526 if($winning_party=="contra")
1528 DB_query("INSERT INTO Score".
1529 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1532 /* one point each for winning and each 30 points + calls */
1533 if($winning_party=="re")
1535 foreach(array(120,150,180,210,240) as $p)
1538 if($p==240 || $call_contra!=NULL)
1542 DB_query("INSERT INTO Score".
1543 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
1545 /* re called something and won */
1546 foreach(array(0,30,60,90,120) as $p)
1548 if($call_re!=NULL && $call_re<$p+1)
1549 DB_query("INSERT INTO Score".
1550 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
1553 else if( $winning_party=="contra")
1555 foreach(array(120,150,180,210,240) as $p)
1558 if($p==240 || $call_re!=NULL)
1561 if($contra>$p-$offset)
1562 DB_query("INSERT INTO Score".
1563 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
1565 /* re called something and won */
1566 foreach(array(0,30,60,90,120) as $p)
1568 if($call_contra!=NULL && $call_contra<$p+1)
1569 DB_query("INSERT INTO Score".
1570 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
1575 /* add score points to email */
1578 $message .= " Points Re: \n";
1579 $queryresult = DB_query("SELECT score FROM Score ".
1580 " WHERE game_id=$gameid AND party='re'".
1582 while($r = DB_fetch_array($queryresult) )
1584 $message .= " ".$r[0]."\n";
1587 $message .= " Points Contra: \n";
1588 $queryresult = DB_query("SELECT score FROM Score ".
1589 " WHERE game_id=$gameid AND party='contra'".
1591 while($r = DB_fetch_array($queryresult) )
1593 $message .= " ".$r[0]."\n";
1596 $message .= " Total Points (from the Re point of view): $Tpoint\n";
1599 $session = DB_get_session_by_gameid($gameid);
1600 $score = generate_score_table($session);
1601 /* convert html to ascii */
1602 $score = str_replace("<div class=\"scoretable\">\n<table class=\"score\">\n <tr>\n","",$score);
1603 $score = str_replace("</table></div>\n","",$score);
1604 $score = str_replace("\n","",$score);
1605 $score = str_replace(array("<tr>","</tr>","<td>","</td>"),array("","\n","","|"),$score);
1606 $score = explode("\n",$score);
1608 $header = array_slice($score,0,1);
1609 $header = explode("|",$header[0]);
1610 for($i=0;$i<sizeof($header);$i++)
1611 $header[$i]=str_pad($header[$i],6," ",STR_PAD_BOTH);
1612 $header = implode("|",$header);
1613 $header.= "\n------+------+------+------+------+\n";
1614 if(sizeof($score)>5) $header.= " ... \n";
1616 if(sizeof($score)>5) $score = array_slice($score,-5,5);
1617 for($i=0;$i<sizeof($score);$i++)
1619 $line = explode("|",$score[$i]);
1620 for($j=0;$j<sizeof($line);$j++)
1621 $line[$j]=str_pad($line[$j],6," ",STR_PAD_LEFT);
1622 $score[$i] = implode("|",$line);
1625 $score = implode("\n",$score);
1626 $score = $header.$score;
1628 $message .= "Score Table:\n";
1631 /* send out final email */
1634 foreach($userids as $user)
1635 $all[] = DB_get_email('userid',$user);
1636 $To = implode(",",$all);
1638 $help = "\n\n (you can use reply all on this email to reach all the players.)\n";
1639 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 1(2)",$message.$help);
1641 foreach($userids as $user)
1643 $To = DB_get_email('userid',$user);
1644 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1646 $link = "Use this link to have a look at game ".DB_format_gameid($gameid).": ".
1647 $HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
1648 if( DB_get_email_pref_by_uid($user) != "emailaddict" )
1649 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 2(2)",$link);
1655 echo "can't find that card?! <br />\n";
1658 else if(myisset("card") && !$myturn )
1660 echo "please wait until it's your turn! <br />\n";
1663 if($seq!=4 && $trickNR>1)
1664 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1666 /* display points in case game is over */
1667 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1669 echo " <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1670 " <div class=\"trick\" id=\"trick13\">\n";
1671 /* add pic for re/contra
1672 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1674 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1675 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1676 " LEFT JOIN User ON User.id=Hand.user_id".
1677 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1678 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1679 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1680 " WHERE Hand.game_id='$gameid'".
1681 " GROUP BY User.fullname" );
1682 while( $r = DB_fetch_array($result))
1683 echo " <div class=\"card".($r[3]-1)."\">\n".
1684 " <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
1687 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1691 echo "</ul>\n"; /* end ul tricks*/
1693 echo "<div class=\"notes\"> Personal notes: <br />\n";
1694 $notes = DB_get_notes_by_userid_and_gameid($myid,$gameid);
1695 foreach($notes as $note)
1696 echo "$note <hr \>\n";
1697 echo "Insert note:<input name=\"note\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1700 $mycards = DB_get_hand($me);
1701 $mycards = mysort($mycards,$gametype);
1702 echo "<div class=\"mycards\">\n";
1704 if($myturn && !myisset("card") && $mystatus=='play' )
1706 echo "Hello ".$myname.", it's your turn! <br />\n";
1707 echo "Your cards are: <br />\n";
1709 /* do we have to follow suite? */
1711 if(have_suit($mycards,$firstcard))
1714 foreach($mycards as $card)
1716 if($followsuit && !same_type($card,$firstcard))
1717 display_card($card,$PREF["cardset"]);
1719 display_link_card($card,$PREF["cardset"]);
1722 else if($mystatus=='play' )
1724 echo "Your cards are: <br />\n";
1725 foreach($mycards as $card)
1726 display_card($card,$PREF["cardset"]);
1728 else if($mystatus=='gameover')
1730 $oldcards = DB_get_all_hand($me);
1731 $oldcards = mysort($oldcards,$gametype);
1732 echo "Your cards were: <br />\n";
1733 foreach($oldcards as $card)
1734 display_card($card,$PREF["cardset"]);
1736 $userids = DB_get_all_userid_by_gameid($gameid);
1737 foreach($userids as $user)
1739 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1745 $name = DB_get_name('userid',$user);
1746 $oldcards = DB_get_all_hand($userhash);
1747 $oldcards = mysort($oldcards,$gametype);
1748 echo "$name's cards were: <br />\n";
1749 foreach($oldcards as $card)
1750 display_card($card,$PREF["cardset"]);
1756 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1757 if($mystatus=='play')
1760 /* the following happens only when the gamestatus is 'gameover' */
1761 /* check if game is over, display results */
1762 if(DB_get_game_status_by_gameid($gameid)=='play')
1764 echo "The game is over for you.. other people still need to play though";
1768 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1769 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1770 " LEFT JOIN User ON User.id=Hand.user_id".
1771 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1772 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1773 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1774 " WHERE Hand.game_id='$gameid'".
1775 " GROUP BY Hand.party" );
1776 echo "<div class=\"total\"> Totals:<br />\n";
1777 while( $r = DB_fetch_array($result))
1778 echo " ".$r[0]." ".$r[1]."<br />\n";
1780 $queryresult = DB_query("SELECT timediff(mod_date,create_date) ".
1781 " FROM Game WHERE id='$gameid'");
1782 $r = DB_fetch_array($queryresult);
1783 echo "<p>This game took ".$r[0]." hours.</p>";
1785 echo "<div class=\"re\">\n Points Re: <br />\n";
1786 $queryresult = DB_query("SELECT score FROM Score ".
1787 " WHERE game_id=$gameid AND party='re'".
1789 while($r = DB_fetch_array($queryresult) )
1790 echo " ".$r[0]."<br />\n";
1793 echo "<div class=\"contra\">\n Points Contra: <br />\n";
1794 $queryresult = DB_query("SELECT score FROM Score ".
1795 " WHERE game_id=$gameid AND party='contra'".
1797 while($r = DB_fetch_array($queryresult) )
1798 echo " ".$r[0]."<br />\n";
1807 myerror("error in testing the status");
1809 /* output left menu */
1810 display_user_menu();
1812 /* output right menu */
1814 /* display rule set for this game */
1815 echo "<div class=\"gameinfo\">\n";
1817 if($gamestatus != 'pre')
1818 echo " Gametype: $GT <br />\n";
1820 echo "Rules: <br />\n";
1821 echo "10ofhearts : ".$RULES["dullen"] ."<br />\n";
1822 echo "schweinchen: ".$RULES["schweinchen"] ."<br />\n";
1823 echo "call: ".$RULES["call"] ."<br />\n";
1826 if($gamestatus == 'play' )
1827 output_form_calls($me);
1829 /* get time from the last action of the game */
1830 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1831 $gameend = time() - strtotime($r[0]);
1833 if($gamestatus == 'play' || $gameend < 60*60*24*7)
1835 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1839 echo "<input type=\"submit\" value=\"submit\" />\n<hr />\n";
1841 $session = DB_get_session_by_gameid($gameid);
1842 $score = generate_score_table($session);
1850 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1853 $session = DB_get_session_by_gameid($gameid);
1854 $result = DB_query("SELECT id,create_date FROM Game".
1855 " WHERE session=$session".
1856 " ORDER BY create_date DESC".
1860 $r = DB_fetch_array($result);
1862 if(!$session || $gameid==$r[0])
1864 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
1865 $names = DB_get_all_names_by_gameid($gameid);
1866 $type = DB_get_gametype_by_gameid($gameid);
1870 $solo = DB_get_solo_by_gameid($gameid);
1872 if($solo!='silent') /* repeat game with same first player */
1873 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
1874 else /* rotate normally */
1875 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
1877 else /* rotate normally */
1878 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);