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