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