BUGFIX: no check for a followup game if someone else already started one
[e-DoKo.git] / index.php
index c2d93d2e8c4fc28a0e8700f27b1370a0d8846fdc..f6e2d571ed4bfd3bb803a2bf0c807116edc2c4cb 100644 (file)
--- a/index.php
+++ b/index.php
@@ -85,6 +85,23 @@ else if(myisset("new"))
        $session = DB_get_session_by_gameid($followup);
        $ruleset = DB_get_ruleset_by_gameid($followup); /* just copy ruleset from old game, 
                                                         this way no manipulation is possible */
+
+       /* check if there is a game in pre or play mode, in that case do nothing */
+       if( DB_is_session_active($session) > 0 )
+         {
+           echo "<p class=\"message\"> There is already a game going on in session $session, you can't start a new one</p>";
+           output_footer();
+           DB_close();
+           exit();
+         }
+       else if ( DB_is_session_active($session) < 0 )
+         {
+           echo "<p class=\"message\"> ERROR: status of session $session couldn't be determined.</p>";
+           output_footer();
+           DB_close();
+           exit();
+         }
+
        if($session)
          mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
                      "'$ruleset','$session' ,NULL)");
@@ -214,6 +231,58 @@ else if(myisset("cancle","me"))
     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"))
@@ -285,63 +354,6 @@ else if(myisset("me"))
     /* put everyting in a form */
     echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
 
-    /* output left menu */
-
-    display_user_menu();
-
-    /* output right menu */
-
-      /* display rule set for this game */
-    echo "<div class=\"gameinfo\">\n";
-
-    if($gamestatus != 'pre')
-      echo " Gametype: $GT <br />\n";
-    
-    echo "Rules: <br />\n";
-    echo "10ofhearts : ".$RULES["dullen"]      ."<br />\n";
-    echo "schweinchen: ".$RULES["schweinchen"] ."<br />\n";
-    echo "call:        ".$RULES["call"]        ."<br />\n";
-
-    echo "<hr />\n";
-    if($mystatus == 'play' )
-      {
-       output_form_calls($me);
-       
-       echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
-       echo "<hr />";
-      }
-
-    echo "<input type=\"submit\" value=\"submit\" />\n";
-
-
-    if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
-      {
-       echo "<hr />\n";
-       
-       $session = DB_get_session_by_gameid($gameid);
-       $result  = mysql_query("SELECT id,create_date FROM Game".
-                              " WHERE session=$session".
-                              " ORDER BY create_date DESC".
-                              " LIMIT 1");
-       $r = -1;
-       if($result)
-         $r = mysql_fetch_array($result,MYSQL_NUM);
-       
-       if(!$session || $gameid==$r[0])
-         {
-           /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
-           $names = DB_get_all_names_by_gameid($gameid);
-           $type  = DB_get_gametype_by_gameid($gameid);
-           
-           if($type=="solo")
-             output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
-           else
-             output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
-         }
-      }
-
-    echo "</div>\n";
-
     /* output game */
 
     /* output extra division in case this game is part of a session */
@@ -354,9 +366,9 @@ else if(myisset("me"))
        foreach($hashes as $hash)
          {
            if($hash == $me)
-             echo "$i ";
+             echo "$i \n";
            else 
-             echo "<a href=\"".$host."?me=".$hash."\">$i</a> ";
+             echo "<a href=\"".$host."?me=".$hash."\">$i</a> \n";
            $i++;
          }
        echo "</div>\n";
@@ -394,7 +406,7 @@ else if(myisset("me"))
         */
        if( !myisset("in") )
          {
-           echo "<p> You need to answer the <a href=\"$host?me=$me\">question</a>.</p>";
+           echo "<p class=\"message\"> You need to answer the <a href=\"$host?me=$me\">question</a>.</p>";
            DB_set_hand_status_by_hash($me,'start');
          }
        else
@@ -444,12 +456,12 @@ else if(myisset("me"))
           * unless a user tries to cheat ;)
           * can also happen if user reloads the page!
           */
-         echo "<p> You need to answer the <a href=\"$host?me=$me&in=yes\">questions</a>.</p>";
+         echo "<p class=\"message\"> You need to answer the <a href=\"$host?me=$me&in=yes\">questions</a>.</p>";
          DB_set_hand_status_by_hash($me,'init');
        }
       else
        {
-         echo "Processing what you selected in the last step...<br />";
+         echo "<p class=\"message\">Processing what you selected in the last step...";
       
          /* check if this sickness needs to be handled first */
          $gametype    = DB_get_gametype_by_gameid($gameid);
@@ -495,7 +507,7 @@ else if(myisset("me"))
              DB_set_sickness_by_hash($me,"nines");
            }
          
-         echo " Ok, done with checking, please go to the <a href=\"$host?me=$me\">next step of the setup</a>.<br />";
+         echo " Ok, done with checking, please go to the <a href=\"$host?me=$me\">next step of the setup</a>.</p>";
          
          /* move on to the next stage*/
          DB_set_hand_status_by_hash($me,'poverty');
@@ -520,8 +532,7 @@ else if(myisset("me"))
              foreach($userids as $user)
                {
                  $To       = DB_get_email_by_userid($user);
-                 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
-                 if($userhash != $me)
+                 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);                  if($userhash != $me)
                    {
                      $message = "Everyone finish the questionary in game ".DB_format_gameid($gameid).", ".
                        "please visit this link now to continue: \n".
@@ -540,7 +551,8 @@ else if(myisset("me"))
        * set that one in the Game table
        * tell people about it.
        */
-      echo "<br /> Checking if someone else selected solo, nines, wedding or poverty.<br />";
+      echo "<div class=\"message\">\n";
+      echo "<p> Checking if someone else selected solo, nines, wedding or poverty.</p>";
       
       /* check if everyone has reached this stage */
       $userids = DB_get_all_userid_by_gameid($gameid);
@@ -814,11 +826,11 @@ else if(myisset("me"))
                  $result = mysql_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
                  
                  /* add hidden button with trump in it to get to the next point */
-                 echo "<div class=\"poverty\">\n";
+                 echo "</div><div class=\"poverty\">\n";
                  echo "  <input type=\"hidden\" name=\"exchange\" value=\"-1\" />\n";
                  echo "  <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
                  echo "  <input type=\"submit\" class=\"submitbutton\" value=\"select cards to give back\" />\n";
-                 echo "</div>\n";
+                 echo "</div><div>\n";
                }
              else if(myisset("trump","exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
                {
@@ -919,7 +931,7 @@ else if(myisset("me"))
                  else
                    {
                      /* else show all trump, have lowest card pre-selected, have hidden setting for */
-                     echo "<div class=\"poverty\"> you need to get rid of a few cards</div>\n";
+                     echo "</div><div class=\"poverty\"> you need to get rid of a few cards</div>\n";
                      
                      set_gametype($gametype); /* this sets the $CARDS variable */
                      $mycards = DB_get_hand($me);
@@ -931,12 +943,12 @@ else if(myisset("me"))
                        display_link_card($card,$PREF["cardset"],$type);
                      echo "  <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
                      echo "  <input type=\"submit\" class=\"submitbutton\" value=\"select one card to give back\" />\n";
-                     echo "</div>\n";
+                     echo "</div><div>\n";
                    }
                }
              else if($who == $mypos || $who == $mypos*10)
                {
-                 echo "<div class=\"poverty\">\n";
+                 echo "</div><div class=\"poverty\">\n";
                  foreach($userids as $user)
                    {
                      $name     = DB_get_name_by_userid($user);
@@ -954,7 +966,7 @@ else if(myisset("me"))
                        }
                    }
                  echo "<a href=\"index.php?me=$me&amp;trump=no\">No,way I take those trump...</a> <br />\n";
-                 echo "</div>\n";
+                 echo "</div><div>\n";
                  
                  echo "Your cards are: <br />\n";
                  $mycards = DB_get_hand($me);
@@ -1030,6 +1042,7 @@ else if(myisset("me"))
          else
            echo "\n <br />";    
        }
+      echo "</div>\n";
       break;
     case 'play':
     case 'gameover': 
@@ -1061,8 +1074,8 @@ else if(myisset("me"))
       /* has the game started? No, then just wait here...*/
       if($gamestatus == 'pre')
        {
-         echo "You finished the setup, but not everyone else finished it... ".
-              "so you need to wait for the others. Just wait for the an email... <br />";
+         echo "<p class=\"message\"> You finished the setup, but not everyone else finished it... ".
+              "so you need to wait for the others. Just wait for the an email... </p>";
          break; /* not sure this works... the idea is that you can 
                  * only  play a card after everyone is ready to play */
        }
@@ -1125,7 +1138,7 @@ else if(myisset("me"))
          date_default_timezone_set($zone);
 
          /* check if first schweinchen has been played */
-         if($r[0] == 19 || $r[0] == 20 )
+         if( $GAME["schweinchen"] && ($r[0] == 19 || $r[0] == 20) )
            $GAME["schweinchen"]++;
          
          /* save card to be able to find the winner of the trick later */
@@ -1235,9 +1248,9 @@ else if(myisset("me"))
              $playid = DB_play_card($trickid,$handcardid,$sequence);
 
              /* check for schweinchen */
-             if($card == 19 || $card == 20 )
+             if($GAME["schweinchen"] && ($card == 19 || $card == 20) )
                {
-                 $GAME["schweinchen"]++;
+                 $GAME["schweinchen"]++; // count how many have been played including this one
                  if($GAME["schweinchen"]==3 && $RULES["schweinchen"]=="second" )
                    DB_insert_comment("Schweinchen! ",$playid,$myid);
                  if($RULES["schweinchen"]=="both" )
@@ -1481,18 +1494,6 @@ else if(myisset("me"))
        }
       else if($mystatus=='gameover')
        {
-         /* get time from the last action of the game */
-         $result  = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
-         $r       = mysql_fetch_array($result,MYSQL_NUM);
-         $gameend = time() - strtotime($r[0]);
-         
-         if( $gameend < 60*60*24*7 )
-           {
-             echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"30\" maxlength=\"100\" />\n";
-             echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />\n";
-             echo "<input type=\"submit\" value=\"submit\" />\n";
-           }
-
          $oldcards = DB_get_all_hand($me);
          $oldcards = mysort($oldcards,$gametype);
          echo "Your cards were: <br />\n";
@@ -1549,6 +1550,69 @@ else if(myisset("me"))
     default:
       myerror("error in testing the status");
     }
+    /* output left menu */
+    display_user_menu();
+
+    /* output right menu */
+
+      /* display rule set for this game */
+    echo "<div class=\"gameinfo\">\n";
+
+    if($gamestatus != 'pre')
+      echo " Gametype: $GT <br />\n";
+    
+    echo "Rules: <br />\n";
+    echo "10ofhearts : ".$RULES["dullen"]      ."<br />\n";
+    echo "schweinchen: ".$RULES["schweinchen"] ."<br />\n";
+    echo "call:        ".$RULES["call"]        ."<br />\n";
+
+    echo "<hr />\n";
+    if($gamestatus == 'play' )
+      output_form_calls($me);
+
+    /* get time from the last action of the game */
+    $result  = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
+    $r       = mysql_fetch_array($result,MYSQL_NUM);
+    $gameend = time() - strtotime($r[0]);
+    
+    if($gamestatus == 'play' || $gameend < 60*60*24*7)
+      {
+       echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
+       echo "<hr />";
+      }
+
+    echo "<input type=\"submit\" value=\"submit\" />\n";
+
+
+    if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
+      {
+       echo "<hr />\n";
+       
+       $session = DB_get_session_by_gameid($gameid);
+       $result  = mysql_query("SELECT id,create_date FROM Game".
+                              " WHERE session=$session".
+                              " ORDER BY create_date DESC".
+                              " LIMIT 1");
+       $r = -1;
+       if($result)
+         $r = mysql_fetch_array($result,MYSQL_NUM);
+       
+       if(!$session || $gameid==$r[0])
+         {
+           /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
+           $names = DB_get_all_names_by_gameid($gameid);
+           $type  = DB_get_gametype_by_gameid($gameid);
+           
+           if($type=="solo")
+             output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
+           else
+             output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
+         }
+      }
+
+    echo "</div>\n";
+
+
     echo "</form>\n";
     output_footer();
     DB_close();
@@ -1714,90 +1778,80 @@ else if( myisset("email","password") || isset($_SESSION["name"]) )
               /* display links to settings */
               output_user_settings($email,$password);
               
-              echo "last login: ".date("r",$unixtime)."<br />";
+              echo "<div style=\"position:absolute; font-size:smaller; top:0; left:0; \">last login: ".date("r",$unixtime)."</div>";
               
               DB_update_user_timestamp($myid);
             
               display_user_menu();
   
-              echo "<h4>These are your games that haven't started yet:</h4>\n";
-              $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.mod_date,Game.player from Hand".
-                                    " LEFT JOIN Game On Hand.game_id=Game.id".
-                                    " WHERE Hand.user_id='$myid' AND Game.status='pre'".
-                                    " ORDER BY Game.session" );
-              echo "<p>\n";
-              while( $r = mysql_fetch_array($result,MYSQL_NUM))
-                {
-                  echo "<a href=\"".$host."?me=".$r[0]."\">game ".DB_format_gameid($r[1])." </a>";
-                  if($r[3]==$myid || $r[3]==NULL)
-                    echo "(it's <strong>your</strong> turn)\n";
-                  else
-                    {
-                      $name = DB_get_name_by_userid($r[3]);
-                      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>".
-                      " (clicking here is final and can't be restored)";
-                  echo "<br />";
-                }
-              echo "</p>\n";
-
-              echo "<h4>These are the games you are playing in:</h4>\n";
-              $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.mod_date,Game.player from Hand".
-                                    " LEFT JOIN Game On Hand.game_id=Game.id".
-                                    " WHERE Hand.user_id='$myid' AND Game.status='play'".
-                                    " ORDER BY Game.session" );
-              echo "<p>\n";
-              while( $r = mysql_fetch_array($result,MYSQL_NUM))
-                {
-                  echo "<a href=\"".$host."?me=".$r[0]."\">game ".DB_format_gameid($r[1])." </a>";
-                  if($r[3])
-                    {
-                      if($r[3]==$myid)
-                        echo "(it's <strong>your</strong> turn)\n";
-                      else
-                        {
-                          $name = DB_get_name_by_userid($r[3]);
-                          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>".
-                      " (clicking here is final and can't be restored)";
-                  echo "<br />";
-                }
+              echo "<div class=\"user\">";
+              echo "<h4>These are all your games:</h4>\n";
+              echo "<p>Session: <br />\n";
+              echo "<span class=\"gamestatuspre\"> p </span> =  pre-game phase ";
+              echo "<span class=\"gamestatusplay\">P </span> =  game in progess ";
+              echo "<span class=\"gamestatusover\">F </span> =  game finished <br />";
               echo "</p>\n";
               
-              
-              echo "<h4>And these are your games that are already done:</h4>\n";
-              echo "<p>Session:\n";
               $output = array();
-              $result = mysql_query("SELECT hash,game_id from Hand".
+              $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.mod_date,Game.player,Game.status from Hand".
                                     " LEFT JOIN Game ON Game.id=Hand.game_id".
-                                    " WHERE user_id='$myid' AND Game.status='gameover'".
+                                    " WHERE user_id='$myid'".
                                     " ORDER BY Game.session,Game.create_date" );
-              $gamenrold = 0;
-              echo "<br />\n";
+              $gamenrold = -1;
+              echo "<table>\n <tr><td>\n";
               while( $r = mysql_fetch_array($result,MYSQL_NUM))
                 {
                   $game = DB_format_gameid($r[1]);
                   $gamenr = (int) $game;
                   if($gamenrold < $gamenr)
                     {
+                      if($gamenrold!=-1)
+                        echo "</td></tr>\n <tr> <td>$gamenr:</td><td> ";
+                      else
+                        echo "$gamenr:</td><td> ";
                       $gamenrold = $gamenr;
-                      echo "<br /> $gamenr: ";
                     }
-                  echo "<a href=\"".$host."?me=".$r[0]."\">I </a>";
+                  if($r[4]=='pre')
+                    {
+                      echo "\n   <span class=\"gamestatuspre\"><a href=\"".$host."?me=".$r[0]."\">p </a></span> ";
+
+                    }
+                  else if ($r[4]=='gameover')
+                    echo "\n   <span class=\"gamestatusover\"><a href=\"".$host."?me=".$r[0]."\">F </a></span> ";
+                  else
+                    {
+                      echo "\n   <span class=\"gamestatusplay\"><a href=\"".$host."?me=".$r[0]."\">P </a></span> ";
+                    }
+                  if($r[4] != 'gameover')
+                    {
+                      echo "</td><td>\n    ";
+                      if($r[3])
+                        {
+                          if($r[3]==$myid)
+                            echo "(it's <strong>your</strong> turn)\n";
+                          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 "".
+                          "<a href=\"$host?cancle=1&amp;me=".$r[0]."\">Cancel?</a>".
+                          " (clicking here is final and can't be restored)";
+
+                    }
                 }
-              echo "</p>\n";
+              echo "</td></tr>\n</table>\n";
               $names = DB_get_all_names();
               echo "<h4>Registered players:</h4>\n<p>\n";
               echo implode(", ",$names)."\n";
-              echo "</p>\n";
+              echo "</p>\n</div>";
             }
         }
        else