BUGFIX: finished scoring algorithm
authorArun Persaud <arun@nubati.net>
Sun, 2 Dec 2007 16:14:52 +0000 (17:14 +0100)
committerArun Persaud <arun@nubati.net>
Sun, 2 Dec 2007 16:14:52 +0000 (17:14 +0100)
now all games are scored. Rules can be looked up in the wiki.

create_database.sql
index.php

index 4408426d6ee6376b005ddf756665264a786b3562..d7b7e2091d769e2807c9542de252e30b9ee6a040 100644 (file)
@@ -209,9 +209,9 @@ CREATE TABLE `Score` (
   `party` enum('re','contra') default NULL ,
   `winner_id` int(11) default NULL ,
   `looser_id` int(11) default NULL ,
   `party` enum('re','contra') default NULL ,
   `winner_id` int(11) default NULL ,
   `looser_id` int(11) default NULL ,
-  `score` enum('120', 'call120','against120','90','call90','against90',
-          '60','call60','against60','30','call30','against30',
-          '0','call0','against0',
+  `score` enum('120', 'call120','against120','90','call90','against90','made90'
+          '60','call60','against60','made60','30','call30','against30','made30',
+          '0','call0','against0','made0',
           'fox','karlchen','dulle','doko','againstqueens') default NULL,
   UNIQUE KEY `id` (`id`)
 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
           'fox','karlchen','dulle','doko','againstqueens') default NULL,
   UNIQUE KEY `id` (`id`)
 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
index bf96982f7a61e653a13b43df8631d604d086fb9c..1a26347df718ca35155013e20e48b3914effa758 100644 (file)
--- a/index.php
+++ b/index.php
@@ -1479,8 +1479,13 @@ else if(myisset("me"))
                      else if($r[0] == "contra")
                        $contra = $r[1];
                    }
                      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)
                  $call_re     = NULL;
                  $call_contra = NULL;
                  foreach($userids as $user)
@@ -1509,85 +1514,126 @@ 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
                    {
                  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,'".(240-$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')");
-
-                         /* normal win */
-                         foreach(array(120,150,180,210,240) as $p)
-                           {
-                             $offset = 0;
-                             if($p==240)
-                               $offset = 1;
-
-                             if($re>$p-$offset)
-                               mysql_query("INSERT INTO Score".
-                                           " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
-                           }
+                         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;
 
 
-                         /* contra called something and won */
-                         if($call_contra!=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')");
-                             }
+                         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')");
                        }
                    }
                        }
                    }
+
+
                  /* send out final email */
                  $all = array();
 
                  /* send out final email */
                  $all = array();