2

OK, let's say I have a JavaScript game (preferably on jQuery framework) and its using HTML5 Canvas.

What would be your way of storing data to map out shops, monsters, and NPCs? I was thinking maybe JSON txt files on the website? Or maybe simple text files and simple delimiters? I am not sure. How would you store that kind of data?

test
  • 477
  • 4
  • 11
  • 2
    http://gamedev.stackexchange.com/questions/7952/how-to-choose-how-to-store-data and http://gamedev.stackexchange.com/questions/7015/binary-xml-for-game-data and http://gamedev.stackexchange.com/questions/19046/what-is-a-good-file-format-for-saving-game-data – House Jan 27 '13 at 07:26
  • Basically, if you want to prevent people from directly accessing data on the server, ajax + post + database can accomplish that. However, if you want to prevent your data from being transparent once its loaded into js, you'll need to get creative - and that's a topic for chat. – CodeMoose Jan 28 '13 at 17:06

2 Answers2

4

JSON is definitely the way to go, it's nice and lightweight. Though, storing the data in a database is probably more secure than static files on the server - it prevents people from accessing the files directly. JQuery makes it insanely easy to execute ajax queries, you'll just need a simple PHP (or maybe ASP, depending on your host) script for the ajax to call, to handshake info between javascript and the database. Some quick hunting on google reveals a ton of tutorials and demo classes, so even if you don't have any server-side scripting knowledge, it shouldn't be an obstacle. Best of luck!

CodeMoose
  • 517
  • 2
  • 5
  • 17
  • -1 for the "security" argument. Storing in a database doesn't have anything to do with the fact that people can access it or not. If the data arrives on the client, the user can just intercept it anyways. Or send the request himself, which will be handled from a database. Or read the data from the client-side Javascript. – Liosan Jan 27 '13 at 10:05
  • Yes, well, the information in the database is hidden away while if people can see where the request from something like "monsters.json" ... they can see every monster mapped out. So... how could I fix that? – test Jan 27 '13 at 13:50
  • Why do you want to fix that? Besides, the database is not really hidden away. Not any more that your files. After all, if the user can see the url server/monsters.json, they can also see the AJAX url server/index.php?req=getMonsters – Liosan Jan 27 '13 at 16:12
  • @Liosan Only if you send ajax as a GET. Jquery boils POST requests down to the easiest possible form (pretty much give it an associative array of key-value pairs), which thwarts direct access attempts on the php script as long as you only process POST variables. You can also restrict the response to certain http_referrers, preventing people from accessing via their own scripts. Will it stop a soviet supercomputer? No. Will it stop your average basement hacker? Most likely. – CodeMoose Jan 28 '13 at 02:21
  • See http://api.jquery.com/jQuery.ajax/#entry-examples for a basic breakdown on POST-mode ajax – CodeMoose Jan 28 '13 at 02:22
  • POST doesn't thwart anything. Any kid these days can use Firebug or bookmarklets or Wireshark or whatever. Besides, if you want to hide monster data (dunno why) then you have to hide it from everybody - because it just takes one guy who'll post the data on a forum somewhere. – Liosan Jan 28 '13 at 06:52
  • 1
    @Liosan That's a good point - no matter how you protect things on the server side, once javascript has the data it's transparent. Maybe there's some way to be more minimalist in the data handling? Sounds like it's time to send this over to chat. – CodeMoose Jan 28 '13 at 16:57
2

JSON would be fine, so would anything else. You can use XMLHttpRequest to load it, or some other technique as you see fit.

JQuery really doesn't buy you anything, it's almost exclusively for manipulating the DOM, if you use Canvas, you don't need it (You still need a DOM, but it probably very rarely changes).

MarkR
  • 1,666
  • 9
  • 9