Pull Requests continued

As promised from last week, i'm submitting a second PR to the same repo as last week. In my last weeks blog post which you can find here, i was talking about contributing to a repo for a simple web based board game. Last weeks task was easy. All i had todo was find a way to save the state of the game so it can be loaded later. That was the easy part, now i have to load that data from the users machine and find a way to actually load the game exactly where the user left off.

The difficult part of this weeks task is not loading the data, but actually loading the game so that it's exactly where the user left off when they saved. First i started off by actually accessing indexedDB to retrieve the game state from the users machine which was pretty simple after reading some of googles documentation. I had to load the data in such a manner that if something went wrong, or there was just no game states saved, the user would be notified. I also used this time to write a little extra code to notify the user when they are saving the game that they will overwrite their previous game state. I chose todo this because for a simple project like this, theres no need to have multiple game states saved and then given multiple options on which one to load. That would add a lot of unnecessary complexity.

Now comes the hard part, using the loaded data to load the game in the same state that it was saved in. Of course this doesn't sound too bad, but i needed to achieve this in such a way that i would be changing the least amount of code from the original project. In general its best practice to try and write the least amount of code, that way you reduce the chances of introducing a new bug. Also when contributing to other peoples projects, its best to try not touch parts of the code that you don't need to touch, and if you absolutely have to, try and change as little as possible.

With that said, the original game creator had split up his code into multiple small functions that would call each other in sequence, so when the web page would load initially, it would make one call to the "initialize" function and that function would call the other functions. So i needed to inject the game state data at the very beginning so i wouldn't have to disturb the rest of the code.  So my only option was to declare a global variable and when the user presses the load button, the game state data would be saved to that variable. Then i could just add a small condition inside the first initialize function that would check to see if there was a game state that was loaded into the variable that we declared. If there wasn't, then it would simply continue on same as before and initialize an empty board, but if there was a game state available, it would use the data from that to create the board.

It sounds a lot simpler now that i'm writing this blog but it actually took some time to find a nice and clean way to achieve this without having to rewrite a bunch of the other functions. Just like last week, i tested the code to ensure it was working the way it should and i submitted another pull request to the original project owner. Im not sure what i'll be doing for next weeks contribution but be sure to come back and check it out.

Comments

Popular posts from this blog

Hacktoberfest is just around the corner

Finding issues by accident

Filing issues and pull requests using git hub