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