[ALPHA] Items API

A place for MOD Authors to post and receive feedback on MODs still in development. No MODs within this forum should be used within a live environment! No new topics are allowed in this forum.
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

IMPORTANT: MOD Development Forum rules

On February 1, 2009 this forum will be set to read only as part of retiring of phpBB2.
Xore
Former Team Member
Posts: 2594
Joined: Wed Apr 16, 2003 7:42 pm
Location: the wastelands between insomnia and clairvoyance
Contact:

[ALPHA] Items API

Post by Xore »

So, this is a project i've had on the backburner, conceptually, for a long time. I've finally got a bit of time, so i've put together some code, and now i'm blowing it wide open for input from you guys.

Please don't post here just to say 'use mod x instead'. What i'm trying to do here is create a backend for MOD Authors to use, that's much more coder friendly (hence 'API') to people who want to deal with items on their forums.

Please note: i'm not interested in writing a lot of item-based mods. I just have a compulsion to write this backend, and see what heights the rest of you take it to :-)

So, basically, what this MOD will be, when it's completed, is something that handles direct database interfacing for items, and users. A mod author, instead of having to look up and figure out the proper methods of dealing with updating the user's list of items, can instead tell my code... $user->give($item_id,$amount); and not worry about having to make the appropriate database calls, etc.

... what i really want here, is what you guys want to see in this API, so that i know what to add.

without further ado, this is what i have... (not much, but i'll be working on it)
Camels in the Mist
<noise type="random" source="camel" />
Cash Mod
Xore
Former Team Member
Posts: 2594
Joined: Wed Apr 16, 2003 7:42 pm
Location: the wastelands between insomnia and clairvoyance
Contact:

Post by Xore »

(SQL to get started):

Code: Select all

ALTER TABLE phpbb_users ADD user_items TEXT;

CREATE TABLE phpbb_items (
  item_id int(11) NOT NULL default '0',
  item_settings smallint(4) DEFAULT '15' NOT NULL,
  item_name varchar(64) NOT NULL default '',
  item_image varchar(255) NOT NULL default '',
  item_type varchar(64) NOT NULL default '',
  item_subtype varchar(64) NOT NULL default '',
  item_description text NOT NULL,
  item_stats varchar(255) NOT NULL default '',
);
(code...
functions_item.php)

Code: Select all

<?php

if ( defined('ITEMS_API_INCLUDE') )
{
	return;
}

define('ITEMS_API_INCLUDE',true);

// Bitmask filters - the order here matters. don't change it. only add to the end of the list.
$i = 0;
define('ITEM_ENABLED', 1 << $i++);
define('ITEM_IMAGE', 1 << $i++);
define('ITEM_VISIBLE', 1 << $i++);
define('ITEM_EXCHANGEABLE', 1 << $i++);
define('ITEM_USABLE', 1 << $i++);
// insert new bitmasks before this line

define('ITEMS_DELIM1',':');
define('ITEMS_DELIM2',',');

define('ITEMS_TABLE', $table_prefix.'items');

class item_table
{
	var $items;
	var $idlist;
	function items_api()
	{
		$items = array();
		$idlist = array();
	}
	function load( $specs = false )
	{
		global $db;
		$sql = "SELECT * FROM " . ITEMS_TABLE;
		if ( is_array($specs) )
		{
			$update_specs = array();
			for ( $i = 0; $i < count($specs); $i++ )
			{
				if ( !isset($this->items[$specs[$i]]) )
				{
					$update_specs[] = $specs[$i];
				}
			}
			if ( !count($update_specs) )
			{
				return;
			}
			$sql .= " WHERE item_id IN (" . implode(', ',$update_specs) . ")";
		}
		if ( !($result = $db->sql_query($sql)) )
		{
			message_die(GENERAL_ERROR, 'Error retrieving item data', '', __LINE__, __FILE__, $sql);
		}
		while ( $row = $db->sql_fetchrow($result) )
		{
			if ( ! isset($this->items[$row['item_id']]) )
			{
				$this->idlist[] = $row['item_id'];
				$this->items[$row['item_id']] = new item($row);
			}
		}		

	}
	function item_exists($item_id)
	{
		return ( isset($this->idlist[$item_id]) );
	}
	function &next_item(&$iterater, $mask = false)
	{
		if ( empty($iterater) )
		{
			$iterater = 0;
		}
		if ( !($iterater < count($this->idlist)) )
		{
			$iterater = 0;
			return false;
		}
		$item_id = $this->idlist[$iterater];

		if ( !$mask )
		{
			$iterater++;
			return $this->items[$item_id];
		}

		while ( !$this->items[$item_id]->mask($mask) )
		{
			$iterater++;
			if ( !($iterater < count($this->idlist)) )
			{
				$iterater = 0;
				return false;
			}
			$item_id = $this->idlist[$iterater];
		}
		$iterater++;
		return $this->items[$item_id];
	}
	function item_count($mask = false)
	{
		if ( !$mask )
		{
			return count($this->idlist);
		}
		$count = 0;
		$i = 0;
		while ( $i < count($this->idlist) )
		{
			$item_id = $this->idlist[$i];
			if ( $this->items[$item_id]->mask($mask) )
			{
				$count++;
			}
			$i++;
		}
		return $count;
	}

}

class item
{
	var $data;
	function item($row)
	{
		$this->data = $row;
	}
	function mask($bitmask = false)
	{
		if ( !$bitmask )
		{
			return true;
		}
		return (($bitmask & $this->data['item_settings']) == $bitmask);
	}
	function name($amount = false)
	{
		$postttext = '';
		if ( $amount )
		{
			$posttext = ' ( ' . $amount . ' )';
		}
		return ($this->data['item_name'] . $posttext);
	}
	function display($amount = false)
	{
		if ( $this->mask(ITEM_IMAGE) )
		{
			return ( '<img src="' . $this->data['item_image'] . '" alt="' . str_replace('"','"',$this->name($amount)) . '" />' );
		}
		else
		{
			return $this->name($amount);
		}
	}
}

$items = new item_table();

class item_user
{
	var $user_id;
	var $userdata;
	var $userdata_stored;
	var $user_items;
	var $user_item_ids;

	function item_user($user_id,$userdata = false)
	{
		$this->user_id = $user_id;
		$this->user_items = array();
		$this->user_items_ids = array();
		if ( $userdata )
		{
			$this->userdata = $userdata;
			$this->userdata_stored = true;
			$this->unpack_items();
		}
		else
		{
			$this->userdata_stored = false;
		}
	}
	function get_userdata()
	{
		if ( !$this->userdata_stored )
		{
			$this->userdata = get_userdata($this->user_id);
			$this->userdata_stored = true;
			$this->unpack_items();
		}
	}
	function unpack_items()
	{
		if( isset($this->userdata['user_items']) && (strlen($this->userdata['user_items']) > 2) )
		{
			$item_set = explode(ITEMS_DELIM2,$this->userdata['user_items']);
			for ( $i = 0; $i < count($item_set); $i++ )
			{
				list($item_id,$item_count) = explode(ITEMS_DELIM1,$item_set[$i]);
				$item_id = intval($item_id);
				$item_count = intval($item_count);
				if ( !isset($this->user_items[$item_id]) )
				{
					$this->user_items_ids[] = $item_id;
					$this->user_items[$item_id] = $item_count;
				}
			}
		}
	}
	function get_list()
	{
		return $this->user_item_ids;
	}
	function has( $item_id, $quantity = 1 )
	{
		$this->get_userdata();
		return ( isset($this->user_items[$item_id]) && ($this->user_items[$item_id] >= $quantity) );
	}
	function give($item_id,$amount)
	{
		$this->get_userdata();
		if ( isset($this->user_items[$item_id]) )
		{
			$this->user_items[$item_id] += $amount;
		}
		else
		{
			$this->user_items_ids[] = $item_id;
			$this->user_items[$item_id] = $amount;
		}
		$this->update_user();
	}
	function take($item_id,$amount)
	{
		$this->get_userdata();
		if ( $this->has($item_id,$amount) )
		{
			$this->user_items[$item_id] -= $amount;
			$this->update_user();
		}
	}
	function give_items_by_id( $item_array )
	{
		$this->get_userdata();
		while ( list($item_id,$amount) = each($item_array) )
		{
			$item_id = intval($item_id);
			$amount = intval($amount);
			if ( isset($this->user_items[$item_id]) )
			{
				$this->user_items[$item_id] += $amount;
			}
			else
			{
				$this->user_items_ids[] = $item_id;
				$this->user_items[$item_id] = $amount;
			}
		}
		reset($item_array);
		$this->update_user();
	}
	function take_items_by_id( $item_array )
	{
		$this->get_userdata();
		while ( list($item_id,$amount) = each($item_array) )
		{
			$item_id = intval($item_id);
			$amount = intval($amount);
			$this->has($item_id,$amount)
			{
				$this->user_items[$item_id] -= $amount;
			}
		}
		reset($item_array);
		$this->update_user();
	}
	function update_user()
	{
		global $items, $db;
		$this->get_userdata();
		$items->load($this->user_items_ids);
		$packing = array();
		for ( $i = 0; $i < count($this->user_item_ids); $i++ )
		{
			if ( $items->item_exists($this->user_item_ids[$i]) && ($this->user_items[$this->user_item_ids[$i]] > 0) )
			{
				$packing = $this->user_item_ids[$i] . ITEMS_DELIM1 . $this->user_items[$this->user_item_ids[$i]];
			}
		}
		$user_items = implode(ITEMS_DELIM2,$packing);
		$sql = "UPDATE " . USERS_TABLE . "
				SET user_items = '" . $user_items . "'";
		if ( !($db->sql_query($sql)) )
		{
			message_die(GENERAL_ERROR, 'Error updating user item data', '', __LINE__, __FILE__, $sql);
		}
	}
}

?>
Last edited by Xore on Sun Oct 26, 2003 8:27 pm, edited 2 times in total.
Camels in the Mist
<noise type="random" source="camel" />
Cash Mod
Xore
Former Team Member
Posts: 2594
Joined: Wed Apr 16, 2003 7:42 pm
Location: the wastelands between insomnia and clairvoyance
Contact:

Post by Xore »

So, for example, if usera (userid = 45) wants to give 2 of item #23 to userb (userid = 67) the code would look something like this:

Code: Select all

$usera = new item_user(45);
$userb = new item_user(67);
if($usera->has(23,2))
{
 $usera->take(23,2);
 $userb->give(23,2);
}
.. and that's it. done, database is updated and everything.

and there should be a lot of other stuff, as soon as i get the item display & admincp item creator up.
Camels in the Mist
<noise type="random" source="camel" />
Cash Mod
User avatar
GPHemsley
Registered User
Posts: 1475
Joined: Fri Apr 18, 2003 3:12 am
Location: Long Beach, NY
Name: Gordon Hemsley
Contact:

Post by GPHemsley »

Xore wrote: (code...
functions_item.php)

Code: Select all

<?php

if ( defined('ITEMS_API_INCLUDE) )

Missed a ' after the constant. :P
Xore
Former Team Member
Posts: 2594
Joined: Wed Apr 16, 2003 7:42 pm
Location: the wastelands between insomnia and clairvoyance
Contact:

Post by Xore »

i suppose i should have at least tested this code before posting it... humm ho...

mind you, this is ALPHA code... i don't even have a framework in place yet to test it
Camels in the Mist
<noise type="random" source="camel" />
Cash Mod
McLR
Registered User
Posts: 95
Joined: Thu Aug 21, 2003 12:51 pm

Post by McLR »

I didn't understand what is this MOD..
If you can , please describe it in simple language plz.
tnx
AbelaJohnB
Former Team Member
Posts: 5674
Joined: Fri Jul 06, 2001 11:56 pm

Post by AbelaJohnB »

So, Xore...


You know what I'm gonna ask, so I won't ask it :P


(ok, ok.... care putting something together for my referral mod, maybe as a 'real life' example <grin>)
wGEric
Former Team Member
Posts: 8805
Joined: Sun Oct 13, 2002 3:01 am
Location: Friday
Name: Eric Faerber
Contact:

Post by wGEric »

Looks good. I think I'll wait until you have this mostly done before I try to make the Store MOD use it.
Eric
User avatar
thewizard
Registered User
Posts: 356
Joined: Sun Sep 29, 2002 7:46 am

Post by thewizard »

ooh i cant wait. Especially to get the next version of wGErics Store mod hehe great job once again Xore
PhilippT
Registered User
Posts: 419
Joined: Mon May 06, 2002 5:59 pm

Post by PhilippT »

a 'standard' item-backend is a really good idea and with a propre defination the "boom" for the RPG-mods can com againe :mrgreen:

Im very interessted in this.....

edit:

Code: Select all

item_settings smallint(4) DEFAULT '15' NOT NULL ;
is it not

Code: Select all

item_settings smallint(4) DEFAULT '15' NOT NULL ,
?
Xore
Former Team Member
Posts: 2594
Joined: Wed Apr 16, 2003 7:42 pm
Location: the wastelands between insomnia and clairvoyance
Contact:

Post by Xore »

yes.

thank you :-)
Camels in the Mist
<noise type="random" source="camel" />
Cash Mod
AbelaJohnB
Former Team Member
Posts: 5674
Joined: Fri Jul 06, 2001 11:56 pm

Post by AbelaJohnB »

Anybody gotten this to work?


Looking at plugging it into my own (referral) MOD, and would be interrested in how you've gotten it to work.

Thanks.
Xore
Former Team Member
Posts: 2594
Joined: Wed Apr 16, 2003 7:42 pm
Location: the wastelands between insomnia and clairvoyance
Contact:

Post by Xore »

AbelaJohnB wrote: Anybody gotten this to work?


Looking at plugging it into my own (referral) MOD, and would be interrested in how you've gotten it to work.

Thanks.


I can write an events plugin system similar to that of cash mod (next release) that you can include also, i guess. I'm just waiting for some translations (lang) before i submit Cash Mod.
Camels in the Mist
<noise type="random" source="camel" />
Cash Mod
wGEric
Former Team Member
Posts: 8805
Joined: Sun Oct 13, 2002 3:01 am
Location: Friday
Name: Eric Faerber
Contact:

Post by wGEric »

Whats the status of this, Xore?
Eric
Epostile
Registered User
Posts: 121
Joined: Sat Nov 15, 2003 1:56 pm
Location: Somewhere...
Contact:

Post by Epostile »

I'm just wondering if theres been any progress with this mod? I could use this in a project i'm building (and include the code somewhat into my crusades RPG mod.. all credits will as always be given)...

Just wondering if theres been any progress???

Tez
DERP Fantasy Gaming
The home of Fantasy Gaming.
Post Reply

Return to “[2.0.x] MODs in Development”