CLEANUP: some small code cleanup
[e-DoKo.git] / index.php
index 7d2d4abeb3fbe39c89c1197eb813b028a362e305..74b4fc129824e849c79db4e92e262fa58b902144 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)");
@@ -197,7 +214,8 @@ else if(myisset("cancle","me"))
     if(time()-strtotime($r[0]) > 60*60*24*30) /* = 1 month */
       {
        $message = "Hello, \n\n".
-         "Game ".DB_format_gameid($gameid)." has been cancled since nothing happend for a while and $myname requested it.\n";
+         "Game ".DB_format_gameid($gameid).
+         " has been cancled since nothing happend for a while and $myname requested it.\n";
        
        $userids = DB_get_all_userid_by_gameid($gameid);
        foreach($userids as $user)
@@ -209,7 +227,8 @@ else if(myisset("cancle","me"))
        /* delete everything from the dB */
        DB_cancel_game($me);
        
-       echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been cancled.<br /><br /></p>";
+       echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid).
+         " has been cancled.<br /><br /></p>";
       }
     else
       echo "<p>You need to wait longer before you can cancle a game...</p>\n";
@@ -260,7 +279,8 @@ else if(myisset("remind","me"))
            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>";
+           echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid).
+             ": an email has been sent out.<br /><br /></p>";
          }
       }
     else
@@ -349,9 +369,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";
@@ -389,7 +409,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
@@ -439,94 +459,109 @@ 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 />";
-      
-         /* check if this sickness needs to be handled first */
-         $gametype    = DB_get_gametype_by_gameid($gameid);
-         $startplayer = DB_get_startplayer_by_gameid($gameid);
-         
-         if( $_REQUEST["solo"]!="No")
+         /* check if someone selected more than one vorbehalt */
+         $Nvorbehalt = 0;
+         if($_REQUEST["solo"]!="No")       $Nvorbehalt++;
+         if($_REQUEST["wedding"] == "yes") $Nvorbehalt++;
+         if($_REQUEST["poverty"] == "yes") $Nvorbehalt++;
+         if($_REQUEST["nines"] == "yes")   $Nvorbehalt++;
+
+         if($Nvorbehalt>1)
+           {
+             echo "<p class=\"message\"> You selected more than one vorbehalt, please go back ".
+               "and answer the <a href=\"$host?me=$me&in=yes\">question</a> again.</p>";
+             DB_set_hand_status_by_hash($me,'init');
+           }
+         else
            {
-             /* user wants to play a solo */
+             echo "<p class=\"message\">Processing what you selected in the last step...";
 
-             /* store the info in the user's hand info */
-             DB_set_solo_by_hash($me,$_REQUEST["solo"]);
-             DB_set_sickness_by_hash($me,"solo");
+             /* check if this sickness needs to be handled first */
+             $gametype    = DB_get_gametype_by_gameid($gameid);
+             $startplayer = DB_get_startplayer_by_gameid($gameid);
 
-             echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
-             
-             if($gametype == "solo" && $startplayer<$mypos)
-               {}/* do nothing, since someone else already is playing solo */
-             else
+             if( $_REQUEST["solo"]!="No")
                {
-                 /* this solo comes first 
-                  * store info in game table
-                  */
-                 DB_set_gametype_by_gameid($gameid,"solo");
-                 DB_set_startplayer_by_gameid($gameid,$mypos);
-                 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
-               };
-           }
-         else if($_REQUEST["wedding"] == "yes")
-           {
-             /* TODO: add silent solo somewhere*/
-             echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
-             DB_set_sickness_by_hash($me,"wedding");
-           }
-         else if($_REQUEST["poverty"] == "yes")
-           {
-             echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
-             DB_set_sickness_by_hash($me,"poverty");
-           }
-         else if($_REQUEST["nines"] == "yes")
-           {
-             echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
-               " is playing solo, this game will be canceled.<br />\n";
-             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 />";
-         
-         /* move on to the next stage*/
-         DB_set_hand_status_by_hash($me,'poverty');
-         
-         /* check if everyone has reached this stage, send out email */
-         $userids = DB_get_all_userid_by_gameid($gameid);
-         $ok = 1;
-         foreach($userids as $user)
-           {
-             $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
-             if($userstat!='poverty' && $userstat!='play')
+                 /* user wants to play a solo */
+
+                 /* store the info in the user's hand info */
+                 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
+                 DB_set_sickness_by_hash($me,"solo");
+
+                 echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
+
+                 if($gametype == "solo" && $startplayer<$mypos)
+                   {}/* do nothing, since someone else already is playing solo */
+                 else
+                   {
+                     /* this solo comes first
+                      * store info in game table
+                      */
+                     DB_set_gametype_by_gameid($gameid,"solo");
+                     DB_set_startplayer_by_gameid($gameid,$mypos);
+                     DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
+                   };
+               }
+             else if($_REQUEST["wedding"] == "yes")
                {
-                 $ok = 0;
-                 DB_set_player_by_gameid($gameid,$user);
+                 /* TODO: add silent solo somewhere*/
+                 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
+                 DB_set_sickness_by_hash($me,"wedding");
                }
-           };
-         if($ok)
-           {
-             /* reset player = everyone has to do something now */
-             DB_set_player_by_gameid($gameid,NULL);
-             
+             else if($_REQUEST["poverty"] == "yes")
+               {
+                 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
+                 DB_set_sickness_by_hash($me,"poverty");
+               }
+             else if($_REQUEST["nines"] == "yes")
+               {
+                 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
+                   " is playing solo, this game will be canceled.<br />\n";
+                 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>.</p>";
+
+             /* move on to the next stage*/
+             DB_set_hand_status_by_hash($me,'poverty');
+
+             /* check if everyone has reached this stage, send out email */
+             $userids = DB_get_all_userid_by_gameid($gameid);
+             $ok = 1;
              foreach($userids as $user)
                {
-                 $To       = DB_get_email_by_userid($user);
-                 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
-                 if($userhash != $me)
+                 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
+                 if($userstat!='poverty' && $userstat!='play')
                    {
-                     $message = "Everyone finish the questionary in game ".DB_format_gameid($gameid).", ".
-                       "please visit this link now to continue: \n".
-                       " ".$host."?me=".$userhash."\n\n" ;
-                     mymail($To,$EmailName." finished setup in game ".DB_format_gameid($gameid),$message);
+                     $ok = 0;
+                     DB_set_player_by_gameid($gameid,$user);
                    }
                };
+             if($ok)
+               {
+                 /* reset player = everyone has to do something now */
+                 DB_set_player_by_gameid($gameid,NULL);
+
+                 foreach($userids as $user)
+                   {
+                     $To       = DB_get_email_by_userid($user);
+                     $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".
+                           " ".$host."?me=".$userhash."\n\n" ;
+                         mymail($To,$EmailName." finished setup in game ".DB_format_gameid($gameid),$message);
+                       }
+                   };
+               };
            };
        };
-
       break;
 
     case 'poverty':
@@ -535,7 +570,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);
@@ -809,11 +845,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))
                {
@@ -914,7 +950,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);
@@ -926,12 +962,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);
@@ -949,7 +985,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);
@@ -1025,6 +1061,7 @@ else if(myisset("me"))
          else
            echo "\n <br />";    
        }
+      echo "</div>\n";
       break;
     case 'play':
     case 'gameover': 
@@ -1056,8 +1093,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 */
        }
@@ -1083,7 +1120,8 @@ else if(myisset("me"))
                            "       Hand.position as position,".
                            "       Play.sequence as sequence, ".
                            "       Trick.id, ".
-                           "       GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>') SEPARATOR '\n' ), ".
+                           "       GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
+                           "                    SEPARATOR '\n' ), ".
                            "       Play.create_date, ".
                            "       Hand.user_id ".
                            "FROM Trick ".
@@ -1120,7 +1158,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 */
@@ -1230,9 +1268,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" )
@@ -1758,9 +1796,9 @@ else if( myisset("email","password") || isset($_SESSION["name"]) )
                 output_status($_SESSION["name"]);
               
               /* display links to settings */
-              output_user_settings($email,$password);
+              output_user_settings();
               
-              echo "<div style=\"position:absolute; font-size:smaller; top:0; left:0; \">last login: ".date("r",$unixtime)."</div>";
+              echo "<div class=\"lastlogin\">last login: ".date("r",$unixtime)."</div>";
               
               DB_update_user_timestamp($myid);