diff --git a/docs/configuration.md b/docs/configuration.md index 009f54bd..82376b98 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -284,6 +284,7 @@ Defaults to true. ID of the mod pack to use for instances that don't have factorio.mod_pack_id set. Instances without a mod pack of their own fail to start if this is null. +When the controller adds the default mod packs to a new cluster it sets this to the newest Base Game pack if it is null. Defaults to null. diff --git a/packages/controller/src/Controller.ts b/packages/controller/src/Controller.ts index 7b0ef1fa..41eda66e 100644 --- a/packages/controller/src/Controller.ts +++ b/packages/controller/src/Controller.ts @@ -233,6 +233,12 @@ export default class Controller { // Add default mod packs if (modPacks.size === 0) { modPacks.setMany(lib.ModPack.defaultModPacks); + if (config.get("controller.default_mod_pack_id") === null) { + const baseGame = lib.ModPack.defaultModPacks + .filter(pack => pack.name.startsWith("Base Game ")) + .reduce((a, b) => (b.integerFactorioVersion > a.integerFactorioVersion ? b : a)); + config.set("controller.default_mod_pack_id", baseGame.id); + } } // Add default roles diff --git a/test/controller/Controller.js b/test/controller/Controller.js index 9a5908c0..abaff944 100644 --- a/test/controller/Controller.js +++ b/test/controller/Controller.js @@ -93,6 +93,43 @@ describe("controller/src/Controller", function() { assert(loaded.modPacks.size > 0, "default mod packs were not added"); await fs.access(modsDirectory); }); + it("should default to the newest Base Game pack on a new cluster", async function() { + const config = new ControllerConfig("controller", { + "controller.version": controllerVersion, + "controller.database_directory": path.join(dir, "database"), + "controller.mods_directory": path.join(dir, "mods"), + }); + + const loaded = await Controller.fromConfig(config, [], {}); + const id = config.get("controller.default_mod_pack_id"); + assert.equal(loaded.modPacks.get(id).name, "Base Game 2.1"); + }); + it("should keep a set default mod pack when seeding mod packs", async function() { + const config = new ControllerConfig("controller", { + "controller.version": controllerVersion, + "controller.database_directory": path.join(dir, "database"), + "controller.mods_directory": path.join(dir, "mods"), + "controller.default_mod_pack_id": 42, + }); + + await Controller.fromConfig(config, [], {}); + assert.equal(config.get("controller.default_mod_pack_id"), 42); + }); + it("should not set a default mod pack when mod packs exist", async function() { + const databaseDirectory = path.join(dir, "database"); + await fs.mkdir(databaseDirectory); + await fs.writeFile(path.join(databaseDirectory, "mod-packs.json"), JSON.stringify([ + new ModPack().toJSON(), + ])); + const config = new ControllerConfig("controller", { + "controller.version": controllerVersion, + "controller.database_directory": databaseDirectory, + "controller.mods_directory": path.join(dir, "mods"), + }); + + await Controller.fromConfig(config, [], {}); + assert.equal(config.get("controller.default_mod_pack_id"), null); + }); it("should start and stop with an existing database", async function() { this.timeout(10000); const databaseDirectory = path.join(dir, "database");