Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts

    How much is too many..

    I've been typing some code for my program these last few weeks. Maybe i could do it faster or better, but step by step program does what it's supposed to do.

    No i mean, program is not 10% finished for now, and i already have 50KB of code so far..

    Is that to much, i mean, really lot's of typing is involved here, and i'm just trying to figure out is that a normal thing...

  • #2
    Regular Coder
    Join Date
    Apr 2008
    Posts
    685
    Thanks
    15
    Thanked 105 Times in 104 Posts
    Well it's hard to say really, as each individual project will have a varying amount of code needed for it.

    For example coding a complete CMS say like WordPress will require considerably more code than a simple plugin.

    That said there are many possibilities and different ways of coding a solution, maybe through the use of a different function, using a loop etc. So code can be cut down by using the most optimized, effective solution.

    By learning PHP in all it's detail you can easily pick up all the different methods and choose a correct one to apply to the requirements.

    For a more specific answer you'd have to say what your actually coding, or post up a snippet of what you think could be longer than you should be doing.

    If it's doing it's job then fair enough, but use of inapprorpiate methods can delay execution time.

  • #3
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    do you think this code can be made shorter

    PHP Code:
    <?php

    class Battle {
        public function 
    __construct($player) {
            
    $this->database = new Database;
            
            
    $this->player     $player;
            
    $this->brojigraca $player['korisnik'];
        }
        
        public function 
    startbattle($enemy) {
            
    $brojigraca $this->player['korisnik'];
            
    $enemy        $this->database->selectallwhere("cudovista""broj""where tip = $enemy");
            
    $key        rand(0count($enemy['broj'])-1);
            
    $enemy        $enemy['broj'][$key];

            
    $this->database->update("igraci""battle"$enemy"where korisnik = $brojigraca");
            
    $this->database->insertdata("battles""igrac,protivnik""'$brojigraca', '$enemy'");
            
            
    $this->updateAIinfo($brojigraca$enemy);
        }
        
        public function 
    updateAIinfo($brojigraca$enemy) {
            
    $enemy        $this->database->selectwhere("cudovista""napad,obrana,zpoeni""where broj = $enemy");
            
    extract ($enemy);
            
    $this->database->newupdate("battles""napad = '$napad', obrana = '$obrana', zpoeni = '$zpoeni'""where igrac = $brojigraca");
            
    $this->sansazanapad($brojigraca);
        }
        
        public function 
    sansazanapad($brojigraca) {
            
    $protivnikakcija     rand(0,100);
            
    $igracakcija        rand(0,100);
            
            
    /*if ($igracakcija >= $protivnikakcija) {
                $akcija = $igracakcija;
            }
            elseif ($igracakcija < $protivnikakcija) {
                $akcija = $protivnikakcija;
            }*/
            
            
    $this->database->newupdate(    "battles"
                                        
    "protivnikakcija = '$protivnikakcija', igracakcija = '$igracakcija'"#, akcija = '$akcija'
                                        
    "where igrac = $brojigraca");
            
    $this->checkall($brojigraca);
        }
        
        public function 
    checkall($brojigraca) {
            
    $battles $this->database->selectallwhere("battles""broj""where igrac = $brojigraca");
            if (
    $battles == 0) {
                
    $this->database->update("igraci""battle"0"where korisnik = $brojigraca");
                
    header("Location: ?method=2");
            }
            return 
    0;
        }
        
        
        public function 
    fightoption($battleinfo) {
                            
                            
    extract($battleinfo);
        
            
    $AI         = new AIAttacks($this->player$protivnik);
            
    $player        = new Attacks($this->player$protivnik);
            
            if (
    $akcija == 0) {
                
    $akcija     =     $AI->akcija($broj);
                
    $akcija    .=    $player->akcija($broj);
                 
    /*mogućnosti igrača*/ $akcija .= $player->prikazimogucnosti();
            }        
            elseif (
    $igracakcija == $akcija) {
                
    $akcija     =     $AI->akcija($broj);
                
    $akcija    .=    $player->akcija($broj);
                 
    /*mogućnosti igrača*/ $akcija .= $player->prikazimogucnosti();
            }
            elseif(
    $protivnikakcija == $akcija) {
                
    $akcija     =    $AI->akcija($broj);
                
    $akcija    .=    $player->akcija($broj);
                    
    $akcija .= $player->prikazimogucnosti();
            }
            
                
    $doing    $AI->prikazi();
                
    $doing .= $akcija;
                
    $doing .= $player->prikazi();
            
            if (
    $this->player['zpoeni'] == || $battleinfo['zpoeni'] == 0) {
                
    $doing $this->battleover($broj);
            }
            
            return 
    $doing;
        }
        
        public function 
    battleover($brojborbe) {
            
    $battleoverhtml file_get_contents("html/borba/battleoverhtml.html");
            
            
    $this->database->update("igraci""battle"0"where korisnik = $this->brojigraca");
            
    $this->database->delete("battles""where broj = $brojborbe");
            return 
    $battleoverhtml;
        }
        
    }


    ?>

  • #4
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    oh, and if you want database class here it is

    PHP Code:
    <?php

    class Database {
        public function 
    connect($host$name$pass) {
            
    $connection mysql_connect($host$name$pass) or die(mysql_error());
            return 
    $connection;
        }
        public function 
    usedatabase($databasename) {
            return 
    mysql_select_db($databasename) or die(mysql_error()."method usedatabase");
        }
        
        public function 
    selectdata($tablename$fields) {
            
    $sql = ("SELECT $fields FROM $tablename");
            
    $query mysql_query($sql) or die(mysql_error()."method selectdata 1");
            
            if (!
    $query) {
                return 
    "Could not successfully run query ($sql) from DB: " mysql_error()."method selectdata";
                exit;
            }

            if (
    mysql_num_rows($query) == 0) {
                return 
    0;
                exit;
            }
            if(
    preg_match("/,/"$fields)) {
            
    $fields explode(","$fields);
            
    #print_r($fields);
            
    }
            while (
    $row mysql_fetch_assoc($query)) {
                if (
    is_array($fields)) {
                    foreach (
    $fields as $field) {
                        
    $rezultati[$field][] = $row[$field];
                    }
                }
                else {
                        
    $rezultati[$fields][] = $row[$fields];
                }
            
            }
            
    mysql_free_result($query);
            return 
    $rezultati;
        }
        
        public function 
    selectwhere($tablename$fields$where) {
            
    $sql = ("SELECT $fields FROM $tablename $where");
            
    $query mysql_query($sql) or die(mysql_error()."select where 1");
            
            if (!
    $query) {
                return 
    "Could not successfully run query ($sql) from DB: " mysql_error()."select where 2";
                exit;
            }

            if (
    mysql_num_rows($query) == 0) {
                return 
    0;
                exit;
            }
            if(
    preg_match("/,/"$fields)) {
            
    $fields explode(","$fields);
            
    #print_r($fields);
            
    }
            while (
    $row mysql_fetch_assoc($query)) {
                if (
    is_array($fields)) {
                    foreach (
    $fields as $field) {
                        
    $rezultati[$field] = $row[$field];
                    }
                }
                else {
                        
    $rezultati[$fields] = $row[$fields];
                }
            
            }
            
    mysql_free_result($query);
            return 
    $rezultati;
        }
        
        public function 
    selectallwhere ($tablename$fields$where) {
            
    $sql = ("SELECT $fields FROM $tablename $where");
            
    $query mysql_query($sql) or die(mysql_error()."select where 1");
            
            if (!
    $query) {
                return 
    "Could not successfully run query ($sql) from DB: " mysql_error()."select where 2";
                exit;
            }

            if (
    mysql_num_rows($query) == 0) {
                return 
    0;
                exit;
            }
            if(
    preg_match("/,/"$fields)) {
            
    $fields explode(","$fields);
            
    #print_r($fields);
            
    }
            while (
    $row mysql_fetch_assoc($query)) {
                if (
    is_array($fields)) {
                    foreach (
    $fields as $field) {
                        
    $rezultati[$field][] = $row[$field];
                    }
                }
                else {
                        
    $rezultati[$fields][] = $row[$fields];
                }
            
            }
            
    mysql_free_result($query);
            return 
    $rezultati;
        }
        
        public function 
    leftjoin($lefttable$righttable$matchedrow$fields$order) {
            
    $sql = ("SELECT $fields FROM $lefttable LEFT JOIN $righttable ON $lefttable.$matchedrow = $righttable.$matchedrow $order");
            
    $query mysql_query($sql) or die(mysql_error()."method selectdata left join");

            if (!
    $query) {
                return 
    "Could not successfully run query ($sql) from DB: " mysql_error()."method selectdata";
                exit;
            }

            if (
    mysql_num_rows($query) == 0) {
                return 
    0;
                exit;
            }
            
            if(
    preg_match("/,/"$fields)) {
            
    $fields explode(","$fields);
            
    #print_r($fields);
            
    }
            while (
    $row mysql_fetch_assoc($query)) {
                if (
    is_array($fields)) {
                    foreach (
    $fields as $field) {
                        
    $rezultati[$field][] = $row[$field];
                    }
                }
                else {
                        
    $rezultati[$fields][] = $row[$fields];
                }
            
            }
            
            
            
    mysql_free_result($query);
            return 
    $rezultati;
            
        }
        
        public function 
    insertdata($tablename$fields$values) {
            return 
    mysql_query(
            
    "INSERT INTO $tablename ($fields) VALUES ($values)"
            

                or die(
    mysql_error());
        }
        public function 
    update($tablename$field$value$where) {
            return 
    mysql_query("UPDATE $tablename SET $field = $value $where") or die(mysql_error());
        }
        public function 
    newupdate($tablename$values$where) {
            return 
    mysql_query("UPDATE $tablename SET $values $where") or die(mysql_error());
        }
        public function 
    delete($tablename$where) {
            return 
    mysql_query("DELETE FROM $tablename $where");
        }
    }


    ?>
    index.php

    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    session_start();
    include(
    "php/database.php");
    include(
    "php/sessions.php");
    include(
    "php/visitors.php");
    include(
    "php/postmethods.php");
    include(
    "php/getmethods.php");
    include(
    "php/player.php");
    include(
    "php/gamefunctions.php");

    include(
    "php/playerattributes.php");
    include(
    "php/playeritems.php");
    include(
    "php/maploader.php");
    include(
    "php/oruzarnica.php");
    include(
    "php/playerpower.php");
    include(
    "php/battle.php");
    include(
    "php/attacks.php");
    include(
    "php/aiattacks.php");

    ##Explained in database.php file
    $connection = new Database();
    ##
    $connection->connect("localhost""""");
    $connection->usedatabase("");
    #

    ##Explained in postmethods.php file
    $postmethod     = new Postmethods(@$_POST['ime'], htmlentities(@$_POST['sifra']));
    ##

    $post_methods = array(
        
    "login",
        
    "register"
    );


    ##Explained in user.php file
    $visitor         = new Visitors();
    ##


    if (empty($_SESSION['korisnik'])) {
        if (!empty(
    $_POST)) {
            foreach (
    $post_methods as $post_type) {
                switch (isset(
    $_POST[$post_type])) {
                    case 
    $post_type:
                        
    $postmethod->$post_type();
                    break;
                }
            }
        }
        else {
            
    $stranica $visitor->visitor("gost""", @$_GET['register']);
        }
    }
    else {
            if (!empty(
    $_GET)) {
                
    ##Explained in getmethods.php file
                
    $getmethod         = new Getmethods($_SESSION['broj']);
                
    ##
                
    if (isset($_GET['method'])) {
                    
    $stranica $visitor->visitor("korisnik"""$getmethod->method($_GET['method']));
                }
                else {
                    
    $stranica $visitor->visitor("korisnik"""$getmethod->method(0));
                }
            }
            else {
                
    $stranica $visitor->visitor("korisnik""""");
            }
    }
    print 
    $stranica;
    ?>
    Last edited by funnymoney; 08-30-2009 at 08:46 PM.

  • #5
    Regular Coder the-dream's Avatar
    Join Date
    Mar 2007
    Location
    Northamptonshire, UK
    Posts
    477
    Thanks
    8
    Thanked 4 Times in 4 Posts
    It looks like quite clean code to me, I mean, I've seen a lot worse. Obviously, with most code there will be some parts that can be optimised.

    Some of my sites, have over 3MB in just code files. For example, ad servers, and ad targeting systems, require a lot of logic and they're big. (I code an ad server.)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •