BUGFIX: wrong SQL query for min and max time of games
[e-DoKo.git] / index.php
index 77c2c2270b739ead0ee4e0d0be7932ddd4f8033c..8acb8715b28289a76a7daf1b048db88c7b85026e 100644 (file)
--- a/index.php
+++ b/index.php
@@ -29,18 +29,22 @@ if(myisset("logout"))
     session_unset();
     session_destroy();
     $_SESSION = array();
-    echo "you are now logged out!";
+    echo "<div class=\"message\"><span class=\"bigger\">You are now logged out!</span><br />\n".
+      "(<a href=\"$host\">This will take you back to the home-page</a>)</div>";
   }
 else if(myisset("new"))
   {
     if( isset($_SESSION["name"]) )
       {
        $names = DB_get_all_names();
+       echo "<div class=\"user\">\n";
        output_form_for_new_game($names);
+       echo "</div>\n";
+       display_user_menu();
       }
     else
       {
-       echo "Please log in.";
+       echo "<div class=\"message\">Please <a href=\"$host\">log in</a>.</div>";
       }
   }
 /*check if everything is ready to set up a new game */
@@ -1048,7 +1052,7 @@ else if(myisset("me"))
              $who         = DB_get_userid_by_email($email);
              DB_set_player_by_gameid($gameid,$who);
 
-             if($hash!=$me)
+             if($hash!=$me && DB_get_email_pref_by_hash($hash)!="emailaddict")
                {
                  /* email startplayer) */
                  $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
@@ -1401,13 +1405,13 @@ else if(myisset("me"))
                    "      <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
                }
 
-             echo "<div class=\"card".($pos-1)."\">";
+             echo "      <div class=\"card".($pos-1)."\">\n        ";
 
              /* display comments */
              display_card($card,$PREF["cardset"]);
              if($comment!="")
-               echo "  <span class=\"comment\"> ".$comment."</span>\n";
-             echo "</div></div></li>\n";
+               echo "\n        <span class=\"comment\"> ".$comment."</span>\n";
+             echo "      </div>\n";
 
              /*check if we still have cards left, else set status to gameover */
              if(sizeof(DB_get_hand($me))==0)
@@ -1441,7 +1445,8 @@ else if(myisset("me"))
                  $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."?me=".$next_hash."\n\n" ;
-                 mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
+                 if( DB_get_email_pref_by_uid($who)!="emailaddict" )
+                   mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
                }
              else /* send out final email */
                {
@@ -1478,8 +1483,13 @@ else if(myisset("me"))
                      else if($r[0] == "contra")
                        $contra = $r[1];
                    }
-                 /* save score in database */
-                 // get calls from re/contra
+
+                 /*
+                  * save score in database
+                  *
+                  */
+
+                 /* get calls from re/contra */
                  $call_re     = NULL;
                  $call_contra = NULL;
                  foreach($userids as $user)
@@ -1508,85 +1518,150 @@ else if(myisset("me"))
                            }
                        }
                    }
-                 // no call, check 120:120
-                 if($call_re == NULL && $call_contra==NULL)
-                   if($re==120)
-                     mysql_query("INSERT INTO Score".
-                                 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
 
-                 // no call or one call do scoring
-                 if($call_re != NULL && $call_contra!=NULL)
-                   {
-                     // two calls: not sure what to do yet
-                   }
+                 /* figure out who one */
+                 $winning_party = NULL;
+
+                 if($call_re == NULL && $call_contra==NULL)
+                   if($re>120)
+                     $winning_party="re";
+                   else
+                     $winning_party="contra";
                  else
                    {
-                     $offset = 0;
+                     if($call_re)
+                       {
+                         $offset = 120 - $call_re;
+                         if($call_re == 0)
+                           $offset--; /* since we use a > in the next equation */
+
+                         if($re > 120+$offset)
+                           $winning_party="re";
+                         else if ( $call_contra == NULL )
+                           $winning_party="contra";
+                       }
 
-                     if($call_re!=NULL)
-                       $offset = 120 - $call_re;
-                     else if($call_contra!=NULL)
-                       $offset = -1*(120 - $call_contra)-1;
+                     if($call_contra)
+                       {
+                         $offset = 120 - $call_contra;
+                         if($call_contra == 0)
+                           $offset--; /* since we use a > in the next equation */
+
+                         if($contra > 120+$offset)
+                           $winning_party="contra";
+                         else if ( $call_contra == NULL )
+                           $winning_party="re";
+                       }
+                   }
 
-                     if($call_re==0)
-                       $offset -= 1;
+                 /* one point for each call of the other party in case the other party didn't win
+                  * and one point each in case the party made more than points than one of the calls
+                  */
+                 if($winning_party!="contra" && $call_contra!=NULL)
+                   {
+                     for( $p=$call_contra;$p<=120; $p+=30 )
+                       {
+                         mysql_query("INSERT INTO Score".
+                                     " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
+                       }
 
-                     if($call_contra==0)
-                       $offset += 1;
+                     for( $p=$call_contra; $p<120; $p+=30)
+                       {
+                         if( $re >= $p )
+                           mysql_query("INSERT INTO Score".
+                                       " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'made$p')");
+                       }
+                   }
+                 if($winning_party!="re" and $call_re!=NULL)
+                   {
+                     for( $p=$call_re;$p<=120; $p+=30 )
+                       {
+                         mysql_query("INSERT INTO Score".
+                                     " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'against$p')");
+                       }
 
-                     if($re > 120 + $offset)
+                     for( $p=$call_re; $p<120; $p+=30)
                        {
-                         /* re won */
+                         if( $contra>=$p )
+                           mysql_query("INSERT INTO Score".
+                                       " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
+                       }
+                   }
 
-                         /* normal win */
-                         foreach(array(120,150,180,210,240) as $p)
-                           {
-                             $offset = 0;
-                             if($p==240)
-                               $offset = 1;
+                 /* point in case contra won */
+                 if($winning_party=="contra")
+                   {
+                     mysql_query("INSERT INTO Score".
+                                 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
+                   }
 
-                             if($re>$p-$offset)
-                               mysql_query("INSERT INTO Score".
-                                           " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'$p')");
-                           }
+                 /* one point each for winning and each 30 points + calls */
+                 if($winning_party=="re")
+                   {
+                     foreach(array(120,150,180,210,240) as $p)
+                       {
+                         $offset = 0;
+                         if($p==240 || $call_contra!=NULL)
+                           $offset = 1;
 
-                         /* re called something and won */
-                         if($call_re!=NULL)
-                           foreach(array(0,30,60,90,120) as $p)
-                             {
-                               if($call_re<$p+1)
-                                 mysql_query("INSERT INTO Score".
-                                             " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
-                             }
+                         if($re>$p-$offset)
+                           mysql_query("INSERT INTO Score".
+                                       " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
                        }
-                     else
+                     /* re called something and won */
+                     foreach(array(0,30,60,90,120) as $p)
                        {
-                         /* contra won */
-                         mysql_query("INSERT INTO Score".
-                                     " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
+                         if($call_re!=NULL && $call_re<$p+1)
+                           mysql_query("INSERT INTO Score".
+                                       " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
+                       }
+                   }
+                 else if( $winning_party=="contra")
+                   {
+                     foreach(array(120,150,180,210,240) as $p)
+                       {
+                         $offset = 0;
+                         if($p==240 || $call_re!=NULL)
+                           $offset = 1;
 
-                         /* normal win */
-                         foreach(array(120,150,180,210,240) as $p)
-                           {
-                             $offset = 0;
-                             if($p==240)
-                               $offset = 1;
+                         if($contra>$p-$offset)
+                           mysql_query("INSERT INTO Score".
+                                       " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
+                       }
+                     /* re called something and won */
+                     foreach(array(0,30,60,90,120) as $p)
+                       {
+                         if($call_contra!=NULL && $call_contra<$p+1)
+                           mysql_query("INSERT INTO Score".
+                                       " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
+                       }
+                   }
 
-                             if($re>$p-$offset)
-                               mysql_query("INSERT INTO Score".
-                                           " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'$p')");
-                           }
 
-                         /* re called something and won */
-                         if($call_re!=NULL)
-                           foreach(array(0,30,60,90,120) as $p)
-                             {
-                               if($call_contra<$p+1)
-                                 mysql_query("INSERT INTO Score".
-                                             " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
-                             }
-                       }
+                 /* add score points to email */
+                 $message .= "\n";
+                 $Tpoint = 0;
+                 $message .= " Points Re: \n";
+                 $queryresult = mysql_query("SELECT score FROM Score ".
+                                            "  WHERE game_id=$gameid AND party='re'".
+                                            " ");
+                 while($r = mysql_fetch_array($queryresult,MYSQL_NUM) )
+                   {
+                     $message .= "   ".$r[0]."\n";
+                     $Tpoint ++;
+                   }
+                 $message .= " Points Contra: \n";
+                 $queryresult = mysql_query("SELECT score FROM Score ".
+                                            "  WHERE game_id=$gameid AND party='contra'".
+                                            " ");
+                 while($r = mysql_fetch_array($queryresult,MYSQL_NUM) )
+                   {
+                     $message .= "   ".$r[0]."\n";
+                     $Tpoint --;
                    }
+                 $message .= " Total Points (from Re points of view): $Tpoint\n";
+                 $message .= "\n";
+
                  /* send out final email */
                  $all = array();
 
@@ -1603,7 +1678,8 @@ else if(myisset("me"))
                      $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
 
                      $link = "Use this link to have a look at game ".DB_format_gameid($gameid).": ".$host."?me=".$hash."\n\n" ;
-                     mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 2(2)",$link);
+                     if( DB_get_email_pref_by_uid($user) != "emailaddict" )
+                       mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 2(2)",$link);
                    }
                }
            }
@@ -1638,7 +1714,7 @@ else if(myisset("me"))
                                " GROUP BY User.fullname" );
          while( $r = mysql_fetch_array($result,MYSQL_NUM))
            echo "      <div class=\"card".($r[3]-1)."\">\n".
-                "        <span class=\"score\">".$r[2]."<br /> ".$r[1]."</span>\n".
+                "        <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
                 "      </div>\n";
 
          echo "    </div>\n  </li>\n";  /* end div trick, end li trick */
@@ -1711,7 +1787,7 @@ else if(myisset("me"))
       /* check if game is over, display results */
       if(DB_get_game_status_by_gameid($gameid)=='play')
        {
-         echo "the game is over for you.. other people still need to play though";
+         echo "The game is over for you.. other people still need to play though";
        }
       else
        {
@@ -1726,8 +1802,26 @@ else if(myisset("me"))
          echo "<div class=\"total\"> Totals:<br />\n";
          while( $r = mysql_fetch_array($result,MYSQL_NUM))
            echo "  ".$r[0]." ".$r[1]."<br />\n";
+
+         echo "<div class=\"re\">\n Points Re: <br />\n";
+         $queryresult = mysql_query("SELECT score FROM Score ".
+                                    "  WHERE game_id=$gameid AND party='re'".
+                                    " ");
+         while($r = mysql_fetch_array($queryresult,MYSQL_NUM) )
+           echo "   ".$r[0]."<br />\n";
+         echo "</div>\n";
+
+         echo "<div class=\"contra\">\n Points Contra: <br />\n";
+         $queryresult = mysql_query("SELECT score FROM Score ".
+                                    "  WHERE game_id=$gameid AND party='contra'".
+                                    " ");
+         while($r = mysql_fetch_array($queryresult,MYSQL_NUM) )
+           echo "   ".$r[0]."<br />\n";
          echo "</div>\n";
 
+         echo "</div>\n";
+
+
        }
       break;
     default:
@@ -1910,6 +2004,18 @@ else if( myisset("email","password") || isset($_SESSION["name"]) )
                                           DB_quote_smart($setpref).")");
                   echo "Ok, changed you preferences for the cards.\n";
                   break;
+                case "emailaddict":
+                case "emailnonaddict":
+                  $result = mysql_query("SELECT * from User_Prefs".
+                                        " WHERE user_id='$myid' AND pref_key='email'" );
+                  if( mysql_fetch_array($result,MYSQL_NUM))
+                    $result = mysql_query("UPDATE User_Prefs SET value=".DB_quote_smart($setpref).
+                                          " WHERE user_id='$myid' AND pref_key='email'" );
+                  else
+                    $result = mysql_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','email',".
+                                          DB_quote_smart($setpref).")");
+                  echo "Ok, changed you preferences for sending out emails.\n";
+                  break;
                 }
             }
           else if(myisset("passwd"))
@@ -2045,7 +2151,7 @@ else if( myisset("email","password") || isset($_SESSION["name"]) )
         }
        else
         {
-          echo "Sorry email and password don't match. Please <a href=\"$host\">try again</a>. <br />";
+          echo "<div class=\"message\">Sorry email and password don't match. Please <a href=\"$host\">try again</a>. </div>";
         }
      };
      output_footer();