wie sieht guter code in php aus?



  • hallo!
    wie sieht guter php code aus?



  • Es gibt nur 1 PHP Code 😃 😃 😃



  • Original erstellt von Razy:
    Es gibt nur 1 PHP Code 😃 😃 😃

    hä?!? deine antwort kapiere ich nicht



  • So sieht mein Style aus. Ich find den super !!!

    <?//***********************************************************
    //  CTerraner.php   
    //*************************************************************
    
    Class CTerraner extends CBaseRace
    {
        var $m_obProductions = NULL;
    
        //////////////////////////////////////////////////////////////
        // UpdatePlanets
        //
        //////////////////////////////////////////////////////////////
        function UpdatePlanets( $time )
        {
            //echo( "<br>Planet wird bist zur Zeit ". strftime( "%c", $time ) . " geupdatet!" );
            //echo( "<br><br>" );
    
            $Userdaten = NULL;
    
            // Lese Daten des Users aus
            $qresult = mysql_query( "SELECT * FROM $this->m_tbMain WHERE owner_id='$this->m_iUserId' " );
            while( $daten = mysql_fetch_array( $qresult ) )
            {
                // Speicher Daten
                $Userdaten = $daten;
            }
    
            // set/save race
            if( $this->m_iRace == 0 )
            {
                $this->m_iRace = $Userdaten['race'];
            }
    
            // Berechne Zeit in Sekunden des letzten  Uodates
            $iTimestamp = $time-$Userdaten['timestamp'];
    
            // Falls Updatezeit 0 oder kleiner beträgt, breche Update ab
            if( $iTimestamp <= 0 )
            {
                //echo( "INFO: Die Update Zeit beträgt " . $iTimestamp . ". Das Update wird abgebrochen." );
                //return false ;
            }
                // Update Planeten
            $qresult = mysql_query( "SELECT * FROM $this->m_tbPlanets WHERE owner_id='$this->m_iUserId' ORDER BY id ASC" );
            while( $Planetdaten = mysql_fetch_array( $qresult ) )
            {
                 //echo( "<br> ---------  Planet <b>". $Planetdaten['name'] . "</b>---------------<br>" );
                 $this->m_obProductions = new CTerranerProductions();
    
                 // define sructures
                 $tyResources = new RESOURCES;
    
                 $tyBuildings   = new BUILDINGS;
                 $tyResearches  = new RESEARCHES;
                 $tyShips       = new SHIPS;
    
                 $tyBuildingProduction = new PRODUCTION;
                 $tyResearchProduction = new PRODUCTION;
                 $tyShipProduction = new PRODUCTION;
    
                 // read exsists buildings, researches, ships
                 $tyBuildings  = $this->m_obUtil->BUILDINGS_FROM_DB( $Planetdaten['buildings']  );
                 $tyResearches = $this->m_obUtil->RESEARCHES_FROM_DB( $Userdaten['researches'] );
                 $tyShips      = $this->m_obUtil->SHIPS_FROM_DB( $Planetdaten['ships'] );
    
                 //read resources
                 $tyResources  = $this->m_obUtil->RESOURCES_FROM_DB( $Planetdaten['resources'] );
    
                 //read current productions
                 $tyBuildingProduction = $this->m_obUtil->PRODUCTION_FROM_DB( $Planetdaten['production_building'] );
                 $tyResearchProduction = $this->m_obUtil->PRODUCTION_FROM_DB( $Planetdaten['production_research'] );
                 $tyShipProduction     = $this->m_obUtil->PRODUCTION_FROM_DB( $Planetdaten['production_ship'] );
    
                 if( $iTimestamp > 0 )
                 {
                    /* this have to be executed if the timestamp ist bigger then 0 !  IMPORTANT::
                    */
                    //update  production & resource update
                    $this->m_obProductions->Update( &$tyBuildings, &$tyResearches, &$tyShips, &$tyResources,
                                                 &$tyBuildingProduction, &$tyResearchProduction, &$tyShipProduction,
                                                 $iTimestamp );
    
                    // write data for writing into db
                    $szBuildings  = $this->m_obUtil->BUILDINGS_TO_DB( $tyBuildings );
                    $szResearches = $this->m_obUtil->RESEARCHES_TO_DB( $tyResearches );
    
                    //  $szShips      = $this->m_obUtil->SHIPS_TO_DB( $tyShips );
                    $szResources          = $this->m_obUtil->RESOURCES_TO_DB( $tyResources );
                    $szBuildingProduction = $this->m_obUtil->PRODUCTION_TO_DB( $tyBuildingProduction );
                 }
    
                 // save planet dates
                 $this->m_tyPlanets[$this->m_iNumPlanets]->Resources  = $tyResources;
                 $this->m_tyPlanets[$this->m_iNumPlanets]->Buildings  = $tyBuildings;
                 $this->m_tyPlanets[$this->m_iNumPlanets]->Researches = $tyResearches;
                 $this->m_tyPlanets[$this->m_iNumPlanets]->Ships      = $tyShips;
    
                 $this->m_tyPlanets[$this->m_iNumPlanets]->BuildingProduction = $tyBuildingProduction;
                 $this->m_tyPlanets[$this->m_iNumPlanets]->ResearchProduction = $tyResearchProduction;
                 $this->m_tyPlanets[$this->m_iNumPlanets]->ShipProduction     = $tyShipProduction;
                 $this->m_tyPlanets[$this->m_iNumPlanets]->Db                 = $Planetdaten;
    
                 /*
                // ---------------- Test ausgabe ------------------
                echo( "<br> -> Planet <b>". $Planetdaten['name'] . "</b>" );
                echo( "<br> ---> Gebäude " );
                echo( "<br> ------- " . $tyBuildings->Metal );
                echo( "<br> ------- " . $tyBuildings->Biomass );
                echo( "<br> ------- " . $tyBuildings->Xylit );
                echo( "<br> ------- " . $tyBuildings->Energie );
                echo( "<br> ------- " . $tyBuildings->Habitat );
    
                // ...
    
                echo( "<br>-> <b>Resources </b> " );
                echo( "<br>-------- " . $tyResources->Metal );
                echo( "<br>-------- " . $tyResources->Biomass );
                echo( "<br>-------- " . $tyResources->Xylit );
                echo( "<br>-------- " . $tyResources->Energie );
                echo( "<br>-------- " . $tyResources->Population );
                echo( "<br>-------- Abweichungen " );
                //echo( "<br>-------- " . $tyResources->Slaves );
                echo( "<br>-------- " . $tyResources->Credits );
    
                // ...
    
                */
                //write in db
    
                 if( $iTimestamp > 0 )
                 {
                         $hresult = mysql_query( "UPDATE $this->m_tbPlanets SET buildings='$szBuildings',
                                                                                production_building='$szBuildingProduction',
                                                                                resources='$szResources'
    
                                                                                WHERE id='$Planetdaten[id]' " );
                         if( !$hresult )
                             echo( "ERROR beim Schreiben in die DB" );
                 }
    
                 // increment planet counter
                 $this->m_iNumPlanets++;
            }
    
            //Write new Data into Main DB
            if( $iTimestamp > 0 )
            {
                    // Speicher bzw Update Gobale Daten
                    $hresult = mysql_query( "UPDATE $this->m_tbMain SET timestamp='$time' WHERE owner_id='$this->m_iUserId' " );
                    if( !$hresult )
                        echo( "ERROR beim Schreiben in die DB" );
            }// if
    
        } // update
    
        //set glaobal ?
    
        //////////////////////////////////////////////////////////////
        // StartBuildingProduction
        //
        //////////////////////////////////////////////////////////////
        function StartBuildingProduction( $PlanetId, $ProductionId, $TotalTime, $Resources )
        {
                 $Planet = $this->m_tyPlanets[$PlanetId];
    
                 // check for resources
                 // Production current exists ?
                 if( $this->m_obUtil->IsProduction( $Planet->BuildingProduction->ProductionId ))
                 {
                     return ERROR_PRODUCTIONEXSISTS;
                 }
    
                 // enought resources ?
                 if( $Planet->Resources->Metal < $Resources->Metal )
                 {
                     return ERROR_NOMETAL;
                 }
    
                 // enought resources ?
                 if( $Planet->Resources->Biomass < $Resources->Biomass )
                 {
                     return ERROR_NOBIOMASS;
                 }
    
                 // enought resources ?
                 if( $Planet->Resources->Energie < $Resources->Energie )
                 {
                     return ERROR_NOENERGIE;
                 }
    
                 // enought resources ?
                 if( $Planet->Resources->Xylit < $Resources->Xylit )
                 {
                     return ERROR_NOXYLIT;
                 }
    
                 // enought resources ?
                 if( $Planet->Resources->Population < $Resources->Population )
                 {
                     return ERROR_NOPOPULATION;
                 }
    
                 // enought resources ?
                 if( $Planet->Resources->Credits < $Resources->Credits )
                 {
                     return ERROR_NOCREDITS;
                 }
    
                 // all ok!!! start production
                 $Production = new PRODUCTION;
                 $Production->ProductionId = $ProductionId;
                 $Production->Timeleft     = $TotalTime;
                 $Production->TotalTime    = $TotalTime;
    
                 // update building production
                 $Planet->BuildingProduction = $Production;
    
                 // update resources
                 $Planet->Resources->Metal      -= $Resources->Metal;
                 $Planet->Resources->Biomass    -= $Resources->Biomass;
                 $Planet->Resources->Energie    -= $Resources->Energie;
                 $Planet->Resources->Xylit      -= $Resources->Xylit;
                 $Planet->Resources->Population -= $Resources->Population;
                 $Planet->Resources->Credits    -= $Resources->Credits;
    
                 // write db format
                 $szResources = $this->m_obUtil->RESOURCES_TO_DB( $this->m_tyPlanets[$PlanetId]->Resources );
                 $szProduction = $this->m_obUtil->PRODUCTION_TO_DB( $Production );
    
                 // write in DB
                 $hresult = mysql_query( "UPDATE $this->m_tbPlanets  SET production_building='$szProduction',
                                                                                    resources='$szResources'
                                                                                    WHERE id='".$Planet->Db['id']."' " );
    
                 if( !$hresult )
                 {
                    echo( "ERROR beim Schreiben in die DB" );
                    return ERROR_DBWRITE;
                 }
                 return ERROR_NO;
        }
    
    }
    
    Grüsse  :sunglasses:
    

Log in to reply