2 /* make sure that we are not called from outside the scripts,
3 * use a variable defined in config.php to check this
8 /* calling game.php only makes sense when we give it a hash for a game */
11 echo "Hmm, you really shouldn't mess with the urls.<br />\n";
16 $me = $_REQUEST["me"];
18 /* Ok, got a hash, but is it valid? */
19 $myid = DB_get_userid('hash',$me);
22 echo "Can't find you in the database, please check the url.<br />\n";
23 echo "perhaps the game has been canceled, check by login in <a href=\"$INDEX\">here</a>.";
29 global $GAME,$RULES,$CARDS;
31 /* user might get here by clicking on the link in an email, so session might not be set */
32 if(isset($_SESSION["name"]))
33 output_status($_SESSION["name"]);
35 /* the user has done something, update the timestamp */
36 DB_update_user_timestamp($myid);
38 /* get some information from the DB */
39 $gameid = DB_get_gameid_by_hash($me);
40 $myname = DB_get_name('hash',$me);
41 $mystatus = DB_get_status_by_hash($me);
42 $mypos = DB_get_pos_by_hash($me);
43 $myhand = DB_get_handid('hash',$me);
44 $session = DB_get_session_by_gameid($gameid);
46 /* get prefs and save them in a variable*/
47 $PREF = DB_get_PREF($myid);
49 /* get rule set for this game */
50 $RULES = DB_get_RULES($gameid);
52 /* get some infos about the game */
53 $gametype = DB_get_gametype_by_gameid($gameid);
54 $gamestatus = DB_get_game_status_by_gameid($gameid);
58 $gametype = DB_get_solo_by_gameid($gameid);
59 $GT = $gametype." ".$GT;
62 /* do we need to worry about Schweinchen?
63 * check gametype and rules
64 * if yes, figure out if someone actually has Schweinchen
65 * save information in $GAME
68 if( $gamestatus == 'pre' )
70 /* always need to use Schweinchen to figure out for example who has poverty */
75 /* in a game Schweinchen is not valid in all types of games */
76 if( $gametype == 'normal' || $gametype == 'silent' || $gametype=='trump' )
77 if( in_array($RULES['schweinchen'],array('both','second','secondaftercall')) )
81 /* these are the defaults */
82 $GAME['schweinchen-who'] = NULL;
83 $GAME['schweinchen-first'] = NULL;
84 $GAME['schweinchen-second'] = NULL;
88 /* need to check for Schweinchen */
91 $hash = DB_get_hash_from_game_and_pos($gameid,$i);
92 $cards = DB_get_all_hand($hash);
93 if( in_array("19",$cards) && in_array("20",$cards) )
94 $GAME['schweinchen-who']=$hash;
96 $GAME['schweinchen-first'] = 0; /* to keep track if they have been played already */
97 $GAME['schweinchen-second'] = 0;
99 /* end check for Schweinchen */
101 /* set the $CARDS variable, needed for sorting the cards
102 * we set it to normal so that the pre-game phase is handled ok
103 * and later set it to the correct game type that is played
105 set_gametype('normal');
107 /* put everyting in a form */
108 echo "<form action=\"index.php?action=game&me=$me\" method=\"post\">\n";
110 /* handle user notes (only possible while game is running)*/
111 if( $mystatus!='gameover' )
114 $note = $_REQUEST['note'];
117 DB_insert_note($note,$gameid,$myid);
119 output_user_notes($myid,$gameid,$mystatus);
122 if(myisset("call") && $_REQUEST["call"] == "120" && can_call(120,$me))
123 $result = DB_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
124 if(myisset("call") && $_REQUEST["call"] == "90" && can_call(90,$me))
125 $result = DB_query("UPDATE Hand SET point_call='90' WHERE hash='$me' ");
126 if(myisset("call") && $_REQUEST["call"] == "60" && can_call(60,$me))
127 $result = DB_query("UPDATE Hand SET point_call='60' WHERE hash='$me' ");
128 if(myisset("call") && $_REQUEST["call"] == "30" && can_call(30,$me))
129 $result = DB_query("UPDATE Hand SET point_call='30' WHERE hash='$me' ");
130 if(myisset("call") && $_REQUEST["call"] == "0" && can_call(0,$me))
131 $result = DB_query("UPDATE Hand SET point_call='0' WHERE hash='$me' ");
133 /* output extra division in case this game is part of a session */
136 echo "<div class=\"session\">\n";
137 echo " <div class=\"sessionrules\">Rules (+icons fur rules) \n";
139 echo " 10ofhearts : ".$RULES["dullen"] ."<br />\n";
140 echo " schweinchen: ".$RULES["schweinchen"] ."<br />\n";
141 echo " call: ".$RULES["call"] ."<br />\n";
142 echo " </div>\n </div>\n";
143 echo " <div class=\"sessionscore\">Score \n";
144 $score = generate_score_table($session);
145 echo format_score_table_html($score,$myid);
147 $hashes = DB_get_hashes_by_session($session,$myid);
149 foreach($hashes as $hash)
157 echo "This is game number $j of <a href=\"".$INDEX."?action=game&me=$lasthash\">$i</a> in session $session.";
161 /* display the table and the names */
164 /* mystatus gets the player through the different stages of a game.
165 * start: does the player want to play?
166 * init: check for sickness
167 * check: check for return values from init
168 * poverty: handle poverty, wait here until all player have reached this state
169 * display sickness and move on to game
170 * play: game in progress
171 * gameover: are we revisiting a game
176 /* don't ask if user has autosetup set to yest */
178 if($PREF['autosetup']=='yes') $skip = 1;
180 if( !myisset("in") && !$skip)
182 /* asks the player, if he wants to join the game */
183 output_check_want_to_play($me);
188 /* check the result, if player wants to join, got next stage, else cancel game */
189 if(!$skip && $_REQUEST["in"] == "no" )
191 /* cancel the game */
192 $message = "Hello, \n\n".
193 "the game has been canceled due to the request of one of the players.\n";
195 $userids = DB_get_all_userid_by_gameid($gameid);
196 foreach($userids as $user)
198 $To = DB_get_email('userid',$user);
199 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
202 /* delete everything from the dB */
208 /* user wants to join the game */
210 /* move on to the next stage,
211 * no break statement to immediately go to the next stage
214 DB_set_hand_status_by_hash($me,'init');
216 /* check if everyone has reached this stage, send out email */
217 $userids = DB_get_all_userid_by_gameid($gameid);
219 foreach($userids as $user)
221 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
222 if($userstat!='init')
224 /* whos turn is it? */
225 DB_set_player_by_gameid($gameid,$user);
232 /* all done, send out email unless this player is the startplayer */
233 $startplayer = DB_get_startplayer_by_gameid($gameid);
234 if($mypos == $startplayer)
236 /* do nothing, go to next stage */
240 /* email startplayer */
242 $email = DB_get_email('position-gameid',$startplayer,$gameid);
243 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
244 $who = DB_get_userid('email',$email);
245 DB_set_player_by_gameid($gameid,$who);
247 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
248 "Use this link to go the game: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
249 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
256 /* here we ask the player if he is sick */
257 $mycards = DB_get_hand($me);
258 $mycards = mysort($mycards,$gametype);
260 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
261 echo "\n<ul class=\"tricks\">\n";
262 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
263 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
264 " <div class=\"trick\" id=\"trick0\">\n";
266 for($pos=1;$pos<5;$pos++)
268 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
269 $userid = DB_get_userid('gameid-position',$gameid,$pos);
270 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
272 if($userstatus=='start' || $userstatus=='init')
273 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs to decide </div>\n"; /* show this to everyone */
275 if($usersick!=NULL && $pos<=$mypos ) /* only show this for people sitting before the player */
276 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
277 else if($usersick==NULL && $pos<=$mypos)
278 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
280 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
281 /* end displaying sickness */
283 if(!myisset("solo","wedding","poverty","nines") )
285 output_check_for_sickness($me,$mycards);
287 echo "<div class=\"mycards\">Your cards are: <br />\n";
288 foreach($mycards as $card)
289 display_card($card,$PREF["cardset"]);
296 /* check if someone selected more than one sickness */
298 if($_REQUEST["solo"]!="No") $Nsickness++;
299 if($_REQUEST["wedding"] == "yes") $Nsickness++;
300 if($_REQUEST["poverty"] == "yes") $Nsickness++;
301 if($_REQUEST["nines"] == "yes") $Nsickness++;
305 echo "<p class=\"message\"> You selected more than one sickness, please go back ".
306 "and answer the <a href=\"$INDEX?action=game&me=$me&in=yes\">question</a> again.</p>";
308 echo "<div class=\"mycards\">Your cards are: <br />\n";
309 foreach($mycards as $card)
310 display_card($card,$PREF["cardset"]);
317 /* everything is ok, save what user said and proceed */
318 echo "<p class=\"message\">Processing what you selected in the last step...";
320 /* check if this sickness needs to be handled first */
321 $gametype = DB_get_gametype_by_gameid($gameid);
322 $startplayer = DB_get_startplayer_by_gameid($gameid); /* need this to check which solo goes first */
324 if( $_REQUEST["solo"]!="No" )
326 /* user wants to play a solo */
328 /* store the info in the user's hand info */
329 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
330 DB_set_sickness_by_hash($me,"solo");
332 echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
334 if($gametype == "solo" && $startplayer<$mypos)
335 {}/* do nothing, since someone else already is playing solo */
338 /* this solo comes first
339 * store info in game table
341 DB_set_gametype_by_gameid($gameid,"solo");
342 DB_set_startplayer_by_gameid($gameid,$mypos);
343 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
346 else if($_REQUEST["wedding"] == "yes")
348 /* silent solo is set further down */
349 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
350 DB_set_sickness_by_hash($me,"wedding");
352 else if($_REQUEST["poverty"] == "yes")
354 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
355 DB_set_sickness_by_hash($me,"poverty");
357 else if($_REQUEST["nines"] == "yes")
359 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
360 " is playing solo, this game will be canceled.<br />\n";
361 DB_set_sickness_by_hash($me,"nines");
366 /* move on to the next stage*/
367 DB_set_hand_status_by_hash($me,'check');
372 /* here we check what all players said and figure out what game we are playing
373 * this can therefore only be handled once all players finished the last stage
376 /* only need to redisplay the cards when the user reloads the page and lands directly here */
377 if($mystatus=='check')
379 $mycards = DB_get_hand($me);
380 $mycards = mysort($mycards,$gametype);
382 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
383 echo "\n<ul class=\"tricks\">\n";
384 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
385 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
386 " <div class=\"trick\" id=\"trick0\">\n";
388 for($pos=1;$pos<5;$pos++)
390 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
391 $userid = DB_get_userid('gameid-position',$gameid,$pos);
392 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
394 if($userstatus=='start' || $userstatus=='init')
395 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs to decide </div>\n"; /* show this to everyone */
397 if($usersick!=NULL) /* in the init-phase we only showed players with $pos<$mypos, now we can show all */
398 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
400 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
402 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
403 /* end displaying sickness */
406 echo "<div class=\"message\">\n";
407 echo "<p> Checking if someone else selected solo, nines, wedding or poverty.</p>";
409 /* check if everyone has reached this stage */
410 $userids = DB_get_all_userid_by_gameid($gameid);
412 foreach($userids as $user)
414 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
415 if($userstat!='check')
418 DB_set_player_by_gameid($gameid,$user);
425 echo "<p>This step can only be handled after everyone finished the last step. ".
426 "Seems like this is not the case, so you need to wait a bit... ".
427 "you will get an email once that is the case, please use the link in ".
428 "that email to continue the game.</p></div>";
430 /* display cards, if player was just at the init-phase he will still see the cards from there
431 * we can put this one here, since the last player to finish the init state won't get here and
432 * will still see his card anyway from the init-phase
434 if($mystatus=='check')
437 echo "<div class=\"mycards\">Your cards are: <br />\n";
438 foreach($mycards as $card)
439 display_card($card,$PREF["cardset"]);
446 /* Ok, everyone finished the init-phase, time to figure out what game we
447 * are playing, in case there are any solos this already
448 * will have the correct information in it */
450 echo "<p> Ok, everyone is done... figuring out what kind of game we are playing.</p>";
452 $gametype = DB_get_gametype_by_gameid($gameid);
453 $startplayer = DB_get_startplayer_by_gameid($gameid);
455 /* check for sickness */
460 foreach($userids as $user)
462 $name = DB_get_name('userid',$user);
463 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
464 if($usersick == 'nines')
467 break; /* no need to check for other poverties, since only solo can win and that is already set */
469 else if($usersick == 'poverty')
471 else if($usersick == 'wedding')
473 else if($usersick == 'solo')
477 /* now check which sickness comes first and set the gametype to it */
478 if($gametype == "solo")
485 /* TODO: should we keep statistics of this? */
486 $message = "Hello, \n\n".
487 " the game has been canceled because ".DB_get_name('userid',$nines).
488 " has five or more nines and nobody is playing solo.\n\n".
489 " To redeal either start a new game or, in case the game was part of a tournament, \n".
490 " go to the last game and use the link at the bottom of the page to redeal.";
492 $userids = DB_get_all_userid_by_gameid($gameid);
493 foreach($userids as $user)
495 $To = DB_get_email('userid',$user);
496 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
499 /* delete everything from the dB */
502 echo "The game has been canceled because ".DB_get_name('userid',$nines).
503 " has five or more nines and nobody is playing solo.\n";
508 else if($poverty==1) /* one person has poverty */
510 DB_set_gametype_by_gameid($gameid,"poverty");
511 $gametype = "poverty";
512 $who = DB_get_sickness_by_gameid($gameid);
515 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
516 if($firstsick == "poverty")
517 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
519 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
522 else if($poverty==2) /* two people have poverty */
524 DB_set_gametype_by_gameid($gameid,"dpoverty");
525 $gametype = "dpoverty";
526 $who = DB_get_sickness_by_gameid($gameid);
529 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
530 if($firstsick == "poverty")
532 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
533 if($secondsick == "poverty")
534 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
536 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
539 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
544 DB_set_gametype_by_gameid($gameid,"wedding");
545 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
546 $gametype = "wedding";
548 /* now the gametype is set correctly in the database */
549 echo "<p> Got it :)</p>";
551 /* loop over all players, set re/contra if possible and start the game if possible */
552 $userids = DB_get_all_userid_by_gameid($gameid);
553 foreach($userids as $userid)
555 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
560 /* are we the solo player? set us to re, else set us to contra */
561 $pos = DB_get_pos_by_hash($userhash);
562 if($pos == $startplayer)
563 DB_set_party_by_hash($userhash,"re");
565 DB_set_party_by_hash($userhash,"contra");
566 DB_set_hand_status_by_hash($userhash,'play');
570 /* set person with the wedding to re, do the rest during the game */
571 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
572 if($usersick == "wedding")
573 DB_set_party_by_hash($userhash,"re");
575 DB_set_party_by_hash($userhash,"contra");
577 DB_set_hand_status_by_hash($userhash,'play');
581 $hand = DB_get_all_hand($userhash);
583 if(in_array('3',$hand)||in_array('4',$hand))
584 DB_set_party_by_hash($userhash,"re");
586 DB_set_party_by_hash($userhash,"contra");
587 DB_set_hand_status_by_hash($userhash,'play');
591 /* set person with poverty to play status */
592 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
593 if($usersick == "poverty")
594 DB_set_hand_status_by_hash($userhash,'play');
596 /* set status of first player to be asked to poverty */
597 $who = DB_get_sickness_by_gameid($gameid);
598 if($who > 6) $who= $who/10; /* in case we have dpoverty */
599 $whoid = DB_get_userid('gameid-position',$gameid,$who);
601 DB_set_hand_status_by_hash($userhash,'poverty');
604 /* check for silent solo, set game type to solo in this case */
605 $gametype = DB_get_gametype_by_gameid($gameid);
606 $userids = DB_get_all_userid_by_gameid($gameid);
607 foreach($userids as $userid)
609 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
611 if($gametype=='normal')
613 $userhand = DB_get_all_hand($userhash);
614 if(check_wedding($userhand))
616 /* normal game type and player has both queens -> silent solo */
617 /* keep startplayer, just set gametype to silent solo */
618 DB_set_gametype_by_gameid($gameid,"solo");
619 DB_set_solo_by_gameid($gameid,'silent');
624 /* send out email to first player or poverty person*/
625 if($gametype!="poverty" && $gametype!="dpoverty")
627 $startplayer = DB_get_startplayer_by_gameid($gameid);
628 $email = DB_get_email('position-gameid',$startplayer,$gameid);
629 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
630 $who = DB_get_userid('email',$email);
631 DB_set_player_by_gameid($gameid,$who);
635 if(DB_get_email_pref_by_hash($hash)!="emailaddict")
637 /* email startplayer */
638 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
639 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
640 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
644 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
648 /* set status of first player to be asked to poverty */
649 $who = DB_get_sickness_by_gameid($gameid);
650 if($who > 6) $who= $who/10; /* in case we have dpoverty */
652 $whoid = DB_get_userid('gameid-position',$gameid,$who);
654 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
657 $email = DB_get_email('position-gameid',$who,$gameid);
658 $whohash = DB_get_hash_from_game_and_pos($gameid,$who);
659 DB_set_player_by_gameid($gameid,$whoid);
661 if(DB_get_email_pref_by_hash($hash)!="emailaddict")
663 /* email player for poverty */
664 $message = "Poverty: It's your turn now in game ".DB_format_gameid($gameid).".\n".
665 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$whohash."\n\n" ;
666 mymail($email,$EmailName."Poverty (game ".DB_format_gameid($gameid).") ",$message);
674 /* user only gets here in a poverty game, several things have to be handled here:
675 * A) ask, if user wants to take trump
677 * poverty: set re/contra
678 * dpoverty: first time: set re, send email to second player
679 * second time: set contra
680 * poverty: set status of other players to 'play'
681 * set status to play in case 0 trump
682 * no -> set status to play,
683 * ask next player or cancle the game if no more players
684 * B) user took trump and has too many cards (e.g. count(cards)>12 and re/contra set)
685 * ask to give cards back, set status to play, once player has 12 cards
687 * it is easier to check B) first
690 /* output pre game in case user reloads */
693 set_gametype($gametype); /* this sets the $CARDS variable */
694 $myparty = DB_get_party_by_hash($me);
696 /* the following is part B) of whats needs to be done)
697 /* check if user wants to give cards back */
698 if(myisset("exchange"))
700 $exchange = $_REQUEST['exchange'];
701 $partnerhash = DB_get_partner_hash_by_hash($me);
702 $partnerid = DB_get_userid('hash',$partnerhash);
703 $partnerhand = DB_get_handid('gameid-userid',$gameid,$partnerid);
705 /* if exchange is set to a value>0, exchange that card back to the partner */
708 $result = DB_query("UPDATE Hand_Card SET hand_id='$partnerhand'".
709 " WHERE hand_id='$myhand' AND card_id=".DB_quote_smart($exchange));
714 $mycards = DB_get_hand($me);
715 $mycards = mysort($mycards,$gametype);
717 /* check if user need to give more cards back */
718 if( ($myparty=='re' || $myparty=='contra') && count($mycards)>12)
720 echo "<div class=\"poverty\"> you need to get rid of a few cards</div>\n";
723 echo "<div class=\"mycards\">Your cards are: <br />\n";
724 foreach($mycards as $card)
725 display_link_card($card,$PREF["cardset"],$type);
726 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select card to give back\" />\n";
729 else if( ($myparty=='re' || $myparty=='contra') && count($mycards)==12)
731 /* user is done, ready to play */
732 DB_set_hand_status_by_hash($me,'play');
734 /* email start player */
735 $startplayer = DB_get_startplayer_by_gameid($gameid);
736 $email = DB_get_email('position-gameid',$startplayer,$gameid);
737 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
738 $who = DB_get_userid('email',$email);
739 DB_set_player_by_gameid($gameid,$who);
743 if(DB_get_email_pref_by_hash($hash)!="emailaddict")
745 /* email startplayer */
746 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
747 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
748 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
752 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
755 /* the following is part A) of what needs to be done */
756 if(!myisset("trump"))
760 echo "<div class=\"poverty\">\n";
761 $userids = DB_get_all_userid_by_gameid($gameid);
762 foreach($userids as $user)
764 $name = DB_get_name('userid',$user);
765 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
766 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
767 $userparty = DB_get_party_by_hash($userhash);
769 if($usersick=="poverty" && !$userparty)
771 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
772 $cards = DB_get_hand($hash);
775 foreach($cards as $card)
776 if($card<27) $nrtrump++;
777 echo "Player $name has $nrtrump trump. Do you want to take them?".
778 "<a href=\"index.php?action=game&me=$me&trump=$user\">Yes</a> <br />\n";
781 echo "<a href=\"index.php?action=game&me=$me&trump=no\">No way</a> <br />\n";
782 echo "</div><div>\n";
784 echo "<div class=\"mycards\">Your cards are: <br />\n";
785 foreach($mycards as $card)
786 display_card($card,$PREF["cardset"]);
787 echo "</div></div>\n";
793 $trump = $_REQUEST['trump'];
797 /* user doesn't want to take trump */
798 DB_set_hand_status_by_hash($me,'play');
800 /* set next player who needs to be asked and email him*/
801 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
802 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
804 /* don't ask people who have poverty */
806 if($firstsick=="poverty")
808 if($secondsick=="poverty")
813 if($gametype=="dpoverty")
815 $next=999; /* need to cancel for sure, since both would need to take the trump */
818 /* no more people to ask, need to cancel the game */
821 $message = "Hello, \n\n".
822 "Game ".DB_format_gameid($gameid)." has been canceled since nobody wanted to take the trump.\n";
824 $userids = DB_get_all_userid_by_gameid($gameid);
825 foreach($userids as $user)
827 $To = DB_get_email('userid',$user);
828 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled (poverty not resolved)",$message);
831 /* delete everything from the dB */
834 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been canceled.<br /><br /></p>";
841 /* email next player, set his status to poverty */
842 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
843 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
844 $userid = DB_get_userid('email',$To);
846 DB_set_player_by_gameid($gameid,$userid);
847 DB_set_hand_status_by_hash($userhash,'poverty');
849 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
850 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
851 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
856 /* player wants to take trump, change cards */
858 /* user wants to take trump */
859 $trump = $_REQUEST["trump"];
860 $userhand = DB_get_handid('gameid-userid',$gameid,$trump);
861 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
863 /* copy trump from player A to B */
864 $result = DB_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
867 $mycards = DB_get_hand($me);
870 if($gametype=='poverty')
872 $userids = DB_get_all_userid_by_gameid($gameid);
873 foreach($userids as $user)
875 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
876 if($hash==$userhash||$hash==$me)
878 DB_set_party_by_hash($hash,"re");
882 DB_set_party_by_hash($hash,"contra");
883 DB_set_hand_status_by_hash($hash,'play'); /* the contra party is ready to play */
886 /* check if we are done (in case of no trump handed over), if so, go to 'play' phase right away*/
887 if(count($mycards)==12)
889 DB_set_hand_status_by_hash($me,'play');
894 /* has the re party already been set?*/
896 $userids = DB_get_all_userid_by_gameid($gameid);
897 foreach($userids as $user)
899 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
900 $party = DB_get_party_by_hash($hash);
906 DB_set_party_by_hash($me,'contra');
907 DB_set_party_by_hash($userhash,'contra');
911 DB_set_party_by_hash($me,'re');
912 DB_set_party_by_hash($userhash,'re');
914 /* send out email to second non-poverty player */
915 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
916 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
919 if($firstsick=="poverty")
920 if($secondsick=="poverty")
926 echo "<div class=\"message\">Error in poverty, please contact the Admin</div>\n";
928 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
929 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
930 $userid = DB_get_userid('email',$To);
932 DB_set_player_by_gameid($gameid,$userid);
933 DB_set_hand_status_by_hash($userhash,'poverty');
935 $message = "Two people have poverty, it's your turn to decide, if you want to take the trump. Please visit:".
936 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
937 mymail($To,$EmailName." double poverty (game ".DB_format_gameid($gameid).")",$message);
942 echo "<div class=\"message\"> Please, <a href=\"$INDEX?action=game&me=$me\">continue</a> here.</div>\n";
950 /* both entries here, so that the tricks are visible for both.
951 * in case of 'play' there is a break later that skips the last part
954 /* check if all players are ready to play,
955 * if so, send out email to the startplayer
956 * only need to do this if the game hasn't started yet
958 $gamestatus = DB_get_game_status_by_gameid($gameid);
959 if($gamestatus == 'pre')
962 $userids = DB_get_all_userid_by_gameid($gameid);
963 foreach($userids as $user)
965 $userstatus = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
966 if($userstatus !='play' && $userstatus!='gameover')
969 DB_set_player_by_gameid($gameid,$user);
975 /* only set this after all poverty, etc. are handled*/
976 DB_set_game_status_by_gameid($gameid,'play');
978 /* email startplayer */
979 $startplayer = DB_get_startplayer_by_gameid($gameid);
980 $email = DB_get_email('position-gameid',$startplayer,$gameid);
981 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
982 $who = DB_get_userid('email',$email);
983 DB_set_player_by_gameid($gameid,$who);
985 if($hash!=$me && DB_get_email_pref_by_hash($hash)!="emailaddict")
987 /* email startplayer) */
988 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
989 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
990 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
994 /* figure out what kind of game we are playing,
995 * set the global variables $CARDS["trump"],$CARDS["diamonds"],$CARDS["hearts"],
996 * $CARDS["clubs"],$CARDS["spades"],$CARDS["foxes"]
1000 $gametype = DB_get_gametype_by_gameid($gameid);
1002 if($gametype=="solo")
1004 $gametype = DB_get_solo_by_gameid($gameid);
1005 if($gametype=='silent')
1008 $GT = $gametype." ".$GT;
1011 $gametype = "normal";
1013 set_gametype($gametype); /* this sets the $CARDS variable */
1015 /* get some infos about the game, need to reset this, since it might have changed */
1016 $gamestatus = DB_get_game_status_by_gameid($gameid);
1018 /* has the game started? No, then just wait here...*/
1019 if($gamestatus == 'pre')
1021 echo "<p class=\"message\"> You finished the setup, but not everyone else finished it... ".
1022 "You need to wait for the others. Just wait for an email. </p>";
1024 $mycards = DB_get_hand($me);
1025 $mycards = mysort($mycards,$gametype);
1027 echo "<div class=\"mycards\">Your cards are: <br />\n";
1028 foreach($mycards as $card)
1029 display_card($card,$PREF["cardset"]);
1032 break; /* not sure this works... the idea is that you can
1033 * only play a card after everyone is ready to play */
1036 /* get time from the last action of the game */
1037 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1038 $gameend = time() - strtotime($r[0]);
1040 /* handle comments in case player didn't play a card, allow comments a week after the end of the game */
1041 if( (!myisset("card") && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
1042 if(myisset("comment"))
1044 $comment = $_REQUEST["comment"];
1045 $playid = DB_get_current_playid($gameid);
1048 DB_insert_comment($comment,$playid,$myid);
1051 /* get everything relevant to display the tricks */
1052 $result = DB_query("SELECT Hand_Card.card_id as card,".
1053 " Hand.position as position,".
1054 " Play.sequence as sequence, ".
1056 " GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
1057 " SEPARATOR '\n' ), ".
1058 " Play.create_date, ".
1061 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1062 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1063 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1064 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1065 "LEFT JOIN User On User.id=Comment.user_id ".
1066 "WHERE Trick.game_id='".$gameid."' ".
1067 "GROUP BY Trick.id, sequence ".
1068 "ORDER BY Trick.id, sequence ASC");
1070 $lasttrick = DB_get_max_trickid($gameid);
1072 $play = array(); /* needed to calculate winner later */
1074 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1075 $firstcard = ""; /* first card in a trick */
1077 echo "\n<ul class=\"tricks\">\n";
1078 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
1080 /* output vorbehalte */
1081 $mygametype = DB_get_gametype_by_gameid($gameid);
1082 if($mygametype != 'normal' && $mygametype != 'silent') /* only show when needed */
1084 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
1085 " <div class=\"trick\" id=\"trick0\">\n";
1087 for($mypos=1;$mypos<5;$mypos++)
1089 $usersick = DB_get_sickness_by_pos_and_gameid($mypos,$gameid);
1092 echo " <div class=\"vorbehalt".($mypos-1)."\"> Vorbehalt <br />";
1094 echo " $usersick <br />";
1097 if($mygametype == $usersick)
1101 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1105 while($r = DB_fetch_array($result))
1113 /* count number of tricks */
1117 /* check if first schweinchen has been played */
1118 if( $GAME['schweinchen-who'] && ($r[0] == 19 || $r[0] == 20) )
1119 if(!$GAME['schweinchen-first'])
1120 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1122 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1124 /* save card to be able to find the winner of the trick later */
1125 $play[$seq] = array("card"=>$r[0],"pos"=>$pos);
1129 /* first card in a trick, output some html */
1130 if($trick!=$lasttrick)
1132 /* start of an old trick? */
1133 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1134 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1135 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1137 else if($trick==$lasttrick)
1139 /* start of a last trick? */
1140 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1141 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1142 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1145 /* remember first card, so that we are able to check, what cards can be played */
1150 echo " <div class=\"card".($pos-1)."\">\n";
1152 /* display comments */
1154 echo " <span class=\"comment\">".$comment."</span>\n";
1157 display_card($r[0],$PREF["cardset"]);
1159 echo " </div>\n"; /* end div card */
1164 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1168 /* whos turn is it? */
1171 $winner = get_winner($play,$gametype); /* returns the position */
1173 $firstcard = ""; /* new trick, no first card */
1178 if($next==5) $next = 1;
1181 /* my turn?, display cards as links, ask for comments*/
1182 if(DB_get_pos_by_hash($me) == $next)
1187 /* do we want to play a card? */
1188 if(myisset("card") && $myturn)
1190 $card = $_REQUEST["card"];
1191 $handid = DB_get_handid('hash',$me);
1192 $commentSchweinchen =""; /* used to add a comment when Schweinchen is being played */
1194 /* check if we have card and that we haven't played it yet*/
1195 /* set played in hand_card to true where hand_id and card_id*/
1196 $r = DB_query_array("SELECT id FROM Hand_Card WHERE played='false' and ".
1197 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1198 $handcardid = $r[0];
1200 if($handcardid) /* everything ok, play card */
1202 /* update Game timestamp */
1203 DB_update_game_timestamp($gameid);
1205 /* mark card as played */
1206 DB_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1207 DB_quote_smart($card));
1209 /* get trick id or start new trick */
1210 $a = DB_get_current_trickid($gameid);
1215 $playid = DB_play_card($trickid,$handcardid,$sequence);
1217 /* check special output for schweinchen in case in case a fox is being played
1218 * check for correct rules, etc. has already been done
1220 if( $GAME["schweinchen-who"] && ($card == 19 || $card == 20) )
1222 if(!$GAME['schweinchen-first'])
1223 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1225 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1227 if( ($GAME['schweinchen-second']==1 && $RULES['schweinchen']=='second') || $RULES['schweinchen']=='both')
1229 DB_insert_comment("Schweinchen! ",$playid,$myid);
1230 $commentSchweinchen = "Schweinchen! ";
1233 echo "schweinchen = ".$GAME["schweinchen-who"]." ---<br />";
1236 /* if sequence == 4 check who one in case of wedding */
1237 if($sequence == 4 && $GT == "wedding")
1239 /* is wedding resolve */
1240 $resolved = DB_get_sickness_by_gameid($gameid);
1243 /* who has wedding */
1244 $userids = DB_get_all_userid_by_gameid($gameid);
1245 foreach($userids as $user)
1247 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1248 if($usersick == "wedding")
1251 /* who won the trick */
1252 $play = DB_get_cards_by_trick($trickid);
1253 $winner = get_winner($play,$gametype); /* returns the position */
1254 $winnerid = DB_get_userid('gameid-position',$gameid,$winner);
1255 /* is tricknr <=3 */
1256 if($tricknr <=3 && $winnerid!=$whosick)
1258 /* set resolved at tricknr*/
1259 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1261 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1262 DB_set_party_by_hash($whash,"re");
1264 if($tricknr == 3 && $winnerid==$whosick)
1266 /* set resolved at tricknr*/
1267 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1272 /* if sequence == 4, set winner of the trick, count points and set the next player */
1275 $play = DB_get_cards_by_trick($trickid);
1276 $winner = get_winner($play,$gametype); /* returns the position */
1278 /* check if someone caught a fox */
1279 /* first check if we should account for solos at all,
1280 * since it doesn't make sense in some games
1282 $ok = 0; /* fox shouldn't be counted */
1283 if(DB_get_gametype_by_gameid($gameid)=="solo")
1285 $solo = DB_get_solo_by_gameid($gameid);
1286 if($solo == 'trump' || $solo == 'silent')
1287 $ok = 1; /* for trump solos and silent solos, foxes are ok */
1290 $ok = 1; /* for all other games (not solos) foxes are ok too */
1293 foreach($play as $played)
1295 if ( $played['card']==19 || $played['card']==20 )
1296 if ($played['pos']!= $winner )
1298 /* possible caught a fox, check party */
1299 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1300 $uid2 = DB_get_userid('gameid-position',$gameid,$played['pos']);
1302 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1303 $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1305 if($party1 != $party2)
1306 DB_query("INSERT INTO Score".
1307 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1311 /* check for karlchen (jack of clubs in the last trick)*/
1312 /* same as for foxes, karlchen doesn't always make sense
1313 * check what kind of game it is and set karlchen accordingly */
1314 $ok = 1; /* default: karlchen should be accounted for */
1316 $ok = 0; /* Karlchen works only in the last trick */
1317 if($ok && DB_get_gametype_by_gameid($gameid)=="solo" )
1319 $solo = DB_get_solo_by_gameid($gameid);
1320 if($solo == "trumpless" || $solo == "jack" || $solo == "queen" )
1321 $ok = 0; /* no Karlchen in these solos */
1325 foreach($play as $played)
1326 if ( $played['card']==11 || $played['card']==12 )
1327 if ($played['pos'] == $winner )
1329 /* possible caught a fox, check party */
1330 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1331 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1333 DB_query("INSERT INTO Score".
1334 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1336 /* check for doppelopf (>40 points)*/
1338 foreach($play as $played)
1340 $points += DB_get_card_value_by_cardid($played['card']);
1344 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1345 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1347 DB_query("INSERT INTO Score".
1348 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
1352 DB_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1354 echo "ERROR during scoring";
1357 echo "DEBUG: position $winner won the trick <br />";
1359 /* who is the next player? */
1364 $next = DB_get_pos_by_hash($me)+1;
1366 if($next==5) $next=1;
1368 /* check for coment */
1369 if(myisset("comment"))
1371 $comment = $_REQUEST["comment"];
1373 DB_insert_comment($comment,$playid,$myid);
1374 if($commentSchweinchen)
1375 $comment = $commentSchweinchen . $comment;
1378 /* check for note */
1381 $note = $_REQUEST["note"];
1383 DB_insert_note($note,$gameid,$myid);
1386 /* display played card */
1387 $pos = DB_get_pos_by_hash($me);
1390 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1391 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1392 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1395 echo " <div class=\"card".($pos-1)."\">\n ";
1397 /* display comments */
1398 display_card($card,$PREF["cardset"]);
1400 echo "\n <span class=\"comment\"> ".$comment."</span>\n";
1403 /*check if we still have cards left, else set status to gameover */
1404 if(sizeof(DB_get_hand($me))==0)
1406 DB_set_hand_status_by_hash($me,'gameover');
1407 $mystatus = 'gameover';
1410 /* if all players are done, set game status to game over,
1411 * get the points of the last trick and send out an email
1414 $userids = DB_get_all_userid_by_gameid($gameid);
1417 foreach($userids as $user)
1418 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1422 DB_set_game_status_by_gameid($gameid,"gameover");
1424 /* email next player, if game is still running */
1425 if(DB_get_game_status_by_gameid($gameid)=='play')
1427 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1428 $email = DB_get_email('hash',$next_hash);
1429 $who = DB_get_userid('email',$email);
1430 DB_set_player_by_gameid($gameid,$who);
1432 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1433 "It's your turn now.\n".
1434 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$next_hash."\n\n" ;
1435 if( DB_get_email_pref_by_uid($who)!="emailaddict" )
1436 mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
1438 else /* send out final email */
1440 /* individual score */
1441 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1442 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1443 " LEFT JOIN User ON User.id=Hand.user_id".
1444 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1445 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1446 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1447 " WHERE Hand.game_id='$gameid'".
1448 " GROUP BY User.fullname" );
1449 $message = "The game is over. Thanks for playing :)\n";
1450 $message .= "Final score:\n";
1451 while( $r = DB_fetch_array($result) )
1452 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1454 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1455 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1456 " LEFT JOIN User ON User.id=Hand.user_id".
1457 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1458 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1459 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1460 " WHERE Hand.game_id='$gameid'".
1461 " GROUP BY Hand.party" );
1462 $message .= "\nTotals:\n";
1465 while( $r = DB_fetch_array($result) )
1467 $message .= " ".$r[0]." ".$r[1]."\n";
1470 else if($r[0] == "contra")
1475 * save score in database
1479 /* get calls from re/contra */
1481 $call_contra = NULL;
1482 foreach($userids as $user)
1484 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1485 $call = DB_get_call_by_hash($hash);
1486 $party = DB_get_party_by_hash($hash);
1490 $call = (int) $call;
1496 else if( $call < $call_re)
1499 else if($party=="contra")
1501 if($call_contra==NULL)
1502 $call_contra = $call;
1503 else if( $call < $call_re)
1504 $call_contra = $call;
1509 /* figure out who one */
1510 $winning_party = NULL;
1512 if($call_re == NULL && $call_contra==NULL)
1514 /* nobody made a call, so it's easy to figure out who won */
1516 $winning_party="re";
1518 $winning_party="contra";
1522 /* if one party makes a call, they only win, iff they make enough points
1523 * if only one party made a call, the other one wins,
1524 * if the first one didn't make it
1528 $offset = 120 - $call_re;
1530 $offset--; /* since we use a > in the next equation */
1532 if($re > 120+$offset)
1533 $winning_party="re";
1534 else if ($call_contra == NULL )
1535 $winning_party="contra";
1540 $offset = 120 - $call_contra;
1541 if($call_contra == 0)
1542 $offset--; /* since we use a > in the next equation */
1544 if($contra > 120+$offset)
1545 $winning_party="contra";
1546 else if ($call_re == NULL )
1547 $winning_party="re";
1551 /* one point for each call of the other party in case the other party didn't win
1552 * and one point each in case the party made more than points than one of the calls
1554 if($winning_party!="contra" && $call_contra!=NULL)
1556 for( $p=$call_contra;$p<=120; $p+=30 )
1558 DB_query("INSERT INTO Score".
1559 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
1562 for( $p=$call_contra; $p<120; $p+=30)
1565 DB_query("INSERT INTO Score".
1566 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'made$p')");
1569 if($winning_party!="re" and $call_re!=NULL)
1571 for( $p=$call_re;$p<=120; $p+=30 )
1573 DB_query("INSERT INTO Score".
1574 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'against$p')");
1577 for( $p=$call_re; $p<120; $p+=30)
1580 DB_query("INSERT INTO Score".
1581 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
1585 /* point in case contra won */
1586 if($winning_party=="contra")
1588 DB_query("INSERT INTO Score".
1589 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1592 /* one point each for winning and each 30 points + calls */
1593 if($winning_party=="re")
1595 foreach(array(120,150,180,210,240) as $p)
1598 if($p==240 || $call_contra!=NULL)
1602 DB_query("INSERT INTO Score".
1603 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
1605 /* re called something and won */
1606 foreach(array(0,30,60,90,120) as $p)
1608 if($call_re!=NULL && $call_re<$p+1)
1609 DB_query("INSERT INTO Score".
1610 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
1613 else if( $winning_party=="contra")
1615 foreach(array(120,150,180,210,240) as $p)
1618 if($p==240 || $call_re!=NULL)
1621 if($contra>$p-$offset)
1622 DB_query("INSERT INTO Score".
1623 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
1625 /* re called something and won */
1626 foreach(array(0,30,60,90,120) as $p)
1628 if($call_contra!=NULL && $call_contra<$p+1)
1629 DB_query("INSERT INTO Score".
1630 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
1635 /* add score points to email */
1638 $message .= " Points Re: \n";
1639 $queryresult = DB_query("SELECT score FROM Score ".
1640 " WHERE game_id=$gameid AND party='re'".
1642 while($r = DB_fetch_array($queryresult) )
1644 $message .= " ".$r[0]."\n";
1647 $message .= " Points Contra: \n";
1648 $queryresult = DB_query("SELECT score FROM Score ".
1649 " WHERE game_id=$gameid AND party='contra'".
1651 while($r = DB_fetch_array($queryresult) )
1653 $message .= " ".$r[0]."\n";
1656 $message .= " Total Points (from the Re point of view): $Tpoint\n";
1659 $session = DB_get_session_by_gameid($gameid);
1660 $score = generate_score_table($session);
1662 $message .= "Score Table:\n";
1663 $message .= format_score_table_ascii($score);
1665 /* send out final email */
1668 foreach($userids as $user)
1669 $all[] = DB_get_email('userid',$user);
1670 $To = implode(",",$all);
1672 $help = "\n\n (you can use reply all on this email to reach all the players.)\n";
1673 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 1(2)",$message.$help);
1675 foreach($userids as $user)
1677 $To = DB_get_email('userid',$user);
1678 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1680 $link = "Use this link to have a look at game ".DB_format_gameid($gameid).": ".
1681 $HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
1682 if( DB_get_email_pref_by_uid($user) != "emailaddict" )
1683 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 2(2)",$link);
1689 echo "can't find that card?! <br />\n";
1692 else if(myisset("card") && !$myturn )
1694 echo "please wait until it's your turn! <br />\n";
1697 if($seq!=4 && $trickNR>=1)
1698 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1700 /* display points in case game is over */
1701 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1703 echo " <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1704 " <div class=\"trick\" id=\"trick13\">\n";
1705 /* add pic for re/contra
1706 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1708 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1709 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1710 " LEFT JOIN User ON User.id=Hand.user_id".
1711 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1712 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1713 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1714 " WHERE Hand.game_id='$gameid'".
1715 " GROUP BY User.fullname" );
1716 while( $r = DB_fetch_array($result))
1717 echo " <div class=\"card".($r[3]-1)."\">\n".
1718 " <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
1721 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1724 echo " <li onclick=\"hl_prev();\" class=\"old\"><a href=\"#\">prev</a></li>\n";
1725 echo " <li onclick=\"hl_next();\" class=\"old\"><a href=\"#\">next</a></li>\n";
1726 echo "</ul>\n"; /* end ul tricks*/
1728 $mycards = DB_get_hand($me);
1729 $mycards = mysort($mycards,$gametype);
1730 echo "<div class=\"mycards\">\n";
1732 if($myturn && !myisset("card") && $mystatus=='play' )
1734 echo "Hello ".$myname.", it's your turn! <br />\n";
1735 echo "Your cards are: <br />\n";
1737 /* do we have to follow suite? */
1739 if(have_suit($mycards,$firstcard))
1742 foreach($mycards as $card)
1744 if( ($followsuit && !same_type($card,$firstcard)) ||
1745 ( (int)($card)==19 && ($RULES['schweinchen']=='second'||$RULES['schweinchen']=='secondaftercall')
1746 && $GAME['schweinchen-who']==$me && !$GAME['schweinchen-first'] )
1748 display_card($card,$PREF["cardset"]);
1750 display_link_card($card,$PREF["cardset"]);
1753 else if($mystatus=='play' )
1755 echo "Your cards are: <br />\n";
1756 foreach($mycards as $card)
1757 display_card($card,$PREF["cardset"]);
1759 else if($mystatus=='gameover')
1761 $oldcards = DB_get_all_hand($me);
1762 $oldcards = mysort($oldcards,$gametype);
1763 echo "Your cards were: <br />\n";
1764 foreach($oldcards as $card)
1765 display_card($card,$PREF["cardset"]);
1767 $userids = DB_get_all_userid_by_gameid($gameid);
1768 foreach($userids as $user)
1770 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1776 $name = DB_get_name('userid',$user);
1777 $oldcards = DB_get_all_hand($userhash);
1778 $oldcards = mysort($oldcards,$gametype);
1779 echo "$name's cards were: <br />\n";
1780 foreach($oldcards as $card)
1781 display_card($card,$PREF["cardset"]);
1787 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1788 if($mystatus=='play')
1791 /* the following happens only when the gamestatus is 'gameover' */
1792 /* check if game is over, display results */
1793 if(DB_get_game_status_by_gameid($gameid)=='play')
1795 echo "The game is over for you.. other people still need to play though";
1799 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1800 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1801 " LEFT JOIN User ON User.id=Hand.user_id".
1802 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1803 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1804 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1805 " WHERE Hand.game_id='$gameid'".
1806 " GROUP BY Hand.party" );
1807 echo "<div class=\"total\"> Totals:<br />\n";
1808 while( $r = DB_fetch_array($result))
1809 echo " ".$r[0]." ".$r[1]."<br />\n";
1811 $queryresult = DB_query("SELECT timediff(mod_date,create_date) ".
1812 " FROM Game WHERE id='$gameid'");
1813 $r = DB_fetch_array($queryresult);
1814 echo "<p>This game took ".$r[0]." hours.</p>";
1816 echo "<div class=\"re\">\n Points Re: <br />\n";
1817 $queryresult = DB_query("SELECT score FROM Score ".
1818 " WHERE game_id=$gameid AND party='re'".
1820 while($r = DB_fetch_array($queryresult) )
1821 echo " ".$r[0]."<br />\n";
1824 echo "<div class=\"contra\">\n Points Contra: <br />\n";
1825 $queryresult = DB_query("SELECT score FROM Score ".
1826 " WHERE game_id=$gameid AND party='contra'".
1828 while($r = DB_fetch_array($queryresult) )
1829 echo " ".$r[0]."<br />\n";
1838 myerror("error in testing the status");
1840 /* output left menu */
1841 display_user_menu();
1843 /* output right menu */
1845 /* display rule set for this game */
1846 echo "<div class=\"gameinfo\">\n";
1848 if($gamestatus == 'play' )
1849 output_form_calls($me);
1851 /* get time from the last action of the game */
1852 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1853 $gameend = time() - strtotime($r[0]);
1855 if($gamestatus == 'play' || $gameend < 60*60*24*7)
1857 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1860 echo "<input type=\"submit\" value=\"submit\" />\n";
1866 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1869 $session = DB_get_session_by_gameid($gameid);
1870 $result = DB_query("SELECT id,create_date FROM Game".
1871 " WHERE session=$session".
1872 " ORDER BY create_date DESC".
1876 $r = DB_fetch_array($result);
1878 if(!$session || $gameid==$r[0])
1880 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
1881 $names = DB_get_all_names_by_gameid($gameid);
1882 $type = DB_get_gametype_by_gameid($gameid);
1886 $solo = DB_get_solo_by_gameid($gameid);
1888 if($solo!='silent') /* repeat game with same first player */
1889 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
1890 else /* rotate normally */
1891 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
1893 else /* rotate normally */
1894 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);