I have a webgame written in PHP and using MariaDB for the database. I'm currently looking into ways of improving core systems.
One example would be items. Currently, there is a table data_items
that lists all of the items, what category they fall into, their sale value, description, icon and so on. There is then an inventory
table with foreign keys referencing users
and data_items
, and a quantity
column. Simple, and it works.
However, some items have effects. Actually most of them do.
Currently effects are implemented in one of two ways.
The item is requested specifically by the code. For example, a particular feature requires a particular item, so that item is loaded up, its quantity is checked in the player's inventory, and the code proceeds if the user has the item. This is fine.
The item's properties are checked. For items in the "Consumables" category, there's currently a
switch
statement that provides different effects based on the item in question. This is horrible.
I'm considering the idea of having a class
for each item. The class can then have methods such as isConsumable()
, consume()
, and whatever else may be needed as applicable to each item.
Should I still have the database? I assume so. There are cases when items need to be searched, eg. by category, for showing the player's inventory. In a case like this, where there are upwards of 800 items at this point, I wouldn't want to have to load 800 classes.
What kind of combination of things should I be aiming for here? Maybe have an item "prototype" that just holds data, which can be used when enumerating the items for the inventory? Then, when the item is actually used for something, check for its specific class and only load it at that point? I think that makes sense, but any advice on this matter would be very helpful indeed.
I'm facing a similar problem for the creatures within the game. Most of them have nothing special to them, so the database works fine. But some have different images based on certain factors. One in particular can be customised to have whichever pair of colours the user chooses. These are currently all handled by switch
statements in the getImage()
method of the generic creature object. Should this be split into classes for each (of the 1200+) creatures?