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