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( $gamestatus == 'pre' )
68 /* always need to use Schweinchen to figure out for example who has poverty */
73 /* in a game Schweinchen is not valid in all types of games */
74 if( $gametype == 'normal' || $gametype == 'silent' || $gametype=='trump' )
75 if( in_array($RULES['schweinchen'],array('both','second','secondaftercall')) )
81 /* need to check for Schweinchen */
84 $hash = DB_get_hash_from_game_and_pos($gameid,$i);
85 $cards = DB_get_all_hand($hash);
86 if( in_array("19",$cards) && in_array("20",$cards) )
87 $GAME['schweinchen-who']=$hash;
89 $GAME['schweinchen-first'] = 0; /* to keep track if they have been played already */
90 $GAME['schweinchen-second'] = 0;
94 /* no need to check for Schweinchen */
95 $GAME['schweinchen-who']=NULL;
97 /* end check for Schweinchen */
99 /* put everyting in a form */
100 echo "<form action=\"index.php?action=game&me=$me\" method=\"post\">\n";
104 /* output extra division in case this game is part of a session */
107 echo "<div class=\"session\">\n".
108 "This game is part of session $session: \n";
109 $hashes = DB_get_hashes_by_session($session,$myid);
111 foreach($hashes as $hash)
116 echo "<a href=\"".$INDEX."?action=game&me=".$hash."\">$i</a> \n";
122 /* display the table and the names */
125 /* mystatus gets the player through the different stages of a game.
126 * start: does the player want to play?
127 * init: check for sickness
128 * check: check for return values from init
129 * poverty: handle poverty, wait here until all player have reached this state
130 * display sickness and move on to game
131 * play: game in progress
132 * gameover: are we revisiting a game
139 /* asks the player, if he wants to join the game */
140 output_check_want_to_play($me);
145 /* check the result, if player wants to join, got next stage, else cancel game */
146 if($_REQUEST["in"] == "no")
148 /* cancel the game */
149 $message = "Hello, \n\n".
150 "the game has been canceled due to the request of one of the players.\n";
152 $userids = DB_get_all_userid_by_gameid($gameid);
153 foreach($userids as $user)
155 $To = DB_get_email('userid',$user);
156 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
159 /* delete everything from the dB */
165 /* user wants to join the game */
167 /* move on to the next stage,
168 * no break statement to immediately go to the next stage
171 DB_set_hand_status_by_hash($me,'init');
173 /* check if everyone has reached this stage, send out email */
174 $userids = DB_get_all_userid_by_gameid($gameid);
176 foreach($userids as $user)
178 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
179 if($userstat!='init')
181 /* whos turn is it? */
182 DB_set_player_by_gameid($gameid,$user);
188 /* all done, send out email unless this player is the startplayer */
189 $startplayer = DB_get_startplayer_by_gameid($gameid);
190 if($mypos == $startplayer)
192 /* do nothing, go to next stage */
196 /* email startplayer */
198 $email = DB_get_email('position-gameid',$startplayer,$gameid);
199 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
200 $who = DB_get_userid('email',$email);
201 DB_set_player_by_gameid($gameid,$who);
203 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
204 "Use this link to go the game: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
205 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
212 /* here we ask the player if he is sick */
213 $mycards = DB_get_hand($me);
216 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
217 echo "\n<ul class=\"tricks\">\n";
218 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
219 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
220 " <div class=\"trick\" id=\"trick0\">\n";
222 for($pos=1;$pos<5;$pos++)
224 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
225 $userid = DB_get_userid('gameid-position',$gameid,$pos);
226 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
228 if($userstatus=='start' || $userstatus=='init')
229 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs to decide </div>\n"; /* show this to everyone */
231 if($usersick!=NULL && $pos<=$mypos ) /* only show this for people sitting before the player */
232 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
233 else if($usersick==NULL && $pos<=$mypos)
234 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
236 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
237 /* end displaying sickness */
239 if(!myisset("solo","wedding","poverty","nines") )
241 output_check_for_sickness($me,$mycards);
243 echo "<div class=\"mycards\">Your cards are: <br />\n";
244 foreach($mycards as $card)
245 display_card($card,$PREF["cardset"]);
252 /* check if someone selected more than one sickness */
254 if($_REQUEST["solo"]!="No") $Nsickness++;
255 if($_REQUEST["wedding"] == "yes") $Nsickness++;
256 if($_REQUEST["poverty"] == "yes") $Nsickness++;
257 if($_REQUEST["nines"] == "yes") $Nsickness++;
261 echo "<p class=\"message\"> You selected more than one sickness, please go back ".
262 "and answer the <a href=\"$INDEX?action=game&me=$me&in=yes\">question</a> again.</p>";
264 echo "<div class=\"mycards\">Your cards are: <br />\n";
265 foreach($mycards as $card)
266 display_card($card,$PREF["cardset"]);
273 /* everything is ok, save what user said and proceed */
274 echo "<p class=\"message\">Processing what you selected in the last step...";
276 /* check if this sickness needs to be handled first */
277 $gametype = DB_get_gametype_by_gameid($gameid);
278 $startplayer = DB_get_startplayer_by_gameid($gameid); /* need this to check which solo goes first */
280 if( $_REQUEST["solo"]!="No" )
282 /* user wants to play a solo */
284 /* store the info in the user's hand info */
285 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
286 DB_set_sickness_by_hash($me,"solo");
288 echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
290 if($gametype == "solo" && $startplayer<$mypos)
291 {}/* do nothing, since someone else already is playing solo */
294 /* this solo comes first
295 * store info in game table
297 DB_set_gametype_by_gameid($gameid,"solo");
298 DB_set_startplayer_by_gameid($gameid,$mypos);
299 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
302 else if($_REQUEST["wedding"] == "yes")
304 /* TODO: add silent solo somewhere*/
305 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
306 DB_set_sickness_by_hash($me,"wedding");
308 else if($_REQUEST["poverty"] == "yes")
310 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
311 DB_set_sickness_by_hash($me,"poverty");
313 else if($_REQUEST["nines"] == "yes")
315 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
316 " is playing solo, this game will be canceled.<br />\n";
317 DB_set_sickness_by_hash($me,"nines");
322 /* move on to the next stage*/
323 DB_set_hand_status_by_hash($me,'check');
328 /* here we check what all players said and figure out what game we are playing
329 * this can therefore only be handled once all players finished the last stage
332 /* only need to redisplay the cards when the user reloads the page and lands directly here */
333 if($mystatus=='check')
335 $mycards = DB_get_hand($me);
338 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
339 echo "\n<ul class=\"tricks\">\n";
340 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
341 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
342 " <div class=\"trick\" id=\"trick0\">\n";
344 for($pos=1;$pos<5;$pos++)
346 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
347 $userid = DB_get_userid('gameid-position',$gameid,$pos);
348 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
350 if($userstatus=='start' || $userstatus=='init')
351 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs to decide </div>\n"; /* show this to everyone */
353 if($usersick!=NULL) /* in the init-phase we only showed players with $pos<$mypos, now we can show all */
354 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
356 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
358 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
359 /* end displaying sickness */
362 echo "<div class=\"message\">\n";
363 echo "<p> Checking if someone else selected solo, nines, wedding or poverty.</p>";
365 /* check if everyone has reached this stage */
366 $userids = DB_get_all_userid_by_gameid($gameid);
368 foreach($userids as $user)
370 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
371 if($userstat!='check')
374 DB_set_player_by_gameid($gameid,$user);
380 echo "<p>This step can only be handled after everyone finished the last step. ".
381 "Seems like this is not the case, so you need to wait a bit... ".
382 "you will get an email once that is the case, please use the link in ".
383 "that email to continue the game.</p></div>";
385 /* display cards, if player was just at the init-phase he will still see the cards from there
386 * we can put this one here, since the last player to finish the init state won't get here and
387 * will still see his card anyway from the init-phase
389 if($mystatus=='check')
392 echo "<div class=\"mycards\">Your cards are: <br />\n";
393 foreach($mycards as $card)
394 display_card($card,$PREF["cardset"]);
401 /* Ok, everyone finished the init-phase, time to figure out what game we
402 * are playing, in case there are any solos this already
403 * will have the correct information in it */
405 echo "<p> Ok, everyone is done... figuring out what kind of game we are playing.</p>";
407 $gametype = DB_get_gametype_by_gameid($gameid);
408 $startplayer = DB_get_startplayer_by_gameid($gameid);
410 /* check for sickness */
415 foreach($userids as $user)
417 $name = DB_get_name('userid',$user);
418 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
419 if($usersick == 'nines')
422 break; /* no need to check for other poverties, since only solo can win and that is already set */
424 else if($usersick == 'poverty')
426 else if($usersick == 'wedding')
428 else if($usersick == 'solo')
432 /* now check which sickness comes first and set the gametype to it */
433 if($gametype == "solo")
440 /* TODO: should we keep statistics of this? */
441 $message = "Hello, \n\n".
442 " the game has been canceled because ".DB_get_name('userid',$nines).
443 " has five or more nines and nobody is playing solo.\n\n".
444 " To redeal either start a new game or, in case the game was part of a tournament, \n".
445 " go to the last game and use the link at the bottom of the page to redeal.";
447 $userids = DB_get_all_userid_by_gameid($gameid);
448 foreach($userids as $user)
450 $To = DB_get_email('userid',$user);
451 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
454 /* delete everything from the dB */
457 echo "The game has been canceled because ".DB_get_name('userid',$nines).
458 " has five or more nines and nobody is playing solo.\n";
463 else if($poverty==1) /* one person has poverty */
465 DB_set_gametype_by_gameid($gameid,"poverty");
466 $gametype = "poverty";
467 $who = DB_get_sickness_by_gameid($gameid);
470 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
471 if($firstsick == "poverty")
472 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
474 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
477 else if($poverty==2) /* two people have poverty */
479 DB_set_gametype_by_gameid($gameid,"dpoverty");
480 $gametype = "dpoverty";
481 $who = DB_get_sickness_by_gameid($gameid);
484 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
485 if($firstsick == "poverty")
487 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
488 if($secondsick == "poverty")
489 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
491 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
494 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
499 DB_set_gametype_by_gameid($gameid,"wedding");
500 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
501 $gametype = "wedding";
503 /* now the gametype is set correctly in the database */
504 echo "<p> Got it :)</p>";
506 /* loop over all players, set re/contra if possible and start the game if possible */
507 $userids = DB_get_all_userid_by_gameid($gameid);
508 foreach($userids as $userid)
510 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
515 /* are we the solo player? set us to re, else set us to contra */
516 $pos = DB_get_pos_by_hash($userhash);
517 if($pos == $startplayer)
518 DB_set_party_by_hash($userhash,"re");
520 DB_set_party_by_hash($userhash,"contra");
521 DB_set_hand_status_by_hash($userhash,'play');
525 /* set person with the wedding to re, do the rest during the game */
526 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
527 if($usersick == "wedding")
528 DB_set_party_by_hash($userhash,"re");
530 DB_set_party_by_hash($userhash,"contra");
532 DB_set_hand_status_by_hash($userhash,'play');
536 $hand = DB_get_all_hand($userhash);
538 if(in_array('3',$hand)||in_array('4',$hand))
539 DB_set_party_by_hash($userhash,"re");
541 DB_set_party_by_hash($userhash,"contra");
542 DB_set_hand_status_by_hash($userhash,'play');
546 /* set person with poverty to play status */
547 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
548 if($usersick == "poverty")
549 DB_set_hand_status_by_hash($userhash,'play');
551 /* set status of first player to be asked to poverty */
552 $who = DB_get_sickness_by_gameid($gameid);
553 if($who > 6) $who= $who/10; /* in case we have dpoverty */
554 $whoid = DB_get_userid('gameid-position',$gameid,$who);
556 DB_set_hand_status_by_hash($userhash,'poverty');
559 /* check for silent solo, set game type to solo in this case */
560 $gametype = DB_get_gametype_by_gameid($gameid);
561 $userids = DB_get_all_userid_by_gameid($gameid);
562 foreach($userids as $userid)
564 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
566 if($gametype=='normal')
568 $userhand = DB_get_all_hand($userhash);
569 if(check_wedding($userhand))
571 /* normal game type and player has both queens -> silent solo */
572 /* keep startplayer, just set gametype to silent solo */
573 DB_set_gametype_by_gameid($gameid,"solo");
574 DB_set_solo_by_gameid($gameid,'silent');
579 /* send out email to first player or poverty person*/
580 if($gametype!="poverty" && $gametype!="dpoverty")
582 $startplayer = DB_get_startplayer_by_gameid($gameid);
583 $email = DB_get_email('position-gameid',$startplayer,$gameid);
584 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
585 $who = DB_get_userid('email',$email);
586 DB_set_player_by_gameid($gameid,$who);
590 if(DB_get_email_pref_by_hash($hash)!="emailaddict")
592 /* email startplayer */
593 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
594 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
595 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
599 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
603 /* set status of first player to be asked to poverty */
604 $who = DB_get_sickness_by_gameid($gameid);
605 if($who > 6) $who= $who/10; /* in case we have dpoverty */
607 $whoid = DB_get_userid('gameid-position',$gameid,$who);
609 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
612 $email = DB_get_email('position-gameid',$who,$gameid);
613 $whohash = DB_get_hash_from_game_and_pos($gameid,$who);
614 DB_set_player_by_gameid($gameid,$whoid);
616 if(DB_get_email_pref_by_hash($hash)!="emailaddict")
618 /* email player for poverty */
619 $message = "Poverty: It's your turn now in game ".DB_format_gameid($gameid).".\n".
620 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$whohash."\n\n" ;
621 mymail($email,$EmailName."Poverty (game ".DB_format_gameid($gameid).") ",$message);
629 /* user only gets here in a poverty game, several things have to be handled here:
630 * A) ask, if user wants to take trump
632 * poverty: set re/contra
633 * dpoverty: first time: set re, send email to second player
634 * second time: set contra
635 * set status to play in case 0 trump
636 * no -> set status to play,
637 * ask next player or cancle the game if no more players
638 * B) user took trump and has too many cards (e.g. count(cards)>12 and re/contra set)
639 * ask to give cards back, set status to play, once player has 12 cards
641 * it is easier to check B) first
644 /* output pre game in case user reloads */
647 set_gametype($gametype); /* this sets the $CARDS variable */
648 $myparty = DB_get_party_by_hash($me);
650 /* the following is part B) of whats needs to be done)
651 /* check if user wants to give cards back */
652 if(myisset("exchange"))
654 $exchange = $_REQUEST['exchange'];
655 $partnerhash = DB_get_partner_hash_by_hash($me);
656 $partnerid = DB_get_userid('hash',$partnerhash);
657 $partnerhand = DB_get_handid('gameid-userid',$gameid,$partnerid);
659 /* if exchange is set to a value>0, exchange that card back to the partner */
662 $result = DB_query("UPDATE Hand_Card SET hand_id='$partnerhand'".
663 " WHERE hand_id='$myhand' AND card_id=".DB_quote_smart($exchange));
667 $mycards = DB_get_hand($me);
668 $mycards = mysort($mycards,$gametype);
670 /* check if user need to give more cards back */
671 if( ($myparty=='re' || $myparty=='contra') && count($mycards)>12)
673 echo "<div class=\"poverty\"> you need to get rid of a few cards</div>\n";
676 echo "<div class=\"mycards\">Your cards are: <br />\n";
677 foreach($mycards as $card)
678 display_link_card($card,$PREF["cardset"],$type);
679 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select card to give back\" />\n";
682 else if( ($myparty=='re' || $myparty=='contra') && count($mycards)==12)
684 /* user is done, ready to play */
685 DB_set_hand_status_by_hash($me,'play');
686 /* TODO if resolved; email start player, set startplayer */
689 /* the following is part A) of what needs to be done */
690 if(!myisset("trump"))
694 echo "<div class=\"poverty\">\n";
695 $userids = DB_get_all_userid_by_gameid($gameid);
696 foreach($userids as $user)
698 $name = DB_get_name('userid',$user);
699 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
700 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
701 $userparty = DB_get_party_by_hash($userhash);
703 if($usersick=="poverty" && !$userparty)
705 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
706 $cards = DB_get_hand($hash);
709 foreach($cards as $card)
710 if($card<27) $nrtrump++;
711 echo "Player $name has $nrtrump trump. Do you want to take them?".
712 "<a href=\"index.php?action=game&me=$me&trump=$user\">yes</a> <br />\n";
715 echo "<a href=\"index.php?action=game&me=$me&trump=no\">No,way I take those trump...</a> <br />\n";
716 echo "</div><div>\n";
718 echo "<div class=\"mycards\">Your cards are: <br />\n";
719 foreach($mycards as $card)
720 display_card($card,$PREF["cardset"]);
721 echo "</div></div>\n";
727 $trump = $_REQUEST['trump'];
731 /* user doesn't want to take trump */
732 DB_set_hand_status_by_hash($me,'play');
734 /* set next player who needs to be asked and email him*/
735 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
736 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
738 /* don't ask people who have poverty */
740 if($firstsick=="poverty")
742 if($secondsick=="poverty")
747 if($gametype=="dpoverty")
749 $next=999; /* need to cancel for sure, since both would need to take the trump */
752 /* no more people to ask, need to cancel the game */
755 $message = "Hello, \n\n".
756 "Game ".DB_format_gameid($gameid)." has been canceled since nobody wanted to take the trump.\n";
758 $userids = DB_get_all_userid_by_gameid($gameid);
759 foreach($userids as $user)
761 $To = DB_get_email('userid',$user);
762 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled (poverty not resolved)",$message);
765 /* delete everything from the dB */
768 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been canceled.<br /><br /></p>";
775 /* email next player, set his status to poverty */
776 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
777 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
778 $userid = DB_get_userid('email',$To);
780 DB_set_player_by_gameid($gameid,$userid);
781 DB_set_hand_status_by_hash($userhash,'poverty');
783 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
784 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
785 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
790 /* player wants to take trump, change cards */
792 /* user wants to take trump */
793 $trump = $_REQUEST["trump"];
794 $userhand = DB_get_handid('gameid-userid',$gameid,$trump);
795 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
797 /* copy trump from player A to B */
798 $result = DB_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
800 $mycards = DB_get_hand($me);
803 if($gametype=='poverty')
805 $userids = DB_get_all_userid_by_gameid($gameid);
806 foreach($userids as $user)
808 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
809 if($hash==$userhash||$hash==$me)
810 DB_set_party_by_hash($hash,"re");
812 DB_set_party_by_hash($hash,"contra");
814 /* check if we are done, if so, send everyone into the 'play' phase */
815 if(count($mycards)==12)
817 foreach($userids as $user)
819 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
820 DB_set_hand_status_by_hash($hash,'play');
826 /* has the re party already been set?*/
828 $userids = DB_get_all_userid_by_gameid($gameid);
829 foreach($userids as $user)
831 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
832 $party = DB_get_party_by_hash($hash);
838 DB_set_party_by_hash($me,'contra');
839 DB_set_party_by_hash($userhash,'contra');
843 DB_set_party_by_hash($me,'re');
844 DB_set_party_by_hash($userhash,'re');
846 /* send out email to second non-poverty player */
847 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
848 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
851 if($firstsick=="poverty")
852 if($secondsick=="poverty")
858 echo "<div class=\"message\">Error in poverty, please contact the Admin</div>\n";
860 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
861 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
862 $userid = DB_get_userid('email',$To);
864 DB_set_player_by_gameid($gameid,$userid);
865 DB_set_hand_status_by_hash($userhash,'poverty');
867 $message = "Two people have poverty, it's your turn to decide, if you want to take the trump. Please visit:".
868 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
869 mymail($To,$EmailName." double poverty (game ".DB_format_gameid($gameid).")",$message);
874 echo "<div class=\"message\"> Please, <a href=\"$INDEX?action=game&me=$me\">continue</a> here.</div>\n";
882 /* both entries here, so that the tricks are visible for both.
883 * in case of 'play' there is a break later that skips the last part
886 /* check if all players are ready to play,
887 * if so, send out email to the startplayer
888 * only need to do this if the game hasn't started yet
890 $gamestatus = DB_get_game_status_by_gameid($gameid);
891 if($gamestatus == 'pre')
894 $userids = DB_get_all_userid_by_gameid($gameid);
895 foreach($userids as $user)
897 $userstatus = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
898 if($userstatus !='play' && $userstatus!='gameover')
901 DB_set_player_by_gameid($gameid,$user);
906 /* only set this after all poverty, etc. are handled*/
907 DB_set_game_status_by_gameid($gameid,'play');
909 /* email startplayer */
910 $startplayer = DB_get_startplayer_by_gameid($gameid);
911 $email = DB_get_email('position-gameid',$startplayer,$gameid);
912 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
913 $who = DB_get_userid('email',$email);
914 DB_set_player_by_gameid($gameid,$who);
916 if($hash!=$me && DB_get_email_pref_by_hash($hash)!="emailaddict")
918 /* email startplayer) */
919 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
920 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
921 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
925 /* figure out what kind of game we are playing,
926 * set the global variables $CARDS["trump"],$CARDS["diamonds"],$CARDS["hearts"],
927 * $CARDS["clubs"],$CARDS["spades"],$CARDS["foxes"]
931 $gametype = DB_get_gametype_by_gameid($gameid);
933 if($gametype=="solo")
935 $gametype = DB_get_solo_by_gameid($gameid);
936 if($gametype=='silent')
939 $GT = $gametype." ".$GT;
942 $gametype = "normal";
944 set_gametype($gametype); /* this sets the $CARDS variable */
946 /* get some infos about the game, need to reset this, since it might have changed */
947 $gamestatus = DB_get_game_status_by_gameid($gameid);
949 /* has the game started? No, then just wait here...*/
950 if($gamestatus == 'pre')
952 echo "<p class=\"message\"> You finished the setup, but not everyone else finished it... ".
953 "You need to wait for the others. Just wait for an email. </p>";
955 $mycards = DB_get_hand($me);
956 $mycards = mysort($mycards,$gametype);
958 echo "<div class=\"mycards\">Your cards are: <br />\n";
959 foreach($mycards as $card)
960 display_card($card,$PREF["cardset"]);
963 break; /* not sure this works... the idea is that you can
964 * only play a card after everyone is ready to play */
967 /* get time from the last action of the game */
968 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
969 $gameend = time() - strtotime($r[0]);
971 /* handle comments in case player didn't play a card, allow comments a week after the end of the game */
972 if( (!myisset("card") && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
973 if(myisset("comment"))
975 $comment = $_REQUEST["comment"];
976 $playid = DB_get_current_playid($gameid);
979 DB_insert_comment($comment,$playid,$myid);
982 /* handle notes in case player didn't play a card, allow notes only during a game */
983 if( (!myisset("card") && $mystatus=='play') )
986 $note = $_REQUEST["note"];
989 DB_insert_note($note,$gameid,$myid);
992 /* get everything relevant to display the tricks */
993 $result = DB_query("SELECT Hand_Card.card_id as card,".
994 " Hand.position as position,".
995 " Play.sequence as sequence, ".
997 " GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
998 " SEPARATOR '\n' ), ".
999 " Play.create_date, ".
1002 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1003 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1004 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1005 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1006 "LEFT JOIN User On User.id=Comment.user_id ".
1007 "WHERE Trick.game_id='".$gameid."' ".
1008 "GROUP BY Trick.id, sequence ".
1009 "ORDER BY Trick.id, sequence ASC");
1011 $lasttrick = DB_get_max_trickid($gameid);
1013 $play = array(); /* needed to calculate winner later */
1015 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1016 $firstcard = ""; /* first card in a trick */
1018 echo "\n<ul class=\"tricks\">\n";
1019 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
1021 /* output vorbehalte */
1022 $mygametype = DB_get_gametype_by_gameid($gameid);
1023 if($mygametype != 'normal' && $mygametype != 'silent') /* only show when needed */
1025 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
1026 " <div class=\"trick\" id=\"trick0\">\n";
1028 for($mypos=1;$mypos<5;$mypos++)
1030 $usersick = DB_get_sickness_by_pos_and_gameid($mypos,$gameid);
1033 echo " <div class=\"vorbehalt".($mypos-1)."\"> Vorbehalt <br />";
1035 echo " $usersick <br />";
1038 if($mygametype == $usersick)
1042 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1046 while($r = DB_fetch_array($result))
1054 /* check if first schweinchen has been played */
1055 if( $GAME['schweinchen-who'] && ($r[0] == 19 || $r[0] == 20) )
1056 if(!$GAME['schweinchen-first'])
1057 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1059 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1061 /* save card to be able to find the winner of the trick later */
1062 $play[$seq] = array("card"=>$r[0],"pos"=>$pos);
1066 /* first card in a trick, output some html */
1067 if($trick!=$lasttrick)
1069 /* start of an old trick? */
1070 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1071 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1072 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1074 else if($trick==$lasttrick)
1076 /* start of a last trick? */
1077 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1078 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1079 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1082 /* remember first card, so that we are able to check, what cards can be played */
1087 echo " <div class=\"card".($pos-1)."\">\n";
1089 /* display comments */
1091 echo " <span class=\"comment\">".$comment."</span>\n";
1094 display_card($r[0],$PREF["cardset"]);
1096 echo " </div>\n"; /* end div card */
1102 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1106 /* whos turn is it? */
1109 $winner = get_winner($play,$gametype); /* returns the position */
1111 $firstcard = ""; /* new trick, no first card */
1116 if($next==5) $next = 1;
1119 /* my turn?, display cards as links, ask for comments*/
1120 if(DB_get_pos_by_hash($me) == $next)
1125 /* do we want to play a card? */
1126 if(myisset("card") && $myturn)
1128 $card = $_REQUEST["card"];
1129 $handid = DB_get_handid('hash',$me);
1131 /* check if we have card and that we haven't played it yet*/
1132 /* set played in hand_card to true where hand_id and card_id*/
1133 $r = DB_query_array("SELECT id FROM Hand_Card WHERE played='false' and ".
1134 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1135 $handcardid = $r[0];
1137 if($handcardid) /* everything ok, play card */
1139 /* update Game timestamp */
1140 DB_update_game_timestamp($gameid);
1142 /* check if a call was made, must do this before we set the card status to played */
1143 if(myisset("call") && $_REQUEST["call"] == "120" && can_call(120,$me))
1144 $result = DB_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
1145 if(myisset("call") && $_REQUEST["call"] == "90" && can_call(90,$me))
1146 $result = DB_query("UPDATE Hand SET point_call='90' WHERE hash='$me' ");
1147 if(myisset("call") && $_REQUEST["call"] == "60" && can_call(60,$me))
1148 $result = DB_query("UPDATE Hand SET point_call='60' WHERE hash='$me' ");
1149 if(myisset("call") && $_REQUEST["call"] == "30" && can_call(30,$me))
1150 $result = DB_query("UPDATE Hand SET point_call='30' WHERE hash='$me' ");
1151 if(myisset("call") && $_REQUEST["call"] == "0" && can_call(0,$me))
1152 $result = DB_query("UPDATE Hand SET point_call='0' WHERE hash='$me' ");
1154 /* mark card as played */
1155 DB_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1156 DB_quote_smart($card));
1158 /* get trick id or start new trick */
1159 $a = DB_get_current_trickid($gameid);
1164 $playid = DB_play_card($trickid,$handcardid,$sequence);
1166 /* check special output for schweinchen in case in case a fox is being played
1167 * check for correct rules, etc. has already been done
1169 if( $GAME["schweinchen-who"] && ($card == 19 || $card == 20) )
1171 if(!$GAME['schweinchen-first'])
1172 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1174 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1176 if( ($GAME['schweinchen-second']==1 && $RULES['schweinchen']=='second') || $RULES['schweinchen']=='both')
1178 DB_insert_comment("Schweinchen! ",$playid,$myid);
1179 $commentSchweinchen = "Schweinchen! ";
1182 echo "schweinchen = ".$GAME["schweinchen-who"]." ---<br />";
1185 /* if sequence == 4 check who one in case of wedding */
1186 if($sequence == 4 && $GT == "wedding")
1188 /* is wedding resolve */
1189 $resolved = DB_get_sickness_by_gameid($gameid);
1192 /* who has wedding */
1193 $userids = DB_get_all_userid_by_gameid($gameid);
1194 foreach($userids as $user)
1196 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1197 if($usersick == "wedding")
1200 /* who won the trick */
1201 $play = DB_get_cards_by_trick($trickid);
1202 $winner = get_winner($play,$gametype); /* returns the position */
1203 $winnerid = DB_get_userid('gameid-position',$gameid,$winner);
1204 /* is tricknr <=3 */
1205 if($tricknr <=3 && $winnerid!=$whosick)
1207 /* set resolved at tricknr*/
1208 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1210 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1211 DB_set_party_by_hash($whash,"re");
1213 if($tricknr == 3 && $winnerid==$whosick)
1215 /* set resolved at tricknr*/
1216 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1221 /* if sequence == 4, set winner of the trick, count points and set the next player */
1224 $play = DB_get_cards_by_trick($trickid);
1225 $winner = get_winner($play,$gametype); /* returns the position */
1227 /* check if someone caught a fox */
1228 /* first check if we should account for solos at all,
1229 * since it doesn't make sense in some games
1231 $ok = 0; /* fox shouldn't be counted */
1232 if(DB_get_gametype_by_gameid($gameid)=="solo")
1234 $solo = DB_get_solo_by_gameid($gameid);
1235 if($solo == 'trump' || $solo == 'silent')
1236 $ok = 1; /* for trump solos and silent solos, foxes are ok */
1239 $ok = 1; /* for all other games (not solos) foxes are ok too */
1242 foreach($play as $played)
1244 if ( $played['card']==19 || $played['card']==20 )
1245 if ($played['pos']!= $winner )
1247 /* possible caught a fox, check party */
1248 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1249 $uid2 = DB_get_userid('gameid-position',$gameid,$played['pos']);
1251 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1252 $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1254 if($party1 != $party2)
1255 DB_query("INSERT INTO Score".
1256 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1260 /* check for karlchen (jack of clubs in the last trick)*/
1261 /* same as for foxes, karlchen doesn't always make sense
1262 * check what kind of game it is and set karlchen accordingly */
1263 $ok = 1; /* default: karlchen should be accounted for */
1265 $ok = 0; /* Karlchen works only in the last trick */
1266 if($ok && DB_get_gametype_by_gameid($gameid)=="solo" )
1268 $solo = DB_get_solo_by_gameid($gameid);
1269 if($solo == "trumpless" || $solo == "jack" || $solo == "queen" )
1270 $ok = 0; /* no Karlchen in these solos */
1274 foreach($play as $played)
1275 if ( $played['card']==11 || $played['card']==12 )
1276 if ($played['pos'] == $winner )
1278 /* possible caught a fox, check party */
1279 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1280 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1282 DB_query("INSERT INTO Score".
1283 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1285 /* check for doppelopf (>40 points)*/
1287 foreach($play as $played)
1289 $points += DB_get_card_value_by_cardid($played['card']);
1293 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1294 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1296 DB_query("INSERT INTO Score".
1297 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
1301 DB_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1303 echo "ERROR during scoring";
1306 echo "DEBUG: position $winner won the trick <br />";
1308 /* who is the next player? */
1313 $next = DB_get_pos_by_hash($me)+1;
1315 if($next==5) $next=1;
1317 /* check for coment */
1318 if(myisset("comment"))
1320 $comment = $_REQUEST["comment"];
1322 DB_insert_comment($comment,$playid,$myid);
1323 if($commentSchweinchen)
1324 $comment = $commentSchweinchen . $comment;
1327 /* check for note */
1330 $note = $_REQUEST["note"];
1332 DB_insert_note($note,$gameid,$myid);
1335 /* display played card */
1336 $pos = DB_get_pos_by_hash($me);
1339 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1340 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1341 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1344 echo " <div class=\"card".($pos-1)."\">\n ";
1346 /* display comments */
1347 display_card($card,$PREF["cardset"]);
1349 echo "\n <span class=\"comment\"> ".$comment."</span>\n";
1352 /*check if we still have cards left, else set status to gameover */
1353 if(sizeof(DB_get_hand($me))==0)
1355 DB_set_hand_status_by_hash($me,'gameover');
1356 $mystatus = 'gameover';
1359 /* if all players are done, set game status to game over,
1360 * get the points of the last trick and send out an email
1363 $userids = DB_get_all_userid_by_gameid($gameid);
1366 foreach($userids as $user)
1367 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1371 DB_set_game_status_by_gameid($gameid,"gameover");
1373 /* email next player, if game is still running */
1374 if(DB_get_game_status_by_gameid($gameid)=='play')
1376 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1377 $email = DB_get_email('hash',$next_hash);
1378 $who = DB_get_userid('email',$email);
1379 DB_set_player_by_gameid($gameid,$who);
1381 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1382 "It's your turn now.\n".
1383 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$next_hash."\n\n" ;
1384 if( DB_get_email_pref_by_uid($who)!="emailaddict" )
1385 mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
1387 else /* send out final email */
1389 /* individual score */
1390 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1391 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1392 " LEFT JOIN User ON User.id=Hand.user_id".
1393 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1394 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1395 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1396 " WHERE Hand.game_id='$gameid'".
1397 " GROUP BY User.fullname" );
1398 $message = "The game is over. Thanks for playing :)\n";
1399 $message .= "Final score:\n";
1400 while( $r = DB_fetch_array($result) )
1401 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1403 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1404 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1405 " LEFT JOIN User ON User.id=Hand.user_id".
1406 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1407 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1408 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1409 " WHERE Hand.game_id='$gameid'".
1410 " GROUP BY Hand.party" );
1411 $message .= "\nTotals:\n";
1414 while( $r = DB_fetch_array($result) )
1416 $message .= " ".$r[0]." ".$r[1]."\n";
1419 else if($r[0] == "contra")
1424 * save score in database
1428 /* get calls from re/contra */
1430 $call_contra = NULL;
1431 foreach($userids as $user)
1433 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1434 $call = DB_get_call_by_hash($hash);
1435 $party = DB_get_party_by_hash($hash);
1439 $call = (int) $call;
1445 else if( $call < $call_re)
1448 else if($party=="contra")
1450 if($call_contra==NULL)
1451 $call_contra = $call;
1452 else if( $call < $call_re)
1453 $call_contra = $call;
1458 /* figure out who one */
1459 $winning_party = NULL;
1461 if($call_re == NULL && $call_contra==NULL)
1463 /* nobody made a call, so it's easy to figure out who won */
1465 $winning_party="re";
1467 $winning_party="contra";
1471 /* if one party makes a call, they only win, iff they make enough points
1472 * if only one party made a call, the other one wins,
1473 * if the first one didn't make it
1477 $offset = 120 - $call_re;
1479 $offset--; /* since we use a > in the next equation */
1481 if($re > 120+$offset)
1482 $winning_party="re";
1483 else if ($call_contra == NULL )
1484 $winning_party="contra";
1489 $offset = 120 - $call_contra;
1490 if($call_contra == 0)
1491 $offset--; /* since we use a > in the next equation */
1493 if($contra > 120+$offset)
1494 $winning_party="contra";
1495 else if ($call_re == NULL )
1496 $winning_party="re";
1500 /* one point for each call of the other party in case the other party didn't win
1501 * and one point each in case the party made more than points than one of the calls
1503 if($winning_party!="contra" && $call_contra!=NULL)
1505 for( $p=$call_contra;$p<=120; $p+=30 )
1507 DB_query("INSERT INTO Score".
1508 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
1511 for( $p=$call_contra; $p<120; $p+=30)
1514 DB_query("INSERT INTO Score".
1515 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'made$p')");
1518 if($winning_party!="re" and $call_re!=NULL)
1520 for( $p=$call_re;$p<=120; $p+=30 )
1522 DB_query("INSERT INTO Score".
1523 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'against$p')");
1526 for( $p=$call_re; $p<120; $p+=30)
1529 DB_query("INSERT INTO Score".
1530 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
1534 /* point in case contra won */
1535 if($winning_party=="contra")
1537 DB_query("INSERT INTO Score".
1538 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1541 /* one point each for winning and each 30 points + calls */
1542 if($winning_party=="re")
1544 foreach(array(120,150,180,210,240) as $p)
1547 if($p==240 || $call_contra!=NULL)
1551 DB_query("INSERT INTO Score".
1552 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
1554 /* re called something and won */
1555 foreach(array(0,30,60,90,120) as $p)
1557 if($call_re!=NULL && $call_re<$p+1)
1558 DB_query("INSERT INTO Score".
1559 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
1562 else if( $winning_party=="contra")
1564 foreach(array(120,150,180,210,240) as $p)
1567 if($p==240 || $call_re!=NULL)
1570 if($contra>$p-$offset)
1571 DB_query("INSERT INTO Score".
1572 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
1574 /* re called something and won */
1575 foreach(array(0,30,60,90,120) as $p)
1577 if($call_contra!=NULL && $call_contra<$p+1)
1578 DB_query("INSERT INTO Score".
1579 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
1584 /* add score points to email */
1587 $message .= " Points Re: \n";
1588 $queryresult = DB_query("SELECT score FROM Score ".
1589 " WHERE game_id=$gameid AND party='re'".
1591 while($r = DB_fetch_array($queryresult) )
1593 $message .= " ".$r[0]."\n";
1596 $message .= " Points Contra: \n";
1597 $queryresult = DB_query("SELECT score FROM Score ".
1598 " WHERE game_id=$gameid AND party='contra'".
1600 while($r = DB_fetch_array($queryresult) )
1602 $message .= " ".$r[0]."\n";
1605 $message .= " Total Points (from the Re point of view): $Tpoint\n";
1608 $session = DB_get_session_by_gameid($gameid);
1609 $score = generate_score_table($session);
1610 /* convert html to ascii */
1611 $score = str_replace("<div class=\"scoretable\">\n<table class=\"score\">\n <tr>\n","",$score);
1612 $score = str_replace("</table></div>\n","",$score);
1613 $score = str_replace("\n","",$score);
1614 $score = str_replace(array("<tr>","</tr>","<td>","</td>"),array("","\n","","|"),$score);
1615 $score = explode("\n",$score);
1617 $header = array_slice($score,0,1);
1618 $header = explode("|",$header[0]);
1619 for($i=0;$i<sizeof($header);$i++)
1620 $header[$i]=str_pad($header[$i],6," ",STR_PAD_BOTH);
1621 $header = implode("|",$header);
1622 $header.= "\n------+------+------+------+------+\n";
1623 if(sizeof($score)>5) $header.= " ... \n";
1625 if(sizeof($score)>5) $score = array_slice($score,-5,5);
1626 for($i=0;$i<sizeof($score);$i++)
1628 $line = explode("|",$score[$i]);
1629 for($j=0;$j<sizeof($line);$j++)
1630 $line[$j]=str_pad($line[$j],6," ",STR_PAD_LEFT);
1631 $score[$i] = implode("|",$line);
1634 $score = implode("\n",$score);
1635 $score = $header.$score;
1637 $message .= "Score Table:\n";
1640 /* send out final email */
1643 foreach($userids as $user)
1644 $all[] = DB_get_email('userid',$user);
1645 $To = implode(",",$all);
1647 $help = "\n\n (you can use reply all on this email to reach all the players.)\n";
1648 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 1(2)",$message.$help);
1650 foreach($userids as $user)
1652 $To = DB_get_email('userid',$user);
1653 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1655 $link = "Use this link to have a look at game ".DB_format_gameid($gameid).": ".
1656 $HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
1657 if( DB_get_email_pref_by_uid($user) != "emailaddict" )
1658 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 2(2)",$link);
1664 echo "can't find that card?! <br />\n";
1667 else if(myisset("card") && !$myturn )
1669 echo "please wait until it's your turn! <br />\n";
1672 if($seq!=4 && $trickNR>1)
1673 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1675 /* display points in case game is over */
1676 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1678 echo " <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1679 " <div class=\"trick\" id=\"trick13\">\n";
1680 /* add pic for re/contra
1681 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1683 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1684 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1685 " LEFT JOIN User ON User.id=Hand.user_id".
1686 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1687 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1688 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1689 " WHERE Hand.game_id='$gameid'".
1690 " GROUP BY User.fullname" );
1691 while( $r = DB_fetch_array($result))
1692 echo " <div class=\"card".($r[3]-1)."\">\n".
1693 " <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
1696 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1700 echo "</ul>\n"; /* end ul tricks*/
1702 echo "<div class=\"notes\"> Personal notes: <br />\n";
1703 $notes = DB_get_notes_by_userid_and_gameid($myid,$gameid);
1704 foreach($notes as $note)
1705 echo "$note <hr \>\n";
1706 echo "Insert note:<input name=\"note\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1709 $mycards = DB_get_hand($me);
1710 $mycards = mysort($mycards,$gametype);
1711 echo "<div class=\"mycards\">\n";
1713 if($myturn && !myisset("card") && $mystatus=='play' )
1715 echo "Hello ".$myname.", it's your turn! <br />\n";
1716 echo "Your cards are: <br />\n";
1718 /* do we have to follow suite? */
1720 if(have_suit($mycards,$firstcard))
1723 foreach($mycards as $card)
1725 if($followsuit && !same_type($card,$firstcard))
1726 display_card($card,$PREF["cardset"]);
1728 display_link_card($card,$PREF["cardset"]);
1731 else if($mystatus=='play' )
1733 echo "Your cards are: <br />\n";
1734 foreach($mycards as $card)
1735 display_card($card,$PREF["cardset"]);
1737 else if($mystatus=='gameover')
1739 $oldcards = DB_get_all_hand($me);
1740 $oldcards = mysort($oldcards,$gametype);
1741 echo "Your cards were: <br />\n";
1742 foreach($oldcards as $card)
1743 display_card($card,$PREF["cardset"]);
1745 $userids = DB_get_all_userid_by_gameid($gameid);
1746 foreach($userids as $user)
1748 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1754 $name = DB_get_name('userid',$user);
1755 $oldcards = DB_get_all_hand($userhash);
1756 $oldcards = mysort($oldcards,$gametype);
1757 echo "$name's cards were: <br />\n";
1758 foreach($oldcards as $card)
1759 display_card($card,$PREF["cardset"]);
1765 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1766 if($mystatus=='play')
1769 /* the following happens only when the gamestatus is 'gameover' */
1770 /* check if game is over, display results */
1771 if(DB_get_game_status_by_gameid($gameid)=='play')
1773 echo "The game is over for you.. other people still need to play though";
1777 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1778 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1779 " LEFT JOIN User ON User.id=Hand.user_id".
1780 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1781 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1782 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1783 " WHERE Hand.game_id='$gameid'".
1784 " GROUP BY Hand.party" );
1785 echo "<div class=\"total\"> Totals:<br />\n";
1786 while( $r = DB_fetch_array($result))
1787 echo " ".$r[0]." ".$r[1]."<br />\n";
1789 $queryresult = DB_query("SELECT timediff(mod_date,create_date) ".
1790 " FROM Game WHERE id='$gameid'");
1791 $r = DB_fetch_array($queryresult);
1792 echo "<p>This game took ".$r[0]." hours.</p>";
1794 echo "<div class=\"re\">\n Points Re: <br />\n";
1795 $queryresult = DB_query("SELECT score FROM Score ".
1796 " WHERE game_id=$gameid AND party='re'".
1798 while($r = DB_fetch_array($queryresult) )
1799 echo " ".$r[0]."<br />\n";
1802 echo "<div class=\"contra\">\n Points Contra: <br />\n";
1803 $queryresult = DB_query("SELECT score FROM Score ".
1804 " WHERE game_id=$gameid AND party='contra'".
1806 while($r = DB_fetch_array($queryresult) )
1807 echo " ".$r[0]."<br />\n";
1816 myerror("error in testing the status");
1818 /* output left menu */
1819 display_user_menu();
1821 /* output right menu */
1823 /* display rule set for this game */
1824 echo "<div class=\"gameinfo\">\n";
1826 if($gamestatus != 'pre')
1827 echo " Gametype: $GT <br />\n";
1829 echo "Rules: <br />\n";
1830 echo "10ofhearts : ".$RULES["dullen"] ."<br />\n";
1831 echo "schweinchen: ".$RULES["schweinchen"] ."<br />\n";
1832 echo "call: ".$RULES["call"] ."<br />\n";
1835 if($gamestatus == 'play' )
1836 output_form_calls($me);
1838 /* get time from the last action of the game */
1839 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1840 $gameend = time() - strtotime($r[0]);
1842 if($gamestatus == 'play' || $gameend < 60*60*24*7)
1844 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1848 echo "<input type=\"submit\" value=\"submit\" />\n<hr />\n";
1850 $session = DB_get_session_by_gameid($gameid);
1851 $score = generate_score_table($session);
1859 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1862 $session = DB_get_session_by_gameid($gameid);
1863 $result = DB_query("SELECT id,create_date FROM Game".
1864 " WHERE session=$session".
1865 " ORDER BY create_date DESC".
1869 $r = DB_fetch_array($result);
1871 if(!$session || $gameid==$r[0])
1873 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
1874 $names = DB_get_all_names_by_gameid($gameid);
1875 $type = DB_get_gametype_by_gameid($gameid);
1879 $solo = DB_get_solo_by_gameid($gameid);
1881 if($solo!='silent') /* repeat game with same first player */
1882 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
1883 else /* rotate normally */
1884 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
1886 else /* rotate normally */
1887 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);