Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Best method to store user data  (Read 2713 times)

0 Members and 1 Guest are viewing this topic.

Eevee204

  • Newbie
  • *
  • Posts: 9
    • View Profile
Best method to store user data
« on: July 12, 2014, 09:04:27 pm »
Hello, I am working on an ORPG and I am wondering on the best method to store the users data, by this I mean, I want to be able to store the items that the user has in the his/her inventory or bank.

The user has a their username, password and other details stored in a database, my first initial idea to store the users value was to have a secondary table with a foreign key from the item table and the player table, but having multiple players updating a database all the time is slow? (Correct me if I am wrong!)

My second thought was too load all the possible items from the database into a list array with the item class type and use that though out the applications life time, but if I do this the list array will only exist while the application is running unless I store the values on the server or a database of some sort, preferably after the user logs out of the game?

I am just wondering if I am on the right tracks to do this, is the logic I am trying to accomplish the most efficient? Any tips or resources would be greatly appreciated on this. Thanks

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Best method to store user data
« Reply #1 on: July 12, 2014, 09:49:24 pm »
I'm a little confused since your second paragraph sounds like you're talking about the list of items the user actually has, while the third paragraph sounds like it's talking about the list of all item types that exist in the game.  The latter absolutely has to be in the client for obvious reasons, and there's no point putting it in a database since it's the same for all clients, so hopefully that's not what you meant.

Quote
but having multiple players updating a database all the time is slow? (Correct me if I am wrong!)
Nope.  You may have to configure your database to get the performance you want, but generally speaking any modern database will have a lot of ways to tweak performance based on things like your expected ratio of reads to writes, write frequency, read frequency, etc.  Most importantly, it sounds like no two users ever have to update the same record (they're only storing their information) so this should be no problem at all.

Though I have no idea how big your userbase will be and how many times you plan on updating each one's data.  You'll probably want to speed test a few databases once you have some good estimates on how many writes you actually need.

Quote
The user has their ... password and other details stored in a database ...
Hopefully those things aren't stored in plaintext.



And finally:
Quote
my first initial idea to store the users value was to have a secondary table with a foreign key from the item table and the player table

How to design the database is going to be the hard part.  Or more accurately, the easiest part to screw up without knowing it.  I've had to do a lot of this at my job, so here's my advice: Plan this out in advance like crazy.  Nothing is more irritating or fiddly than having to break everyone's data and rewrite a bunch of code because of a subtle mistake back in the design phase.  All the code "on top of" the database is a lot easier to fix later.

For instance, write down the exact SQL queries you want to make on this database.  Rank them by importance.  Make sure whatever database schema you use makes it not only easy to write the queries, but easy to add indexes on the frequent/important ones should you need to make them faster.  Then write down some changes you might conceivably want to add to the game in the future (adding new item types, supporting custom item filters/sorting, adding some kind of shared inventory features, who knows) and think through how you would change the database in a backwards-compatible way to support them.  Once you're sure how much of that your design can handle, and you're satisfied with that, then you're good.  The final design is usually quite simple, but picking the right simple design pays off big time.
« Last Edit: July 12, 2014, 10:08:57 pm by Ixrec »

Eevee204

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Best method to store user data
« Reply #2 on: July 12, 2014, 10:18:38 pm »
Hi thanks for the reply, I was just looking for the if it's the best way to go about it so basically what I wanted to do was -

I have a table in the database called tbl_users (for example) and then a table called tbl_items. I would first load all the items into the game as some global list (client side this is). Then I would search the database for say a table called tbl_inventory (which is the relationship table for tbl_users and tbl_items).

Once I have the items from the inventory table, I would then create the the list of items the user has, once the user logs out I would then update the database with the newest list of items the user has in their inventory.

So basically I fetch the items, create the inventory the user has and then when they decide to log out I will then update the table with their newest items and that will be the cycle of the game for the users inventory.

I am just wondering if I should use a database for this because I once used an engine called Eclipse Origins and the users items were stored on the server side using a .dat file, so I was just wondering if the method I am using was the best way or if there was another better way to do what I need to do.

Also I know what you mean about the important of good design, nothing is worse than having to fix a database that had no plan in it!

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Best method to store user data
« Reply #3 on: July 12, 2014, 10:28:05 pm »
A single .dat file might be a perfectly satisfactory database if it has a good interface and does everything you need.  The main issue is it's probably nowhere near as safe or robust as a "proper" database; one error in your code or your engine and the file could be corrupted forever.

Anyway, this question has nothing to do with SFML, and is a subjective design question only you can really answer, so we shouldn't spend too much time on it here.  Just a few quick comments:

Quote
Once I have the items from the inventory table, I would then create the the list of items the user has
You make it sound as if the user's list of items in the database is completely different from the list of items the user has in the game.  That doesn't seem right.

Quote
tbl_inventory (which is the relationship table for tbl_users and tbl_items)
Having a "relationship" table is an option, but it's probably the best option only if you expect there to be a many-to-many mapping between users and items, and you expect to have to query in both directions (users to items, and items to users).  I would think you want a one-to-many mapping and query only from users to items, in which case using only two tables (one with a foreign key) would probably be better.  Unless you think you really will want some crazy shared inventory feature that requires many-to-many mappings.

Eevee204

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Best method to store user data
« Reply #4 on: July 12, 2014, 10:37:35 pm »
Quote
You make it sound as if the user's list of items in the database is completely different from the list of items the user has in the game.  That doesn't seem right.

Yeah don't mean to make it sound like that, what I mean by that is there would be a list called
std::List<Item> allGameItems(); and another list such as std::List<Item> usersInventory(); and the items from the users inventory would be created from the database table and the allGamesItems list.

Either way, thanks for the advise and help.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Best method to store user data
« Reply #5 on: July 12, 2014, 11:06:22 pm »
SQLite can sometimes be a good solution when you want a SQL interface to your data but want them stored in a plain file on disk. Check it out, maybe it'll suit your needs :)