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