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