Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

lib/tools.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  This program is free software; you can redistribute it and/or
00004  modify it under the terms of the GNU General Public License
00005  as published by the Free Software Foundation; either version 2
00006  of the License, or (at your option) any later version.
00007 
00008  This program is distributed in the hope that it will be useful,
00009  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  GNU General Public License for more details.
00012 
00013  You should have received a copy of the GNU General Public License
00014  along with this program; if not, write to the Free Software
00015  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00016  ***************************************************************************/
00017 
00030 class Tools
00031 {
00032     var $oDB;   
00033 
00038     function Tools( &$oDB )
00039     {
00040         $this->oDB =& $oDB;
00041     }
00042 
00048     function clean( $booCleanMap )
00049     {
00050         $intCount = 0;
00051         $this->oDB->query('DELETE FROM '.$this->oDB->tblPlayer.' WHERE player_id > 1;');
00052         $this->oDB->query('UPDATE '.$this->oDB->tblUser.' SET player_id=0;');
00053         $intCount += intval( $this->oDB->affected_rows() );
00054         $this->oDB->query('DELETE FROM '.$this->oDB->tblTown.';');
00055         $intCount += intval( $this->oDB->affected_rows() );
00056         $this->oDB->query('DELETE FROM '.$this->oDB->tblGuild.' WHERE guild_id > 1;');
00057         $intCount += intval( $this->oDB->affected_rows() );
00058         $this->oDB->query('DELETE FROM '.$this->oDB->tblMapData.';');
00059         $intCount += intval( $this->oDB->affected_rows() );
00060         if( $booCleanMap )
00061         {
00062             $this->oDB->query('DELETE FROM '.$this->oDB->tblMap.';');
00063             $intCount += intval( $this->oDB->affected_rows() );
00064         }
00065         return $intCount;
00066     }
00067 
00078     function csvimport( $intServerId, $intLine = 0, $intLinesPerCall = 10, $booDbIsClean = false )
00079     {
00080         // check if import file exists
00081         if( !file_exists( './server'.$intServerId.'.csv' ) )
00082         {
00083             return false;
00084         }
00085         $fp = fopen( './server'.$intServerId.'.csv', 'r' );
00086         if( $fp === false )
00087         {
00088             // opening file failed
00089             return false;
00090         }
00091 
00092 
00093         // get first line (which is not needed) and check if it has the right length
00094         $aLine = fgetcsv( $fp, 10000, ',' );
00095         if( count( $aLine ) - 2 != $GLOBALS['intMapSize'] )
00096         {
00097             return false;
00098         }
00099         $booLastRun = false;
00100 
00101         // position the filepointer
00102         $intPosition = ( $GLOBALS['intMapSize'] * 3 + 5 ) * $intLine;
00103         fseek( $fp, $intPosition );
00104 
00105         $aMap = array();
00106         $intTime = time();
00107         $intCount = $intLine + $intLinesPerCall;
00108         if( $intCount > $GLOBALS['intMapSize'] + 1 )
00109         {
00110             $intCount = $GLOBALS['intMapSize'] + 1;
00111             $booLastRun = true;
00112         }
00113         // first add all fields to the map array
00114         for( $y = $intLine; $y < $intCount; $y++)
00115         {
00116             $aLineInfo = fgetcsv( $fp, 10000, ',' );
00117             if( $aLineInfo === false )
00118             {
00119                 // there was not a line... -> finished
00120                 $booLastRun = true;
00121                 $y = $intCount;
00122                 continue;
00123             }
00124             $intSize = count( $aLineInfo ) - 1;
00125             for( $x = 1; $x < $intSize; $x++ )
00126             {
00127                 $aMap[$x.'-'.$y] = array( 't' => $aLineInfo[$x], 'x' => $x, 'y' => $y );
00128             }
00129         }
00130         fclose( $fp );
00131 
00132         // now insert all into the database
00133         if( !$booDbIsClean )
00134         {
00135             // database is not clean, we have to check each field
00136             $aCurrentMap = array();
00137             $sqlReturn = $this->oDB->query( "SELECT x,y,terrain FROM ".$this->oDB->tblMap." WHERE y BETWEEN $intLine AND $intCount;");
00138             while( $aRow = $this->oDB->fetch_assoc( $sqlReturn ) )
00139             {
00140                 $aCurrentMap[$aRow['x'].'-'.$aRow['y']] = $aRow['terrain'];
00141             }
00142             // all available fields are in the array, start the import
00143             $strInsert = '';
00144             foreach( $aMap AS $aField )
00145             {
00146                 if( !isset( $aCurrentMap[$aField['x'].'-'.$aField['y']] ) )
00147                 {
00148                     // field is not in the db, insert it later
00149                     $strInsert .= '('.$aField['x'].','.$aField['y'].','.$aField['t'].'),';
00150                 }
00151                 elseif( $aCurrentMap[$aField['x'].'-'.$aField['y']]['terrain'] != $aField['t'] )
00152                 {
00153                     // update the field
00154                     $this->oDB->query("UPDATE ".$this->oDB->tblMap." SET terrain='".$aField['t']."' WHERE x='".$aField['x']."' AND y='".$aField['y']."';" );
00155                 }
00156             }
00157             if( $strInsert != '' )
00158             {
00159                 // strip the last ,
00160                 $strInsert = 'INSERT INTO '.$this->oDB->tblMap.' (x,y,terrain) VALUES '.substr( $strInsert, 0, -1 ).';';
00161                 $this->oDB->query( $strInsert );
00162             }
00163         }
00164         else
00165         {
00166             // database is clean, we can do all inserts at once!
00167             $strInsert = '';
00168             foreach( $aMap AS $aField )
00169             {
00170                 $strInsert .= '('.$aField['x'].','.$aField['y'].','.$aField['t'].'),';
00171             }
00172             if( $strInsert != '' )
00173             {
00174                 // strip the last ,
00175                 $strInsert = 'INSERT INTO '.$this->oDB->tblMap.' (x,y,terrain) VALUES '.substr( $strInsert, 0, -1 ).';';
00176                 $this->oDB->query($strInsert);
00177             }
00178         }
00179         if( $booLastRun )
00180         {
00181             // analyze table, to optimize later queries
00182             $this->oDB->query('ANALYZE TABLE `'.$this->oDB->tblMap.'`;');
00183         }
00184 
00185         return $booLastRun ? true : ( $intLine + $intLinesPerCall );
00186     }
00187 }

Generated on Sun May 8 19:29:45 2005 for PhpMap by  doxygen 1.4.2