Author Topic: Helpful Advice :: Reloaded  (Read 1714 times)

Offline codestryke

  • Administrator
  • Level 33
  • *****
  • Posts: 589
  • Reputation: +22/-0
    • View Profile
    • eXtremeCast Games
Helpful Advice :: Reloaded
« on: September 24, 2006, 12:39:55 PM »
Since my original topic got trunicated with the switch over to SMF I'll repost it :) Nice forum software btw, been testing it myself since I hate phpBB with a passion LOL.

Two great code libraries you should learn and use before you start a gaming project are:
Smarty Template System http://smarty.php.net
ADODB Database Abstraction Layer http://adodb.sourceforge.net

When I begin a project I always start with 3 files that I put in my include directory:
config.server.php
config.game.php
common.lib.php

config.server.php
Hold specific information about the sever like file paths and database connections. You put these in a seperate file set them on your dev server, then on your web host then you should never have to touch the files again :) Since mose dev servers don't share the same connection or directory structures as your web host thats why you want to put them in a seperate file.

config.game.php
This file holds all your defines and constants for the game. This way you only have to open a single file to make adjustments to the game instead of opening a bunch of files and making edits.

common.lib.php
In this file you include both the above files then on every page of your game all you have to do is have a single include call to common.lib.php and your all setup with your server specific definitions and game constants.

I also like to thow in some common functions into the common.lib.php file. Ya know the ones that you are going to need in just about anything you program.. Here I'll share what my common.lib.php looks like:

Code: [Select]
<?
  require_once("config.server.php");
  require_once("config.game.php");
  require_once("adodb/adodb.inc.php");
  require_once("smarty/Smarty.class.php");

  // Create SMARTY class instance
  // See config.server.php for TPL_DIR definition
  $tpl = new Smarty;
  $tpl->template_dir = TPL_DIR.'templates/';
  $tpl->compile_dir  = TPL_DIR.'templates_c/';
  $tpl->config_dir   = TPL_DIR.'configs/';
  $tpl->cache_dir    = TPL_DIR.'cache/';

  // Create ADODB class instance
  // See config.server.php for DB Settings
  $db = ADONewConnection('mysql'); # eg 'mysql' or 'postgres'
  $db->Connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);

  // Start random number generator
  mt_srand ((double) microtime() * 1000000);

  function getmicrotime(){
    list($usec, $sec) = explode(" ",microtime());
    return ((float)$usec + (float)$sec);
  }

  function isnumber($amount, $allowzero = false) {
    if( $amount < 0 || stristr($amount,".") !== false || !is_numeric($amount) ) return false;
    if( !$allowzero && $amount == 0) return false;
    return true;
  }
?>

The function getmicrotime is handy if you want to display how long a function of page generation is taking.

The isnumber function is used when testing input from the browser NEVER trust input from the browser!!!! Some people just love exploiting code so you have to make sure you check ANY AND ALL input. By default the isnumber will return FALSE (not a valid number) when checking 0. The reason I do this is 99.9% of the items I'm checking for are ID's generated by the database and ID's always start with 1 never zero ;) However once in a while 0 is a valid input so then passing it a true will allow 0 to be a valid number.


Hope this helps you on your journey to coding great games....

Creating online addictions, one game at a time:

Offline 420laner666

  • Level 13
  • *
  • Posts: 102
  • Reputation: +3/-6
    • View Profile
Re: Helpful Advice :: Reloaded
« Reply #1 on: September 24, 2006, 01:37:27 PM »
wow another great post well done man...imtrying to create a game and theese to post have really helped thanks

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: Helpful Advice :: Reloaded
« Reply #2 on: September 27, 2006, 10:26:36 AM »
Nice tips :)
That's some great organization. My files are never so organized  ???

Offline Sava

  • Level 13
  • *
  • Posts: 101
  • Reputation: +3/-15
  • It's just me
    • View Profile
Re: Helpful Advice :: Reloaded
« Reply #3 on: September 28, 2006, 05:22:07 PM »
yep that's the professional way .. and that's the way I do it to.
I've even made a blog script and it uses only 1 file :D but it doesn't have categories, archives or links.
Just to post ideeas to your site ... and it's flatfile also :D

Offline smiley

  • Level 13
  • *
  • Posts: 101
  • Reputation: +0/-7
    • View Profile
Re: Helpful Advice :: Reloaded
« Reply #4 on: September 30, 2006, 09:03:42 AM »
thanks for the help dudes:)

Offline Mgccl

  • Level 7
  • *
  • Posts: 33
  • Reputation: +1/-0
    • View Profile
    • WebDevLogs
Re: Helpful Advice :: Reloaded
« Reply #5 on: November 09, 2006, 10:10:11 PM »
I think smarty are cool too....
but I still learns not to use
ADODB Database Abstraction Layer
Reason?
MySQL is the only I will need... not possible for me to change to something else...

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: Helpful Advice :: Reloaded
« Reply #6 on: November 10, 2006, 02:24:35 AM »
AdoDB also helps make some queries simpler...
Like the autoexecute function, and automatically cleaning your strings using a different sql format.
Also helps to reduce the query count.

Offline codestryke

  • Administrator
  • Level 33
  • *****
  • Posts: 589
  • Reputation: +22/-0
    • View Profile
    • eXtremeCast Games
Re: Helpful Advice :: Reloaded
« Reply #7 on: November 10, 2006, 04:49:35 PM »
I think smarty are cool too....
but I still learns not to use
ADODB Database Abstraction Layer
Reason?
MySQL is the only I will need... not possible for me to change to something else...
I don't think you understand what ADODB is with that post.. ADODB is an abstraction layer that allows you to use many different databases. Instead of raw mysql commands you use the abstraction layer so you are not tied down to mysql.

To each his own but I'm not putting all my eggs in one database basket! Lot of open source companies are going into corporate sponsorships, getting bought out by for profit businesses and such.  If the time came I needed to jump ship because mySQL started charging or anything my games would be down for 3 days tops for a tweak or two because I use ADODB ;)

Plus, in the end, ADODB is a MUCH simpler syntax :)

Creating online addictions, one game at a time:

Offline Mgccl

  • Level 7
  • *
  • Posts: 33
  • Reputation: +1/-0
    • View Profile
    • WebDevLogs
Re: Helpful Advice :: Reloaded
« Reply #8 on: November 10, 2006, 05:29:59 PM »
I know...
Yes.. one day maybe MySQL won't be that hot...
and people uses ADODB will start LOLing like hell...
But.. can't bother to use something else. Because someday PHP will be taken away by something else... and I don't write an engine can work on any system and write my own script

Offline Broda

  • Level 13
  • *
  • Posts: 97
  • Reputation: +2/-0
    • View Profile
    • Nightfall Games
Re: Helpful Advice :: Reloaded
« Reply #9 on: March 22, 2007, 10:35:52 PM »
Great post CS!

I'm glad they made the ADO libraries. I write all of my software at work with VB6 + ADO for db access. This is going to make learning PHP that much simpler (though I have written a couple PHP, db-driven sites - none as extreme as a pbbg though).

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal