some more comments
[e-DoKo.git] / include / game.php
index 845a5629b6cea269fab793e38ef4bff6b3b1af8d..f612b202870aec926035dedd1a9cfc4fe3319e77 100644 (file)
@@ -210,32 +210,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\">";
     if($finalscore)
-      { 
+      {
        echo "Score: \n";
-       /* output the final score on the front page */
        foreach($finalscore as $user=>$value)
          {
            $name = DB_get_name('userid',$user);
            echo " ".substr($name,0,2).": $value ";
          }
       }
-    else 
+    else
       {
-       /* first game, no score yet */ 
+       /* 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;
@@ -302,17 +308,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
@@ -351,14 +357,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);
                    */
                  }
              }
@@ -590,22 +595,20 @@ 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).
-             " 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.";
+           $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.\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";
@@ -731,9 +734,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)
@@ -743,11 +745,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
          {
@@ -757,10 +760,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);
 
@@ -769,7 +771,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);
                  }
              }
          }
@@ -856,9 +859,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)
@@ -868,11 +870,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 */
@@ -942,17 +945,17 @@ 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;
@@ -960,16 +963,16 @@ switch($mystatus)
            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
@@ -1052,21 +1055,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>";
@@ -1078,6 +1079,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
@@ -1104,9 +1126,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')
@@ -1114,7 +1135,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);
              }
          }
       }
@@ -1278,6 +1300,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 */
          }
       }
@@ -1540,15 +1563,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 */
              {
@@ -1779,12 +1804,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);
@@ -1792,10 +1813,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
@@ -2011,7 +2031,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".
@@ -2037,6 +2060,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);
       }