DIR : /home/kozerus/public_html/dgs/include/db/ratinglog.php

/home/kozerus/public_html/dgs/include/db

<?php
/*
Dragon Go Server
Copyright (C) 2001-  Erik Ouchterlony, Jens-Uwe Gaspar

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

 /* Author: Jens-Uwe Gaspar */

$TranslateGroups[] = "Game";

require_once 'include/globals.php';
require_once 'include/db_classes.php';
require_once 'include/dgs_cache.php';
require_once 'include/std_classes.php';

 /*!
  * \file ratinglog.php
  *
  * \brief Functions for managing rating results: tables Ratinglog
  */


 /*!
  * \class Ratinglog
  *
  * \brief Class to manage rating-log
  */

global $ENTITY_RATINGLOG; //PHP5
$ENTITY_RATINGLOG = new Entity( 'Ratinglog',
      FTYPE_PKEY, 'ID',
      FTYPE_AUTO, 'ID',
      FTYPE_INT,  'ID', 'uid', 'gid',
      FTYPE_FLOAT, 'Rating', 'RatingMin', 'RatingMax', 'RatingDiff',
      FTYPE_DATE, 'Time'
   );

class Ratinglog
{
   public $ID;
   public $uid;
   public $gid;
   public $Time;
   public $Rating;
   public $RatingMin;
   public $RatingMax;
   public $RatingDiff;

   /*! \brief Constructs Ratinglog-object with specified arguments. */
   public function __construct( $id=0, $uid=0, $gid=0, $time=0,
         $rating=null, $rating_min=null, $rating_max=null, $rating_diff=null )
   {
      $this->ID = (int)$id;
      $this->uid = (int)$uid;
      $this->gid = (int)$gid;
      $this->Time = (int)$time;
      $this->Rating = is_null($rating) ? null : (float)$rating;
      $this->RatingMin = is_null($rating_min) ? null : (float)$rating_min;
      $this->RatingMax = is_null($rating_max) ? null : (float)$rating_max;
      $this->RatingDiff = is_null($rating_diff) ? null : (float)$rating_diff;
   }

   public function to_string()
   {
      return print_r($this, true);
   }

   /*! \brief Inserts or updates Ratinglog-entry in database. */
   public function persist()
   {
      if ( $this->ID > 0 )
         $success = $this->update();
      else
         $success = $this->insert();
      return $success;
   }

   public function insert()
   {
      $this->Time = $GLOBALS['NOW'];

      $entityData = $this->fillEntityData();
      $result = $entityData->insert( "Ratinglog.insert(%s)" );
      if ( $result )
         $this->ID = mysql_insert_id();
      return $result;
   }

   public function update()
   {
      $entityData = $this->fillEntityData();
      return $entityData->update( "Ratinglog.update(%s)" );
   }

   public function delete()
   {
      $entityData = $this->fillEntityData();
      return $entityData->delete( "Ratinglog.delete(%s)" );
   }

   public function fillEntityData( EntityData $data = null )
   {
      if ( is_null($data) )
         $data = $GLOBALS['ENTITY_RATINGLOG']->newEntityData();
      $data->set_value( 'ID', $this->ID );
      $data->set_value( 'uid', $this->uid );
      $data->set_value( 'gid', $this->gid );
      $data->set_value( 'Time', $this->Time );
      $data->set_value( 'Rating', $this->Rating );
      $data->set_value( 'RatingMin', $this->RatingMin );
      $data->set_value( 'RatingMax', $this->RatingMax );
      $data->set_value( 'RatingDiff', $this->RatingDiff );
      return $data;
   }


   // ------------ static functions ----------------------------

   /*! \brief Returns db-fields to be used for query of Ratinglog-objects for given game-id. */
   public static function build_query_sql( $gid=0, $uid=0 )
   {
      $qsql = $GLOBALS['ENTITY_RATINGLOG']->newQuerySQL('RL');
      if ( $gid > 0 )
         $qsql->add_part( SQLP_WHERE, "RL.gid='$gid'" );
      if ( $uid > 0 )
         $qsql->add_part( SQLP_WHERE, "RL.uid='$uid'" );
      return $qsql;
   }

   /*! \brief Returns Ratinglog-object created from specified (db-)row. */
   public static function new_from_row( array $row )
   {
      $rl = new Ratinglog(
            // from Ratinglog
            @$row['ID'],
            @$row['uid'],
            @$row['gid'],
            @$row['X_Time'],
            @$row['Rating'],
            @$row['RatingMin'],
            @$row['RatingMax'],
            @$row['RatingDiff']
         );
      return $rl;
   }

   /*!
    * \brief Loads and returns Ratinglog-object for given games-id limited to 1 result-entry.
    * \param $query_qsql QuerySQL restricting entries, expecting one result
    * \return NULL if nothing found; Games otherwise
    */
   public static function load_ratinglog_with_query( QuerySQL $query_qsql )
   {
      $qsql = self::build_query_sql();
      $qsql->merge( $query_qsql );
      $qsql->add_part( SQLP_LIMIT, '1' );

      $row = mysql_single_fetch( "Ratinglog:load_ratinglog_with_query.find_ratinglog()",
         $qsql->get_select() );
      return ($row) ? self::new_from_row($row) : NULL;
   }//load_ratinglog_with_query

   /*! \brief Returns enhanced (passed) ListIterator with Ratinglog-objects. */
   public static function load_ratinglogs( ListIterator $iterator, $gid=0, $uid=0 )
   {
      $qsql = self::build_query_sql( $gid, $uid );
      $iterator->setQuerySQL( $qsql );
      $query = $iterator->buildQuery();
      $result = db_query( "Ratinglog:load_ratinglogs", $query );
      $iterator->setResultRows( mysql_num_rows($result) );

      $iterator->clearItems();
      while ( $row = mysql_fetch_array( $result ) )
      {
         $rlog = self::new_from_row( $row );
         $iterator->addItem( $rlog, $row );
      }
      mysql_free_result($result);

      return $iterator;
   }//load_ratinglogs

   /*!
    * \brief Loads and caches part of Ratinglog-table for given user-id.
    * \param $max_cache_count if row-count exceeds given $max_cache_count caching will not be done
    * \return arr( is-cached, entries-count, RESULT ) with data ordered by Time:
    *       RESULT is array of row-arr( Rating/RatingMin/RatingMax/seconds => val ) if is-cached == true; otherwise
    *       RESULT is db-result from db_query()-call and client must iterator and close result by itself
    * \note conditional caching needed, because LIVE-server memory is not enough to cover large rating-row for some users.
    */
   public static function load_cache_ratinglogs( $uid, $max_cache_count )
   {
      $uid = (int)$uid;
      $dbgmsg = "Ratinglog::load_cache_ratinglogs($uid)";
      $key = "RLog.$uid";

      $data_result = DgsCache::fetch( $dbgmsg, CACHE_GRP_RATINGLOG, $key );
      if ( is_null($data_result) )
      {
         $result = db_query( $dbgmsg.'.find',
               "SELECT SQL_CALC_FOUND_ROWS Rating, RatingMin, RatingMax, UNIX_TIMESTAMP(Time) AS seconds " .
               "FROM Ratinglog WHERE uid=$uid ORDER BY Time" );

         $cnt_rows = mysql_found_rows( $dbgmsg.'.row_count' );
         if ( $cnt_rows > $max_cache_count )
         {
            $data_result = array( false, $cnt_rows, $result );
         }
         else
         {
            $rating_logs = array();
            while ( $row = mysql_fetch_array( $result ) )
               $rating_logs[] = $row;
            mysql_free_result($result);

            $data_result = array( true, $cnt_rows, $rating_logs );
            DgsCache::store( $dbgmsg, CACHE_GRP_RATINGLOG, $key, $data_result, 15*SECS_PER_MIN );
         }
      }

      return $data_result;
   }//load_cache_ratinglogs

   public static function delete_cache_ratinglogs( $dbgmsg, $uid )
   {
      DgsCache::delete( $dbgmsg, CACHE_GRP_RATINGLOG, "RLog.$uid" );
   }

} // end of 'Ratinglog'
?>
//


koh5_pano



Your browser does not support the HTML5 canvas element.


Drag mouse to navigate.

Navigation





17.Aug.2010, Martin Wengenmayer