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