Forum BigDB BigDB Question

Discussion and help relating to the PlayerIO database solution, BigDB.

BigDB Question

Postby twick » November 12th, 2010, 11:54 pm

Hi,

I'm designing a turn-based multiplayer game that uses items (such as potions, etc). I want a player to log in with his details and store info into a database. This is the first project I've had to work with databases, so I'm new to them in general :)

The question I have is the following: What's the most efficient way of storing many objects in a database? The way I'm thinking is have each item as an object in the database, however I want to separate the objects into 2 categories: some objects will be usable in fights (such as potions) but some will only be available outside of them (quest items). The server would check to see if the items exists in the database and then send the info to the client.

Is it possible to store an object in a database with properties (such as: name(potion), quantity(x), usableInFights(bool))? My game has maaaaany items, how will this affect performance? Will storing an ID number instead of a string for the name offer any advantages?
twick
 
Posts: 15
Joined: October 13th, 2010, 5:27 pm

Re: BigDB Question

Postby default0 » November 13th, 2010, 12:02 pm

twick wrote:Hi,

I'm designing a turn-based multiplayer game that uses items (such as potions, etc). I want a player to log in with his details and store info into a database. This is the first project I've had to work with databases, so I'm new to them in general :)

The question I have is the following: What's the most efficient way of storing many objects in a database? The way I'm thinking is have each item as an object in the database, however I want to separate the objects into 2 categories: some objects will be usable in fights (such as potions) but some will only be available outside of them (quest items). The server would check to see if the items exists in the database and then send the info to the client.

Is it possible to store an object in a database with properties (such as: name(potion), quantity(x), usableInFights(bool))? My game has maaaaany items, how will this affect performance? Will storing an ID number instead of a string for the name offer any advantages?


Hai, storing an ID is the best way to do it.
Then in you gamecode create a List of items (either by typing in each item manually, or by setting up a BigDB table for the items and loading in that using loadRange) and once a player joins load his items: Item ID #0 (health pot fe) x4 and set that in an item list of the player, once a potion is used reduce the number and at the end of the game save the new amount.
I would be making a server type for battles and another one for outside battles and load the information depending on that.

Best regards
Try to play my Game: -->BlackGalaxy<--
User avatar
default0
 
Posts: 115
Joined: February 2nd, 2010, 6:46 pm
Location: Germany

Re: BigDB Question

Postby twick » November 16th, 2010, 9:54 pm

Thanks for the reply!

I've set it up using that method, but it needs to access the database each time it needs to check if potions > 0. Is there any way to load each player's info (in this case, number of potions) into the server and store it in memory until player disconnects?

For example, in the server have a variable int named potions. On login, load the amount of potions from bigDB, and update the potions int. Then use the serverside potions int for that particular player (sending the info back to the player, removing or adding, etc. Then update the DB if there have been any changes to the quantity of potions.

It appears that initialising the int will affect all connected users instead of just the one.
twick
 
Posts: 15
Joined: October 13th, 2010, 5:27 pm

Re: BigDB Question

Postby fox1980 » November 17th, 2010, 1:14 pm

You have to declare your variable on the "Player" class so it's unique for each player, if you declare it elsewhere you're just using the same variable for every player.

Your player class should look something like this:

Code: Select all
   //Player class. each player that join the game will have these attributes.
   public class Player : BasePlayer {
      public int potions = 0;
   }


On the userjoined function you load the value from the database and then assign it to player.potions.
On the userleft function you get the value player.potions and save it back to the DB.

Now whenever you want to know how many potions the player has you just look at the player.potions value instead of querying the DB.
Last edited by fox1980 on November 17th, 2010, 3:31 pm, edited 1 time in total.
fox1980
 
Posts: 206
Joined: April 1st, 2010, 10:39 pm

Re: BigDB Question

Postby twick » November 17th, 2010, 2:31 pm

Thanks! works perfectly!

Is there any possibility that UserLeft() wont get called when the player disconnects? Can I rely on it to update the database once instead of using bandwidth everytime a change is made? Or is that bad practise?
twick
 
Posts: 15
Joined: October 13th, 2010, 5:27 pm

Re: BigDB Question

Postby default0 » November 17th, 2010, 2:34 pm

twick wrote:Thanks! works perfectly!

Is there any possibility that UserLeft() wont get called when the player disconnects? Can I rely on it to update the database once instead of using bandwidth everytime a change is made? Or is that bad practise?


Nope, using UserLeft() for saves is the exact right way to do it :)
If it doesnt fire, its a bug in the API, but Ive never heard of anything like that, so you can use it safely =)

Best regards
Try to play my Game: -->BlackGalaxy<--
User avatar
default0
 
Posts: 115
Joined: February 2nd, 2010, 6:46 pm
Location: Germany

Re: BigDB Question

Postby azuanagames » November 18th, 2010, 6:32 am

I've had UserLeft not get called plenty of times... Not sure why it happens. But if you can add safe guards for it, I would. Maybe save every once in a while.

For instance I record the room the user is in and clear the room in UserLeft. I have users with rooms in the db. This shouldn't be possible, unless the server crashed or something else happens that I can't explain...

Moral of the story, make sure you don't count on UserLeft...
azuanagames
 
Posts: 157
Joined: April 29th, 2010, 10:59 pm

Re: BigDB Question

Postby twick » November 25th, 2010, 5:39 am

Thanks for all the help :) I'm just using a "save changes" button now that saves everything onto the DB instead of each time the player modifies his settings!
twick
 
Posts: 15
Joined: October 13th, 2010, 5:27 pm


Return to BigDB



cron