Background
I have written a simple WebGL puzzle game and I want to allow people to design and share their own levels.
My idea was to store the level data in the query string of a URL. This would then let people simply share the URL if they wanted a friend to try the level.
What is a good way to store game data in a query string?
What I’ve tried
Currently I generate a URL via
"penguins.html?data="+encodeURIComponent(JSON.stringify([X,P]));
This first converts the data to JSON:
[[[[3,-1,0,0,0],[4,-1,0,0,0]]],[{"x":20,"y":20,"dx":0,"dy":0,"vy":2.9999999871229686,"vx":0.0002779607689814708,"thetaf":180,"speed":3,"type":0,"state":6,"steps":0,"numfish":0,"d":4,"size":8},{"x":60,"y":60,"dx":20,"dy":100,"vy":97.51615330485151,"vx":-78.06665276994102,"thetaf":0,"speed":3,"type":17,"state":0,"steps":0,"numfish":0,"d":15,"size":89.00000012433019}]]
and then escapes it to become:
[[[[3%2C-1%2C0%2C0%2C0]%2C[4%2C-1%2C0%2C0%2C0]]]%2C[{%22x%22%3A20%2C%22y%22%3A20%2C%22dx%22%3A0%2C%22dy%22%3A0%2C%22vy%22%3A2.9999999871229686%2C%22vx%22%3A0.0002779607689814708%2C%22thetaf%22%3A180%2C%22speed%22%3A3%2C%22type%22%3A0%2C%22state%22%3A6%2C%22steps%22%3A0%2C%22numfish%22%3A0%2C%22d%22%3A4%2C%22size%22%3A8}%2C{%22x%22%3A60%2C%22y%22%3A60%2C%22dx%22%3A20%2C%22dy%22%3A100%2C%22vy%22%3A97.51615330485151%2C%22vx%22%3A-78.06665276994102%2C%22thetaf%22%3A0%2C%22speed%22%3A3%2C%22type%22%3A17%2C%22state%22%3A0%2C%22steps%22%3A0%2C%22numfish%22%3A0%2C%22d%22%3A15%2C%22size%22%3A89.00000012433019}]]
Drawback of current approach
This approach works fine for small levels, but for bigger levels I get the error:
414. That’s an error.
The requested URL /penguins.html... is too large to process.
It seems that my current host (Google Appengine) has a limit on the length of the URL.
Question
Is there a better way in Javascript to store my game data (i.e. which would result in a shorter URI)?
I’ve done something similar where I make a POST to GAE with the JSON data and store it in the datastore with a generated key and return a url based on that key for the user to share.
So you make your post of the JSON data. You store that data in an object like:
The
keyis an auto generatedint. Then you return to the user a url like:http://www.mygameurl.com/games?id=1234with id corresponding to the generatedkey. Then you can let the user share that url to get the game data back from the datastore. It shortens the url that the user is sharing and prevents them from messing with the url and breaking the game data.