CLEANUP: changed the user page a bit and added a hint for newbies
[e-DoKo.git] / include / game.php
1 <?php
2 /* make sure that we are not called from outside the scripts,
3  * use a variable defined in config.php to check this
4  */
5 if(!isset($HOST))
6   exit;
7
8 /* calling game.php only makes sense when we give it a hash for a game */
9 if(!myisset('me'))
10   {
11     echo "Hmm, you really shouldn't mess with the urls.<br />\n";
12     return;
13   }
14 $me = $_REQUEST['me'];
15
16 /* Ok, got a hash, but is it valid? */
17 $myid = DB_get_userid('hash',$me);
18 if(!$myid)
19   {
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>.";
22     return;
23   }
24
25 global $GAME,$RULES,$CARDS;
26
27 /* the user has done something, update the timestamp */
28 if(isset($_SESSION['id']))
29   DB_update_user_timestamp($_SESSION['id']);
30
31 /* get some information from the DB */
32 $gameid   = DB_get_gameid_by_hash($me);
33 $myname   = DB_get_name('hash',$me);
34 $mystatus = DB_get_status_by_hash($me);
35 $mypos    = DB_get_pos_by_hash($me);
36 $myhand   = DB_get_handid('hash',$me);
37 $myparty  = DB_get_party_by_hash($me);
38 $session  = DB_get_session_by_gameid($gameid);
39
40 /* get prefs and save them in a variable*/
41 $PREF = DB_get_PREF(isset($_SESSION['id'])?$_SESSION['id']:$myid);
42
43 /* get rule set for this game */
44 $RULES = DB_get_RULES($gameid);
45
46 /* get some infos about the game */
47 $gametype   = DB_get_gametype_by_gameid($gameid);
48 $gamestatus = DB_get_game_status_by_gameid($gameid);
49 $GT         = $gametype;
50 if($gametype=='solo')
51   {
52     $gametype = DB_get_solo_by_gameid($gameid);
53     $GT  = $gametype.' '.$GT;
54   }
55
56 /* do we need to worry about Schweinchen?
57  * check gametype and rules
58  * if yes, figure out if someone actually has Schweinchen
59  * save information in $GAME
60  */
61 $ok=0;
62 if( $gamestatus == 'pre' )
63   {
64     /* always need to use Schweinchen to figure out for example who has poverty */
65     $ok=1;
66   }
67 else
68   {
69     /* in a game Schweinchen is not valid in all types of games */
70     if( $gametype == 'normal' || $gametype == 'silent' || $gametype=='trump' || $gametype=='wedding')
71       if( in_array($RULES['schweinchen'],array('both','second','secondaftercall')) )
72         $ok=1;
73   }
74
75 /* these are the defaults */
76 $GAME['schweinchen-who']    = NULL;
77 $GAME['schweinchen-first']  = NULL;
78 $GAME['schweinchen-second'] = NULL;
79
80 if($ok)
81 {
82   /* need to check for Schweinchen */
83   for($i=1;$i<5;$i++)
84     {
85       $hash  = DB_get_hash_from_game_and_pos($gameid,$i);
86       $cards = DB_get_all_hand($hash);
87       if( in_array('19',$cards) && in_array('20',$cards) )
88         $GAME['schweinchen-who']=$hash;
89     };
90   $GAME['schweinchen-first']  = 0; /* to keep track if they have been played already */
91   $GAME['schweinchen-second'] = 0;
92 }
93 /* end check for Schweinchen */
94
95 /* set the $CARDS variable, needed for sorting the cards
96  * we set it to normal so that the pre-game phase is handled ok
97  * and later set it to the correct game type that is played
98  */
99 set_gametype('normal');
100
101 /* put everyting in a form */
102 echo "<form action=\"index.php?action=game&amp;me=$me\" method=\"post\">\n";
103
104 /* handle user notes (only possible while game is running)*/
105 if( $mystatus!='gameover'  )
106   if(myisset('note'))
107     {
108       $note = $_REQUEST['note'];
109
110       if($note != '')
111         DB_insert_note($note,$gameid,$myid);
112     };
113
114 /* make sure that we don't show the notes to the wrong person
115  * (e.g. other people looking at an old game)
116  */
117 if( $mystatus != 'gameover' ||
118     (  $mystatus == 'gameover' &&
119        isset($_SESSION['id'])  &&
120        $myid == $_SESSION['id']))
121   output_user_notes($myid,$gameid,$mystatus);
122
123 /* handle calls, output a comment to show when the call was made */
124 /* initialize comments */
125 $comment  =  '';
126
127 /* check for calls, set comment */
128 if(myisset('call')  && $_REQUEST['call']  == '120' && can_call(120,$me))
129   {
130     $result = DB_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
131     if($myparty=='re')
132       $comment .= "Re";
133     else if($myparty=='contra')
134       $comment .= "Contra";
135   }
136 if(myisset('call')  && $_REQUEST['call']  == '90' && can_call(90,$me))
137   {
138     $result = DB_query("UPDATE Hand SET point_call='90'  WHERE hash='$me' ");
139     $comment .= "No 90";
140   }
141 if(myisset('call')  && $_REQUEST['call']  == '60' && can_call(60,$me))
142   {
143     $result = DB_query("UPDATE Hand SET point_call='60'  WHERE hash='$me' ");
144     $comment .= "No 60";
145   }
146 if(myisset('call')  && $_REQUEST['call']  == '30' && can_call(30,$me))
147   {
148     $result = DB_query("UPDATE Hand SET point_call='30'  WHERE hash='$me' ");
149     $comment .= "No 30";
150   }
151 if(myisset('call')  && $_REQUEST['call']  == '0' && can_call(0,$me))
152   {
153     $result = DB_query("UPDATE Hand SET point_call='0'   WHERE hash='$me' ");
154     $comment .= "Zero";
155   }
156
157 /* get information needed to submit comment */
158 $playid = DB_get_current_playid($gameid);
159
160 /* set comment */
161 if($comment != '')
162   DB_insert_comment($comment,$playid,$myid);
163 /* clear up */
164 unset($comment);
165 /* end check for calls */
166
167
168 /* output extra division in case this game is part of a session */
169 if($session)
170   {
171     echo "<div class=\"session\">\n";
172     echo "  <div class=\"sessionrules\">Rules: ";
173     switch($RULES['dullen'])
174       {
175       case 'none':
176         echo "  <img class=\"rulesicon\" alt=\"not ten of hearts\" src=\"pics/button/no-ten-of-hearts.png\"/>\n"; break;
177       case 'firstwins':
178         echo "  <img class=\"rulesicon\" alt=\"ten of hearts\" src=\"pics/button/ten-of-hearts.png\"/>\n"; break;
179       case 'secondwins':
180         echo "  <img class=\"rulesicon\" alt=\"second ten of hearts\" src=\"pics/button/second-ten-of-hearts.png\"/>\n"; break;
181       }
182     switch($RULES['schweinchen'])
183       {
184       case 'none':
185         echo "  <img class=\"rulesicon\" alt=\"no schweinchen\" ".
186           "src=\"pics/button/no-schweinchen.png\"/>\n"; break;
187       case 'both':
188         echo "  <img class=\"rulesicon\" alt=\"two schweinchen \" ".
189           "src=\"pics/button/two-schweinchen.png\"/>\n"; break;
190       case 'second':
191         echo "  <img class=\"rulesicon\" alt=\"second schweinchen\" ".
192           "src=\"pics/button/second-schweinchen.png\"/>\n"; break;
193       case 'secondaftercall':
194         echo "  <img class=\"rulesicon\" alt=\"second schweinchen after call\" ".
195           "src=\"pics/button/second-schweinchen-after-call.png\"/>\n"; break;
196       }
197     switch($RULES['call'])
198       {
199       case '1st-own-card':
200         echo "  <img class=\"rulesicon\" alt=\"1st-own-card\" src=\"pics/button/1st-own-card.png\"/>\n"; break;
201       case '5th-card':
202         echo "  <img class=\"rulesicon\" alt=\"5th-card\" src=\"pics/button/5th-card.png\"/>\n"; break;
203       case '9-cards':
204         echo "  <img class=\"rulesicon\" alt=\"9-cards\" src=\"pics/button/9-cards.png\"/>\n"; break;
205       }
206     echo "  <div>\n";
207     echo "       10ofhearts : {$RULES['dullen']}      <br />\n";
208     echo "       schweinchen: {$RULES['schweinchen']} <br />\n";
209     echo "       call:        {$RULES['call']}        <br />\n";
210     echo "  </div>\n  </div>\n";
211
212     /* show score */
213     $score   = generate_score_table($session);
214     $tmpscore= $score;
215     $finalscore = array_pop($tmpscore);
216     $finalscore = $finalscore['players'];
217
218     echo "  <div class=\"sessionscore\">Score: \n";
219     foreach($finalscore as $user=>$value)
220       {
221         $name = DB_get_name('userid',$user);
222         echo " ".substr($name,0,2).": $value ";
223       }
224     echo format_score_table_html($score,$myid);
225     echo "  </div>\n";
226
227     /* figure out which game in a session we are in and link to the previous and next game if possible */
228     $hashes = DB_get_hashes_by_session($session,$myid);
229     $next     = NULL;
230     $i = 1;
231     foreach($hashes as $hash)
232       {
233         if($hash == $me)
234           $j=$i;
235         $i++;
236         $lasthash=$hash;
237       }
238     $i--;
239
240     if($j>1)
241       $previous = $hashes[$j-2];
242     else
243       $previous = NULL;
244     if($j<$i)
245       $next = $hashes[$j];
246     else
247       $next = NULL;
248
249     if(isset($_SESSION['id']) && $_SESSION['id']==$myid)
250       {
251         if($previous)
252           echo "<a href=\"{$INDEX}?action=game&amp;me=$previous\">previous game</a>&nbsp;&nbsp;&nbsp; \n";
253         echo "This is game number $j of <a href=\"{$INDEX}?action=game&amp;me=$lasthash\">$i</a> in session $session.\n";
254         if($next)
255           echo "&nbsp;&nbsp;&nbsp;<a href=\"{$INDEX}?action=game&amp;me=$next\">next game</a> \n";
256       }
257     else
258       echo "This is game number $j of $i in session $session.";
259     echo "\n</div>\n";
260   }
261
262 /* display the table and the names */
263 display_table();
264
265 /* mystatus gets the player through the different stages of a game.
266  * start:    does the player want to play?
267  * init:     check for sickness
268  * check:    check for return values from init
269  * poverty:  handle poverty, wait here until all player have reached this state
270  *           display sickness and move on to game
271  * play:     game in progress
272  * gameover: are we revisiting a game
273  */
274 switch($mystatus)
275   {
276   case 'start':
277     /* don't ask if user has autosetup set to yest */
278     $skip = 0;
279     if($PREF['autosetup']=='yes') $skip = 1;
280
281     if( !myisset('in') && !$skip)
282       {
283         /* asks the player, if he wants to join the game */
284         output_check_want_to_play($me);
285         break;
286       }
287     else
288       {
289         /* check the result, if player wants to join, got next stage, else cancel game */
290         if(!$skip && $_REQUEST['in'] == 'no' )
291           {
292             /* cancel the game */
293             $message = "Hello, \n\n".
294               "the game has been canceled due to the request of one of the players.\n";
295
296             $userids = DB_get_all_userid_by_gameid($gameid);
297             foreach($userids as $user)
298               {
299                 $To = DB_get_email('userid',$user);
300                 mymail($To,"$EmailName game ".DB_format_gameid($gameid)." canceled",$message);
301               }
302
303             /* delete everything from the dB */
304             DB_cancel_game($me);
305             break;
306           }
307         else
308           {
309             /* user wants to join the game */
310
311             /* move on to the next stage,
312              * no break statement to immediately go to the next stage
313              */
314
315             DB_set_hand_status_by_hash($me,'init');
316
317             /* check if everyone has reached this stage, send out email */
318             $userids = DB_get_all_userid_by_gameid($gameid);
319             $ok = 1;
320             foreach($userids as $user)
321               {
322                 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
323                 if($userstat!='init')
324                   {
325                     /* whos turn is it? */
326                     DB_set_player_by_gameid($gameid,$user);
327                     $ok = 0;
328                     break;
329                   }
330               };
331             if($ok)
332               {
333                 /* all done, send out email unless this player is the startplayer */
334                 $startplayer = DB_get_startplayer_by_gameid($gameid);
335                 if($mypos == $startplayer)
336                   {
337                     /* do nothing, go to next stage */
338                   }
339                 else
340                   {
341                     /* email startplayer */
342                     /*
343                      $email       = DB_get_email('position-gameid',$startplayer,$gameid);
344                      $hash        = DB_get_hash_from_game_and_pos($gameid,$startplayer);
345                      $who         = DB_get_userid('email',$email);
346                      DB_set_player_by_gameid($gameid,$who);
347
348                      $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
349                      "Use this link to go the game: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
350                      mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
351                     */
352                   }
353               }
354           }
355       }
356   case 'init':
357     /* here we ask the player if he is sick */
358     $mycards = DB_get_hand($me);
359     $mycards = mysort($mycards,$gametype);
360
361     if(!myisset('solo','wedding','poverty','nines') )
362       {
363         /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
364         echo "\n<ul class=\"tricks\">\n";
365         echo "  <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
366         echo "  <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
367           "    <div class=\"trick\" id=\"trick0\">\n";
368
369         for($pos=1;$pos<5;$pos++)
370           {
371             $usersick   = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
372             $userid     = DB_get_userid('gameid-position',$gameid,$pos);
373             $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
374
375             if($userstatus=='start' || $userstatus=='init')
376               echo " <div class=\"vorbehalt".($pos-1)."\"> still needs <br/ > to decide </div>\n"; /* show this to everyone */
377             else
378               if($usersick!=NULL && $pos<=$mypos ) /* only show this for people sitting before the player */
379                 echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
380               else if($usersick==NULL && $pos<=$mypos)
381                 echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
382           }
383         echo "    </div>\n  </li>\n</ul>\n";  /* end div trick, end li trick , end tricks*/
384         /* end displaying sickness */
385
386         output_check_for_sickness($me,$mycards);
387
388         echo "<div class=\"mycards\">Your cards are: <br />\n";
389         foreach($mycards as $card)
390           display_card($card,$PREF['cardset']);
391         echo "</div>\n";
392
393         break;
394       }
395     else
396       {
397         /* check if someone selected more than one sickness */
398         $Nsickness = 0;
399         if($_REQUEST['solo']!='No')       $Nsickness++;
400         if($_REQUEST['wedding'] == 'yes') $Nsickness++;
401         if($_REQUEST['poverty'] == 'yes') $Nsickness++;
402         if($_REQUEST['nines'] == 'yes')   $Nsickness++;
403
404         if($Nsickness>1)
405           {
406             echo "<p class=\"message\"> You selected more than one sickness, please go back ".
407               "and answer the <a href=\"$INDEX?action=game&amp;me=$me&amp;in=yes\">question</a> again.</p>";
408
409             echo "<div class=\"mycards\">Your cards are: <br />\n";
410             foreach($mycards as $card)
411               display_card($card,$PREF['cardset']);
412             echo "</div>\n";
413
414             break;
415           }
416         else
417           {
418             /* everything is ok, save what user said and proceed */
419             echo "<p class=\"message\">Processing what you selected in the last step...";
420
421             /* check if this sickness needs to be handled first */
422             $gametype    = DB_get_gametype_by_gameid($gameid);
423             $startplayer = DB_get_startplayer_by_gameid($gameid); /* need this to check which solo goes first */
424
425             if( $_REQUEST['solo']!='No' )
426               {
427                 /* user wants to play a solo */
428
429                 /* store the info in the user's hand info */
430                 DB_set_solo_by_hash($me,$_REQUEST['solo']);
431                 DB_set_sickness_by_hash($me,'solo');
432
433                 echo "<br />Seems like you want to play a {$_REQUEST['solo']} solo. Got it.<br />\n";
434
435                 if($gametype == 'solo' && $startplayer<$mypos)
436                   {}/* do nothing, since someone else already is playing solo */
437                 else
438                   {
439                     /* this solo comes first
440                      * store info in game table
441                      */
442                     DB_set_gametype_by_gameid($gameid,'solo');
443                     DB_set_startplayer_by_gameid($gameid,$mypos);
444                     DB_set_solo_by_gameid($gameid,$_REQUEST['solo']);
445                   };
446               }
447             else if($_REQUEST['wedding'] == 'yes')
448               {
449                 /* silent solo is set further down */
450                 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
451                 DB_set_sickness_by_hash($me,'wedding');
452               }
453             else if($_REQUEST['poverty'] == 'yes')
454               {
455                 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
456                 DB_set_sickness_by_hash($me,'poverty');
457               }
458             else if($_REQUEST['nines'] == 'yes')
459               {
460                 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
461                   " is playing solo, this game will be canceled.<br />\n";
462                 DB_set_sickness_by_hash($me,'nines');
463               }
464
465             echo "</p>\n";
466
467             /* move on to the next stage*/
468             DB_set_hand_status_by_hash($me,'check');
469           };
470       };
471
472   case 'check':
473     /* here we check what all players said and figure out what game we are playing
474      * this can therefore only be handled once all players finished the last stage
475      */
476
477     $mycards = DB_get_hand($me);
478     $mycards = mysort($mycards,$gametype);
479
480     /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
481     echo "\n<ul class=\"tricks\">\n";
482     echo "  <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
483     echo "  <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
484       "    <div class=\"trick\" id=\"trick0\">\n";
485
486     for($pos=1;$pos<5;$pos++)
487       {
488         $usersick   = DB_get_sickness_by_pos_and_gameid($pos,$gameid);
489         $userid     = DB_get_userid('gameid-position',$gameid,$pos);
490         $userstatus = DB_get_hand_status_by_userid_and_gameid($userid,$gameid);
491
492         if($userstatus=='start' || $userstatus=='init')
493           echo " <div class=\"vorbehalt".($pos-1)."\"> still needs <br />to decide </div>\n"; /* show this to everyone */
494         else
495           if($usersick!=NULL) /* in the init-phase we only showed players with $pos<$mypos, now we can show all */
496             echo " <div class=\"vorbehalt".($pos-1)."\"> sick </div>\n";
497           else
498             echo " <div class=\"vorbehalt".($pos-1)."\"> healthy </div>\n";
499       }
500     echo "    </div>\n  </li>\n</ul>\n";  /* end div trick, end li trick , end tricks*/
501     /* end displaying sickness */
502
503     echo "<div class=\"message\">\n";
504     echo "<p> Checking if someone else selected solo, nines, wedding or poverty.</p>";
505
506     /* check if everyone has reached this stage */
507     $userids = DB_get_all_userid_by_gameid($gameid);
508     $ok = 1;
509     foreach($userids as $user)
510       {
511         $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
512         if($userstat!='check')
513           {
514             $ok = 0;
515             DB_set_player_by_gameid($gameid,$user);
516             break;
517           }
518       };
519
520     if(!$ok)
521       {
522         echo "<p>This step can only be handled after everyone finished the last step. ".
523           "Seems like this is not the case, so you need to wait a bit... ".
524           "you will get an email once that is the case, please use the link in ".
525           "that email to continue the game.</p></div>";
526
527         /* display cards, if player was just at the init-phase he will still see the cards from there
528          * we can put this one here, since the last player to finish the init state won't get here and
529          * will still see his card anyway from the init-phase
530          */
531         if($mystatus=='check')
532           {
533             /* show cards */
534             echo "<div class=\"mycards\">Your cards are: <br />\n";
535             foreach($mycards as $card)
536               display_card($card,$PREF['cardset']);
537             echo "</div>\n";
538           }
539         break;
540       }
541     else
542       {
543         /* Ok, everyone finished the init-phase, time to figure out what game we
544          * are playing, in case there are any solos this already
545          * will have the correct information in it */
546
547         echo "<p> Ok, everyone is done... figuring out what kind of game we are playing.</p>";
548
549         $gametype    = DB_get_gametype_by_gameid($gameid);
550         $startplayer = DB_get_startplayer_by_gameid($gameid);
551
552         /* check for sickness */
553         $nines   = 0;
554         $poverty = 0;
555         $wedding = 0;
556         $solo    = 0;
557         foreach($userids as $user)
558           {
559             $name     = DB_get_name('userid',$user);
560             $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
561             if($usersick == 'nines')
562               {
563                 $nines = $user;
564                 break; /* no need to check for other poverties, since only solo can win and that is already set */
565               }
566             else if($usersick == 'poverty')
567               $poverty++;
568             else if($usersick == 'wedding')
569               $wedding=$user;
570             else if($usersick == 'solo')
571               $solo++;
572           }
573
574         /* now check which sickness comes first and set the gametype to it */
575         if($gametype == 'solo')
576           {
577             /* do nothing */
578           }
579         else if($nines)
580           {
581             /* cancel game */
582             /* TODO: should we keep statistics of this? */
583             $message = "Hello, \n\n".
584               " the game has been canceled because ".DB_get_name('userid',$nines).
585               " has five or more nines and nobody is playing solo.\n\n".
586               " To redeal either start a new game or, in case the game was part of a tournament, \n".
587               " go to the last game and use the link at the bottom of the page to redeal.";
588
589             $userids = DB_get_all_userid_by_gameid($gameid);
590             foreach($userids as $user)
591               {
592                 $To = DB_get_email('userid',$user);
593                 mymail($To,"$EmailName game ".DB_format_gameid($gameid)." canceled",$message);
594               }
595
596             /* delete everything from the dB */
597             DB_cancel_game($me);
598
599             echo "The game has been canceled because ".DB_get_name('userid',$nines).
600               " has five or more nines and nobody is playing solo.\n";
601             return;
602           }
603         else if($poverty==1) /* one person has poverty */
604           {
605             DB_set_gametype_by_gameid($gameid,'poverty');
606             $gametype = 'poverty';
607             $who      = DB_get_sickness_by_gameid($gameid);
608             if(!$who)
609               {
610                 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
611                 if($firstsick == 'poverty')
612                   DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
613                 else
614                   DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
615               }
616           }
617         else if($poverty==2) /* two people have poverty */
618           {
619             DB_set_gametype_by_gameid($gameid,'dpoverty');
620             $gametype = 'dpoverty';
621             $who      = DB_get_sickness_by_gameid($gameid);
622             if(!$who)
623               {
624                 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
625                 if($firstsick == 'poverty')
626                   {
627                     $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
628                     if($secondsick == 'poverty')
629                       DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
630                     else
631                       DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
632                   }
633                 else
634                   DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
635               }
636           }
637         else if($wedding> 0)
638           {
639             DB_set_gametype_by_gameid($gameid,'wedding');
640             DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
641             $gametype = 'wedding';
642           };
643         /* now the gametype is set correctly in the database */
644         echo "<p> Got it :)</p>";
645
646         /* loop over all players, set re/contra if possible and start the game if possible */
647         $userids = DB_get_all_userid_by_gameid($gameid);
648         foreach($userids as $userid)
649           {
650             $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
651
652             switch($gametype)
653               {
654               case 'solo':
655                 /* are we the solo player? set us to re, else set us to contra */
656                 $pos = DB_get_pos_by_hash($userhash);
657                 if($pos == $startplayer)
658                   DB_set_party_by_hash($userhash,'re');
659                 else
660                   DB_set_party_by_hash($userhash,'contra');
661                 DB_set_hand_status_by_hash($userhash,'play');
662                 break;
663
664               case 'wedding':
665                 /* set person with the wedding to re, do the rest during the game */
666                 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
667                 if($usersick == 'wedding')
668                   DB_set_party_by_hash($userhash,'re');
669                 else
670                   DB_set_party_by_hash($userhash,'contra');
671
672                 DB_set_hand_status_by_hash($userhash,'play');
673                 break;
674
675               case 'normal':
676                 $hand = DB_get_all_hand($userhash);
677
678                 if(in_array('3',$hand)||in_array('4',$hand))
679                   DB_set_party_by_hash($userhash,'re');
680                 else
681                   DB_set_party_by_hash($userhash,'contra');
682                 DB_set_hand_status_by_hash($userhash,'play');
683                 break;
684               case 'poverty':
685               case 'dpoverty':
686                 /* set person with poverty to play status */
687                 $usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
688                 if($usersick == 'poverty')
689                   DB_set_hand_status_by_hash($userhash,'play');
690
691                 /* set status of first player to be asked to poverty */
692                 $who = DB_get_sickness_by_gameid($gameid);
693                 if($who > 6) $who= $who/10; /* in case we have dpoverty */
694                 $whoid = DB_get_userid('gameid-position',$gameid,$who);
695                 if($whoid==$userid)
696                   DB_set_hand_status_by_hash($userhash,'poverty');
697               }
698           }
699         /* check for silent solo, set game type to solo in this case */
700         $gametype = DB_get_gametype_by_gameid($gameid);
701         $userids  = DB_get_all_userid_by_gameid($gameid);
702         foreach($userids as $userid)
703           {
704             $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
705
706             if($gametype=='normal')
707               {
708                 $userhand = DB_get_all_hand($userhash);
709                 if(check_wedding($userhand))
710                   {
711                     /* normal game type and player has both queens -> silent solo */
712                     /* keep startplayer, just set gametype to silent solo */
713                     DB_set_gametype_by_gameid($gameid,'solo');
714                     DB_set_solo_by_gameid($gameid,'silent');
715                   }
716               }
717           }
718
719         /* send out email to first player or poverty person*/
720         if($gametype!='poverty' && $gametype!='dpoverty')
721           {
722             $startplayer = DB_get_startplayer_by_gameid($gameid);
723             $email       = DB_get_email('position-gameid',$startplayer,$gameid);
724             $hash        = DB_get_hash_from_game_and_pos($gameid,$startplayer);
725             $who         = DB_get_userid('email',$email);
726             DB_set_player_by_gameid($gameid,$who);
727
728             if($hash!=$me)
729               {
730                 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
731                   {
732                     /* email startplayer */
733                     $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
734                       "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
735                     mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
736                   }
737               }
738             else
739               echo " Please, <a href=\"$INDEX?action=game&amp;me=$me\">start</a> the game.<br />\n";
740           }
741         else
742           {
743             /* set status of first player to be asked to poverty */
744             $who   = DB_get_sickness_by_gameid($gameid);
745             if($who > 6) $who= $who/10; /* in case we have dpoverty */
746
747             $whoid = DB_get_userid('gameid-position',$gameid,$who);
748             if($whoid==$myid)
749               echo " Please, <a href=\"$INDEX?action=game&amp;me=$me\">start</a> the game.<br />\n";
750             else
751               {
752                 $email   = DB_get_email('position-gameid',$who,$gameid);
753                 $whohash = DB_get_hash_from_game_and_pos($gameid,$who);
754                 DB_set_player_by_gameid($gameid,$whoid);
755
756                 if(DB_get_email_pref_by_hash($hash)!='emailaddict')
757                   {
758                     /* email player for poverty */
759                     $message = "Poverty: It's your turn now in game ".DB_format_gameid($gameid).".\n".
760                       "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$whohash."\n\n" ;
761                     mymail($email,$EmailName."Poverty (game ".DB_format_gameid($gameid).") ",$message);
762                   }
763               }
764           }
765         echo "</div>\n";
766         break;
767       }
768   case 'poverty':
769     /* user only gets here in a poverty game, several things have to be handled here:
770      * A) ask, if user wants to take trump
771      *      yes-> take trump,
772      *            poverty: set re/contra
773      *            dpoverty: first time: set re, send email to second player
774      *                      second time: set contra
775      *            poverty: set status of other players to 'play'
776      *            set status to play in case 0 trump
777      *      no -> set status to play,
778      *            ask next player or cancle the game if no more players
779      * B) user took trump and has too many cards (e.g. count(cards)>12 and re/contra set)
780      *         ask to give cards back, set status to play, once player has 12 cards
781      *
782      * it is easier to check B) first
783      */
784
785     /* output pre game in case user reloads */
786
787
788     set_gametype($gametype); /* this sets the $CARDS variable */
789     $myparty = DB_get_party_by_hash($me);
790
791     /* the following is part B) of whats needs to be done)
792     /*    check if user wants to give cards back */
793     if(myisset('exchange'))
794       {
795         $exchange    = $_REQUEST['exchange'];
796         $partnerhash = DB_get_partner_hash_by_hash($me);
797         $partnerid   = DB_get_userid('hash',$partnerhash);
798         $partnerhand = DB_get_handid('gameid-userid',$gameid,$partnerid);
799
800         /* if exchange is set to a value>0, exchange that card back to the partner */
801         if($exchange >0)
802           {
803             $result = DB_query("UPDATE Hand_Card SET hand_id='$partnerhand'".
804                                " WHERE hand_id='$myhand' AND card_id=".DB_quote_smart($exchange));
805             DB_add_exchanged_card(DB_quote_smart($exchange),$myhand,$partnerhand);
806           };
807       }
808
809     /* update hand */
810     $mycards = DB_get_hand($me);
811     $mycards = mysort($mycards,$gametype);
812
813     /* check if user need to give more cards back */
814     if( ($myparty=='re' || $myparty=='contra') && count($mycards)>12)
815       {
816         echo "<div class=\"poverty\"> you need to get rid of a few cards</div>\n";
817
818         $type='exchange';
819         echo "<div class=\"mycards\">Your cards are: <br />\n";
820         foreach($mycards as $card)
821           display_link_card($card,$PREF['cardset'],$type);
822         echo "  <input type=\"submit\" class=\"submitbutton\" value=\"select card to give back\" />\n";
823         echo "</div>\n";
824       }
825     else if( ($myparty=='re' || $myparty=='contra') && count($mycards)==12)
826       {
827         /* user is done, ready to play */
828         DB_set_hand_status_by_hash($me,'play');
829
830         /* email start player */
831         $startplayer = DB_get_startplayer_by_gameid($gameid);
832         $email       = DB_get_email('position-gameid',$startplayer,$gameid);
833         $hash        = DB_get_hash_from_game_and_pos($gameid,$startplayer);
834         $who         = DB_get_userid('email',$email);
835         DB_set_player_by_gameid($gameid,$who);
836
837         if($hash!=$me)
838           {
839             if(DB_get_email_pref_by_hash($hash)!='emailaddict')
840               {
841                 /* email startplayer */
842                 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
843                   "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
844                 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
845               }
846           }
847         else
848           echo " Please, <a href=\"$INDEX?action=game&amp;me=$me\">start</a> the game.<br />\n";
849       }
850
851     /* the following is part A) of what needs to be done */
852     if(!myisset('trump'))
853       {
854         if(!$myparty)
855           {
856             echo "<div class=\"poverty\">\n";
857             $userids = DB_get_all_userid_by_gameid($gameid);
858             foreach($userids as $user)
859               {
860                 $name      = DB_get_name('userid',$user);
861                 $usersick  = DB_get_sickness_by_userid_and_gameid($user,$gameid);
862                 $userhash  = DB_get_hash_from_gameid_and_userid($gameid,$user);
863                 $userparty = DB_get_party_by_hash($userhash);
864
865                 if($usersick=='poverty' && !$userparty)
866                   {
867                     $hash    = DB_get_hash_from_gameid_and_userid($gameid,$user);
868                     $cards   = DB_get_hand($hash);
869                     /* count trump */
870                     $nrtrump = 0;
871                     foreach($cards as $card)
872                       if($card<27) $nrtrump++;
873                     echo "Player $name has $nrtrump trump. Do you want to take them?".
874                       "<a href=\"index.php?action=game&amp;me=$me&amp;trump=$user\">Yes</a> <br />\n";
875                   }
876               }
877             echo "<a href=\"index.php?action=game&amp;me=$me&amp;trump=no\">No way</a> <br />\n";
878             echo "</div><div>\n";
879
880             echo "<div class=\"mycards\">Your cards are: <br />\n";
881             foreach($mycards as $card)
882               display_card($card,$PREF['cardset']);
883             echo "</div></div>\n";
884           }
885         break;
886       }
887     else
888       {
889         $trump = $_REQUEST['trump'];
890
891         if($trump=='no')
892           {
893             /* user doesn't want to take trump */
894             DB_set_hand_status_by_hash($me,'play');
895
896             /* set next player who needs to be asked and email him*/
897             $firstsick  = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
898             $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
899
900             /* don't ask people who have poverty */
901             $next=1;
902             if($firstsick=='poverty')
903               {
904                 if($secondsick=='poverty')
905                   $next=3;
906                 else
907                   $next=2;
908               }
909             if($gametype=='dpoverty')
910               {
911                 $next=999; /* need to cancel for sure, since both would need to take the trump */
912               }
913
914             /* no more people to ask, need to cancel the game */
915             if($mypos+$next>4)
916               {
917                 $message = "Hello, \n\n".
918                   "Game ".DB_format_gameid($gameid)." has been canceled since nobody wanted to take the trump.\n";
919
920                 $userids = DB_get_all_userid_by_gameid($gameid);
921                 foreach($userids as $user)
922                   {
923                     $To = DB_get_email('userid',$user);
924                     mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled (poverty not resolved)",$message);
925                   }
926
927                 /* delete everything from the dB */
928                 DB_cancel_game($me);
929
930                 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been canceled.<br /><br /></p>";
931                 return;
932               }
933             else
934               {
935                 /* email next player, set his status to poverty */
936                 $To       = DB_get_email('position-gameid',$mypos+$next,$gameid);
937                 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
938                 $userid   = DB_get_userid('email',$To);
939
940                 DB_set_player_by_gameid($gameid,$userid);
941                 DB_set_hand_status_by_hash($userhash,'poverty');
942
943                 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
944                   " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
945                 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
946               }
947           }
948         else
949           {
950             /* player wants to take trump, change cards */
951
952             /* user wants to take trump */
953             $trump = $_REQUEST['trump'];
954             $userhand = DB_get_handid('gameid-userid',$gameid,$trump);
955             $userhash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
956
957             /* remember which cards were handed over*/
958             $partnerhand = DB_get_all_hand($userhash);
959             foreach ($partnerhand as $card)
960               if($card<27)
961                 DB_add_exchanged_card($card,$userhand,$myhand);
962
963             /* copy trump from player A to B */
964             $result = DB_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
965
966             /* reload cards */
967             $mycards = DB_get_hand($me);
968
969             /* set re/contra */
970             if($gametype=='poverty')
971               {
972                 $userids = DB_get_all_userid_by_gameid($gameid);
973                 foreach($userids as $user)
974                   {
975                     $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
976                     if($hash==$userhash||$hash==$me)
977                       {
978                         DB_set_party_by_hash($hash,'re');
979                       }
980                     else
981                       {
982                         DB_set_party_by_hash($hash,'contra');
983                         DB_set_hand_status_by_hash($hash,'play'); /* the contra party is ready to play */
984                       }
985                   }
986                 /* check if we are done (in case of no trump handed over), if so, go to 'play' phase right away*/
987                 if(count($mycards)==12)
988                   {
989                     DB_set_hand_status_by_hash($me,'play');
990                   }
991               }
992             else /*dpoverty*/
993               {
994                 /* has the re party already been set?*/
995                 $re_set=0;
996                 $userids = DB_get_all_userid_by_gameid($gameid);
997                 foreach($userids as $user)
998                   {
999                     $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1000                     $party = DB_get_party_by_hash($hash);
1001                     if($party=='re')
1002                       $re_set=1;
1003                   }
1004                 if($re_set)
1005                   {
1006                     DB_set_party_by_hash($me,'contra');
1007                     DB_set_party_by_hash($userhash,'contra');
1008                   }
1009                 else
1010                   {
1011                     DB_set_party_by_hash($me,'re');
1012                     DB_set_party_by_hash($userhash,'re');
1013
1014                     /* send out email to second non-poverty player */
1015                     $firstsick  = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
1016                     $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
1017
1018                     $next=1;
1019                     if($firstsick=='poverty')
1020                       if($secondsick=='poverty')
1021                         $next=3;
1022                       else
1023                         $next=2;
1024
1025                     if($mypos+$next>4)
1026                       echo "<div class=\"message\">Error in poverty, please contact the Admin</div>\n";
1027
1028                     $To       = DB_get_email('position-gameid',$mypos+$next,$gameid);
1029                     $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
1030                     $userid   = DB_get_userid('email',$To);
1031
1032                     DB_set_player_by_gameid($gameid,$userid);
1033                     DB_set_hand_status_by_hash($userhash,'poverty');
1034
1035                     $message = "Two people have poverty, it's your turn to decide, if you want to take the trump. Please visit:".
1036                       " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
1037                     mymail($To,$EmailName." double poverty (game ".DB_format_gameid($gameid).")",$message);
1038
1039
1040                   }
1041               }
1042             echo "<div class=\"message\"> Please, <a href=\"$INDEX?action=game&amp;me=$me\">continue</a> here.</div>\n";
1043           }
1044       }
1045     echo "</div>";
1046     break;
1047
1048   case 'play':
1049   case 'gameover':
1050     /* both entries here,  so that the tricks are visible for both.
1051      * in case of 'play' there is a break later that skips the last part
1052      */
1053
1054     /* check if all players are ready to play,
1055      * if so, send out email to the startplayer
1056      * only need to do this if the game hasn't started yet
1057      */
1058     $gamestatus = DB_get_game_status_by_gameid($gameid);
1059     if($gamestatus == 'pre')
1060       {
1061         $ok = 1;
1062         $userids = DB_get_all_userid_by_gameid($gameid);
1063         foreach($userids as $user)
1064           {
1065             $userstatus = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
1066             if($userstatus !='play' && $userstatus!='gameover')
1067               {
1068                 $ok = 0;
1069                 DB_set_player_by_gameid($gameid,$user);
1070                 break;
1071               }
1072           }
1073         if($ok)
1074           {
1075             /* only set this after all poverty, etc. are handled*/
1076             DB_set_game_status_by_gameid($gameid,'play');
1077
1078             /* email startplayer */
1079             $startplayer = DB_get_startplayer_by_gameid($gameid);
1080             $email       = DB_get_email('position-gameid',$startplayer,$gameid);
1081             $hash        = DB_get_hash_from_game_and_pos($gameid,$startplayer);
1082             $who         = DB_get_userid('email',$email);
1083             DB_set_player_by_gameid($gameid,$who);
1084
1085             if($hash!=$me && DB_get_email_pref_by_hash($hash)!='emailaddict')
1086               {
1087                 /* email startplayer) */
1088                 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
1089                   "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
1090                 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
1091               }
1092           }
1093       }
1094     /* figure out what kind of game we are playing,
1095      * set the global variables $CARDS['trump'],$CARDS['diamonds'],$CARDS['hearts'],
1096      * $CARDS['clubs'],$CARDS['spades'],$CARDS['foxes']
1097      * accordingly
1098      */
1099
1100     $gametype = DB_get_gametype_by_gameid($gameid);
1101     $GT       = $gametype;
1102     if($gametype=='solo')
1103       {
1104         $gametype = DB_get_solo_by_gameid($gameid);
1105         if($gametype=='silent')
1106           $GT = 'normal';
1107         else
1108           $GT = $gametype.' '.$GT;
1109       }
1110     else
1111       $gametype = 'normal';
1112
1113     set_gametype($gametype); /* this sets the $CARDS variable */
1114
1115     /* get some infos about the game, need to reset this, since it might have changed */
1116     $gamestatus = DB_get_game_status_by_gameid($gameid);
1117
1118     /* has the game started? No, then just wait here...*/
1119     if($gamestatus == 'pre')
1120       {
1121         echo "<p class=\"message\"> You finished the setup, but not everyone else finished it... ".
1122           "You need to wait for the others. Just wait for an email. </p>";
1123
1124         $mycards = DB_get_hand($me);
1125         $mycards = mysort($mycards,$gametype);
1126
1127         echo "<div class=\"mycards\">Your cards are: <br />\n";
1128         foreach($mycards as $card)
1129           display_card($card,$PREF['cardset']);
1130         echo "</div>\n";
1131
1132         break; /* not sure this works... the idea is that you can
1133                 * only  play a card after everyone is ready to play */
1134       }
1135
1136
1137     /* get time from the last action of the game */
1138     $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1139     $gameend = time() - strtotime($r[0]);
1140
1141     /* handle comments in case player didn't play a card, allow comments a week after the end of the game */
1142     if( (!myisset('card') && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
1143       if(myisset('comment'))
1144         {
1145           $comment = $_REQUEST['comment'];
1146           $playid = DB_get_current_playid($gameid);
1147
1148           if($comment != '')
1149             DB_insert_comment($comment,$playid,$myid);
1150         };
1151
1152     /* get everything relevant to display the tricks */
1153     $result = DB_query("SELECT Hand_Card.card_id as card,".
1154                        "       Hand.position as position,".
1155                        "       Play.sequence as sequence, ".
1156                        "       Trick.id, ".
1157                        "       GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
1158                        "                    SEPARATOR '\n' ), ".
1159                        "       Play.create_date, ".
1160                        "       Hand.user_id ".
1161                        "FROM Trick ".
1162                        "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1163                        "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1164                        "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1165                        "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1166                        "LEFT JOIN User On User.id=Comment.user_id ".
1167                        "WHERE Trick.game_id='".$gameid."' ".
1168                        "GROUP BY Trick.id, sequence ".
1169                        "ORDER BY Trick.id, sequence  ASC");
1170     $trickNR   = 0;
1171     $lasttrick = DB_get_max_trickid($gameid);
1172
1173     $play = array(); /* needed to calculate winner later  */
1174     $seq  = 1;
1175     $pos  = DB_get_startplayer_by_gameid($gameid)-1;
1176     $firstcard = ''; /* first card in a trick */
1177
1178     echo "\n<ul class=\"tricks\">\n";
1179     echo "  <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
1180
1181     /* output vorbehalte */
1182     $mygametype =  DB_get_gametype_by_gameid($gameid);
1183     if($mygametype != 'normal' && $mygametype != 'silent') /* only show when needed */
1184       {
1185         echo "  <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
1186              "    <div class=\"trick\" id=\"trick0\">\n";
1187         /* get information so show the cards that have been handed over in a poverty game */
1188         $partnerpos1 = 0;
1189         $povertypos1 = 0;
1190         $partnerpos2 = 0;
1191         $povertypos2 = 0;
1192         if($mygametype == 'poverty' || $mygametype=='dpoverty')
1193           {
1194             /* who has poverty */
1195             for($mypos=1;$mypos<5;$mypos++)
1196               {
1197                 $usersick = DB_get_sickness_by_pos_and_gameid($mypos,$gameid);
1198                 if($usersick == 'poverty')
1199                   if($povertypos1)
1200                     $povertypos2 = $mypos;
1201                   else
1202                     $povertypos1 = $mypos;
1203               }
1204             /* get hash and cards for all */
1205             $povertyhash1 = DB_get_hash_from_game_and_pos($gameid,$povertypos1);
1206             $partnerhash1 = DB_get_partner_hash_by_hash($povertyhash1);
1207
1208             $povertycards1 = DB_get_exchanged_cards($povertyhash1);
1209             $partnercards1 = DB_get_exchanged_cards($partnerhash1);
1210
1211             $partnerpos1 = DB_get_pos_by_hash($partnerhash1);
1212             if($povertypos2)
1213               {
1214                 $povertyhash2 = DB_get_hash_from_game_and_pos($gameid,$povertypos2);
1215                 $partnerhash2 = DB_get_partner_hash_by_hash($povertyhash2);
1216
1217                 $povertycards2 = DB_get_exchanged_cards($povertyhash2);
1218                 $partnercards2 = DB_get_exchanged_cards($partnerhash2);
1219
1220                 $partnerpos2 = DB_get_pos_by_hash($partnerhash2);
1221               }
1222           }
1223
1224         $show = 1;
1225         for($mypos=1;$mypos<5;$mypos++)
1226           {
1227             $usersick = DB_get_sickness_by_pos_and_gameid($mypos,$gameid);
1228             if($usersick!=NULL ||
1229                $mypos==$povertypos1 || $mypos==$partnerpos1 ||
1230                $mypos==$povertypos2 || $mypos==$partnerpos2 )
1231               {
1232                 echo "      <div class=\"vorbehalt".($mypos-1)."\"> Vorbehalt <br />";
1233                 if($show)
1234                   echo " $usersick <br />";
1235                 if($mypos==$partnerpos1)
1236                   {
1237                     foreach($partnercards1 as $card)
1238                       if($povertyhash1 == $me || $partnerhash1 == $me || $mystatus=='gameover')
1239                         display_card($card,$PREF['cardset']);
1240                       else
1241                         display_card(0,$PREF['cardset']);
1242                   }
1243                 else if($mypos==$povertypos1)
1244                   {
1245                     foreach($povertycards1 as $card)
1246                       if($povertyhash1 == $me || $partnerhash1 == $me || $mystatus=='gameover')
1247                         display_card($card,$PREF['cardset']);
1248                       else
1249                         display_card(0,$PREF['cardset']);
1250                   }
1251                 else if($mypos==$povertypos2)
1252                   {
1253                     foreach($povertycards2 as $card)
1254                       if($povertyhash2 == $me || $partnerhash2 == $me || $mystatus=='gameover')
1255                         display_card($card,$PREF['cardset']);
1256                       else
1257                         display_card(0,$PREF['cardset']);
1258                   }
1259                 else if($mypos==$partnerpos2)
1260                   {
1261                     foreach($partnercards2 as $card)
1262                       if($povertyhash2 == $me || $partnerhash2 == $me || $mystatus=='gameover')
1263                         display_card($card,$PREF['cardset']);
1264                       else
1265                         display_card(0,$PREF['cardset']);
1266                   }
1267
1268                 echo  " </div>\n";
1269
1270                 if($mygametype == $usersick)
1271                   $show = 0;
1272               }
1273           }
1274         echo "    </div>\n  </li>\n";  /* end div trick, end li trick */
1275       }
1276
1277     /* output tricks */
1278     while($r = DB_fetch_array($result))
1279       {
1280         $pos     = $r[1];
1281         $seq     = $r[2];
1282         $trick   = $r[3];
1283         $comment = $r[4];
1284         $user    = $r[6];
1285
1286         /* count number of tricks */
1287         if($seq==1)
1288           $trickNR++;
1289
1290         /* check if first schweinchen has been played */
1291         if( $GAME['schweinchen-who'] && ($r[0] == 19 || $r[0] == 20) )
1292           if(!$GAME['schweinchen-first'])
1293             $GAME['schweinchen-first'] = 1; /* playing the first fox */
1294           else
1295             $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1296
1297         /* save card to be able to find the winner of the trick later */
1298         $play[$seq] = array('card'=>$r[0],'pos'=>$pos);
1299
1300         if($seq==1)
1301           {
1302             /* first card in a trick, output some html */
1303             if($trick!=$lasttrick)
1304               {
1305                 /* start of an old trick? */
1306                 echo "  <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1307                   "    <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1308                   "      <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1309               }
1310             else if($trick==$lasttrick)
1311               {
1312                 /* start of a last trick? */
1313                 echo "  <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1314                   "    <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1315                   "      <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1316               };
1317
1318             /* remember first card, so that we are able to check, what cards can be played */
1319             $firstcard = $r[0];
1320           };
1321
1322         /* display card */
1323         echo "      <div class=\"card".($pos-1)."\">\n";
1324
1325         /* display comments */
1326         if($comment!='')
1327           echo "        <span class=\"comment\">".$comment."</span>\n";
1328
1329         echo '        ';
1330         display_card($r[0],$PREF['cardset']);
1331
1332         echo "      </div>\n"; /* end div card */
1333
1334         /* end of trick? */
1335         if($seq==4)
1336           {
1337             echo "    </div>\n  </li>\n";  /* end div trick, end li trick */
1338           }
1339       }
1340
1341     /* whos turn is it? */
1342     if($seq==4)
1343       {
1344         $winner    = get_winner($play,$gametype); /* returns the position */
1345         $next      = $winner;
1346         $firstcard = ''; /* new trick, no first card */
1347       }
1348     else
1349       {
1350         $next = $pos+1;
1351         if($next==5) $next = 1;
1352       }
1353
1354     /* my turn?, display cards as links, ask for comments*/
1355     if(DB_get_pos_by_hash($me) == $next)
1356       $myturn = 1;
1357     else
1358       $myturn = 0;
1359
1360     /* do we want to play a card? */
1361     if(myisset('card') && $myturn)
1362       {
1363         $card   = $_REQUEST['card'];
1364         $handid = DB_get_handid('hash',$me);
1365         $commentSchweinchen =''; /* used to add a comment when Schweinchen is being played */
1366
1367         /* check if we have card and that we haven't played it yet*/
1368         /* set played in hand_card to true where hand_id and card_id*/
1369         $r = DB_query_array("SELECT id FROM Hand_Card WHERE played='false' and ".
1370                               "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1371         $handcardid = $r[0];
1372
1373         if($handcardid) /* everything ok, play card  */
1374           {
1375             /* update Game timestamp */
1376             DB_update_game_timestamp($gameid);
1377
1378             /* mark card as played */
1379             DB_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1380                      DB_quote_smart($card));
1381
1382             /* get trick id or start new trick */
1383             $a = DB_get_current_trickid($gameid);
1384             $trickid  = $a[0];
1385             $sequence = $a[1];
1386             $tricknr  = $a[2];
1387
1388             $playid = DB_play_card($trickid,$handcardid,$sequence);
1389
1390             /* check special output for schweinchen in case in case a fox is being played
1391              * check for correct rules, etc. has already been done
1392              */
1393             if( $GAME['schweinchen-who'] && ($card == 19 || $card == 20) )
1394               {
1395                 if(!$GAME['schweinchen-first'])
1396                   $GAME['schweinchen-first'] = 1; /* playing the first fox */
1397                 else
1398                   $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1399
1400                 if( $RULES['schweinchen']=='both' ||
1401                     ($RULES['schweinchen']=='second' && $GAME['schweinchen-second']==1 )||
1402                     ($RULES['schweinchen']=='secondaftercall' && $GAME['schweinchen-second']==1 &&
1403                      (DB_get_call_by_hash($GAME['schweinchen-who']) || DB_get_partner_call_by_hash($GAME['schweinchen-who']) ))
1404                   )
1405                   {
1406                     DB_insert_comment('Schweinchen! ',$playid,$myid);
1407                     $commentSchweinchen = 'Schweinchen! ';
1408                   }
1409                 if ($debug)
1410                   echo 'schweinchen = '.$GAME['schweinchen-who'].' ---<br />';
1411               }
1412
1413             /* if sequence == 4 check who one in case of wedding */
1414             if($sequence == 4 && $GT == 'wedding')
1415               {
1416                 /* is wedding resolve */
1417                 $resolved = DB_get_sickness_by_gameid($gameid);
1418                 if($resolved<0)
1419                   {
1420                     /* who has wedding */
1421                     $userids = DB_get_all_userid_by_gameid($gameid);
1422                     foreach($userids as $user)
1423                       {
1424                         $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1425                         if($usersick == 'wedding')
1426                           $whosick = $user;
1427                       }
1428                     /* who won the trick */
1429                     $play     = DB_get_cards_by_trick($trickid);
1430                     $winner   = get_winner($play,$gametype); /* returns the position */
1431                     $winnerid = DB_get_userid('gameid-position',$gameid,$winner);
1432                     /* is tricknr <=3 */
1433                     if($tricknr <=3 && $winnerid!=$whosick)
1434                       {
1435                         /* set resolved at tricknr*/
1436                         $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1437                         /* set partner */
1438                         $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1439                         DB_set_party_by_hash($whash,'re');
1440                       }
1441                     if($tricknr == 3 && $winnerid==$whosick)
1442                       {
1443                         /* set resolved at tricknr*/
1444                         $resolved = DB_set_sickness_by_gameid($gameid,'3');
1445                       }
1446                   }
1447               }
1448
1449             /* if sequence == 4, set winner of the trick, count points and set the next player */
1450             if($sequence==4)
1451               {
1452                 $play   = DB_get_cards_by_trick($trickid);
1453                 $winner = get_winner($play,$gametype); /* returns the position */
1454
1455                 /* check if someone caught a fox */
1456                 /* first check if we should account for solos at all,
1457                  * since it doesn't make sense in some games
1458                  */
1459                 $ok = 0; /* fox shouldn't be counted */
1460                 if(DB_get_gametype_by_gameid($gameid)=='solo')
1461                   {
1462                     $solo = DB_get_solo_by_gameid($gameid);
1463                     if($solo == 'trump' || $solo == 'silent')
1464                       $ok = 1; /* for trump solos and silent solos, foxes are ok */
1465                   }
1466                 else
1467                   $ok = 1; /* for all other games (not solos) foxes are ok too */
1468
1469                 if($ok==1)
1470                   foreach($play as $played)
1471                     {
1472                       if ( $played['card']==19 || $played['card']==20 )
1473                         if ($played['pos']!= $winner )
1474                           {
1475                             /* possible caught a fox, check party */
1476                             $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1477                             $uid2 = DB_get_userid('gameid-position',$gameid,$played['pos']);
1478
1479                             $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1480                             $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1481
1482                             if($party1 != $party2)
1483                               DB_query("INSERT INTO Score".
1484                                        " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1485                           }
1486                     }
1487
1488                 /* check for karlchen (jack of clubs in the last trick)*/
1489                 /* same as for foxes, karlchen doesn't always make sense
1490                  * check what kind of game it is and set karlchen accordingly */
1491                 $ok = 1; /* default: karlchen should be accounted for */
1492                 if($tricknr != 12 )
1493                   $ok = 0; /* Karlchen works only in the last trick */
1494                 if($ok && DB_get_gametype_by_gameid($gameid)=='solo' )
1495                   {
1496                     $solo = DB_get_solo_by_gameid($gameid);
1497                     if($solo == 'trumpless' || $solo == 'jack' || $solo == 'queen' )
1498                       $ok = 0; /* no Karlchen in these solos */
1499                   }
1500
1501                 if($ok)
1502                   foreach($play as $played)
1503                     if ( $played['card']==11 || $played['card']==12 )
1504                       if ($played['pos'] == $winner )
1505                         {
1506                           /* possible caught a fox, check party */
1507                           $uid1   = DB_get_userid('gameid-position',$gameid,$winner);
1508                           $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1509
1510                           DB_query("INSERT INTO Score".
1511                                    " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1512                         }
1513                 /* check for doppelopf (>40 points)*/
1514                 $points = 0;
1515                 foreach($play as $played)
1516                   {
1517                     $points += DB_get_card_value_by_cardid($played['card']);
1518                   }
1519                 if($points > 39)
1520                   {
1521                     $uid1   = DB_get_userid('gameid-position',$gameid,$winner);
1522                     $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1523
1524                     DB_query("INSERT INTO Score".
1525                              " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
1526                   }
1527
1528                 if($winner>0)
1529                   DB_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1530                 else
1531                   echo "ERROR during scoring";
1532
1533                 if($debug)
1534                   echo "DEBUG: position $winner won the trick <br />";
1535
1536                 /* who is the next player? */
1537                 $next = $winner;
1538               }
1539             else
1540               {
1541                 $next = DB_get_pos_by_hash($me)+1;
1542               }
1543             if($next==5) $next=1;
1544
1545             /* check for coment */
1546             if(myisset('comment'))
1547               {
1548                 $comment = $_REQUEST['comment'];
1549                 if($comment != '')
1550                   DB_insert_comment($comment,$playid,$myid);
1551                 if($commentSchweinchen)
1552                   $comment = $commentSchweinchen . $comment;
1553               };
1554
1555             /* display played card */
1556             $pos = DB_get_pos_by_hash($me);
1557             if($sequence==1)
1558               {
1559                 echo "  <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1560                   "    <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1561                   "      <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1562               }
1563
1564             echo "      <div class=\"card".($pos-1)."\">\n        ";
1565
1566             /* display comments */
1567             display_card($card,$PREF['cardset']);
1568             if($comment!='')
1569               echo "\n        <span class=\"comment\"> ".$comment."</span>\n";
1570             echo "      </div>\n";
1571
1572             echo "    </div>\n  </li>\n";  /* end div trick, end li trick */
1573
1574             /*check if we still have cards left, else set status to gameover */
1575             if(sizeof(DB_get_hand($me))==0)
1576               {
1577                 DB_set_hand_status_by_hash($me,'gameover');
1578                 $mystatus = 'gameover';
1579               }
1580
1581             /* if all players are done, set game status to game over,
1582              * get the points of the last trick and send out an email
1583              * to all players
1584              */
1585             $userids = DB_get_all_userid_by_gameid($gameid);
1586
1587             $done=1;
1588             foreach($userids as $user)
1589               if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1590                 $done=0;
1591
1592             if($done)
1593               DB_set_game_status_by_gameid($gameid,'gameover');
1594
1595             /* email next player, if game is still running */
1596             if(DB_get_game_status_by_gameid($gameid)=='play')
1597               {
1598                 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1599                 $email     = DB_get_email('hash',$next_hash);
1600                 $who       = DB_get_userid('email',$email);
1601                 DB_set_player_by_gameid($gameid,$who);
1602
1603                 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1604                   "It's your turn  now.\n".
1605                   "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$next_hash."\n\n" ;
1606                 if( DB_get_email_pref_by_uid($who)!='emailaddict' )
1607                   mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
1608               }
1609             else /* send out final email */
1610               {
1611                 /* individual score */
1612                 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1613                                    " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1614                                    " LEFT JOIN User ON User.id=Hand.user_id".
1615                                    " LEFT JOIN Play ON Trick.id=Play.trick_id".
1616                                    " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1617                                    " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1618                                    " WHERE Hand.game_id='$gameid'".
1619                                    " GROUP BY User.fullname" );
1620                 $message  = "The game is over. Thanks for playing :)\n";
1621                 $message .= "Final score:\n";
1622                 while( $r = DB_fetch_array($result) )
1623                   $message .= "   ".$r[0]."(".$r[2].") ".$r[1]."\n";
1624
1625                 $result = DB_query("SELECT  Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1626                                    " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1627                                    " LEFT JOIN User ON User.id=Hand.user_id".
1628                                    " LEFT JOIN Play ON Trick.id=Play.trick_id".
1629                                    " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1630                                    " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1631                                    " WHERE Hand.game_id='$gameid'".
1632                                    " GROUP BY Hand.party" );
1633                 $message .= "\nTotals:\n";
1634                 $re     = 0;
1635                 $contra = 0;
1636                 while( $r = DB_fetch_array($result) )
1637                   {
1638                     $message .= "    ".$r[0]." ".$r[1]."\n";
1639                     if($r[0] == 're')
1640                       $re = $r[1];
1641                     else if($r[0] == 'contra')
1642                       $contra = $r[1];
1643                   }
1644
1645                 /*
1646                  * save score in database
1647                  *
1648                  */
1649
1650                 /* get calls from re/contra */
1651                 $call_re     = NULL;
1652                 $call_contra = NULL;
1653                 foreach($userids as $user)
1654                   {
1655                     $hash  = DB_get_hash_from_gameid_and_userid($gameid,$user);
1656                     $call  = DB_get_call_by_hash($hash);
1657                     $party = DB_get_party_by_hash($hash);
1658
1659                     if($call!=NULL)
1660                       {
1661                         $call = (int) $call;
1662
1663                         if($party=='re')
1664                           {
1665                             if($call_re==NULL)
1666                               $call_re = $call;
1667                             else if( $call < $call_re)
1668                               $call_re = $call;
1669                           }
1670                         else if($party=='contra')
1671                           {
1672                             if($call_contra==NULL)
1673                               $call_contra = $call;
1674                             else if( $call < $call_contra)
1675                               $call_contra = $call;
1676                           }
1677                       }
1678                   }
1679
1680                 /* figure out who one */
1681                 $winning_party = NULL;
1682
1683                 if($call_re == NULL && $call_contra==NULL)
1684                   {
1685                     /* nobody made a call, so it's easy to figure out who won */
1686                     if($re>120)
1687                       $winning_party='re';
1688                     else
1689                       $winning_party='contra';
1690                   }
1691                 else
1692                   {
1693                     /* if one party makes a call, they only win, iff they make enough points
1694                      * if only one party made a call, the other one wins,
1695                      * if the first one didn't make it
1696                      */
1697                     if($call_re)
1698                       {
1699                         $offset = 120 - $call_re;
1700                         if($call_re == 0)
1701                           $offset--; /* since we use a > in the next equation */
1702
1703                         if($re > 120+$offset)
1704                           $winning_party='re';
1705                         else if ($call_contra == NULL )
1706                           $winning_party='contra';
1707                       }
1708
1709                     if($call_contra)
1710                       {
1711                         $offset = 120 - $call_contra;
1712                         if($call_contra == 0)
1713                           $offset--; /* since we use a > in the next equation */
1714
1715                         if($contra > 120+$offset)
1716                           $winning_party='contra';
1717                         else if ($call_re == NULL )
1718                           $winning_party='re';
1719                       }
1720                   }
1721
1722                 /* one point for each call of the other party in case the other party didn't win
1723                  * and one point each in case the party made more than points than one of the calls
1724                  */
1725                 if($winning_party!='contra' && $call_contra!=NULL)
1726                   {
1727                     for( $p=$call_contra;$p<=120; $p+=30 )
1728                       {
1729                           DB_query("INSERT INTO Score".
1730                                    " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
1731                         }
1732
1733                       for( $p=$call_contra; $p<120; $p+=30)
1734                         {
1735                           if( $re >= $p )
1736                             DB_query("INSERT INTO Score".
1737                                      " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'made$p')");
1738                         }
1739                     }
1740                   if($winning_party!='re' and $call_re!=NULL)
1741                     {
1742                       for( $p=$call_re;$p<=120; $p+=30 )
1743                         {
1744                           DB_query("INSERT INTO Score".
1745                                    " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'against$p')");
1746                         }
1747
1748                       for( $p=$call_re; $p<120; $p+=30)
1749                         {
1750                           if( $contra>=$p )
1751                             DB_query("INSERT INTO Score".
1752                                      " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
1753                         }
1754                     }
1755
1756                   /* point in case contra won */
1757                   if($winning_party=='contra')
1758                     {
1759                       DB_query("INSERT INTO Score".
1760                                " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1761                     }
1762
1763                   /* one point each for winning and each 30 points + calls */
1764                   if($winning_party=='re')
1765                     {
1766                       foreach(array(120,150,180,210,240) as $p)
1767                         {
1768                           $offset = 0;
1769                           if($p==240 || $call_contra!=NULL)
1770                             $offset = 1;
1771
1772                           if($re>$p-$offset)
1773                             DB_query("INSERT INTO Score".
1774                                      " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
1775                         }
1776                       /* re called something and won */
1777                       foreach(array(0,30,60,90,120) as $p)
1778                         {
1779                           if($call_re!=NULL && $call_re<$p+1)
1780                             DB_query("INSERT INTO Score".
1781                                      " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
1782                         }
1783                     }
1784                   else if( $winning_party=='contra')
1785                     {
1786                       foreach(array(120,150,180,210,240) as $p)
1787                         {
1788                           $offset = 0;
1789                           if($p==240 || $call_re!=NULL)
1790                             $offset = 1;
1791
1792                           if($contra>$p-$offset)
1793                             DB_query("INSERT INTO Score".
1794                                      " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
1795                         }
1796                       /* re called something and won */
1797                       foreach(array(0,30,60,90,120) as $p)
1798                         {
1799                           if($call_contra!=NULL && $call_contra<$p+1)
1800                             DB_query("INSERT INTO Score".
1801                                      " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
1802                         }
1803                     }
1804
1805
1806                   /* add score points to email */
1807                   $message .= "\n";
1808                   $Tpoint = 0;
1809                   $message .= " Points Re: \n";
1810                   $queryresult = DB_query("SELECT score FROM Score ".
1811                                           "  WHERE game_id=$gameid AND party='re'".
1812                                           " ");
1813                   while($r = DB_fetch_array($queryresult) )
1814                     {
1815                       $message .= "   ".$r[0]."\n";
1816                       $Tpoint ++;
1817                     }
1818                   $message .= " Points Contra: \n";
1819                   $queryresult = DB_query("SELECT score FROM Score ".
1820                                           "  WHERE game_id=$gameid AND party='contra'".
1821                                           " ");
1822                   while($r = DB_fetch_array($queryresult) )
1823                     {
1824                       $message .= "   ".$r[0]."\n";
1825                       $Tpoint --;
1826                     }
1827                   $message .= " Total Points (from the Re point of view): $Tpoint\n";
1828                   $message .= "\n";
1829
1830                   $session = DB_get_session_by_gameid($gameid);
1831                   $score = generate_score_table($session);
1832
1833                   $message .= "Score Table:\n";
1834                   $message .= format_score_table_ascii($score);
1835                   $message .= "\nUse these links to have a look at game ".DB_format_gameid($gameid).": \n";
1836
1837                   /* send out final email */
1838                   $all = array();
1839
1840                   foreach($userids as $user)
1841                     {
1842                       $all[] = DB_get_email('userid',$user);
1843
1844                       /* add links for all players */
1845                       $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1846                       $name = DB_get_name('userid',$user);
1847
1848                       $link = "$name: ".$HOST.$INDEX."?action=game&me=".$hash."\n" ;
1849                       $message .= $link;
1850                     }
1851                   $To = implode(",",$all);
1852
1853                   $message .= "\n\n (you can use reply all on this email to reach all the players.)\n";
1854                   mymail($To,$EmailName."Game over (game ".DB_format_gameid($gameid).") ",$message);
1855               }
1856           }
1857         else
1858           {
1859             echo "can't find that card?! <br />\n";
1860           }
1861       }
1862     else if(myisset('card') && !$myturn )
1863       {
1864         echo "please wait until it's your turn! <br />\n";
1865       }
1866
1867     if($seq!=4 && $trickNR>=1 && !(myisset('card') && $myturn) )
1868       echo "    </div>\n  </li>\n";  /* end div trick, end li trick */
1869
1870     /* display points in case game is over */
1871     if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1872       {
1873         echo "  <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1874           "    <div class=\"trick\" id=\"trick13\">\n";
1875         /* add pic for re/contra
1876          "      <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1877
1878         $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1879                            " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1880                            " LEFT JOIN User ON User.id=Hand.user_id".
1881                            " LEFT JOIN Play ON Trick.id=Play.trick_id".
1882                            " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1883                            " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1884                            " WHERE Hand.game_id='$gameid'".
1885                            " GROUP BY User.fullname" );
1886         while( $r = DB_fetch_array($result))
1887           echo "      <div class=\"card".($r[3]-1)."\">\n".
1888             "        <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
1889             "      </div>\n";
1890
1891         /* display totals */
1892         $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1893                            " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1894                            " LEFT JOIN User ON User.id=Hand.user_id".
1895                            " LEFT JOIN Play ON Trick.id=Play.trick_id".
1896                            " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1897                            " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1898                            " WHERE Hand.game_id='$gameid'".
1899                            " GROUP BY Hand.party" );
1900         echo "<div class=\"total\">\n  Totals:<br />\n";
1901         while( $r = DB_fetch_array($result))
1902           echo "  ".$r[0]." ".$r[1]."<br />\n";
1903
1904         $queryresult = DB_query("SELECT timediff(mod_date,create_date) ".
1905                                 " FROM Game WHERE id='$gameid'");
1906         $r = DB_fetch_array($queryresult);
1907         echo "  <p>This game took ".$r[0]." hours.</p>\n";
1908
1909         echo "  <div class=\"re\">\n   Points Re: <br />\n";
1910         $queryresult = DB_query("SELECT score FROM Score ".
1911                                 "  WHERE game_id=$gameid AND party='re'".
1912                                 " ");
1913         while($r = DB_fetch_array($queryresult) )
1914           echo "   ".$r[0]."<br />\n";
1915         echo "  </div>\n";
1916
1917         echo "  <div class=\"contra\">\n   Points Contra: <br />\n";
1918         $queryresult = DB_query("SELECT score FROM Score ".
1919                                 "  WHERE game_id=$gameid AND party='contra'".
1920                                 " ");
1921         while($r = DB_fetch_array($queryresult) )
1922           echo "   ".$r[0]."<br />\n";
1923         echo "  </div>\n";
1924
1925         echo "</div>\n";
1926
1927         echo "    </div>\n  </li>\n";  /* end div trick, end li trick */
1928       }
1929
1930     echo "  <li onclick=\"hl_prev();\" class=\"old\"><a href=\"#\">prev</a></li>\n";
1931     echo "  <li onclick=\"hl_next();\" class=\"old\"><a href=\"#\">next</a></li>\n";
1932     echo "</ul>\n"; /* end ul tricks*/
1933
1934     $mycards = DB_get_hand($me);
1935     $mycards = mysort($mycards,$gametype);
1936     echo "<div class=\"mycards\">\n";
1937
1938     if($myturn && !myisset('card') && $mystatus=='play' )
1939       {
1940         echo "Hello ".$myname.", it's your turn!  <br />\n";
1941         echo "Your cards are: <br />\n";
1942
1943         /* do we have to follow suite? */
1944         $followsuit = 0;
1945         if(have_suit($mycards,$firstcard))
1946           $followsuit = 1;
1947
1948         foreach($mycards as $card)
1949           {
1950             /* display only cards that the player is allowed to play as links, the rest just display normal
1951              * also check if we have both schweinchen, in that case only display on of them as playable
1952              */
1953             if( ($followsuit && !same_type($card,$firstcard)) ||
1954                 ( (int)($card)==19 && !$GAME['schweinchen-first'] &&
1955                   ($RULES['schweinchen']=='second'||
1956                    ( $RULES['schweinchen']=='secondaftercall' &&
1957                      (DB_get_call_by_hash($GAME['schweinchen-who']) || DB_get_partner_call_by_hash($GAME['schweinchen-who']) )))
1958                   && $GAME['schweinchen-who']==$me  )
1959                 )
1960               display_card($card,$PREF['cardset']);
1961             else
1962               display_link_card($card,$PREF['cardset']);
1963           }
1964       }
1965     else if($mystatus=='play' )
1966       {
1967         echo "Your cards are: <br />\n";
1968         foreach($mycards as $card)
1969           display_card($card,$PREF['cardset']);
1970       }
1971     else if($mystatus=='gameover')
1972       {
1973         $oldcards = DB_get_all_hand($me);
1974         $oldcards = mysort($oldcards,$gametype);
1975         echo "Your cards were: <br />\n";
1976         foreach($oldcards as $card)
1977           display_card($card,$PREF['cardset']);
1978
1979         $userids = DB_get_all_userid_by_gameid($gameid);
1980         foreach($userids as $user)
1981           {
1982             $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1983
1984             if($userhash!=$me)
1985               {
1986                 echo "<br />";
1987
1988                 $name = DB_get_name('userid',$user);
1989                 $oldcards = DB_get_all_hand($userhash);
1990                 $oldcards = mysort($oldcards,$gametype);
1991                 echo "$name's cards were: <br />\n";
1992                 foreach($oldcards as $card)
1993                   display_card($card,$PREF['cardset']);
1994               }
1995           };
1996       }
1997     echo "</div>\n";
1998
1999     /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
2000     if($mystatus=='play')
2001       break;
2002
2003     /* the following happens only when the gamestatus is 'gameover' */
2004     /* check if game is over, display results */
2005     if(DB_get_game_status_by_gameid($gameid)=='play')
2006       {
2007         echo "The game is over for you.. other people still need to play though";
2008       }
2009     break;
2010   default:
2011     myerror("error in testing the status");
2012   }
2013
2014 /* output other games where it is the users turn
2015  * make sure that the people looking at old games don't see the wrong games here
2016  */
2017 if( $mystatus != 'gameover' )
2018   display_user_menu($myid);
2019 else if(  $mystatus == 'gameover' &&
2020        isset($_SESSION['id']) )
2021   {
2022     display_user_menu($_SESSION['id']);
2023   }
2024 else
2025   {
2026     echo "<div class=\"usermenu\">\n";
2027     echo "It's your turn in these games:<br />\n";
2028     echo "Please log in to see this information.\n";
2029     echo "</div>\n";
2030   }
2031
2032 /* display rule set for this game */
2033 echo "<div class=\"gameinfo\">\n";
2034
2035 if($gamestatus == 'play' )
2036   {
2037     $myparty = DB_get_party_by_hash($me);
2038     output_form_calls($me,$myparty);
2039   }
2040 /* get time from the last action of the game */
2041 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
2042 $gameend = time() - strtotime($r[0]);
2043
2044 if($gamestatus == 'play' || $gameend < 60*60*24*7)
2045   {
2046     echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
2047   }
2048
2049 echo "<input type=\"submit\" value=\"submit\" />\n";
2050
2051 /* has this hand been played by others? */
2052 $other_game_ids = DB_played_by_others($gameid);
2053 if(sizeof($other_game_ids)>0 && $mystatus=='gameover')
2054   {
2055     $mypos = DB_get_pos_by_hash($me);
2056     echo "<p>See how other played the same hand: <br />\n";
2057     foreach($other_game_ids as $id)
2058       {
2059         $otherhash = DB_get_hash_from_game_and_pos($id,$mypos);
2060         $othername = DB_get_name('hash',$otherhash);
2061         echo "<a href=\"$INDEX?action=game&amp;me=$otherhash\">$othername</a><br />";
2062       }
2063     echo "</p>\n";
2064   }
2065
2066 echo "</div>\n";
2067
2068 echo "</form>\n";
2069
2070 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' && isset($_SESSION['id']) && $_SESSION['id']==$myid)
2071   {
2072     $session = DB_get_session_by_gameid($gameid);
2073     $result  = DB_query("SELECT id,create_date FROM Game".
2074                         " WHERE session=$session".
2075                         " ORDER BY create_date DESC".
2076                         " LIMIT 1");
2077     $r = -1;
2078     if($result)
2079       $r = DB_fetch_array($result);
2080
2081     if(!$session || $gameid==$r[0])
2082       {
2083         /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
2084         $names = DB_get_all_names_by_gameid($gameid);
2085         $type  = DB_get_gametype_by_gameid($gameid);
2086
2087         if($type=='solo')
2088           {
2089             $solo = DB_get_solo_by_gameid($gameid);
2090
2091             if($solo!='silent') /* repeat game with same first player */
2092               output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
2093             else /* rotate normally */
2094               output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
2095           }
2096         else /* rotate normally */
2097           output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
2098       }
2099   }
2100 ?>