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