DIGG IT!
Published
Tuesday, November 13, 2007
at
10:00 AM
.
I have been working on a card game for AIR and just finished the API for the game. The API returns shuffled decks of cards with some parameters in the URL. In looking at the various formats, I chose delimited ASCII text. The question you are asking is why, well sometimes XML and AMF are overkill and honestly delimited ASCII text is fast to parse and easy to use/debug. On the client side in Flash Player, String.spilt is over 10x faster than both XML and XMLDocument in parsing speed and is linear in performance with size of the text being parsed. If you are simply passing an array of strings, you might want to look at using delimited text. It isn't glamorous but it works and is easily scalable on the server side with caching and compressed if your server supports gzip.
So here are the API details on the Card Shuffle API:
// Default URL
// 1 is the version # of the API
http://onflex.org/api/games/cards/shuffle/1/// Decks Parameter - Shuffle 3 decks of 52 cards each
http://onflex.org/api/games/cards/shuffle/1/3// Jokers Parameter - Shuffle 2 decks of 52 and add in 2 jokers per deck
http://onflex.org/api/games/cards/shuffle/1/2/2/The logic shuffles an array of cards randomly between 5-20 times and each shuffle uses a new random seed value. This makes it easy to build the game logic client side without having to worry about shuffle randomness and allows for multi-user. More on the multi-user behavior in a later post.
In Flex 3 I wrote a simple API Test client that shows the decks visually. The card graphics are loaded from a Flash CS3 SWF9 file dynamically
(more on the technique here) so that I can swap the card designs at runtime as a user preference. So here is the example with full source:
Card Shuffle API Tester in Flex 3Card Shuffle API Tester in Flex 3 SourceI used a simple HTTPService tag like so:
<mx:HTTPService
id="shuffleService"
resultFormat="text"
result="shuffleServiceResult( String( event.result ).split('|') )"
url="{'http://onflex.org/api/games/cards/shuffle/1/' + decks.value + '/' + jokers.value}"
/>Note that the resultFormat is set to 'text' and when the result event is fired I used String.split to parse the result into an Array. I used the "|" as the delimiter. If you look at the raw text results you will see a number in the first array position, this is milliseconds from 1/1/1970 (Epoch) and makes it easy to transform into a Date object via the constructor like so:
// shift a value off the array, turn it into a number and pass it to the Date constructor
shuffleTime = new Date( Number( data.shift() ) );
Basically this simple API allows me to get shuffled decks of cards for my upcoming AIR game. I will be posting more about the making of as things progress.
Cheers,
Ted :)
very good information
thank you
Hehe... I can't even remember the last time I saw you post game related code? I bet you are having a great time doing this for a change? ;)
It is a great change! With MAX behind me I am finding lots of time for blogging and writing some apps that have been burning a hole in my pocket.
Ted :)
Nice! :) Just added a SWX API for it to make it easier to use in Flash Lite etc.
Planning any other methods? :)
Cool. I'm in the midst of a 3-4 month card game project myself now... and will (in my shinning new blog) post some stuff I've learned.
I am curious why you need to shuffle several times? Mine's a two player p2p (that is, no server side code) so I have to use a seed. I don't see how the results will be any more random if I shuffle multiple times. In fact, the way I'm currently doing it is I don't shuffle the deck at all... but when a card is drawn, it's spliced from the deck using my randomizer.
For a small app like this game, it makes sense. But larger apps where you have many service calls, you really should use the same transport. Otherwise you'll be stuck with a mashup of formats to have to decipher... talk about a maintenance nightmare.
It would also be interesting to see the cost comparison between this and a json response.