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