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