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