Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added resuming of game from localstorage #75

Merged
merged 1 commit into from
Mar 22, 2014

Conversation

mfrederiksen
Copy link
Contributor

Hi, I've added support for persisting and reloading the game stage. On each move, the GameManager and Board state are serialized to LocalStorage (just before the Actuator runs). On load if the game state is present, the GameManager and Board are initialized based on the saved state, otherwise they initialize normally. Since reloads do not reset the game anymore, I've added a New Game button above the board (the styling and placement, I'm not crazy about).

This, along with the cache manifest PR would make for a 100% offline app.

Added rebuilding of game state from localstoage on page load
Added New Game button to allow game restarts now that refreshes resume the game
@iirelu
Copy link
Collaborator

iirelu commented Mar 19, 2014

This has been discussed before and every time it's boiled down to the inherent complexity of a save system not having enough value for a game like 2048. It's like being able to save Hotline Miami or Super Hexagon.

Remember this is a game as well as an open-source project; Every feature needs to be carefully considered. I want to be able to link this game in a year and not be worried about if the game has completely changed by that point.

@mfrederiksen
Copy link
Contributor Author

So, the reason I wrote this in, was specifically because I was playing 2048 on iOS using an "Add to home" link. This is actually a very handy way to play on iPad/iPhone. The problem is that navigating away (say for a call) or even locking/unlocking, forces a refresh killing your game.

I realize that's a rather specific use case of the game but a valid one IMHO and together with resume/offline web app (cache manifest), a perfect way to deploy on mobile devices.

In any event, I think the save system is actually very simple. I'm not familiar with Hotline Miami or Super Hexagon but given that 2048 is a turn-based strategy with (often) a longer playing time, I can't see why resuming wouldn't be a good thing to have.

@gdelazzer
Copy link

I have also wanted to have this feature as I have lost my party started in the subway and had a quite big score on it :)

@iirelu
Copy link
Collaborator

iirelu commented Mar 19, 2014

If the implementation is near invisible and done well enough I imagine it could work. How well does local storage work on mobiles?

@mfrederiksen
Copy link
Contributor Author

Implementation is invisible to the end-user. Simply loading the page will attempt to resume a game if available. The only difference to game play is the addition of a New Game button.

As for support, localstorage has been supported on all browsers (both desktop and mobile) for a long time. See the following table http://caniuse.com/#feat=namevalue-storage

Also, localstorage was already being used to store high scores, I simply added a second field for game state. The pre-existing implementation has detection for browsers that do not support localstorage and provides a mock which will allow the game to work exactly as it does today.

@iirelu
Copy link
Collaborator

iirelu commented Mar 19, 2014

👍
Some of the other saving implementations attempted were horrible, this is heavenly by comparison.

@mpassell
Copy link

I'm so glad someone worked on this! I've had so many games where I've gotten pretty far along (at least a 512 & a 256 on the board), only to have the browser page refresh and reset my game when I return to it.

@sukima
Copy link

sukima commented Mar 21, 2014

👍

@gabrielecirulli
Copy link
Owner

The code looks good. Gonna work on merging. 👍

@johankj
Copy link
Contributor

johankj commented Mar 22, 2014

Oh, finally… I've been wishing for this feature for couple of days too – mostly because of the reason mentioned by @mfrederiksen.
I was thinking of doing it this weekend, but there's no need for that, as @mfrederiksen already has done it in a clean and simple manner. 👍

@gabrielecirulli gabrielecirulli merged commit 02a24c0 into gabrielecirulli:master Mar 22, 2014
@gabrielecirulli
Copy link
Owner

It's online now. Testing is appreciated 😄

@iirelu
Copy link
Collaborator

iirelu commented Mar 22, 2014

When you lose, the save should probably be reset. It's pretty jarring when it loads from a lost state and says game over, and it makes sense for it to then reset on the next load.

@gabrielecirulli
Copy link
Owner

Great idea Anna, done 👍 (please give it a try if you have some time to spare)

@gabrielecirulli
Copy link
Owner

It's gonna require some refreshing before you get the latest version, or you can also test in incognito mode, which has no cache.

@iirelu
Copy link
Collaborator

iirelu commented Mar 22, 2014

Ctrl+F5 also works for clearing the cache.

The resuming works perfectly, even when stress-testing it by moving and refreshing very rapidly, and the save is properly cleared when you lose. Tested in both Chrome beta and Firefox.

@gabrielecirulli
Copy link
Owner

Thank you for testing! 😄

@mfrederiksen
Copy link
Contributor Author

Awesome! Thanks for merging. I've been "testing" (playing :) quite a bit on iOS (iPhone/iPad) and everything looks good.

@mpassell
Copy link

I've added a direct link to it on my home screen using Chrome for Android. It works perfectly almost all of the time, but occasionally just comes up with a blank screen. If I kill it and reopen, it works fine and pulls the correct state out of local storage. I'm not sure how I'd diagnose the issue and it might just be some sort of funkiness with my Galaxy S4. Unless others are experiencing the same thing, it probably doesn't merit a lot of digging/research.

@gabrielecirulli
Copy link
Owner

Fully blank screen or empty board?

@mpassell
Copy link

A fully blank screen of the same color as the normal page's background color.

@gabrielecirulli
Copy link
Owner

Weird.

@mpassell
Copy link

mpassell commented Apr 3, 2014

I completely agree. If I see the problem again, I'll try to turn on USB debugging and see if I find out anything useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants