All Projects → azerion → phaser-super-storage

azerion / phaser-super-storage

Licence: MIT license
A cross platform storage plugin for Phaser

Programming Languages

typescript
32286 projects
HTML
75241 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to phaser-super-storage

Store
A better way to use localStorage and sessionStorage
Stars: ✭ 1,646 (+3259.18%)
Mutual labels:  storage, localstorage, namespace
Proxy Storage
Provides an adapter for storage mechanisms (cookies, localStorage, sessionStorage, memoryStorage) and implements the Web Storage interface
Stars: ✭ 10 (-79.59%)
Mutual labels:  storage, cookie, localstorage
Brownies
🍫 Tastier cookies, local, session, and db storage in a tiny package. Includes subscribe() events for changes.
Stars: ✭ 2,386 (+4769.39%)
Mutual labels:  storage, cookie, localstorage
Vue Warehouse
A Cross-browser storage for Vue.js and Nuxt.js, with plugins support and easy extensibility based on Store.js.
Stars: ✭ 161 (+228.57%)
Mutual labels:  storage, cookie, localstorage
Depot.js
📦 depot.js is a storage library with a simple API
Stars: ✭ 247 (+404.08%)
Mutual labels:  storage, localstorage
elm-storage
Unified interface for accessing and modifying LocalStorage, SessionStorage and Cookies
Stars: ✭ 13 (-73.47%)
Mutual labels:  cookie, localstorage
phaser-particle-editor-plugin
This plugin creates particles based on JSON data generated by Phaser Particle Editor
Stars: ✭ 28 (-42.86%)
Mutual labels:  phaser, phaser-plugin
phaser-plugin-advanced-timing
Shows FPS, frame intervals, and performance info. Phaser 2/CE
Stars: ✭ 25 (-48.98%)
Mutual labels:  phaser, phaser-plugin
Remotestorage.js
⬡ JavaScript client library for integrating remoteStorage in apps
Stars: ✭ 2,155 (+4297.96%)
Mutual labels:  storage, localstorage
Client-Storage
🗄 Bulletproof persistent Client storage, works with disabled Cookies and/or localStorage
Stars: ✭ 15 (-69.39%)
Mutual labels:  cookie, localstorage
ng2-storage
A local and session storage wrapper for angular 2.
Stars: ✭ 14 (-71.43%)
Mutual labels:  storage, localstorage
phaser-plugin-game-scale
Scale or resize the game canvas. Phaser v3.15 only
Stars: ✭ 35 (-28.57%)
Mutual labels:  phaser, phaser-plugin
Flutter localstorage
📦flutter localstorage for ios/android/desktop/web
Stars: ✭ 206 (+320.41%)
Mutual labels:  storage, localstorage
phaser-plugin-scene-graph
Prints the display tree. Phaser 2/CE
Stars: ✭ 32 (-34.69%)
Mutual labels:  phaser, phaser-plugin
Store.js
Cross-browser storage for all use cases, used across the web.
Stars: ✭ 13,656 (+27769.39%)
Mutual labels:  storage, localstorage
phaser-ui-comps
Phaser 3 UI Components built by Adobe Animate
Stars: ✭ 60 (+22.45%)
Mutual labels:  phaser, phaser-plugin
craft
Phaser Library with utility chainable functions
Stars: ✭ 27 (-44.9%)
Mutual labels:  phaser, phaser-plugin
phaser-plugin-debug-arcade-physics
Draws properties of Arcade Physics bodies. Phaser 2/CE
Stars: ✭ 18 (-63.27%)
Mutual labels:  phaser, phaser-plugin
React Storage Hooks
React hooks for persistent state
Stars: ✭ 146 (+197.96%)
Mutual labels:  storage, localstorage
h5webstorage
Web Storage for Angular 2
Stars: ✭ 38 (-22.45%)
Mutual labels:  storage, localstorage

Phaser Super Storage

A cross platform pluggable storage plugin for Phaser.

Key features:

  • Cross browser support
  • Cookie Fallback
  • Support for iframes with helper script
  • Support for custom storage adapters

Requirements

  • If you use TypeScript, also include the types for es6-promise in your project

Getting Started

First you want to get a fresh copy of the plugin. You can get it from this repo or from npm, ain't that handy.

npm install @azerion/phaser-super-storage --save-dev

Next up you'd want to add it to your list of js sources you load into your game

<script src="path/to/phaser-super-storage.min.js"></script>

After adding the script to the page you can activate it by enabling the plugin:

game.add.plugin(PhaserSuperStorage.StoragePlugin);

Usage

When you load the plugin, it automatically checks for availability of localStorage and fallbacks to cookies if it's not available. Both of these are StorageAdapters and will be overwritten if you register a custom StorageAdaper, but more on this later.

The plugin will append the Phaser game object with a storage object, you can reference this object with exactly the same API as localStorage, and should therefore be fairly easy for you to implement.

//Store Tetris at FavoriteGame
game.storage.setItem('FavoriteGame', 'Tetris');

//Get FavoriteGame
var favoriteGame = game.storage.getItem('FavoriteGame');  // Tetris

//Remove FavoriteGame
game.storage.removeItem('FavoriteGame');

//get the length of all items in storage
var l = game.storage.length;    // 1

//Get the name of the key at the n'th position
var keyName = game.storage.key(0); // FavoriteGame

//Clear all keys
game.storage.clear();

Namespaces

If you are like us, and put multiple games on the same domain, you might want to add namespaces to your localStorage. Namespaces get prepended to any key value pair you set, and all API calls to the storage object are segregated by namespaces. This allows you to set a 'score' key for multiple games on the same domain, and they'll always get their own stored value

game.storage.setNamespace('tetris');
game.storage.setItem('score', 250);

game.storage.setNamespace('pong');

//Length also takes namespaces into account
var l = game.storage.length;    // 0

//this won't do because the score was registered under a different namespace
var value = game.storage.get('score'); // null

Promises

Both Cookies and localStorage work synchronous, meaning you immediately get a return value after calling a function, e.g; getItem('key'); But when you are using a HTTP service (Amazon Cognito Sync, or custom REST server) or when you are using the iFrameStorage supported by this library, the results are coming back in an asynchronous manner. In order for you to parse your result nicely phaser-super-storage uses Promises to get you the result.

It is also possible to enable promises on the Cookie and localStorage adapters by setting forcePromises to true.

//classical way of getting your item
var item = game.storage.getItem('key');

//Now we are gonna force promises
game.storage.forcePromises = true;
game.storage.getItem('key').then(function (item) {
    //do something with the item here
});

Adapters

The actual Storage of content happens within these so-called StorageAdapters. Basicly a StorageAdapter can store data somewhere, as long as it implements the following interface:

 interface IStorage {
    //Promises or no Promises
    forcePromises: boolean;

    //The amount of items in the storage
    length: number;

    //The namespace for the current storage
    namespace:string;

    //Get an item from the storage
    getItem(key: string): any | Promise<any>;

    //remove an item from the localStorage
    removeItem(key: string): any | Promise<any>;

    //Set an item in the storage
    setItem(key: string, value:any): void | Promise<void>;

    //Get the n'th key
    key(n: number): any | Promise<any>;

    //empty the (namespaced) storage
    clear(): void | Promise<void>;

    setNamespace(namespace: string): void | Promise<void>;
}

Local & Cookie Storage

The default usage of phaser-super-storage require the LocalStorage and CookieStorage adapter. It will always try to use the LocalStorage Adapater, but when all fails it falls back to Cookie storage, no configuration needed!

Cordova

You can now also use the CordovaStorage adapter, which uses the NativeStorage plugin of Cordova. This prevents the auto-deletion of data on IOS when not having enough memory. If you are using the adapter, please note that passing the namespace in the constructor is not allowed and that it is only testable in a Cordova application. It can be enabled by the following command:

game.storage.setAdapter(new PhaserSuperStorage.StorageAdapters.CordovaStorage());

Iframe

We publish our games on HTML5 game portals through the usage of iframes, a downside of this is that for iOS both localStorage and Cookies aren't persisted for iframes. In order to counter this we included an IframeStorage adapter that should be set in the game, then the helper script included in the build folder should be loaded in the parent frame. This way we'll utilize the storage capacity of the parent frame to store our data

<!--So in the parent frame we include the following: -->
<script src="http://cdn.fbrq.io/phaser-super-storage/phaser-storage-helper.min.js" type="text/javascript"></script>
//Then in our game we add the iframe adapter
var iframeAdapter = new IframeStorage(
    '',                     //The namespace to store the data under
    document.referrer       //Then url of the parent domain, you need this for security reasons
);

//We call init first to see if the helper script is available, result as a Promise due to asynchronous communication
iframeAdapter.init().then(function() {
    //It succeeded! Now set the iframe adapter as the main storage adapter
    game.storage.setAdapter(iframeAdapter);
}).catch(function (e) {
    //failed to start communication with parent, so lets enable promises on the original storage adapter to keep the API the same
    game.storage.forcePromises = true;
});

Caveats

Although we try our best to store data, in some cases you can consider data lost when a user closes his browser or ends his session. I'm talking of course about private browsing. Both LocalStorage and Cookies will be cleared, so if you want to keep userdata alive there I suggest you try to get people to login and use a custom StorageAdapter to save the data server-side. Please note that we use the colon as namespace appendix, so we advice you not to use it yourself.

Disclaimer

We at Azerion just love playing and creating awesome games. We aren't affiliated with Phaser.io. We just needed to storage some awesome data in our awesome HTML5 games. Feel free to use it for enhancing your own awesome games!

Phaser Super Storage is distributed under the MIT license. All 3rd party libraries and components are distributed under their respective license terms.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].