Friday, June 28, 2013

Amazingly Long HTTP Requests

Hi Internet!

By now you probably know that we are making a TCCG sandbox. The git repo is setup (here's a link to it: https://github.com/IndieGameDev/tccgsandbox) and having exchanged emails with my good friend Alex (touched on some repo organization points) first tiny bit of structure has taken shape. So what now?

Today I have been doing something interesting on my way to work. I was thinking: what is a major common part among the games we would like to play? And I came to a conclusion that it is player's turn. Actually all games can be divided by this factor. On one side you have all your action oriented and sports games (like football, Counter Strike or trying to win the attention of a girl you like) - actions in such games are taken simultaneously and are only limited by laws of physics or economics. On the other side we have turn based games (chess, card games, stuff we'd like to make a sandbox for). These tend to be in our interest group.

This lead me to thinking: what is the major problem with developing turn-based games that support multiple people. Especially if we consider this in the realm of HTTP requests - polling would spring into mind. So what's the problem?


The "Donkey" problem

When one player needs to know if it is his turn - he needs to ask game server about it. Traditionally it is done by setting up an interval and just asking "Is it my turn yet?"
"Is it my turn yet?"
"Is it my turn yet?"
"Is it my turn yet?"
"Is it my turn yet?"
....
This can get really annoying, really quickly! (by annoying I mean, overhead in sending extra HTTP headers just to get a negative response and also killing mobile device battery life!).

So what can we do instead?


The solution

First of all we need something that doesn't require a fancy pants push notifications server! Why? These things are expensive to implement (time issue) and maintain (money issue). We are an indie game shop after all!

We also want something that doesn't require a LOT of socket management to keep connections from closing.

Finally, it should be easy to swap out if needed!

So here's what I'm thinking:

The server
For the sake of illustration our server script will look like this:

<?php
    sleep(rand(1,10));
    echo("[".date("h:m:s")."] Here is a server response!");
?>

It is very simple. It waits for a random number of seconds and then returns a message with time.

Now the client

This is where the magic is! (using DojoToolkit here).

<script>
function smartLongRequestHandler(){
    dojo.xhrGet({url:"serverScript.php", async:true}).then(
        function(data){
            console.log("success: "+data);
            setTimeout(smartLongRequestHandler, 0);
        },
        function(data){
            console.log("error: "+data+" server probably timed out!");
            setTimeout(smartLongRequestHandler, 0);
        }
    );
}
smartLongRequestHandler();
</script>

So what happens?
First we are calling our server asynchronously and are waiting for response. It can take between 1-10 seconds for us to get one. So connection just sits there open. Then, as soon as we get something back (or fail on server timeout) we re-queue! and wait again. This is that simple.


So what does it win us? 

Say, for the sake of argument, we have 1 sec delay tolerance. In polling implementation we'd have to check the server every second for message. The request/response timeline would look like this

1s   Client: are we there yet? Server: NO!
2s   Client: are we there yet? Server: NO!
3s   Client: are we there yet? Server: YES! Here is your message!
4s   Client: are we there yet? Server: NO!
5s   Client: are we there yet? Server: NO!
6s   Client: are we there yet? Server: YES! Here is your message!
7s   Client: are we there yet? Server: NO!
8s   Client: are we there yet? Server: NO!
9s   Client: are we there yet? Server: NO!
10s Client: are we there yet? Server: YES! Here is your message!

So we get 10 request/responses and 3 meaningful messages! Yay!

Now in our implementation

1s   Client: are we there yet? 
3s   Server: YES! Here is your message!
4s   Client: are we there yet?
6s   Server: YES! Here is your message!
7s   Client: are we there yet?
10s Server: YES! Here is your message!
 
So we get 3 request/responses and 3 meaningful messages!

100% effectiveness (if server doesn't timeout, but we'll talk about it tomorrow!) vs 33% effectiveness.

The choice is yours!

Thursday, June 27, 2013

The Plan

Hi there! Wonder how you got here?

Well, no matter. Since you are already here, why don't you stay awhile and listen...

So I a good start would be telling you a little bit about me (given that you care). I'm Max. I write code for living: websites, hybrid mobile apps, so native apps - all for entertainment, financial, private sectors. All this is done in many different languages. PHP, Flex/ActionScript, Java, Perl, Objective-C, recently JavaScript! When I'm not writing code, I spend time with my beautiful wife and daughter - and when I'm not doing that, I game. It is this simple in my life (kinda). So I've been playing games for years now, since I was 14 really: FIFA98 was my first one, then NFS, Might and Magic, Heroes of Might & Magic, countless other titles, Guild Wars, World of Warcraft, GW2. I also love board games (Elder Signs anyone?).

So enough about me. Why am I writing all this? See... I like coding things, but I would also want to combine the coding with gaming, as I love both. So I had this idea... I'm spending so much time each day on commute: 43 - 60 mins one way usually. Toronto transit FML... So why don't I get my laptop out and code for roughly that one hour on my way to work (coding on my way home is no option - too crowded). And who knows, in some time I'm bound to have a game done!

So again, why am I telling the Internet about it? Well... To keep myself honest and actually work for that one hour.

So here's THE GOAL: 

I would like to make a TCCG (tradable collectible card game) Sandbox that will run on tablets and desktops and maybe phones with large screen resolution (retina!). This will include android and iOS devices on tablets and chrome-like browsers on desktops. Sorry IE users - it's time you upgrade to better!

Why a TCCG Sandbox?

Well, I've been reading some board game forums with all these people and their cool ideas that usually end with "If only I could make such a game". Guess I can make it come true! Enter: the Sandbox. How is it different from an actual game... Well, glad I made you ask! Sandbox will let others define cards, rules, winning conditions etc. with some limitations, but without coding!


Will there be money in it?

Who knows! I'm not doing it for cash, more for an opportunity to make something I like to use myself! However, I wouldn't shy away from putting it up on Kickstarter or IndieGoGo, if I think I need to put some extra money into this (hosting, graphics, some dev help). Again, all money to go into the game.


Can You join My project?

If you'd like! This is open source anyways - so that's the idea. It is up to you - but drop me a line if you'd like to help. And this is not just for coders. Wanna do graphics? Design game rules? Handle PR and promote? Have some other ideas? Drop me a line - maxim.drozd (weird a sign) gmail.com or leave a comment below!


Thats all for this post folks! See you tomorrow!