NEW FEATURE: new settings page, user can now change timezone
[e-DoKo.git] / include / game.php
index cfb2b55db0b004ea1085c0905a6d7a50a530b392..35b94cd70c1cc6862ad285b949fb98fe7525ddb3 100644 (file)
@@ -5,6 +5,7 @@
 if(!isset($HOST))
   exit;
 
+/* calling game.php only makes sense when we give it a hash for a game */
 if(!myisset("me"))
   {
     echo "Hmm, you really shouldn't mess with the urls.<br />\n";
@@ -12,10 +13,9 @@ if(!myisset("me"))
     DB_close();
     exit();
   }
-
 $me = $_REQUEST["me"];
 
-/* test for valid ID */
+/* Ok, got a hash, but is it valid? */
 $myid = DB_get_userid('hash',$me);
 if(!$myid)
   {
@@ -30,7 +30,7 @@ if(!$myid)
 if(isset($_SESSION["name"]))
   output_status($_SESSION["name"]);
 
-/* the user had done something, update the timestamp */
+/* the user has done something, update the timestamp */
 DB_update_user_timestamp($myid);
 
 /* get some information from the DB */
@@ -41,18 +41,11 @@ $mypos    = DB_get_pos_by_hash($me);
 $myhand   = DB_get_handid('hash',$me);
 $session  = DB_get_session_by_gameid($gameid);
 
-/* get prefs and save them */
-DB_get_PREF($myid);
+/* get prefs and save them in a variable*/
+$PREF = DB_get_PREF($myid);
 
 /* get rule set for this game */
-$result = mysql_query("SELECT * FROM Rulesets".
-                     " LEFT JOIN Game ON Game.ruleset=Rulesets.id ".
-                     " WHERE Game.id='$gameid'" );
-$r      = mysql_fetch_array($result,MYSQL_NUM);
-
-$RULES["dullen"]      = $r[2];
-$RULES["schweinchen"] = $r[3];
-$RULES["call"]        = $r[4];
+$RULES = DB_get_RULES($gameid);
 
 /* get some infos about the game */
 $gametype   = DB_get_gametype_by_gameid($gameid);
@@ -190,59 +183,52 @@ switch($mystatus)
          }
       }
   case 'init':
-
+    /* here we ask the player if he is sick */
     $mycards = DB_get_hand($me);
     sort($mycards);
-
-    output_check_for_sickness($me,$mycards);
-
-    echo "<p class=\"mycards\">Your cards are: <br />\n";
-    foreach($mycards as $card)
-      display_card($card,$PREF["cardset"]);
-    echo "</p>\n";
-
-    /* move on to the next stage*/
-    DB_set_hand_status_by_hash($me,'check');
-    break;
-
-  case 'check':
-    /* ok, user is in the game, saw his cards and selected his vorbehalt
-     * so first we check what he selected
-     */
+    
     if(!myisset("solo","wedding","poverty","nines") )
       {
-       /* all these variables have a pre-selected default,
-        * so we should never get here,
-        * 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?action=game&me=$me&in=yes\">questions</a>.</p>";
-       DB_set_hand_status_by_hash($me,'init');
+       output_check_for_sickness($me,$mycards);
+
+       echo "<p class=\"mycards\">Your cards are: <br />\n";
+       foreach($mycards as $card)
+         display_card($card,$PREF["cardset"]);
+       echo "</p>\n";
+
+       break;
       }
     else
       {
-       /* 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)
+       /* check if someone selected more than one sickness */
+       $Nsickness = 0;
+       if($_REQUEST["solo"]!="No")       $Nsickness++;
+       if($_REQUEST["wedding"] == "yes") $Nsickness++;
+       if($_REQUEST["poverty"] == "yes") $Nsickness++;
+       if($_REQUEST["nines"] == "yes")   $Nsickness++;
+
+       if($Nsickness>1)
          {
-           echo "<p class=\"message\"> You selected more than one vorbehalt, please go back ".
+           echo "<p class=\"message\"> You selected more than one sickness, please go back ".
              "and answer the <a href=\"$INDEX?action=game&me=$me&in=yes\">question</a> again.</p>";
-           DB_set_hand_status_by_hash($me,'init');
+
+           echo "<p class=\"mycards\">Your cards are: <br />\n";
+           foreach($mycards as $card)
+             display_card($card,$PREF["cardset"]);
+           echo "</p>\n";
+
+           break;
          }
        else
          {
+           /* everything is ok, save what user said and proceed */
            echo "<p class=\"message\">Processing what you selected in the last step...";
 
            /* check if this sickness needs to be handled first */
            $gametype    = DB_get_gametype_by_gameid($gameid);
-           $startplayer = DB_get_startplayer_by_gameid($gameid);
+           $startplayer = DB_get_startplayer_by_gameid($gameid); /* need this to check which solo goes first */
 
-           if( $_REQUEST["solo"]!="No")
+           if( $_REQUEST["solo"]!="No" )
              {
                /* user wants to play a solo */
 
@@ -281,45 +267,18 @@ switch($mystatus)
                  " 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=\"$INDEX?action=game&me=$me\">next step of the setup</a>.</p>";
-
+           
+           echo "</p>\n";
+           
            /* 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')
-                 {
-                   $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('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.$INDEX."?action=game&me=".$userhash."\n\n" ;
-                       mymail($To,$EmailName." finished setup in game ".DB_format_gameid($gameid),$message);
-                     }
-                 };
-             };
          };
       };
-    break;
+
+  case 'check':
+    /* hmm, by reorganizing things a bit, this stage is empty at the moment */
+    /* move on to the next stage, but user shouldn't get here anymore anyway */
+    DB_set_hand_status_by_hash($me,'poverty');
 
   case 'poverty':
     /* here we need to check if there is a solo or some other form of sickness.
@@ -600,7 +559,7 @@ switch($mystatus)
                /* get hand id for user $trump */
                $userhand = DB_get_handid('gameid-userid',$gameid,$trump);
                /* copy trump from player A to B */
-               $result = mysql_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
+               $result = DB_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><div class=\"poverty\">\n";
@@ -618,13 +577,12 @@ switch($mystatus)
                /* if exchange is set to a value>0, exchange that card back to user $trump */
                if($exchange >0)
                  {
-                   $result = mysql_query("UPDATE Hand_Card SET hand_id='$userhand'".
-                                         " WHERE hand_id='$myhand' AND card_id='$exchange'" );
+                   $result = DB_query("UPDATE Hand_Card SET hand_id='$userhand'".
+                                      " WHERE hand_id='$myhand' AND card_id='$exchange'" );
                  };
 
                /* if number of cards == 12, set status to play for both users */
-               $result = mysql_query("SELECT COUNT(*) FROM Hand_Card  WHERE hand_id='$myhand'" );
-               $r      = mysql_fetch_array($result,MYSQL_NUM);
+               $r = DB_query_array("SELECT COUNT(*) FROM Hand_Card  WHERE hand_id='$myhand'" );
                if(!$r)
                  {
                    myerror("error in poverty");
@@ -859,8 +817,7 @@ switch($mystatus)
       }
 
     /* get time from the last action of the game */
-    $result  = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
-    $r       = mysql_fetch_array($result,MYSQL_NUM);
+    $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
     $gameend = time() - strtotime($r[0]);
 
     /* handel comments in case player didn't play a card, allow comments a week after the end of the game */
@@ -885,23 +842,23 @@ switch($mystatus)
        };
 
     /* get everything relevant to display the tricks */
-    $result = mysql_query("SELECT Hand_Card.card_id as card,".
-                         "       Hand.position as position,".
-                         "       Play.sequence as sequence, ".
-                         "       Trick.id, ".
-                         "       GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
-                         "                    SEPARATOR '\n' ), ".
-                         "       Play.create_date, ".
-                         "       Hand.user_id ".
-                         "FROM Trick ".
-                         "LEFT JOIN Play ON Trick.id=Play.trick_id ".
-                         "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
-                         "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
-                         "LEFT JOIN Comment ON Play.id=Comment.play_id ".
-                         "LEFT JOIN User On User.id=Comment.user_id ".
-                         "WHERE Trick.game_id='".$gameid."' ".
-                         "GROUP BY Trick.id, sequence ".
-                         "ORDER BY Trick.id, sequence  ASC");
+    $result = DB_query("SELECT Hand_Card.card_id as card,".
+                      "       Hand.position as position,".
+                      "       Play.sequence as sequence, ".
+                      "       Trick.id, ".
+                      "       GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
+                      "                    SEPARATOR '\n' ), ".
+                      "       Play.create_date, ".
+                      "       Hand.user_id ".
+                      "FROM Trick ".
+                      "LEFT JOIN Play ON Trick.id=Play.trick_id ".
+                      "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
+                      "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
+                      "LEFT JOIN Comment ON Play.id=Comment.play_id ".
+                      "LEFT JOIN User On User.id=Comment.user_id ".
+                      "WHERE Trick.game_id='".$gameid."' ".
+                      "GROUP BY Trick.id, sequence ".
+                      "ORDER BY Trick.id, sequence  ASC");
     $trickNR   = 1;
     $lasttrick = DB_get_max_trickid($gameid);
 
@@ -938,7 +895,7 @@ switch($mystatus)
       }
 
     /* output tricks */
-    while($r = mysql_fetch_array($result,MYSQL_NUM))
+    while($r = DB_fetch_array($result))
       {
        $pos     = $r[1];
        $seq     = $r[2];
@@ -1022,9 +979,8 @@ switch($mystatus)
 
        /* check if we have card and that we haven't played it yet*/
        /* set played in hand_card to true where hand_id and card_id*/
-       $result = mysql_query("SELECT id FROM Hand_Card WHERE played='false' and ".
+       $r = DB_query_array("SELECT id FROM Hand_Card WHERE played='false' and ".
                              "hand_id='$handid' AND card_id=".DB_quote_smart($card));
-       $r = mysql_fetch_array($result,MYSQL_NUM);
        $handcardid = $r[0];
 
        if($handcardid) /* everything ok, play card  */
@@ -1034,19 +990,19 @@ switch($mystatus)
 
            /* check if a call was made, must do this before we set the card status to played */
            if(myisset("call")  && $_REQUEST["call"]  == "120" && can_call(120,$me))
-             $result = mysql_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
+             $result = DB_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
            if(myisset("call")  && $_REQUEST["call"]  == "90" && can_call(90,$me))
-             $result = mysql_query("UPDATE Hand SET point_call='90'  WHERE hash='$me' ");
+             $result = DB_query("UPDATE Hand SET point_call='90'  WHERE hash='$me' ");
            if(myisset("call")  && $_REQUEST["call"]  == "60" && can_call(60,$me))
-             $result = mysql_query("UPDATE Hand SET point_call='60'  WHERE hash='$me' ");
+             $result = DB_query("UPDATE Hand SET point_call='60'  WHERE hash='$me' ");
            if(myisset("call")  && $_REQUEST["call"]  == "30" && can_call(30,$me))
-             $result = mysql_query("UPDATE Hand SET point_call='30'  WHERE hash='$me' ");
+             $result = DB_query("UPDATE Hand SET point_call='30'  WHERE hash='$me' ");
            if(myisset("call")  && $_REQUEST["call"]  == "0" && can_call(0,$me))
-             $result = mysql_query("UPDATE Hand SET point_call='0'   WHERE hash='$me' ");
+             $result = DB_query("UPDATE Hand SET point_call='0'   WHERE hash='$me' ");
 
            /* mark card as played */
-           mysql_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
-                       DB_quote_smart($card));
+           DB_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
+                    DB_quote_smart($card));
 
            /* get trick id or start new trick */
            $a = DB_get_current_trickid($gameid);
@@ -1142,8 +1098,8 @@ switch($mystatus)
                            $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
 
                            if($party1 != $party2)
-                             mysql_query("INSERT INTO Score".
-                                         " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
+                             DB_query("INSERT INTO Score".
+                                      " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
                          }
                    }
 
@@ -1169,8 +1125,8 @@ switch($mystatus)
                          $uid1   = DB_get_userid('gameid-position',$gameid,$winner);
                          $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
 
-                         mysql_query("INSERT INTO Score".
-                                     " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
+                         DB_query("INSERT INTO Score".
+                                  " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
                        }
                /* check for doppelopf (>40 points)*/
                $points = 0;
@@ -1183,12 +1139,12 @@ switch($mystatus)
                    $uid1   = DB_get_userid('gameid-position',$gameid,$winner);
                    $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
 
-                   mysql_query("INSERT INTO Score".
-                               " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
+                   DB_query("INSERT INTO Score".
+                            " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
                  }
 
                if($winner>0)
-                 mysql_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
+                 DB_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
                else
                  echo "ERROR during scoring";
 
@@ -1275,31 +1231,31 @@ switch($mystatus)
            else /* send out final email */
              {
                /* individual score */
-               $result = mysql_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
-                                     " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
-                                     " LEFT JOIN User ON User.id=Hand.user_id".
-                                     " LEFT JOIN Play ON Trick.id=Play.trick_id".
-                                     " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
-                                     " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
-                                     " WHERE Hand.game_id='$gameid'".
-                                     " GROUP BY User.fullname" );
+               $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
+                                  " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
+                                  " LEFT JOIN User ON User.id=Hand.user_id".
+                                  " LEFT JOIN Play ON Trick.id=Play.trick_id".
+                                  " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
+                                  " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
+                                  " WHERE Hand.game_id='$gameid'".
+                                  " GROUP BY User.fullname" );
                $message  = "The game is over. Thanks for playing :)\n";
                $message .= "Final score:\n";
-               while( $r = mysql_fetch_array($result,MYSQL_NUM))
+               while( $r = DB_fetch_array($result) )
                  $message .= "   ".$r[0]."(".$r[2].") ".$r[1]."\n";
 
-               $result = mysql_query("SELECT  Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
-                                     " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
-                                     " LEFT JOIN User ON User.id=Hand.user_id".
-                                     " LEFT JOIN Play ON Trick.id=Play.trick_id".
-                                     " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
-                                     " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
-                                     " WHERE Hand.game_id='$gameid'".
-                                     " GROUP BY Hand.party" );
+               $result = DB_query("SELECT  Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
+                                  " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
+                                  " LEFT JOIN User ON User.id=Hand.user_id".
+                                  " LEFT JOIN Play ON Trick.id=Play.trick_id".
+                                  " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
+                                  " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
+                                  " WHERE Hand.game_id='$gameid'".
+                                  " GROUP BY Hand.party" );
                $message .= "\nTotals:\n";
                $re     = 0;
                $contra = 0;
-               while( $r = mysql_fetch_array($result,MYSQL_NUM))
+               while( $r = DB_fetch_array($result) )
                  {
                    $message .= "    ".$r[0]." ".$r[1]."\n";
                    if($r[0] == "re")
@@ -1392,38 +1348,38 @@ switch($mystatus)
                  {
                    for( $p=$call_contra;$p<=120; $p+=30 )
                      {
-                         mysql_query("INSERT INTO Score".
-                                     " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
+                         DB_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')");
+                           DB_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')");
+                         DB_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')");
+                           DB_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')");
+                     DB_query("INSERT INTO Score".
+                              " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
                    }
 
                  /* one point each for winning and each 30 points + calls */
@@ -1436,15 +1392,15 @@ switch($mystatus)
                            $offset = 1;
 
                          if($re>$p-$offset)
-                           mysql_query("INSERT INTO Score".
-                                       " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
+                           DB_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')");
+                           DB_query("INSERT INTO Score".
+                                    " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
                        }
                    }
                  else if( $winning_party=="contra")
@@ -1456,15 +1412,15 @@ switch($mystatus)
                            $offset = 1;
 
                          if($contra>$p-$offset)
-                           mysql_query("INSERT INTO Score".
-                                       " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
+                           DB_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')");
+                           DB_query("INSERT INTO Score".
+                                    " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
                        }
                    }
 
@@ -1473,19 +1429,19 @@ switch($mystatus)
                  $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) )
+                 $queryresult = DB_query("SELECT score FROM Score ".
+                                         "  WHERE game_id=$gameid AND party='re'".
+                                         " ");
+                 while($r = DB_fetch_array($queryresult) )
                    {
                      $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) )
+                 $queryresult = DB_query("SELECT score FROM Score ".
+                                         "  WHERE game_id=$gameid AND party='contra'".
+                                         " ");
+                 while($r = DB_fetch_array($queryresult) )
                    {
                      $message .= "   ".$r[0]."\n";
                      $Tpoint --;
@@ -1568,15 +1524,15 @@ switch($mystatus)
          /* add pic for re/contra
           "      <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
 
-         $result = mysql_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
-                               " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
-                               " LEFT JOIN User ON User.id=Hand.user_id".
-                               " LEFT JOIN Play ON Trick.id=Play.trick_id".
-                               " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
-                               " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
-                               " WHERE Hand.game_id='$gameid'".
-                               " GROUP BY User.fullname" );
-         while( $r = mysql_fetch_array($result,MYSQL_NUM))
+         $result = DB_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
+                            " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
+                            " LEFT JOIN User ON User.id=Hand.user_id".
+                            " LEFT JOIN Play ON Trick.id=Play.trick_id".
+                            " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
+                            " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
+                            " WHERE Hand.game_id='$gameid'".
+                            " GROUP BY User.fullname" );
+         while( $r = DB_fetch_array($result))
            echo "      <div class=\"card".($r[3]-1)."\">\n".
                 "        <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
                 "      </div>\n";
@@ -1662,36 +1618,36 @@ switch($mystatus)
        }
       else
        {
-         $result = mysql_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
-                               " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
-                               " LEFT JOIN User ON User.id=Hand.user_id".
-                               " LEFT JOIN Play ON Trick.id=Play.trick_id".
-                               " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
-                               " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
-                               " WHERE Hand.game_id='$gameid'".
-                               " GROUP BY Hand.party" );
+         $result = DB_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
+                            " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
+                            " LEFT JOIN User ON User.id=Hand.user_id".
+                            " LEFT JOIN Play ON Trick.id=Play.trick_id".
+                            " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
+                            " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
+                            " WHERE Hand.game_id='$gameid'".
+                            " GROUP BY Hand.party" );
          echo "<div class=\"total\"> Totals:<br />\n";
-         while( $r = mysql_fetch_array($result,MYSQL_NUM))
+         while( $r = DB_fetch_array($result))
            echo "  ".$r[0]." ".$r[1]."<br />\n";
 
-         $queryresult = mysql_query("SELECT timediff(mod_date,create_date) ".
-                                    " FROM Game WHERE id='$gameid'");
-         $r = mysql_fetch_array($queryresult,MYSQL_NUM);
+         $queryresult = DB_query("SELECT timediff(mod_date,create_date) ".
+                                 " FROM Game WHERE id='$gameid'");
+         $r = DB_fetch_array($queryresult);
          echo "<p>This game took ".$r[0]." hours.</p>";
 
          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) )
+         $queryresult = DB_query("SELECT score FROM Score ".
+                                 "  WHERE game_id=$gameid AND party='re'".
+                                 " ");
+         while($r = DB_fetch_array($queryresult) )
            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) )
+         $queryresult = DB_query("SELECT score FROM Score ".
+                                 "  WHERE game_id=$gameid AND party='contra'".
+                                 " ");
+         while($r = DB_fetch_array($queryresult) )
            echo "   ".$r[0]."<br />\n";
          echo "</div>\n";
 
@@ -1724,8 +1680,7 @@ switch($mystatus)
       output_form_calls($me);
 
     /* get time from the last action of the game */
-    $result  = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
-    $r       = mysql_fetch_array($result,MYSQL_NUM);
+    $r = DB_query_array("SELECT mod_date from Game WHERE id='$gameid' " );
     $gameend = time() - strtotime($r[0]);
 
     if($gamestatus == 'play' || $gameend < 60*60*24*7)
@@ -1749,13 +1704,13 @@ switch($mystatus)
       {
 
        $session = DB_get_session_by_gameid($gameid);
-       $result  = mysql_query("SELECT id,create_date FROM Game".
-                              " WHERE session=$session".
-                              " ORDER BY create_date DESC".
-                              " LIMIT 1");
+       $result  = DB_query("SELECT id,create_date FROM Game".
+                           " WHERE session=$session".
+                           " ORDER BY create_date DESC".
+                           " LIMIT 1");
        $r = -1;
        if($result)
-         $r = mysql_fetch_array($result,MYSQL_NUM);
+         $r = DB_fetch_array($result);
 
        if(!$session || $gameid==$r[0])
          {