Javascript World-to-String script

Hey all,

My little brother makes Flash games and was having some trouble coming up with a way of having his level editor create strings for users to send to each other (for custom levels). So I wrote this little Javascript to do the task.

It works in any scenario where you only have objects of certain types at certain locations (i.e. you can’t use it to save parameters or anything). Pretty much it stores all object information in 4 characters, where the first character is the type and the next 3 characters represent the position. In this iteration, it can only save objects that exist in a world where width*height <= 238,328, but you could pretty easily increase that if you needed to by allowing more characters. It only uses uppercase, lowercase, and digits, so essentially it’s like a base 62 number system.

It also has a simple check digit added on at the end which is the total of all the digits mod 62 so that users can’t go editing the string to get whatever they want.

Even though it’s in Javascript, I figured it might be useful for some of you. I will probably convert it to Java at some point and use it in some of my simpler games. It will adapt great to grid-based environments.

http://www.otcsw.com/LevelSaveTest.html

(Just view source to see it)

Also the extremely verbose and somewhat obvious comments are meant to try to get my brother (who is not very experienced) to understand it. Also sometimes doing things like string = string + “foo” instead of string += “foo” are for that reason also. :slight_smile:

Have you considered JSON as a object graph serializer?

There are APIs for most languages to import and export JSON.

You can also GZ it, then base64 it, to make it easier to share.

Yeah, I did consider that, but I figured that importing a library would be too complicated for my little brother to do so I threw the idea out. If I were doing this on my own then I likely would used it.

But but but, JSON is Javascript. No library required.

Note:
If the browser doesn’t support it (older than: MSIE 8, FireFox 3.5), use the (tiny) code described here:
http://www.sitepoint.com/blogs/2009/08/19/javascript-json-serialization/

Unless they also change the last letter, you mean? It’s still possible to edit the string to get anything you want.

Repeat after me:

Nothing from the client can be trusted.
Nothing from the client can be trusted.
Nothing from the client can be trusted.

Anyway, thanks for sharing the code. Though I would have to say JSON would be the easier option since no libraries would be needed on either side.

Yes yes, but then they have to do math. :slight_smile:

No I wasn’t considering this something you would use to pass around sensitive information, I was making it specifically for the purpose (simple level saving) for which changing the string won’t do much. I more wanted to introduce the concept of a check digit to my little brother than anything - obviously the algorithm for the check digit is not very complicated, plus it’s out there for anyone to see if they want to reverse engineer it.

Definitely not right for sensitive data.

I admit I didn’t think about the fact that Flash can straight up perform Javascript - d’oh. Still these strings will typically end up being shorter than if you did a base64 encoding on a JSON string, plus maybe my little brother learned something. :stuck_out_tongue: