Our Scripts Vault contains many game scripts that you can use to create your own game!
Still though... as has been said... it's not the best way of doing it. Columns FTW.
Would it be a good idea to have your users table with peritanant info then another table with columns of stats... and matching id's or just throw all the stats in columns on the users table?
Then, another question would be... does it lower performance when loading several rows over and over or when you have too many columns? With that Tutorial... I also thought it was saying that basically at the heart of it all... "we do not want a bunch of columns in our table because it make the game slower"?
I have around 21 stats, per user and about that per 'monster', then about 10 stats per item in the game... would it make sense to do a seperate table for stats in columns for the users so i do have a table with 30 columns haha?
If you mean like a users table, then like a users_profile table with stuff like forum_signature, profile_blurb, AIM etc linked by a userid, I think yes. The profile data won't be called often so it's OK to just select it from that other table when needed IMO.I just wouldn't do it as a users_attributes table with columns attribute, value and then rows that have things like signature -> value, blurb -> value etc as the original article suggests.
One would need to benchmark, but I'd imagine selecting one row from a table based on userid is better than selecting many rows from a table and linking them by userid.
I'd do it that way... have a table called monsters and all their stats as columns. If all monsters will have it then that's the way to go.A scenario where I'd use the original article's method would be when I have something like a categories table, and even though each category may have multiple subcategories, I wouldn't put them as columns, I'd have them as their own entry and linked by a parentid column.
It makes the table a bit longer (column-wise) but since I'm pretty much using most, if not all, of those fields on each page, why not.
function getStat($statName,$userID) { require_once 'config.php'; $conn = mysql_connect($dbhost,$dbuser,$dbpass) or die ('Error connecting to mysql'); mysql_select_db($dbname); $query = sprintf("SELECT value FROM user_stats WHERE stat_id = (SELECT id FROM stats WHERE display_name = '%s' OR short_name = '%s') AND user_id = '%s'", mysql_real_escape_string($statName), mysql_real_escape_string($statName), mysql_real_escape_string($userID)); $result = mysql_query($query); list($value) = mysql_fetch_row($result); return $value; }
function getStat($userID) { require_once 'config.php'; $conn = mysql_connect($dbhost,$dbuser,$dbpass) or die ('Error connecting to mysql'); mysql_select_db($dbname); $query = sprintf("SELECT value1, value2 FROM user_stats WHERE stat_id = (SELECT id FROM stats WHERE value1Name = 'value1Namey' AND value2Name = 'value2Namey') AND user_id = '%s'", mysql_real_escape_string($userID)); $result = mysql_query($query); list($value) = mysql_fetch_row($result); return $value; }
Well ultimately it comes down to how your code is set up. I have a user class, and when it's initialized (and passed a user ID which is cleaned to an INT) it just goes:Then the variables are stored in my user class and any time I need to access them within my code I can simply go:$attack = $user->strength - $opponent->defense;You'll notice that the class is generic so if I'm having a battle I can instantiate two user classes ($user and $opponent), pass them individual IDs and then just use them. This way you fetch the data ONCE and use it when needed inside the code.
class User { var $username; var $strength; var $defense; function User(){ $this->initStats($session->userid); } function initStats($userid){ $query = sprintf("SELECT username, strength, defense FROM users WHERE id = $userid"); $result = mysql_query($query); $stats = mysql_fetch_assoc($result); $this->username = $stats['username']; $this->strength = $stats['strength']; $this->defense = $stats['defense']; }};$user = new User;
I really like that idea... would it be something like this:Code: [Select]class User { var $username; var $strength; var $defense; function User(){ $this->initStats($session->userid); } function initStats($userid){ $query = sprintf("SELECT username, strength, defense FROM users WHERE id = $userid"); $result = mysql_query($query); $stats = mysql_fetch_assoc($result); $this->username = $stats['username']; $this->strength = $stats['strength']; $this->defense = $stats['defense']; }};$user = new User;
function User($id) { $this->initStats($id);}
Then to init the class it would be$player = new User($_SESSION['userid']);Then during the battle you then can do$opponent = new User($opponentid);
Notice: Undefined variable: _SESSION in \www\include\user.php on line 76QUERY: SELECT * FROM stats WHERE id = ''Notice: A session had already been started - ignoring session_start() in \www\include\user.php on line 34QUERY: SELECT * FROM stats WHERE id = '0'Notice: A session had already been started - ignoring session_start() in \www\include\user.php on line 34QUERY: SELECT * FROM items WHERE id = '0'Notice: A session had already been started - ignoring session_start() in \www\include\user.php on line 34QUERY: SELECT * FROM targets WHERE id = '0'Notice: A session had already been started - ignoring session_start() in \www\include\session.php on line 27
$id = 0;$player = new Stats($_SESSION['userid']);$opponent = new Stats($id);$item = new Stats($id);$target = new Stats($id);