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'
|
name: 'textSpeed'
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
var game = session.getGame();
|
var game = session.get('game');
|
||||||
if(game != undefined) {
|
if(game != undefined) {
|
||||||
menus.start.entries.unshift(
|
menus.start.entries.unshift(
|
||||||
{label: 'Continue', action: function() { messaging.send({tag: 'Resume', game: game}); }}
|
{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');
|
ui.frame('GameInit');
|
||||||
messaging.send({tag: 'NewGame'});
|
messaging.send({tag: 'NewGame'});
|
||||||
async.run(
|
async.run(
|
||||||
session.update(),
|
session.syncGame(),
|
||||||
async.bind(
|
async.bind(
|
||||||
askName(),
|
askName(),
|
||||||
set('name'),
|
set('name'),
|
||||||
|
|
|
@ -13,7 +13,7 @@ function Buttons(session) {
|
||||||
};
|
};
|
||||||
|
|
||||||
document.addEventListener('keydown', function(event) {
|
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) {
|
if(button != undefined && mapping[button] != undefined) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
mapping[button]();
|
mapping[button]();
|
||||||
|
@ -30,7 +30,7 @@ function Buttons(session) {
|
||||||
|
|
||||||
function assign(key, button) {
|
function assign(key, button) {
|
||||||
var layoutDiff = {};
|
var layoutDiff = {};
|
||||||
var layout = session.getOptions().layout;
|
var layout = session.get('options').layout;
|
||||||
for(var currentKey in layout) {
|
for(var currentKey in layout) {
|
||||||
if(layout[currentKey] == button) {
|
if(layout[currentKey] == button) {
|
||||||
layoutDiff[currentKey] = undefined;
|
layoutDiff[currentKey] = undefined;
|
||||||
|
|
|
@ -1,33 +1,83 @@
|
||||||
function Session(async) {
|
function Session(async) {
|
||||||
var game = JSON.parse(localStorage.getItem('game'));
|
var session = {
|
||||||
var options = JSON.parse(localStorage.getItem('options')) || defaultOptions();
|
character: JSON.parse(localStorage.getItem('character')),
|
||||||
|
game: JSON.parse(localStorage.getItem('game')),
|
||||||
|
options: JSON.parse(localStorage.getItem('options')) || defaultOptions()
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
getOptions: getOptions,
|
get: get,
|
||||||
getGame: getGame,
|
set: set,
|
||||||
save: save,
|
syncGame: syncGame,
|
||||||
setOptions: setOptions,
|
|
||||||
update: update
|
update: update
|
||||||
};
|
};
|
||||||
|
|
||||||
function getGame() {
|
function defaultOptions() {
|
||||||
return game;
|
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(
|
return async.bind(
|
||||||
messaging.receive(function(message) {
|
messaging.receive(function(message) {
|
||||||
return message.tag == "Game";
|
return message.tag == "Game";
|
||||||
}),
|
}),
|
||||||
function(message) {
|
function(message) {
|
||||||
game = message.contents;
|
session.game = message.contents;
|
||||||
return async.wrap();
|
return async.wrap();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function save() {
|
function update(newSession) {
|
||||||
localStorage.setItem('game', JSON.stringify(game));
|
for(var key in newSession) {
|
||||||
|
merge(session[key], newSession[key]);
|
||||||
|
save(key);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function merge(a, b) {
|
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,
|
medium: 50,
|
||||||
fast: 20
|
fast: 20
|
||||||
};
|
};
|
||||||
var textDelay = textSpeeds[session.getOptions().textSpeed];
|
var textSpeed = session.get('options').textSpeed;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
animation: animation,
|
animation: animation,
|
||||||
|
@ -148,7 +148,9 @@ function UI(async, buttons, dom, screen, session) {
|
||||||
var ready = false;
|
var ready = false;
|
||||||
var append = function(c) { return function() { screen.appendText(c); }; };
|
var append = function(c) { return function() { screen.appendText(c); }; };
|
||||||
for(var i = 0; i < characters.length; i++) {
|
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});
|
frames.push({action: function() { ready = true; screen.markAsRead(); }, delay: null});
|
||||||
var remote = animation(frames);
|
var remote = animation(frames);
|
||||||
|
@ -210,7 +212,7 @@ function UI(async, buttons, dom, screen, session) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function setLayout() {
|
function setLayout() {
|
||||||
var layout = session.getOptions().layout
|
var layout = session.get('options').layout
|
||||||
var buttonsMenu = {
|
var buttonsMenu = {
|
||||||
cancel: function() {},
|
cancel: function() {},
|
||||||
entries: [],
|
entries: [],
|
||||||
|
@ -235,7 +237,7 @@ function UI(async, buttons, dom, screen, session) {
|
||||||
screen.currentMenu().getElementsByClassName('selected')[0].textContent = keyPromptLabel(null, button);
|
screen.currentMenu().getElementsByClassName('selected')[0].textContent = keyPromptLabel(null, button);
|
||||||
var promptKey = function(event) {
|
var promptKey = function(event) {
|
||||||
var assignment = buttons.assign(event.key, button);
|
var assignment = buttons.assign(event.key, button);
|
||||||
session.setOptions({layout: assignment.layoutDiff});
|
session.update({options: {layout: assignment.layoutDiff}});
|
||||||
screen.clear(elem);
|
screen.clear(elem);
|
||||||
screen.currentMenu().getElementsByClassName('selected')[0].textContent = keyPromptLabel(event.key, button);
|
screen.currentMenu().getElementsByClassName('selected')[0].textContent = keyPromptLabel(event.key, button);
|
||||||
document.removeEventListener('keydown', promptKey);
|
document.removeEventListener('keydown', promptKey);
|
||||||
|
@ -254,7 +256,6 @@ function UI(async, buttons, dom, screen, session) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function setTextSpeed(choices) {
|
function setTextSpeed(choices) {
|
||||||
var currentTextSpeed = session.getOptions().textSpeed;
|
|
||||||
var config = {
|
var config = {
|
||||||
entries: [],
|
entries: [],
|
||||||
cancel: choices.cancel,
|
cancel: choices.cancel,
|
||||||
|
@ -262,13 +263,13 @@ function UI(async, buttons, dom, screen, session) {
|
||||||
};
|
};
|
||||||
for(var i = 0; i < choices.entries.length; i++) {
|
for(var i = 0; i < choices.entries.length; i++) {
|
||||||
config.entries[i] = choices.entries[i];
|
config.entries[i] = choices.entries[i];
|
||||||
if(choices.entries[i].value == currentTextSpeed) {
|
if(choices.entries[i].value == textSpeed) {
|
||||||
config.selected = i;
|
config.selected = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ask(config)(function(value) {
|
ask(config)(function(value) {
|
||||||
session.setOptions({textSpeed: value});
|
session.update({options: {textSpeed: value}});
|
||||||
textDelay = textSpeeds[value];
|
textSpeed = value;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue