From 8996671beac0a5f76e20c1628e65dc3dc9f84ee8 Mon Sep 17 00:00:00 2001 From: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> Date: Fri, 14 Feb 2025 14:47:26 +0000 Subject: [PATCH] Add default mod packs (#735) --- packages/controller/src/Controller.ts | 4 ++++ packages/lib/src/data/ModPack.ts | 21 +++++++++++++++++++++ packages/lib/src/datastore.ts | 5 +++++ test/integration/clusterio.js | 9 ++++++++- test/lib/data/ModPack.js | 18 ++++++++++++++++++ test/lib/datastore.js | 16 ++++++++++++++++ 6 files changed, 72 insertions(+), 1 deletion(-) diff --git a/packages/controller/src/Controller.ts b/packages/controller/src/Controller.ts index 1394053c..1517677a 100644 --- a/packages/controller/src/Controller.ts +++ b/packages/controller/src/Controller.ts @@ -127,6 +127,10 @@ export default class Controller { lib.ModPack.fromJSON.bind(lib.ModPack) ).bootstrap()); + if (modPacks.size === 0) { + modPacks.setMany(lib.ModPack.defaultModPacks); + } + const userManager = new UserManager(config); await userManager.load(path.join(databaseDirectory, "users.json")); diff --git a/packages/lib/src/data/ModPack.ts b/packages/lib/src/data/ModPack.ts index df140c8b..26fe2565 100644 --- a/packages/lib/src/data/ModPack.ts +++ b/packages/lib/src/data/ModPack.ts @@ -383,6 +383,27 @@ export default class ModPack { } } + /** Array of default mod packs which should exist on a newly installed cluster */ + static defaultModPacks = [ + ...["0.17", "0.18", "1.0", "1.1", "2.0"] + .map(version => this.fromJSON({ + name: `Base Game ${version}`, + description: `Factorio ${version} with no extra mods.`, + factorio_version: version, + } as any)), + ...["2.0"] + .map(version => this.fromJSON({ + name: `Space Age ${version}`, + description: `Factorio ${version} with Space Age expansion.`, + factorio_version: version, + mods: [ + { name: "elevated-rails", enabled: true, version }, + { name: "quality", enabled: true, version }, + { name: "space-age", enabled: true, version }, + ], + } as any)), + ]; + /** * Returns an array of ModInfo containing all mods included in factorio * diff --git a/packages/lib/src/datastore.ts b/packages/lib/src/datastore.ts index 4da49273..213ab262 100644 --- a/packages/lib/src/datastore.ts +++ b/packages/lib/src/datastore.ts @@ -189,6 +189,11 @@ export abstract class Datastore< return this.data.get(key); } + // Allow getting the size of the datastore + get size() { + return this.data.size; + } + // Allow iterating through the datastore like a Map. [Symbol.iterator](): IterableIterator<[K, Readonly]> { return this.data.entries(); diff --git a/test/integration/clusterio.js b/test/integration/clusterio.js index 872ee34a..12cdc886 100644 --- a/test/integration/clusterio.js +++ b/test/integration/clusterio.js @@ -1072,8 +1072,15 @@ describe("Integration of Clusterio", function() { }); describe("mod-pack list", function() { + let result = null; it("runs", async function() { - await execCtl("mod-pack list"); + result = await execCtl("mod-pack list"); + }); + it("contains defaults", function() { + assert(result !== null, "Failed to return a value"); + const stdout = result.stdout.trim(); + assert(stdout.indexOf("Base Game") >= 0, "No base game pack"); + assert(stdout.indexOf("Space Age") >= 0, "No space age pack"); }); }); diff --git a/test/lib/data/ModPack.js b/test/lib/data/ModPack.js index 4cd8a5fd..324edf15 100644 --- a/test/lib/data/ModPack.js +++ b/test/lib/data/ModPack.js @@ -378,6 +378,24 @@ describe("lib/data/ModPack", function() { /* eslint-enable indent */ }); }); + describe("defaultModPacks", function() { + it("should contain at least one base game only", function() { + const modPack = ModPack.defaultModPacks.find(pack => { + const base = pack.mods.get("base"); + const spaceAge = pack.mods.get("space-age"); + return base && base.enabled && (!spaceAge || !spaceAge.enabled); + }); + assert(modPack, "Does not contain base game only"); + }); + it("should contain at least one space age expansion", function() { + const modPack = ModPack.defaultModPacks.find(pack => { + const base = pack.mods.get("base"); + const spaceAge = pack.mods.get("space-age"); + return base && base.enabled && spaceAge && spaceAge.enabled; + }); + assert(modPack, "Does not contain space age expansion"); + }); + }); describe("getBuiltinMods()", function() { it("should work with versions before 2.0", function() { const builtinMods = ModPack.getBuiltinMods("1.1"); diff --git a/test/lib/datastore.js b/test/lib/datastore.js index 6d44bdaa..f64fe3a0 100644 --- a/test/lib/datastore.js +++ b/test/lib/datastore.js @@ -413,6 +413,22 @@ describe("lib/datastore", function() { }); }); + describe("size", function() { + it("returns the correct size", function() { + assert.equal(datastore.size, 3); + }); + }); + + describe("iterator", function() { + it("iterates all values", function() { + const entries = []; + for (const entry of datastore) { + entries.push(entry); + } + assert.deepEqual(entries, [...datastoreProvider.value.entries()]); + }); + }); + describe("values", function() { it("returns all values", function() { assert.deepEqual([...datastore.values()], [...datastoreProvider.value.values()]);