CLEANUP: used tidy to clean up the HTML
[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&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   = 1;
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         /* check if first schweinchen has been played */
1093         if( $GAME['schweinchen-who'] && ($r[0] == 19 || $r[0] == 20) )
1094           if(!$GAME['schweinchen-first'])
1095             $GAME['schweinchen-first'] = 1; /* playing the first fox */
1096           else
1097             $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1098
1099         /* save card to be able to find the winner of the trick later */
1100         $play[$seq] = array("card"=>$r[0],"pos"=>$pos);
1101
1102         if($seq==1)
1103           {
1104             /* first card in a trick, output some html */
1105             if($trick!=$lasttrick)
1106               {
1107                 /* start of an old trick? */
1108                 echo "  <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1109                   "    <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1110                   "      <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1111               }
1112             else if($trick==$lasttrick)
1113               {
1114                 /* start of a last trick? */
1115                 echo "  <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1116                   "    <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1117                   "      <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1118               };
1119
1120             /* remember first card, so that we are able to check, what cards can be played */
1121             $firstcard = $r[0];
1122           };
1123
1124         /* display card */
1125         echo "      <div class=\"card".($pos-1)."\">\n";
1126
1127         /* display comments */
1128         if($comment!="")
1129           echo "        <span class=\"comment\">".$comment."</span>\n";
1130
1131         echo "        ";
1132         display_card($r[0],$PREF["cardset"]);
1133
1134         echo "      </div>\n"; /* end div card */
1135
1136         /* end of trick? */
1137         if($seq==4)
1138           {
1139             $trickNR++;
1140             echo "    </div>\n  </li>\n";  /* end div trick, end li trick */
1141           }
1142       }
1143
1144     /* whos turn is it? */
1145     if($seq==4)
1146       {
1147         $winner    = get_winner($play,$gametype); /* returns the position */
1148         $next      = $winner;
1149         $firstcard = ""; /* new trick, no first card */
1150       }
1151     else
1152       {
1153         $next = $pos+1;
1154         if($next==5) $next = 1;
1155       }
1156
1157     /* my turn?, display cards as links, ask for comments*/
1158     if(DB_get_pos_by_hash($me) == $next)
1159       $myturn = 1;
1160     else
1161       $myturn = 0;
1162
1163     /* do we want to play a card? */
1164     if(myisset("card") && $myturn)
1165       {
1166         $card   = $_REQUEST["card"];
1167         $handid = DB_get_handid('hash',$me);
1168         $commentSchweinchen =""; /* used to add a comment when Schweinchen is being played */
1169
1170         /* check if we have card and that we haven't played it yet*/
1171         /* set played in hand_card to true where hand_id and card_id*/
1172         $r = DB_query_array("SELECT id FROM Hand_Card WHERE played='false' and ".
1173                               "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1174         $handcardid = $r[0];
1175
1176         if($handcardid) /* everything ok, play card  */
1177           {
1178             /* update Game timestamp */
1179             DB_update_game_timestamp($gameid);
1180
1181             /* check if a call was made, must do this before we set the card status to played */
1182             if(myisset("call")  && $_REQUEST["call"]  == "120" && can_call(120,$me))
1183               $result = DB_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
1184             if(myisset("call")  && $_REQUEST["call"]  == "90" && can_call(90,$me))
1185               $result = DB_query("UPDATE Hand SET point_call='90'  WHERE hash='$me' ");
1186             if(myisset("call")  && $_REQUEST["call"]  == "60" && can_call(60,$me))
1187               $result = DB_query("UPDATE Hand SET point_call='60'  WHERE hash='$me' ");
1188             if(myisset("call")  && $_REQUEST["call"]  == "30" && can_call(30,$me))
1189               $result = DB_query("UPDATE Hand SET point_call='30'  WHERE hash='$me' ");
1190             if(myisset("call")  && $_REQUEST["call"]  == "0" && can_call(0,$me))
1191               $result = DB_query("UPDATE Hand SET point_call='0'   WHERE hash='$me' ");
1192
1193             /* mark card as played */
1194             DB_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1195                      DB_quote_smart($card));
1196
1197             /* get trick id or start new trick */
1198             $a = DB_get_current_trickid($gameid);
1199             $trickid  = $a[0];
1200             $sequence = $a[1];
1201             $tricknr  = $a[2];
1202
1203             $playid = DB_play_card($trickid,$handcardid,$sequence);
1204
1205             /* check special output for schweinchen in case in case a fox is being played
1206              * check for correct rules, etc. has already been done
1207              */
1208             if( $GAME["schweinchen-who"] && ($card == 19 || $card == 20) )
1209               {
1210                 if(!$GAME['schweinchen-first'])
1211                   $GAME['schweinchen-first'] = 1; /* playing the first fox */
1212                 else
1213                   $GAME['schweinchen-second'] = 1; /* this must be the second fox */
1214
1215                 if( ($GAME['schweinchen-second']==1 && $RULES['schweinchen']=='second') || $RULES['schweinchen']=='both')
1216                   {
1217                     DB_insert_comment("Schweinchen! ",$playid,$myid);
1218                     $commentSchweinchen = "Schweinchen! ";
1219                   }
1220                 if ($debug)
1221                   echo "schweinchen = ".$GAME["schweinchen-who"]." ---<br />";
1222               }
1223
1224             /* if sequence == 4 check who one in case of wedding */
1225             if($sequence == 4 && $GT == "wedding")
1226               {
1227                 /* is wedding resolve */
1228                 $resolved = DB_get_sickness_by_gameid($gameid);
1229                 if($resolved<0)
1230                   {
1231                     /* who has wedding */
1232                     $userids = DB_get_all_userid_by_gameid($gameid);
1233                     foreach($userids as $user)
1234                       {
1235                         $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1236                         if($usersick == "wedding")
1237                           $whosick = $user;
1238                       }
1239                     /* who won the trick */
1240                     $play     = DB_get_cards_by_trick($trickid);
1241                     $winner   = get_winner($play,$gametype); /* returns the position */
1242                     $winnerid = DB_get_userid('gameid-position',$gameid,$winner);
1243                     /* is tricknr <=3 */
1244                     if($tricknr <=3 && $winnerid!=$whosick)
1245                       {
1246                         /* set resolved at tricknr*/
1247                         $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1248                         /* set partner */
1249                         $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1250                         DB_set_party_by_hash($whash,"re");
1251                       }
1252                     if($tricknr == 3 && $winnerid==$whosick)
1253                       {
1254                         /* set resolved at tricknr*/
1255                         $resolved = DB_set_sickness_by_gameid($gameid,'3');
1256                       }
1257                   }
1258               }
1259
1260             /* if sequence == 4, set winner of the trick, count points and set the next player */
1261             if($sequence==4)
1262               {
1263                 $play   = DB_get_cards_by_trick($trickid);
1264                 $winner = get_winner($play,$gametype); /* returns the position */
1265
1266                 /* check if someone caught a fox */
1267                 /* first check if we should account for solos at all,
1268                  * since it doesn't make sense in some games
1269                  */
1270                 $ok = 0; /* fox shouldn't be counted */
1271                 if(DB_get_gametype_by_gameid($gameid)=="solo")
1272                   {
1273                     $solo = DB_get_solo_by_gameid($gameid);
1274                     if($solo == 'trump' || $solo == 'silent')
1275                       $ok = 1; /* for trump solos and silent solos, foxes are ok */
1276                   }
1277                 else
1278                   $ok = 1; /* for all other games (not solos) foxes are ok too */
1279
1280                 if($ok==1)
1281                   foreach($play as $played)
1282                     {
1283                       if ( $played['card']==19 || $played['card']==20 )
1284                         if ($played['pos']!= $winner )
1285                           {
1286                             /* possible caught a fox, check party */
1287                             $uid1 = DB_get_userid('gameid-position',$gameid,$winner);
1288                             $uid2 = DB_get_userid('gameid-position',$gameid,$played['pos']);
1289
1290                             $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1291                             $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1292
1293                             if($party1 != $party2)
1294                               DB_query("INSERT INTO Score".
1295                                        " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1296                           }
1297                     }
1298
1299                 /* check for karlchen (jack of clubs in the last trick)*/
1300                 /* same as for foxes, karlchen doesn't always make sense
1301                  * check what kind of game it is and set karlchen accordingly */
1302                 $ok = 1; /* default: karlchen should be accounted for */
1303                 if($tricknr != 12 )
1304                   $ok = 0; /* Karlchen works only in the last trick */
1305                 if($ok && DB_get_gametype_by_gameid($gameid)=="solo" )
1306                   {
1307                     $solo = DB_get_solo_by_gameid($gameid);
1308                     if($solo == "trumpless" || $solo == "jack" || $solo == "queen" )
1309                       $ok = 0; /* no Karlchen in these solos */
1310                   }
1311
1312                 if($ok)
1313                   foreach($play as $played)
1314                     if ( $played['card']==11 || $played['card']==12 )
1315                       if ($played['pos'] == $winner )
1316                         {
1317                           /* possible caught a fox, check party */
1318                           $uid1   = DB_get_userid('gameid-position',$gameid,$winner);
1319                           $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1320
1321                           DB_query("INSERT INTO Score".
1322                                    " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1323                         }
1324                 /* check for doppelopf (>40 points)*/
1325                 $points = 0;
1326                 foreach($play as $played)
1327                   {
1328                     $points += DB_get_card_value_by_cardid($played['card']);
1329                   }
1330                 if($points > 39)
1331                   {
1332                     $uid1   = DB_get_userid('gameid-position',$gameid,$winner);
1333                     $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1334
1335                     DB_query("INSERT INTO Score".
1336                              " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
1337                   }
1338
1339                 if($winner>0)
1340                   DB_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1341                 else
1342                   echo "ERROR during scoring";
1343
1344                 if($debug)
1345                   echo "DEBUG: position $winner won the trick <br />";
1346
1347                 /* who is the next player? */
1348                 $next = $winner;
1349               }
1350             else
1351               {
1352                 $next = DB_get_pos_by_hash($me)+1;
1353               }
1354             if($next==5) $next=1;
1355
1356             /* check for coment */
1357             if(myisset("comment"))
1358               {
1359                 $comment = $_REQUEST["comment"];
1360                 if($comment != "")
1361                   DB_insert_comment($comment,$playid,$myid);
1362                 if($commentSchweinchen)
1363                   $comment = $commentSchweinchen . $comment;
1364               };
1365
1366             /* check for note */
1367             if(myisset("note"))
1368               {
1369                 $note = $_REQUEST["note"];
1370                 if($note != "")
1371                   DB_insert_note($note,$gameid,$myid);
1372               };
1373
1374             /* display played card */
1375             $pos = DB_get_pos_by_hash($me);
1376             if($sequence==1)
1377               {
1378                 echo "  <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1379                   "    <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1380                   "      <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1381               }
1382
1383             echo "      <div class=\"card".($pos-1)."\">\n        ";
1384
1385             /* display comments */
1386             display_card($card,$PREF["cardset"]);
1387             if($comment!="")
1388               echo "\n        <span class=\"comment\"> ".$comment."</span>\n";
1389             echo "      </div>\n";
1390
1391             /*check if we still have cards left, else set status to gameover */
1392             if(sizeof(DB_get_hand($me))==0)
1393               {
1394                 DB_set_hand_status_by_hash($me,'gameover');
1395                 $mystatus = 'gameover';
1396               }
1397
1398             /* if all players are done, set game status to game over,
1399              * get the points of the last trick and send out an email
1400              * to all players
1401              */
1402             $userids = DB_get_all_userid_by_gameid($gameid);
1403
1404             $done=1;
1405             foreach($userids as $user)
1406               if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1407                 $done=0;
1408
1409             if($done)
1410               DB_set_game_status_by_gameid($gameid,"gameover");
1411
1412             /* email next player, if game is still running */
1413             if(DB_get_game_status_by_gameid($gameid)=='play')
1414               {
1415                 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1416                 $email     = DB_get_email('hash',$next_hash);
1417                 $who       = DB_get_userid('email',$email);
1418                 DB_set_player_by_gameid($gameid,$who);
1419
1420                 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1421                   "It's your turn  now.\n".
1422                   "Use this link to play a card: ".$HOST.$INDEX."?action=game&amp;me=".$next_hash."\n\n" ;
1423                 if( DB_get_email_pref_by_uid($who)!="emailaddict" )
1424                   mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
1425               }
1426             else /* send out final email */
1427               {
1428                 /* individual score */
1429                 $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1430                                    " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1431                                    " LEFT JOIN User ON User.id=Hand.user_id".
1432                                    " LEFT JOIN Play ON Trick.id=Play.trick_id".
1433                                    " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1434                                    " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1435                                    " WHERE Hand.game_id='$gameid'".
1436                                    " GROUP BY User.fullname" );
1437                 $message  = "The game is over. Thanks for playing :)\n";
1438                 $message .= "Final score:\n";
1439                 while( $r = DB_fetch_array($result) )
1440                   $message .= "   ".$r[0]."(".$r[2].") ".$r[1]."\n";
1441
1442                 $result = DB_query("SELECT  Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1443                                    " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1444                                    " LEFT JOIN User ON User.id=Hand.user_id".
1445                                    " LEFT JOIN Play ON Trick.id=Play.trick_id".
1446                                    " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1447                                    " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1448                                    " WHERE Hand.game_id='$gameid'".
1449                                    " GROUP BY Hand.party" );
1450                 $message .= "\nTotals:\n";
1451                 $re     = 0;
1452                 $contra = 0;
1453                 while( $r = DB_fetch_array($result) )
1454                   {
1455                     $message .= "    ".$r[0]." ".$r[1]."\n";
1456                     if($r[0] == "re")
1457                       $re = $r[1];
1458                     else if($r[0] == "contra")
1459                       $contra = $r[1];
1460                   }
1461
1462                 /*
1463                  * save score in database
1464                  *
1465                  */
1466
1467                 /* get calls from re/contra */
1468                 $call_re     = NULL;
1469                 $call_contra = NULL;
1470                 foreach($userids as $user)
1471                   {
1472                     $hash  = DB_get_hash_from_gameid_and_userid($gameid,$user);
1473                     $call  = DB_get_call_by_hash($hash);
1474                     $party = DB_get_party_by_hash($hash);
1475
1476                     if($call!=NULL)
1477                       {
1478                         $call = (int) $call;
1479
1480                         if($party=="re")
1481                           {
1482                             if($call_re==NULL)
1483                               $call_re = $call;
1484                             else if( $call < $call_re)
1485                               $call_re = $call;
1486                           }
1487                         else if($party=="contra")
1488                           {
1489                             if($call_contra==NULL)
1490                               $call_contra = $call;
1491                             else if( $call < $call_re)
1492                               $call_contra = $call;
1493                           }
1494                       }
1495                   }
1496
1497                 /* figure out who one */
1498                 $winning_party = NULL;
1499
1500                 if($call_re == NULL && $call_contra==NULL)
1501                   {
1502                     /* nobody made a call, so it's easy to figure out who won */
1503                     if($re>120)
1504                       $winning_party="re";
1505                     else
1506                       $winning_party="contra";
1507                   }
1508                 else
1509                   {
1510                     /* if one party makes a call, they only win, iff they make enough points
1511                      * if only one party made a call, the other one wins,
1512                      * if the first one didn't make it
1513                      */
1514                     if($call_re)
1515                       {
1516                         $offset = 120 - $call_re;
1517                         if($call_re == 0)
1518                           $offset--; /* since we use a > in the next equation */
1519
1520                         if($re > 120+$offset)
1521                           $winning_party="re";
1522                         else if ($call_contra == NULL )
1523                           $winning_party="contra";
1524                       }
1525
1526                     if($call_contra)
1527                       {
1528                         $offset = 120 - $call_contra;
1529                         if($call_contra == 0)
1530                           $offset--; /* since we use a > in the next equation */
1531
1532                         if($contra > 120+$offset)
1533                           $winning_party="contra";
1534                         else if ($call_re == NULL )
1535                           $winning_party="re";
1536                       }
1537                   }
1538
1539                 /* one point for each call of the other party in case the other party didn't win
1540                  * and one point each in case the party made more than points than one of the calls
1541                  */
1542                 if($winning_party!="contra" && $call_contra!=NULL)
1543                   {
1544                     for( $p=$call_contra;$p<=120; $p+=30 )
1545                       {
1546                           DB_query("INSERT INTO Score".
1547                                    " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
1548                         }
1549
1550                       for( $p=$call_contra; $p<120; $p+=30)
1551                         {
1552                           if( $re >= $p )
1553                             DB_query("INSERT INTO Score".
1554                                      " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'made$p')");
1555                         }
1556                     }
1557                   if($winning_party!="re" and $call_re!=NULL)
1558                     {
1559                       for( $p=$call_re;$p<=120; $p+=30 )
1560                         {
1561                           DB_query("INSERT INTO Score".
1562                                    " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'against$p')");
1563                         }
1564
1565                       for( $p=$call_re; $p<120; $p+=30)
1566                         {
1567                           if( $contra>=$p )
1568                             DB_query("INSERT INTO Score".
1569                                      " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
1570                         }
1571                     }
1572
1573                   /* point in case contra won */
1574                   if($winning_party=="contra")
1575                     {
1576                       DB_query("INSERT INTO Score".
1577                                " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1578                     }
1579
1580                   /* one point each for winning and each 30 points + calls */
1581                   if($winning_party=="re")
1582                     {
1583                       foreach(array(120,150,180,210,240) as $p)
1584                         {
1585                           $offset = 0;
1586                           if($p==240 || $call_contra!=NULL)
1587                             $offset = 1;
1588
1589                           if($re>$p-$offset)
1590                             DB_query("INSERT INTO Score".
1591                                      " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
1592                         }
1593                       /* re called something and won */
1594                       foreach(array(0,30,60,90,120) as $p)
1595                         {
1596                           if($call_re!=NULL && $call_re<$p+1)
1597                             DB_query("INSERT INTO Score".
1598                                      " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
1599                         }
1600                     }
1601                   else if( $winning_party=="contra")
1602                     {
1603                       foreach(array(120,150,180,210,240) as $p)
1604                         {
1605                           $offset = 0;
1606                           if($p==240 || $call_re!=NULL)
1607                             $offset = 1;
1608
1609                           if($contra>$p-$offset)
1610                             DB_query("INSERT INTO Score".
1611                                      " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
1612                         }
1613                       /* re called something and won */
1614                       foreach(array(0,30,60,90,120) as $p)
1615                         {
1616                           if($call_contra!=NULL && $call_contra<$p+1)
1617                             DB_query("INSERT INTO Score".
1618                                      " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
1619                         }
1620                     }
1621
1622
1623                   /* add score points to email */
1624                   $message .= "\n";
1625                   $Tpoint = 0;
1626                   $message .= " Points Re: \n";
1627                   $queryresult = DB_query("SELECT score FROM Score ".
1628                                           "  WHERE game_id=$gameid AND party='re'".
1629                                           " ");
1630                   while($r = DB_fetch_array($queryresult) )
1631                     {
1632                       $message .= "   ".$r[0]."\n";
1633                       $Tpoint ++;
1634                     }
1635                   $message .= " Points Contra: \n";
1636                   $queryresult = DB_query("SELECT score FROM Score ".
1637                                           "  WHERE game_id=$gameid AND party='contra'".
1638                                           " ");
1639                   while($r = DB_fetch_array($queryresult) )
1640                     {
1641                       $message .= "   ".$r[0]."\n";
1642                       $Tpoint --;
1643                     }
1644                   $message .= " Total Points (from the Re point of view): $Tpoint\n";
1645                   $message .= "\n";
1646
1647                   $session = DB_get_session_by_gameid($gameid);
1648                   $score = generate_score_table($session);
1649                   /* convert html to ascii */
1650                   $score = str_replace("<div class=\"scoretable\">\n<table class=\"score\">\n <tr>\n","",$score);
1651                   $score = str_replace("</table></div>\n","",$score);
1652                   $score = str_replace("\n","",$score);
1653                   $score = str_replace(array("<tr>","</tr>","<td>","</td>"),array("","\n","","|"),$score);
1654                   $score = explode("\n",$score);
1655
1656                   $header = array_slice($score,0,1);
1657                   $header = explode("|",$header[0]);
1658                   for($i=0;$i<sizeof($header);$i++)
1659                     $header[$i]=str_pad($header[$i],6," ",STR_PAD_BOTH);
1660                   $header = implode("|",$header);
1661                   $header.= "\n------+------+------+------+------+\n";
1662                   if(sizeof($score)>5) $header.=   "                ...   \n";
1663
1664                   if(sizeof($score)>5) $score = array_slice($score,-5,5);
1665                   for($i=0;$i<sizeof($score);$i++)
1666                     {
1667                       $line = explode("|",$score[$i]);
1668                       for($j=0;$j<sizeof($line);$j++)
1669                         $line[$j]=str_pad($line[$j],6," ",STR_PAD_LEFT);
1670                       $score[$i] = implode("|",$line);
1671                     }
1672
1673                   $score = implode("\n",$score);
1674                   $score = $header.$score;
1675
1676                   $message .= "Score Table:\n";
1677                   $message .= $score;
1678
1679                   /* send out final email */
1680                   $all = array();
1681
1682                   foreach($userids as $user)
1683                     $all[] = DB_get_email('userid',$user);
1684                   $To = implode(",",$all);
1685
1686                   $help = "\n\n (you can use reply all on this email to reach all the players.)\n";
1687                   mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 1(2)",$message.$help);
1688
1689                   foreach($userids as $user)
1690                     {
1691                       $To   = DB_get_email('userid',$user);
1692                       $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1693
1694                       $link = "Use this link to have a look at game ".DB_format_gameid($gameid).": ".
1695                         $HOST.$INDEX."?action=game&amp;me=".$hash."\n\n" ;
1696                       if( DB_get_email_pref_by_uid($user) != "emailaddict" )
1697                         mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 2(2)",$link);
1698                     }
1699                 }
1700             }
1701           else
1702             {
1703               echo "can't find that card?! <br />\n";
1704             }
1705         }
1706       else if(myisset("card") && !$myturn )
1707         {
1708           echo "please wait until it's your turn! <br />\n";
1709         }
1710
1711       if($seq!=4 && $trickNR>=1)
1712         echo "    </div>\n  </li>\n";  /* end div trick, end li trick */
1713
1714       /* display points in case game is over */
1715       if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1716         {
1717           echo "  <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1718             "    <div class=\"trick\" id=\"trick13\">\n";
1719           /* add pic for re/contra
1720            "      <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1721
1722           $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1723                              " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1724                              " LEFT JOIN User ON User.id=Hand.user_id".
1725                              " LEFT JOIN Play ON Trick.id=Play.trick_id".
1726                              " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1727                              " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1728                              " WHERE Hand.game_id='$gameid'".
1729                              " GROUP BY User.fullname" );
1730           while( $r = DB_fetch_array($result))
1731             echo "      <div class=\"card".($r[3]-1)."\">\n".
1732                  "        <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
1733                  "      </div>\n";
1734
1735           echo "    </div>\n  </li>\n";  /* end div trick, end li trick */
1736         }
1737
1738       echo "  <li onclick=\"hl_prev();\" class=\"old\"><a href=\"#\">prev</a></li>\n";
1739       echo "  <li onclick=\"hl_next();\" class=\"old\"><a href=\"#\">next</a></li>\n</ul>\n"; /* end ul tricks*/
1740
1741       echo "<div class=\"notes\"> Personal notes: <br />\n";
1742       $notes = DB_get_notes_by_userid_and_gameid($myid,$gameid);
1743       foreach($notes as $note)
1744         echo "$note <hr />\n";
1745       echo "Insert note:<input name=\"note\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1746       echo "</div> \n";
1747
1748       $mycards = DB_get_hand($me);
1749       $mycards = mysort($mycards,$gametype);
1750       echo "<div class=\"mycards\">\n";
1751
1752       if($myturn && !myisset("card") && $mystatus=='play' )
1753         {
1754           echo "Hello ".$myname.", it's your turn!  <br />\n";
1755           echo "Your cards are: <br />\n";
1756
1757           /* do we have to follow suite? */
1758           $followsuit = 0;
1759           if(have_suit($mycards,$firstcard))
1760             $followsuit = 1;
1761
1762           foreach($mycards as $card)
1763             {
1764               if( ($followsuit && !same_type($card,$firstcard)) ||
1765                   ( (int)($card)==19 && ($RULES['schweinchen']=='second'||$RULES['schweinchen']=='secondaftercall')
1766                     && $GAME['schweinchen-who']==$me && !$GAME['schweinchen-first'] )
1767                   )
1768                 display_card($card,$PREF["cardset"]);
1769               else
1770                 display_link_card($card,$PREF["cardset"]);
1771             }
1772         }
1773       else if($mystatus=='play' )
1774         {
1775           echo "Your cards are: <br />\n";
1776           foreach($mycards as $card)
1777             display_card($card,$PREF["cardset"]);
1778         }
1779       else if($mystatus=='gameover')
1780         {
1781           $oldcards = DB_get_all_hand($me);
1782           $oldcards = mysort($oldcards,$gametype);
1783           echo "Your cards were: <br />\n";
1784           foreach($oldcards as $card)
1785             display_card($card,$PREF["cardset"]);
1786
1787           $userids = DB_get_all_userid_by_gameid($gameid);
1788           foreach($userids as $user)
1789             {
1790               $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1791
1792               if($userhash!=$me)
1793                 {
1794                   echo "<br />";
1795
1796                   $name = DB_get_name('userid',$user);
1797                   $oldcards = DB_get_all_hand($userhash);
1798                   $oldcards = mysort($oldcards,$gametype);
1799                   echo "$name's cards were: <br />\n";
1800                   foreach($oldcards as $card)
1801                     display_card($card,$PREF["cardset"]);
1802                 }
1803             };
1804         }
1805       echo "</div>\n";
1806
1807       /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1808       if($mystatus=='play')
1809         break;
1810
1811       /* the following happens only when the gamestatus is 'gameover' */
1812       /* check if game is over, display results */
1813       if(DB_get_game_status_by_gameid($gameid)=='play')
1814         {
1815           echo "The game is over for you.. other people still need to play though";
1816         }
1817       else
1818         {
1819           $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1820                              " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1821                              " LEFT JOIN User ON User.id=Hand.user_id".
1822                              " LEFT JOIN Play ON Trick.id=Play.trick_id".
1823                              " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1824                              " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1825                              " WHERE Hand.game_id='$gameid'".
1826                              " GROUP BY Hand.party" );
1827           echo "<div class=\"total\"> Totals:<br />\n";
1828           while( $r = DB_fetch_array($result))
1829             echo "  ".$r[0]." ".$r[1]."<br />\n";
1830
1831           $queryresult = DB_query("SELECT timediff(mod_date,create_date) ".
1832                                   " FROM Game WHERE id='$gameid'");
1833           $r = DB_fetch_array($queryresult);
1834           echo "<p>This game took ".$r[0]." hours.</p>";
1835
1836           echo "<div class=\"re\">\n Points Re: <br />\n";
1837           $queryresult = DB_query("SELECT score FROM Score ".
1838                                   "  WHERE game_id=$gameid AND party='re'".
1839                                   " ");
1840           while($r = DB_fetch_array($queryresult) )
1841             echo "   ".$r[0]."<br />\n";
1842           echo "</div>\n";
1843
1844           echo "<div class=\"contra\">\n Points Contra: <br />\n";
1845           $queryresult = DB_query("SELECT score FROM Score ".
1846                                   "  WHERE game_id=$gameid AND party='contra'".
1847                                   " ");
1848           while($r = DB_fetch_array($queryresult) )
1849             echo "   ".$r[0]."<br />\n";
1850           echo "</div>\n";
1851
1852           echo "</div>\n";
1853
1854
1855         }
1856       break;
1857     default:
1858       myerror("error in testing the status");
1859     }
1860     /* output left menu */
1861     display_user_menu();
1862
1863     /* output right menu */
1864
1865       /* display rule set for this game */
1866     echo "<div class=\"gameinfo\">\n";
1867
1868     if($gamestatus != 'pre')
1869       echo " Gametype: $GT <br />\n";
1870
1871     echo "Rules: <br />\n";
1872     echo "10ofhearts : ".$RULES["dullen"]      ."<br />\n";
1873     echo "schweinchen: ".$RULES["schweinchen"] ."<br />\n";
1874     echo "call:        ".$RULES["call"]        ."<br />\n";
1875
1876     echo "<hr />\n";
1877     if($gamestatus == 'play' )
1878       output_form_calls($me);
1879
1880     /* get time from the last action of the game */
1881     $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
1882     $gameend = time() - strtotime($r[0]);
1883
1884     if($gamestatus == 'play' || $gameend < 60*60*24*7)
1885       {
1886         echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1887         echo "<hr />";
1888       }
1889
1890     echo "<input type=\"submit\" value=\"submit\" />\n<hr />\n";
1891
1892     $session = DB_get_session_by_gameid($gameid);
1893     $score   = generate_score_table($session);
1894
1895     echo $score;
1896
1897     echo "</div>\n";
1898
1899     echo "</form>\n";
1900
1901     if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1902       {
1903
1904         $session = DB_get_session_by_gameid($gameid);
1905         $result  = DB_query("SELECT id,create_date FROM Game".
1906                             " WHERE session=$session".
1907                             " ORDER BY create_date DESC".
1908                             " LIMIT 1");
1909         $r = -1;
1910         if($result)
1911           $r = DB_fetch_array($result);
1912
1913         if(!$session || $gameid==$r[0])
1914           {
1915             /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
1916             $names = DB_get_all_names_by_gameid($gameid);
1917             $type  = DB_get_gametype_by_gameid($gameid);
1918
1919             if($type=="solo")
1920               {
1921                 $solo = DB_get_solo_by_gameid($gameid);
1922
1923                 if($solo!='silent') /* repeat game with same first player */
1924                   output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
1925                 else /* rotate normally */
1926                   output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
1927               }
1928             else /* rotate normally */
1929               output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
1930           }
1931       }
1932
1933
1934     output_footer();
1935     DB_close();
1936     exit();
1937 ?>