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