11.30.06

Game Creation with PHP - Our Database

Posted in PHP at by jason.michael

In selecting a database for our game, I wanted to make sure I choose something that was robust enough to handle the job.  I decided to go with MySQL 5.x, but think I may regret it due to overhead problems.  I know there are things that can be done to tweak the system, but maybe I’ll address those issues later.

 I also decided to create a custom interface that would make managing the game tables in the database easy to administer.  And that is what I’ll refer to them as - game tables.  So far I have tables for players, player items, locations, and items.  The way I setup these tables is kind of crazy and really hard to explain.  I have a players table which is nothing but the fields I think I’m going to use in the game.  these are just records containing and ID field and a name field.  Then I have a table called players_data which ties to the `players` table to get the field names for each player record.  Player’s data has the records of id, recordid, fieldid (from players table), and value (the value of the field in the record).  Pretty nuts eh?  Well, this allows me to quickly and easily design the data structure for the game, and I forgot to mention, I also have a pID (parent ID) field in the Players table, so I can categories the fields in a tree structure.  I’m using a script I found online to accomodate the creation of the tree.

I also created some custom database functions to deal with all the selecting, updating, inserting, and deleting of records.  The select functions used to determine the players x,y coordinates on the map are pretty interesting.  I have to basically get two arrays of x and y results and then get the intersecting ‘point’ using an array_intersect_assoc command.  I was afraid with these extra queries and the craziness of my array manipulations used to support my game design program, the performance might be slow, but it works amazingly fast.  I’m sure it will be faster after I flaten out the database structure and use regular queries to handle the bulk of the work. 

I’ll try to get some more code and information listed here soon.  I actually am further ahead on the game than I am on this series, but I might as well make sure that whatever ideas I’m going to talk about here are actually working first before wasting my time talking about it.

 

11.25.06

Amazon API Affiliate Script using PHP - Version 2 of a bad posting

Posted in PHP at by jason.michael

I just want to apologize to the Blogosphere, or whatever you call yourselves, for this additional post.  I am having technical difficulties with Wordpress which have cost me hours of time.  Anyway, here’s what I was trying to say earlier:

Lets get down to the substance:  I have for you some rough code that I setup to setup my own Amazon Affiliate store, using the Amazon API.  I thought I would try my hand at some affiliate marketing - so far its been okay.  I picked up a copy of this book that explains how to use the various API services available from Amazon, Fedex, UPS,  Google, and others.  I think its a great book if you want to setup some custom shopping cart software to take orders and allow for customers to track their shipments.  Its also good for me, as I don’t have a store yet to speak of, and will have to settle for affiliate marketing of the Amazon products.

To use this code, you’re going to need the ‘nusoap’ Soap library for PHP to use it with the Amazon API.  It allows you to use SOAP services without having to have SOAP installed on your system.  I tried installing SOAP years ago, and that was a pain.  So save yourself some hassle and use this instead.

If you would like to see this code in action, I have it in use online at my Christmas Gifts and Crafts site.

Forget posting a code snippet - Wordpress 2 couldn’t handle it and I tried numerous plugins - none helped the situation.

Download the zipped index.php, styles.css and images folder here:

Amazon Affliliate Code version 1

 

11.23.06

Import MySQL script file with your own PHP script to Import MySQL

Posted in PHP at by jason.michael

Okay, I use PHPMyAdmin for all of my MySQL administration.   I’ve learned that there may be better tools available, but I have been hooked on using PHPMyAdmin.  I have a big gripe with it though - if you want to import an sql script for execution, you must browse for a file to upload which limits you to a set number of bytes (around 2 MB I think) OR you can copy and paste the SQL into the textarea.  None of these options worked for me, because the amount of SQL code I had amounted to well over 2MB. 

I ‘Googled’ the term ‘php mysql import script’.  Did I find anything useful?  Not really.  Just alot of folks asking the same question, or tools that are integrated in other applications.  Boy that really stunk.  So I had to go ahead and write my own utility.  Here’s the code below. 

  • Note that I didn’t include a listing of my ‘common_new.php’ file - this is only a file that contains the dbconnect() function, which does nothing but connect me to my database with my login credentials - you can create your own library of functions to include)
  • Also note that I have some search patterns setup to massage my SQL data to work with my version of MySQL, you might use these examples to make additional changes.
  • Finally, also note that this is setup to drop the tables before reimporting them!  If you don’t want to lose tables and maybe just run a script that only does INSERT statements, then comment out the line: $page=$prepage.$page;  But if you have no CREATE TABLES statement to begin with, it shouldn’t matter - as I have this program setup, it only wants to drop tables that already are being created in your SQL

 

< ?

include ("common_new.php");

$filename="transaction_sheeet_dev.sql";

$page=file_get_contents($filename);

$pattern="/CREATE TABLE \`(.*?)\` \(/si";
preg_match_all($pattern,$page,$matches);

foreach ($matches[1] as $k=>$v)
{
 $prepage.=”DROP TABLE IF EXISTS`$v`;”;
}
// TAKE OUT THIS LINE IF YOU DON’T WANT TO DROP THE TABLES
$page=$prepage.$page;

$pattern=”/CREATE TABLE /si”;
$replace=”CREATE TABLE IF NOT EXISTS “;
$page=preg_replace($pattern,$replace,$page);

$pattern=”/\`timestamp\` timestamp NOT NULL .*?\,/si”;
$replace=”`timestamp` timestamp NULL,”;
$page=preg_replace($pattern,$replace,$page);
unset($matches);
$pattern=”/InnoDB/i”;
$replace=”MyISAM”;
$page=preg_replace($pattern,$replace,$page);
unset($matches);$pattern=”/latin1/i”;
$replace=”utf8″;
$page=preg_replace($pattern,$replace,$page);
unset($matches);
$pattern=”/(.*?)\;/si”;
$r=preg_match_all($pattern,$page,$matches);
$db=dbconnect();
foreach ($matches[1] as $k=>$v)
{
 $sql=$v;
 echo “executing:”. $v.”
“;
 $rst=mysql_query($sql,$db);
 //if (!$rst) die(”could not execute: $sql
MySQL said:”.mysql_error());
 
}
if (count($matches[1])==0) die(”found no matches”);

?>

 

 

Game Creation with PHP - Introduction

Posted in PHP at by jason.michael

If there is one thing I have been wanting to do, but somehow never had the time, was to create a turn based game written in PHP.  A couple years ago, I actually came close to having a good framework created, which allowed the player to move around a generated map.  It took me an hour to get that far, but then I was stumped trying to decide how to store the items the player would pick up, and how to store the information about each building on the map.  There are so many factors that need to be considered for one of these games to be successful. 

I have finally decided that in order to put together a decent framework for this game, what is needed is a good plan.  So I started to write some things down and make the decisions I could not make two years ago, and so far, here is what I have:

The game would be similar to Urban Dead at urbandead.com

The players table will have fields with information directly related to the player:

  • name
  • password
  • location on the map
  • player class type
  • health points
  • experience points
  • action points
  • timezone
  • email
  • last login time
  • created date
  • number of logins
  • disabled flag
  • IP Address

The players items would be stored in a seperate table in a category type structure in which you have a parent item, with its associated child items, and each item would have some attributes such as hit point effectiveness (I will have to research a better term for this from other games), which is the minimum and maximum on both hit points multiplied by max and min percentage.  The value could be negative (for weapon items) and positive (for healing items). 

I want to give some more thought to the player items data structure and design, and will layout these details in my next posting.  I hope you can appreciate the value in planning these database details first.  It will make coding the game alot more fun.

Setup of this site

Posted in General Site Stuff at by jason.michael

If you’re like me, which I hope you’re not, you have all of your sites hosted on a server that you’re co-locating, and have hundreds of accounts setup with abbreviations that you have to look up in an excel file, to keep everything straight.  Well, my system bit me, as I spent 2 hours trying to figure out which account was tided into the domain for internet-computer-helper-mi.com. 

The two accounts I *thought* were for possibilities for this domain, I thought I had narrowed it down to one or the other, several times, till I finally logged into my shell account, and renamed the index.php to index.phpz, and realized I was still getting the default page, even though the index.php file was renamed.

Anyway, I figured out which account was really tied into this blog, and I can now start posting to it.

For all of you in the USA, I hope you have a Happy Thanksgiving - I know I will.

-Jason