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