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