Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Javascript World-to-String script  (Read 1065 times)
0 Members and 2 Guests are viewing this topic.
Offline Eli Delventhal
« League of Dukes »

JGO Kernel
*****

Posts: 3574
Medals: 44


Game Engineer


« on: 2010-07-21 16:49:40 »

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

See my work:
OTC Software
<br />
Currently Working On:
Secret project...
Quote from: _Riven
I edit JGO in production, because I simply don't waste time writing bugs
Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #1 on: 2010-07-21 19:28:53 »

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.

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline Eli Delventhal
« League of Dukes »

JGO Kernel
*****

Posts: 3574
Medals: 44


Game Engineer


« Reply #2 on: 2010-07-22 05:50:03 »

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.

See my work:
OTC Software
<br />
Currently Working On:
Secret project...
Quote from: _Riven
I edit JGO in production, because I simply don't waste time writing bugs
Games published by our own members! Go get 'em!
Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #3 on: 2010-07-22 11:50:17 »

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/

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline Markus_Persson

JGO Kernel
*****

Posts: 2092
Medals: 10


Mojang Specifications


« Reply #4 on: 2010-07-22 16:48:43 »

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.

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

Play Minecraft!
Offline woogley

JGO Neuromancer
****

Posts: 1098
Medals: 5



« Reply #5 on: 2010-07-22 17:05:07 »

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.

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.
Offline Eli Delventhal
« League of Dukes »

JGO Kernel
*****

Posts: 3574
Medals: 44


Game Engineer


« Reply #6 on: 2010-07-23 10:19:38 »

Unless they also change the last letter, you mean? It's still possible to edit the string to get anything you want.
Yes yes, but then they have to do math. Smiley

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

See my work:
OTC Software
<br />
Currently Working On:
Secret project...
Quote from: _Riven
I edit JGO in production, because I simply don't waste time writing bugs
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.103 seconds with 19 queries.