changed message for new dealer into message for new startplayer
[e-DoKo.git] / include / game.php
index 377d0aa017dba09776f81d21718274fc7c1f8769..a1c02a0b4f90f4929f6c856254bf3b13ca6a7b30 100644 (file)
@@ -1,4 +1,23 @@
 <?php
+/* Copyright 2006, 2007, 2008, 2009, 2010 Arun Persaud <arun@nubati.net>
+ *
+ *   This file is part of e-DoKo.
+ *
+ *   e-DoKo is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation, either version 3 of the License, or
+ *   (at your option) any later version.
+ *
+ *   e-DoKo is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with e-DoKo.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
 /* make sure that we are not called from outside the scripts,
  * use a variable defined in config.php to check this
  */
@@ -24,10 +43,6 @@ if(!$myid)
 
 global $GAME,$RULES,$CARDS;
 
-/* the user has done something, update the timestamp */
-if(isset($_SESSION['id']))
-  DB_update_user_timestamp($_SESSION['id']);
-
 /* get some information from the DB */
 $gameid   = DB_get_gameid_by_hash($me);
 $myname   = DB_get_name('hash',$me);
@@ -67,7 +82,7 @@ if( $gamestatus == 'pre' )
 else
   {
     /* in a game Schweinchen is not valid in all types of games */
-    if( $gametype == 'normal' || $gametype == 'silent' || $gametype=='trump' || $gametype=='wedding')
+    if( in_array($gametype,array('normal','wedding','trump','silent') ))
       if( in_array($RULES['schweinchen'],array('both','second','secondaftercall')) )
        $ok=1;
   }
@@ -210,21 +225,38 @@ if($session)
     echo "  </div>\n  </div>\n";
 
     /* show score */
+
+    echo "  <div class=\"sessionscore\">";
+
     $score   = generate_score_table($session);
+
+    /* get the last entry to show on the main page */
     $tmpscore= $score;
     $finalscore = array_pop($tmpscore);
     $finalscore = $finalscore['players'];
 
-    echo "  <div class=\"sessionscore\">Score: \n";
-    foreach($finalscore as $user=>$value)
+    if($finalscore)
       {
-       $name = DB_get_name('userid',$user);
-       echo " ".substr($name,0,2).": $value ";
+       echo "Score: \n";
+       foreach($finalscore as $user=>$value)
+         {
+           $name = DB_get_name('userid',$user);
+           echo " ".substr($name,0,2).": $value ";
+         }
+      }
+    else
+      {
+       /* first game, no score yet */
+       echo "&nbsp;";
       }
+
+    /* output all games for the score table */
     echo format_score_table_html($score,$myid);
     echo "  </div>\n";
 
-    /* figure out which game in a session we are in and link to the previous and next game if possible */
+    /* figure out which game in a session we are in and link to the
+     * previous and next game if possible
+     */
     $hashes = DB_get_hashes_by_session($session,$myid);
     $next     = NULL;
     $i = 1;
@@ -271,6 +303,16 @@ display_table();
  * play:     game in progress
  * gameover: are we revisiting a game
  */
+
+/* the user has done something, update the timestamp. Use $myid in
+ * active games and check for session-id in old games (myid might be wrong in that case)
+ */
+if($mystatus!='gameover')
+  DB_update_user_timestamp($myid);
+ else
+   if(isset($_SESSION['id']))
+     DB_update_user_timestamp($_SESSION['id']);
+
 switch($mystatus)
   {
   case 'start':
@@ -291,17 +333,17 @@ switch($mystatus)
          {
            /* cancel the game */
            $message = "Hello, \n\n".
-             "the game has been canceled due to the request of one of the players.\n";
+             "the game has been canceled due to the request of one of the players.\n\n";
 
            $userids = DB_get_all_userid_by_gameid($gameid);
            foreach($userids as $user)
              {
-               $To = DB_get_email('userid',$user);
-               mymail($To,"$EmailName game ".DB_format_gameid($gameid)." canceled",$message);
+               $subject = 'Game '.DB_format_gameid($gameid).' canceled';
+               mymail($user,$subject,$message);
              }
 
-           /* delete everything from the dB */
-           DB_cancel_game($me);
+           /* update game status */
+           cancel_game('noplay',$gameid);
            break;
          }
        else
@@ -340,14 +382,13 @@ switch($mystatus)
                  {
                    /* email startplayer */
                    /*
-                    $email       = DB_get_email('position-gameid',$startplayer,$gameid);
                     $hash        = DB_get_hash_from_game_and_pos($gameid,$startplayer);
-                    $who         = DB_get_userid('email',$email);
+                    $who         = DB_get_userid('hash',$hash);
                     DB_set_player_by_gameid($gameid,$who);
 
                     $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
                     "Use this link to go the game: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
-                    mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
+                    mymail($who,"Ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
                    */
                  }
              }
@@ -579,26 +620,25 @@ switch($mystatus)
        else if($nines)
          {
            /* cancel game */
-           /* TODO: should we keep statistics of this? */
-           $message = "Hello, \n\n".
-             " the game has been canceled because ".DB_get_name('userid',$nines).
+           $message = "The game has been canceled because ".DB_get_name('userid',$nines).
              " has five or more nines and nobody is playing solo.\n\n".
-             " To redeal either start a new game or, in case the game was part of a tournament, \n".
-             " go to the last game and use the link at the bottom of the page to redeal.";
+             "To redeal either start a new game or, in case the game was part of a tournament,\n".
+             "go to the last game and use the link at the bottom of the page to redeal.\n\n";
 
            $userids = DB_get_all_userid_by_gameid($gameid);
            foreach($userids as $user)
              {
-               $To = DB_get_email('userid',$user);
-               mymail($To,"$EmailName game ".DB_format_gameid($gameid)." canceled",$message);
+               $subject = 'Game '.DB_format_gameid($gameid).' canceled';
+               mymail($user,$subject,$message);
              }
 
-           /* delete everything from the dB */
-           DB_cancel_game($me);
+           /* update game status */
+           cancel_game('nines',$gameid);
 
-           echo "The game has been canceled because ".DB_get_name('userid',$nines).
-             " has five or more nines and nobody is playing solo.\n";
-           return;
+           echo "<p>The game has been canceled because ".DB_get_name('userid',$nines).
+             " has five or more nines and nobody is playing solo.</p>\n";
+           echo "</div>\n";
+           break;
          }
        else if($poverty==1) /* one person has poverty */
          {
@@ -720,9 +760,8 @@ switch($mystatus)
        if($gametype!='poverty' && $gametype!='dpoverty')
          {
            $startplayer = DB_get_startplayer_by_gameid($gameid);
-           $email       = DB_get_email('position-gameid',$startplayer,$gameid);
            $hash        = DB_get_hash_from_game_and_pos($gameid,$startplayer);
-           $who         = DB_get_userid('email',$email);
+           $who         = DB_get_userid('hash',$hash);
            DB_set_player_by_gameid($gameid,$who);
 
            if($hash!=$me)
@@ -732,11 +771,12 @@ switch($mystatus)
                    /* email startplayer */
                    $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
                      "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
-                   mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
+                   $subject = 'Ready, set, go... (game '.DB_format_gameid($gameid).')';
+                   mymail($who,$subject,$message);
                  }
              }
            else
-             echo " Please, <a href=\"$INDEX?action=game&amp;me=$me\">start</a> the game.<br />\n";
+             echo "<div class=\"message\">Please, <a href=\"$INDEX?action=game&amp;me=$me\">start</a> the game.</div>\n";
          }
        else
          {
@@ -746,10 +786,9 @@ switch($mystatus)
 
            $whoid = DB_get_userid('gameid-position',$gameid,$who);
            if($whoid==$myid)
-             echo " Please, <a href=\"$INDEX?action=game&amp;me=$me\">start</a> the game.<br />\n";
+             echo "<div class=\"message\">Please, <a href=\"$INDEX?action=game&amp;me=$me\">start</a> the game.</div>\n";
            else
              {
-               $email   = DB_get_email('position-gameid',$who,$gameid);
                $whohash = DB_get_hash_from_game_and_pos($gameid,$who);
                DB_set_player_by_gameid($gameid,$whoid);
 
@@ -758,7 +797,8 @@ switch($mystatus)
                    /* email player for poverty */
                    $message = "Poverty: It's your turn now in game ".DB_format_gameid($gameid).".\n".
                      "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$whohash."\n\n" ;
-                   mymail($email,$EmailName."Poverty (game ".DB_format_gameid($gameid).") ",$message);
+                   $subject = 'Poverty (game '.DB_format_gameid($gameid).') ';
+                   mymail($whoid,$subject,$message);
                  }
              }
          }
@@ -845,9 +885,8 @@ switch($mystatus)
 
        /* email start player */
        $startplayer = DB_get_startplayer_by_gameid($gameid);
-       $email       = DB_get_email('position-gameid',$startplayer,$gameid);
        $hash        = DB_get_hash_from_game_and_pos($gameid,$startplayer);
-       $who         = DB_get_userid('email',$email);
+       $who         = DB_get_userid('hash',$hash);
        DB_set_player_by_gameid($gameid,$who);
 
        if($hash!=$me)
@@ -857,11 +896,12 @@ switch($mystatus)
                /* email startplayer */
                $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
                  "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
-               mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
+               $subject = 'Ready, set, go... (game '.DB_format_gameid($gameid).') ';
+               mymail($who,$subject,$message);
              }
          }
        else
-         echo " Please, <a href=\"$INDEX?action=game&amp;me=$me\">start</a> the game.<br />\n";
+         echo "<div class=\"message\">Please, <a href=\"$INDEX?action=game&amp;me=$me\">start</a> the game.</div>\n";
       }
 
     /* the following is part A) of what needs to be done */
@@ -931,34 +971,34 @@ switch($mystatus)
            if($mypos+$next>4)
              {
                $message = "Hello, \n\n".
-                 "Game ".DB_format_gameid($gameid)." has been canceled since nobody wanted to take the trump.\n";
+                 "Game ".DB_format_gameid($gameid)." has been canceled since nobody wanted to take the trump.\n\n";
 
                $userids = DB_get_all_userid_by_gameid($gameid);
                foreach($userids as $user)
                  {
-                   $To = DB_get_email('userid',$user);
-                   mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled (poverty not resolved)",$message);
+                   $subject = 'Game '.DB_format_gameid($gameid).' canceled (poverty not resolved)';
+                   mymail($user,$subject,$message);
                  }
 
-               /* delete everything from the dB */
-               DB_cancel_game($me);
+               /* update game status */
+               cancel_game('trump',$gameid);
 
-               echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been canceled.<br /><br /></p>";
-               return;
+               echo "<p class=\"message\";>Game ".DB_format_gameid($gameid)." has been canceled.<br /><br /></p>";
+               break;
              }
            else
              {
                /* email next player, set his status to poverty */
-               $To       = DB_get_email('position-gameid',$mypos+$next,$gameid);
                $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
-               $userid   = DB_get_userid('email',$To);
+               $userid   = DB_get_userid('hash',$userhash);
 
                DB_set_player_by_gameid($gameid,$userid);
                DB_set_hand_status_by_hash($userhash,'poverty');
 
                $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
                  " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
-               mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
+               $subject = 'Poverty (game '.DB_format_gameid($gameid).')';
+               mymail($userid,$subject,$message);
              }
          }
        else
@@ -1041,21 +1081,19 @@ switch($mystatus)
                    if($mypos+$next>4)
                      echo "<div class=\"message\">Error in poverty, please contact the Admin</div>\n";
 
-                   $To       = DB_get_email('position-gameid',$mypos+$next,$gameid);
                    $userhash = DB_get_hash_from_game_and_pos($gameid,$mypos+$next);
-                   $userid   = DB_get_userid('email',$To);
+                   $userid   = DB_get_userid('hash',$userhash);
 
                    DB_set_player_by_gameid($gameid,$userid);
                    DB_set_hand_status_by_hash($userhash,'poverty');
 
                    $message = "Two people have poverty, it's your turn to decide, if you want to take the trump. Please visit:".
                      " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
-                   mymail($To,$EmailName." double poverty (game ".DB_format_gameid($gameid).")",$message);
-
-
+                   $subject = 'Double poverty (game '.DB_format_gameid($gameid).')';
+                   mymail($userid,$subject,$message);
                  }
              }
-           echo "<div class=\"message\"> Please, <a href=\"$INDEX?action=game&amp;me=$me\">continue</a> here.</div>\n";
+           echo "<div class=\"message\">Please, <a href=\"$INDEX?action=game&amp;me=$me\">continue</a> here.</div>\n";
          }
       }
     echo "</div>";
@@ -1067,6 +1105,27 @@ switch($mystatus)
      * in case of 'play' there is a break later that skips the last part
      */
 
+    /* first check if the game has been canceled and display */
+    switch($gamestatus)
+      {
+      case 'cancel-noplay':
+       echo "<div class=\"message\"><p>The game has been canceled due to the request of one player.</p><p>If this was a mistake all 4 players need to send an Email to $ADMIN_NAME at $ADMIN_EMAIL requesting that the game should be restarted.</p></div>";
+       break;
+      case 'cancel-timedout':
+       echo "<div class=\"message\"><p>The game has been canceled because one player wasn't responding.</p><p>If this was a mistake all 4 players need to send an Email to $ADMIN_NAME at $ADMIN_EMAIL requesting that the game should be restarted.</p></div>";
+       break;
+      case 'cancel-nines':
+      case 'cancel-timedout':
+       echo "<div class=\"message\"><p>The game has been canceled because one player had too many nines.</p></div>";
+       break;
+      case 'cancel-trump':
+       echo "<div class=\"message\"><p>The game has been canceled because nobody wanted to take the trump.</p></div>";
+       break;
+      }
+    /* for these two types, we shouldn't show the cards, since we might want to restart the game */
+    if (in_array($gamestatus,array('cancel-noplay','cancel-timedout')))
+      break;
+
     /* check if all players are ready to play,
      * if so, send out email to the startplayer
      * only need to do this if the game hasn't started yet
@@ -1093,9 +1152,8 @@ switch($mystatus)
 
            /* email startplayer */
            $startplayer = DB_get_startplayer_by_gameid($gameid);
-           $email       = DB_get_email('position-gameid',$startplayer,$gameid);
            $hash        = DB_get_hash_from_game_and_pos($gameid,$startplayer);
-           $who         = DB_get_userid('email',$email);
+           $who         = DB_get_userid('hash',$hash);
            DB_set_player_by_gameid($gameid,$who);
 
            if($hash!=$me && DB_get_email_pref_by_hash($hash)!='emailaddict')
@@ -1103,7 +1161,8 @@ switch($mystatus)
                /* email startplayer) */
                $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
                  "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
-               mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
+               $subject = 'Ready, set, go... (game '.DB_format_gameid($gameid).')';
+               mymail($who,$subject,$message);
              }
          }
       }
@@ -1267,6 +1326,7 @@ switch($mystatus)
        /* end of trick? */
        if($seq==4)
          {
+           $winner    = get_winner($play,$gametype); /* returns the position */
            echo "    </div>\n  </li>\n";  /* end div trick, end li trick */
          }
       }
@@ -1385,7 +1445,10 @@ switch($mystatus)
                $play   = DB_get_cards_by_trick($trickid);
                $winner = get_winner($play,$gametype); /* returns the position */
 
-               /* check if someone caught a fox */
+               /*
+                * check if someone caught a fox
+                *******************************/
+
                /* first check if we should account for solos at all,
                 * since it doesn't make sense in some games
                 */
@@ -1418,7 +1481,10 @@ switch($mystatus)
                          }
                    }
 
-               /* check for karlchen (jack of clubs in the last trick)*/
+               /*
+                * check for karlchen (jack of clubs in the last trick)
+                ******************************************************/
+
                /* same as for foxes, karlchen doesn't always make sense
                 * check what kind of game it is and set karlchen accordingly */
                $ok = 1; /* default: karlchen should be accounted for */
@@ -1443,7 +1509,10 @@ switch($mystatus)
                          DB_query("INSERT INTO Score".
                                   " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
                        }
-               /* check for doppelopf (>40 points)*/
+               /*
+                * check for doppelopf (>40 points)
+                ***********************************/
+
                $points = 0;
                foreach($play as $played)
                  {
@@ -1458,6 +1527,10 @@ switch($mystatus)
                             " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
                  }
 
+               /*
+                * set winner (for this trick)
+                */
+
                if($winner>0)
                  DB_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
                else
@@ -1529,15 +1602,17 @@ switch($mystatus)
            if(DB_get_game_status_by_gameid($gameid)=='play')
              {
                $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
-               $email     = DB_get_email('hash',$next_hash);
-               $who       = DB_get_userid('email',$email);
+               $who       = DB_get_userid('hash',$next_hash);
                DB_set_player_by_gameid($gameid,$who);
 
                $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
                  "It's your turn  now.\n".
                  "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$next_hash."\n\n" ;
                if( DB_get_email_pref_by_uid($who)!='emailaddict' )
-                 mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
+                 {
+                   $subject = 'A card has been played in game '.DB_format_gameid($gameid);
+                   mymail($who,$subject,$message);
+                 }
              }
            else /* send out final email */
              {
@@ -1768,12 +1843,8 @@ switch($mystatus)
                  $message .= "\nUse these links to have a look at game ".DB_format_gameid($gameid).": \n";
 
                  /* send out final email */
-                 $all = array();
-
                  foreach($userids as $user)
                    {
-                     $all[] = DB_get_email('userid',$user);
-
                      /* add links for all players */
                      $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
                      $name = DB_get_name('userid',$user);
@@ -1781,10 +1852,9 @@ switch($mystatus)
                      $link = "$name: ".$HOST.$INDEX."?action=game&me=".$hash."\n" ;
                      $message .= $link;
                    }
-                 $To = implode(",",$all);
-
-                 $message .= "\n\n (you can use reply all on this email to reach all the players.)\n";
-                 mymail($To,$EmailName."Game over (game ".DB_format_gameid($gameid).") ",$message);
+                 $message .= "\n\n (you can use reply all on this email to reach all the players.)\n\n";
+                 $subject = ' Game over (game '.DB_format_gameid($gameid).') ';
+                 mymail($userids,$subject,$message);
              }
          }
        else
@@ -1884,11 +1954,17 @@ switch($mystatus)
             * also check if we have both schweinchen, in that case only display on of them as playable
             */
            if( ($followsuit && !same_type($card,$firstcard)) ||
-               ( (int)($card)==19 && !$GAME['schweinchen-first'] &&
-                 ($RULES['schweinchen']=='second'||
-                  ( $RULES['schweinchen']=='secondaftercall' &&
-                    (DB_get_call_by_hash($GAME['schweinchen-who']) || DB_get_partner_call_by_hash($GAME['schweinchen-who']) )))
-                 && $GAME['schweinchen-who']==$me  )
+               ( (int)($card)==19 &&
+                 !$GAME['schweinchen-first'] &&
+                 ( $RULES['schweinchen']=='second' ||
+                   ( $RULES['schweinchen']=='secondaftercall' &&
+                    (DB_get_call_by_hash($GAME['schweinchen-who']) ||
+                     DB_get_partner_call_by_hash($GAME['schweinchen-who']) )
+                   )
+                 ) &&
+                 $GAME['schweinchen-who']==$me &&
+                 in_array($gametype,array('normal','wedding','trump','silent'))
+                 )
                )
              display_card($card,$PREF['cardset']);
            else
@@ -1905,7 +1981,15 @@ switch($mystatus)
       {
        $oldcards = DB_get_all_hand($me);
        $oldcards = mysort($oldcards,$gametype);
-       echo "Your cards were: <br />\n";
+
+       if(isset($_SESSION['id']) && $myid==$_SESSION['id'])
+         echo "Your cards were: <br />\n";
+       else
+         {
+           $name = DB_get_name('userid',$myid);
+           echo "$name's were: <br />\n";
+         }
+
        foreach($oldcards as $card)
          display_card($card,$PREF['cardset']);
 
@@ -2000,7 +2084,10 @@ echo "</div>\n";
 
 echo "</form>\n";
 
-if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' && isset($_SESSION['id']) && $_SESSION['id']==$myid)
+$gamestatus = DB_get_game_status_by_gameid($gameid);
+if($mystatus=='gameover' &&
+   ($gamestatus =='gameover' || $gamestatus =='cancel-nines' || $gamestatus =='cancel-trump') &&
+   isset($_SESSION['id']) && $_SESSION['id']==$myid)
   {
     $session = DB_get_session_by_gameid($gameid);
     $result  = DB_query("SELECT id,create_date FROM Game".
@@ -2026,6 +2113,8 @@ if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' &&
            else /* rotate normally */
              output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
          }
+       else if($gamestatus == 'cancel-nines' || $gamestatus == 'cancel-trump')
+         output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
        else /* rotate normally */
          output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
       }