mirror of
https://github.com/loot/libloot.git
synced 2026-07-27 14:16:01 -07:00
Refactor some game logic
This commit is contained in:
@@ -476,9 +476,9 @@
|
||||
<script src="js/dialog.js"></script>
|
||||
<script src="js/dom.js"></script>
|
||||
<script src="js/events.js"></script>
|
||||
<script src="js/plugin.js"></script>
|
||||
<script src="js/game.js"></script>
|
||||
<script src="js/translateStaticText.js"></script>
|
||||
<script src="js/plugin.js"></script>
|
||||
<script src="js/query.js"></script>
|
||||
<script src="js/handlePromiseError.js"></script>
|
||||
<script src="js/filters.js"></script>
|
||||
|
||||
@@ -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);
|
||||
|
||||
+42
-3
@@ -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);
|
||||
|
||||
@@ -14,8 +14,8 @@
|
||||
<script>mocha.setup('bdd')</script>
|
||||
<script src="mock_dom.js"></script>
|
||||
<script src="../../../../gui/html/js/filters.js"></script>
|
||||
<script src="../../../../gui/html/js/game.js"></script>
|
||||
<script src="../../../../gui/html/js/plugin.js"></script>
|
||||
<script src="../../../../gui/html/js/game.js"></script>
|
||||
<script src="../../../../gui/html/js/query.js"></script>
|
||||
<script src="../../../../gui/html/js/state.js"></script>
|
||||
<script src="../../../../gui/html/js/translator.js"></script>
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user