00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
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
00089 return false;
00090 }
00091
00092
00093
00094 $aLine = fgetcsv( $fp, 10000, ',' );
00095 if( count( $aLine ) - 2 != $GLOBALS['intMapSize'] )
00096 {
00097 return false;
00098 }
00099 $booLastRun = false;
00100
00101
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
00114 for( $y = $intLine; $y < $intCount; $y++)
00115 {
00116 $aLineInfo = fgetcsv( $fp, 10000, ',' );
00117 if( $aLineInfo === false )
00118 {
00119
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
00133 if( !$booDbIsClean )
00134 {
00135
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
00143 $strInsert = '';
00144 foreach( $aMap AS $aField )
00145 {
00146 if( !isset( $aCurrentMap[$aField['x'].'-'.$aField['y']] ) )
00147 {
00148
00149 $strInsert .= '('.$aField['x'].','.$aField['y'].','.$aField['t'].'),';
00150 }
00151 elseif( $aCurrentMap[$aField['x'].'-'.$aField['y']]['terrain'] != $aField['t'] )
00152 {
00153
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
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
00167 $strInsert = '';
00168 foreach( $aMap AS $aField )
00169 {
00170 $strInsert .= '('.$aField['x'].','.$aField['y'].','.$aField['t'].'),';
00171 }
00172 if( $strInsert != '' )
00173 {
00174
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
00182 $this->oDB->query('ANALYZE TABLE `'.$this->oDB->tblMap.'`;');
00183 }
00184
00185 return $booLastRun ? true : ( $intLine + $intLinesPerCall );
00186 }
00187 }