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

lib/field.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 
00028 class Field
00029 {
00030     var $oDB;       
00031     var $oUser;     
00032     var $oLog;      
00033     var $aField;    
00034     var $oTown;     
00035 
00044     function Field( &$oDB, &$oUser, &$oTown, &$oLog )
00045     {
00046         $this->oDB =& $oDB;
00047         $this->oUser =& $oUser;
00048         $this->oTown =& $oTown;
00049         $this->oLog =& $oLog;
00050     }
00051 
00058     function get( $x, $y )
00059     {
00060         $this->aField = array(
00061             'fow' => 1,
00062             'border' => NULL,
00063             'building' => NULL,
00064             'street' => NULL,
00065             'unit' => NULL,
00066             'player_id' => NULL,
00067             'name' => NULL,
00068             'guild_id' => NULL,
00069             'race' => NULL,
00070             'typ' => NULL,
00071             'town_name' => NULL,
00072             'town_id' => NULL,
00073             'sight' => NULL );
00074 
00075 
00076         if( !$this->oUser->check( $x, $y ) )
00077         {
00078             return $this->aField;
00079         }
00080 
00081         // get field data
00082         $aRow = $this->oDB->fetch_assoc($this->oDB->query("SELECT m.* FROM `".$this->oDB->tblMap."` m WHERE m.`x`='$x' AND m.`y`='$y';" ) );
00083         $this->aField['terrain'] = $aRow['terrain'];
00084         $this->aField['x'] = $aRow['x'];
00085         $this->aField['y'] = $aRow['y'];
00086 
00087         $sqlResult = $this->oDB->query("SELECT m.* FROM `".$this->oDB->tblMapData."` m WHERE m.`x`='$x' AND m.`y`='$y';" );
00088         if( $this->oDB->num_rows( $sqlResult ) )
00089         {
00090             $aRow = $this->oDB->fetch_assoc( $sqlResult );
00091             $this->aField['fow'] = $aRow['fow'];
00092             $this->aField['border'] = $aRow['border'];
00093             $this->aField['building'] = $aRow['building'];
00094             $this->aField['street'] = $aRow['street'];
00095             $this->aField['unit'] = $aRow['unit'];
00096         }
00097 
00098         $sqlResult = $this->oDB->query("SELECT p.*, t.name AS town_name, t.town_id, t.sight ".
00099                                        "FROM `".$this->oDB->tblTown."` t ".
00100                                        "LEFT JOIN `".$this->oDB->tblPlayer."` p USING (`player_id`) ".
00101                                        "WHERE t.`x`='$x' AND t.`y`='$y';");
00102         if( $this->oDB->num_rows( $sqlResult ) )
00103         {
00104             $aRow = $this->oDB->fetch_assoc( $sqlResult );
00105             $this->aField['player_id'] = $aRow['player_id'];
00106             $this->aField['name'] = $aRow['name'];
00107             $this->aField['guild_id'] = $aRow['guild_id'];
00108             $this->aField['race'] = $aRow['race'];
00109             $this->aField['typ'] = $aRow['typ'];
00110             $this->aField['town_name'] = $aRow['town_name'];
00111             $this->aField['town_id'] = $aRow['town_id'];
00112             $this->aField['sight'] = $aRow['sight'];
00113         }
00114         return $this->aField;
00115     }
00116 
00130     function set( $x, $y, $strPlayer, $strTown, $intGuild, $intType, $intRace, $intTown, $intSight )
00131     {
00132         if( !$this->oUser->check( $x, $y ) )
00133         {
00134             // user has no access to this field
00135             return false;
00136         }
00137         $this->get( $x, $y );
00138 
00139         if( $strPlayer == '' )
00140         {
00141             // remove the owner of the town
00142             $this->oTown->set( $this->aField['town_id'], $this->aField['town_name'], 1 );
00143         }
00144         else
00145         {
00146             $booUpdate = false;
00147             $strQuery = "UPDATE `".$this->oDB->tblPlayer."` SET ";
00148 
00149             // do not save unknown guild/type/race
00150             if( $intGuild > 0 )
00151             {
00152                 $strQuery .= " `guild_id`='$intGuild'";
00153                 $booUpdate = true;
00154             }
00155             if( $intType != 0 )
00156             {
00157                 if( $strQuery ) $strQuery .= ',';
00158                 $strQuery .= " typ='$intType'";
00159                 $booUpdate = true;
00160             }
00161             if( $intRace != 0 )
00162             {
00163                 if( $strQuery ) $strQuery .= ',';
00164                 $strQuery .= " race='$intRace'";
00165                 $booUpdate = true;
00166             }
00167 
00168             // get new owner
00169             $aRow = $this->oDB->query_first( "SELECT `player_id`, `name` FROM `".$this->oDB->tblPlayer."` WHERE UPPER(`name`)=UPPER('$strPlayer');");
00170             if( $aRow['player_id'] != $this->aField['player_id'] || is_null( $aRow['player_id'] ) )
00171             {
00172                 // link town with new owner
00173                 if( strcasecmp( $aRow['name'], $strPlayer ) != 0 )
00174                 {
00175                     // there is no player entry in the database
00176                     $this->oDB->query("INSERT INTO `".$this->oDB->tblPlayer."` SET name='$strPlayer', guild_id='$intGuild', typ='$intType', race='$intRace'".$GLOBALS['strUpdateString'].$GLOBALS['strCreatedString'].";");
00177                     $aRow['player_id'] = $this->oDB->insert_id();
00178                 }
00179                 $this->oTown->set( $this->aField['town_id'], $strTown, $aRow['player_id'], $intSight );
00180             }
00181             else
00182             {
00183                 // owner has not changed
00184                 if( $strTown != $this->aField['town_name'] || $intSight != $this->aField['sight'] )
00185                 {
00186                     // update townname
00187                     $this->oTown->set( $this->aField['town_id'], $strTown, -1, $intSight );
00188                 }
00189             }
00190 
00191             // update guild/race/type?
00192             if( $booUpdate && $aRow['player_id'] > 1 )
00193             {
00194                 $strQuery .= $GLOBALS['strUpdateString']." WHERE player_id='".$aRow['player_id']."';";
00195                 $this->oDB->query( $strQuery );
00196             }
00197         }
00198 
00199         // towntype has changed
00200         if( $intTown != 0 )
00201         {
00202           $this->oDB->query( "UPDATE `".$this->oDB->tblMapData."` SET `building` ='$intTown' WHERE `x`='$x' AND `y`='$y'" );
00203         }
00204         return true;
00205     }
00206 
00214     function add( $x, $y, $intTownType = -1 )
00215     {
00216         if( !$this->oUser->check( $x, $y ) )
00217         {
00218             // user has no access to this field
00219             return false;
00220         }
00221 
00222         $this->get( $x, $y );
00223         if( $this->aField['town_id'] != NULL )
00224         {
00225             // town already exists
00226             return false;
00227         }
00228 
00229         // create new town
00230         $intTownId = $this->oTown->add( $x, $y, '', 1 );
00231 
00232         // link town with field
00233         $this->oDB->query( "SELECT x,y FROM `".$this->oDB->tblMapData."` WHERE `x`='$x' AND `y`='$y';");
00234         $aRows = $this->oDB->fetch_array();
00235         if(  $this->oDB->affected_rows() > 0 )
00236         {
00237             $this->oDB->query( "UPDATE `".$this->oDB->tblMapData."` SET `building`='".$intTownType."'".$GLOBALS['strUpdateString']." WHERE `x`='$x' AND `y`='$y';");
00238         }
00239         else
00240         {
00241             $this->oDB->query( "REPLACE INTO `".$this->oDB->tblMapData."` SET `building`='".$intTownType."', `x`='$x', `y`='$y'".$GLOBALS['strUpdateString'].";");
00242         }
00243         $this->oDB->query( "SELECT x,y FROM `".$this->oDB->tblMap."` WHERE `x`='$x' AND `y`='$y';");
00244         $aRows = $this->oDB->fetch_array();
00245         if(  $this->oDB->affected_rows() < 1 )
00246         {
00247             $this->oDB->query("INSERT INTO ".$this->oDB->tblMap." SET `x`='$x', `y`='$y', `terrain`='0';");
00248         }
00249         return true;
00250     }
00251 
00258     function delete( $x, $y )
00259     {
00260         if( !$this->oUser->check( $x, $y ) )
00261         {
00262             // user has no access to this field
00263             return false;
00264         }
00265 
00266         $this->get( $x, $y );
00267         if( $x == $this->aField['x'] && $y == $this->aField['y'] && $this->aField['town_id'] > 0 )
00268         {
00269             $this->oTown->delete( $this->aField['town_id'], $x, $y );
00270             return true;
00271         }
00272         // field/town did not exist
00273         return false;
00274     }
00275 }

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