BUGFIX: typo in scoring algorithm
[e-DoKo.git] / include / game.php
index 0e16292abd5fb4459152e7b507531260ff21d4f6..cfb2b55db0b004ea1085c0905a6d7a50a530b392 100644 (file)
@@ -1,10 +1,18 @@
 <?php
-/* make sure that we are not called from outside the scripts, 
+/* make sure that we are not called from outside the scripts,
  * use a variable defined in config.php to check this
  */
 if(!isset($HOST))
   exit;
 
+if(!myisset("me"))
+  {
+    echo "Hmm, you really shouldn't mess with the urls.<br />\n";
+    output_footer();
+    DB_close();
+    exit();
+  }
+
 $me = $_REQUEST["me"];
 
 /* test for valid ID */
@@ -17,7 +25,7 @@ if(!$myid)
     DB_close();
     exit();
   }
-    
+
 /* user might get here by clicking on the link in an email, so session might not be set */
 if(isset($_SESSION["name"]))
   output_status($_SESSION["name"]);
@@ -70,7 +78,7 @@ for($i=1;$i<5;$i++)
   };
 
 /* put everyting in a form */
-echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
+echo "<form action=\"index.php?action=game&me=$me\" method=\"post\">\n";
 
 /* output game */
 
@@ -86,7 +94,7 @@ if($session)
        if($hash == $me)
          echo "$i \n";
        else
-         echo "<a href=\"".$INDEX."?me=".$hash."\">$i</a> \n";
+         echo "<a href=\"".$INDEX."?action=game&me=".$hash."\">$i</a> \n";
        $i++;
       }
     echo "</div>\n";
@@ -174,7 +182,7 @@ switch($mystatus)
                     DB_set_player_by_gameid($gameid,$who);
 
                     $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
-                    "Use this link to go the game: ".$HOST.$INDEX."?me=".$hash."\n\n" ;
+                    "Use this link to go the game: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
                     mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
                    */
                  }
@@ -208,7 +216,7 @@ switch($mystatus)
         * unless a user tries to cheat ;)
         * can also happen if user reloads the page!
         */
-       echo "<p class=\"message\"> You need to answer the <a href=\"$INDEX?me=$me&in=yes\">questions</a>.</p>";
+       echo "<p class=\"message\"> You need to answer the <a href=\"$INDEX?action=game&me=$me&in=yes\">questions</a>.</p>";
        DB_set_hand_status_by_hash($me,'init');
       }
     else
@@ -223,7 +231,7 @@ switch($mystatus)
        if($Nvorbehalt>1)
          {
            echo "<p class=\"message\"> You selected more than one vorbehalt, please go back ".
-             "and answer the <a href=\"$INDEX?me=$me&in=yes\">question</a> again.</p>";
+             "and answer the <a href=\"$INDEX?action=game&me=$me&in=yes\">question</a> again.</p>";
            DB_set_hand_status_by_hash($me,'init');
          }
        else
@@ -274,7 +282,7 @@ switch($mystatus)
                DB_set_sickness_by_hash($me,"nines");
              }
 
-           echo " Ok, done with checking, please go to the <a href=\"$INDEX?me=$me\">next step of the setup</a>.</p>";
+           echo " Ok, done with checking, please go to the <a href=\"$INDEX?action=game&me=$me\">next step of the setup</a>.</p>";
 
            /* move on to the next stage*/
            DB_set_hand_status_by_hash($me,'poverty');
@@ -304,7 +312,7 @@ switch($mystatus)
                      {
                        $message = "Everyone finish the questionary in game ".DB_format_gameid($gameid).", ".
                          "please visit this link now to continue: \n".
-                         " ".$HOST.$INDEX."?me=".$userhash."\n\n" ;
+                         " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
                        mymail($To,$EmailName." finished setup in game ".DB_format_gameid($gameid),$message);
                      }
                  };
@@ -513,7 +521,7 @@ switch($mystatus)
              DB_set_party_by_hash($me,"contra");
 
            echo "Whoever will make the first trick will be on the re team. <br />\n";
-           echo " Ok, the game can start now, please finish <a href=\"$INDEX?me=$me\">the setup</a>.<br />";
+           echo " Ok, the game can start now, please finish <a href=\"$INDEX?action=game&me=$me\">the setup</a>.<br />";
            DB_set_hand_status_by_hash($me,'play');
            break;
 
@@ -576,7 +584,7 @@ switch($mystatus)
                    DB_set_player_by_gameid($gameid,$userid);
 
                    $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
-                     " ".$HOST.$INDEX."?me=".$userhash."\n\n" ;
+                     " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
                    mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
                  }
 
@@ -658,7 +666,7 @@ switch($mystatus)
 
                            $message = "Someone has poverty, it's your turn to decide, ".
                              "if you want to take the trump. Please visit:".
-                             " ".$HOST.$INDEX."?me=".$userhash."\n\n" ;
+                             " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
                            mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
                          }
                      }
@@ -732,10 +740,10 @@ switch($mystatus)
                        /* count trump */
                        if($nrtrump<4)
                          echo "Player $name has $nrtrump trump. Do you want to take them?".
-                           "<a href=\"index.php?me=$me&amp;trump=$user\">yes</a> <br />\n";
+                           "<a href=\"index.php?action=game&me=$me&amp;trump=$user\">yes</a> <br />\n";
                      }
                  }
-               echo "<a href=\"index.php?me=$me&amp;trump=no\">No,way I take those trump...</a> <br />\n";
+               echo "<a href=\"index.php?action=game&me=$me&amp;trump=no\">No,way I take those trump...</a> <br />\n";
                echo "</div><div>\n";
 
                echo "Your cards are: <br />\n";
@@ -803,11 +811,11 @@ switch($mystatus)
              {
                /* email startplayer) */
                $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
-                 "Use this link to play a card: ".$HOST.$INDEX."?me=".$hash."\n\n" ;
+                 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
                mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
              }
            else
-             echo " Please, <a href=\"$INDEX?me=$me\">start</a> the game.<br />";
+             echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />";
          }
        else
          echo "\n <br />";
@@ -1048,11 +1056,11 @@ switch($mystatus)
 
            $playid = DB_play_card($trickid,$handcardid,$sequence);
 
-           /* check special output for schweinchen in case: 
+           /* check special output for schweinchen in case:
             * schweinchen is in the rules, a fox has been played and the gametype is correct
             */
-           if( $GAME["schweinchen"] && 
-               ($card == 19 || $card == 20) && 
+           if( $GAME["schweinchen"] &&
+               ($card == 19 || $card == 20) &&
                ($gametype == "normal" || $gametype == "silent"|| $gametype=="trump"))
              {
                $GAME["schweinchen"]++; // count how many have been played including this one
@@ -1107,7 +1115,7 @@ switch($mystatus)
                $winner = get_winner($play,$gametype); /* returns the position */
 
                /* check if someone caught a fox */
-               /* first check if we should account for solos at all, 
+               /* first check if we should account for solos at all,
                 * since it doesn't make sense in some games
                 */
                $ok = 0; /* fox shouldn't be counted */
@@ -1119,7 +1127,7 @@ switch($mystatus)
                  }
                else
                  $ok = 1; /* for all other games (not solos) foxes are ok too */
-                 
+
                if($ok==1)
                  foreach($play as $played)
                    {
@@ -1138,7 +1146,7 @@ switch($mystatus)
                                          " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
                          }
                    }
-                 
+
                /* check for karlchen (jack of clubs in the last trick)*/
                /* same as for foxes, karlchen doesn't always make sense
                 * check what kind of game it is and set karlchen accordingly */
@@ -1151,7 +1159,7 @@ switch($mystatus)
                    if($solo == "trumpless" || $solo == "jack" || $solo == "queen" )
                      $ok = 0; /* no Karlchen in these solos */
                  }
-                 
+
                if($ok)
                  foreach($play as $played)
                    if ( $played['card']==11 || $played['card']==12 )
@@ -1260,7 +1268,7 @@ switch($mystatus)
 
                $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.$INDEX."?me=".$next_hash."\n\n" ;
+                 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$next_hash."\n\n" ;
                if( DB_get_email_pref_by_uid($who)!="emailaddict" )
                  mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
              }
@@ -1339,12 +1347,19 @@ switch($mystatus)
                $winning_party = NULL;
 
                if($call_re == NULL && $call_contra==NULL)
-                 if($re>120)
-                   $winning_party="re";
-                 else
-                   $winning_party="contra";
+                 {
+                   /* nobody made a call, so it's easy to figure out who won */
+                   if($re>120)
+                     $winning_party="re";
+                   else
+                     $winning_party="contra";
+                 }
                else
                  {
+                   /* if one party makes a call, they only win, iff they make enough points
+                    * if only one party made a call, the other one wins,
+                    * if the first one didn't make it
+                    */
                    if($call_re)
                      {
                        $offset = 120 - $call_re;
@@ -1353,7 +1368,7 @@ switch($mystatus)
 
                        if($re > 120+$offset)
                          $winning_party="re";
-                       else if ( $call_contra == NULL )
+                       else if ($call_contra == NULL )
                          $winning_party="contra";
                      }
 
@@ -1365,7 +1380,7 @@ switch($mystatus)
 
                        if($contra > 120+$offset)
                          $winning_party="contra";
-                       else if ( $call_contra == NULL )
+                       else if ($call_re == NULL )
                          $winning_party="re";
                      }
                  }
@@ -1506,7 +1521,7 @@ switch($mystatus)
 
                  $score = implode("\n",$score);
                  $score = $header.$score;
-                 
+
                  $message .= "Score Table:\n";
                  $message .= $score;
 
@@ -1526,7 +1541,7 @@ switch($mystatus)
                      $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.$INDEX."?me=".$hash."\n\n" ;
+                       $HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
                      if( DB_get_email_pref_by_uid($user) != "emailaddict" )
                        mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 2(2)",$link);
                    }
@@ -1578,7 +1593,7 @@ switch($mystatus)
        echo "$note <hr \>\n";
       echo "Insert note:<input name=\"note\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
       echo "</div> \n";
-      
+
       $mycards = DB_get_hand($me);
       $mycards = mysort($mycards,$gametype);
       echo "<div class=\"mycards\">\n";
@@ -1719,12 +1734,19 @@ switch($mystatus)
        echo "<hr />";
       }
 
-    echo "<input type=\"submit\" value=\"submit\" />\n";
+    echo "<input type=\"submit\" value=\"submit\" />\n<hr />\n";
+
+    $session = DB_get_session_by_gameid($gameid);
+    $score   = generate_score_table($session);
 
+    echo $score;
+
+    echo "</div>\n";
+
+    echo "</form>\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".
@@ -1748,15 +1770,7 @@ switch($mystatus)
          }
       }
 
-    $session = DB_get_session_by_gameid($gameid);
-    $score = generate_score_table($session);
-
-    //  if(size_of($score)>30)
-      echo $score;
 
-    echo "</div>\n";
-
-    echo "</form>\n";
     output_footer();
     DB_close();
     exit();