Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

DataBase

Tdata

Sponsor

I have a question.

I am currently using MySQL to store a lot of data like items and weapons. I'd like to use an internal database to hold all this to reduce the amount of data the Client has to pull from the MySQL database. I was wondering if you had any ideas. I would prefer if the data was encrypted in the database so that end-users wouldn't be able to easily change the internal values.

I'd like it to be a Database type solution to make it easier to edit and access. I don't want to have to resort to using a large array to hold all this... A few 500+ item arrays are a bit much to have the program hold in memory...
 
I dont get what your problem is exactly...

You have a database that contains all your weapons and stuff... so like:
Code:
 

{Weapons Table}

id - int

name - varchar(30)

type - tinyint

power - smallint

 

Then when you have a user run your game it calls the DB (be it directly from your app or from another layer) and returns the data... am i right up to this bit?

If so are you just wanting to cut down on calling the DB whenever you want a weapon if you already have it loaded or something?

You sound like you are always loading in EVERY weapon, and usually you would only make a call for weapons based on an individual ID or based on some other criteria... like all weapons owned by a player (which would be something like)

Code:
 

SELECT * FROM weapons

JOIN players_weaponsON weapons.id = player_weapons.item_id

WHERE players_weapons.player_id = @YourPlayerID;

 

If you could explain your scenario a little more then i could possibly advise you a bit better...

If you are doing some sort of admin system and want to load in all weapons then you can either bring them back in chunks so lets say 0-50, 51-100 etc (a LIMIT clause if in MySQL), or you can just bring them all back when the user loads the weapons section... it may sound like its alot but if you have a simple weapon model like described above it would use next to no memory:

id (int) - 4 bytes
name (string) - i think its 2 bytes per character as its unicode (could be wrong)
type (byte) - 1 byte
power (short) - 2 bytes

That is a total of 7 bytes (Excluding string data), so lets say the average name is 10 letters long your average object size in this scenario would be 27bytes... so if you have 1000 of them in memory it would be 26.5kb (roughly) which is nothing... and if you only do it once then just keep it in some generic storage... like a dictionary keyed on the id or something it would yield fast lookups and simple data storage.

You can alternativley try to sort your data before you bring it back, so ask the user if they want to sort by chunks/type/power/value etc then just limit the stuff brought back...


If however you are doing this on a client side system, like your in-game stuff you should NEVER need to bring back all weapons at once as there would be no scenario when any one user would need to know of ALL weapons at any one time... if you are however having some sort of shared backend that caches all weapons or something then maybe you could look into Memcached or something which would give you a distributed caching system that would easily churn over hundereds of thousands of rows in storage without any worries...

One final thing that worries me is that you mention that the clients could change your *static* data models... by static i mean models that come from the DB but dont get updated, so areas, skills, items. A user may have many items but it can never change the items, only the amount of them that it owns. Whenever a user does do an update be it to their position or equipment etc the client should rarely do the work (im assuming you have a multiplayer system or at least a single player game that is driven from a DB/server layer), they would shoot over their update (like ive moved right) the server would then verify it, and if its correct it would insert it into the DB itself and send the client a true response or similar. So even if the user did some hack to change their level to 100 and HP 2000 and moved themselves 1000 pixels to the right, the server would laugh at them because its invalid compared to the servers snapshot.

Anyway im just rambling and most of it is probably not useful, if you can narrow it down or give us a scenario... pm me if you dont want to post publicly and i can try and help...
 

Tdata

Sponsor

I'll try to make it more clear, even though I am now using XML, even if it is a bit more complicated.

What I was asking for was an internal database option that would allow me to cut back on the amount of MySQL calls I have to make to the server. Something that would allow me to use one call to fill the internal database and to be able to access this internal database like I would were it a MySQL server.

I just want a solution that will allow me to reduce the lag for my game. I figure the more information the game has locally, the less the lag.
 
Hey,

Im sure you have already done performance testing of some variety but usually DBs can handle thousands of cocurrent connections pulling data back before they start to fall over... so just make sure you are not removing work from the DB for no good reason...

As i said earlier usually your client system will only need to do a few queries to get up and running... as they only need to know about their items, stats, area and then anyone/anything nearby... ok it may take a few seconds, but thats ok as this is done up front before your game starts properly...

Once thats done the only time they would need to query is if something has changed... someone has entered their scene that they need to get info on, or they are changing areas, looting new equipment... and a DB should be fast enough to cope with all these small reads/writes on the fly... if its not then you may want to check that your DB design is ok (all PKs are set and you have indexed any FKs etc) also make sure your tables are not too bloated i.e you havent got one long player table with like 20 columns when you only really need 10 of those columns and the other 10 could be split into 2 smaller sub tables, i think the technical term for it is normalizing...

Anyway once you are SURE everything else is awesome you may want to think about how you are dealing with caching your data, as XML is fine, but every time you want to load in something you have to get the file, open it, read it in, parse the xml, convert it to an in game entity before you can use it. If you are on a web server then the DB is MUCH MORE efficient at pulling data from files and delivering it than a web server would be. Also if you are manually parsing each file from XML to your models its a load of work... you may want to take a look at my other post of automatic model serialization...

Again just so im clear on what you are doing... are you pretty much copying and pasting the entire DB content to some temporary xml files on the client machine, and if so are you doing this each time they load up? if so i think we may be having more discussions on here... could you also tell me what platform your game is... i.e MySQL DB, C# TCP Backend, c# Winforms Game client and PHP Game client... or something along those lines so i can offer you some more suited advice, thats if you are not sick of me ranting already :D
 

Tdata

Sponsor

No, I am using the XML files to store the data that will not be updated while the game is running. When I pull the SQL data, I tend to place it in an array, then I use the data to fill a List<> before blanking the array until it is needed again...

Also, to help you understand why I am trying to load as much data as I can rather than doing so on-the-fly, It is a RTS type game. I don't want it to become a game where the faster the connection, the better you do... Also, were I to query the database everytime someone hits a button, the server will crash...
 
Indeed, any realtime sort of movement would clogg up a DB server... if you had lots of people doing it...

Currently do you just have your app(game) and your db server?

If thats the case i would recommend putting another layer in that sits in front of the DB and the player goes through that, be it a TCP layer or a WCF service or something. As that way you can have your entities sitting on that layer, and just push the information needed to the clients as and when they need it, also you can delay database postbacks until a large chunk of updates have happened and chunk it together or purely update anything that has changed after a given interval...

Most games will never have the client deal directly with a DB, because its a security vulnerability, mainly because they can fudge in whatever values they want to their client objects that get updated and also they could work out where your DB server is by watching their net traffic then spam your machine to buggery, so if you havent already i would build another layer that sits outside of the client on a server or something and validates all the movements etc and tries to optimize DB chatter...

Just want to say as well that you are not doing anything *Wrong* at the moment, so if your game works and stuff, dont bother with my ramblings, im just trying to let you know of other implementations that *may* be able to make life easier further down the line...
 

Tdata

Sponsor

I have looked for months for a Client/Server solution. I hate having to deal with the DB directly. But, so far, there are only Tuts on how to make a chat server.
 
Tdata":239gicyu said:
I have looked for months for a Client/Server solution. I hate having to deal with the DB directly. But, so far, there are only Tuts on how to make a chat server.

I completely understand your reasons, its a good learning exercise if you wanted to learn more about networking, but to add this new layer in you would need to make sure your server allows you to run your own apps.

You could possibly use a web service or WCF service to use shared hosting and also mitigate most of the risk, although WCF services are bloated and slow for realtime applications... if you are building on a budget or time constraint and security isnt a major concern then your current approach is fine, but if you plan to make it alot more widely used going forward you may have to do some research into this or at least find someone who can help you out, as there are MAJOR reasons why most games have dedicated servers etc...

Sorry i cant be of any more help, but i guess if it works you can worry about the rest later once your game takes off
 

Tdata

Sponsor

My main problem with figuring it out is that I haven't been able to see a working example of this kind of C/S solution.



Packet_Length || ClientEvents.Login || String_Username || String_Password

That is who I see the Login Packet working, but I have no clue how to read that and make sure that once the ClientEvents.Login is seen, it expects that there will ne two string objects after it. Then to send the result like this:


Packet_Length || ServerEvents.LoginResult || Bool_Login


I have never really learned how to do Packets and bit arrays...
 
If you are wanting to do it that way then you would probably need to go with asynchronous sockets, i have a library that i wrote that does that sort of stuff but its not as simple as i would like it to be... you are welcome to take a look if you think it could help you understand a bit more, but basically you would want to create a simple TCP server, there are plenty of examples online, and in the server you would do the following steps:

- Create a socket object (System.Net)
- Make it of type TCP / Internetworks (IPv4) or IPv6 if you want
- Bind it to a port and accept connections
- Register a callback for handling connections

- Once you recieve a connection, save the generated socket
- Call BeginRecieve and give it a callback for when you recieve data
- Check any incoming data forms a whole packet (check below section for more info)
- Create a simple model in you data layer that gets fired off on an event saying you have data and pass the model
- Then get your server logic to turn it into a game entity or update a game entity (whatever you want to do with the data), then carry out some validation logic, and even send a response if you want/need to.

It may seem a bit daunting to start with but its not that hard once you get used to it, and there are multiple ways to make sure you have downloaded *entire* packets... the way i do it is i split my packets into a header and content, so the header is always 4 bytes or so (short Content_Size, short Message_Type), you can always make it larger or smaller, but try to keep it fairly short, then i read the header into the buffer, then i know from that how large the message itself will be, so i can allocate the correct buffer size rather than trying to keep recieving a blob of data and then trying to re-arrange it.

You would basically do the opposite on the client, have your socket connect to the given server, and like the server have BeginRecieve start with a callback, and then send your stuff over... if you look on codeplex there are plenty of examples, and if you pm me i can send you the framework im using for my backend at the moment, which may or may not help :(
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top