2
\$\begingroup\$

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?

\$\endgroup\$
2

2 Answers 2

4
\$\begingroup\$

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!

\$\endgroup\$
7
  • \$\begingroup\$ -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. \$\endgroup\$
    – Liosan
    Jan 27, 2013 at 10:05
  • \$\begingroup\$ 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? \$\endgroup\$
    – test
    Jan 27, 2013 at 13:50
  • \$\begingroup\$ 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 \$\endgroup\$
    – Liosan
    Jan 27, 2013 at 16:12
  • \$\begingroup\$ @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. \$\endgroup\$
    – CodeMoose
    Jan 28, 2013 at 2:21
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – CodeMoose
    Jan 28, 2013 at 16:57
2
\$\begingroup\$

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).

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged .