BUGFIX: store canceled games, don't delete them
authorArun Persaud <arun@nubati.net>
Tue, 17 Mar 2009 05:06:21 +0000 (22:06 -0700)
committerArun Persaud <arun@nubati.net>
Sun, 29 Mar 2009 17:47:55 +0000 (10:47 -0700)
deleting the games could result in the same people getting the same hand redealt over and over again.
By just keeping track of the hands in the game table, this problem should be gone.

Fixed issue #50.

create_database.sql
include/cancelgame.php
include/db.php
include/functions.php
include/game.php
include/output.php
include/user.php

index 47d1b5593fc8621aa80aec1c51bc00f5a5b68c47..319cdd356fd6108db0a8c3fd501f4257716bdc4b 100644 (file)
 /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
 /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
 
+
+--
+-- Table structure for table `Version`
+--
+
+DROP TABLE IF EXISTS `Version`;
+CREATE TABLE `Version` (
+  `version` int NOT NULL default '0'
+) ENGINE=MyISAM DEFAULT CHARSET=utf8;
+
+--
+-- Dumping data for table `Card`
+--
+
+
+/*!40000 ALTER TABLE `Card` DISABLE KEYS */;
+LOCK TABLES `Version` WRITE;
+INSERT INTO `Version` VALUES (1);
+UNLOCK TABLES;
+/*!40000 ALTER TABLE `Version` ENABLE KEYS */;
+
+
 --
 -- Table structure for table `Card`
 --
@@ -98,9 +120,9 @@ CREATE TABLE `Game` (
   `type` enum('normal','solo','wedding','poverty','dpoverty') default NULL,
   `solo` enum('trumpless','jack','queen','trump','club','spade','heart','silent') default NULL,
   `sickness` int(11) default NULL,
-  `startplayer` tinyint(4) default '1', 
-  `player` int(11) default NULL, 
-  `status` enum('pre','play','gameover') default NULL,
+  `startplayer` tinyint(4) default '1',
+  `player` int(11) default NULL,
+  `status` enum('pre','play','gameover','cancel-timedout','cancel-nines','cancel-trump','cancel-noplay') default NULL,
   `ruleset` int(11) default NULL,
   `session` int(11) default NULL,
   `id` int(11) NOT NULL auto_increment,
@@ -133,7 +155,7 @@ CREATE TABLE `Rulesets` (
   `id` int(11) NOT NULL auto_increment,
   UNIQUE KEY `id` (`id`)
 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-  
+
 --
 -- Dumping data for table `Rulesets`
 --
index 224020356f6bf95efc5965905b53255f9d3f639d..71bb58bee2b74a993bb15f16c1c5da29a262d976 100644 (file)
@@ -48,8 +48,8 @@ if(time()-strtotime($r[0]) > 60*60*24*30) /* = 1 month */
        mymail($user,$subject,$message);
       }
 
-    /* delete everything from the dB */
-    DB_cancel_game($me);
+    /* set gamestatus to canceled */
+    cancel_game('timedout',$gameid);
 
     echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid).
       " has been canceled.<br /><br /></p>";
index b84a56670c329c3dfb6ef1c16bcaab19f02ada7c..ef902adaf21e73a0bc23f301f14c0a1aaddcb45b 100644 (file)
@@ -971,7 +971,7 @@ function DB_is_session_active($session)
 {
   $r = DB_query_array("SELECT COUNT(*) FROM Game ".
                      "  WHERE session=$session ".
-                     "  AND status<>'gameover' ");
+                     "  AND status IN ('pre','play') ");
   if($r)
     return $r[0];
   else
index 0559895fbd1ef3684a0c618cf0ab1f83ed5ec1ec..981cabc76010662840bb9573118d0423b08cece6 100644 (file)
@@ -1253,4 +1253,35 @@ function check_vacation($userid)
     return NULL;
 }
 
+function cancel_game($why,$gameid)
+{
+  $gameid = DB_quote_smart($gameid);
+
+  /* update the game table */
+  switch($why)
+    {
+    case 'timedout':
+      DB_query("UPDATE Game SET status='cancel-timedout' WHERE id=$gameid");
+      break;
+    case 'nines':
+      DB_query("UPDATE Game SET status='cancel-nines' WHERE id=$gameid");
+      break;
+    case 'trump':
+      DB_query("UPDATE Game SET status='cancel-trump' WHERE id=$gameid");
+      break;
+    case 'noplay':
+      DB_query("UPDATE Game SET status='cancel-noplay' WHERE id=$gameid");
+      break;
+    }
+  /* set each player to gameover */
+  $result = DB_query("SELECT id FROM Hand WHERE game_id=".DB_quote_smart($gameid));
+  while($r = DB_fetch_array($result))
+    {
+      $id = $r[0];
+      DB_query("UPDATE Hand SET status='gameover' WHERE id=".DB_quote_smart($id));
+    }
+
+  return;
+}
+
 ?>
index 6d47fc9517af6b665884dda628d7750d37d0c27c..1dc4802b3ee7155a16b6aed098b73e97df10e1c1 100644 (file)
@@ -217,7 +217,7 @@ if($session)
 
     echo "  <div class=\"sessionscore\">";
     if($finalscore)
-      { 
+      {
        echo "Score: \n";
        /* output the final score on the front page */
        foreach($finalscore as $user=>$value)
@@ -226,9 +226,9 @@ if($session)
            echo " ".substr($name,0,2).": $value ";
          }
       }
-    else 
+    else
       {
-       /* first game, no score yet */ 
+       /* first game, no score yet */
        echo "&nbsp;";
       }
 
@@ -311,8 +311,8 @@ switch($mystatus)
                mymail($user,$subject,$message);
              }
 
-           /* delete everything from the dB */
-           DB_cancel_game($me);
+           /* update game status */
+           cancel_game('noplay',$gameid);
            break;
          }
        else
@@ -589,7 +589,6 @@ 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".
@@ -603,8 +602,8 @@ switch($mystatus)
                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";
@@ -950,8 +949,8 @@ switch($mystatus)
                    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;
@@ -1075,6 +1074,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
@@ -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);
       }
index 231f8f691cc2a39cfa821bcde88b847956c37bda..94aaa19942d2b204a050f3e75799c2054b0f06eb 100644 (file)
@@ -12,7 +12,7 @@ function output_ask_for_new_game($playerA,$playerB,$playerC,$playerD,$oldgameid)
   global $RULES;
 
   echo "<div class=\"message\">\n<form action=\"index.php?action=new\" method=\"post\">\n";
-  echo "Do you want to continue playing?(This will start a new game, with the next person as dealer.)\n";
+  echo "Do you want to continue playing?(This will start a new game, with the $playerD as dealer.)\n";
   echo "  <input type=\"hidden\" name=\"PlayerA\" value=\"$playerA\" />\n";
   echo "  <input type=\"hidden\" name=\"PlayerB\" value=\"$playerB\" />\n";
   echo "  <input type=\"hidden\" name=\"PlayerC\" value=\"$playerC\" />\n";
index 8855fc070502bade79e253dd5d6b054502415ee7..0273183d0ba5a99b909f8319362ac91c82881527 100644 (file)
@@ -154,7 +154,7 @@ else
            $Multi = ($r[5]>1) ? "multi" : "";
            if($r[4]=='pre')
              echo "   <span class=\"gamestatuspre $Multi\"><a href=\"".$INDEX."?action=game&amp;me=".$r[0]."\">p </a></span>\n";
-           else if ($r[4]=='gameover')
+           else if (in_array($r[4],array('gameover','cancel-timedout','cancel-nines','cancel-noplay','cancel-trump')))
            {
              echo "   <span class=\"gamestatusover $Multi\"><a href=\"".$INDEX."?action=game&amp;me=".$r[0]."\">";
              if($r[5]<2)
@@ -165,7 +165,7 @@ else
            }
            else
              echo "   <span class=\"gamestatusplay $Multi\"><a href=\"".$INDEX."?action=game&amp;me=".$r[0]."\">P </a></span>\n";
-           if($r[4] != 'gameover')
+           if($r[4] == 'pre' || $r[4] == 'play')
              {
                echo "</td>\n<td>\n    ";
                if($r[3]==$myid || !$r[3])
@@ -190,8 +190,7 @@ else
                      echo "(it's $name's turn)\n";
                  };
                if(time()-strtotime($r[2]) > 60*60*24*30)
-                 echo "<a href=\"$INDEX?action=cancel&amp;me=".$r[0]."\">Cancel?</a>".
-                   " (clicking here is final and can't be restored)";
+                 echo "<a href=\"$INDEX?action=cancel&amp;me=".$r[0]."\">Cancel?</a> ";
              }
          }
        echo "</td></tr>\n</table>\n";