From 81f9a91b8748887db1a7b86b8741df7cc5496d55 Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Sun, 19 Jun 2016 11:37:20 +0100 Subject: [PATCH] Refactor some game logic --- src/gui/html/index.html | 2 +- src/gui/html/js/events.js | 35 +------ src/gui/html/js/game.js | 45 ++++++++- src/tests/gui/html/js/test.html | 2 +- src/tests/gui/html/js/test_game.js | 151 +++++++++++++++++++++++++++++ 5 files changed, 199 insertions(+), 36 deletions(-) diff --git a/src/gui/html/index.html b/src/gui/html/index.html index 86edd8e3..fa724982 100644 --- a/src/gui/html/index.html +++ b/src/gui/html/index.html @@ -476,9 +476,9 @@ + - diff --git a/src/gui/html/js/events.js b/src/gui/html/js/events.js index e06a9c1d..d2ebe66c 100644 --- a/src/gui/html/js/events.js +++ b/src/gui/html/js/events.js @@ -134,20 +134,9 @@ function onSortPlugins() { loot.Dialog.showNotification(loot.l10n.translate('Sorting made no changes to the load order.')); return; } - loot.game.oldLoadOrder = loot.game.plugins; - loot.game.loadOrder = []; - result.plugins.forEach((plugin) => { - let existingPlugin = loot.game.plugins.find(item => item.name === plugin.name); - if (existingPlugin) { - existingPlugin.update(plugin); - } else { - existingPlugin = new loot.Plugin(plugin); - } - loot.game.loadOrder.push(existingPlugin); - }); + loot.game.updatePlugins(result.plugins); /* Now update the UI for the new order. */ - loot.game.plugins = loot.game.loadOrder; loot.filters.apply(loot.game.plugins); loot.state.enterSortingState(); @@ -158,23 +147,15 @@ function onSortPlugins() { function onApplySort() { const loadOrder = loot.game.getPluginNames(); return loot.query('applySort', loadOrder).then(() => { - /* Remove old load order storage. */ - delete loot.game.loadOrder; - delete loot.game.oldLoadOrder; + loot.game.applySort(); loot.state.exitSortingState(); }).catch(loot.handlePromiseError); } function onCancelSort() { - return loot.query('cancelSort').then(JSON.parse).then((messages) => { + return loot.query('cancelSort').then(JSON.parse).then(loot.game.cancelSort).then(() => { /* Sort UI elements again according to stored old load order. */ - loot.game.plugins = loot.game.oldLoadOrder; loot.filters.apply(loot.game.plugins); - delete loot.game.loadOrder; - delete loot.game.oldLoadOrder; - - /* Update general messages */ - loot.game.globalMessages = messages; loot.state.exitSortingState(); }).catch(loot.handlePromiseError); @@ -198,16 +179,8 @@ function onClearAllMetadata() { if (!plugins) { return; } - /* Need to empty the UI-side user metadata. */ - plugins.forEach((plugin) => { - const existingPlugin = loot.game.plugins.find(item => item.name === plugin.name); - if (existingPlugin) { - existingPlugin.userlist = undefined; - existingPlugin.editor = undefined; - existingPlugin.update(plugin); - } - }); + loot.game.clearMetadata(plugins); loot.Dialog.showNotification(loot.l10n.translate('All user-added metadata has been cleared.')); }).catch(loot.handlePromiseError); diff --git a/src/gui/html/js/game.js b/src/gui/html/js/game.js index b079127c..e04d127b 100644 --- a/src/gui/html/js/game.js +++ b/src/gui/html/js/game.js @@ -7,16 +7,15 @@ } else { // Browser globals root.loot = root.loot || {}; - root.loot.Game = factory(root.marked); + root.loot.Game = factory(root.marked, root.loot.Plugin); } -}(this, (marked) => class { +}(this, (marked, Plugin) => class { constructor(obj, l10n) { this.folder = obj.folder || ''; this.globalMessages = obj.globalMessages || []; this.masterlist = obj.masterlist || {}; this.plugins = obj.plugins || []; - this.loadOrder = undefined; this.oldLoadOrder = undefined; this._notApplicableString = l10n.translate('N/A'); @@ -290,6 +289,46 @@ return this.plugins.map(plugin => plugin.name); } + setSortedPlugins(plugins) { + this.oldLoadOrder = this.plugins; + this.plugins = []; + + plugins.forEach((plugin) => { + let existingPlugin = this.oldLoadOrder.find(item => item.name === plugin.name); + if (existingPlugin) { + existingPlugin.update(plugin); + } else { + existingPlugin = new Plugin(plugin); + } + this.plugins.push(existingPlugin); + }); + } + + applySort() { + delete this.oldLoadOrder; + } + + cancelSort(globalMessages) { + this.plugins = this.oldLoadOrder; + delete this.oldLoadOrder; + + /* Update general messages */ + this.globalMessages = globalMessages; + } + + clearMetadata(plugins) { + /* Need to empty the UI-side user metadata. */ + plugins.forEach((plugin) => { + const existingPlugin = this.plugins.find(item => item.name === plugin.name); + if (existingPlugin) { + //delete existingPlugin.userlist; + existingPlugin.userlist = undefined; + + existingPlugin.update(plugin); + } + }); + } + static onPluginsChange(evt) { if (!evt.detail.valuesAreTotals) { evt.detail.totalMessageNo += parseInt(document.getElementById('totalMessageNo').textContent, 10); diff --git a/src/tests/gui/html/js/test.html b/src/tests/gui/html/js/test.html index b7b5a7b0..55169a1f 100644 --- a/src/tests/gui/html/js/test.html +++ b/src/tests/gui/html/js/test.html @@ -14,8 +14,8 @@ - + diff --git a/src/tests/gui/html/js/test_game.js b/src/tests/gui/html/js/test_game.js index 55ff5579..4f7672e9 100644 --- a/src/tests/gui/html/js/test_game.js +++ b/src/tests/gui/html/js/test_game.js @@ -455,4 +455,155 @@ describe('Game', () => { game.getPluginNames().should.deepEqual(['foo']); }); }); + + describe('#setSortedPlugins', () => { + let game; + + beforeEach(() => { + game = new loot.Game({}, l10n); + }); + + it('should append new plugins to the plugins array', () => { + game.setSortedPlugins([{ + name: 'foo', + }]); + + game.plugins[0].name.should.equal('foo'); + }); + + it('should update existing plugins with new data', () => { + game._plugins = [new loot.Plugin({ + name: 'foo', + isActive: true, + messages: [{ type: 'warn' }], + })]; + + game.setSortedPlugins([{ + name: 'foo', + crc: 0xDEADBEEF, + }]); + + game.plugins[0].crc.should.equal(0xDEADBEEF); + game.plugins[0].isActive.should.be.true(); + }); + + it('should reorder plugins to given order', () => { + game._plugins = [new loot.Plugin({ + name: 'foo', + }), new loot.Plugin({ + name: 'bar', + })]; + + game.setSortedPlugins([{ + name: 'bar', + }, { + name: 'foo', + }]); + + game.plugins[0].name.should.equal('bar'); + game.plugins[1].name.should.equal('foo'); + }); + + it('should store old load order', () => { + game._plugins = [new loot.Plugin({ + name: 'foo', + }), new loot.Plugin({ + name: 'bar', + })]; + + game.setSortedPlugins([{ + name: 'bar', + }, { + name: 'foo', + }]); + + game.oldLoadOrder[0].name.should.equal('foo'); + game.oldLoadOrder[1].name.should.equal('bar'); + }); + }); + + describe('#applySort', () => { + let game; + + beforeEach(() => { + game = new loot.Game({}, l10n); + }); + + it('should delete the stored old load order', () => { + game.oldLoadOrder = [0, 1, 2]; + + game.applySort(); + + should(game.oldLoadOrder).be.undefined(); + }); + }); + + describe('#cancelSort', () => { + let game; + + beforeEach(() => { + game = new loot.Game({}, l10n); + }); + + it('should set the current load order to the old load order', () => { + game.oldLoadOrder = [0, 1, 2]; + game.plugins = [3, 4, 5]; + + game.cancelSort(); + + game.plugins.should.deepEqual([0, 1, 2]); + }); + + it('should delete the stored old load order', () => { + game.oldLoadOrder = [0, 1, 2]; + + game.cancelSort(); + + should(game.oldLoadOrder).be.undefined(); + }); + + it('should set the global messages to the passed object', () => { + game.oldLoadOrder = [0, 1, 2]; + + game.cancelSort(['foo']); + + game.globalMessages.should.deepEqual(['foo']); + }); + }); + + describe('#clearMetadata', () => { + let game; + + beforeEach(() => { + game = new loot.Game({}, l10n); + }); + + it('should delete stored userlist data for existing plugins', () => { + game._plugins = [new loot.Plugin({ + name: 'foo', + userlist: {}, + })]; + + game.clearMetadata([{ + name: 'foo', + }]); + + should(game.plugins[0].userlist).be.undefined(); + }); + + it('should update existing plugin data', () => { + game._plugins = [new loot.Plugin({ + name: 'foo', + isActive: true, + })]; + + game.clearMetadata([{ + name: 'foo', + crc: 0xDEADBEEF, + }]); + + game.plugins[0].crc.should.equal(0xDEADBEEF); + game.plugins[0].isActive.should.be.true(); + }); + }); });