BUGFIX: forgot to include one variable as global in a function
[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     set_gametype($gametype); /* this sets the $CARDS variable */
786     $myparty = DB_get_party_by_hash($me);
787
788     /* the following is part B) of whats needs to be done)
789     /*    check if user wants to give cards back */
790     if(myisset('exchange'))
791       {
792         $exchange    = $_REQUEST['exchange'];
793         $partnerhash = DB_get_partner_hash_by_hash($me);
794         $partnerid   = DB_get_userid('hash',$partnerhash);
795         $partnerhand = DB_get_handid('gameid-userid',$gameid,$partnerid);
796
797         /* if exchange is set to a value>0, exchange that card back to the partner */
798         if($exchange >0)
799           {
800             $result = DB_query("UPDATE Hand_Card SET hand_id='$partnerhand'".
801                                " WHERE hand_id='$myhand' AND card_id=".DB_quote_smart($exchange));
802             DB_add_exchanged_card(DB_quote_smart($exchange),$myhand,$partnerhand);
803           };
804       }
805
806     /* update hand */
807     $mycards = DB_get_hand($me);
808     $mycards = mysort($mycards,$gametype);
809
810     /* output pre-game trick in case user reloads,
811      * only needs to be done when a team has been formed */
812     if($myparty=='re' || $myparty=='contra')
813       {
814         echo "\n<ul class=\"tricks\">\n";
815         echo "  <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
816
817         $mygametype =  DB_get_gametype_by_gameid($gameid);
818
819         echo "  <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
820           "    <div class=\"trick\" id=\"trick0\">\n";
821
822         /* get information so show the cards that have been handed over in a poverty game */
823         output_exchanged_cards();
824
825         echo "    </div>\n  </li>\n";  /* end div trick, end li trick */
826       }
827     /* end output pre-game trick */
828
829     /* check if user need to give more cards back */
830     if( ($myparty=='re' || $myparty=='contra') && count($mycards)>12)
831       {
832         echo "<div class=\"poverty\"> you need to get rid of a few cards</div>\n";
833
834         $type='exchange';
835         echo "<div class=\"mycards\">Your cards are: <br />\n";
836         foreach($mycards as $card)
837           display_link_card($card,$PREF['cardset'],$type);
838         echo "  <input type=\"submit\" class=\"submitbutton\" value=\"select card to give back\" />\n";
839         echo "</div>\n";
840       }
841     else if( ($myparty=='re' || $myparty=='contra') && count($mycards)==12)
842       {
843         /* user is done, ready to play */
844         DB_set_hand_status_by_hash($me,'play');
845
846         /* email start player */
847         $startplayer = DB_get_startplayer_by_gameid($gameid);
848         $email       = DB_get_email('position-gameid',$startplayer,$gameid);
849         $hash        = DB_get_hash_from_game_and_pos($gameid,$startplayer);
850         $who         = DB_get_userid('email',$email);
851         DB_set_player_by_gameid($gameid,$who);
852
853         if($hash!=$me)
854           {
855             if(DB_get_email_pref_by_hash($hash)!='emailaddict')
856               {
857                 /* email startplayer */
858                 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
859                   "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
860                 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
861               }
862           }
863         else
864           echo " Please, <a href=\"$INDEX?action=game&amp;me=$me\">start</a> the game.<br />\n";
865       }
866
867     /* the following is part A) of what needs to be done */
868     if(!myisset('trump'))
869       {
870         if(!$myparty)
871           {
872             echo "<div class=\"poverty\">\n";
873             $userids = DB_get_all_userid_by_gameid($gameid);
874             foreach($userids as $user)
875               {
876                 $name      = DB_get_name('userid',$user);
877                 $usersick  = DB_get_sickness_by_userid_and_gameid($user,$gameid);
878                 $userhash  = DB_get_hash_from_gameid_and_userid($gameid,$user);
879                 $userparty = DB_get_party_by_hash($userhash);
880
881                 if($usersick=='poverty' && !$userparty)
882                   {
883                     $hash    = DB_get_hash_from_gameid_and_userid($gameid,$user);
884                     $cards   = DB_get_hand($hash);
885                     /* count trump */
886                     $nrtrump = 0;
887                     foreach($cards as $card)
888                       if($card<27) $nrtrump++;
889                     echo "Player $name has $nrtrump trump. Do you want to take them?".
890                       "<a href=\"index.php?action=game&amp;me=$me&amp;trump=$user\">Yes</a> <br />\n";
891                   }
892               }
893             echo "<a href=\"index.php?action=game&amp;me=$me&amp;trump=no\">No way</a> <br />\n";
894             echo "</div><div>\n";
895
896             echo "<div class=\"mycards\">Your cards are: <br />\n";
897             foreach($mycards as $card)
898               display_card($card,$PREF['cardset']);
899             echo "</div></div>\n";
900           }
901         break;
902       }
903     else
904       {
905         $trump = $_REQUEST['trump'];
906
907         if($trump=='no')
908           {
909             /* user doesn't want to take trump */
910             DB_set_hand_status_by_hash($me,'play');
911
912             /* set next player who needs to be asked and email him*/
913             $firstsick  = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
914             $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
915
916             /* don't ask people who have poverty */
917             $next=1;
918             if($firstsick=='poverty')
919               {
920                 if($secondsick=='poverty')
921                   $next=3;
922                 else
923                   $next=2;
924               }
925             if($gametype=='dpoverty')
926               {
927                 $next=999; /* need to cancel for sure, since both would need to take the trump */
928               }
929
930             /* no more people to ask, need to cancel the game */
931             if($mypos+$next>4)
932               {
933                 $message = "Hello, \n\n".
934                   "Game ".DB_format_gameid($gameid)." has been canceled since nobody wanted to take the trump.\n";
935
936                 $userids = DB_get_all_userid_by_gameid($gameid);
937                 foreach($userids as $user)
938                   {
939                     $To = DB_get_email('userid',$user);
940                     mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled (poverty not resolved)",$message);
941                   }
942
943                 /* delete everything from the dB */
944                 DB_cancel_game($me);
945
946                 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been canceled.<br /><br /></p>";
947                 return;
948               }
949             else
950               {
951                 /* email next player, set his status to poverty */
952                 $To       = DB_get_email('position-gameid',$mypos+$next,$gameid);
953                 $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
954                 $userid   = DB_get_userid('email',$To);
955
956                 DB_set_player_by_gameid($gameid,$userid);
957                 DB_set_hand_status_by_hash($userhash,'poverty');
958
959                 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
960                   " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
961                 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
962               }
963           }
964         else
965           {
966             /* player wants to take trump, change cards */
967
968             /* user wants to take trump */
969             $trump = $_REQUEST['trump'];
970             $userhand = DB_get_handid('gameid-userid',$gameid,$trump);
971             $userhash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
972
973             /* remember which cards were handed over*/
974             $partnerhand = DB_get_all_hand($userhash);
975             foreach ($partnerhand as $card)
976               if($card<27)
977                 DB_add_exchanged_card($card,$userhand,$myhand);
978
979             /* copy trump from player A to B */
980             $result = DB_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
981
982             /* reload cards */
983             $mycards = DB_get_hand($me);
984
985             /* set re/contra */
986             if($gametype=='poverty')
987               {
988                 $userids = DB_get_all_userid_by_gameid($gameid);
989                 foreach($userids as $user)
990                   {
991                     $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
992                     if($hash==$userhash||$hash==$me)
993                       {
994                         DB_set_party_by_hash($hash,'re');
995                       }
996                     else
997                       {
998                         DB_set_party_by_hash($hash,'contra');
999                         DB_set_hand_status_by_hash($hash,'play'); /* the contra party is ready to play */
1000                       }
1001                   }
1002                 /* check if we are done (in case of no trump handed over), if so, go to 'play' phase right away*/
1003                 if(count($mycards)==12)
1004                   {
1005                     DB_set_hand_status_by_hash($me,'play');
1006                   }
1007               }
1008             else /*dpoverty*/
1009               {
1010                 /* has the re party already been set?*/
1011                 $re_set=0;
1012                 $userids = DB_get_all_userid_by_gameid($gameid);
1013                 foreach($userids as $user)
1014                   {
1015                     $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1016                     $party = DB_get_party_by_hash($hash);
1017                     if($party=='re')
1018                       $re_set=1;
1019                   }
1020                 if($re_set)
1021                   {
1022                     DB_set_party_by_hash($me,'contra');
1023                     DB_set_party_by_hash($userhash,'contra');
1024                   }
1025                 else
1026                   {
1027                     DB_set_party_by_hash($me,'re');
1028                     DB_set_party_by_hash($userhash,'re');
1029
1030                     /* send out email to second non-poverty player */
1031                     $firstsick  = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
1032                     $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
1033
1034                     $next=1;
1035                     if($firstsick=='poverty')
1036                       if($secondsick=='poverty')
1037                         $next=3;
1038                       else
1039                         $next=2;
1040
1041                     if($mypos+$next>4)
1042                       echo "<div class=\"message\">Error in poverty, please contact the Admin</div>\n";
1043
1044                     $To       = DB_get_email('position-gameid',$mypos+$next,$gameid);
1045                     $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
1046                     $userid   = DB_get_userid('email',$To);
1047
1048                     DB_set_player_by_gameid($gameid,$userid);
1049                     DB_set_hand_status_by_hash($userhash,'poverty');
1050
1051                     $message = "Two people have poverty, it's your turn to decide, if you want to take the trump. Please visit:".
1052                       " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
1053                     mymail($To,$EmailName." double poverty (game ".DB_format_gameid($gameid).")",$message);
1054
1055
1056                   }
1057               }
1058             echo "<div class=\"message\"> Please, <a href=\"$INDEX?action=game&amp;me=$me\">continue</a> here.</div>\n";
1059           }
1060       }
1061     echo "</div>";
1062     break;
1063
1064   case 'play':
1065   case 'gameover':
1066     /* both entries here,  so that the tricks are visible for both.
1067      * in case of 'play' there is a break later that skips the last part
1068      */
1069
1070     /* check if all players are ready to play,
1071      * if so, send out email to the startplayer
1072      * only need to do this if the game hasn't started yet
1073      */
1074     $gamestatus = DB_get_game_status_by_gameid($gameid);
1075     if($gamestatus == 'pre')
1076       {
1077         $ok = 1;
1078         $userids = DB_get_all_userid_by_gameid($gameid);
1079         foreach($userids as $user)
1080           {
1081             $userstatus = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
1082             if($userstatus !='play' && $userstatus!='gameover')
1083               {
1084                 $ok = 0;
1085                 DB_set_player_by_gameid($gameid,$user);
1086                 break;
1087               }
1088           }
1089         if($ok)
1090           {
1091             /* only set this after all poverty, etc. are handled*/
1092             DB_set_game_status_by_gameid($gameid,'play');
1093
1094             /* email startplayer */
1095             $startplayer = DB_get_startplayer_by_gameid($gameid);
1096             $email       = DB_get_email('position-gameid',$startplayer,$gameid);
1097             $hash        = DB_get_hash_from_game_and_pos($gameid,$startplayer);
1098             $who         = DB_get_userid('email',$email);
1099             DB_set_player_by_gameid($gameid,$who);
1100
1101             if($hash!=$me && DB_get_email_pref_by_hash($hash)!='emailaddict')
1102               {
1103                 /* email startplayer) */
1104                 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
1105                   "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
1106                 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
1107               }
1108           }
1109       }
1110     /* figure out what kind of game we are playing,
1111      * set the global variables $CARDS['trump'],$CARDS['diamonds'],$CARDS['hearts'],
1112      * $CARDS['clubs'],$CARDS['spades'],$CARDS['foxes']
1113      * accordingly
1114      */
1115
1116     $gametype = DB_get_gametype_by_gameid($gameid);
1117     $GT       = $gametype;
1118     if($gametype=='solo')
1119       {
1120         $gametype = DB_get_solo_by_gameid($gameid);
1121         if($gametype=='silent')
1122           $GT = 'normal';
1123         else
1124           $GT = $gametype.' '.$GT;
1125       }
1126     else
1127       $gametype = 'normal';
1128
1129     set_gametype($gametype); /* this sets the $CARDS variable */
1130
1131     /* get some infos about the game, need to reset this, since it might have changed */
1132     $gamestatus = DB_get_game_status_by_gameid($gameid);
1133
1134     /* has the game started? No, then just wait here...*/
1135     if($gamestatus == 'pre')
1136       {
1137         echo "<p class=\"message\"> You finished the setup, but not everyone else finished it... ".
1138           "You need to wait for the others. Just wait for an email. </p>";
1139
1140         $mycards = DB_get_hand($me);
1141         $mycards = mysort($mycards,$gametype);
1142
1143         echo "<div class=\"mycards\">Your cards are: <br />\n";
1144         foreach($mycards as $card)
1145           display_card($card,$PREF['cardset']);
1146         echo "</div>\n";
1147
1148         break; /* not sure this works... the idea is that you can
1149                 * only  play a card after everyone is ready to play */
1150       }
1151
1152
1153     /* get time from the last action of the game */
1154     $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1155     $gameend = time() - strtotime($r[0]);
1156
1157     /* handle comments in case player didn't play a card, allow comments a week after the end of the game */
1158     if( (!myisset('card') && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
1159       if(myisset('comment'))
1160         {
1161           $comment = $_REQUEST['comment'];
1162           $playid = DB_get_current_playid($gameid);
1163
1164           if($comment != '')
1165             DB_insert_comment($comment,$playid,$myid);
1166         };
1167
1168     /* get everything relevant to display the tricks */
1169     $result = DB_query("SELECT Hand_Card.card_id as card,".
1170                        "       Hand.position as position,".
1171                        "       Play.sequence as sequence, ".
1172                        "       Trick.id, ".
1173                        "       GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
1174                        "                    SEPARATOR '\n' ), ".
1175                        "       Play.create_date, ".
1176                        "       Hand.user_id ".
1177                        "FROM Trick ".
1178                        "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1179                        "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1180                        "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1181                        "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1182                        "LEFT JOIN User On User.id=Comment.user_id ".
1183                        "WHERE Trick.game_id='".$gameid."' ".
1184                        "GROUP BY Trick.id, sequence ".
1185                        "ORDER BY Trick.id, sequence  ASC");
1186     $trickNR   = 0;
1187     $lasttrick = DB_get_max_trickid($gameid);
1188
1189     $play = array(); /* needed to calculate winner later  */
1190     $seq  = 1;
1191     $pos  = DB_get_startplayer_by_gameid($gameid)-1;
1192     $firstcard = ''; /* first card in a trick */
1193
1194     echo "\n<ul class=\"tricks\">\n";
1195     echo "  <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
1196
1197     /* output vorbehalte */
1198     $mygametype =  DB_get_gametype_by_gameid($gameid);
1199     if($mygametype != 'normal' && $mygametype != 'silent') /* only show when needed */
1200       {
1201         echo "  <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
1202              "    <div class=\"trick\" id=\"trick0\">\n";
1203
1204         /* get information so show the cards that have been handed over in a poverty game */
1205         output_exchanged_cards();
1206
1207         echo "    </div>\n  </li>\n";  /* end div trick, end li trick */
1208       }
1209
1210     /* output tricks */
1211     while($r = DB_fetch_array($result))
1212       {
1213         $pos     = $r[1];
1214         $seq     = $r[2];
1215         $trick   = $r[3];
1216         $comment = $r[4];
1217         $user    = $r[6];
1218
1219         /* count number of tricks */
1220         if($seq==1)
1221           $trickNR++;
1222
1223         /* check if first schweinchen has been played */
1224         if( $GAME['schweinchen-who'] && ($r[0] == 19 || $r[0] == 20) )
1225           if(!$GAME['schweinchen-first'])
1226             $GAME['schweinchen-first'] = 1; /* playing the first fox */
1227           else
1228             $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1229
1230         /* save card to be able to find the winner of the trick later */
1231         $play[$seq] = array('card'=>$r[0],'pos'=>$pos);
1232
1233         if($seq==1)
1234           {
1235             /* first card in a trick, output some html */
1236             if($trick!=$lasttrick)
1237               {
1238                 /* start of an old trick? */
1239                 echo "  <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1240                   "    <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1241                   "      <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1242               }
1243             else if($trick==$lasttrick)
1244               {
1245                 /* start of a last trick? */
1246                 echo "  <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1247                   "    <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1248                   "      <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1249               };
1250
1251             /* remember first card, so that we are able to check, what cards can be played */
1252             $firstcard = $r[0];
1253           };
1254
1255         /* display card */
1256         echo "      <div class=\"card".($pos-1)."\">\n";
1257
1258         /* display comments */
1259         if($comment!='')
1260           echo "        <span class=\"comment\">".$comment."</span>\n";
1261
1262         echo '        ';
1263         display_card($r[0],$PREF['cardset']);
1264
1265         echo "      </div>\n"; /* end div card */
1266
1267         /* end of trick? */
1268         if($seq==4)
1269           {
1270             echo "    </div>\n  </li>\n";  /* end div trick, end li trick */
1271           }
1272       }
1273
1274     /* whos turn is it? */
1275     if($seq==4)
1276       {
1277         $winner    = get_winner($play,$gametype); /* returns the position */
1278         $next      = $winner;
1279         $firstcard = ''; /* new trick, no first card */
1280       }
1281     else
1282       {
1283         $next = $pos+1;
1284         if($next==5) $next = 1;
1285       }
1286
1287     /* my turn?, display cards as links, ask for comments*/
1288     if(DB_get_pos_by_hash($me) == $next)
1289       $myturn = 1;
1290     else
1291       $myturn = 0;
1292
1293     /* do we want to play a card? */
1294     if(myisset('card') && $myturn)
1295       {
1296         $card   = $_REQUEST['card'];
1297         $handid = DB_get_handid('hash',$me);
1298         $commentSchweinchen =''; /* used to add a comment when Schweinchen is being played */
1299
1300         /* check if we have card and that we haven't played it yet*/
1301         /* set played in hand_card to true where hand_id and card_id*/
1302         $r = DB_query_array("SELECT id FROM Hand_Card WHERE played='false' and ".
1303                               "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1304         $handcardid = $r[0];
1305
1306         if($handcardid) /* everything ok, play card  */
1307           {
1308             /* update Game timestamp */
1309             DB_update_game_timestamp($gameid);
1310
1311             /* mark card as played */
1312             DB_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1313                      DB_quote_smart($card));
1314
1315             /* get trick id or start new trick */
1316             $a = DB_get_current_trickid($gameid);
1317             $trickid  = $a[0];
1318             $sequence = $a[1];
1319             $tricknr  = $a[2];
1320
1321             $playid = DB_play_card($trickid,$handcardid,$sequence);
1322
1323             /* check special output for schweinchen in case in case a fox is being played
1324              * check for correct rules, etc. has already been done
1325              */
1326             if( $GAME['schweinchen-who'] && ($card == 19 || $card == 20) )
1327               {
1328                 if(!$GAME['schweinchen-first'])
1329                   $GAME['schweinchen-first'] = 1; /* playing the first fox */
1330                 else
1331                   $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1332
1333                 if( $RULES['schweinchen']=='both' ||
1334                     ($RULES['schweinchen']=='second' && $GAME['schweinchen-second']==1 )||
1335                     ($RULES['schweinchen']=='secondaftercall' && $GAME['schweinchen-second']==1 &&
1336                      (DB_get_call_by_hash($GAME['schweinchen-who']) || DB_get_partner_call_by_hash($GAME['schweinchen-who']) ))
1337                   )
1338                   {
1339                     DB_insert_comment('Schweinchen! ',$playid,$myid);
1340                     $commentSchweinchen = 'Schweinchen! ';
1341                   }
1342                 if ($debug)
1343                   echo 'schweinchen = '.$GAME['schweinchen-who'].' ---<br />';
1344               }
1345
1346             /* if sequence == 4 check who one in case of wedding */
1347             if($sequence == 4 && $GT == 'wedding')
1348               {
1349                 /* is wedding resolve */
1350                 $resolved = DB_get_sickness_by_gameid($gameid);
1351                 if($resolved<0)
1352                   {
1353                     /* who has wedding */
1354                     $userids = DB_get_all_userid_by_gameid($gameid);
1355                     foreach($userids as $user)
1356                       {
1357                         $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1358                         if($usersick == 'wedding')
1359                           $whosick = $user;
1360                       }
1361                     /* who won the trick */
1362                     $play     = DB_get_cards_by_trick($trickid);
1363                     $winner   = get_winner($play,$gametype); /* returns the position */
1364                     $winnerid = DB_get_userid('gameid-position',$gameid,$winner);
1365                     /* is tricknr <=3 */
1366                     if($tricknr <=3 && $winnerid!=$whosick)
1367                       {
1368                         /* set resolved at tricknr*/
1369                         $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1370                         /* set partner */
1371                         $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1372                         DB_set_party_by_hash($whash,'re');
1373                       }
1374                     if($tricknr == 3 && $winnerid==$whosick)
1375                       {
1376                         /* set resolved at tricknr*/
1377                         $resolved = DB_set_sickness_by_gameid($gameid,'3');
1378                       }
1379                   }
1380               }
1381
1382             /* if sequence == 4, set winner of the trick, count points and set the next player */
1383             if($sequence==4)
1384               {
1385                 $play   = DB_get_cards_by_trick($trickid);
1386                 $winner = get_winner($play,$gametype); /* returns the position */
1387
1388                 /* check if someone caught a fox */
1389                 /* first check if we should account for solos at all,
1390                  * since it doesn't make sense in some games
1391                  */
1392                 $ok = 0; /* fox shouldn't be counted */
1393                 if(DB_get_gametype_by_gameid($gameid)=='solo')
1394                   {
1395                     $solo = DB_get_solo_by_gameid($gameid);
1396                     if($solo == 'trump' || $solo == 'silent')
1397                       $ok = 1; /* for trump solos and silent solos, foxes are ok */
1398                   }
1399                 else
1400                   $ok = 1; /* for all other games (not solos) foxes are ok too */
1401
1402                 if($ok==1)
1403                   foreach($play as $played)
1404                     {
1405                       if ( $played['card']==19 || $played['card']==20 )
1406                         if ($played['pos']!= $winner )
1407                           {
1408                             /* possible caught a fox, check party */
1409                             $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1410                             $uid2 = DB_get_userid('gameid-position',$gameid,$played['pos']);
1411
1412                             $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1413                             $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1414
1415                             if($party1 != $party2)
1416                               DB_query("INSERT INTO Score".
1417                                        " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1418                           }
1419                     }
1420
1421                 /* check for karlchen (jack of clubs in the last trick)*/
1422                 /* same as for foxes, karlchen doesn't always make sense
1423                  * check what kind of game it is and set karlchen accordingly */
1424                 $ok = 1; /* default: karlchen should be accounted for */
1425                 if($tricknr != 12 )
1426                   $ok = 0; /* Karlchen works only in the last trick */
1427                 if($ok && DB_get_gametype_by_gameid($gameid)=='solo' )
1428                   {
1429                     $solo = DB_get_solo_by_gameid($gameid);
1430                     if($solo == 'trumpless' || $solo == 'jack' || $solo == 'queen' )
1431                       $ok = 0; /* no Karlchen in these solos */
1432                   }
1433
1434                 if($ok)
1435                   foreach($play as $played)
1436                     if ( $played['card']==11 || $played['card']==12 )
1437                       if ($played['pos'] == $winner )
1438                         {
1439                           /* possible caught a fox, check party */
1440                           $uid1   = DB_get_userid('gameid-position',$gameid,$winner);
1441                           $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1442
1443                           DB_query("INSERT INTO Score".
1444                                    " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1445                         }
1446                 /* check for doppelopf (>40 points)*/
1447                 $points = 0;
1448                 foreach($play as $played)
1449                   {
1450                     $points += DB_get_card_value_by_cardid($played['card']);
1451                   }
1452                 if($points > 39)
1453                   {
1454                     $uid1   = DB_get_userid('gameid-position',$gameid,$winner);
1455                     $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1456
1457                     DB_query("INSERT INTO Score".
1458                              " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
1459                   }
1460
1461                 if($winner>0)
1462                   DB_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1463                 else
1464                   echo "ERROR during scoring";
1465
1466                 if($debug)
1467                   echo "DEBUG: position $winner won the trick <br />";
1468
1469                 /* who is the next player? */
1470                 $next = $winner;
1471               }
1472             else
1473               {
1474                 $next = DB_get_pos_by_hash($me)+1;
1475               }
1476             if($next==5) $next=1;
1477
1478             /* check for coment */
1479             if(myisset('comment'))
1480               {
1481                 $comment = $_REQUEST['comment'];
1482                 if($comment != '')
1483                   DB_insert_comment($comment,$playid,$myid);
1484                 if($commentSchweinchen)
1485                   $comment = $commentSchweinchen . $comment;
1486               };
1487
1488             /* display played card */
1489             $pos = DB_get_pos_by_hash($me);
1490             if($sequence==1)
1491               {
1492                 echo "  <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1493                   "    <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1494                   "      <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1495               }
1496
1497             echo "      <div class=\"card".($pos-1)."\">\n        ";
1498
1499             /* display comments */
1500             display_card($card,$PREF['cardset']);
1501             if($comment!='')
1502               echo "\n        <span class=\"comment\"> ".$comment."</span>\n";
1503             echo "      </div>\n";
1504
1505             echo "    </div>\n  </li>\n";  /* end div trick, end li trick */
1506
1507             /*check if we still have cards left, else set status to gameover */
1508             if(sizeof(DB_get_hand($me))==0)
1509               {
1510                 DB_set_hand_status_by_hash($me,'gameover');
1511                 $mystatus = 'gameover';
1512               }
1513
1514             /* if all players are done, set game status to game over,
1515              * get the points of the last trick and send out an email
1516              * to all players
1517              */
1518             $userids = DB_get_all_userid_by_gameid($gameid);
1519
1520             $done=1;
1521             foreach($userids as $user)
1522               if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1523                 $done=0;
1524
1525             if($done)
1526               DB_set_game_status_by_gameid($gameid,'gameover');
1527
1528             /* email next player, if game is still running */
1529             if(DB_get_game_status_by_gameid($gameid)=='play')
1530               {
1531                 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1532                 $email     = DB_get_email('hash',$next_hash);
1533                 $who       = DB_get_userid('email',$email);
1534                 DB_set_player_by_gameid($gameid,$who);
1535
1536                 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1537                   "It's your turn  now.\n".
1538                   "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$next_hash."\n\n" ;
1539                 if( DB_get_email_pref_by_uid($who)!='emailaddict' )
1540                   mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
1541               }
1542             else /* send out final email */
1543               {
1544                 /* individual score */
1545                 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1546                                    " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1547                                    " LEFT JOIN User ON User.id=Hand.user_id".
1548                                    " LEFT JOIN Play ON Trick.id=Play.trick_id".
1549                                    " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1550                                    " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1551                                    " WHERE Hand.game_id='$gameid'".
1552                                    " GROUP BY User.fullname" );
1553                 $message  = "The game is over. Thanks for playing :)\n";
1554                 $message .= "Final score:\n";
1555                 while( $r = DB_fetch_array($result) )
1556                   $message .= "   ".$r[0]."(".$r[2].") ".$r[1]."\n";
1557
1558                 $result = DB_query("SELECT  Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1559                                    " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1560                                    " LEFT JOIN User ON User.id=Hand.user_id".
1561                                    " LEFT JOIN Play ON Trick.id=Play.trick_id".
1562                                    " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1563                                    " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1564                                    " WHERE Hand.game_id='$gameid'".
1565                                    " GROUP BY Hand.party" );
1566                 $message .= "\nTotals:\n";
1567                 $re     = 0;
1568                 $contra = 0;
1569                 while( $r = DB_fetch_array($result) )
1570                   {
1571                     $message .= "    ".$r[0]." ".$r[1]."\n";
1572                     if($r[0] == 're')
1573                       $re = $r[1];
1574                     else if($r[0] == 'contra')
1575                       $contra = $r[1];
1576                   }
1577
1578                 /*
1579                  * save score in database
1580                  *
1581                  */
1582
1583                 /* get calls from re/contra */
1584                 $call_re     = NULL;
1585                 $call_contra = NULL;
1586                 foreach($userids as $user)
1587                   {
1588                     $hash  = DB_get_hash_from_gameid_and_userid($gameid,$user);
1589                     $call  = DB_get_call_by_hash($hash);
1590                     $party = DB_get_party_by_hash($hash);
1591
1592                     if($call!=NULL)
1593                       {
1594                         $call = (int) $call;
1595
1596                         if($party=='re')
1597                           {
1598                             if($call_re==NULL)
1599                               $call_re = $call;
1600                             else if( $call < $call_re)
1601                               $call_re = $call;
1602                           }
1603                         else if($party=='contra')
1604                           {
1605                             if($call_contra==NULL)
1606                               $call_contra = $call;
1607                             else if( $call < $call_contra)
1608                               $call_contra = $call;
1609                           }
1610                       }
1611                   }
1612
1613                 /* figure out who one */
1614                 $winning_party = NULL;
1615
1616                 if($call_re == NULL && $call_contra==NULL)
1617                   {
1618                     /* nobody made a call, so it's easy to figure out who won */
1619                     if($re>120)
1620                       $winning_party='re';
1621                     else
1622                       $winning_party='contra';
1623                   }
1624                 else
1625                   {
1626                     /* if one party makes a call, they only win, iff they make enough points
1627                      * if only one party made a call, the other one wins,
1628                      * if the first one didn't make it
1629                      */
1630                     if($call_re)
1631                       {
1632                         $offset = 120 - $call_re;
1633                         if($call_re == 0)
1634                           $offset--; /* since we use a > in the next equation */
1635
1636                         if($re > 120+$offset)
1637                           $winning_party='re';
1638                         else if ($call_contra == NULL )
1639                           $winning_party='contra';
1640                       }
1641
1642                     if($call_contra)
1643                       {
1644                         $offset = 120 - $call_contra;
1645                         if($call_contra == 0)
1646                           $offset--; /* since we use a > in the next equation */
1647
1648                         if($contra > 120+$offset)
1649                           $winning_party='contra';
1650                         else if ($call_re == NULL )
1651                           $winning_party='re';
1652                       }
1653                   }
1654
1655                 /* one point for each call of the other party in case the other party didn't win
1656                  * and one point each in case the party made more than points than one of the calls
1657                  */
1658                 if($winning_party!='contra' && $call_contra!=NULL)
1659                   {
1660                     for( $p=$call_contra;$p<=120; $p+=30 )
1661                       {
1662                           DB_query("INSERT INTO Score".
1663                                    " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
1664                         }
1665
1666                       for( $p=$call_contra; $p<120; $p+=30)
1667                         {
1668                           if( $re >= $p )
1669                             DB_query("INSERT INTO Score".
1670                                      " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'made$p')");
1671                         }
1672                     }
1673                   if($winning_party!='re' and $call_re!=NULL)
1674                     {
1675                       for( $p=$call_re;$p<=120; $p+=30 )
1676                         {
1677                           DB_query("INSERT INTO Score".
1678                                    " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'against$p')");
1679                         }
1680
1681                       for( $p=$call_re; $p<120; $p+=30)
1682                         {
1683                           if( $contra>=$p )
1684                             DB_query("INSERT INTO Score".
1685                                      " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
1686                         }
1687                     }
1688
1689                   /* point in case contra won */
1690                   if($winning_party=='contra')
1691                     {
1692                       DB_query("INSERT INTO Score".
1693                                " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1694                     }
1695
1696                   /* one point each for winning and each 30 points + calls */
1697                   if($winning_party=='re')
1698                     {
1699                       foreach(array(120,150,180,210,240) as $p)
1700                         {
1701                           $offset = 0;
1702                           if($p==240 || $call_contra!=NULL)
1703                             $offset = 1;
1704
1705                           if($re>$p-$offset)
1706                             DB_query("INSERT INTO Score".
1707                                      " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
1708                         }
1709                       /* re called something and won */
1710                       foreach(array(0,30,60,90,120) as $p)
1711                         {
1712                           if($call_re!=NULL && $call_re<$p+1)
1713                             DB_query("INSERT INTO Score".
1714                                      " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
1715                         }
1716                     }
1717                   else if( $winning_party=='contra')
1718                     {
1719                       foreach(array(120,150,180,210,240) as $p)
1720                         {
1721                           $offset = 0;
1722                           if($p==240 || $call_re!=NULL)
1723                             $offset = 1;
1724
1725                           if($contra>$p-$offset)
1726                             DB_query("INSERT INTO Score".
1727                                      " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
1728                         }
1729                       /* re called something and won */
1730                       foreach(array(0,30,60,90,120) as $p)
1731                         {
1732                           if($call_contra!=NULL && $call_contra<$p+1)
1733                             DB_query("INSERT INTO Score".
1734                                      " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
1735                         }
1736                     }
1737
1738
1739                   /* add score points to email */
1740                   $message .= "\n";
1741                   $Tpoint = 0;
1742                   $message .= " Points Re: \n";
1743                   $queryresult = DB_query("SELECT score FROM Score ".
1744                                           "  WHERE game_id=$gameid AND party='re'".
1745                                           " ");
1746                   while($r = DB_fetch_array($queryresult) )
1747                     {
1748                       $message .= "   ".$r[0]."\n";
1749                       $Tpoint ++;
1750                     }
1751                   $message .= " Points Contra: \n";
1752                   $queryresult = DB_query("SELECT score FROM Score ".
1753                                           "  WHERE game_id=$gameid AND party='contra'".
1754                                           " ");
1755                   while($r = DB_fetch_array($queryresult) )
1756                     {
1757                       $message .= "   ".$r[0]."\n";
1758                       $Tpoint --;
1759                     }
1760                   $message .= " Total Points (from the Re point of view): $Tpoint\n";
1761                   $message .= "\n";
1762
1763                   $session = DB_get_session_by_gameid($gameid);
1764                   $score = generate_score_table($session);
1765
1766                   $message .= "Score Table:\n";
1767                   $message .= format_score_table_ascii($score);
1768                   $message .= "\nUse these links to have a look at game ".DB_format_gameid($gameid).": \n";
1769
1770                   /* send out final email */
1771                   $all = array();
1772
1773                   foreach($userids as $user)
1774                     {
1775                       $all[] = DB_get_email('userid',$user);
1776
1777                       /* add links for all players */
1778                       $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1779                       $name = DB_get_name('userid',$user);
1780
1781                       $link = "$name: ".$HOST.$INDEX."?action=game&me=".$hash."\n" ;
1782                       $message .= $link;
1783                     }
1784                   $To = implode(",",$all);
1785
1786                   $message .= "\n\n (you can use reply all on this email to reach all the players.)\n";
1787                   mymail($To,$EmailName."Game over (game ".DB_format_gameid($gameid).") ",$message);
1788               }
1789           }
1790         else
1791           {
1792             echo "can't find that card?! <br />\n";
1793           }
1794       }
1795     else if(myisset('card') && !$myturn )
1796       {
1797         echo "please wait until it's your turn! <br />\n";
1798       }
1799
1800     if($seq!=4 && $trickNR>=1 && !(myisset('card') && $myturn) )
1801       echo "    </div>\n  </li>\n";  /* end div trick, end li trick */
1802
1803     /* display points in case game is over */
1804     if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1805       {
1806         echo "  <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1807           "    <div class=\"trick\" id=\"trick13\">\n";
1808         /* add pic for re/contra
1809          "      <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1810
1811         $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1812                            " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1813                            " LEFT JOIN User ON User.id=Hand.user_id".
1814                            " LEFT JOIN Play ON Trick.id=Play.trick_id".
1815                            " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1816                            " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1817                            " WHERE Hand.game_id='$gameid'".
1818                            " GROUP BY User.fullname" );
1819         while( $r = DB_fetch_array($result))
1820           echo "      <div class=\"card".($r[3]-1)."\">\n".
1821             "        <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
1822             "      </div>\n";
1823
1824         /* display totals */
1825         $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1826                            " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1827                            " LEFT JOIN User ON User.id=Hand.user_id".
1828                            " LEFT JOIN Play ON Trick.id=Play.trick_id".
1829                            " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1830                            " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1831                            " WHERE Hand.game_id='$gameid'".
1832                            " GROUP BY Hand.party" );
1833         echo "<div class=\"total\">\n  Totals:<br />\n";
1834         while( $r = DB_fetch_array($result))
1835           echo "  ".$r[0]." ".$r[1]."<br />\n";
1836
1837         $queryresult = DB_query("SELECT timediff(mod_date,create_date) ".
1838                                 " FROM Game WHERE id='$gameid'");
1839         $r = DB_fetch_array($queryresult);
1840         echo "  <p>This game took ".$r[0]." hours.</p>\n";
1841
1842         echo "  <div class=\"re\">\n   Points Re: <br />\n";
1843         $queryresult = DB_query("SELECT score FROM Score ".
1844                                 "  WHERE game_id=$gameid AND party='re'".
1845                                 " ");
1846         while($r = DB_fetch_array($queryresult) )
1847           echo "   ".$r[0]."<br />\n";
1848         echo "  </div>\n";
1849
1850         echo "  <div class=\"contra\">\n   Points Contra: <br />\n";
1851         $queryresult = DB_query("SELECT score FROM Score ".
1852                                 "  WHERE game_id=$gameid AND party='contra'".
1853                                 " ");
1854         while($r = DB_fetch_array($queryresult) )
1855           echo "   ".$r[0]."<br />\n";
1856         echo "  </div>\n";
1857
1858         echo "</div>\n";
1859
1860         echo "    </div>\n  </li>\n";  /* end div trick, end li trick */
1861       }
1862
1863     echo "  <li onclick=\"hl_prev();\" class=\"old\"><a href=\"#\">prev</a></li>\n";
1864     echo "  <li onclick=\"hl_next();\" class=\"old\"><a href=\"#\">next</a></li>\n";
1865     echo "</ul>\n"; /* end ul tricks*/
1866
1867     $mycards = DB_get_hand($me);
1868     $mycards = mysort($mycards,$gametype);
1869     echo "<div class=\"mycards\">\n";
1870
1871     if($myturn && !myisset('card') && $mystatus=='play' )
1872       {
1873         echo "Hello ".$myname.", it's your turn!  <br />\n";
1874         echo "Your cards are: <br />\n";
1875
1876         /* do we have to follow suite? */
1877         $followsuit = 0;
1878         if(have_suit($mycards,$firstcard))
1879           $followsuit = 1;
1880
1881         foreach($mycards as $card)
1882           {
1883             /* display only cards that the player is allowed to play as links, the rest just display normal
1884              * also check if we have both schweinchen, in that case only display on of them as playable
1885              */
1886             if( ($followsuit && !same_type($card,$firstcard)) ||
1887                 ( (int)($card)==19 && !$GAME['schweinchen-first'] &&
1888                   ($RULES['schweinchen']=='second'||
1889                    ( $RULES['schweinchen']=='secondaftercall' &&
1890                      (DB_get_call_by_hash($GAME['schweinchen-who']) || DB_get_partner_call_by_hash($GAME['schweinchen-who']) )))
1891                   && $GAME['schweinchen-who']==$me  )
1892                 )
1893               display_card($card,$PREF['cardset']);
1894             else
1895               display_link_card($card,$PREF['cardset']);
1896           }
1897       }
1898     else if($mystatus=='play' )
1899       {
1900         echo "Your cards are: <br />\n";
1901         foreach($mycards as $card)
1902           display_card($card,$PREF['cardset']);
1903       }
1904     else if($mystatus=='gameover')
1905       {
1906         $oldcards = DB_get_all_hand($me);
1907         $oldcards = mysort($oldcards,$gametype);
1908         echo "Your cards were: <br />\n";
1909         foreach($oldcards as $card)
1910           display_card($card,$PREF['cardset']);
1911
1912         $userids = DB_get_all_userid_by_gameid($gameid);
1913         foreach($userids as $user)
1914           {
1915             $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1916
1917             if($userhash!=$me)
1918               {
1919                 echo "<br />";
1920
1921                 $name = DB_get_name('userid',$user);
1922                 $oldcards = DB_get_all_hand($userhash);
1923                 $oldcards = mysort($oldcards,$gametype);
1924                 echo "$name's cards were: <br />\n";
1925                 foreach($oldcards as $card)
1926                   display_card($card,$PREF['cardset']);
1927               }
1928           };
1929       }
1930     echo "</div>\n";
1931
1932     /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1933     if($mystatus=='play')
1934       break;
1935
1936     /* the following happens only when the gamestatus is 'gameover' */
1937     /* check if game is over, display results */
1938     if(DB_get_game_status_by_gameid($gameid)=='play')
1939       {
1940         echo "The game is over for you.. other people still need to play though";
1941       }
1942     break;
1943   default:
1944     myerror("error in testing the status");
1945   }
1946
1947 /* output other games where it is the users turn
1948  * make sure that the people looking at old games don't see the wrong games here
1949  */
1950 if( $mystatus != 'gameover' )
1951   display_user_menu($myid);
1952 else if(  $mystatus == 'gameover' &&
1953        isset($_SESSION['id']) )
1954   {
1955     display_user_menu($_SESSION['id']);
1956   }
1957 else
1958   {
1959     echo "<div class=\"usermenu\">\n";
1960     echo "It's your turn in these games:<br />\n";
1961     echo "Please log in to see this information.\n";
1962     echo "</div>\n";
1963   }
1964
1965 /* display rule set for this game */
1966 echo "<div class=\"gameinfo\">\n";
1967
1968 if($gamestatus == 'play' )
1969   {
1970     $myparty = DB_get_party_by_hash($me);
1971     output_form_calls($me,$myparty);
1972   }
1973 /* get time from the last action of the game */
1974 $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1975 $gameend = time() - strtotime($r[0]);
1976
1977 if($gamestatus == 'play' || $gameend < 60*60*24*7)
1978   {
1979     echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1980   }
1981
1982 echo "<input type=\"submit\" value=\"submit\" />\n";
1983
1984 /* has this hand been played by others? */
1985 $other_game_ids = DB_played_by_others($gameid);
1986 if(sizeof($other_game_ids)>0 && $mystatus=='gameover')
1987   {
1988     $mypos = DB_get_pos_by_hash($me);
1989     echo "<p>See how other played the same hand: <br />\n";
1990     foreach($other_game_ids as $id)
1991       {
1992         $otherhash = DB_get_hash_from_game_and_pos($id,$mypos);
1993         $othername = DB_get_name('hash',$otherhash);
1994         echo "<a href=\"$INDEX?action=game&amp;me=$otherhash\">$othername</a><br />";
1995       }
1996     echo "</p>\n";
1997   }
1998
1999 echo "</div>\n";
2000
2001 echo "</form>\n";
2002
2003 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' && isset($_SESSION['id']) && $_SESSION['id']==$myid)
2004   {
2005     $session = DB_get_session_by_gameid($gameid);
2006     $result  = DB_query("SELECT id,create_date FROM Game".
2007                         " WHERE session=$session".
2008                         " ORDER BY create_date DESC".
2009                         " LIMIT 1");
2010     $r = -1;
2011     if($result)
2012       $r = DB_fetch_array($result);
2013
2014     if(!$session || $gameid==$r[0])
2015       {
2016         /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
2017         $names = DB_get_all_names_by_gameid($gameid);
2018         $type  = DB_get_gametype_by_gameid($gameid);
2019
2020         if($type=='solo')
2021           {
2022             $solo = DB_get_solo_by_gameid($gameid);
2023
2024             if($solo!='silent') /* repeat game with same first player */
2025               output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
2026             else /* rotate normally */
2027               output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
2028           }
2029         else /* rotate normally */
2030           output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
2031       }
2032   }
2033 ?>