2016-07-29 14:19:18 -04:00
This guide will describe how FullScreenShenanigans projects keep track of game information.
# ItemsHoldr
2017-01-27 01:19:31 -08:00
The ItemsHoldr module is a wrapper around localStorage.
It lets GameStartr instances store data locally with default values and value-based event handlers.
2016-07-29 14:19:18 -04:00
## Local Storage
2017-01-27 01:19:31 -08:00
Other websites also use `localStorage` , so ItemsHoldr has a prefix property to differentiate its information.
2016-07-29 14:19:18 -04:00
2017-01-27 01:19:31 -08:00
```javascript
const itemsHolder = new ItemsHoldr ({
2016-07-29 14:19:18 -04:00
prefix : "FullScreenShenanigans"
});
```
## Items
To add, update, and remove an item in the collection, use `addItem` , `setItem` , and `removeItem` respectively.
2017-01-27 01:19:31 -08:00
```javascript
2016-11-26 01:45:46 -05:00
itemsHolder . addItem ( "color" , { value : "red" });
itemsHolder . setItem ( "color" , "purple" );
itemsHolder . removeItem ( "color" );
2016-07-29 14:19:18 -04:00
```
`increase` and `decrease` add and subtract from the item's value.
`toggle` flips the boolean value of the item.
2017-01-27 01:19:31 -08:00
```javascript
2016-11-26 01:45:46 -05:00
itemsHolder . increase ( "weight" , 14 );
itemsHolder . decrease ( "weight" , 10 );
itemsHolder . toggle ( "started" );
2016-07-29 14:19:18 -04:00
```
`checkExistence` can be used to check if there is an item under the specified key and if not, add it to the collection.
2017-01-27 01:19:31 -08:00
```javascript
2016-11-26 01:45:46 -05:00
itemsHolder . checkExistence ( "color" );
2016-07-29 14:19:18 -04:00
```
By default if an item does not exist when `setItem` , `getItem` , `increase` , or `toggle` are called, the item is then added to the collection.
Define `allowNewItems` to explicitly enable or disable this feature.
2017-01-27 01:19:31 -08:00
```javascript
const itemsHolder = new ItemsHoldr ({
2016-07-29 14:19:18 -04:00
allowNewItems : false
});
```
To manually update a changed item's value in localStorage, use `saveItem` .
To save all the items' values to localStorage, use `saveAll` .
2017-01-27 01:19:31 -08:00
```javascript
2016-11-26 01:45:46 -05:00
itemsHolder . saveItem ( "color" );
itemsHolder . saveAll ();
2016-07-29 14:19:18 -04:00
```
### Auto Save
Auto saving, which updates an item's value in localStorage when the value is changed, can be enabled when making the ItemsHolder container.
2017-01-27 01:19:31 -08:00
```javascript
2016-11-26 01:45:46 -05:00
const itemsHolder = new ItemsHoldr ({
2016-07-29 14:19:18 -04:00
autoSave : true
});
```
By default, this is disabled.
To toggle autoSave, use `toggleAutoSave` .
2017-01-27 01:19:31 -08:00
```javascript
2016-11-26 01:45:46 -05:00
itemsHolder . toggleAutoSave ();
2016-07-29 14:19:18 -04:00
```
## Updating
Triggers are functions that get run when an item's value equals the specified value.
2017-01-27 01:19:31 -08:00
```javascript
2016-11-26 01:45:46 -05:00
itemsHolder . addItem ( "color" , {
2016-07-29 14:19:18 -04:00
value : "cyan" ,
triggers : {
red : () => { this . value = "black" ; }
}
});
2016-11-26 01:45:46 -05:00
itemsHolder . setItem ( "color" , "red" );
2016-07-29 14:19:18 -04:00
```
Modularity restricts an item's value to a range from 0 to the specified max.
When the value is changed, the `onModular` function is run `x / modularity` times, where `x` is the number the value was set to.
Each time the function is run, value gets set to `value % modularity` .
2017-01-27 01:19:31 -08:00
```javascript
2016-11-26 01:45:46 -05:00
itemsHolder . addItem ( "counter" , { value : 0 });
itemsHolder . addItem ( "time" , {
2016-07-29 14:19:18 -04:00
value : 0 ,
modularity : 12 ,
onModular : () => { ItemsHolder . increase ( "counter" , 1 ); }
});
2016-11-26 01:45:46 -05:00
itemsHolder . setItem ( "time" , 100 );
2016-07-29 14:19:18 -04:00
```
## Clearing and Defaults
To clear all items from the collection, use `clear` .
2017-01-27 01:19:31 -08:00
```javascript
2016-11-26 01:45:46 -05:00
itemsHolder . clear ();
2016-07-29 14:19:18 -04:00
```
To clear the collection, but keep some items which will always be in the collection, use `valueDefault` .
2017-01-27 01:19:31 -08:00
```javascript
const itemsHolder = new ItemsHoldr ({
2016-07-29 14:19:18 -04:00
values : {
color : {
valueDefault : "red"
}
}
});
ItemsHolder . clear (); // "color" is still in the collection with a reset value of "red"
```
## Elements
*Note:* This part of ItemsHoldr will be placed into another module in the future.
To signal if a container should be made to hold HTML elements, assign a value to `doMakeContainer` .
2017-01-27 01:19:31 -08:00
```javascript
const itemsHolder = new ItemsHoldr ({
2016-07-29 14:19:18 -04:00
doMakeContainer : true
});
```
Changes to element values will show on the page if the container is appended to an element on the page.
To signal if an item is an element, assign `hasElement` to true.
2017-01-27 01:19:31 -08:00
```javascript
const itemsHolder = new ItemsHoldr ({
2016-07-29 14:19:18 -04:00
doMakeContainer : true ,
values : {
color : {
valueDefault : "red" ,
hasElement : true
}
}
});
```
### Display Changes
If you want certain values to be represented differently on the page, use `displayChanges` .
These changes are hardcoded values that replace specified values when element items are updated.
2017-01-27 01:19:31 -08:00
```javascript
const itemsHolder = new ItemsHoldr ({
2016-07-29 14:19:18 -04:00
displayChanges : {
Infinity : "INF" ;
}
});
2016-11-26 01:45:46 -05:00
itemsHolder . addItem ( "limit" , {
2016-07-29 14:19:18 -04:00
value : "4000" ,
hasElement : true
});
2016-11-26 01:45:46 -05:00
itemsHolder . setItem ( "limit" , "Infinity" );
2016-07-29 14:19:18 -04:00
```
`hasDisplayChange` checks to see if a value has a recorded change and `getDisplayChange` returns the entry to replace the value with.
2017-01-27 01:19:31 -08:00
```javascript
2016-11-26 01:45:46 -05:00
itemsHolder . hasDisplayChange ( "Infinity" );
const newValue : string = ItemsHolder . getDisplayChange ( "Infinity" );
2016-07-29 14:19:18 -04:00
```
### Container Visibility
`hideContainer` hides the container from view and `displayContainer` makes the container visible.
2017-01-27 01:19:31 -08:00
```javascript
2016-11-26 01:45:46 -05:00
itemsHolder . hideContainer ();
itemsHolder . displayContainer ();
2016-07-29 14:19:18 -04:00
```
More can be read about ItemsHoldr on its [Readme ](https://github.com/FullScreenShenanigans/ItemsHoldr/blob/master/README.md ).
2017-01-27 01:19:31 -08:00
2016-07-29 14:19:18 -04:00
# StateHoldr
2016-07-29 17:44:32 -04:00
StateHoldr is a module for tracking changes of items in ItemsHolder in objects called collections.
Changes are key-value pairs of attributes for items and values.
A collection describes how named items in a group have been changed. For each item in the group, the collection will store a key-value pair of attributes and new values.
For example, a garage collection that contains a "car" item with "color" and "name" changes could be described as:
2017-01-27 01:19:31 -08:00
```javascript
2016-11-26 01:45:46 -05:00
const garage : { [ i : string ] : { [ i : string ] : string } } = {
2016-07-29 17:44:32 -04:00
car : {
color : "red"
}
};
```
Collections allow for various attributes for a single item to be grouped together versus storing individual items.
## Prefix
Like ItemsHoldr, StateHoldr has its own prefix property.
All collections in StateHoldr are stored in ItemsHoldr prepended with the StateHoldr prefix.
2017-01-27 01:19:31 -08:00
```javascript
const stateHolder = new StateHoldr ({
2016-07-29 17:44:32 -04:00
ItemsHolder : new ItemsHoldr (),
prefix : "StateHolder"
});
```
## Collections
The collections the module has recorded are stored in a list keyed by `stateCollectionKeys` in ItemsHolder (no StateHoldr prefix prepended).
StateHoldr always has one collection as its current collection.
`setCollection` sets the current collection.
2017-01-27 01:19:31 -08:00
```javascript
2016-11-26 01:45:46 -05:00
stateHolder . setCollection ( "newCollection" , {
2016-07-29 17:44:32 -04:00
car : {
color : "red"
}
});
```
If the name passed in is the name of a collection that already exists, that collection's old values will be used.
2017-01-27 01:19:31 -08:00
```javascript
2016-11-26 01:45:46 -05:00
stateHolder . setCollection ( "newCollection" , {
2016-07-29 17:44:32 -04:00
car : {
color : "red"
}
});
2016-11-26 01:45:46 -05:00
stateHolder . setCollection ( "secondCollection" );
stateHolder . setCollection ( "newCollection" ); // car is still in this collection
2016-07-29 17:44:32 -04:00
```
Collections aren't put into ItemsHolder until they are saved.
`saveCollection` saves the current collection to ItemsHolder.
2017-01-27 01:19:31 -08:00
```javascript
2016-11-26 01:45:46 -05:00
stateHolder . saveCollection ();
2016-07-29 17:44:32 -04:00
```
## Changes
To add a change to the current collection, use `addChange` .
`addChange` takes in the key of the item, an attribute of the item, and the value being stored.
2017-01-27 01:19:31 -08:00
```javascript
2016-11-26 01:45:46 -05:00
stateHolder . addChange ( "car" , "color" , "red" );
2016-07-29 17:44:32 -04:00
```
A change to a specific collection can be added with `addCollectionChange` .
2017-01-27 01:19:31 -08:00
```javascript
2016-11-26 01:45:46 -05:00
stateHolder . addCollectionChange ( "otherCollection" , "car" , "color" , "red" );
2016-07-29 17:44:32 -04:00
```
To copy changes from an item in the current collection into a target object, use `applyChanges` .
2017-01-27 01:19:31 -08:00
```javascript
2016-11-26 01:45:46 -05:00
const recipient : { [ i : string ] : string } = {};
stateHolder . applyChanges ( "car" , recipient );
2016-07-29 17:44:32 -04:00
```
`getChanges` returns the changes for a specific item.
2017-01-27 01:19:31 -08:00
```javascript
2016-11-26 01:45:46 -05:00
const changes : { [ i : string ] : string } = StateHolder . getChanges ( "car" );
2016-07-29 17:44:32 -04:00
```
2016-11-26 01:45:46 -05:00
More can be read about StateHoldr on its [Readme ](https://github.com/FullScreenShenanigans/StateHoldr/blob/master/README.md ).