Thursday, August 16, 2007

Tiny little bug

It's always the little ones.

Today I was developing the league ranking system, essentially a way for the game to record the finishing positions of all the teams in a league, and also for the teams to be ordered correctly. This can then be used by competitions to determine seedings, or collecting only the top 8 teams to play.

While testing this out I was frustrated for several hours by a bizarre error. At the end of the season, all the teams' rank information are updated accordingly. This is then followed by promotions and relegations, which involves updating the current level each team are playing at. Irritatingly, doing so tended to set the "rank level" - the level the team were playing at last season - to 0, the highest level available. As far as the game was then concerned, if Cardiff City won the Championship, they're ranked as high as Manchester United.

After a while I had traced it down to the exact function that sets the current level. A function that simply should not fail. I feared the worst - if a part of my database storage and retrieval code was broken, the entire database could be compromised. Sure, I have backups but I'd still need to find out the error, and if it's damaging I'd need to code an entire routine to reorganise the data and ensure it's fine.

Eventually, and naturally, I traced it to simply using the wrong variable at a crucial moment. My database works by only using the required storage for any given maximum data value in a field - if the field holds the numbers 0 to 15, it will use four bits. The next four bits in the byte will be used by something else. This enables me to store a great deal of data in a much smaller space. In order to store two values in a single byte, I need to be able to "mask out" the irrelevant data at any given point. This was the broken bit, with the code therefore ignoring the masking and wiping the entire bite before storing my data.

I think, but currently have no proof, that this error was the cause of a few other issues dotted here and there with some competitions "forgetting" what league they were part of if I edited them after setting the league up. All it would take is one editing of a preceding value and that data goes out the window.

No comments: