00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00023 class ImportHtml extends ImportBase
00024 {
00025 function import( $strData )
00026 {
00027 $aData = explode( "\n", stripslashes( $strData ) );
00028
00029 $intFieldsUpdated = 0;
00030 $intFieldsSaved = 0;
00031 $intFields = 0;
00032 $intUnitsSaved = 0;
00033 $intInfos = 0;
00034 $intUnits = 0;
00035
00036
00037 $aMapData = array();
00038
00039
00040 for( $x = 0; $x < count( $aData ); ++$x )
00041 {
00042 $aTerrainInfo = array();
00043
00044
00045 if( strpos( $aData[$x], '<img' ) !== false )
00046 {
00047
00048 preg_match( '/id="t:(\d+):(\d+)" src="(\d*)-(.*?)(f){0,1}\.png"/', $aData[$x], $aField );
00049 if( empty( $aField ) ) continue;
00050
00051
00052
00053
00054
00055 $key = $aField[1].'-'.$aField[2];
00056 $aMapData[$key] = array(
00057 'x' => $aField[1],
00058 'y' => $aField[2],
00059 'terrain' => $aField[3],
00060 'pic' => $aField[4],
00061 'fow' => intval( isset( $aField[5] ) ) );
00062 $intFields++;
00063 }
00064
00065 elseif( strpos( $aData[$x], 'aBorders' ) !== false )
00066 {
00067
00068 preg_match( '/new Array\("(\d*)", "(\d*)", "(\w)(\d*)"\)/', $aData[$x], $aField );
00069 if( empty( $aField ) ) continue;
00070
00071
00072
00073
00074 $key = $aField[1].'-'.$aField[2];
00075 if( !isset( $aMapData[$key] ) ) continue;
00076
00077 $aField[4] = ( $aField[3] == 'e' ) ? intval( $aField[4] ) | 16 : intval( $aField[4] );
00078 $aMapData[$key]['border'] = $aField[4];
00079 $intInfos++;
00080 }
00081
00082 elseif( strpos( $aData[$x], 'aRoads' ) !== false )
00083 {
00084
00085 preg_match( '/new Array\("(\d*)", "(\d*)", "(\d*)"\)/', $aData[$x], $aField );
00086 if( empty( $aField ) ) continue;
00087
00088
00089
00090
00091 $key = $aField[1].'-'.$aField[2];
00092 if( !isset( $aMapData[$key] ) ) continue;
00093
00094 $aMapData[$key]['street'] = $aField[3];
00095 $intInfos++;
00096 }
00097 elseif( strpos( $aData[$x], 'aBuildings' ) !== false )
00098 {
00099
00100 preg_match( '/new Array\("(\d*)", "(\d*)", "(.*)", "(.*)".*\)/U', $aData[$x], $aField );
00101 if( empty( $aField ) ) continue;
00102
00103
00104
00105 $key = $aField[1].'-'.$aField[2];
00106 if( isset( $GLOBALS['aBuildingPics'][$aField[4]] ) && isset( $aMapData[$key] ) )
00107 {
00108 $aMapData[$key]['building'] = $GLOBALS['aBuildingPics'][$aField[4]];
00109 $intInfos++;
00110 }
00111 }
00112 elseif( strpos( $aData[$x], 'aUnitFields' ) !== false )
00113 {
00114
00115
00116 preg_match( '/new Array\("(\d*)", "(\d*)", "(.*)", "(\d*)", "(\d*)"\)/', $aData[$x], $aField );
00117 if( empty( $aField ) ) continue;
00118
00119
00120
00121 $key = $aField[1].'-'.$aField[2];
00122
00123 if( !isset( $aMapData[$key] ) ) continue;
00124
00125 if( !isset( $aMapData[$key]['unit'] ) )
00126 {
00127 $aMapData[$key]['unit'] = pow( 2, intval( $aField[3][0] ) - 1 );
00128 }
00129 else
00130 {
00131 $aMapData[$key]['unit'] |= pow( 2, intval( $aField[3][0] ) - 1 );
00132 }
00133 $intInfos++;
00134 }
00135 }
00136
00137 $aMapKeys = array_keys( $aMapData );
00138 $intSize = count( $aMapKeys );
00139
00140
00141 if( $intSize > 0 )
00142 {
00143 $field = $aMapData[$aMapKeys[0]];
00144 $field2 = $aMapData[$aMapKeys[$intSize-1]];
00145 $x_center = $field['x'] + ($field2['x']-$field['x']) /2;
00146 $y_center = $field['y'] + ($field2['y']-$field['y']) /2;
00147 }
00148 else
00149 {
00150
00151 return;
00152 }
00153
00154
00155 $aFieldDb = array();
00156 $sqlResult = $this->oDB->query("SELECT m.*, md.fow, md.border, md.building, md.street, md.unit ".
00157 "FROM ".$this->oDB->tblMap." m ".
00158 "LEFT JOIN ".$this->oDB->tblMapData." md ON (m.x=md.x AND m.y=md.y) ".
00159 "WHERE m.x BETWEEN $field[x] AND $field2[x] AND m.y BETWEEN $field[y] AND $field2[y];");
00160 while( $aRow = $this->oDB->fetch_assoc($sqlResult) )
00161 {
00162 $aRow['town_id'] = 0;
00163 $aRow['details'] = 1;
00164 if( is_null( $aRow['fow'] ) )
00165 {
00166
00167 $aRow['fow'] = 1;
00168 $aRow['details'] = 0;
00169 }
00170 $aFieldDb[$aRow['x']][$aRow['y']] =& $aRow;
00171 unset( $aRow );
00172 }
00173
00174 $sqlResult = $this->oDB->query("SELECT x, y, town_id ".
00175 "FROM ".$this->oDB->tblTown." t ".
00176 "WHERE t.x BETWEEN $field[x] AND $field2[x] AND t.y BETWEEN $field[y] AND $field2[y];");
00177 while( $aRow = $this->oDB->fetch_assoc($sqlResult) )
00178 {
00179 $aFieldDb[$aRow['x']][$aRow['y']]['town_id'] = $aRow['town_id'];
00180 }
00181
00182
00183
00184 $this->oLog->write( 'Kartenquelltext (' . $x_center . ',' . $y_center . ')', 6 );
00185
00186
00187 for( $i = 0; $i<$intSize; $i++ )
00188 {
00189
00190 $field = $aMapData[$aMapKeys[$i]];
00191
00192
00193 if( !$this->oUser->check( $field['x'], $field['y'] ) )
00194 {
00195 continue;
00196 }
00197
00198
00199 $field['terrain'] = isset( $field['terrain'] ) ? $field['terrain'] : 'NULL';
00200 $field['border'] = isset( $field['border'] ) ? $field['border'] : 'NULL';
00201 $field['building'] = isset( $field['building'] ) ? $field['building'] : 'NULL';
00202 $field['street'] = isset( $field['street'] ) ? $field['street'] : 'NULL';
00203 $field['unit'] = !empty( $field['unit'] ) ? $field['unit'] : 'NULL';
00204 $field['fow'] = isset( $field['fow'] ) ? $field['fow'] : '1';
00205
00206
00207 $field['db'] = isset( $aFieldDb[$field['x']][$field['y']] );
00208
00209
00210 if( !$field['db'] )
00211 {
00212
00213 $intTownId = 'NULL';
00214 if( $field['building'] != 'NULL' && isset( $GLOBALS['aTowns'][$field['building']] ) )
00215 {
00216 $intTownId = $this->oTown->add( $field['x'], $field['y'] );
00217 }
00218 $this->oDB->query( 'REPLACE INTO '.$this->oDB->tblMapData.' '.
00219 "SET x='".$field['x']."', y='".$field['y']."', ".
00220 "fow='".$field['fow']."', ".
00221 "building=".$field['building'].", unit=".$field['unit'].", ".
00222 "border=".$field['border'].", street=".$field['street'].$GLOBALS['strUpdateString'].";");
00223 $this->oDB->query( 'INSERT INTO '.$this->oDB->tblMap." SET x='".$field['x']."', y='".$field['y']."', terrain='".$field['terrain']."';" );
00224 $intFieldsSaved++;
00225 if( $field['unit'] != 'NULL')
00226 {
00227 $intUnitsSaved++;
00228 }
00229 continue;
00230 }
00231
00232
00233
00234
00235 if( $field['fow'] == 0 )
00236 {
00237
00238 $intTownId = 'NULL';
00239 if( $field['building'] != 'NULL' && isset( $GLOBALS['aTowns'][$field['building']] ) )
00240 {
00241 $aTown = $this->oTown->get( $field['x'], $field['y'] );
00242 if( $aTown['town_id'] > 0 )
00243 {
00244 $intTownId = $aTown['town_id'];
00245 }
00246
00247 else
00248 {
00249 $intTownId = $this->oTown->add( $field['x'], $field['y'] );
00250 }
00251 }
00252 elseif( $field['building'] != 'NULL' && !empty( $aFieldDb[$field['x']][$field['y']]['town_id'] ) )
00253 {
00254
00255 $this->oTown->delete( $aFieldDb[$field['x']][$field['y']]['town_id'], $field['x'], $field['y'] );
00256 }
00257
00258 $aCurrent =& $aFieldDb[$field['x']][$field['y']];
00259 if( $aCurrent['fow'] == 0
00260 && intval( $aCurrent['terrain'] ) == intval( $field['terrain'] )
00261 && intval( $aCurrent['building'] ) == intval( $field['building'] )
00262 && intval( $aCurrent['unit'] ) == intval( $field['unit'] )
00263 && intval( $aCurrent['border'] ) == intval( $field['border'] )
00264 && intval( $aCurrent['street'] ) == intval( $field['street'] )
00265 && intval( $aCurrent['town_id'] ) == intval( $intTownId ) )
00266 {
00267
00268 continue;
00269 }
00270
00271
00272 if( $aFieldDb[$field['x']][$field['y']]['terrain'] == 0 )
00273 {
00274 $this->oDB->query( 'UPDATE '.$this->oDB->tblMap." SET terrain='".$field['terrain']."' WHERE x='".$field['x']."' AND y='".$field['y']."';" );
00275 }
00276 if( empty( $aFieldDb[$field['x']][$field['y']]['details'] ) )
00277 {
00278
00279 $this->oDB->query( 'REPLACE INTO '.$this->oDB->tblMapData.' '.
00280 "SET fow='0', x='".$field['x']."', y='".$field['y']."', ".
00281 "building=".$field['building'].", unit=".$field['unit'].", ".
00282 "border=".$field['border'].", street=".$field['street'].$GLOBALS['strUpdateString'] );
00283 }
00284 else
00285 {
00286
00287 $this->oDB->query( 'UPDATE '.$this->oDB->tblMapData.' '.
00288 "SET fow='0', ".
00289 "building=".$field['building'].", unit=".$field['unit'].", ".
00290 "border=".$field['border'].", street=".$field['street'].$GLOBALS['strUpdateString'].
00291 "WHERE x='".$field['x']."' AND y='".$field['y']."'" );
00292 }
00293
00294 $intFieldsUpdated++;
00295 if( $field['unit'] != 'NULL' )
00296 {
00297 $intUnitsSaved++;
00298 }
00299 continue;
00300 }
00301
00302
00303
00304
00305 if( $aFieldDb[$field['x']][$field['y']]['terrain'] == 0 )
00306 {
00307 $this->oDB->query( 'UPDATE '.$this->oDB->tblMap." SET terrain='".$field['terrain']."' WHERE x='".$field['x']."' AND y='".$field['y']."';" );
00308 $intFieldsSaved++;
00309 }
00310 }
00311 return array( 'map.php?x='.$x_center.'&y='.$y_center, "Erkannte Felder: $intFields / Erkannte Infos: $intInfos / Erkannte Einheiten: $intUnits<br> <br>Gespeicherte Felder: $intFieldsSaved<br>Update für Felder: $intFieldsUpdated<br>Gespeicherte Einheiten: $intUnitsSaved<br>" );
00312 }
00313 }