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 /* does anyone have both foxes */
61 $GAME["schweinchen"]=0;
64 $hash = DB_get_hash_from_game_and_pos($gameid,$i);
65 $cards = DB_get_all_hand($hash);
66 if( in_array("19",$cards) && in_array("20",$cards) )
68 $GAME["schweinchen"]=1;
69 $GAME["schweinchen-who"]=$hash;
73 /* put everyting in a form */
74 echo "<form action=\"index.php?action=game&me=$me\" method=\"post\">\n";
78 /* output extra division in case this game is part of a session */
81 echo "<div class=\"session\">\n".
82 "This game is part of session $session: \n";
83 $hashes = DB_get_hashes_by_session($session,$myid);
85 foreach($hashes as $hash)
90 echo "<a href=\"".$INDEX."?action=game&me=".$hash."\">$i</a> \n";
96 /* display the table and the names */
99 /* mystatus gets the player through the different stages of a game.
100 * start: does the player want to play?
101 * init: check for sickness
102 * check: check for return values from init
103 * poverty: handle poverty, wait here until all player have reached this state
104 * display sickness and move on to game
105 * play: game in progress
106 * gameover: are we revisiting a game
113 /* asks the player, if he wants to join the game */
114 output_check_want_to_play($me);
119 /* check the result, if player wants to join, got next stage, else cancel game */
120 if($_REQUEST["in"] == "no")
122 /* cancel the game */
123 $message = "Hello, \n\n".
124 "the game has been canceled due to the request of one of the players.\n";
126 $userids = DB_get_all_userid_by_gameid($gameid);
127 foreach($userids as $user)
129 $To = DB_get_email('userid',$user);
130 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
133 /* delete everything from the dB */
139 /* user wants to join the game */
141 /* move on to the next stage,
142 * no break statement to immediately go to the next stage
145 DB_set_hand_status_by_hash($me,'init');
147 /* check if everyone has reached this stage, send out email */
148 $userids = DB_get_all_userid_by_gameid($gameid);
150 foreach($userids as $user)
152 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
153 if($userstat!='init')
155 /* whos turn is it? */
156 DB_set_player_by_gameid($gameid,$user);
162 /* all done, send out email unless this player is the startplayer */
163 $startplayer = DB_get_startplayer_by_gameid($gameid);
164 if($mypos == $startplayer)
166 /* do nothing, go to next stage */
170 /* email startplayer */
172 $email = DB_get_email('position-gameid',$startplayer,$gameid);
173 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
174 $who = DB_get_userid('email',$email);
175 DB_set_player_by_gameid($gameid,$who);
177 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
178 "Use this link to go the game: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
179 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
186 /* here we ask the player if he is sick */
187 $mycards = DB_get_hand($me);
190 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
191 echo "\n<ul class=\"tricks\">\n";
192 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
193 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
194 " <div class=\"trick\" id=\"trick0\">\n";
196 for($pos=1;$pos<5;$pos++)
198 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
199 $userid = DB_get_userid('gameid-position',$gameid,$pos);
200 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
202 if($userstatus=='start' || $userstatus=='init')
203 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs to decide </div>\n"; /* show this to everyone */
205 if($usersick!=NULL && $pos<=$mypos ) /* only show this for people sitting before the player */
206 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
207 else if($usersick==NULL && $pos<=$mypos)
208 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
210 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
211 /* end displaying sickness */
213 if(!myisset("solo","wedding","poverty","nines") )
215 output_check_for_sickness($me,$mycards);
217 echo "<div class=\"mycards\">Your cards are: <br />\n";
218 foreach($mycards as $card)
219 display_card($card,$PREF["cardset"]);
226 /* check if someone selected more than one sickness */
228 if($_REQUEST["solo"]!="No") $Nsickness++;
229 if($_REQUEST["wedding"] == "yes") $Nsickness++;
230 if($_REQUEST["poverty"] == "yes") $Nsickness++;
231 if($_REQUEST["nines"] == "yes") $Nsickness++;
235 echo "<p class=\"message\"> You selected more than one sickness, please go back ".
236 "and answer the <a href=\"$INDEX?action=game&me=$me&in=yes\">question</a> again.</p>";
238 echo "<div class=\"mycards\">Your cards are: <br />\n";
239 foreach($mycards as $card)
240 display_card($card,$PREF["cardset"]);
247 /* everything is ok, save what user said and proceed */
248 echo "<p class=\"message\">Processing what you selected in the last step...";
250 /* check if this sickness needs to be handled first */
251 $gametype = DB_get_gametype_by_gameid($gameid);
252 $startplayer = DB_get_startplayer_by_gameid($gameid); /* need this to check which solo goes first */
254 if( $_REQUEST["solo"]!="No" )
256 /* user wants to play a solo */
258 /* store the info in the user's hand info */
259 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
260 DB_set_sickness_by_hash($me,"solo");
262 echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
264 if($gametype == "solo" && $startplayer<$mypos)
265 {}/* do nothing, since someone else already is playing solo */
268 /* this solo comes first
269 * store info in game table
271 DB_set_gametype_by_gameid($gameid,"solo");
272 DB_set_startplayer_by_gameid($gameid,$mypos);
273 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
276 else if($_REQUEST["wedding"] == "yes")
278 /* TODO: add silent solo somewhere*/
279 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
280 DB_set_sickness_by_hash($me,"wedding");
282 else if($_REQUEST["poverty"] == "yes")
284 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
285 DB_set_sickness_by_hash($me,"poverty");
287 else if($_REQUEST["nines"] == "yes")
289 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
290 " is playing solo, this game will be canceled.<br />\n";
291 DB_set_sickness_by_hash($me,"nines");
296 /* move on to the next stage*/
297 DB_set_hand_status_by_hash($me,'check');
302 /* here we check what all players said and figure out what game we are playing
303 * this can therefore only be handled once all players finished the last stage
306 /* only need to redisplay the cards when the user reloads the page and lands directly here */
307 if($mystatus=='check')
309 $mycards = DB_get_hand($me);
312 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
313 echo "\n<ul class=\"tricks\">\n";
314 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
315 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
316 " <div class=\"trick\" id=\"trick0\">\n";
318 for($pos=1;$pos<5;$pos++)
320 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
321 $userid = DB_get_userid('gameid-position',$gameid,$pos);
322 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
324 if($userstatus=='start' || $userstatus=='init')
325 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs to decide </div>\n"; /* show this to everyone */
327 if($usersick!=NULL) /* in the init-phase we only showed players with $pos<$mypos, now we can show all */
328 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
330 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
332 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
333 /* end displaying sickness */
336 echo "<div class=\"message\">\n";
337 echo "<p> Checking if someone else selected solo, nines, wedding or poverty.</p>";
339 /* check if everyone has reached this stage */
340 $userids = DB_get_all_userid_by_gameid($gameid);
342 foreach($userids as $user)
344 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
345 if($userstat!='check')
348 DB_set_player_by_gameid($gameid,$user);
354 echo "<p>This step can only be handled after everyone finished the last step. ".
355 "Seems like this is not the case, so you need to wait a bit... ".
356 "you will get an email once that is the case, please use the link in ".
357 "that email to continue the game.</p></div>";
359 /* display cards, if player was just at the init-phase he will still see the cards from there
360 * we can put this one here, since the last player to finish the init state won't get here and
361 * will still see his card anyway from the init-phase
363 if($mystatus=='check')
366 echo "<div class=\"mycards\">Your cards are: <br />\n";
367 foreach($mycards as $card)
368 display_card($card,$PREF["cardset"]);
375 /* Ok, everyone finished the init-phase, time to figure out what game we
376 * are playing, in case there are any solos this already
377 * will have the correct information in it */
379 echo "<p> Ok, everyone is done... figuring out what kind of game we are playing.</p>";
381 $gametype = DB_get_gametype_by_gameid($gameid);
382 $startplayer = DB_get_startplayer_by_gameid($gameid);
384 /* check for sickness */
389 foreach($userids as $user)
391 $name = DB_get_name('userid',$user);
392 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
393 if($usersick == 'nines')
396 break; /* no need to check for other poverties, since only solo can win and that is already set */
398 else if($usersick == 'poverty')
400 else if($usersick == 'wedding')
402 else if($usersick == 'solo')
406 /* now check which sickness comes first and set the gametype to it */
407 if($gametype == "solo")
414 /* TODO: should we keep statistics of this? */
415 $message = "Hello, \n\n".
416 " the game has been canceled because ".DB_get_name('userid',$nines).
417 " has five or more nines and nobody is playing solo.\n\n".
418 " To redeal either start a new game or, in case the game was part of a tournament, \n".
419 " go to the last game and use the link at the bottom of the page to redeal.";
421 $userids = DB_get_all_userid_by_gameid($gameid);
422 foreach($userids as $user)
424 $To = DB_get_email('userid',$user);
425 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
428 /* delete everything from the dB */
431 echo "The game has been canceled because ".DB_get_name('userid',$nines).
432 " has five or more nines and nobody is playing solo.\n";
437 else if($poverty==1) /* one person has poverty */
439 DB_set_gametype_by_gameid($gameid,"poverty");
440 $gametype = "poverty";
441 $who = DB_get_sickness_by_gameid($gameid);
444 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
445 if($firstsick == "poverty")
446 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
448 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
451 else if($poverty==2) /* two people have poverty */
453 DB_set_gametype_by_gameid($gameid,"dpoverty");
454 $gametype = "dpoverty";
455 $who = DB_get_sickness_by_gameid($gameid);
458 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
459 if($firstsick == "poverty")
461 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
462 if($secondsick == "poverty")
463 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
465 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
468 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
473 DB_set_gametype_by_gameid($gameid,"wedding");
474 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
475 $gametype = "wedding";
477 /* now the gametype is set correctly in the database */
478 echo "<p> Got it :)</p>";
480 /* loop over all players, set re/contra if possible and start the game if possible */
481 $userids = DB_get_all_userid_by_gameid($gameid);
482 foreach($userids as $userid)
484 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
489 /* are we the solo player? set us to re, else set us to contra */
490 $pos = DB_get_pos_by_hash($userhash);
491 if($pos == $startplayer)
492 DB_set_party_by_hash($userhash,"re");
494 DB_set_party_by_hash($userhash,"contra");
495 DB_set_hand_status_by_hash($userhash,'play');
499 /* set person with the wedding to re, do the rest during the game */
500 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
501 if($usersick == "wedding")
502 DB_set_party_by_hash($userhash,"re");
504 DB_set_party_by_hash($userhash,"contra");
506 DB_set_hand_status_by_hash($userhash,'play');
510 $hand = DB_get_all_hand($userhash);
512 if(in_array('3',$hand)||in_array('4',$hand))
513 DB_set_party_by_hash($userhash,"re");
515 DB_set_party_by_hash($userhash,"contra");
516 DB_set_hand_status_by_hash($userhash,'play');
520 /* set person with poverty to play status */
521 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
522 if($usersick == "poverty")
523 DB_set_hand_status_by_hash($userhash,'play');
525 /* set status of first player to be asked to poverty */
526 $who = DB_get_sickness_by_gameid($gameid);
527 if($who > 6) $who= $who/10; /* in case we have dpoverty */
528 $whoid = DB_get_userid('gameid-position',$gameid,$who);
530 DB_set_hand_status_by_hash($userhash,'poverty');
534 /* send out email to first player or poverty person*/
535 if($gametype!="poverty" && $gametype!="dpoverty")
537 $startplayer = DB_get_startplayer_by_gameid($gameid);
538 $email = DB_get_email('position-gameid',$startplayer,$gameid);
539 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
540 $who = DB_get_userid('email',$email);
541 DB_set_player_by_gameid($gameid,$who);
545 if(DB_get_email_pref_by_hash($hash)!="emailaddict")
547 /* email startplayer */
548 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
549 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
550 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
554 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
558 /* set status of first player to be asked to poverty */
559 $who = DB_get_sickness_by_gameid($gameid);
560 if($who > 6) $who= $who/10; /* in case we have dpoverty */
562 $whoid = DB_get_userid('gameid-position',$gameid,$who);
564 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
567 $email = DB_get_email('position-gameid',$who,$gameid);
568 $whohash = DB_get_hash_from_game_and_pos($gameid,$who);
569 DB_set_player_by_gameid($gameid,$whoid);
571 if(DB_get_email_pref_by_hash($hash)!="emailaddict")
573 /* email player for poverty */
574 $message = "Poverty: It's your turn now in game ".DB_format_gameid($gameid).".\n".
575 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$whohash."\n\n" ;
576 mymail($email,$EmailName."Poverty (game ".DB_format_gameid($gameid).") ",$message);
584 /* user only gets here in a poverty game, several things have to be handled here:
585 * A) ask, if user wants to take trump
587 * poverty: set re/contra
588 * dpoverty: first time: set re, send email to second player
589 * second time: set contra
590 * set status to play in case 0 trump
591 * no -> set status to play,
592 * ask next player or cancle the game if no more players
593 * B) user took trump and has too many cards (e.g. count(cards)>12 and re/contra set)
594 * ask to give cards back, set status to play, once player has 12 cards
596 * it is easier to check B) first
599 /* output pre game in case user reloads */
602 set_gametype($gametype); /* this sets the $CARDS variable */
603 $myparty = DB_get_party_by_hash($me);
605 /* the following is part B) of whats needs to be done)
606 /* check if user wants to give cards back */
607 if(myisset("exchange"))
609 $exchange = $_REQUEST['exchange'];
610 $partnerhash = DB_get_partner_hash_by_hash($me);
611 $partnerid = DB_get_userid('hash',$partnerhash);
612 $partnerhand = DB_get_handid('gameid-userid',$gameid,$partnerid);
614 /* if exchange is set to a value>0, exchange that card back to the partner */
617 $result = DB_query("UPDATE Hand_Card SET hand_id='$partnerhand'".
618 " WHERE hand_id='$myhand' AND card_id=".DB_quote_smart($exchange));
622 $mycards = DB_get_hand($me);
623 $mycards = mysort($mycards,$gametype);
625 /* check if user need to give more cards back */
626 if( ($myparty=='re' || $myparty=='contra') && count($mycards)>12)
628 echo "<div class=\"poverty\"> you need to get rid of a few cards</div>\n";
631 echo "<div class=\"mycards\">Your cards are: <br />\n";
632 foreach($mycards as $card)
633 display_link_card($card,$PREF["cardset"],$type);
634 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select card to give back\" />\n";
637 else if( ($myparty=='re' || $myparty=='contra') && count($mycards)==12)
639 /* user is done, ready to play */
640 DB_set_hand_status_by_hash($me,'play');
641 /* TODO if resolved; email start player, set startplayer */
644 /* the following is part A) of what needs to be done */
645 if(!myisset("trump"))
649 echo "<div class=\"poverty\">\n";
650 $userids = DB_get_all_userid_by_gameid($gameid);
651 foreach($userids as $user)
653 $name = DB_get_name('userid',$user);
654 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
655 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
656 $userparty = DB_get_party_by_hash($userhash);
658 if($usersick=="poverty" && !$userparty)
660 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
661 $cards = DB_get_hand($hash);
664 foreach($cards as $card)
665 if($card<27) $nrtrump++;
666 echo "Player $name has $nrtrump trump. Do you want to take them?".
667 "<a href=\"index.php?action=game&me=$me&trump=$user\">yes</a> <br />\n";
670 echo "<a href=\"index.php?action=game&me=$me&trump=no\">No,way I take those trump...</a> <br />\n";
671 echo "</div><div>\n";
673 echo "<div class=\"mycards\">Your cards are: <br />\n";
674 foreach($mycards as $card)
675 display_card($card,$PREF["cardset"]);
676 echo "</div></div>\n";
682 $trump = $_REQUEST['trump'];
686 /* user doesn't want to take trump */
687 DB_set_hand_status_by_hash($me,'play');
689 /* set next player who needs to be asked and email him*/
690 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
691 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
693 /* don't ask people who have poverty */
695 if($firstsick=="poverty")
697 if($secondsick=="poverty")
702 if($gametype=="dpoverty")
704 $next=999; /* need to cancel for sure, since both would need to take the trump */
707 /* no more people to ask, need to cancel the game */
710 $message = "Hello, \n\n".
711 "Game ".DB_format_gameid($gameid)." has been canceled since nobody wanted to take the trump.\n";
713 $userids = DB_get_all_userid_by_gameid($gameid);
714 foreach($userids as $user)
716 $To = DB_get_email('userid',$user);
717 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled (poverty not resolved)",$message);
720 /* delete everything from the dB */
723 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been canceled.<br /><br /></p>";
730 /* email next player, set his status to poverty */
731 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
732 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
733 $userid = DB_get_userid('email',$To);
735 DB_set_player_by_gameid($gameid,$userid);
736 DB_set_hand_status_by_hash($userhash,'poverty');
738 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
739 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
740 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
745 /* player wants to take trump, change cards */
747 /* user wants to take trump */
748 $trump = $_REQUEST["trump"];
749 $userhand = DB_get_handid('gameid-userid',$gameid,$trump);
750 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
752 /* copy trump from player A to B */
753 $result = DB_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
755 $mycards = DB_get_hand($me);
758 if($gametype=='poverty')
760 $userids = DB_get_all_userid_by_gameid($gameid);
761 foreach($userids as $user)
763 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
764 if($hash==$userhash||$hash==$me)
765 DB_set_party_by_hash($hash,"re");
767 DB_set_party_by_hash($hash,"contra");
769 /* check if we are done, if so, send everyone into the 'play' phase */
770 if(count($mycards)==12)
772 foreach($userids as $user)
774 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
775 DB_set_hand_status_by_hash($hash,'play');
781 /* has the re party already been set?*/
783 $userids = DB_get_all_userid_by_gameid($gameid);
784 foreach($userids as $user)
786 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
787 $party = DB_get_party_by_hash($hash);
793 DB_set_party_by_hash($me,'contra');
794 DB_set_party_by_hash($userhash,'contra');
798 DB_set_party_by_hash($me,'re');
799 DB_set_party_by_hash($userhash,'re');
801 /* send out email to second non-poverty player */
802 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
803 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
806 if($firstsick=="poverty")
807 if($secondsick=="poverty")
813 echo "<div class=\"message\">Error in poverty, please contact the Admin</div>\n";
815 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
816 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
817 $userid = DB_get_userid('email',$To);
819 DB_set_player_by_gameid($gameid,$userid);
820 DB_set_hand_status_by_hash($userhash,'poverty');
822 $message = "Two people have poverty, it's your turn to decide, if you want to take the trump. Please visit:".
823 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
824 mymail($To,$EmailName." double poverty (game ".DB_format_gameid($gameid).")",$message);
829 echo "<div class=\"message\"> Please, <a href=\"$INDEX?action=game&me=$me\">continue</a> here.</div>\n";
837 /* both entries here, so that the tricks are visible for both.
838 * in case of 'play' there is a break later that skips the last part
841 /* check if all players are ready to play */
843 $userids = DB_get_all_userid_by_gameid($gameid);
844 foreach($userids as $user)
846 $userstatus = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
847 if($userstatus !='play')
850 DB_set_player_by_gameid($gameid,$user);
855 /* only set this after all poverty, etc. are handled*/
856 DB_set_game_status_by_gameid($gameid,'play');
858 /* email startplayer */
859 $startplayer = DB_get_startplayer_by_gameid($gameid);
860 $email = DB_get_email('position-gameid',$startplayer,$gameid);
861 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
862 $who = DB_get_userid('email',$email);
863 DB_set_player_by_gameid($gameid,$who);
865 if($hash!=$me && DB_get_email_pref_by_hash($hash)!="emailaddict")
867 /* email startplayer) */
868 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
869 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
870 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
874 /* figure out what kind of game we are playing,
875 * set the global variables $CARDS["trump"],$CARDS["diamonds"],$CARDS["hearts"],
876 * $CARDS["clubs"],$CARDS["spades"],$CARDS["foxes"]
880 $gametype = DB_get_gametype_by_gameid($gameid);
882 if($gametype=="solo")
884 $gametype = DB_get_solo_by_gameid($gameid);
885 $GT = $gametype." ".$GT;
888 $gametype = "normal";
890 set_gametype($gametype); /* this sets the $CARDS variable */
892 /* get some infos about the game */
893 $gamestatus = DB_get_game_status_by_gameid($gameid);
895 /* has the game started? No, then just wait here...*/
896 if($gamestatus == 'pre')
898 echo "<p class=\"message\"> You finished the setup, but not everyone else finished it... ".
899 "You need to wait for the others. Just wait for an email. </p>";
901 $mycards = DB_get_hand($me);
902 $mycards = mysort($mycards,$gametype);
904 echo "<div class=\"mycards\">Your cards are: <br />\n";
905 foreach($mycards as $card)
906 display_card($card,$PREF["cardset"]);
909 break; /* not sure this works... the idea is that you can
910 * only play a card after everyone is ready to play */
913 /* get time from the last action of the game */
914 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
915 $gameend = time() - strtotime($r[0]);
917 /* handle comments in case player didn't play a card, allow comments a week after the end of the game */
918 if( (!myisset("card") && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
919 if(myisset("comment"))
921 $comment = $_REQUEST["comment"];
922 $playid = DB_get_current_playid($gameid);
925 DB_insert_comment($comment,$playid,$myid);
928 /* handle notes in case player didn't play a card, allow notes only during a game */
929 if( (!myisset("card") && $mystatus=='play') )
932 $note = $_REQUEST["note"];
935 DB_insert_note($note,$gameid,$myid);
938 /* get everything relevant to display the tricks */
939 $result = DB_query("SELECT Hand_Card.card_id as card,".
940 " Hand.position as position,".
941 " Play.sequence as sequence, ".
943 " GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
944 " SEPARATOR '\n' ), ".
945 " Play.create_date, ".
948 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
949 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
950 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
951 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
952 "LEFT JOIN User On User.id=Comment.user_id ".
953 "WHERE Trick.game_id='".$gameid."' ".
954 "GROUP BY Trick.id, sequence ".
955 "ORDER BY Trick.id, sequence ASC");
957 $lasttrick = DB_get_max_trickid($gameid);
959 $play = array(); /* needed to calculate winner later */
961 $pos = DB_get_startplayer_by_gameid($gameid)-1;
962 $firstcard = ""; /* first card in a trick */
964 echo "\n<ul class=\"tricks\">\n";
965 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
967 /* output vorbehalte */
968 $mygametype = DB_get_gametype_by_gameid($gameid);
969 if($mygametype != "normal") /* only show when needed */
971 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
972 " <div class=\"trick\" id=\"trick0\">\n";
974 for($mypos=1;$mypos<5;$mypos++)
976 $usersick = DB_get_sickness_by_pos_and_gameid($mypos,$gameid);
979 echo " <div class=\"vorbehalt".($mypos-1)."\"> Vorbehalt <br />";
981 echo " $usersick <br />";
984 if($mygametype == $usersick)
988 echo " </div>\n </li>\n"; /* end div trick, end li trick */
992 while($r = DB_fetch_array($result))
1000 /* check if first schweinchen has been played */
1001 if( $GAME["schweinchen"] && ($r[0] == 19 || $r[0] == 20) )
1002 $GAME["schweinchen"]++;
1004 /* save card to be able to find the winner of the trick later */
1005 $play[$seq] = array("card"=>$r[0],"pos"=>$pos);
1009 /* first card in a trick, output some html */
1010 if($trick!=$lasttrick)
1012 /* start of an old trick? */
1013 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1014 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1015 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1017 else if($trick==$lasttrick)
1019 /* start of a last trick? */
1020 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1021 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1022 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1025 /* remember first card, so that we are able to check, what cards can be played */
1030 echo " <div class=\"card".($pos-1)."\">\n";
1032 /* display comments */
1034 echo " <span class=\"comment\">".$comment."</span>\n";
1037 display_card($r[0],$PREF["cardset"]);
1039 echo " </div>\n"; /* end div card */
1045 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1049 /* whos turn is it? */
1052 $winner = get_winner($play,$gametype); /* returns the position */
1054 $firstcard = ""; /* new trick, no first card */
1059 if($next==5) $next = 1;
1062 /* my turn?, display cards as links, ask for comments*/
1063 if(DB_get_pos_by_hash($me) == $next)
1068 /* do we want to play a card? */
1069 if(myisset("card") && $myturn)
1071 $card = $_REQUEST["card"];
1072 $handid = DB_get_handid('hash',$me);
1074 /* check if we have card and that we haven't played it yet*/
1075 /* set played in hand_card to true where hand_id and card_id*/
1076 $r = DB_query_array("SELECT id FROM Hand_Card WHERE played='false' and ".
1077 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1078 $handcardid = $r[0];
1080 if($handcardid) /* everything ok, play card */
1082 /* update Game timestamp */
1083 DB_update_game_timestamp($gameid);
1085 /* check if a call was made, must do this before we set the card status to played */
1086 if(myisset("call") && $_REQUEST["call"] == "120" && can_call(120,$me))
1087 $result = DB_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
1088 if(myisset("call") && $_REQUEST["call"] == "90" && can_call(90,$me))
1089 $result = DB_query("UPDATE Hand SET point_call='90' WHERE hash='$me' ");
1090 if(myisset("call") && $_REQUEST["call"] == "60" && can_call(60,$me))
1091 $result = DB_query("UPDATE Hand SET point_call='60' WHERE hash='$me' ");
1092 if(myisset("call") && $_REQUEST["call"] == "30" && can_call(30,$me))
1093 $result = DB_query("UPDATE Hand SET point_call='30' WHERE hash='$me' ");
1094 if(myisset("call") && $_REQUEST["call"] == "0" && can_call(0,$me))
1095 $result = DB_query("UPDATE Hand SET point_call='0' WHERE hash='$me' ");
1097 /* mark card as played */
1098 DB_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1099 DB_quote_smart($card));
1101 /* get trick id or start new trick */
1102 $a = DB_get_current_trickid($gameid);
1107 $playid = DB_play_card($trickid,$handcardid,$sequence);
1109 /* check special output for schweinchen in case:
1110 * schweinchen is in the rules, a fox has been played and the gametype is correct
1112 if( $GAME["schweinchen"] &&
1113 ($card == 19 || $card == 20) &&
1114 ($gametype == "normal" || $gametype == "silent"|| $gametype=="trump"))
1116 $GAME["schweinchen"]++; // count how many have been played including this one
1117 if($GAME["schweinchen"]==3 && $RULES["schweinchen"]=="second" )
1118 DB_insert_comment("Schweinchen! ",$playid,$myid);
1119 if($RULES["schweinchen"]=="both" )
1120 DB_insert_comment("Schweinchen! ",$playid,$myid);
1122 echo "schweinchen = ".$GAME["schweinchen"]." ---<br />";
1125 /* if sequence == 4 check who one in case of wedding */
1126 if($sequence == 4 && $GT == "wedding")
1128 /* is wedding resolve */
1129 $resolved = DB_get_sickness_by_gameid($gameid);
1132 /* who has wedding */
1133 $userids = DB_get_all_userid_by_gameid($gameid);
1134 foreach($userids as $user)
1136 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1137 if($usersick == "wedding")
1140 /* who won the trick */
1141 $play = DB_get_cards_by_trick($trickid);
1142 $winner = get_winner($play,$gametype); /* returns the position */
1143 $winnerid = DB_get_userid('gameid-position',$gameid,$winner);
1144 /* is tricknr <=3 */
1145 if($tricknr <=3 && $winnerid!=$whosick)
1147 /* set resolved at tricknr*/
1148 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1150 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1151 DB_set_party_by_hash($whash,"re");
1153 if($tricknr == 3 && $winnerid==$whosick)
1155 /* set resolved at tricknr*/
1156 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1161 /* if sequence == 4, set winner of the trick, count points and set the next player */
1164 $play = DB_get_cards_by_trick($trickid);
1165 $winner = get_winner($play,$gametype); /* returns the position */
1167 /* check if someone caught a fox */
1168 /* first check if we should account for solos at all,
1169 * since it doesn't make sense in some games
1171 $ok = 0; /* fox shouldn't be counted */
1172 if(DB_get_gametype_by_gameid($gameid)=="solo")
1174 $solo = DB_get_solo_by_gameid($gameid);
1175 if($solo == "trump" || $solo == "silent")
1176 $ok = 1; /* for trump solos and silent solos, foxes are ok */
1179 $ok = 1; /* for all other games (not solos) foxes are ok too */
1182 foreach($play as $played)
1184 if ( $played['card']==19 || $played['card']==20 )
1185 if ($played['pos']!= $winner )
1187 /* possible caught a fox, check party */
1188 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1189 $uid2 = DB_get_userid('gameid-position',$gameid,$played['pos']);
1191 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1192 $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1194 if($party1 != $party2)
1195 DB_query("INSERT INTO Score".
1196 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1200 /* check for karlchen (jack of clubs in the last trick)*/
1201 /* same as for foxes, karlchen doesn't always make sense
1202 * check what kind of game it is and set karlchen accordingly */
1203 $ok = 1; /* default: karlchen should be accounted for */
1205 $ok = 0; /* Karlchen works only in the last trick */
1206 if($ok && DB_get_gametype_by_gameid($gameid)=="solo" )
1208 $solo = DB_get_solo_by_gameid($gameid);
1209 if($solo == "trumpless" || $solo == "jack" || $solo == "queen" )
1210 $ok = 0; /* no Karlchen in these solos */
1214 foreach($play as $played)
1215 if ( $played['card']==11 || $played['card']==12 )
1216 if ($played['pos'] == $winner )
1218 /* possible caught a fox, check party */
1219 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1220 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1222 DB_query("INSERT INTO Score".
1223 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1225 /* check for doppelopf (>40 points)*/
1227 foreach($play as $played)
1229 $points += DB_get_card_value_by_cardid($played['card']);
1233 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1234 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1236 DB_query("INSERT INTO Score".
1237 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
1241 DB_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1243 echo "ERROR during scoring";
1246 echo "DEBUG: position $winner won the trick <br />";
1248 /* who is the next player? */
1253 $next = DB_get_pos_by_hash($me)+1;
1255 if($next==5) $next=1;
1257 /* check for coment */
1258 if(myisset("comment"))
1260 $comment = $_REQUEST["comment"];
1262 DB_insert_comment($comment,$playid,$myid);
1265 /* check for note */
1268 $note = $_REQUEST["note"];
1270 DB_insert_note($note,$gameid,$myid);
1273 /* display played card */
1274 $pos = DB_get_pos_by_hash($me);
1277 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1278 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1279 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1282 echo " <div class=\"card".($pos-1)."\">\n ";
1284 /* display comments */
1285 display_card($card,$PREF["cardset"]);
1287 echo "\n <span class=\"comment\"> ".$comment."</span>\n";
1290 /*check if we still have cards left, else set status to gameover */
1291 if(sizeof(DB_get_hand($me))==0)
1293 DB_set_hand_status_by_hash($me,'gameover');
1294 $mystatus = 'gameover';
1297 /* if all players are done, set game status to game over,
1298 * get the points of the last trick and send out an email
1301 $userids = DB_get_all_userid_by_gameid($gameid);
1304 foreach($userids as $user)
1305 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1309 DB_set_game_status_by_gameid($gameid,"gameover");
1311 /* email next player, if game is still running */
1312 if(DB_get_game_status_by_gameid($gameid)=='play')
1314 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1315 $email = DB_get_email('hash',$next_hash);
1316 $who = DB_get_userid('email',$email);
1317 DB_set_player_by_gameid($gameid,$who);
1319 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1320 "It's your turn now.\n".
1321 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$next_hash."\n\n" ;
1322 if( DB_get_email_pref_by_uid($who)!="emailaddict" )
1323 mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
1325 else /* send out final email */
1327 /* individual score */
1328 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1329 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1330 " LEFT JOIN User ON User.id=Hand.user_id".
1331 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1332 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1333 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1334 " WHERE Hand.game_id='$gameid'".
1335 " GROUP BY User.fullname" );
1336 $message = "The game is over. Thanks for playing :)\n";
1337 $message .= "Final score:\n";
1338 while( $r = DB_fetch_array($result) )
1339 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1341 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1342 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1343 " LEFT JOIN User ON User.id=Hand.user_id".
1344 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1345 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1346 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1347 " WHERE Hand.game_id='$gameid'".
1348 " GROUP BY Hand.party" );
1349 $message .= "\nTotals:\n";
1352 while( $r = DB_fetch_array($result) )
1354 $message .= " ".$r[0]." ".$r[1]."\n";
1357 else if($r[0] == "contra")
1362 * save score in database
1366 /* get calls from re/contra */
1368 $call_contra = NULL;
1369 foreach($userids as $user)
1371 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1372 $call = DB_get_call_by_hash($hash);
1373 $party = DB_get_party_by_hash($hash);
1377 $call = (int) $call;
1383 else if( $call < $call_re)
1386 else if($party=="contra")
1388 if($call_contra==NULL)
1389 $call_contra = $call;
1390 else if( $call < $call_re)
1391 $call_contra = $call;
1396 /* figure out who one */
1397 $winning_party = NULL;
1399 if($call_re == NULL && $call_contra==NULL)
1401 /* nobody made a call, so it's easy to figure out who won */
1403 $winning_party="re";
1405 $winning_party="contra";
1409 /* if one party makes a call, they only win, iff they make enough points
1410 * if only one party made a call, the other one wins,
1411 * if the first one didn't make it
1415 $offset = 120 - $call_re;
1417 $offset--; /* since we use a > in the next equation */
1419 if($re > 120+$offset)
1420 $winning_party="re";
1421 else if ($call_contra == NULL )
1422 $winning_party="contra";
1427 $offset = 120 - $call_contra;
1428 if($call_contra == 0)
1429 $offset--; /* since we use a > in the next equation */
1431 if($contra > 120+$offset)
1432 $winning_party="contra";
1433 else if ($call_re == NULL )
1434 $winning_party="re";
1438 /* one point for each call of the other party in case the other party didn't win
1439 * and one point each in case the party made more than points than one of the calls
1441 if($winning_party!="contra" && $call_contra!=NULL)
1443 for( $p=$call_contra;$p<=120; $p+=30 )
1445 DB_query("INSERT INTO Score".
1446 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
1449 for( $p=$call_contra; $p<120; $p+=30)
1452 DB_query("INSERT INTO Score".
1453 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'made$p')");
1456 if($winning_party!="re" and $call_re!=NULL)
1458 for( $p=$call_re;$p<=120; $p+=30 )
1460 DB_query("INSERT INTO Score".
1461 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'against$p')");
1464 for( $p=$call_re; $p<120; $p+=30)
1467 DB_query("INSERT INTO Score".
1468 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
1472 /* point in case contra won */
1473 if($winning_party=="contra")
1475 DB_query("INSERT INTO Score".
1476 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1479 /* one point each for winning and each 30 points + calls */
1480 if($winning_party=="re")
1482 foreach(array(120,150,180,210,240) as $p)
1485 if($p==240 || $call_contra!=NULL)
1489 DB_query("INSERT INTO Score".
1490 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
1492 /* re called something and won */
1493 foreach(array(0,30,60,90,120) as $p)
1495 if($call_re!=NULL && $call_re<$p+1)
1496 DB_query("INSERT INTO Score".
1497 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
1500 else if( $winning_party=="contra")
1502 foreach(array(120,150,180,210,240) as $p)
1505 if($p==240 || $call_re!=NULL)
1508 if($contra>$p-$offset)
1509 DB_query("INSERT INTO Score".
1510 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
1512 /* re called something and won */
1513 foreach(array(0,30,60,90,120) as $p)
1515 if($call_contra!=NULL && $call_contra<$p+1)
1516 DB_query("INSERT INTO Score".
1517 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
1522 /* add score points to email */
1525 $message .= " Points Re: \n";
1526 $queryresult = DB_query("SELECT score FROM Score ".
1527 " WHERE game_id=$gameid AND party='re'".
1529 while($r = DB_fetch_array($queryresult) )
1531 $message .= " ".$r[0]."\n";
1534 $message .= " Points Contra: \n";
1535 $queryresult = DB_query("SELECT score FROM Score ".
1536 " WHERE game_id=$gameid AND party='contra'".
1538 while($r = DB_fetch_array($queryresult) )
1540 $message .= " ".$r[0]."\n";
1543 $message .= " Total Points (from the Re point of view): $Tpoint\n";
1546 $session = DB_get_session_by_gameid($gameid);
1547 $score = generate_score_table($session);
1548 /* convert html to ascii */
1549 $score = str_replace("<div class=\"scoretable\">\n<table class=\"score\">\n <tr>\n","",$score);
1550 $score = str_replace("</table></div>\n","",$score);
1551 $score = str_replace("\n","",$score);
1552 $score = str_replace(array("<tr>","</tr>","<td>","</td>"),array("","\n","","|"),$score);
1553 $score = explode("\n",$score);
1555 $header = array_slice($score,0,1);
1556 $header = explode("|",$header[0]);
1557 for($i=0;$i<sizeof($header);$i++)
1558 $header[$i]=str_pad($header[$i],6," ",STR_PAD_BOTH);
1559 $header = implode("|",$header);
1560 $header.= "\n------+------+------+------+------+\n";
1561 if(sizeof($score)>5) $header.= " ... \n";
1563 if(sizeof($score)>5) $score = array_slice($score,-5,5);
1564 for($i=0;$i<sizeof($score);$i++)
1566 $line = explode("|",$score[$i]);
1567 for($j=0;$j<sizeof($line);$j++)
1568 $line[$j]=str_pad($line[$j],6," ",STR_PAD_LEFT);
1569 $score[$i] = implode("|",$line);
1572 $score = implode("\n",$score);
1573 $score = $header.$score;
1575 $message .= "Score Table:\n";
1578 /* send out final email */
1581 foreach($userids as $user)
1582 $all[] = DB_get_email('userid',$user);
1583 $To = implode(",",$all);
1585 $help = "\n\n (you can use reply all on this email to reach all the players.)\n";
1586 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 1(2)",$message.$help);
1588 foreach($userids as $user)
1590 $To = DB_get_email('userid',$user);
1591 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1593 $link = "Use this link to have a look at game ".DB_format_gameid($gameid).": ".
1594 $HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
1595 if( DB_get_email_pref_by_uid($user) != "emailaddict" )
1596 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 2(2)",$link);
1602 echo "can't find that card?! <br />\n";
1605 else if(myisset("card") && !$myturn )
1607 echo "please wait until it's your turn! <br />\n";
1610 if($seq!=4 && $trickNR>1)
1611 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1613 /* display points in case game is over */
1614 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1616 echo " <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1617 " <div class=\"trick\" id=\"trick13\">\n";
1618 /* add pic for re/contra
1619 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1621 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1622 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1623 " LEFT JOIN User ON User.id=Hand.user_id".
1624 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1625 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1626 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1627 " WHERE Hand.game_id='$gameid'".
1628 " GROUP BY User.fullname" );
1629 while( $r = DB_fetch_array($result))
1630 echo " <div class=\"card".($r[3]-1)."\">\n".
1631 " <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
1634 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1638 echo "</ul>\n"; /* end ul tricks*/
1640 echo "<div class=\"notes\"> Personal notes: <br />\n";
1641 $notes = DB_get_notes_by_userid_and_gameid($myid,$gameid);
1642 foreach($notes as $note)
1643 echo "$note <hr \>\n";
1644 echo "Insert note:<input name=\"note\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1647 $mycards = DB_get_hand($me);
1648 $mycards = mysort($mycards,$gametype);
1649 echo "<div class=\"mycards\">\n";
1651 if($myturn && !myisset("card") && $mystatus=='play' )
1653 echo "Hello ".$myname.", it's your turn! <br />\n";
1654 echo "Your cards are: <br />\n";
1656 /* do we have to follow suite? */
1658 if(have_suit($mycards,$firstcard))
1661 foreach($mycards as $card)
1663 if($followsuit && !same_type($card,$firstcard))
1664 display_card($card,$PREF["cardset"]);
1666 display_link_card($card,$PREF["cardset"]);
1669 else if($mystatus=='play' )
1671 echo "Your cards are: <br />\n";
1672 foreach($mycards as $card)
1673 display_card($card,$PREF["cardset"]);
1675 else if($mystatus=='gameover')
1677 $oldcards = DB_get_all_hand($me);
1678 $oldcards = mysort($oldcards,$gametype);
1679 echo "Your cards were: <br />\n";
1680 foreach($oldcards as $card)
1681 display_card($card,$PREF["cardset"]);
1683 $userids = DB_get_all_userid_by_gameid($gameid);
1684 foreach($userids as $user)
1686 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1692 $name = DB_get_name('userid',$user);
1693 $oldcards = DB_get_all_hand($userhash);
1694 $oldcards = mysort($oldcards,$gametype);
1695 echo "$name's cards were: <br />\n";
1696 foreach($oldcards as $card)
1697 display_card($card,$PREF["cardset"]);
1703 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1704 if($mystatus=='play')
1707 /* the following happens only when the gamestatus is 'gameover' */
1708 /* check if game is over, display results */
1709 if(DB_get_game_status_by_gameid($gameid)=='play')
1711 echo "The game is over for you.. other people still need to play though";
1715 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1716 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1717 " LEFT JOIN User ON User.id=Hand.user_id".
1718 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1719 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1720 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1721 " WHERE Hand.game_id='$gameid'".
1722 " GROUP BY Hand.party" );
1723 echo "<div class=\"total\"> Totals:<br />\n";
1724 while( $r = DB_fetch_array($result))
1725 echo " ".$r[0]." ".$r[1]."<br />\n";
1727 $queryresult = DB_query("SELECT timediff(mod_date,create_date) ".
1728 " FROM Game WHERE id='$gameid'");
1729 $r = DB_fetch_array($queryresult);
1730 echo "<p>This game took ".$r[0]." hours.</p>";
1732 echo "<div class=\"re\">\n Points Re: <br />\n";
1733 $queryresult = DB_query("SELECT score FROM Score ".
1734 " WHERE game_id=$gameid AND party='re'".
1736 while($r = DB_fetch_array($queryresult) )
1737 echo " ".$r[0]."<br />\n";
1740 echo "<div class=\"contra\">\n Points Contra: <br />\n";
1741 $queryresult = DB_query("SELECT score FROM Score ".
1742 " WHERE game_id=$gameid AND party='contra'".
1744 while($r = DB_fetch_array($queryresult) )
1745 echo " ".$r[0]."<br />\n";
1754 myerror("error in testing the status");
1756 /* output left menu */
1757 display_user_menu();
1759 /* output right menu */
1761 /* display rule set for this game */
1762 echo "<div class=\"gameinfo\">\n";
1764 if($gamestatus != 'pre')
1765 echo " Gametype: $GT <br />\n";
1767 echo "Rules: <br />\n";
1768 echo "10ofhearts : ".$RULES["dullen"] ."<br />\n";
1769 echo "schweinchen: ".$RULES["schweinchen"] ."<br />\n";
1770 echo "call: ".$RULES["call"] ."<br />\n";
1773 if($gamestatus == 'play' )
1774 output_form_calls($me);
1776 /* get time from the last action of the game */
1777 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1778 $gameend = time() - strtotime($r[0]);
1780 if($gamestatus == 'play' || $gameend < 60*60*24*7)
1782 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1786 echo "<input type=\"submit\" value=\"submit\" />\n<hr />\n";
1788 $session = DB_get_session_by_gameid($gameid);
1789 $score = generate_score_table($session);
1797 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1800 $session = DB_get_session_by_gameid($gameid);
1801 $result = DB_query("SELECT id,create_date FROM Game".
1802 " WHERE session=$session".
1803 " ORDER BY create_date DESC".
1807 $r = DB_fetch_array($result);
1809 if(!$session || $gameid==$r[0])
1811 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
1812 $names = DB_get_all_names_by_gameid($gameid);
1813 $type = DB_get_gametype_by_gameid($gameid);
1816 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
1818 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);