+ {
+ $message .= " ".$r[0]." ".$r[1]."\n";
+ if($r[0] == "re")
+ $re = $r[1];
+ else if($r[0] == "contra")
+ $contra = $r[1];
+ }
+
+ /*
+ * save score in database
+ *
+ */
+
+ /* get calls from re/contra */
+ $call_re = NULL;
+ $call_contra = NULL;
+ foreach($userids as $user)
+ {
+ $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
+ $call = DB_get_call_by_hash($hash);
+ $party = DB_get_party_by_hash($hash);
+
+ if($call!=NULL)
+ {
+ $call = (int) $call;
+
+ if($party=="re")
+ {
+ if($call_re==NULL)
+ $call_re = $call;
+ else if( $call < $call_re)
+ $call_re = $call;
+ }
+ else if($party=="contra")
+ {
+ if($call_contra==NULL)
+ $call_contra = $call;
+ else if( $call < $call_re)
+ $call_contra = $call;
+ }
+ }
+ }
+
+ /* 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
+ {
+ 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_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";
+ }
+ }
+
+ /* 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')");
+ }
+
+ 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')");
+ }
+
+ for( $p=$call_re; $p<120; $p+=30)
+ {
+ if( $contra>=$p )
+ mysql_query("INSERT INTO Score".
+ " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
+ }
+ }
+
+ /* point in case contra won */
+ if($winning_party=="contra")
+ {
+ mysql_query("INSERT INTO Score".
+ " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
+ }
+
+ /* 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;
+
+ if($re>$p-$offset)
+ mysql_query("INSERT INTO Score".
+ " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
+ }
+ /* re called something and won */
+ foreach(array(0,30,60,90,120) as $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;
+
+ 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')");
+ }
+ }
+
+
+ /* 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";
+