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";
14 $me = $_REQUEST['me'];
16 /* Ok, got a hash, but is it valid? */
17 $myid = DB_get_userid('hash',$me);
20 echo "Can't find you in the database, please check the url.<br />\n";
21 echo "perhaps the game has been canceled, check by login in <a href=\"$INDEX\">here</a>.";
25 global $GAME,$RULES,$CARDS;
27 /* the user has done something, update the timestamp */
28 DB_update_user_timestamp($myid);
30 /* get some information from the DB */
31 $gameid = DB_get_gameid_by_hash($me);
32 $myname = DB_get_name('hash',$me);
33 $mystatus = DB_get_status_by_hash($me);
34 $mypos = DB_get_pos_by_hash($me);
35 $myhand = DB_get_handid('hash',$me);
36 $session = DB_get_session_by_gameid($gameid);
38 /* get prefs and save them in a variable*/
39 $PREF = DB_get_PREF($myid);
41 /* get rule set for this game */
42 $RULES = DB_get_RULES($gameid);
44 /* get some infos about the game */
45 $gametype = DB_get_gametype_by_gameid($gameid);
46 $gamestatus = DB_get_game_status_by_gameid($gameid);
50 $gametype = DB_get_solo_by_gameid($gameid);
51 $GT = $gametype.' '.$GT;
54 /* do we need to worry about Schweinchen?
55 * check gametype and rules
56 * if yes, figure out if someone actually has Schweinchen
57 * save information in $GAME
60 if( $gamestatus == 'pre' )
62 /* always need to use Schweinchen to figure out for example who has poverty */
67 /* in a game Schweinchen is not valid in all types of games */
68 if( $gametype == 'normal' || $gametype == 'silent' || $gametype=='trump' )
69 if( in_array($RULES['schweinchen'],array('both','second','secondaftercall')) )
73 /* these are the defaults */
74 $GAME['schweinchen-who'] = NULL;
75 $GAME['schweinchen-first'] = NULL;
76 $GAME['schweinchen-second'] = NULL;
80 /* need to check for Schweinchen */
83 $hash = DB_get_hash_from_game_and_pos($gameid,$i);
84 $cards = DB_get_all_hand($hash);
85 if( in_array('19',$cards) && in_array('20',$cards) )
86 $GAME['schweinchen-who']=$hash;
88 $GAME['schweinchen-first'] = 0; /* to keep track if they have been played already */
89 $GAME['schweinchen-second'] = 0;
91 /* end check for Schweinchen */
93 /* set the $CARDS variable, needed for sorting the cards
94 * we set it to normal so that the pre-game phase is handled ok
95 * and later set it to the correct game type that is played
97 set_gametype('normal');
99 /* put everyting in a form */
100 echo "<form action=\"index.php?action=game&me=$me\" method=\"post\">\n";
102 /* handle user notes (only possible while game is running)*/
103 if( $mystatus!='gameover' )
106 $note = $_REQUEST['note'];
109 DB_insert_note($note,$gameid,$myid);
111 output_user_notes($myid,$gameid,$mystatus);
114 if(myisset('call') && $_REQUEST['call'] == '120' && can_call(120,$me))
115 $result = DB_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
116 if(myisset('call') && $_REQUEST['call'] == '90' && can_call(90,$me))
117 $result = DB_query("UPDATE Hand SET point_call='90' WHERE hash='$me' ");
118 if(myisset('call') && $_REQUEST['call'] == '60' && can_call(60,$me))
119 $result = DB_query("UPDATE Hand SET point_call='60' WHERE hash='$me' ");
120 if(myisset('call') && $_REQUEST['call'] == '30' && can_call(30,$me))
121 $result = DB_query("UPDATE Hand SET point_call='30' WHERE hash='$me' ");
122 if(myisset('call') && $_REQUEST['call'] == '0' && can_call(0,$me))
123 $result = DB_query("UPDATE Hand SET point_call='0' WHERE hash='$me' ");
125 /* output extra division in case this game is part of a session */
128 echo "<div class=\"session\">\n";
129 echo " <div class=\"sessionrules\">Rules (+icons fur rules) \n";
131 echo " 10ofhearts : {$RULES['dullen']} <br />\n";
132 echo " schweinchen: {$RULES['schweinchen']} <br />\n";
133 echo " call: {$RULES['call']} <br />\n";
134 echo " </div>\n </div>\n";
135 echo " <div class=\"sessionscore\">Score \n";
136 $score = generate_score_table($session);
137 echo format_score_table_html($score,$myid);
139 $hashes = DB_get_hashes_by_session($session,$myid);
141 foreach($hashes as $hash)
149 echo "This is game number $j of <a href=\"{$INDEX}?action=game&me=$lasthash\">$i</a> in session $session.";
153 /* display the table and the names */
156 /* mystatus gets the player through the different stages of a game.
157 * start: does the player want to play?
158 * init: check for sickness
159 * check: check for return values from init
160 * poverty: handle poverty, wait here until all player have reached this state
161 * display sickness and move on to game
162 * play: game in progress
163 * gameover: are we revisiting a game
168 /* don't ask if user has autosetup set to yest */
170 if($PREF['autosetup']=='yes') $skip = 1;
172 if( !myisset('in') && !$skip)
174 /* asks the player, if he wants to join the game */
175 output_check_want_to_play($me);
180 /* check the result, if player wants to join, got next stage, else cancel game */
181 if(!$skip && $_REQUEST['in'] == 'no' )
183 /* cancel the game */
184 $message = "Hello, \n\n".
185 "the game has been canceled due to the request of one of the players.\n";
187 $userids = DB_get_all_userid_by_gameid($gameid);
188 foreach($userids as $user)
190 $To = DB_get_email('userid',$user);
191 mymail($To,"$EmailName game ".DB_format_gameid($gameid)." canceled",$message);
194 /* delete everything from the dB */
200 /* user wants to join the game */
202 /* move on to the next stage,
203 * no break statement to immediately go to the next stage
206 DB_set_hand_status_by_hash($me,'init');
208 /* check if everyone has reached this stage, send out email */
209 $userids = DB_get_all_userid_by_gameid($gameid);
211 foreach($userids as $user)
213 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
214 if($userstat!='init')
216 /* whos turn is it? */
217 DB_set_player_by_gameid($gameid,$user);
224 /* all done, send out email unless this player is the startplayer */
225 $startplayer = DB_get_startplayer_by_gameid($gameid);
226 if($mypos == $startplayer)
228 /* do nothing, go to next stage */
232 /* email startplayer */
234 $email = DB_get_email('position-gameid',$startplayer,$gameid);
235 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
236 $who = DB_get_userid('email',$email);
237 DB_set_player_by_gameid($gameid,$who);
239 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
240 "Use this link to go the game: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
241 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
248 /* here we ask the player if he is sick */
249 $mycards = DB_get_hand($me);
250 $mycards = mysort($mycards,$gametype);
252 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
253 echo "\n<ul class=\"tricks\">\n";
254 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
255 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
256 " <div class=\"trick\" id=\"trick0\">\n";
258 for($pos=1;$pos<5;$pos++)
260 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
261 $userid = DB_get_userid('gameid-position',$gameid,$pos);
262 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
264 if($userstatus=='start' || $userstatus=='init')
265 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs to decide </div>\n"; /* show this to everyone */
267 if($usersick!=NULL && $pos<=$mypos ) /* only show this for people sitting before the player */
268 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
269 else if($usersick==NULL && $pos<=$mypos)
270 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
272 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
273 /* end displaying sickness */
275 if(!myisset('solo','wedding','poverty','nines') )
277 output_check_for_sickness($me,$mycards);
279 echo "<div class=\"mycards\">Your cards are: <br />\n";
280 foreach($mycards as $card)
281 display_card($card,$PREF['cardset']);
288 /* check if someone selected more than one sickness */
290 if($_REQUEST['solo']!='No') $Nsickness++;
291 if($_REQUEST['wedding'] == 'yes') $Nsickness++;
292 if($_REQUEST['poverty'] == 'yes') $Nsickness++;
293 if($_REQUEST['nines'] == 'yes') $Nsickness++;
297 echo "<p class=\"message\"> You selected more than one sickness, please go back ".
298 "and answer the <a href=\"$INDEX?action=game&me=$me&in=yes\">question</a> again.</p>";
300 echo "<div class=\"mycards\">Your cards are: <br />\n";
301 foreach($mycards as $card)
302 display_card($card,$PREF['cardset']);
309 /* everything is ok, save what user said and proceed */
310 echo "<p class=\"message\">Processing what you selected in the last step...";
312 /* check if this sickness needs to be handled first */
313 $gametype = DB_get_gametype_by_gameid($gameid);
314 $startplayer = DB_get_startplayer_by_gameid($gameid); /* need this to check which solo goes first */
316 if( $_REQUEST['solo']!='No' )
318 /* user wants to play a solo */
320 /* store the info in the user's hand info */
321 DB_set_solo_by_hash($me,$_REQUEST['solo']);
322 DB_set_sickness_by_hash($me,'solo');
324 echo "<br />Seems like you want to play a {$_REQUEST['solo']} solo. Got it.<br />\n";
326 if($gametype == 'solo' && $startplayer<$mypos)
327 {}/* do nothing, since someone else already is playing solo */
330 /* this solo comes first
331 * store info in game table
333 DB_set_gametype_by_gameid($gameid,'solo');
334 DB_set_startplayer_by_gameid($gameid,$mypos);
335 DB_set_solo_by_gameid($gameid,$_REQUEST['solo']);
338 else if($_REQUEST['wedding'] == 'yes')
340 /* silent solo is set further down */
341 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
342 DB_set_sickness_by_hash($me,'wedding');
344 else if($_REQUEST['poverty'] == 'yes')
346 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
347 DB_set_sickness_by_hash($me,'poverty');
349 else if($_REQUEST['nines'] == 'yes')
351 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
352 " is playing solo, this game will be canceled.<br />\n";
353 DB_set_sickness_by_hash($me,'nines');
358 /* move on to the next stage*/
359 DB_set_hand_status_by_hash($me,'check');
364 /* here we check what all players said and figure out what game we are playing
365 * this can therefore only be handled once all players finished the last stage
368 /* only need to redisplay the cards when the user reloads the page and lands directly here */
369 if($mystatus=='check')
371 $mycards = DB_get_hand($me);
372 $mycards = mysort($mycards,$gametype);
374 /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
375 echo "\n<ul class=\"tricks\">\n";
376 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
377 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
378 " <div class=\"trick\" id=\"trick0\">\n";
380 for($pos=1;$pos<5;$pos++)
382 $usersick = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
383 $userid = DB_get_userid('gameid-position',$gameid,$pos);
384 $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
386 if($userstatus=='start' || $userstatus=='init')
387 echo " <div class=\"vorbehalt".($pos-1)."\"> still needs to decide </div>\n"; /* show this to everyone */
389 if($usersick!=NULL) /* in the init-phase we only showed players with $pos<$mypos, now we can show all */
390 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
392 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
394 echo " </div>\n </li>\n</ul>\n"; /* end div trick, end li trick , end tricks*/
395 /* end displaying sickness */
398 echo "<div class=\"message\">\n";
399 echo "<p> Checking if someone else selected solo, nines, wedding or poverty.</p>";
401 /* check if everyone has reached this stage */
402 $userids = DB_get_all_userid_by_gameid($gameid);
404 foreach($userids as $user)
406 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
407 if($userstat!='check')
410 DB_set_player_by_gameid($gameid,$user);
417 echo "<p>This step can only be handled after everyone finished the last step. ".
418 "Seems like this is not the case, so you need to wait a bit... ".
419 "you will get an email once that is the case, please use the link in ".
420 "that email to continue the game.</p></div>";
422 /* display cards, if player was just at the init-phase he will still see the cards from there
423 * we can put this one here, since the last player to finish the init state won't get here and
424 * will still see his card anyway from the init-phase
426 if($mystatus=='check')
429 echo "<div class=\"mycards\">Your cards are: <br />\n";
430 foreach($mycards as $card)
431 display_card($card,$PREF['cardset']);
438 /* Ok, everyone finished the init-phase, time to figure out what game we
439 * are playing, in case there are any solos this already
440 * will have the correct information in it */
442 echo "<p> Ok, everyone is done... figuring out what kind of game we are playing.</p>";
444 $gametype = DB_get_gametype_by_gameid($gameid);
445 $startplayer = DB_get_startplayer_by_gameid($gameid);
447 /* check for sickness */
452 foreach($userids as $user)
454 $name = DB_get_name('userid',$user);
455 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
456 if($usersick == 'nines')
459 break; /* no need to check for other poverties, since only solo can win and that is already set */
461 else if($usersick == 'poverty')
463 else if($usersick == 'wedding')
465 else if($usersick == 'solo')
469 /* now check which sickness comes first and set the gametype to it */
470 if($gametype == 'solo')
477 /* TODO: should we keep statistics of this? */
478 $message = "Hello, \n\n".
479 " the game has been canceled because ".DB_get_name('userid',$nines).
480 " has five or more nines and nobody is playing solo.\n\n".
481 " To redeal either start a new game or, in case the game was part of a tournament, \n".
482 " go to the last game and use the link at the bottom of the page to redeal.";
484 $userids = DB_get_all_userid_by_gameid($gameid);
485 foreach($userids as $user)
487 $To = DB_get_email('userid',$user);
488 mymail($To,"$EmailName game ".DB_format_gameid($gameid)." canceled",$message);
491 /* delete everything from the dB */
494 echo "The game has been canceled because ".DB_get_name('userid',$nines).
495 " has five or more nines and nobody is playing solo.\n";
498 else if($poverty==1) /* one person has poverty */
500 DB_set_gametype_by_gameid($gameid,'poverty');
501 $gametype = 'poverty';
502 $who = DB_get_sickness_by_gameid($gameid);
505 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
506 if($firstsick == 'poverty')
507 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
509 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
512 else if($poverty==2) /* two people have poverty */
514 DB_set_gametype_by_gameid($gameid,'dpoverty');
515 $gametype = 'dpoverty';
516 $who = DB_get_sickness_by_gameid($gameid);
519 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
520 if($firstsick == 'poverty')
522 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
523 if($secondsick == 'poverty')
524 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
526 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
529 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
534 DB_set_gametype_by_gameid($gameid,'wedding');
535 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
536 $gametype = 'wedding';
538 /* now the gametype is set correctly in the database */
539 echo "<p> Got it :)</p>";
541 /* loop over all players, set re/contra if possible and start the game if possible */
542 $userids = DB_get_all_userid_by_gameid($gameid);
543 foreach($userids as $userid)
545 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
550 /* are we the solo player? set us to re, else set us to contra */
551 $pos = DB_get_pos_by_hash($userhash);
552 if($pos == $startplayer)
553 DB_set_party_by_hash($userhash,'re');
555 DB_set_party_by_hash($userhash,'contra');
556 DB_set_hand_status_by_hash($userhash,'play');
560 /* set person with the wedding to re, do the rest during the game */
561 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
562 if($usersick == 'wedding')
563 DB_set_party_by_hash($userhash,'re');
565 DB_set_party_by_hash($userhash,'contra');
567 DB_set_hand_status_by_hash($userhash,'play');
571 $hand = DB_get_all_hand($userhash);
573 if(in_array('3',$hand)||in_array('4',$hand))
574 DB_set_party_by_hash($userhash,'re');
576 DB_set_party_by_hash($userhash,'contra');
577 DB_set_hand_status_by_hash($userhash,'play');
581 /* set person with poverty to play status */
582 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
583 if($usersick == 'poverty')
584 DB_set_hand_status_by_hash($userhash,'play');
586 /* set status of first player to be asked to poverty */
587 $who = DB_get_sickness_by_gameid($gameid);
588 if($who > 6) $who= $who/10; /* in case we have dpoverty */
589 $whoid = DB_get_userid('gameid-position',$gameid,$who);
591 DB_set_hand_status_by_hash($userhash,'poverty');
594 /* check for silent solo, set game type to solo in this case */
595 $gametype = DB_get_gametype_by_gameid($gameid);
596 $userids = DB_get_all_userid_by_gameid($gameid);
597 foreach($userids as $userid)
599 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
601 if($gametype=='normal')
603 $userhand = DB_get_all_hand($userhash);
604 if(check_wedding($userhand))
606 /* normal game type and player has both queens -> silent solo */
607 /* keep startplayer, just set gametype to silent solo */
608 DB_set_gametype_by_gameid($gameid,'solo');
609 DB_set_solo_by_gameid($gameid,'silent');
614 /* send out email to first player or poverty person*/
615 if($gametype!='poverty' && $gametype!='dpoverty')
617 $startplayer = DB_get_startplayer_by_gameid($gameid);
618 $email = DB_get_email('position-gameid',$startplayer,$gameid);
619 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
620 $who = DB_get_userid('email',$email);
621 DB_set_player_by_gameid($gameid,$who);
625 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
627 /* email startplayer */
628 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
629 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
630 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
634 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
638 /* set status of first player to be asked to poverty */
639 $who = DB_get_sickness_by_gameid($gameid);
640 if($who > 6) $who= $who/10; /* in case we have dpoverty */
642 $whoid = DB_get_userid('gameid-position',$gameid,$who);
644 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
647 $email = DB_get_email('position-gameid',$who,$gameid);
648 $whohash = DB_get_hash_from_game_and_pos($gameid,$who);
649 DB_set_player_by_gameid($gameid,$whoid);
651 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
653 /* email player for poverty */
654 $message = "Poverty: It's your turn now in game ".DB_format_gameid($gameid).".\n".
655 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$whohash."\n\n" ;
656 mymail($email,$EmailName."Poverty (game ".DB_format_gameid($gameid).") ",$message);
664 /* user only gets here in a poverty game, several things have to be handled here:
665 * A) ask, if user wants to take trump
667 * poverty: set re/contra
668 * dpoverty: first time: set re, send email to second player
669 * second time: set contra
670 * poverty: set status of other players to 'play'
671 * set status to play in case 0 trump
672 * no -> set status to play,
673 * ask next player or cancle the game if no more players
674 * B) user took trump and has too many cards (e.g. count(cards)>12 and re/contra set)
675 * ask to give cards back, set status to play, once player has 12 cards
677 * it is easier to check B) first
680 /* output pre game in case user reloads */
683 set_gametype($gametype); /* this sets the $CARDS variable */
684 $myparty = DB_get_party_by_hash($me);
686 /* the following is part B) of whats needs to be done)
687 /* check if user wants to give cards back */
688 if(myisset('exchange'))
690 $exchange = $_REQUEST['exchange'];
691 $partnerhash = DB_get_partner_hash_by_hash($me);
692 $partnerid = DB_get_userid('hash',$partnerhash);
693 $partnerhand = DB_get_handid('gameid-userid',$gameid,$partnerid);
695 /* if exchange is set to a value>0, exchange that card back to the partner */
698 $result = DB_query("UPDATE Hand_Card SET hand_id='$partnerhand'".
699 " WHERE hand_id='$myhand' AND card_id=".DB_quote_smart($exchange));
704 $mycards = DB_get_hand($me);
705 $mycards = mysort($mycards,$gametype);
707 /* check if user need to give more cards back */
708 if( ($myparty=='re' || $myparty=='contra') && count($mycards)>12)
710 echo "<div class=\"poverty\"> you need to get rid of a few cards</div>\n";
713 echo "<div class=\"mycards\">Your cards are: <br />\n";
714 foreach($mycards as $card)
715 display_link_card($card,$PREF['cardset'],$type);
716 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select card to give back\" />\n";
719 else if( ($myparty=='re' || $myparty=='contra') && count($mycards)==12)
721 /* user is done, ready to play */
722 DB_set_hand_status_by_hash($me,'play');
724 /* email start player */
725 $startplayer = DB_get_startplayer_by_gameid($gameid);
726 $email = DB_get_email('position-gameid',$startplayer,$gameid);
727 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
728 $who = DB_get_userid('email',$email);
729 DB_set_player_by_gameid($gameid,$who);
733 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
735 /* email startplayer */
736 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
737 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
738 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
742 echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
745 /* the following is part A) of what needs to be done */
746 if(!myisset('trump'))
750 echo "<div class=\"poverty\">\n";
751 $userids = DB_get_all_userid_by_gameid($gameid);
752 foreach($userids as $user)
754 $name = DB_get_name('userid',$user);
755 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
756 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
757 $userparty = DB_get_party_by_hash($userhash);
759 if($usersick=='poverty' && !$userparty)
761 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
762 $cards = DB_get_hand($hash);
765 foreach($cards as $card)
766 if($card<27) $nrtrump++;
767 echo "Player $name has $nrtrump trump. Do you want to take them?".
768 "<a href=\"index.php?action=game&me=$me&trump=$user\">Yes</a> <br />\n";
771 echo "<a href=\"index.php?action=game&me=$me&trump=no\">No way</a> <br />\n";
772 echo "</div><div>\n";
774 echo "<div class=\"mycards\">Your cards are: <br />\n";
775 foreach($mycards as $card)
776 display_card($card,$PREF['cardset']);
777 echo "</div></div>\n";
783 $trump = $_REQUEST['trump'];
787 /* user doesn't want to take trump */
788 DB_set_hand_status_by_hash($me,'play');
790 /* set next player who needs to be asked and email him*/
791 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
792 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
794 /* don't ask people who have poverty */
796 if($firstsick=='poverty')
798 if($secondsick=='poverty')
803 if($gametype=='dpoverty')
805 $next=999; /* need to cancel for sure, since both would need to take the trump */
808 /* no more people to ask, need to cancel the game */
811 $message = "Hello, \n\n".
812 "Game ".DB_format_gameid($gameid)." has been canceled since nobody wanted to take the trump.\n";
814 $userids = DB_get_all_userid_by_gameid($gameid);
815 foreach($userids as $user)
817 $To = DB_get_email('userid',$user);
818 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled (poverty not resolved)",$message);
821 /* delete everything from the dB */
824 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been canceled.<br /><br /></p>";
829 /* email next player, set his status to poverty */
830 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
831 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
832 $userid = DB_get_userid('email',$To);
834 DB_set_player_by_gameid($gameid,$userid);
835 DB_set_hand_status_by_hash($userhash,'poverty');
837 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
838 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
839 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
844 /* player wants to take trump, change cards */
846 /* user wants to take trump */
847 $trump = $_REQUEST['trump'];
848 $userhand = DB_get_handid('gameid-userid',$gameid,$trump);
849 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
851 /* copy trump from player A to B */
852 $result = DB_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
855 $mycards = DB_get_hand($me);
858 if($gametype=='poverty')
860 $userids = DB_get_all_userid_by_gameid($gameid);
861 foreach($userids as $user)
863 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
864 if($hash==$userhash||$hash==$me)
866 DB_set_party_by_hash($hash,'re');
870 DB_set_party_by_hash($hash,'contra');
871 DB_set_hand_status_by_hash($hash,'play'); /* the contra party is ready to play */
874 /* check if we are done (in case of no trump handed over), if so, go to 'play' phase right away*/
875 if(count($mycards)==12)
877 DB_set_hand_status_by_hash($me,'play');
882 /* has the re party already been set?*/
884 $userids = DB_get_all_userid_by_gameid($gameid);
885 foreach($userids as $user)
887 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
888 $party = DB_get_party_by_hash($hash);
894 DB_set_party_by_hash($me,'contra');
895 DB_set_party_by_hash($userhash,'contra');
899 DB_set_party_by_hash($me,'re');
900 DB_set_party_by_hash($userhash,'re');
902 /* send out email to second non-poverty player */
903 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
904 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
907 if($firstsick=='poverty')
908 if($secondsick=='poverty')
914 echo "<div class=\"message\">Error in poverty, please contact the Admin</div>\n";
916 $To = DB_get_email('position-gameid',$mypos+$next,$gameid);
917 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
918 $userid = DB_get_userid('email',$To);
920 DB_set_player_by_gameid($gameid,$userid);
921 DB_set_hand_status_by_hash($userhash,'poverty');
923 $message = "Two people have poverty, it's your turn to decide, if you want to take the trump. Please visit:".
924 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
925 mymail($To,$EmailName." double poverty (game ".DB_format_gameid($gameid).")",$message);
930 echo "<div class=\"message\"> Please, <a href=\"$INDEX?action=game&me=$me\">continue</a> here.</div>\n";
938 /* both entries here, so that the tricks are visible for both.
939 * in case of 'play' there is a break later that skips the last part
942 /* check if all players are ready to play,
943 * if so, send out email to the startplayer
944 * only need to do this if the game hasn't started yet
946 $gamestatus = DB_get_game_status_by_gameid($gameid);
947 if($gamestatus == 'pre')
950 $userids = DB_get_all_userid_by_gameid($gameid);
951 foreach($userids as $user)
953 $userstatus = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
954 if($userstatus !='play' && $userstatus!='gameover')
957 DB_set_player_by_gameid($gameid,$user);
963 /* only set this after all poverty, etc. are handled*/
964 DB_set_game_status_by_gameid($gameid,'play');
966 /* email startplayer */
967 $startplayer = DB_get_startplayer_by_gameid($gameid);
968 $email = DB_get_email('position-gameid',$startplayer,$gameid);
969 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
970 $who = DB_get_userid('email',$email);
971 DB_set_player_by_gameid($gameid,$who);
973 if($hash!=$me && DB_get_email_pref_by_hash($hash)!='emailaddict')
975 /* email startplayer) */
976 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
977 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
978 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
982 /* figure out what kind of game we are playing,
983 * set the global variables $CARDS['trump'],$CARDS['diamonds'],$CARDS['hearts'],
984 * $CARDS['clubs'],$CARDS['spades'],$CARDS['foxes']
988 $gametype = DB_get_gametype_by_gameid($gameid);
990 if($gametype=='solo')
992 $gametype = DB_get_solo_by_gameid($gameid);
993 if($gametype=='silent')
996 $GT = $gametype.' '.$GT;
999 $gametype = 'normal';
1001 set_gametype($gametype); /* this sets the $CARDS variable */
1003 /* get some infos about the game, need to reset this, since it might have changed */
1004 $gamestatus = DB_get_game_status_by_gameid($gameid);
1006 /* has the game started? No, then just wait here...*/
1007 if($gamestatus == 'pre')
1009 echo "<p class=\"message\"> You finished the setup, but not everyone else finished it... ".
1010 "You need to wait for the others. Just wait for an email. </p>";
1012 $mycards = DB_get_hand($me);
1013 $mycards = mysort($mycards,$gametype);
1015 echo "<div class=\"mycards\">Your cards are: <br />\n";
1016 foreach($mycards as $card)
1017 display_card($card,$PREF['cardset']);
1020 break; /* not sure this works... the idea is that you can
1021 * only play a card after everyone is ready to play */
1024 /* get time from the last action of the game */
1025 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1026 $gameend = time() - strtotime($r[0]);
1028 /* handle comments in case player didn't play a card, allow comments a week after the end of the game */
1029 if( (!myisset('card') && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
1030 if(myisset('comment'))
1032 $comment = $_REQUEST['comment'];
1033 $playid = DB_get_current_playid($gameid);
1036 DB_insert_comment($comment,$playid,$myid);
1039 /* get everything relevant to display the tricks */
1040 $result = DB_query("SELECT Hand_Card.card_id as card,".
1041 " Hand.position as position,".
1042 " Play.sequence as sequence, ".
1044 " GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
1045 " SEPARATOR '\n' ), ".
1046 " Play.create_date, ".
1049 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1050 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1051 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1052 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1053 "LEFT JOIN User On User.id=Comment.user_id ".
1054 "WHERE Trick.game_id='".$gameid."' ".
1055 "GROUP BY Trick.id, sequence ".
1056 "ORDER BY Trick.id, sequence ASC");
1058 $lasttrick = DB_get_max_trickid($gameid);
1060 $play = array(); /* needed to calculate winner later */
1062 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1063 $firstcard = ''; /* first card in a trick */
1065 echo "\n<ul class=\"tricks\">\n";
1066 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
1068 /* output vorbehalte */
1069 $mygametype = DB_get_gametype_by_gameid($gameid);
1070 if($mygametype != 'normal' && $mygametype != 'silent') /* only show when needed */
1072 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
1073 " <div class=\"trick\" id=\"trick0\">\n";
1075 for($mypos=1;$mypos<5;$mypos++)
1077 $usersick = DB_get_sickness_by_pos_and_gameid($mypos,$gameid);
1080 echo " <div class=\"vorbehalt".($mypos-1)."\"> Vorbehalt <br />";
1082 echo " $usersick <br />";
1085 if($mygametype == $usersick)
1089 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1093 while($r = DB_fetch_array($result))
1101 /* count number of tricks */
1105 /* check if first schweinchen has been played */
1106 if( $GAME['schweinchen-who'] && ($r[0] == 19 || $r[0] == 20) )
1107 if(!$GAME['schweinchen-first'])
1108 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1110 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1112 /* save card to be able to find the winner of the trick later */
1113 $play[$seq] = array('card'=>$r[0],'pos'=>$pos);
1117 /* first card in a trick, output some html */
1118 if($trick!=$lasttrick)
1120 /* start of an old trick? */
1121 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1122 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1123 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1125 else if($trick==$lasttrick)
1127 /* start of a last trick? */
1128 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1129 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1130 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1133 /* remember first card, so that we are able to check, what cards can be played */
1138 echo " <div class=\"card".($pos-1)."\">\n";
1140 /* display comments */
1142 echo " <span class=\"comment\">".$comment."</span>\n";
1145 display_card($r[0],$PREF['cardset']);
1147 echo " </div>\n"; /* end div card */
1152 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1156 /* whos turn is it? */
1159 $winner = get_winner($play,$gametype); /* returns the position */
1161 $firstcard = ''; /* new trick, no first card */
1166 if($next==5) $next = 1;
1169 /* my turn?, display cards as links, ask for comments*/
1170 if(DB_get_pos_by_hash($me) == $next)
1175 /* do we want to play a card? */
1176 if(myisset('card') && $myturn)
1178 $card = $_REQUEST['card'];
1179 $handid = DB_get_handid('hash',$me);
1180 $commentSchweinchen =''; /* used to add a comment when Schweinchen is being played */
1182 /* check if we have card and that we haven't played it yet*/
1183 /* set played in hand_card to true where hand_id and card_id*/
1184 $r = DB_query_array("SELECT id FROM Hand_Card WHERE played='false' and ".
1185 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1186 $handcardid = $r[0];
1188 if($handcardid) /* everything ok, play card */
1190 /* update Game timestamp */
1191 DB_update_game_timestamp($gameid);
1193 /* mark card as played */
1194 DB_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1195 DB_quote_smart($card));
1197 /* get trick id or start new trick */
1198 $a = DB_get_current_trickid($gameid);
1203 $playid = DB_play_card($trickid,$handcardid,$sequence);
1205 /* check special output for schweinchen in case in case a fox is being played
1206 * check for correct rules, etc. has already been done
1208 if( $GAME['schweinchen-who'] && ($card == 19 || $card == 20) )
1210 if(!$GAME['schweinchen-first'])
1211 $GAME['schweinchen-first'] = 1; /* playing the first fox */
1213 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1215 if( ($GAME['schweinchen-second']==1 && $RULES['schweinchen']=='second') || $RULES['schweinchen']=='both')
1217 DB_insert_comment('Schweinchen! ',$playid,$myid);
1218 $commentSchweinchen = 'Schweinchen! ';
1221 echo 'schweinchen = '.$GAME['schweinchen-who'].' ---<br />';
1224 /* if sequence == 4 check who one in case of wedding */
1225 if($sequence == 4 && $GT == 'wedding')
1227 /* is wedding resolve */
1228 $resolved = DB_get_sickness_by_gameid($gameid);
1231 /* who has wedding */
1232 $userids = DB_get_all_userid_by_gameid($gameid);
1233 foreach($userids as $user)
1235 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1236 if($usersick == 'wedding')
1239 /* who won the trick */
1240 $play = DB_get_cards_by_trick($trickid);
1241 $winner = get_winner($play,$gametype); /* returns the position */
1242 $winnerid = DB_get_userid('gameid-position',$gameid,$winner);
1243 /* is tricknr <=3 */
1244 if($tricknr <=3 && $winnerid!=$whosick)
1246 /* set resolved at tricknr*/
1247 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1249 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1250 DB_set_party_by_hash($whash,'re');
1252 if($tricknr == 3 && $winnerid==$whosick)
1254 /* set resolved at tricknr*/
1255 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1260 /* if sequence == 4, set winner of the trick, count points and set the next player */
1263 $play = DB_get_cards_by_trick($trickid);
1264 $winner = get_winner($play,$gametype); /* returns the position */
1266 /* check if someone caught a fox */
1267 /* first check if we should account for solos at all,
1268 * since it doesn't make sense in some games
1270 $ok = 0; /* fox shouldn't be counted */
1271 if(DB_get_gametype_by_gameid($gameid)=='solo')
1273 $solo = DB_get_solo_by_gameid($gameid);
1274 if($solo == 'trump' || $solo == 'silent')
1275 $ok = 1; /* for trump solos and silent solos, foxes are ok */
1278 $ok = 1; /* for all other games (not solos) foxes are ok too */
1281 foreach($play as $played)
1283 if ( $played['card']==19 || $played['card']==20 )
1284 if ($played['pos']!= $winner )
1286 /* possible caught a fox, check party */
1287 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1288 $uid2 = DB_get_userid('gameid-position',$gameid,$played['pos']);
1290 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1291 $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1293 if($party1 != $party2)
1294 DB_query("INSERT INTO Score".
1295 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1299 /* check for karlchen (jack of clubs in the last trick)*/
1300 /* same as for foxes, karlchen doesn't always make sense
1301 * check what kind of game it is and set karlchen accordingly */
1302 $ok = 1; /* default: karlchen should be accounted for */
1304 $ok = 0; /* Karlchen works only in the last trick */
1305 if($ok && DB_get_gametype_by_gameid($gameid)=='solo' )
1307 $solo = DB_get_solo_by_gameid($gameid);
1308 if($solo == 'trumpless' || $solo == 'jack' || $solo == 'queen' )
1309 $ok = 0; /* no Karlchen in these solos */
1313 foreach($play as $played)
1314 if ( $played['card']==11 || $played['card']==12 )
1315 if ($played['pos'] == $winner )
1317 /* possible caught a fox, check party */
1318 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1319 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1321 DB_query("INSERT INTO Score".
1322 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1324 /* check for doppelopf (>40 points)*/
1326 foreach($play as $played)
1328 $points += DB_get_card_value_by_cardid($played['card']);
1332 $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1333 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1335 DB_query("INSERT INTO Score".
1336 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
1340 DB_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1342 echo "ERROR during scoring";
1345 echo "DEBUG: position $winner won the trick <br />";
1347 /* who is the next player? */
1352 $next = DB_get_pos_by_hash($me)+1;
1354 if($next==5) $next=1;
1356 /* check for coment */
1357 if(myisset('comment'))
1359 $comment = $_REQUEST['comment'];
1361 DB_insert_comment($comment,$playid,$myid);
1362 if($commentSchweinchen)
1363 $comment = $commentSchweinchen . $comment;
1366 /* display played card */
1367 $pos = DB_get_pos_by_hash($me);
1370 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1371 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1372 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1375 echo " <div class=\"card".($pos-1)."\">\n ";
1377 /* display comments */
1378 display_card($card,$PREF['cardset']);
1380 echo "\n <span class=\"comment\"> ".$comment."</span>\n";
1383 /*check if we still have cards left, else set status to gameover */
1384 if(sizeof(DB_get_hand($me))==0)
1386 DB_set_hand_status_by_hash($me,'gameover');
1387 $mystatus = 'gameover';
1390 /* if all players are done, set game status to game over,
1391 * get the points of the last trick and send out an email
1394 $userids = DB_get_all_userid_by_gameid($gameid);
1397 foreach($userids as $user)
1398 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1402 DB_set_game_status_by_gameid($gameid,'gameover');
1404 /* email next player, if game is still running */
1405 if(DB_get_game_status_by_gameid($gameid)=='play')
1407 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1408 $email = DB_get_email('hash',$next_hash);
1409 $who = DB_get_userid('email',$email);
1410 DB_set_player_by_gameid($gameid,$who);
1412 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1413 "It's your turn now.\n".
1414 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$next_hash."\n\n" ;
1415 if( DB_get_email_pref_by_uid($who)!='emailaddict' )
1416 mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
1418 else /* send out final email */
1420 /* individual score */
1421 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1422 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1423 " LEFT JOIN User ON User.id=Hand.user_id".
1424 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1425 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1426 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1427 " WHERE Hand.game_id='$gameid'".
1428 " GROUP BY User.fullname" );
1429 $message = "The game is over. Thanks for playing :)\n";
1430 $message .= "Final score:\n";
1431 while( $r = DB_fetch_array($result) )
1432 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1434 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1435 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1436 " LEFT JOIN User ON User.id=Hand.user_id".
1437 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1438 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1439 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1440 " WHERE Hand.game_id='$gameid'".
1441 " GROUP BY Hand.party" );
1442 $message .= "\nTotals:\n";
1445 while( $r = DB_fetch_array($result) )
1447 $message .= " ".$r[0]." ".$r[1]."\n";
1450 else if($r[0] == 'contra')
1455 * save score in database
1459 /* get calls from re/contra */
1461 $call_contra = NULL;
1462 foreach($userids as $user)
1464 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1465 $call = DB_get_call_by_hash($hash);
1466 $party = DB_get_party_by_hash($hash);
1470 $call = (int) $call;
1476 else if( $call < $call_re)
1479 else if($party=='contra')
1481 if($call_contra==NULL)
1482 $call_contra = $call;
1483 else if( $call < $call_re)
1484 $call_contra = $call;
1489 /* figure out who one */
1490 $winning_party = NULL;
1492 if($call_re == NULL && $call_contra==NULL)
1494 /* nobody made a call, so it's easy to figure out who won */
1496 $winning_party='re';
1498 $winning_party='contra';
1502 /* if one party makes a call, they only win, iff they make enough points
1503 * if only one party made a call, the other one wins,
1504 * if the first one didn't make it
1508 $offset = 120 - $call_re;
1510 $offset--; /* since we use a > in the next equation */
1512 if($re > 120+$offset)
1513 $winning_party='re';
1514 else if ($call_contra == NULL )
1515 $winning_party='contra';
1520 $offset = 120 - $call_contra;
1521 if($call_contra == 0)
1522 $offset--; /* since we use a > in the next equation */
1524 if($contra > 120+$offset)
1525 $winning_party='contra';
1526 else if ($call_re == NULL )
1527 $winning_party='re';
1531 /* one point for each call of the other party in case the other party didn't win
1532 * and one point each in case the party made more than points than one of the calls
1534 if($winning_party!='contra' && $call_contra!=NULL)
1536 for( $p=$call_contra;$p<=120; $p+=30 )
1538 DB_query("INSERT INTO Score".
1539 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
1542 for( $p=$call_contra; $p<120; $p+=30)
1545 DB_query("INSERT INTO Score".
1546 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'made$p')");
1549 if($winning_party!='re' and $call_re!=NULL)
1551 for( $p=$call_re;$p<=120; $p+=30 )
1553 DB_query("INSERT INTO Score".
1554 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'against$p')");
1557 for( $p=$call_re; $p<120; $p+=30)
1560 DB_query("INSERT INTO Score".
1561 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
1565 /* point in case contra won */
1566 if($winning_party=='contra')
1568 DB_query("INSERT INTO Score".
1569 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1572 /* one point each for winning and each 30 points + calls */
1573 if($winning_party=='re')
1575 foreach(array(120,150,180,210,240) as $p)
1578 if($p==240 || $call_contra!=NULL)
1582 DB_query("INSERT INTO Score".
1583 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
1585 /* re called something and won */
1586 foreach(array(0,30,60,90,120) as $p)
1588 if($call_re!=NULL && $call_re<$p+1)
1589 DB_query("INSERT INTO Score".
1590 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
1593 else if( $winning_party=='contra')
1595 foreach(array(120,150,180,210,240) as $p)
1598 if($p==240 || $call_re!=NULL)
1601 if($contra>$p-$offset)
1602 DB_query("INSERT INTO Score".
1603 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
1605 /* re called something and won */
1606 foreach(array(0,30,60,90,120) as $p)
1608 if($call_contra!=NULL && $call_contra<$p+1)
1609 DB_query("INSERT INTO Score".
1610 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
1615 /* add score points to email */
1618 $message .= " Points Re: \n";
1619 $queryresult = DB_query("SELECT score FROM Score ".
1620 " WHERE game_id=$gameid AND party='re'".
1622 while($r = DB_fetch_array($queryresult) )
1624 $message .= " ".$r[0]."\n";
1627 $message .= " Points Contra: \n";
1628 $queryresult = DB_query("SELECT score FROM Score ".
1629 " WHERE game_id=$gameid AND party='contra'".
1631 while($r = DB_fetch_array($queryresult) )
1633 $message .= " ".$r[0]."\n";
1636 $message .= " Total Points (from the Re point of view): $Tpoint\n";
1639 $session = DB_get_session_by_gameid($gameid);
1640 $score = generate_score_table($session);
1642 $message .= "Score Table:\n";
1643 $message .= format_score_table_ascii($score);
1645 /* send out final email */
1648 foreach($userids as $user)
1649 $all[] = DB_get_email('userid',$user);
1650 $To = implode(",",$all);
1652 $help = "\n\n (you can use reply all on this email to reach all the players.)\n";
1653 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 1(2)",$message.$help);
1655 foreach($userids as $user)
1657 $To = DB_get_email('userid',$user);
1658 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1660 $link = "Use this link to have a look at game ".DB_format_gameid($gameid).": ".
1661 $HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
1662 if( DB_get_email_pref_by_uid($user) != 'emailaddict' )
1663 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 2(2)",$link);
1669 echo "can't find that card?! <br />\n";
1672 else if(myisset('card') && !$myturn )
1674 echo "please wait until it's your turn! <br />\n";
1677 if($seq!=4 && $trickNR>=1)
1678 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1680 /* display points in case game is over */
1681 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1683 echo " <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1684 " <div class=\"trick\" id=\"trick13\">\n";
1685 /* add pic for re/contra
1686 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1688 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1689 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1690 " LEFT JOIN User ON User.id=Hand.user_id".
1691 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1692 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1693 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1694 " WHERE Hand.game_id='$gameid'".
1695 " GROUP BY User.fullname" );
1696 while( $r = DB_fetch_array($result))
1697 echo " <div class=\"card".($r[3]-1)."\">\n".
1698 " <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
1701 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1704 echo " <li onclick=\"hl_prev();\" class=\"old\"><a href=\"#\">prev</a></li>\n";
1705 echo " <li onclick=\"hl_next();\" class=\"old\"><a href=\"#\">next</a></li>\n";
1706 echo "</ul>\n"; /* end ul tricks*/
1708 $mycards = DB_get_hand($me);
1709 $mycards = mysort($mycards,$gametype);
1710 echo "<div class=\"mycards\">\n";
1712 if($myturn && !myisset('card') && $mystatus=='play' )
1714 echo "Hello ".$myname.", it's your turn! <br />\n";
1715 echo "Your cards are: <br />\n";
1717 /* do we have to follow suite? */
1719 if(have_suit($mycards,$firstcard))
1722 foreach($mycards as $card)
1724 if( ($followsuit && !same_type($card,$firstcard)) ||
1725 ( (int)($card)==19 && ($RULES['schweinchen']=='second'||$RULES['schweinchen']=='secondaftercall')
1726 && $GAME['schweinchen-who']==$me && !$GAME['schweinchen-first'] )
1728 display_card($card,$PREF['cardset']);
1730 display_link_card($card,$PREF['cardset']);
1733 else if($mystatus=='play' )
1735 echo "Your cards are: <br />\n";
1736 foreach($mycards as $card)
1737 display_card($card,$PREF['cardset']);
1739 else if($mystatus=='gameover')
1741 $oldcards = DB_get_all_hand($me);
1742 $oldcards = mysort($oldcards,$gametype);
1743 echo "Your cards were: <br />\n";
1744 foreach($oldcards as $card)
1745 display_card($card,$PREF['cardset']);
1747 $userids = DB_get_all_userid_by_gameid($gameid);
1748 foreach($userids as $user)
1750 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1756 $name = DB_get_name('userid',$user);
1757 $oldcards = DB_get_all_hand($userhash);
1758 $oldcards = mysort($oldcards,$gametype);
1759 echo "$name's cards were: <br />\n";
1760 foreach($oldcards as $card)
1761 display_card($card,$PREF['cardset']);
1767 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1768 if($mystatus=='play')
1771 /* the following happens only when the gamestatus is 'gameover' */
1772 /* check if game is over, display results */
1773 if(DB_get_game_status_by_gameid($gameid)=='play')
1775 echo "The game is over for you.. other people still need to play though";
1779 $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1780 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1781 " LEFT JOIN User ON User.id=Hand.user_id".
1782 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1783 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1784 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1785 " WHERE Hand.game_id='$gameid'".
1786 " GROUP BY Hand.party" );
1787 echo "<div class=\"total\"> Totals:<br />\n";
1788 while( $r = DB_fetch_array($result))
1789 echo " ".$r[0]." ".$r[1]."<br />\n";
1791 $queryresult = DB_query("SELECT timediff(mod_date,create_date) ".
1792 " FROM Game WHERE id='$gameid'");
1793 $r = DB_fetch_array($queryresult);
1794 echo "<p>This game took ".$r[0]." hours.</p>";
1796 echo "<div class=\"re\">\n Points Re: <br />\n";
1797 $queryresult = DB_query("SELECT score FROM Score ".
1798 " WHERE game_id=$gameid AND party='re'".
1800 while($r = DB_fetch_array($queryresult) )
1801 echo " ".$r[0]."<br />\n";
1804 echo "<div class=\"contra\">\n Points Contra: <br />\n";
1805 $queryresult = DB_query("SELECT score FROM Score ".
1806 " WHERE game_id=$gameid AND party='contra'".
1808 while($r = DB_fetch_array($queryresult) )
1809 echo " ".$r[0]."<br />\n";
1818 myerror("error in testing the status");
1820 /* output left menu */
1821 display_user_menu();
1823 /* output right menu */
1825 /* display rule set for this game */
1826 echo "<div class=\"gameinfo\">\n";
1828 if($gamestatus == 'play' )
1829 output_form_calls($me);
1831 /* get time from the last action of the game */
1832 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1833 $gameend = time() - strtotime($r[0]);
1835 if($gamestatus == 'play' || $gameend < 60*60*24*7)
1837 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1840 echo "<input type=\"submit\" value=\"submit\" />\n";
1846 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1849 $session = DB_get_session_by_gameid($gameid);
1850 $result = DB_query("SELECT id,create_date FROM Game".
1851 " WHERE session=$session".
1852 " ORDER BY create_date DESC".
1856 $r = DB_fetch_array($result);
1858 if(!$session || $gameid==$r[0])
1860 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
1861 $names = DB_get_all_names_by_gameid($gameid);
1862 $type = DB_get_gametype_by_gameid($gameid);
1866 $solo = DB_get_solo_by_gameid($gameid);
1868 if($solo!='silent') /* repeat game with same first player */
1869 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
1870 else /* rotate normally */
1871 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
1873 else /* rotate normally */
1874 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);