NEW FEATURE: send out reminder email
authorArun <arun@etaner.(none)>
Mon, 8 Oct 2007 14:01:26 +0000 (16:01 +0200)
committerArun <arun@etaner.(none)>
Mon, 8 Oct 2007 14:01:26 +0000 (16:01 +0200)
if a player hasn't moved for a week a reminder email can be send out.
only one per day is possible, tracking in the db

create_database.sql
db.php
index.php

index c5de955f5e02e18de3d71279bec86e1c6ed690fa..d552be58c6fe8954db7994b013148b20b6aa82a8 100644 (file)
@@ -336,14 +336,34 @@ CREATE TABLE `Recovery` (
 -- Dumping data for table `Recovery`
 --
 
 -- Dumping data for table `Recovery`
 --
 
-
-
-
 /*!40000 ALTER TABLE `Recovery` DISABLE KEYS */;
 LOCK TABLES `Recovery` WRITE;
 UNLOCK TABLES;
 /*!40000 ALTER TABLE `Recovery` ENABLE KEYS */;
 
 /*!40000 ALTER TABLE `Recovery` DISABLE KEYS */;
 LOCK TABLES `Recovery` WRITE;
 UNLOCK TABLES;
 /*!40000 ALTER TABLE `Recovery` ENABLE KEYS */;
 
+/*!40000 ALTER TABLE `Reminder` DISABLE KEYS */;
+LOCK TABLES `Reminder` WRITE;
+UNLOCK TABLES;
+/*!40000 ALTER TABLE `Reminder` ENABLE KEYS */;
+
+DROP TABLE IF EXISTS `Reminder`;
+CREATE TABLE `Reminder` (
+  `id` int(11) NOT NULL auto_increment,
+  `user_id` int(11) NOT NULL default '0',
+  `game_id` int(11) NOT NULL default '0',
+  `create_date` timestamp NOT NULL default '0000-00-00 00:00:00',
+  UNIQUE KEY `id` (`id`)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8;
+
+--
+-- Dumping data for table `Reminder`
+--
+
+/*!40000 ALTER TABLE `Reminder` DISABLE KEYS */;
+LOCK TABLES `Reminder` WRITE;
+UNLOCK TABLES;
+/*!40000 ALTER TABLE `Reminder` ENABLE KEYS */;
+
 
 
 
 
 
 
diff --git a/db.php b/db.php
index a6b9c3b8acb1f80a60c9a3933faaf365ee1de792..646f0043a27b66c7facf5cc57d59ce9e2b896fa2 100644 (file)
--- a/db.php
+++ b/db.php
@@ -916,4 +916,28 @@ function DB_format_gameid($gameid)
   return $session.".".$r[0];
 }
 
   return $session.".".$r[0];
 }
 
+function DB_get_reminder($user,$gameid)
+{
+  $queryresult = mysql_query("SELECT COUNT(*) FROM Reminder ".
+                            "  WHERE user_id=$user ".
+                            "  AND game_id=$gameid ".
+                            "  AND DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= create_date".
+                            "  GROUP BY user_id " );
+  
+  $r = mysql_fetch_array($queryresult,MYSQL_NUM);
+  if($r)
+    return $r[0];
+  else
+    return 0;
+}
+
+function DB_set_reminder($user,$gameid)
+{
+  mysql_query("INSERT INTO Reminder ".
+             "  VALUES(NULL, ".DB_quote_smart($user).", ".DB_quote_smart($gameid).
+              ", NULL) ");
+  return 0;
+}
+
+
 ?>
\ No newline at end of file
 ?>
\ No newline at end of file
index c56560782e144977b467a9fdc3006abc61cf8ff3..f2fa3d69d71a616b0d5cff7973b1925943257e72 100644 (file)
--- a/index.php
+++ b/index.php
@@ -214,6 +214,58 @@ else if(myisset("cancle","me"))
     else
       echo "<p>You need to wait longer before you can cancle a game...</p>\n";
   }
     else
       echo "<p>You need to wait longer before you can cancle a game...</p>\n";
   }
+/* send out a reminder */
+else if(myisset("remind","me"))
+  {
+    $me = $_REQUEST["me"];
+    
+    /* test for valid ID */
+    $myid = DB_get_userid_by_hash($me);
+    if(!$myid)
+      {
+       echo "Can't find you in the database, please check the url.<br />\n";
+       echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
+       output_footer();
+       DB_close();
+       exit();
+      }
+    
+    DB_update_user_timestamp($myid);
+    
+    /* get some information from the DB */
+    $gameid   = DB_get_gameid_by_hash($me);
+    $myname   = DB_get_name_by_hash($me);
+
+    /* check if game really is old enough */
+    $result = mysql_query("SELECT mod_date,player,status from Game WHERE id='$gameid' " );
+    $r = mysql_fetch_array($result,MYSQL_NUM);
+    if( (time()-strtotime($r[0]) > 60*60*24*7)  && ($r[2]!='gameover') ) /* = 1 week */
+      {
+       $name = DB_get_name_by_userid($r[1]);
+       $To = DB_get_email_by_userid($r[1]);
+       $userhash = DB_get_hash_from_gameid_and_userid($gameid,$r[1]);
+       
+       $message = "Hello $name, \n\n".
+         "It's your turn in game ".DB_format_gameid($gameid)." \n".
+         "Actually everyone else is waiting for you for more than a week now ;)\n\n".
+         "Please visit this link now to continue: \n".
+         " ".$host."?me=".$userhash."\n\n" ;
+       
+       if(DB_get_reminder($r[1],$gameid)>0)
+         {
+           echo "<p>An email has already been sent out.</p>\n";
+         }
+       else
+         {
+           DB_set_reminder($r[1],$gameid);
+           mymail($To,$EmailName."Reminder: game ".DB_format_gameid($gameid)." it's your turn",$message);
+           
+           echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid).": an email has been sent out.<br /><br /></p>";
+         }
+      }
+    else
+      echo "<p>You need to wait longer before you can send out a reminder...</p>\n";
+  }
 /* handle request from one specific player for one game,
  * (the hash is set on a per game base) */
 else if(myisset("me"))
 /* handle request from one specific player for one game,
  * (the hash is set on a per game base) */
 else if(myisset("me"))
@@ -1761,12 +1813,17 @@ else if( myisset("email","password") || isset($_SESSION["name"]) )
                           else
                             {
                               $name = DB_get_name_by_userid($r[3]);
                           else
                             {
                               $name = DB_get_name_by_userid($r[3]);
+                              $gameid = $r[1];
+                              if(DB_get_reminder($r[3],$gameid)==0)
+                                if(time()-strtotime($r[2]) > 60*60*24*7)
+                                  echo "".
+                                    "<a href=\"$host?remind=1&amp;me=".$r[0]."\">Send a reminder.</a>";
                               echo "(it's $name's turn)\n";
                             };
                         }
                       if(time()-strtotime($r[2]) > 60*60*24*30)
                               echo "(it's $name's turn)\n";
                             };
                         }
                       if(time()-strtotime($r[2]) > 60*60*24*30)
-                        echo " The game has been running for over a month.".
-                          " Do you want to cancel it? <a href=\"$host?cancle=1&amp;me=".$r[0]."\">yes</a>".
+                        echo "".
+                          "<a href=\"$host?cancle=1&amp;me=".$r[0]."\">Cancel?</a>".
                           " (clicking here is final and can't be restored)";
 
                     }
                           " (clicking here is final and can't be restored)";
 
                     }