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