Saving & Loading Match Data (OR) Why I’m gonna have to use my own server for storing information. /sadface
I can now save & load the match data (that is, who played what and when). I try to condense information as much as possible when saving because when you send match data to Apple’s Game Center servers, that data cannot exceed 4K bytes.
What this means, in turn, is that when the data is received from the server the program then has to reconstruct the entire timeline out of that very, very minimalist dataset. It goes through and creates all the layers (see earlier posts) and other stuff.
Before I do anything with Game Center (I haven’t even [re]built the interface yet after throwing away what I had before), I, being a statistician, decided to collect some data. I made the program save & load different combinations (how many players, how many events each player plays every turn, etc.) to get a clear picture of how the number of events played impacts
- the file size and
- the execution time of the “timeline reconstruction” procedure.
There is a strongly linear, positive relationship between # of events and size. Simple linear regression indicates that there is an increase of ~33 bytes for every extra event. Using the model, we can predict that a timeline with 111 events results in almost 4K bytes (predicted: ~3981 bytes). We can’t go higher than 111 events because the file size ends up being over 4K bytes when we have 112 events.
In fact, when I create 111 events and save the match data, I end up with a file that weighs drumroll 3999 bytes! Isn’t math great?
This is troublesome. Why? Because in the first & third trimesters of the match (turns 1-8 & turns 17-24, respectively) each player can play at most 2 events into every turn. In the second trimester (turns 9-16) there can be at most 3 events played into every turn by each player. Suppose we have a 4 player match:
( (8 + 8) * 2 + 8 * 3 ) * 4 = 224 events total
What happens when 224 events are played? Well, I ran the code that saves the match data and we end up with a file size of 7586 bytes :(
When an iOS game uses Game Center’s Turn-Based API, it sends some information to Apple’s servers when a turn is submitted by a player. When other players open up that match to play, they receive that information from Apple and then the game processes it. ONE of the pieces of that information is the actual match data. That match data can be anything. If you can condense the entire state of the match into less than 4K bytes, then you can just do that.
However, if you can’t get below that limit, then you can send the state of the game to your own server and then your match data would just be the address of where the actual match data is on the internet. You would then download the match data separately when you open the match to make your turn.
The amount of time it takes to actually reconstruct the timeline from the match data is exponential and gets pretty intense as the number of events increases. How long did it take to reconstruct a timeline from 224 events? 57.395168 seconds. Almost a minute to load up the match and start playing (although technically that game is over at that point).
Actually, it’s probably gonna take longer than that because all these calculations are performed on the latest generation of Mac mini. Is it fair to ask people to wait (I’m estimating here) 2 minutes just to make a turn? Well, I’ve waited longer whenever I launched Infinity Blade and Infinity Blade 2. I think I’m gonna have to make it so that there is an internal cache of matches so that there is least wait between opening match and being able to play.
Also keep in mind that I played only 1 specific event which I created just for this data-gathering experiment. Most events wouldn’t be as complex and intense as that specific one, but it’s better to be safe than sorry.
Hopefully this was somewhat insightful to you guys.