View Single Post
Author Message
bottiger
AlliedModders Donor
Join Date: Dec 2010
Old 01-22-2015 , 00:33   [TF2] TF2 Item DB (replaces tf2itemsinfo)
Reply With Quote #1

Want to help test out a new beta version of the plugin with more features? Click Here and please let us know if it works!

This is a library that will allow you to query items or search for them with a specified criteria. For example you can check which slot an item uses or get all items in the melee slot for snipers.

This plugin is meant to replace tf2itemsinfo which has memory leaks and other problems that cannot be fixed and will eventually become permanently broken due to how long it takes to run and that it uses almost the maximum number of handles.

Data is stored in sqlite. A python script is used to update the database. I decided to do it this way because it is a bad idea to put a 10+ second loading time into your server especially when there is time limit for plugins now and is much faster in python. It is recommended to run this after every update. If you do not have python on your server, you can always run it on your computer and upload it to your server.

We need people to start porting plugins to use tf2idb.

I have tried to make the functions close to tf2itemsinfo but unfortunately I could not make them all the same because they were too inefficient, especially for SQLite.

With the TF2IDB_FindItemCustom function, you can create arbitrary SQL queries which should be much faster and much more flexible than the finditem function in tf2itemsinfo.

How to Install - Lazy Method

You can use the pre-made database here but it is not recommended because I will not be updating it. You should follow the additional instructions below so you can make the database yourself after each update.
  • Put tf2idb.smx into your plugins folder.
  • Unzip the sample-db-date.zip file and put the sq3 file into your sourcemod/data/sqlite folder.


How to Install - Additional steps to updating your own database
  • Install Python from python.org. If you are using Linux, it is probably already installed. Type python to check.
  • Unzip tf2idb.zip anywhere.
  • Follow the instructions below.

You need to first edit tf2idb.py and point it to your TF2 server folder. It will look like this.

Code:
TF_FOLDER = r'C:/tf2server/tf/'
ITEMS_GAME = TF_FOLDER + 'scripts/items/items_game.txt'
DB_FILE = TF_FOLDER + 'addons/sourcemod/data/sqlite/tf2idb.sq3'
Normally you will only need to change TF_FOLDER, but if you put items_game.txt or your sqlite folder in a different place, then you will need to change the other 2 lines.

Then run tf2idb.py to create the database. If you need to update the database, run tf2idb.py again.


Code:
// If a native returns bool and it is not obvious from the name what it is, then the bool represents whether the native succeeded.
// Natives returning a handle are ADT arrays. You must close them when you are done with them.

native bool:TF2IDB_IsValidItemID(id);
native bool:TF2IDB_GetItemName(id, String:string[], length);
native bool:TF2IDB_GetItemClass(id, String:string[], length);
native bool:TF2IDB_GetItemSlotName(id, String:string[], length);
native TF2ItemSlot:TF2IDB_GetItemSlot(id);
native bool:TF2IDB_GetItemQualityName(id, String:string[], length);
native TF2ItemQuality:TF2IDB_GetItemQuality(id);
native bool:TF2IDB_GetItemLevels(id, &min, &max);
native TF2IDB_GetItemAttributes(id, aid[TF2IDB_MAX_ATTRIBUTES], Float:values[TF2IDB_MAX_ATTRIBUTES]);
native Handle:TF2IDB_GetItemEquipRegions(id);
native bool:TF2IDB_DoRegionsConflict(const String:region1[], const String:region2[]);
native Handle:TF2IDB_ListParticles();
native Handle:TF2IDB_FindItemCustom(const String:query[]);
native bool:TF2IDB_ItemHasAttribute(id, aid);
Some useful examples.

Code:
// If you are creating items, you will want to filter out items with string attributes like this.
TF2IDB_FindItemCustom("SELECT id FROM tf2idb_item WHERE has_string_attribute=0");

// get all medieval and melee items
TF2IDB_FindItemCustom("SELECT a.id FROM tf2idb_item a JOIN tf2idb_item_attributes b ON a.id=b.id WHERE has_string_attribute=0 AND (attribute=2029 OR slot='melee') GROUP BY a.id");

// get all heavy melee items
TF2IDB_FindItemCustom("SELECT a.id FROM tf2idb_item a JOIN tf2idb_class b ON a.id=b.id WHERE b.class='heavy' AND slot='melee' AND has_string_attribute=0");
Table definitions

Code:
('CREATE TABLE "tf2idb_class" ("id" INTEGER NOT NULL , "class" TEXT NOT NULL , PRIMARY KEY ("id", "class"))')
('CREATE TABLE "tf2idb_item_attributes" ("id" INTEGER NOT NULL , "attribute" INTEGER NOT NULL , "value" TEXT NOT NULL, PRIMARY KEY ("id", "attribute") )')
('CREATE TABLE "tf2idb_item" ('
	'"id" INTEGER PRIMARY KEY  NOT NULL ,'
	'"name" TEXT NOT NULL,'
	'"item_name" TEXT NOT NULL,'
	'"class" TEXT NOT NULL,'
	'"slot" TEXT,'
	'"quality" TEXT NOT NULL,'
	'"tool_type" TEXT,'
	'"min_ilevel" INTEGER,'
	'"max_ilevel" INTEGER,'
	'"baseitem" INTEGER,'
	'"holiday_restriction" TEXT,'
	'"has_string_attribute" INTEGER'
	')'
)
('CREATE TABLE "tf2idb_particles" ("id" INTEGER PRIMARY KEY  NOT NULL , "name" TEXT NOT NULL )')
('CREATE TABLE "tf2idb_equip_conflicts" ("name" TEXT NOT NULL , "region" TEXT NOT NULL , PRIMARY KEY ("name", "region"))')
('CREATE TABLE "tf2idb_equip_regions" ("id" INTEGER NOT NULL , "region" TEXT NOT NULL , PRIMARY KEY ("id", "region"))')
('CREATE TABLE "tf2idb_capabilities"  ("id" INTEGER NOT NULL , "capability" TEXT NOT NULL )')
Changelog

10/21/2017 - Updated tf2idb.py to handle blank item_types.

3/1/2016 - Updated tf2idb.sp and tf2idb.smx - fixed GetItemEquipRegions. Thank you Dagothur.

12/18/2015 - Updated tf2idb.py to handle the typo in today's schema change. Now all attributes are stored in lowercase.

11/12/2015 - Updated tf2idb.py for python 3.5 and added item_name column. Thanks to fakuivan.

10/07/2015 - Updated tf2idb.py to support new schema changes, and not to change the database if parsing failed. Added the table "tf2idb_capabilities" with the columns ("id" INTEGER NOT NULL , "capability" TEXT NOT NULL )
Attached Files
File Type: inc tf2idb.inc (2.7 KB, 5513 views)
File Type: zip sample-db-2015-12-18.zip (410.7 KB, 8909 views)
File Type: sp Get Plugin or Get Source (tf2idb.sp - 3604 views - 13.4 KB)
File Type: smx tf2idb.smx (9.3 KB, 9953 views)
File Type: zip tf2idb.zip (3.5 KB, 6920 views)
__________________

Last edited by bottiger; 10-21-2017 at 20:15.
bottiger is offline