Prepare Session module to generalize its various properties and stop duplicating getters and savers
This commit is contained in:
parent
bed89a5daa
commit
a418ccedac
4 changed files with 75 additions and 50 deletions
|
@ -56,7 +56,7 @@ function Automaton(async, dom, messaging, screen, session, ui) {
|
|||
name: 'textSpeed'
|
||||
}
|
||||
};
|
||||
var game = session.getGame();
|
||||
var game = session.get('game');
|
||||
if(game != undefined) {
|
||||
menus.start.entries.unshift(
|
||||
{label: 'Continue', action: function() { messaging.send({tag: 'Resume', game: game}); }}
|
||||
|
@ -102,7 +102,7 @@ function Automaton(async, dom, messaging, screen, session, ui) {
|
|||
ui.frame('GameInit');
|
||||
messaging.send({tag: 'NewGame'});
|
||||
async.run(
|
||||
session.update(),
|
||||
session.syncGame(),
|
||||
async.bind(
|
||||
askName(),
|
||||
set('name'),
|
||||
|
|
|
@ -13,7 +13,7 @@ function Buttons(session) {
|
|||
};
|
||||
|
||||
document.addEventListener('keydown', function(event) {
|
||||
var button = session.getOptions().layout[event.key];
|
||||
var button = session.get('options').layout[event.key];
|
||||
if(button != undefined && mapping[button] != undefined) {
|
||||
event.preventDefault();
|
||||
mapping[button]();
|
||||
|
@ -30,7 +30,7 @@ function Buttons(session) {
|
|||
|
||||
function assign(key, button) {
|
||||
var layoutDiff = {};
|
||||
var layout = session.getOptions().layout;
|
||||
var layout = session.get('options').layout;
|
||||
for(var currentKey in layout) {
|
||||
if(layout[currentKey] == button) {
|
||||
layoutDiff[currentKey] = undefined;
|
||||
|
|
|
@ -1,33 +1,83 @@
|
|||
function Session(async) {
|
||||
var game = JSON.parse(localStorage.getItem('game'));
|
||||
var options = JSON.parse(localStorage.getItem('options')) || defaultOptions();
|
||||
var session = {
|
||||
character: JSON.parse(localStorage.getItem('character')),
|
||||
game: JSON.parse(localStorage.getItem('game')),
|
||||
options: JSON.parse(localStorage.getItem('options')) || defaultOptions()
|
||||
};
|
||||
|
||||
return {
|
||||
getOptions: getOptions,
|
||||
getGame: getGame,
|
||||
save: save,
|
||||
setOptions: setOptions,
|
||||
get: get,
|
||||
set: set,
|
||||
syncGame: syncGame,
|
||||
update: update
|
||||
};
|
||||
|
||||
function getGame() {
|
||||
return game;
|
||||
function defaultOptions() {
|
||||
var o = {
|
||||
layout: {
|
||||
'a': 'A',
|
||||
'b': 'B',
|
||||
'ArrowLeft': 'Left',
|
||||
'ArrowRight': 'Right',
|
||||
'ArrowUp': 'Up',
|
||||
'ArrowDown': 'Down',
|
||||
'Enter': 'Start',
|
||||
' ': 'Select'
|
||||
},
|
||||
textSpeed: 'medium'
|
||||
};
|
||||
save('options', o);
|
||||
return o;
|
||||
}
|
||||
|
||||
function update() {
|
||||
function get(path, obj) {
|
||||
obj = obj || session;
|
||||
if(Array.isArray(path)) {
|
||||
if(path.length == 0) {
|
||||
return obj;
|
||||
} else {
|
||||
var key = path.shift();
|
||||
return get(path, obj[key]);
|
||||
}
|
||||
} else {
|
||||
return obj[path];
|
||||
}
|
||||
}
|
||||
|
||||
function save(key, value) {
|
||||
if(key != undefined) {
|
||||
localStorage.setItem(key, JSON.stringify(value || session[key]));
|
||||
} else {
|
||||
for(var key in session) {
|
||||
save(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function set(newSession) {
|
||||
for(var key in newSession) {
|
||||
session[key] = newSession[key];
|
||||
save(key);
|
||||
}
|
||||
}
|
||||
|
||||
function syncGame() {
|
||||
return async.bind(
|
||||
messaging.receive(function(message) {
|
||||
return message.tag == "Game";
|
||||
}),
|
||||
function(message) {
|
||||
game = message.contents;
|
||||
session.game = message.contents;
|
||||
return async.wrap();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
function save() {
|
||||
localStorage.setItem('game', JSON.stringify(game));
|
||||
function update(newSession) {
|
||||
for(var key in newSession) {
|
||||
merge(session[key], newSession[key]);
|
||||
save(key);
|
||||
}
|
||||
}
|
||||
|
||||
function merge(a, b) {
|
||||
|
@ -52,30 +102,4 @@ function Session(async) {
|
|||
}
|
||||
}
|
||||
|
||||
function setOptions(newOptions) {
|
||||
merge(options, newOptions);
|
||||
localStorage.setItem('options', JSON.stringify(options));
|
||||
}
|
||||
|
||||
function defaultOptions() {
|
||||
var o = {
|
||||
layout: {
|
||||
'a': 'A',
|
||||
'b': 'B',
|
||||
'ArrowLeft': 'Left',
|
||||
'ArrowRight': 'Right',
|
||||
'ArrowUp': 'Up',
|
||||
'ArrowDown': 'Down',
|
||||
'Enter': 'Start',
|
||||
' ': 'Select'
|
||||
},
|
||||
textSpeed: 'medium'
|
||||
};
|
||||
localStorage.setItem('options', JSON.stringify(o));
|
||||
return o;
|
||||
}
|
||||
|
||||
function getOptions() {
|
||||
return options;
|
||||
}
|
||||
}
|
||||
|
|
17
src/js/ui.js
17
src/js/ui.js
|
@ -5,7 +5,7 @@ function UI(async, buttons, dom, screen, session) {
|
|||
medium: 50,
|
||||
fast: 20
|
||||
};
|
||||
var textDelay = textSpeeds[session.getOptions().textSpeed];
|
||||
var textSpeed = session.get('options').textSpeed;
|
||||
|
||||
return {
|
||||
animation: animation,
|
||||
|
@ -148,7 +148,9 @@ function UI(async, buttons, dom, screen, session) {
|
|||
var ready = false;
|
||||
var append = function(c) { return function() { screen.appendText(c); }; };
|
||||
for(var i = 0; i < characters.length; i++) {
|
||||
frames.push({action: append(characters[i]), delay: textDelay});
|
||||
frames.push(
|
||||
{action: append(characters[i]), delay: textSpeeds[textSpeed]}
|
||||
);
|
||||
}
|
||||
frames.push({action: function() { ready = true; screen.markAsRead(); }, delay: null});
|
||||
var remote = animation(frames);
|
||||
|
@ -210,7 +212,7 @@ function UI(async, buttons, dom, screen, session) {
|
|||
}
|
||||
|
||||
function setLayout() {
|
||||
var layout = session.getOptions().layout
|
||||
var layout = session.get('options').layout
|
||||
var buttonsMenu = {
|
||||
cancel: function() {},
|
||||
entries: [],
|
||||
|
@ -235,7 +237,7 @@ function UI(async, buttons, dom, screen, session) {
|
|||
screen.currentMenu().getElementsByClassName('selected')[0].textContent = keyPromptLabel(null, button);
|
||||
var promptKey = function(event) {
|
||||
var assignment = buttons.assign(event.key, button);
|
||||
session.setOptions({layout: assignment.layoutDiff});
|
||||
session.update({options: {layout: assignment.layoutDiff}});
|
||||
screen.clear(elem);
|
||||
screen.currentMenu().getElementsByClassName('selected')[0].textContent = keyPromptLabel(event.key, button);
|
||||
document.removeEventListener('keydown', promptKey);
|
||||
|
@ -254,7 +256,6 @@ function UI(async, buttons, dom, screen, session) {
|
|||
}
|
||||
|
||||
function setTextSpeed(choices) {
|
||||
var currentTextSpeed = session.getOptions().textSpeed;
|
||||
var config = {
|
||||
entries: [],
|
||||
cancel: choices.cancel,
|
||||
|
@ -262,13 +263,13 @@ function UI(async, buttons, dom, screen, session) {
|
|||
};
|
||||
for(var i = 0; i < choices.entries.length; i++) {
|
||||
config.entries[i] = choices.entries[i];
|
||||
if(choices.entries[i].value == currentTextSpeed) {
|
||||
if(choices.entries[i].value == textSpeed) {
|
||||
config.selected = i;
|
||||
}
|
||||
}
|
||||
ask(config)(function(value) {
|
||||
session.setOptions({textSpeed: value});
|
||||
textDelay = textSpeeds[value];
|
||||
session.update({options: {textSpeed: value}});
|
||||
textSpeed = value;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue