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