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