...

View Full Version : How much is too many..



funnymoney
08-30-2009, 09:01 PM
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...

Scriptet
08-30-2009, 09:19 PM
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.

funnymoney
08-30-2009, 09:34 PM
do you think this code can be made shorter


<?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(0, count($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'] == 0 || $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;
}

}


?>

funnymoney
08-30-2009, 09:41 PM
oh, and if you want database class here it is


<?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
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;
?>

the-dream
08-30-2009, 10:04 PM
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.)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum