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