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

importscan.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 
00022 class ImportScan extends ImportBase
00023 {
00024     function import( $strData )
00025     {
00026         global $aRaces, $aTypes;
00027 
00028         $strData = str_replace( "\r\n", "\n", $strData );
00029 
00030         // replace tabs with spaces
00031         $strData = str_replace( "\t", ' ', $strData );
00032 
00033         // replace multiple spaces with one space
00034         $strData = preg_replace( '/ +/', ' ', $strData );
00035 
00036         $strData = preg_replace( "/ +\n/", "\n", $strData );
00037 
00038         $intX = 0;
00039         $intY = 0;
00040         $strOwner = '';
00041         $strGuild = '';
00042         $strRace = '';
00043         $strType = '';
00044         $strTown = '';
00045         $intTownhall = 0;
00046         $intSight = 0;
00047         $spos = 0;
00048         $intType = -1;
00049         $intRace = -1;
00050         $intBuilding = 0;
00051         $intTerrain = 0;
00052 
00053         $strInfo = 'Scan ' . date('d.m.Y H:i:s') . "\n".$strData;
00054 
00055         // Log-Eintrag schreiben
00056         $this->oLog->write( 'Stadtscan-Import', 6 );
00057 
00058         $source = explode( "\n", $strData );
00059         for( $x=0; $x<count( $source ); ++$x )
00060         {
00061             $strLine = $source[$x];
00062             if( ( $spos = strpos( $strLine, 'Gelände:') ) !== false )
00063             {
00064                 $strTerrain = trim( substr( $strLine, $spos+8, strlen( $strLine ) ) );
00065                 foreach( $GLOBALS['aTerrainText'] AS $int => $str )
00066                 {
00067                     if( $str == $strTerrain || $str == htmlentities( $strTerrain ) )
00068                     {
00069                         $intTerrain = $int;
00070                     }
00071                 }
00072                 continue;
00073             }
00074             if( ( $spos = strpos( $strLine, 'Position:') ) !== false )
00075             {
00076                 $aPosition = explode( ':', trim( substr( $strLine, $spos+9, strlen( $strLine ) ) ) );
00077                 $intX = $aPosition[0];
00078                 $intY = $aPosition[1];
00079                 continue;
00080             }
00081             if( ( $spos = strpos( $strLine, 'Stadt:') ) !== false )
00082             {
00083                 $strTown = trim( substr( $strLine, $spos+6, strlen( $strLine ) ) );
00084                 continue;
00085             }
00086             if( ( $spos = strpos( $strLine, 'Herrscher:') ) !== false )
00087             {
00088                 $strOwner = trim( substr( $strLine, $spos+10, strlen( $strLine ) ) );
00089                 continue;
00090             }
00091             if( ( $spos = strpos( $strLine, 'Gilde:') ) !== false )
00092             {
00093                 $strGuild = trim( substr( $strLine, $spos+6, strlen( $strLine ) ) );
00094                 // strip Tag
00095                 $strGuild = str_replace( '[', '', str_replace( ']', '', $strGuild ) );
00096                 continue;
00097             }
00098             if( ( $spos = strpos( $strLine, 'Rasse:') ) !== false )
00099             {
00100                 $strRace = trim( substr( $strLine, $spos+6, strlen( $strLine ) ) );
00101                 // Rassenid holen
00102                 foreach( $aRaces AS $id => $racename)
00103                 {
00104                     if( $racename == $strType || $racename == htmlentities( $strRace ) ) $intRace = $id;
00105                 }
00106                 $intBuilding = $intRace + 96;
00107                 continue;
00108             }
00109             if( ( $spos = strpos( $strLine, 'Typus:') ) !== false )
00110             {
00111                 $strType = trim( substr( $strLine, $spos+6, strlen( $strLine ) ) );
00112 
00113                 // Typid holen
00114                 foreach( $aTypes AS $id => $typname )
00115                 {
00116                     if( $typname == $strType || $typname == htmlentities( $strType ) ) $intType = $id;
00117                 }
00118                 continue;
00119             }
00120 
00121             if( strpos( $strLine, 'Rathaus' ) !== false )
00122             {
00123                 $intTownhall = 1;
00124             }
00125 
00126             foreach( $GLOBALS['aSight'] AS $strBuilding => $intView )
00127             {
00128                 if( $intView > $intSight && strpos( $strLine, $strBuilding ) !== false )
00129                 {
00130                     $intSight = $intView;
00131                 }
00132             }
00133         }
00134         // end of getting data
00135 
00136         if( $intX > 0 && $intY > 0 && $intRace >= 0 && $intType >= 0 && $this->oUser->check( $intX, $intY ) && $strOwner != '' )
00137         {
00138             $intGuild = 1;
00139             if( $strGuild != '' )
00140             {
00141                 $sqlGuild = $this->oDB->query( "SELECT `guild_id` FROM `".$this->oDB->tblGuild."` WHERE `tag` = '$strGuild';" );
00142                 if( $this->oDB->num_rows( $sqlGuild ) == 0 )
00143                 {
00144                     // Gilde existiert noch nicht... -> anlegen
00145                     $this->oDB->query( "INSERT INTO `".$this->oDB->tblGuild."` SET `tag`='$strGuild'".$GLOBALS['strUpdateString'].$GLOBALS['strCreatedString'].";" );
00146                     $intGuild = $this->oDB->insert_id();
00147                 }
00148                 else
00149                 {
00150                     $intGuild = $this->oDB->fetch_assoc( $sqlGuild );
00151                     $intGuild = $intGuild['guild_id'];
00152                 }
00153             }
00154 
00155             if( $strOwner != '' )
00156             {
00157                 // Spieler überprüfen und gegebenenfalls anlegen
00158                 $sqlResult = $this->oDB->query( "SELECT * FROM `".$this->oDB->tblPlayer."` WHERE `name` = '$strOwner';"  );
00159                 if( $this->oDB->num_rows( $sqlResult ) == 0 )
00160                 {
00161                     $this->oDB->query( "INSERT INTO `".$this->oDB->tblPlayer."` SET `name`='$strOwner', `guild_id`='$intGuild', `typ`='$intType' , `race`='$intRace'".$GLOBALS['strUpdateString'].$GLOBALS['strCreatedString'].";" );
00162                 }
00163                 $sqlResult = $this->oDB->query("SELECT `player_id` FROM `".$this->oDB->tblPlayer."` WHERE `name` = '$strOwner';" );
00164                 $aPlayer   = $this->oDB->fetch_assoc( $sqlResult );
00165                 $intPlayer = $aPlayer['player_id'];
00166             }
00167             else
00168             {
00169                 $intPlayer = 1;
00170             }
00171 
00172             $aTown = $this->oTown->get( $intX, $intY );
00173             if( isset( $aTown['town_id'] ) )
00174             {
00175                 $this->oTown->set( $aTown['town_id'], $strTown, $intPlayer, $intSight, $intTownhall, $strInfo );
00176             }
00177             else
00178             {
00179                 $this->oTown->add( $intX, $intY, $strTown, $intPlayer, $strInfo, $intTownhall, $intSight );
00180             }
00181 
00182             // prüfen ob Feld schon vorhanden
00183             $this->oDB->query( "SELECT `x`, `y`, `terrain` FROM `".$this->oDB->tblMap."` WHERE `x`='$intX' AND `y`='$intY';" );
00184             $aRow = $this->oDB->fetch_assoc();
00185 
00186             $this->oDB->query( "SELECT `x`, `y` FROM `".$this->oDB->tblMapData."` WHERE `x`='$intX' AND `y`='$intY';" );
00187             $aRow2 = $this->oDB->fetch_assoc();
00188             if( $aRow['terrain'] > 0 && $intTerrain > 0 && $aRow['terrain'] != $intTerrain )
00189             {
00190                 // error
00191                 return false;
00192             }
00193             elseif( $aRow2['x'] == $intX && $aRow2['y'] == $intY )
00194             {
00195                 // field exists
00196                 $this->oDB->query( "UPDATE `".$this->oDB->tblMapData."` SET `building`='$intBuilding'".$GLOBALS['strUpdateString']." WHERE `x` = '$intX' AND `y` = '$intY';" );
00197                 if( $aRow['terrain'] == 0 )
00198                 {
00199                     // terrain info is missing
00200                     $this->oDB->query( "UPDATE `".$this->oDB->tblMap."` SET `terrain`='$intTerrain' WHERE `x` = '$intX' AND `y` = '$intY';" );
00201                 }
00202             }
00203             else
00204             {
00205                 // field did not exist
00206                 $this->oDB->query( "REPLACE INTO `".$this->oDB->tblMapData."` SET `building`='$intBuilding', `x` = '$intX', `y` = '$intY'".$GLOBALS['strUpdateString'].";" );
00207                 if( $aRow['x'] != $intX || $aRow['y'] != $intY )
00208                 {
00209                     // terrain info is missing, too
00210                     $this->oDB->query( "INSERT INTO `".$this->oDB->tblMap."` SET `terrain`='$intTerrain', `x` = '$intX', `y` = '$intY';" );
00211                 }
00212             }
00213 
00214             return array( 'map.php?x='.$intX.'&amp;y='.$intY, 'Stadtscan Import erfolgreich' );
00215         }
00216         else
00217         {
00218             // incorrect scan or no rights for these coords
00219             $this->oLog->write( "Import: Stadtscan failed on: XY=$intX,$intY R=$intRace T=$intType A=".$this->oUser->check( $intX, $intY ), 10 );
00220             return false;
00221         }
00222     }
00223 }

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